Tuesday, 19 October 2021

Read data from an Excel file into Python using pandas

The Python pandas package can be used to read data from an Excel file into Python.

For example, I had an Excel file SimpleData.xlsx with three columns (showing the first few rows below):








To read it into Python using pandas, I first installed Pandas using Anaconda (which I had already installed on my computer, a Mac laptop):
% conda install -c conda-forge pandas
I also found that I needed a package called openpyxl to be able to read Excel using pandas: 
% conda install -c conda-forge openpyxl
Then I opened Python using:
% python3
and within the Python prompt typed:
>>> import pandas as pd
Now make a dataframe in pandas:
>>> mydata = pd.read_excel("SimpleData.xlsx")
Now print out the dataframe 'mydata':
>>> mydata
   Cmpd       MW  LogP
0    C1  277.330  3.29
1    C2  374.521  3.60
2    C3  357.360  3.56
3    C4  509.040  5.48
4    C5  424.480  3.03
..  ...      ...   ...
76  C77  954.660  0.00
77  C78  348.358  2.08
78  C79  501.070  3.65
79  C80  470.461  3.63
80  C81  302.780  4.91

[81 rows x 3 columns]

Hurray!

No comments:

Post a Comment