Data analysis using Pandas
Last Updated :
31 Mar, 2023
Pandas are the most popular python library that is used for data analysis. It provides highly optimized performance with back-end source code purely written in C or Python.Â
We can analyze data in Pandas with:
Pandas Series
Series in Pandas is one dimensional(1-D) array defined in pandas that can be used to store any data type.
Creating Pandas Series
Python3
# Program to create series
# Import Panda Library
import pandas as pd
# Create series with Data, and Index
a = pd.Series(Data, index=Index)
Here, Data can be:
- A Scalar value which can be integerValue, string
- A Python Dictionary which can be Key, Value pair
- A Ndarray
Note: Index by default is from 0, 1, 2, ...(n-1) where n is the length of data. Â
Create Series from List
 Creating series with predefined index values.
Python3
# Numeric data
Data = [1, 3, 4, 5, 6, 2, 9]
# Creating series with default index values
s = pd.Series(Data)
# predefined index values
Index = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
si = pd.Series(Data, Index)
Output:
Â
 Create Pandas Series from Dictionary
Program to Create Pandas series from Dictionary.
Python3
dictionary = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
# Creating series of Dictionary type
sd = pd.Series(dictionary)
Output:
Dictionary type dataConvert an Array to Pandas Series
Program to Create ndarray series.
Python3
# Defining 2darray
Data = [[2, 3, 4], [5, 6, 7]]
# Creating series of 2darray
snd = pd.Series(Data)
Output:
Data as NdarrayPandas DataFrames
The DataFrames in Pandas is a two-dimensional (2-D) data structure defined in pandas which consists of rows and columns.
Creating a Pandas DataFrame
Python3
# Program to Create DataFrame
# Import Library
import pandas as pd
# Create DataFrame with Data
a = pd.DataFrame(Data)
Here, Data can be:
- One or more dictionaries
- One or more Series
- 2D-numpy Ndarray
Create a Pandas DataFrame from multiple Dictionary
Program to Create a Dataframe with two dictionaries.
Python3
# Define Dictionary 1
dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
# Define Dictionary 2
dict2 = {'a': 5, 'b': 6, 'c': 7, 'd': 8, 'e': 9}
# Define Data with dict1 and dict2
Data = {'first': dict1, 'second': dict2}
# Create DataFrame
df = pd.DataFrame(Data)
df
Output:
DataFrame with two dictionariesConvert list of dictionaries to a Pandas DataFrame
Here, we are taking three dictionaries and with the help of from_dict() we convert them into Pandas DataFrame.
Python3
import pandas as pd
data_c = [
{'A': 5, 'B': 0, 'C': 3, 'D': 3},
{'A': 7, 'B': 9, 'C': 3, 'D': 5},
{'A': 2, 'B': 4, 'C': 7, 'D': 6}]
pd.DataFrame.from_dict(data_c, orient='columns')
Output:
A B C D
0 5 0 3 3
1 7 9 3 5
2 2 4 7 6
Create DataFrame from Multiple Series
Program to create a dataframe of three Series.
Python3
import pandas as pd
# Define series 1
s1 = pd.Series([1, 3, 4, 5, 6, 2, 9])
# Define series 2
s2 = pd.Series([1.1, 3.5, 4.7, 5.8, 2.9, 9.3])
# Define series 3
s3 = pd.Series(['a', 'b', 'c', 'd', 'e'])
# Define Data
Data ={'first':s1, 'second':s2, 'third':s3}
# Create DataFrame
dfseries = pd.DataFrame(Data)
dfseries
Output:
DataFrame with three seriesConvert a Array to Pandas Dataframe
One constraint has to be maintained while creating a DataFrame of 2D arrays - The dimensions of the 2D array must be the same.
Python3
# Program to create DataFrame from 2D array
# Import Library
import pandas as pd
# Define 2d array 1
d1 =[[2, 3, 4], [5, 6, 7]]
# Define 2d array 2
d2 =[[2, 4, 8], [1, 3, 9]]
# Define Data
Data ={'first': d1, 'second': d2}
# Create DataFrame
df2d = pd.DataFrame(Data)
df2d
Output:
DataFrame with 2d ndarray
Similar Reads
Saving a Pandas Dataframe as a CSV In this article, we will learn how we can export a Pandas DataFrame to a CSV file by using the Pandas to_csv() method. By default, the to csv() method exports DataFrame to a CSV file with row index as the first column and comma as the delimiter.Table of ContentExport CSV to a Working DirectorySaving
2 min read
How to Plot a Dataframe using Pandas Pandas plotting is an interface to Matplotlib, that allows to generate high-quality plots directly from a DataFrame or Series. The .plot() method is the core function for plotting data in Pandas. Depending on the kind of plot we want to create, we can specify various parameters such as plot type (ki
8 min read
Data Processing with Pandas Data Processing is an important part of any task that includes data-driven work. It helps us to provide meaningful insights from the data. As we know Python is a widely used programming language, and there are various libraries and tools available for data processing. In this article, we are going t
10 min read
Python | Pandas dataframe.all() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. DataFrame.all() method checks whether all elements are True, potentially over an axis.
3 min read
Creating Pandas dataframe using list of lists In this article, we will explore the Creating Pandas data frame using a list of lists. A Pandas DataFrame is a versatile 2-dimensional labeled data structure with columns that can contain different data types. It is widely utilized as one of the most common objects in the Pandas library. There are v
4 min read
Python | Pandas dataframe.applymap() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Dataframe.applymap() method applies a function that accepts and returns a scalar to ev
2 min read
Data Manipulation in Python using Pandas In Machine Learning, the model requires a dataset to operate, i.e. to train and test. But data doesnât come fully prepared and ready to use. There are discrepancies like Nan/ Null / NA values in many rows and columns. Sometimes the data set also contains some of the rows and columns which are not ev
6 min read
Python | Pandas Dataframe.at[ ] Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas at[] is used to return data in a dataframe at the passed location. The passed l
2 min read
Python | Data Comparison and Selection in Pandas Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, and makes importing and analyzing data much easier. The most important thing in Data Analysis is comparing values and selecting data acco
2 min read
Creating Horizontal Bar Charts using Pandas Prerequisites: Pandas A bar chart represents categorical data with corresponding data values as rectangular bars. Usually, the x-axis represents categorical values and the y-axis represents the data values or frequencies. This is called a vertical bar chart and the inverse is called a horizontal bar
4 min read