【发布时间】:2019-09-26 21:21:34
【问题描述】:
我有一个数据框,我想将其中的一列作为字符串列表获取,以便从以下内容中获取:
df = pd.DataFrame({'customer':['a','a','a','b','b'],
'location':['1','2','3','4','5']})
我可以得到如下数据框:
a ['1','2','3']
b ['4','5']
其中一列是客户,另一列是他们所在位置的字符串列表。
我已经尝试过 df.astype(str).values.tolist() 但我似乎无法通过 groupby 来获取每个客户的列表。
【问题讨论】:
-
df.groupby('customer')['location'].apply(list)? -
这样,但我不能这样做,因为那时我没有字符串列表
标签: python-3.x string pandas list