简体   繁体   中英

Pandas convert dataframe values to column names

I want to use dataframe values as column names and simplify the dataframe.

I tried df.stack() and then index.map('{0[0]}_{0[1]}'.format)

Input_df(Got this df by doing a groupby):

link price  date
 A     1    01/01
 A     2    01/02
 A     1.2  01/03

Desired_ouput:

link price_01/01 price_01/02 price_01/03
  A      1            2         1.2

Forget mapping over a multiindex

df.set_index(['link', 'date']).price.unstack().add_prefix('price_')

date  price_01/01  price_01/02  price_01/03
link                                       
A             1.0          2.0          1.2

You can try this using set_index and unstack , then using Python 3.6+ can you use f-string with list comprehension to flatten multiindex column headers.

df_out = df.set_index(['link', 'date']).unstack()

df_out.columns = [f'{i}_{j}' for i, j in df_out.columns]

df_out.reset_index()

Output:

  link  price_01/01  price_01/02  price_01/03
0    A          1.0          2.0          1.2

You can pivot your table:

df['date'] = 'price_' + df['date']
df.reset_index(inplace=True)
df = df.pivot(index='link', columns='date', values='price')

print(df)

Output:

date  price_01/01  price_01/02  price_01/03
link                                       
A             1.0          2.0          1.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM