I have over 100 csv files, all contain a table with the same indexes and indentical length. I am able to concat them into a single dataframe and save as csv.
print(df)
a b c d
0 1 5 1 7 7
1 2 3 1 7 7
2 3 2 2 8 7
3 1 6 1 7 6
I want to add a name to the now blank 'initial index' but every solution I find doesn't work... I've tried:
df.rename(columns={'': 'initial'}, inplace=True)
df1 = df.rename(columns={'': 'initial'})
df1 = df.set_index('initial')
df1 = df.rename_axis("initial")
Header printout as is:
print([*df])
[' a b c d']
I am using Python 3.7.3
Edit
Thanks for the feedback. Both Joe Ferndz and icecity96 solutions work. They didn't initially only because .csv file columns were seperated with spaces not comma's, therefore the DataFrame has seen only one column. Problem gone away after adding delim_whitespace=True
pd.read_csv(file, delim_whitespace=True)
Output before:
print(*df)
[' a b c d']
Output after:
print([*df])
['a', 'b', 'c', 'd']
print([*df]), unless somehow you have a single column with the label' a b c d'and everything happened to pretty print perfectly. What isdf.index?