Laurent Cesaro
Laurent Cesaro

Reputation: 341

Python Dataframe rename column compared to value

With Pandas I'm trying to rename unnamed columns in dataframe with values on the first ligne of data.

My dataframe:

id store unnamed: 1 unnamed: 2 windows unnamed: 3 unnamed: 4
0 B1 B2 B3 B1 B2 B3
1 2 c 12 15 15 14
2 4 d 35 14 14 87

My wanted result:

id store_B1 store_B3 store_B2 windows_B1 windows_B2 windows_B3
0 B1 B2 B3 B1 B2 B3
1 2 c 12 15 15 14
2 4 d 35 14 14 87

I don't know how I can match the column name with the value in my data. Thanks for your help. Regards

Upvotes: 1

Views: 239

Answers (1)

user17242583
user17242583

Reputation:

You can use df.columns.where to make unnamed: columns NaN, then convert it to a Series and use ffill:

df.columns = pd.Series(df.columns.where(~df.columns.str.startswith('unnamed:'))).ffill() + np.where(~df.columns.isin(['id','col2']), ('_' + df.iloc[0].astype(str)).tolist(), '')

Output:

>>> df
   id store_B1 store_B2 store_B3 windows_B1 windows_B2 windows_B3
0   0       B1       B2       B3         B1         B2         B3
1   1        2        c       12         15         15         14
2   2        4        d       35         14         14         87

Upvotes: 4

Related Questions