Pandas Example: Create Pandas series from dictionary of values and nd array.

Write a program to create Pandas series from dictionary of values and nd array with Practical Example

Creation of Pandas Series from Dictionary

Creation of Series object from nd-array

Creation of pandas Series object from dictionary

Program Logic:

  • Step 1: Import pandas module in program using import statement
  • Step 2: Create Dictionary object with different set of key value pair
  • Step 3: Create Series object using dictionary object that we have created in step 2
  • Step 4: Display Series object using print function

Below is implementation code/Source code:

Here is code for Series object which is created from Dictionary object. The output is also given below

import pandas as pd
dic = {'A':45,'B':55,'C':67,'D':78}
print("*****Dictionary Object********")
print(dic)
print("****Series object is created from Dictionary*****")
s = pd.Series(dic)
print("*****Series Object*****")
print(s)

Below is Output:

****Dictionary Object***
{‘A’: 45, ‘B’: 55, ‘C’: 67, ‘D’: 78}
****Series object is created from Dictionary****
***Series Object***
A 45
B 55
C 67
D 78
dtype: int64

Below is Snapshot of Executable code with output:

Creating Pandas Series Object from ndarray

  • Step 1: Import Pandas module in program using import statement
  • Step 2: Import numpy module in program using import statement
  • Step 3: Create ndarray with different set of values and store it in some variable
  • Step 4: Display ndarray object using print function
  • Step 5: Create Series object from ndarray that we have created in Step3
  • Step 6: Print Series object using print function

Below is implementation code/Source Code

Here is program to create Pandas Series from ndarray. The output is also shown below.

import pandas as pd
import numpy as np
print("**** ND-Array ****")
nd = np.arange(1,10)
print(nd)
print("****Series object****")
s = pd.Series(nd)
print(s)
print("***Series object is created from nd-array***")

Output:

*** ND-Array ****
[1 2 3 4 5 6 7 8 9]
*Series object*
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
dtype: int32
**Series object is created from nd-array**

Below is snapshot of executable code with output

You can check this too

<