Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas – convert data into list

I have a file.xlsx with numbers in one column. I want it to convert into a list in python. The problem is when I run the code, the output is:

[[123], [234], [345], ...]

but want it to be

["123", "234", "345", ...]

How can I "delete" the additional " [] "?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import pandas as pd


excel_file = pd.read_excel(r'C:\xxx\xxx\xxx\file.xlsx')
excel_list = excel_file.values.tolist()

print(excel_list)

>Solution :

You can unwrap the 1-item lists with a list comprehension that accesses the zeroth member of the list:

excel_list = [[123], [234], [345]]  # stand-in for your current reading code

excel_list = [item[0] for item in excel_list]  # unwrap the lists

However, it’s better to just tell Pandas that the first line is a header (if it is):

import pandas as pd

excel_file = pd.read_excel(r'so71568988.xlsx', header=1)
print(excel_file)
excel_list = excel_file["values"].tolist()
print(excel_list)

outputs

   values
0     123
1     456
2     790
3   35932
[123, 456, 790, 35932]

without extra unwrapping.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading