Informatics Practices
Write a program to print a DataFrame one row at a time and print only first five rows.
Python Pandas
2 Likes
Answer
import pandas as pd
data = {
'Name': ['Amruta', 'Harsh', 'Yogesh', 'Shreya', 'Zoya', 'Nyra'],
'Age': [25, 30, 35, 40, 45, 28],
'City': ['Chandigarh', 'Jaipur', 'Dehradun', 'Delhi', 'Vadodara', 'Guwahati']
}
df = pd.DataFrame(data)
first_five_rows = df.head(5)
print("Each row:")
for index, row in first_five_rows.iterrows():
print("Index:", index)
print(row)
Output
Each row:
Index: 0
Name Amruta
Age 25
City Chandigarh
Name: 0, dtype: object
Index: 1
Name Harsh
Age 30
City Jaipur
Name: 1, dtype: object
Index: 2
Name Yogesh
Age 35
City Dehradun
Name: 2, dtype: object
Index: 3
Name Shreya
Age 40
City Delhi
Name: 3, dtype: object
Index: 4
Name Zoya
Age 45
City Vadodara
Name: 4, dtype: object
Answered By
3 Likes
Related Questions
Consider the DataFrame wdf as shown below :
minTemp maxTemp Rainfall Evaporation 0 2.9 8.0 24.3 0.0 1 3.1 14.0 26.9 3.6 2 6.2 13.7 23.4 3.6 3 5.3 13.3 15.5 39.8 4 6.3 17.6 16.1 2.8 5 5.4 18.2 16.9 0.0 6 5.5 21.1 18.2 0.2 7 4.8 18.3 17.0 0.0 8 3.6 20.8 19.5 0.0 9 7.7 19.4 22.8 16.2 10 9.9 24.1 25.2 0.0 11 11.8 28.5 27.3 0.2 12 13.2 29.1 27.9 0.0 13 16.8 24.1 30.9 0.0 14 19.4 28.1 31.2 0.0 15 21.6 34.4 32.1 0.0 16 20.4 33.8 31.2 0.0 17 18.5 26.7 30.0 1.2 18 18.8 32.4 32.3 0.6 19 17.6 28.6 33.4 0.0 20 19.7 30.3 33.4 0.0 (a) Write statement(s) to calculate minimum value for each of the columns.
(b) Write statement(s) to calculate maximum value for each of the rows.
(c) Write statement(s) to calculate variance for column Rainfall.
(d) Write statement(s) to compute mean , mode median for last 10 rows.
Write a program to print a DataFrame one column at a time and print only first three columns.
Write a program that performs count, sum, max, and min functions :
On rows
On columns
Take a DataFrame of your choice. Write a program to calculate count of values only in a selective column.