Pandas:重塑Dataframe以将多个列压缩为单行值

时间:2018-02-06 17:53:47

标签: python pandas dataframe reshape

我有以下数据框

      pointId    april august december  february    \
0     307      None   None     None       NaN    
1     307      None   None     None       NaN   
2     307      None   None     None       NaN    
3     307      None   None     None      0.88     
4     307      None   None     None      0.60     
   january  july  june  march   may november october september  year  
0      NaN  None  None    NaN  None     None    None      None  2014  
1      NaN  None  None    NaN  None     None    None      None  2015  
2      NaN  None  None    NaN  None     None    None      None  2016  
3      0.7  None  None    1.1  None     None    None      None  2017  
4      0.5  None  None    NaN  None     None    None      None  2018

对于特定的pointId,它在给定年份的月份列中基本上有一些值 我需要对其进行整形,以便将12列压缩到一个日期列中。此列将包含给定值的月份的最后日期。所以我需要在months列中为给定值添加一行。结果数据框应如下所示:

      pointId     Date         Value
0     307        01/31/2017     0.7
1     307        02/28/2017     0.88
2     307        03/31/2017     1.1
3     307        01/31/2018     0.5
4     686307     02/28/2018     0.6

像往常一样,感谢我们的帮助。如果没有SO,我就不会在工作中获得:)

1 个答案:

答案 0 :(得分:3)

使用stack,下一步只需将年份,月份转换为月末

df.set_index(['pointId','year']).replace('None',np.nan).stack()
Out[1127]: 
pointId  year          
307      2017  february    0.88
               january     0.70
               march       1.10
         2018  february    0.60
               january     0.50
dtype: float64

更新

s=df.set_index(['pointId','year']).replace('None',np.nan).stack().reset_index()

s=s.replace({'february':2,'january':1,'march':3})
from pandas.tseries.offsets import MonthEnd
s['Date']=pd.to_datetime(s.year*10+s.level_2,format='%Y%m')+MonthEnd(1)

s.drop(['year','level_2'],1).rename(columns={0:'Value'})
Out[1143]: 
   pointId  Value       Date
0      307   0.88 2017-02-28
1      307   0.70 2017-01-31
2      307   1.10 2017-03-31
3      307   0.60 2018-02-28
4      307   0.50 2018-01-31