Create a DataFrame
Question: Create a DataFrame with columns 'Name', 'Age', and 'City'.
Program:
Output:
Name Age City
0 Alice 24 New York
1 Bob 27 Los Angeles
2 Charlie 22 Chicago
Add a New Column
Question: Add a column 'Marks' to the DataFrame with values 85, 90, and 88.
Program:
Output:
Name Age City Marks
0 Alice 24 New York 85
1 Bob 27 Los Angeles 90
2 Charlie 22 Chicago 88
Delete a Column
Question: Delete the 'City' column from the DataFrame.
Program:
Output:
Name Age Marks
0 Alice 24 85
1 Bob 27 90
2 Charlie 22 88
Display DataFrame Info
Question: Display basic information about the DataFrame.
Program:
Output:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Name 3 non-null object
1 Age 3 non-null int64
2 Marks 3 non-null int64
dtypes: int64(2), object(1)
memory usage: 200.0+ bytes
None
Display Statistical Summary
Question: Display a statistical summary of the DataFrame.
Program:
Output:
Age Marks
count 3.000000 3.000000
mean 24.333333 87.666667
std 2.516611 2.516611
min 22.000000 85.000000
25% 23.000000 86.500000
50% 24.000000 88.000000
75% 25.500000 89.000000
max 27.000000 90.000000
Select a Column
Question: Select the 'Name' column from the DataFrame.
Program:
Output:
0 Alice
1 Bob
2 Charlie
Name: Name, dtype: object
Select Multiple Columns
Question: Select the 'Name' and 'Marks' columns from the DataFrame.
Program:
Output:
Name Marks
0 Alice 85
1 Bob 90
2 Charlie 88
Select Rows Based on Condition
Question: Select rows where 'Age' is greater than 23.
Program:
Output:
Name Age Marks
0 Alice 24 85
1 Bob 27 90
Sort DataFrame
Question: Sort the DataFrame by 'Marks' in descending order.
Program:
Output:
Name Age Marks
1 Bob 27 90
2 Charlie 22 88
0 Alice 24 85
Group by Column
Question: Group the DataFrame by 'Name' and calculate the mean of 'Marks'.
Program:
Output:
Name
Alice 85
Bob 90
Charlie 88
Name: Marks, dtype: int64
Merge Two DataFrames
Question: Merge two DataFrames on 'Name' column.
Program
Output:
Name Maths Science
0 Alice 85 89
1 Bob 92 94