简体   繁体   English

在 dataframe pandas 中将列表项读取为字符串

[英]Read list items as string in dataframe pandas

I have such lines in the code that reads values from excel file我在从 excel 文件中读取值的代码中有这样的行

df = pd.read_excel('Data.xlsx', sheet_name='Sheet1')
mylist = df['Numbers'].tolist()

I got a list of numbers like that [1, 2, 3, 4, 5, 100] How to convert the numbers in that list to string?我有一个类似[1, 2, 3, 4, 5, 100]的数字列表如何将该列表中的数字转换为字符串?

You can cast the pandas series object using astype method which accepts numpy.dtype or Python type.您可以使用astype方法来转换 pandas 系列 object ,该方法接受numpy.dtype或 ZA7F5F35426B9274117FC29 类型。

df['Numbers'].astype(str).tolist()

I am assuming that the problem you're having is probably because join connects elements inside a list of strings and not integers (which is what you have in that list).我假设您遇到的问题可能是因为join连接strings列表中的元素而不是integers (这就是您在该列表中的内容)。 Therefore you can:因此,您可以:

a = ''.join(map(str, my_list))         #1st way
b = ''.join(str(i) for i in my_list)   #2nd way

print(a), print(b)

12345100
12345100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM