简体   繁体   中英

Pandas dataframe: turn date in column into value in row

I'm trying to turn the following dataframe (with values for county and year)

county  region     2012  2013  ...  2035
A       101        10    15    ...  7
B       101        13    8     ...  11
...

into a dataframe that looks like this:

county  region   year  sum
A       101      2012  10
A       101      2013  15
...     ...      ...   ...
A       101      2035  7
B       101      2012  13
B       101      2013  8
B       101      2035  11

My current dataframe has 400 rows (different counties) with values for the years 2012-2035.

My manual approach would be to slice the year columns off and put each of them below the last row of the preceding year. But of course there has to be a pythonic way.

I guess I'm missing a basic pandas concept here, probably I just couldn't find the right answer to this problem because I simply didn't know how to ask the right question. Please be gentle with the newcomer.

You can use melt from pandas:

In [26]: df
Out[26]:
  county  region  2012  2013
0      A     101    10    15
1      B     101    13     8

In [27]: pd.melt(df, id_vars=['county','region'], var_name='year', value_name='sum')
Out[27]:
  county  region  year  sum
0      A     101  2012   10
1      B     101  2012   13
2      A     101  2013   15
3      B     101  2013    8

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