Series| How to Create Series | Creating Series
Series is an important data structure(A data structure is a particular way of storing and organizing data in a computer.) of Pandas. It represents a one-dimensional array of indexed data. It is homogenous data structure.
Points to Remember
- Series is a one-dimensional array.
- It is Homogenous. It means all the elements must be of same data type.
- In series, Values are mutable and Size is immutable. It means the elements of the series can be changed but size of a Series once created, cannot change. If you want to add or drop any element, a new series object will be created internally.
A Series type object can be created in many ways using pandas library Series() method make sure that you have imported Pandas and numpy module with import statements.- Create empty Series object by using just the Series() method with no parameter/attribute:
Syntax:
SeriesObject=pandas.Series()
For example:
import pandas as pd
S1=pd.Series()
print(S1)
To create an empty Series having no values you can just use the Series as the above statement will create an empty series type object with no value having the default type which is float64.
- Create empty Series object by using just the Series() method with parameter/attribute:
Syntax:
SeriesObject=pandas.Series(data , index)
For example:
import pandas as pd
S1=pd.Series(data=[2,3,4])
print(S1)
To create known empty series objects you need to specify arguments for data and indexes as per following above syntax.
Where data is compulsory and index is optional.
If you do not specify index then by default index array consists of integers 0 through the length of data.
The data is a data part of the series object. It can be one of the following:-
1. A Python sequence like List, Tuple , range() etc.
2. An ndarray
3. A Python dictionary
4. A Scalar value
We can create series type object is to give a sequence of values as an attribute to Series(). It will return an object of series type.
For instance, consider the following example statements that create series type objects using some Python sequences
You can give data as following ways:-
1. Specify data as Python Sequence :-
Series with List:
For example:
1. import pandas as pd
L=[2,3,4,5]
S1=pd.Series(data=L)
print(S1)
2. import pandas as pd
L=[2,3,4,5]
S1=pd.Series(L)
print(S1)
3. # Creating Series using data attribute with list
# We can use multiple list to create a series
import pandas as pd
L1=[2,3,4]
L2=[4,5,6]
S1=pd.Series(data=[L1,L2])
print(S1)
Screenshot:
Series with range() function:
4. import pandas as pd
S1=pd.Series(range(6))
print(S1)
5. import pandas as pd
S1=pd.Series(range(3,20))
print(S1)
6. import pandas as pd
S1=pd.Series(data=range(3,20,2))
print(S1)
Screenshot:
Creating Series using range() method |
2. Specify data as Python ndarray :-
You can create a Series using ndarray from any function such as arange(), linspace(),tile() etc.
For example:
1. import pandas as pd
import numpy as np
arr1=np.arange(3,13,3)
S1=pd.Series(arr1)
print(S1)
2. import pandas as pd
import numpy as np
S1=pd.Series(np.arange(3,13,3))
print(S1)
3. Specify data as Python Dictionary :-
If you are creating a Series from a dictionary then Keys of the dictionary become index of the series and the values of dictionary become data of Series.
For example:
1. import pandas as pd
S1=pd.Series({'Name':'Asha','Age':34})
print(S1)
2. import pandas as pd
dict={'EmpName' : 'Harsh' , 'Salary' : 40000, 'Empid' : 001}
S1=pd.Series(dict)
print(S1)
Screenshot:
Creating Series using Dictionary |
4. Specify data as Python Scalar Value :-
If you are creating a Series using scalar value or single value, the index should be displayed according to the value length.
For example:
1. import pandas as pd
S1=pd.Series(data=20)
print(S1)
2. import pandas as pd
val=20
S1=pd.Series(val)
No comments:
Post a Comment