KEMBAR78
Pandas DataFrame Methods Guide | PDF | Python (Programming Language) | Computer Programming
0% found this document useful (0 votes)
12 views12 pages

Pandas DataFrame Methods Guide

This document is a comprehensive guide on using Pandas DataFrame methods in Python, aimed at beginners. It covers creating, modifying, filtering, and performing operations on DataFrames, along with examples and outputs for each method. Additionally, it explains iteration, indexing, and importing/exporting CSV files, providing a solid foundation for data handling and analysis using Pandas.

Uploaded by

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

Pandas DataFrame Methods Guide

This document is a comprehensive guide on using Pandas DataFrame methods in Python, aimed at beginners. It covers creating, modifying, filtering, and performing operations on DataFrames, along with examples and outputs for each method. Additionally, it explains iteration, indexing, and importing/exporting CSV files, providing a solid foundation for data handling and analysis using Pandas.

Uploaded by

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

Pandas DataFrame Methods Guide

can explain me all these dataframes methods using python. i'm a complete beginner. how to use
these and with examples

Absolutely! I'll explain each of the DataFrame concepts and methods shown in the image, step by step
with simple examples in Python. These are all part of the pandas library, which is very commonly used
for data handling and analysis.

✅ 1. Creating DataFrames
python

import pandas as pd # importing pandas

# Creating from a dictionary


data = {
'Name': ['Alice', 'Bob'],
'Age': [25, 50]
}

df = pd.DataFrame(data, index=['row1', 'row2'], columns=['Name', 'Age'])


print(df)

Output:

markdown

Name Age
row1 Alice 25

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/12
row2 Bob 50

✅ 2. Modifying Data
Add a new column

python

df['Gender'] = ['F', 'M']


print(df)

Modify a value

python

df.at['row1', 'Age'] = 26

Remove a column

python

df.drop('Gender', axis=1, inplace=True) # axis=1 means column

Remove a row

python

df.drop('row2', axis=0, inplace=True) # axis=0 means row

✅ 3. Filtering and Slicing


Filter by condition

python

df = pd.DataFrame({'Age': [25, 50, 30]}, index=['A', 'B', 'C'])


filtered_df = df[df['Age'] > 25]
print(filtered_df)

Slice rows

python

print(df[0:2]) # shows first 2 rows

Boolean indexing

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/12
python

print(df[df['Age'] == 30])

✅ 4. Common Functions
python

df.head() # First 5 rows


df.tail() # Last 5 rows
df.shape # (rows, columns)
df.describe() # Summary of numerical columns
df.dropna() # Drops rows with missing values
df.fillna(0) # Fills missing values with 0

✅ 5. Iteration and Indexing


Loop through rows

python

for index, row in df.iterrows():


print(index, row['Age'])

Loop through columns

python

for col in df.columns:


print(df[col])

Access index and column names

python

print(df.index)
print(df.columns)

✅ 6. Math Operations
python

df = pd.DataFrame({'Age': [25, 50, 35]})

df['Age'] + 5 # Add 5 to each Age


df['Age'] * 2 # Multiply each Age by 2
df['Age'].mean() # Average
df['Age'].max() # Maximum

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/12
✅ 7. Importing & Exporting CSV
Read from a CSV file

python

df = pd.read_csv('file.csv') # Make sure the file exists in the directory

Write to a CSV file

python

df.to_csv('output.csv')

Let me know if you want a Jupyter Notebook version of this or want to test with real data!

i want explanation and output too

Perfect! Let's go through each section from the image with clear explanations and real Python code
with outputs. This will help you fully understand how to work with pandas DataFrames.

✅ 1. Creating DataFrames
Code:

python

import pandas as pd

# Creating a DataFrame from a dictionary


data = {
'Name': ['Alice', 'Bob'],
'Age': [25, 50]
}

df = pd.DataFrame(data, index=['row1', 'row2'], columns=['Name', 'Age'])


print(df)

Output:

markdown

Name Age
row1 Alice 25
row2 Bob 50

🧠 Explanation:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/12
We used pd.DataFrame() to create a DataFrame.

data is a dictionary of columns.


We used index to name the rows.

✅ 2. Modifying Data
➕ Add a new column
python

df['Gender'] = ['F', 'M']


print(df)

Output:

markdown

Name Age Gender


row1 Alice 25 F
row2 Bob 50 M

✏️ Modify a value
python

df.at['row1', 'Age'] = 26
print(df)

Output:

markdown

Name Age Gender


row1 Alice 26 F
row2 Bob 50 M

❌ Drop a column
python

df.drop('Gender', axis=1, inplace=True)


print(df)

Output:

markdown

Name Age
row1 Alice 26

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/12
row2 Bob 50

❌ Drop a row
python

df.drop('row2', axis=0, inplace=True)


print(df)

Output:

markdown

Name Age
row1 Alice 26

✅ 3. Filtering and Slicing


Let's create a new DataFrame:

python

df = pd.DataFrame({'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 50, 35]})

📌 Filter rows with condition


python

print(df[df['Age'] > 30])

Output:

markdown

Name Age
1 Bob 50
2 Charlie 35

📌 Slice rows
python

print(df[0:2])

Output:

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/12
nginx

Name Age
0 Alice 25
1 Bob 50

📌 Boolean indexing
python

print(df[df['Name'] == 'Alice'])

Output:

nginx

Name Age
0 Alice 25

✅ 4. Common Functions
python

df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [25, 50], 'Score': [90, 85]})

🧾 First 5 rows
python

print(df.head())

🧾 Last 5 rows
python

print(df.tail())

🧾 Shape
python

print(df.shape)

Output:

scss

(2, 3)

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/12
🧾 Summary statistics
python

print(df.describe())

Output:

matlab

Age Score
count 2.000000 2.00000
mean 37.500000 87.50000
std 17.677670 3.53553
min 25.000000 85.00000
max 50.000000 90.00000

✅ 5. Iteration and Indexing


Code:

python

for index, row in df.iterrows():


print(index, row['Name'])

for col in df.columns:


print(df[col])

Output:

yaml

0 Alice
1 Bob

0 Alice
1 Bob
Name: Name, dtype: object

0 25
1 50
Name: Age, dtype: int64

0 90
1 85
Name: Score, dtype: int64

✅ 6. Math Operations
python

df = pd.DataFrame({'Age': [25, 50, 35]})

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/12
➕ Add 5
python

print(df['Age'] + 5)

Output:

yaml

0 30
1 55
2 40
Name: Age, dtype: int64

✖ Multiply by 2
python

print(df['Age'] * 2)

Output:

yaml

0 50
1 100
2 70
Name: Age, dtype: int64

📊 Average
python

print(df['Age'].mean())

Output:

36.666666666666664

✅ 7. Importing and Exporting


Read CSV (you need a CSV file named file.csv)

python

df = pd.read_csv('file.csv')
print(df.head())

Write to CSV
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 9/12
python

df.to_csv('output.csv', index=False)

⚠ Make sure 'file.csv' exists in your folder, or else it will give an error.

🎁 Bonus Tip: Handling Missing Values


python

df = pd.DataFrame({'A': [1, None, 3], 'B': [4, 5, None]})


print(df)

Fill missing with 0

python

print(df.fillna(0))

Drop rows with missing values

python

print(df.dropna())

Would you like me to prepare a downloadable Jupyter Notebook with all these examples together?

thanks but i didn't get this one:

for index, row in df.iterrows():


print(index, row['Name'])

for col in df.columns:


print(df[col])

Great question! Let's break this down clearly. These two for loops help us iterate through rows and
columns in a DataFrame.

✅ 1. for index, row in df.iterrows():

This is used to loop through each row of the DataFrame.

📌 Example:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 10/12
python

import pandas as pd

df = pd.DataFrame({
'Name': ['Alice', 'Bob'],
'Age': [25, 50]
})

for index, row in df.iterrows():


print(index, row['Name'])

🔍 What happens here:


df.iterrows() returns each row one by one as a Series.
index is the row index (0, 1, etc.)

row is the content of that row.

💡 Output:
0 Alice
1 Bob

So this means:

Row at index 0 → Name is Alice

Row at index 1 → Name is Bob

✅ 2. for col in df.columns:

This is used to loop through each column.

📌 Example:
python

for col in df.columns:


print(f"Column: {col}")
print(df[col])

💡 Output:
pgsql

Column: Name
0 Alice
1 Bob
Name: Name, dtype: object

Column: Age
0 25

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 11/12
1 50
Name: Age, dtype: int64

So this is printing:

The column name (Name or Age)


The entire column content (like a list)

🔁 Summary:
Code What it does
for index, row in df.iterrows() Loops through each row

for col in df.columns Loops through each column

If you want to combine both (rows and columns), you can also do this:

python

for index, row in df.iterrows():


for col in df.columns:
print(f"{col} of row {index} is {row[col]}")

Would you like a diagram to visualize this?

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 12/12

You might also like