Data Manipulation Made Easy with Pandas Basic Functionality!
Pandas is a powerful and versatile data manipulation and analysis library for Python. It provides a variety of functions and methods for working with data in a structured format, including loading, filtering, aggregating, and visualizing data.
Here are some basic examples of how to use Pandas:
Creating Data Structures: Pandas provides two primary data structures, the Series and DataFrame. These can be created from various data types, such as lists, arrays, and dictionaries.
import pandas as pd
import numpy as np
# Creating a Series
s = pd.Series([1, 3, 5, np.nan, 6, 8])
# Creating a DataFrame
data = {'Name': ['John', 'Mary', 'Mike'],
'Age': [25, 22, 30],
'Salary': [5000, 6000, 8000]}
df = pd.DataFrame(data)
Loading data: You can use the read_csv function to load data from a CSV file into a Pandas DataFrame. For example:
import pandas as pd
df = pd.read_csv('data.csv')
Filtering data: You can use the boolean indexing technique to filter rows in a DataFrame based on a condition. For example:
df = df[df['column_name'] > 0] # Keep rows where column_name is greater than 0
df = df[df['column_name'].isin(['value1', 'value2'])] # Keep rows where column_name is 'value1' or 'value2'
Aggregating data: You can use the group by function to group a DataFrame by one or more columns and apply an aggregation function to each group. For example:
df = df.groupby('column_name').mean() # Calculate the mean of each group
df = df.groupby('column_name').size() # Count the number of rows in each group
Visualizing data: You can use the plot function to create a variety of plots, including bar plots, line plots, and scatter plots. For example:
df['column_name'].plot.bar() # Create a bar plot
df.plot.line() # Create a line plot
df.plot.scatter(x='column_name_1', y='column_name_2') # Create a scatter plot
These are some examples of the basic functionality of Pandas. There are many more functions and methods available for working with data in Pandas, including handling missing values, merging and joining data, and more.