- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements
To Continue Learning Please Login
Login with Google