使用groupBy列出的熊猫列

时间:2020-03-27 15:09:03

标签: python pandas list dataframe

简单的数据框,我想使用GroupBy生成列表。

enter image description here

import pandas as pd

data = {'Name': ["David","David","David","David","Kate","Kate","Kate","Kate","Kate","Kate"], 
'Truck' :     ["PLO8318","PLO6372","PLO6473","PLO8249","PLO6540","PLO7623","PLO8036","PLO6314","PLO7791","PLO7848"],
}
df = pd.DataFrame(data)

df_1 = df.groupby('Name')['Truck'].apply(list)
df_1 = df_1.to_frame().reset_index()

for index, row in df_1.iterrows():
    print (row['Name'], row['Truck'])

输出:

('David', ['PLO8318', 'PLO6372', 'PLO6473', 'PLO8249'])
('Kate', ['PLO6540', 'PLO7623', 'PLO8036', 'PLO6314', 'PLO7791', 'PLO7848'])

我如何打印输出:

David = ['PLO8318', 'PLO6372', 'PLO6473', 'PLO8249']
Kate = ['PLO6540', 'PLO7623', 'PLO8036', 'PLO6314', 'PLO7791', 'PLO7848']

0 个答案:

没有答案