要列出的python数据帧字符串

时间:2017-10-19 10:13:27

标签: python dataframe

我有一个excel文件,其中包含以下内容,

Name  id   
A     123
B     124,125,126   
C     127

在调用pandas.read_excel()创建数据帧后,我注意到124,125,126被视为str。其他人则是int。

我想知道pythonic方法将其转换为列表,而不编写循环。

Name  id   
A     123
B     [124,125,126]  
C     127

这样我就可以扩展以下格式的数据框,这里提到了Expand pandas DataFrame column into multiple rows

Name    id
A       123
B       124
B       125
B       126
C       127

非常感谢提前。

1 个答案:

答案 0 :(得分:0)

s = "124,125,126"
[int(x) for x in s.split(",")]