I currently have a dataframe that looks like this:
Unnamed: 1 Unnamed: 2 Unnamed: 3 Unnamed: 4
0 Sample Number Group Number Sample Name Group Name
1 1.0 1.0 s_1 g_1
2 2.0 1.0 s_2 g_1
3 3.0 1.0 s_3 g_1
4 4.0 2.0 s_4 g_2
I'm looking for a way to delete the header row and make the first row the new header row, so the new dataframe would look like this:
Sample Number Group Number Sample Name Group Name
0 1.0 1.0 s_1 g_1
1 2.0 1.0 s_2 g_1
2 3.0 1.0 s_3 g_1
3 4.0 2.0 s_4 g_2
I've tried stuff along the lines of if 'Unnamed' in df.columns:
then make the dataframe without the header df.to_csv(newformat,header=False,index=False)
but I don't seem to be getting anywhere.
If you want a one-liner, you can do:
--another way to do this
If you like it hit up arrow. Thanks
The dataframe can be changed by just doing
Then
Should do the trick.
@ostrokach answer is best. Most likely you would want to keep that throughout any references to the dataframe, thus would benefit from inplace = True.
df.rename(columns=df.iloc[0], inplace = True) df.drop([0], inplace = True)