Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python How to Import xlsx file using numpy

I am having no trouble importing csv data using numpy, but keep getting an error for my xlsx file. How do I convert the xlsx file to csv or how to I import xlsx file to the x2 variable?

from matplotlib import pyplot as pp
import numpy as np

#this creates a line graph comparing flight arrival time, arrival in queue, and processing time

x,y = np.loadtxt ('LAX_flights.csv',
                unpack = True,
                usecols = (1,2),
                delimiter = ',')

print("Imported data set arrival time")

x2 = np.loadtext ('First_Persons_PT.xlsx',
               unpack = True,
               usecols=(0))

print ("Imported start of processing time")


#y2=
#print ("Imported final time when processed")

pp.plot(x,y, 'g', linewidth = 1)
#pp.plot(x2,y, 'y', linewidth = 1)
pp.grid(b=True, which = 'major', color='0', linestyle='-')

pp.title('Comparing Time of Arrival vs. Queue Arrival Time, Queue Finish Time')
pp.ylabel('Arrival in queue (Green),Process Time (Yellow)')
pp.xlabel('Time of arrival')

pp.savefig('line_graph_comparison.png')

Here is the error

Imported data set arrival time
Traceback (most recent call last):
  File "C:\Users\fkrueg1\Dropbox\forest_python_test\Graph_time_of_arrival.py", line 13, in <module>
    x2 = np.loadtext ('First_Persons_PT.xlsx',
AttributeError: 'module' object has no attribute 'loadtext'

The xlsx is just a single column of about 100 numbers

like image 504
godofamerica Avatar asked Dec 08 '25 00:12

godofamerica


2 Answers

import pandas as pd
WS = pd.read_excel('ur.xlsx')
WS_np = np.array(WS)

Using pandas is simpler

like image 148
Ahmad Saadeddin Avatar answered Dec 10 '25 14:12

Ahmad Saadeddin


The method's name is loadtxt, rather than loadtext. That explains the error that you report.

However, loadtxt won't be able to read an OpenXML .xlsx file. The .xlsx file is a binary format, and a rather complex one at that. You will need to use a module dedicated to reading such files in order to be able to read .xlsx files. For instance, xlrd and openpyxl can both read .xlsx files.

Depending on what your requirements are, it may be easier to supply a text file rather than a .xlsx file.

like image 22
David Heffernan Avatar answered Dec 10 '25 14:12

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!