
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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
- Related Articles
- Write a program in Python to read CSV data from a file and print the total sum of last two rows
- Read and Write to an excel file using Python openpyxl module
- Write a C program to read a data from file and display
- Python Program to Read and printing all files from a zip file
- Python Program to Read First n Lines of a File
- Write a program in Python to read sample data from an SQL Database
- Golang program to read and print all files from zip file
- Read and write a string from a text file
- Write a Python code to read JSON data from a file and convert it to dataframe, CSV files
- How to create a file, write data into it and read data from it on iOS?
- Python Pandas - Read data from a CSV file and print the ‘product’ column value that matches ‘Car’ for the first ten rows
- How to read/write data from/to .properties file in Java?
- Read last line from file in PHP
- Python Program to open a file in read-write mode with truncating file
- How to read and write a file using Javascript?

Advertisements