Skip to main content

Section 13.1 Loading Data Files

Here, we are going to import data from a spreadsheet file, bringing it into Python as a NumPy array.

Example 13.1.1. Import from .csv file using NumPy.

If you are starting with data in an .xlsx file, you must first convert it into a .csv file using the ’Save As’ feature, selecting ’CSV (Comma Delimited)’ format. Then, import your data into Python using code like the following:
import numpy as np

# Read filename.csv file into a pandas dataframe. Skip the first two lines as they are string headers.
data = np.loadtxt('filename.csv', skiprows=2, delimiter=',');
R = data[:,0]; 
V = data[:,1];

Example 13.1.2. Import from .xlsx file using Pandas package.

import pandas as pd
import numpy as np

# Read filename.xlsx file into a pandas dataframe
df = pd.read_excel('filename.xlsx'); 

# Create a pandas sequence using the 0th column of data. Rows 0 and 1 contain header info.
df_R = df.iloc[2:,0]; 

# Create a pandas sequence using the 1st column of data. Rows 0 and 1 contain header info.
df_V = df.iloc[2:,1]; 

# Convert pandas sequences to numpy arrays
R = df_R.to_numpy();
V = df_V.to_numpy();