Write a Python program to read an Excel data from file and read all rows of first and last columns


Assume, you have an Excel file stored with the name of pandas.xlsx in your location.

Solution

To solve this, we will follow the steps given below −

  • Define pd.read_excel method to read data from pandas.xlsx file and save it as df

df = pd.read_excel('pandas.xlsx')
  • Apply df.iloc[:,0] to print all rows of first column

df.iloc[:,0]
  • Apply df.iloc[:,-1] to print all rows of last column

df.iloc[:,-1]

Example

Let’s see the below implementation to get a better understanding −

import pandas as pd
df = pd.read_csv('products.csv')
print("all rows of first column is")
print(df.iloc[:,0])
print("all rows of last column is")
print(df.iloc[:,-1])

Output

all rows of first column is
0       1
1       2
2       3
3       4
4       5
      ...
95    96
96    97
97    98
98    99
99    100
Name: id, Length: 100, dtype: int64
all rows of last column is
0    2019
1    2020
2    2018
3    2018
4    2018
      ...
95    2019
96    2019
97    2018
98    2020
99    2018
Name: productionYear, Length: 100, dtype: int64

Updated on: 24-Feb-2021

833 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements