How to read Excel Using Pandas
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
Lets read a excel file, Excel file contains stocks data
df = pd.read_excel('/home/downloads/INTC.xlsx')
df.head()
lets check the columns
df.columns
Index(['Date', 'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume'], dtype='object')
lets access the column 'Date and look at 10 rows'
df['Date'].head() 0 2018-08-17 1 2018-08-20 2 2018-08-21 3 2018-08-22 4 2018-08-23 Name: Date, dtype: datetime64[ns]
lets subtract column high - column open
high_low_diff= df['High'] - df['Open']
high_low_diff.head()
0 0.389999
1 0.070000
2 1.189998
3 0.099998
4 0.309997
dtype: float64
lets go through the indexes and print values from column
for i in df.index[:10]: print(df['Date'][i]) 2018-08-17 00:00:00 2018-08-20 00:00:00 2018-08-21 00:00:00 2018-08-22 00:00:00 2018-08-23 00:00:00 2018-08-24 00:00:00 2018-08-27 00:00:00 2018-08-28 00:00:00 2018-08-29 00:00:00 2018-08-30 00:00:00
same thing can be done using following
df['Date'].head() 0 2018-08-17 1 2018-08-20 2 2018-08-21 3 2018-08-22 4 2018-08-23 Name: Date, dtype: datetime64[ns]
Common Errors While Reading Excel Using Pandas
You might run in to following error...
ImportError: Missing optional dependency 'xlrd'. Install xlrd >= 1.0.0 for Excel support Use pip or conda to install xlrd.
Install xlrd to avoid above error...
pip install xlrd