0% found this document useful (0 votes)
3 views2 pages

Pandas Intermediate Functions Logic

Uploaded by

Vikas Vikas
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)
3 views2 pages

Pandas Intermediate Functions Logic

Uploaded by

Vikas Vikas
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

Pandas Basics: Functions, Conditions, and Logic

Set Index & Reset Index

df.set_index('Name', inplace=True) # Set column as index


df.reset_index(inplace=True) # Reset to default integer index

Data Type Conversion

df['Age'] = df['Age'].astype(float) # Convert data type


pd.to_datetime(df['Date']) # Convert to datetime

Conditional Column Creation

df['Status'] = df['Age'].apply(lambda x: 'Adult' if x >= 18 else 'Child')

String Functions

df['Name'].str.lower() # Lowercase
df['City'].str.contains('New') # String contains
df['Name'].str.replace('a', '@') # Replace characters

Date Functions

df['Date'] = pd.to_datetime(df['Date'])
df['Year'] = df['Date'].dt.year
df['Month'] = df['Date'].dt.month
df['Weekday'] = df['Date'].dt.day_name()

Pivot Tables

pd.pivot_table(df, index='City', values='Age', aggfunc='mean')

Merging DataFrames

pd.merge(df1, df2, on='ID') # Inner join


pd.merge(df1, df2, on='ID', how='left') # Left join
pd.merge(df1, df2, on='ID', how='outer') # Outer join
Pandas Basics: Functions, Conditions, and Logic

Concatenation

pd.concat([df1, df2]) # Vertical concat


pd.concat([df1, df2], axis=1) # Horizontal concat

Handling Duplicates

df.duplicated(subset=['Name']) # Check duplicates in Name


df.drop_duplicates(subset=['Name']) # Drop them

Query Method

df.query('Age > 25 and City == "Delhi"')

Using `.loc` and `.iloc`

df.loc[0] # Access by label/index


df.iloc[1] # Access by position
df.loc[df['Age'] > 30, 'Name'] # Filter and select column

You might also like