Pandas- 分组字符串数值

huangapple go评论62阅读模式
英文:

Pandas- group string values

问题

以下是您要翻译的内容:

I have following dataset:

df = pd.DataFrame({"booking": ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'C', 'C', 'C'],
"city": ['a', 'b', 'c', 'c', 'a', 'a', 'b', 'b', 'c', 'd', 'e'],
"orders": [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10],})

Need to group values to see: what is the sum of orders for each city, I think the best way is to create new DataFrame with two columns:

  1. list of cities
  2. list with SUM of order to each city

If with 2nd point is clear:

new_df1=df.groupby(['booking', 'city'])['orders'].apply(sum).groupby(level=0).apply(list)
print(new_df1)

But with 1st point is not clear, need to get list of unique cities for each booking, like:

needed_df = pd.DataFrame({"booking": ['A', 'B', 'C'],
"city_list": [['a', 'b', 'c'], ['a', 'b'], ['c', 'd', 'e']]})

英文:

I have following dataset:

df = pd.DataFrame({"booking": ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'C', 'C', 'C'],
               "city": ['a', 'b', 'c', 'c', 'a', 'a', 'b', 'b', 'c', 'd', 'e'],
               "orders": [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10],})

Need to group values to see: what is the sum of orders for each city, I think the best way is to create new DataFrame with two columns:

  1. list of cities
  2. list with SUM of order to each city

If with 2nd point is clear:

new_df1=df.groupby(['booking', 'city'])['orders'].apply(sum).groupby(level=0).apply(list)
print(new_df1)

But with 1st point is not clear, need to get list of unique cities for each booking, like:

needed_df = pd.DataFrame({"booking": ['A', 'B', 'C'],
               "city_list": [['a', 'b', 'c'], ['a', 'b'], ['c', 'd', 'e']]})

答案1

得分: 1

尝试使用 unique

out = df.groupby('booking')['city'].agg(pd.unique).reset_index()
Out[7]:
booking city
0 A [a, b, c]
1 B [a, b]
2 C [c, d, e]

英文:

Try with unique

out = df.groupby('booking')['city'].agg(pd.unique).reset_index()
Out[7]: 
  booking       city
0       A  [a, b, c]
1       B     [a, b]
2       C  [c, d, e]

huangapple
  • 本文由 发表于 2023年5月22日 21:59:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定