Informatics Practices
Write a program to print a DataFrame one column at a time and print only first three columns.
Python Pandas
3 Likes
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
2 Likes
Related Questions
Consider the following code that creates two DataFrames :
ore1 = pd.DataFrame(np.array([[20, 35, 25, 20], [11, 28, 32, 29]]), columns = ['iron', 'magnesium', 'copper', 'silver']) ore2 = pd.DataFrame(np.array([[14, 34, 26, 26], [33, 19, 25, 23]]), columns = ['iron', 'magnesium', 'gold', 'silver'])
What will be the output produced by the following code fragments ?
(a) print(ore1 + ore2)
ore3 = ore1.radd(ore2)
print(ore3)(b) print(ore1 - ore2)
ore3 = ore1.rsub(ore2)
print(ore3)(c) print(ore1 * ore2)
ore3 = ore1.mul(ore2)
print(ore3)(d) print(ore1 / ore2)
ore3 = ore1.rdiv(ore2)
print(ore3)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 row at a time and print only first five rows.
Write a program that performs count, sum, max, and min functions :
On rows
On columns