I have a txt file that is in the format
I want to load this into a data frame, with columns as headers and values as rows.
I have a txt file that is in the format
I want to load this into a data frame, with columns as headers and values as rows.
You can use pd.read_csv
with sep = " "
:
df = pd.read_csv({FILE_PATH}.txt, sep=' ', header=None)
df.set_index(0).T
In your case sep
turned out to be \t
. Besides that manually changing some rows is a better idea. I changed Weather conditions
to Weather_conditions
, and Time_taken (min)
to Time_taken_(min)
. I also skipped reading the last row that would cause too hassle:
df = pd.read_csv({FILE_PATH}.txt, sep='\t', header=None)
read_those = len(df) - 1
# We read again after learning how much rows the dataframe has, and skip
# reading the last row, note that I also add *nrows* parameter here:
df = pd.read_csv({FILE_PATH}.txt, sep='\t', header=None, nrows=read_those)
df = df[0].str.split(expand=True).set_index(0).T
import pandas as pd
df = pd.read_csv('0.txt', sep='\t', header=None)
df = df[0].str.split(n=1, expand=True)
df = df.T
df.columns = df.iloc[0]
df = df[1:]
with pd.option_context('display.max_rows', None, 'display.max_columns', None):
print(df)