In this tutorial, we will introduce how to create python pandas series with python list, dict and numpy array using pd.Series().
1.Create an empty series
import pandas as pd s = pd.Series() print(s)
In python pandas, pd.Series() will create an empty series defaultly.
2.Create a series using python list
import pandas as pd items = [1,2,3,4] s = pd.Series(items) print(s)
Here items is a python list, we will use it to create a pandas series.
Run this code, we will see:
0 1 1 2 2 3 3 4 dtype: int64
3.Create a series using python dictionary
import pandas as pd import numpy as np data = { 'uk':'united kingdom','fr':'france' } s = pd.Series(data) print(s)
Here data is python dictionary, run this code, we will see this series:
uk united kingdom fr france dtype: object
4.Create a series using numpy array
import pandas as pd import numpy as np data = np.array(['x','y','z']) s = pd.Series(data) print(s)
Here data is numpy array, run this code we will see:
0 x 1 y 2 z dtype: object