WikiGalaxy

Personalize

Basic Operations with Pandas DataFrames

Introduction

Pandas is a powerful data manipulation library in Python. It provides data structures like DataFrames, which allow for efficient data manipulation and analysis. Here, we explore basic operations with Pandas DataFrames, including creation, indexing, selection, and more.

Creating DataFrames

From Dictionaries

DataFrames can be created from dictionaries, where keys are column names and values are data.


import pandas as pd

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

From Lists

DataFrames can also be created from lists of lists, specifying column names separately.


import pandas as pd

data = [['Alice', 25], ['Bob', 30], ['Charlie', 35]]
df = pd.DataFrame(data, columns=['Name', 'Age'])
print(df)
        

From NumPy Arrays

DataFrames can be constructed from NumPy arrays, which allows for integration with numerical data processing.


import pandas as pd
import numpy as np

data = np.array([['Alice', 25], ['Bob', 30], ['Charlie', 35]])
df = pd.DataFrame(data, columns=['Name', 'Age'])
print(df)
        

Indexing and Selection

Selecting Columns

Columns in a DataFrame can be selected using the column name as a key.


import pandas as pd

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

Selecting Rows

Rows can be selected using the loc method for label-based indexing or iloc for integer-based indexing.


import pandas as pd

df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [25, 30]})
print(df.loc[0])
print(df.iloc[1])
        

Boolean Indexing

Boolean indexing allows selection of data based on conditions.


import pandas as pd

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

Modifying DataFrames

Adding Columns

New columns can be added to a DataFrame by assigning a new Series or value.


import pandas as pd

df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [25, 30]})
df['City'] = ['New York', 'Los Angeles']
print(df)
        

Updating Values

Values in a DataFrame can be updated using index-based selection and assignment.


import pandas as pd

df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [25, 30]})
df.loc[0, 'Age'] = 26
print(df)
        

Removing Columns

Columns can be removed using the drop method, specifying the axis parameter.


import pandas as pd

df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [25, 30]})
df = df.drop('Age', axis=1)
print(df)
        

DataFrame Operations

Sorting Data

DataFrames can be sorted by one or more columns using the sort_values method.


import pandas as pd

df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [30, 25]})
df = df.sort_values(by='Age')
print(df)
        

Aggregating Data

Aggregation functions like sum, mean, and count can be applied to DataFrames.


import pandas as pd

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

Grouping Data

DataFrames can be grouped by one or more columns and aggregation functions can be applied to these groups.


import pandas as pd

df = pd.DataFrame({'Name': ['Alice', 'Bob', 'Alice'], 'Age': [30, 25, 35]})
grouped = df.groupby('Name').mean()
print(grouped)
        

Handling Missing Data

Identifying Missing Data

Missing data in a DataFrame can be identified using the isnull method.


import pandas as pd

df = pd.DataFrame({'Name': ['Alice', None, 'Charlie'], 'Age': [25, 30, None]})
print(df.isnull())
        

Filling Missing Data

Missing data can be filled using the fillna method, specifying a value.


import pandas as pd

df = pd.DataFrame({'Name': ['Alice', None, 'Charlie'], 'Age': [25, 30, None]})
df = df.fillna('Unknown')
print(df)
        

Dropping Missing Data

Rows or columns with missing data can be dropped using the dropna method.


import pandas as pd

df = pd.DataFrame({'Name': ['Alice', None, 'Charlie'], 'Age': [25, 30, None]})
df = df.dropna()
print(df)
        
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025