Informatics Practices
Write a program to iterate and print a dataframe column-wise and print only first three columns.
Python Data Handling
1 Like
Answer
import pandas as pd
data = {
'Name': ['Aliya', 'Hemanth', 'Charlie'],
'Age': [25, 30, 35],
'City': ['Bangalore', 'Chennai', 'Mumbai'],
'Salary': [50000, 60000, 70000]
}
df = pd.DataFrame(data)
first_three_columns = df.iloc[:, :3]
print("Each column:")
for column_name in first_three_columns:
column_data = first_three_columns[column_name]
print(column_name)
print(column_data)
Output
Each column:
Name
0 Aliya
1 Hemanth
2 Charlie
Name: Name, dtype: object
Age
0 25
1 30
2 35
Name: Age, dtype: int64
City
0 Bangalore
1 Chennai
2 Mumbai
Name: City, dtype: object
Answered By
1 Like
Related Questions
What will be the output produced by the following code?
Stationery = ['pencils', 'notebooks', 'scales', 'erasers'] S = pd.Series([20, 33, 52, 10], index = Stationery) S2 = pd.Series([17, 13, 31, 32], index = Stationery) print(S == S2) S = S + S2 print(S)
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 row-wise at a time and print only first five rows.
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)