Informatics Practices
Write a program to iterate and print a dataframe row-wise at a time and print only first five rows.
Python Data Handling
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
1 Like
Related Questions
What will be the output produced by the following codes, considering the Series object S given in Q.13?
(a) print(S[1:4])
(b) print(S[:1])
(c) print(S[0:2])
(d) S[0:2] = 12
print(S)(e) print(S.index)
(f) print(S.values)
The Series object 'S' is as follows:
pencils 20 notebooks 33 scales 52 erasers 10 dtype: int64
Write a program to iterate and print a dataframe column-wise and print only first three columns.
Find the error in the following code fragments:
S2 = pd.Series([101, 102, 1-2, 104]) print (S2.index) S2.index = [0.1.2.3, 4, 5] S2[5] = 220 print (S2)
Find the error in the following code fragments:
S = pd.Series(2, 3, 4, 55, index = range (4))