多列融化Df

时间:2018-11-29 01:11:21

标签: python pandas

我有以下DF

ID,     1,      2,      3              #Columns 
0,Date, Review, Average, Review # Observations
1,01/01/18 2,   4,      3      # Date and Review Score
2,02/01/18 1,   2,      4      #Date and Review Score

我正在尝试将此DF分解为以下内容,使用以下代码将我关闭:

df = pd.melt(df,id_vars=['ID'],var_name=['Store'],value_name='Score').fillna(0).set_index('ID')

此程序:

           Store    Score
ID      
Date        
01/01/18    1       Review
01/01/18    1       2
02/01/18    1       1

我想做的是删除“评论”并将其放在自己的列中,如下所示:

           Store    Review Type Score
ID      
Date        
01/01/18    1,      Review,    1
02/01/18    1,      Review,    2

我尝试做宽到长的操作,但是我认为我需要在这里使用某种程度的多索引,否则我可能会考虑过度。

注意事项:

我的DF长824列,324行 我的变量是按行排列的,沿着日期,以ID为列标题。

1 个答案:

答案 0 :(得分:1)

如果我了解您在寻找什么...

从此数据帧开始,我相信您所拥有的是

    ID           1         2       3
0   Date       Review   Average   Review
1   01/01/18     2         4       3
2   02/01/18     1         2       4

假设您完成了pd.melt(),然后留下了:

new_df = pd.melt(df,id_vars=['ID'],var_name=['Store'],value_name='Score').fillna(0).set_index('ID')

          Store    Score
ID      
Date        1      Review
01/01/18    1      2
02/01/18    1      1
Date        2      Average
01/01/18    2      4
02/01/18    2      2
Date        3      Review
01/01/18    3      3
02/01/18    3      4

然后您可以执行以下操作:

# sort index so all the 'Date' values are at the bottom
new_df.sort_index(inplace=True) 

# create a new df of just the dates becuase that is your review types
review_types = new_df.loc['Date']

# rename column to review types
review_types.rename(columns={'Score':'Review Type'}, inplace=True)

# remove new_df.loc['Date']
# new_df = new_df.drop(new_df.tail(len(review_types)).index).reset_index()

# UPDATED removal of new_df.loc['Date']
# I recommend removing the date values by doing this and not using .tail()
new_df = new_df[~new_df.index.str.contains('Date')].reset_index()

# rename ID column to Date
new_df.rename(columns={'ID':'Date'}, inplace=True)

# merge your two dataframes together
new_df.merge(review_types, on='Store')

为您提供:

    Date      Store  Score  Review Type
0   01/01/18    1     2     Review
1   02/01/18    1     1     Review
2   01/01/18    2     4     Average
3   02/01/18    2     2     Average
4   01/01/18    3     3     Review
5   02/01/18    3     4     Review