如何将`str.join`应用于包含整数和字符串的分组列

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

How to apply str.join to a groupby column that contains integers and strings

问题

我有一段代码,它导入一个文件并将数据水平连接起来。我的输入文件看起来像这样:

|  X   |    Y |
|------|------|
|  a |  hello |
|  a |      3 |
|  a |    bye |
|  a |     hi |
|  b |  apple |
|  b | orange |
|  b |      4 |

我需要的输出如下:

| X | Y |
|---|---|
| a | hello,3,bye,hi |
| b | apple,orange,4 |

我在Jupyter中使用以下Python代码:

import pandas as pd
df = pd.DataFrame({"X": ["a", "a", "a", "a", "b", "b", "b"],
                   "Y": ["hello", 3, "bye", "hi", "apple", "orange", 4]})

orden = df.groupby('X').Y.apply(','.join)

错误:TypeError: sequence item 0: expected str instance, int found

我已经验证了其他数据,我怀疑问题出在整数上。如何改进我的代码以使它也可以连接数字和字符串?

英文:

I have code that imports a file and concatenates the data horizontally. My input file looks like this:

X Y
a hello
a 3
a bye
a hi
b apple
b orange
b 4

and this is the output I need:

X Y
a hello,3,bye,hi
b apple,orange,4

I use this python code on Jupyter:

import pandas as pd
# df=pd.read_excel('test.xlsx')
df = pd.DataFrame({"X": ["a", "a", "a", "a", "b", "b", "b"],
                   "Y": ["hello", 3, "bye", "hi", "apple", "orange", 4]})

orden=df.groupby('X').Y.apply(','.join)

> error: TypeError: sequence item 0: expected str instance, int found

I have validated other data, and I suspect that it falls by the integers. How could I improve my code so that it also concatenates numbers ans string?

答案1

得分: 2

Y列首先转换为字符串:

df = pd.DataFrame({"X": ["a", "a", "a", "a", "b", "b", "b"],
                   "Y": ["hello", 3, "bye", "hi", "apple", "orange", 4]})
df["Y"] = df["Y"].astype(str)
orden = df.groupby('X').Y.apply(','.join)

得到的结果是 orden=

X
a    hello,3,bye,hi
b    apple,orange,4
Name: Y, dtype: object
英文:

Convert the Y column to a string first:

df = pd.DataFrame({"X": ["a", "a", "a", "a", "b", "b", "b"],
                   "Y": ["hello", 3, "bye", "hi", "apple", "orange", 4]})
df["Y"] = df["Y"].astype(str)
orden=df.groupby('X').Y.apply(','.join)

which gives orden=

X
a    hello,3,bye,hi
b    apple,orange,4
Name: Y, dtype: object

huangapple
  • 本文由 发表于 2023年2月18日 01:14:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487347.html
匿名

发表评论

匿名网友

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

确定