动态爆炸Pandas行中的列

时间:2018-04-02 21:40:25

标签: python python-3.x python-2.7 pandas dataframe

我有一个FileName = '../../Datasets/hc-1/d13921/d1392101.dat',每行有多个列,需要按列拆分为新行

DataFrame当前(缩短版)看起来像这样

DataFrame

对于每个|---------------------------------------------------------------------------------------------------------------------------------| |institution_short_name |interest_paid1 |interest_paid2 |product_detail_value_min |term_01m_value |term_02y_value |term_03m_value | |---------------------------------------------------------------------------------------------------------------------------------| |One |Z |Q |2000 |0.50 |0.75 |0.75 | |One |Z |Q |5000 |0.50 |3.65 |3.75 | |One |M |M |20000 |Nan |3.65 |Nan | |---------------------------------------------------------------------------------------------------------------------------------| 列,例如,我想从列的名称中提取01m,02y,03m以及该列下该行的值,并将其爆炸如下:

term

我不是要求一个完整的解决方案,只是让我开始的事情。谢谢!

1 个答案:

答案 0 :(得分:2)

您可以查看wide_to_long

pd.wide_to_long(df.repalce('Nan',np.nan),['term'],i=['institution_short_name','interest_paid1','interest_paid2','product_detail_value_min'],j='Term',suffix='\\w+').reset_index().dropna()