英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论