Reputation: 885
I have multiple dataframes that looks like:
0 1 2 3 4 5
Groupe 1 2 3 4 5 6
1 3 6 8 6 8 9
2 5 5 2 1 1 0.1
3 7 2 8 1.9 11 2.1
4 1 2 1 1.1 2 1
So the columns names are [0, 1, 2, 3, 4, 5] but I'd like to put the values of Groupe
row as columns name, because it's interesting in the context that I analyze the data.
Remarque: the values of Groupe
row could change, so it's not necessarily 1, 2, 3. so the solution could not be just as trivial as to add +1 to actual column names.
the desired output for the dataframe given above is:
1 2 3 4 5 6
1 3 6 8 6 8 9
2 5 5 2 1 1 0.1
3 7 2 8 1.9 11 2.1
4 1 2 1 1.1 2 1
So how can I change the columns names for values of some row ?
Upvotes: 1
Views: 111
Reputation: 862581
If row Groupe
is first row of df
, you can first set column names by first row selected by iloc
, then convert it to int
by astype
, remove column name and last remove first row of DataFrame with [1:]
:
df.columns = df.iloc[0,:].astype(int)
df.columns.name= None
print (df[1:])
1 2 3 4 5 6
1 3 6 8 6.0 8 9.0
2 5 5 2 1.0 1 0.1
3 7 2 8 1.9 11 2.1
4 1 2 1 1.1 2 1.0
Another solution with loc
and drop
, row Groupe
in df
can be anywhere, not only first:
df.columns = df.loc['Groupe',:].astype(int)
df.columns.name= None
df = df.drop('Groupe')
print (df)
1 2 3 4 5 6
1 3 6 8 6.0 8 9.0
2 5 5 2 1.0 1 0.1
3 7 2 8 1.9 11 2.1
4 1 2 1 1.1 2 1.0
Upvotes: 1