15 Essential Python Pandas Code Snippets for Data Scientists
<p>Python’s Pandas library is a fundamental tool for data scientists, offering powerful data manipulation and analysis capabilities. In this article, we’ll explore 15 advanced Pandas code snippets that every data scientist should have in their toolkit. These snippets will help you streamline your data analysis tasks and extract valuable insights from your datasets.</p>
<p> </p>
<h1>1. Filtering Data</h1>
<pre>
import pandas as pd
# Create a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [25, 30, 35, 40]}
df = pd.DataFrame(data)
# Filter rows where Age is greater than 30
filtered_df = df[df['Age'] > 30]
print(filtered_df)</pre>
<h1>2. Grouping and Aggregating Data</h1>
<pre>
# Grouping by a column and calculating the mean
grouped = df.groupby('Age').mean()
print(grouped)</pre>
<h1>3. Handling Missing Data</h1>
<pre>
# Check for missing values
missing_values = df.isnull().sum()
# Fill missing values with a specific value
df['Age'].fillna(0, inplace=True)</pre>
<h1>4. Applying Functions to Columns</h1>
<pre>
# Applying a custom function to a column
df['Age'] = df['Age'].apply(lambda x: x * 2)</pre>
<h1>5. Concatenating DataFrames</h1>
<pre>
# Concatenate two DataFrames
df1 = pd.DataFrame({'A': ['A0', 'A1'], 'B': ['B0', 'B1']})
df2 = pd.DataFrame({'A': ['A2', 'A3'], 'B': ['B2', 'B3']})
result = pd.concat([df1, df2], ignore_index=True)
print(result)</pre>
<h1>6. Merging DataFrames</h1>
<pre>
# Merge two DataFrames
left = pd.DataFrame({'key': ['A', 'B', 'C'], 'value': [1, 2, 3]})
right = pd.DataFrame({'key': ['B', 'C', 'D'], 'value': [4, 5, 6]})
merged = pd.merge(left, right, on='key', how='inner')
print(merged)</pre>
<h1>7. Pivot Tables</h1>
<pre>
# Creating a pivot table
pivot_table = df.pivot_table(index='Name', columns='Age', values='Value')
print(pivot_table)</pre>
<h1>8. Handling DateTime Data</h1>
<pre>
# Converting a column to DateTime
df['Date'] = pd.to_datetime(df['Date'])</pre>
<h1>9. Reshaping Data</h1>
<pre>
# Melting a DataFrame
melted_df = pd.melt(df, id_vars=['Name'], value_vars=['A', 'B'])
print(melted_df)</pre>
<h1>10. Working with Categorical Data</h1>
<pre>
# Encoding categorical variables
df['Category'] = df['Category'].astype('category')
df['Category'] = df['Category'].cat.codes</pre>
<p><a href="https://medium.com/@pythonfundamentals/15-essential-python-pandas-code-snippets-for-data-scientists-87bd499043a4">Visit Now</a></p>