
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 program in Python to read CSV data from a file and print the total sum of last two rows
Assume you have the following data in your csv file and save it as pandas.csv.
pandas.csv
Id,Data 1,11 2,22 3,33 4,44 5,55 6,66 7,77 8,88 9,99 10,100
The result for sum of last two records as,
Sum of last two rows: Id 9 Data 99
Solution 1
Access stored data from csv file and save it as data using the below method,
data = pd.read_csv('pandas.csv')
Convert the data into dataframe and store inside df,
df = pd.DataFrame(data)
Apply the below method to take last two records and calculate the sum,
df.tail(2)).sum()
Example
Let’s see the below implementation to get a better understanding −
import pandas as pd data = pd.read_csv('pandas.csv') df = pd.DataFrame(data) print("Dataframe is\n",df) print("Last two rows\n",df.tail(2).sum())
Output
Dataframe is Id Data 0 1 11 1 2 22 2 3 33 3 4 44 4 5 55 5 6 66 6 7 77 7 8 88 8 9 99 9 10 100 Last two rows Id 19 Data 199
Solution 2
Access stored data from csv file and save it as data using the below method,
data = pd.read_csv('pandas.csv')
Convert the data into dataframe and store inside df,
df = pd.DataFrame(data)
Apply the below slicing index method to take last two records,
df.iloc[-2:]
Example
Let’s see the below implementation to get a better understanding −
import pandas as pd data = pd.read_csv('pandas.csv') df = pd.DataFrame(data) print("Dataframe is\n",df) print("Last two rows\n",df.iloc[-2:].sum())
Output
Dataframe is Id Data 0 1 11 1 2 22 2 3 33 3 4 44 4 5 55 5 6 66 6 7 77 7 8 88 8 9 99 9 10 100 Last two rows Id 19 Data 199
Solution 3
Access stored data from csv file and save it as data using the below method,
data = pd.read_csv('pandas.csv')
Convert the data into dataframe and store inside df,
df = pd.DataFrame(data)
Set id sum and data sum initial value as 0
Set a for loop to access dataframe data and set an if condition to access last two data,
for i in range(len(df)): if(i==len(df)-2 or i==len(df)-1):
Calculate first and second column sum using df.iloc[i][0] and df.iloc[i][1],
id_sum = id_sum + df.iloc[i][0] data_sum = data_sum + df.iloc[i][1]
Example
Let’s see the following implementation to get a better understanding,
import pandas as pd data = pd.read_csv('pandas.csv') df = pd.DataFrame(data) id_sum = 0 data_sum = 0 for i in range(len(df)): if(i==len(df)-2 or i==len(df)-1): id_sum = id_sum + df.iloc[i][0] data_sum = data_sum + df.iloc[i][1] print("Id",id_sum) print("Data",data_sum)
Output
Id 19 Data 199
- Related Questions & Answers
- Write a Python program to read an Excel data from file and read all rows of first and last columns
- Write a C program to read a data from file and display
- Write a Python code to read JSON data from a file and convert it to dataframe, CSV files
- Write data from/to a .csv file in java
- How to read the data from a CSV file in Java?
- How to read data from .csv file in Java?
- How to read data from *.CSV file using JavaScript?
- Write a program in Python to print the first and last three days from a given time series data
- 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 CSV file in Python?
- Write a Python code to swap last two rows in a given dataframe
- Write a program in Python to export a given dataframe into Pickle file format and read the content from the Pickle file
- How to read data from one file and print to another file in Java?
- How to create a file, write data into it and read data from it on iOS?
- Write a program in Python to read sample data from an SQL Database