简体   繁体   中英

Read list items as string in dataframe pandas

I have such lines in the code that reads values from excel file

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?

You can cast the pandas series object using astype method which accepts numpy.dtype or Python type.

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). 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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