Day 4 Data Manipulation With Pandas
Day 4 Data Manipulation With Pandas
Introduction to Pandas: Pandas is a powerful Python library for data manipulation and
analysis. It provides data structures like Series and DataFrame, which are ideal for handling
structured data.
Data Cleaning: Handling Missing Data, Data Transformation: Pandas provides methods
for handling missing data, such as dropping or filling missing values. It also supports various
data transformation operations like merging, reshaping, and aggregating data.
Output:
Pandas is an essential tool for data manipulation and analysis in Python, and mastering its
usage is crucial for working with structured datasets effectively.
Day 4: Data Manipulation with Pandas
Introduction to Pandas
Pandas:
Importing Pandas:
python
Copy code
import pandas as pd
From a dictionary:
python
Copy code
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'Los Angeles', 'Chicago']
}
df = pd.DataFrame(data)
python
Copy code
df = pd.read_csv('data.csv')
Indexing:
python
Copy code
df.set_index('Name', inplace=True)
Selection:
Selecting columns:
python
Copy code
df['Age']
df[['Name', 'City']]
Selecting rows:
python
Copy code
df.iloc[0] # By position
df.loc['Alice'] # By index
Conditional selection:
python
Copy code
df[df['Age'] > 30]
python
Copy code
df.isnull().sum()
python
Copy code
df.dropna(inplace=True)
python
Copy code
df.fillna(value=0, inplace=True)
Data Transformation:
python
Copy code
df['Age_in_10_years'] = df['Age'] + 10
Applying functions:
python
Copy code
df['Age_squared'] = df['Age'].apply(lambda x: x**2)
Example:
python
Copy code
import pandas as pd
# Creating a DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, None],
'City': ['New York', 'Los Angeles', 'Chicago']
}
df = pd.DataFrame(data)
# Data transformation
df['Age_in_10_years'] = df['Age'] + 10
print(df)
This concludes the note for Day 4: Data Manipulation with Pandas.