将多个字段转换为SIngle字段

时间:2019-01-05 19:15:03

标签: python pandas

我有这个Pandas DataFrame

 library("readxl") 
 df <-read_excel("https://www.eia.gov/dnav/pet/hist_xls/W_EPC0_SAX_YCUOK_MBBLw.xls",sheet="Data 1")

我想将其转换为

path

最优化的实现方式是什么?

2 个答案:

答案 0 :(得分:4)

我认为这应该是melt的问题

newdf=df.melt(['a','b']).sort_values('b')
newdf
   a     b variable  value
0  1  2022        c     11
2  1  2022        d     12
4  1  2022        e     13
6  1  2022        f     14
8  1  2022        g     15
1  2  2023        c     17
3  2  2023        d     22
5  2  2023        e     23
7  2  2023        f     24
9  2  2023        g     25

如果您需要这里的0-n

newdf.a=newdf.index+1

答案 1 :(得分:4)

df.drop('a', axis=1)\  # No need for the original 'a`
  .set_index('b')\ # Make 'b' the index
  .stack()\ # Convert from "broad" to "tall"
  .reset_index()\ # Convert the index back to a regular column
  .drop('level_1', axis=1)\ # Remove the residual original column names
  .rename({0: 'c'}, axis=1) # Rename the new column to 'c'
#      b   c
#0  2022  11
#1  2022  12
#2  2022  13
#3  2022  14
#4  2022  15
#5  2023  17
#6  2023  22
#7  2023  23
#8  2023  24
#9  2023  25