上QQ阅读APP看书,第一时间看更新
Reading from an .xlsx file
Now let's read the same data from an .xlsx file and create the x and y NumPy arrays. The .xlsx file format is not supported by the NumPy loadtxt() function. A Python data processing package, pandas can be used:
- Read the .xlsx file into pandas DataFrame. This file has the same five points in 2D space, each in a separate row with x, y columns:
df = pd.read_excel('test.xlsx', 'sheet', header=None)
- Convert the pandas DataFrame to a NumPy array:
data_array = np.array(df)
print(data_array)
You should see the following output:
[[ 1 1] [ 2 4] [ 3 9] [ 4 16] [ 5 25]]
- Now extract the x and y coordinates from the NumPy array:
x , y = data_array[:,0], data_array[:,1]
print(x,y)
You should see the following output:
[1 2 3 4 5] [ 1 4 9 16 25]