0% found this document useful (0 votes)
6 views

Create a DataFrame

The document outlines various operations performed on a DataFrame using pandas, including creating a DataFrame, adding and deleting columns, displaying information and statistical summaries, selecting specific columns and rows based on conditions, sorting, grouping, and merging DataFrames. Each operation is accompanied by a question, program code, and the expected output. The examples demonstrate basic DataFrame manipulation techniques in Python.

Uploaded by

aadisharma9127
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Create a DataFrame

The document outlines various operations performed on a DataFrame using pandas, including creating a DataFrame, adding and deleting columns, displaying information and statistical summaries, selecting specific columns and rows based on conditions, sorting, grouping, and merging DataFrames. Each operation is accompanied by a question, program code, and the expected output. The examples demonstrate basic DataFrame manipulation techniques in Python.

Uploaded by

aadisharma9127
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy