Python Pandas Series
A Series is a one-dimensional labeled array. It can hold any type of data like Integer, Float, String, Python objects, etc. The label is called the index.
Pandas.Series
A Pandas Series can be created using the following constructors-
pandas.Series ( data, index, dtype, copy)
Parameter | Description |
---|---|
data | data takes various forms like ndarray, list, constants |
index | Index values must be unique. By default np.arrange(n) |
dtype | it is a data type |
copy | Copy data. Default false |
How to create a Pandas Series
- Array
- Dictionary
- constant
Create a Series from an array
#imports the pandas library and aliasing as pd
import pandas as pd
import numpy as np
data = np.array([‘a’,’b’,’c’,’d’,’e’])
sr=pd.Series(data)
print (sr)
0 | a |
1 | b |
2 | c |
3 | d |
4 | e |
#imports the pandas library and aliasing as pd
import pandas as pd
import numpy as np
data = np.array([‘a’,’b’,’c’,’d’,’e’])
sr=pd.Series(data, index=[101,102,103,104,105])
print (sr)
101 | a |
102 | b |
103 | c |
104 | d |
105 | e |
Create a Series from a dictionary
#imports the pandas library and aliasing as pd
import pandas as pd
data = {‘a’ : 0, ‘b’ : 1, ‘c’ : 2, ‘d’ : 3}
sr=pd.Series(data)
print (sr)
a | 0 |
b | 1 |
c | 2 |
d | 3 |
Create a Series from constant
#imports the pandas library and aliasing as pd
import pandas as pd
sr=pd.Series(4, index=[0,1,2,3])
print (sr)
0 | 4 |
1 | 4 |
2 | 4 |
3 | 4 |
If you like my post please share it with your friends by simply clicking the below social media button, and don’t forget to comment so we will update ourselves.