如何将Pandas Column Value'爆炸'到唯一的行

时间:2018-03-09 13:27:12

标签: python pandas

所以,我的意思是爆炸是这样的,我想转换一些数据帧,如:

ID | Name     | Food            |   Drink
1    John       Apple, Orange      Tea , Water
2    Shawn                         Milk    
3    Patrick    Chichken
4    Halley     Fish Nugget

进入此数据框:

ID  | Name     | Order Type    | Items
1     John        Food           Apple
2     John        Food           Orange
3     John        Drink          Tea
4     John        Drink          Water
5     Shawn       Drink          Milk
6     Pattrick    Food           Chichken

我不知道如何实现这一目标。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

IIUC stack有不必要的流程,在这里我不会更改ID,我认为保持原来的更好

s=df.set_index(['ID','Name']).stack()

pd.DataFrame(data=s.str.split(',').sum(),index=s.index.repeat(s.str.split(',').str.len())).reset_index()
Out[289]: 
   ID     Name level_2            0
0   1     John    Food        Apple
1   1     John    Food       Orange
2   1     John   Drink          Tea
3   1     John   Drink        Water
4   2    Shawn   Drink         Milk
5   3  Patrick    Food     Chichken
6   4   Halley    Food  Fish Nugget


# if you need rename the column to item try below
#pd.DataFrame(data=s.str.split(',').sum(),index=s.index.repeat(s.str.split(',').str.len())).rename(columns={0:'Item'}).reset_index()

答案 1 :(得分:0)

您可以使用pd.melt将数据从宽格式转换为长格式。我认为这将更容易逐步理解。

# first split into separate columns
df[['Food1','Food2']] = df.Food.str.split(',', expand=True)
df[['Drink1','Drink2']] = df.Drink.str.split(',', expand=True)

# now melt the df into long format
df = pd.melt(df, id_vars=['Name'], value_vars=['Food1','Food2','Drink1','Drink2'])

# remove unwanted rows and filter data
df = df[df['value'].notnull()].sort_values('Name').reset_index(drop=True)

# rename the column names and values
df.rename(columns={'variable':'Order Type', 'value':'Items'}, inplace=True)
df['Order Type'] = df['Order Type'].str.replace('\d','')

# output
print(df)

    Name    Order Type  Items
0   Halley  Food        Fish Nugget
1   John    Food        Apple
2   John    Food        Orange
3   John    Drink       Tea
4   John    Drink       Water
5   Patrick Food        Chichken
6   Shawn   Drink       Milk