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

could not convert string to float error when loading excel files (python)

I am trying to load a couple of excel files (.xlsx) into python, which are all stored in the same folder. I get the error ‘could not convert string to float’ when I am using my code below:

import numpy as np
import glob


data_list = []

filenames = glob.glob('*.xlsx')

for fname in filenames:
    data = np.loadtxt(filenames)
    data_list.append(data)

How can I solve this issue?

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

>Solution :

np.loadtxt can’t read excel file, you can use Pandas instead:

# Python env: pip install pandas
# Miniconda env: conda install pandas
import pandas as pd
import glob

data_list = []

filenames = glob.glob('*.xlsx')

for fname in filenames:
    data = pd.read_excel(fname)  # <- HERE fname, not filenames
    data_list.append(data.to_numpy())
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