Updated April 3, 2023
Introduction to Pandas Series
In pandas, the series is a one-dimensional data structure. This data structure is more of an array-like format and can hold individual items of below data types(integer, string, float, python objects, etc.). Every item is associated with an index value in the series. The labels of a pandas series are not expected to be unique but it is to be an iterable type. The pandas series data structure uses a very wide variety of methods to perform necessary operations on the values.
Syntax and Parameters
Following is a syntax:
Syntax:
pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)
Following are the different parameters with description:
Parameter | Description |
data | The values of the series are declared here. |
index | Mentions the index values for the above-given values, the index values need not be unique. The default range of the index value is (0, 1, 2, …. N). The values of the index need to be the same length of the data being used. |
dtype | The data type for the output Series. If not specified, this will be inferred from the data. |
name | Name of the series |
copy | Used to represent whether it needs to be copied or not |
How to create an Empty Pandas Series?
The Empty pandas series can be created using an empty series method from the pandas’ library. it can be associated with values using the copy() method.
Code:
import pandas as pd
Temp_List1 = [2,4,56,7,8,8]
pd_series = pd.Series([])
print(" Created Empty Series",pd_series)
pd_series = Temp_List1.copy()
print(" Print the entire series",pd_series)
print(" Print the first element of the Series",pd_series[0])
Output
Explanation: In this example, an empty pandas series data structure is created first then the data structure is loaded with values using a copy function. the values which are about to be needed are held as a list then that list is copied into the pandas series. After the copy process is done the series is printed onto the console. more specifically the first element of the series is also printed.
How to create Pandas Series using various inputs?
The pandas series can be created in multiple ways, bypassing a list as an item for the series, by using a manipulated index to the python series values, We can also use a dictionary as an input to the pandas series. for the dictionary case, the key of the series will be considered as the index for the values in the series.
Code:
import pandas as pd
List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
index = [101, 102, 103, 104, 105, 106, 107, 108, 109, 110]
dict = { 1:'Test_Value1', 2:'Test_Value2', 3:'Test_Value3'}
pd_series_default_index = pd.Series(List)
print(" Pandas Series with default index: \n",pd_series_default_index)
pd_series_manipulated_index = pd.Series(List,index)
print(" Pandas Series with Manipulated index: \n",pd_series_manipulated_index)
pd_series_manipulated_data_type = pd.Series(List,dtype=str)
print(" Pandas Series with Manipulated index: \n",type(pd_series_manipulated_data_type))
print(" Print type of first element: \n",type(pd_series_manipulated_data_type[0]))
pd_series_dict = pd.Series(dict)
print(" Pandas series with dicitionary type: \n",pd_series_dict)
print(" Pandas series with dicitionary type: \n",pd_series_dict[3])
Output:
Explanation: Here the pandas series are created in three ways, First it is created with a default index which makes it be associated with index values from a series of 1, 2, 3, 4, ….n. The next instance involves the assignation of manipulated indexes to the series. this index values associated are 101, 102, 103, 104, 105, 106, 107, 108, 109, 110. In the next instance, a dictionary is used for preparing the series, here we can notice the key in the dictionary acts as the index for the whole series. in all the above instances either the entire series or some specific indexes in the series are manually printed on to the console.
Objects in Pandas
The various object-level attributes associated to the pandas’ library are listed in the table below, each attribute is used to achieve some specific functionality or operation in the pandas series, the attribute which is exactly used and the functionality associated to that specific attribute is also listed below.
Attribute | Description |
pd.series.array | Returns a pandas series into a numpy array |
pd.series.at[] | Returns an item at the given index |
pd.series.axes | Returns all the indexes used in that series |
pd.series.dtypes | Returns the data types of the underlying series |
pd.series.size | Returns the size of the series |
pd.series.memory_usage() | Returns the amount of memory occupied by the specific pandas series |
pd.series.shape | Returns the shape of the pandas series |
Code:
import pandas as pd
List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
index = [101, 102, 103, 104, 105, 106, 107, 108, 109, 110]
dict = { 1:'Test_Value1', 2:'Test_Value2', 3:'Test_Value3'}
pd_series = pd.Series(List,index)
print(" Pandas Series used: \n",pd_series)
print(" \n 'array' : ATTRIBUTE IN PANDAS")
print(" Pandas Series returning array: \n",pd_series.array ,type(pd_series.array))
print(" \n 'at' : ATTRIBUTE IN PANDAS")
print(" Pandas Series Access single value: ",pd_series.at[102])
print(" \n 'axes' : ATTRIBUTE IN PANDAS")
print(" Pandas Series indexes: ",pd_series.axes)
print(" \n 'dtypes' : ATTRIBUTE IN PANDAS")
print(" Pandas Series datatypes: ",pd_series.dtypes)
print(" \n 'size' : ATTRIBUTE IN PANDAS")
print(" Pandas Series size: ",pd_series.size)
print(" \n 'memory_usage' : ATTRIBUTE IN PANDAS")
print(" Find memory occurpied by series : ",pd_series.memory_usage())
print(" \n 'shape' : ATTRIBUTE IN PANDAS")
print(" Find shape of the series : ",pd_series.shape)
print(" \n 'empty' : ATTRIBUTE IN PANDAS")
print(" Empty a Pandas Series : ",pd_series.empty)
Output:
Explanation: In the above-given example eight attributes of the pandas series are mentioned. the first attribute is used for returning the entire series as an array. The next attribute at[] is used to access one specific item from the series, this is achieved by mentioning the index of the array. the index attribute is used for printing all the indexes in the array. the type attribute prints the type of the series being used. the size attribute prints the size of the series. next, the memory_usage method prints the memory held by the series, in this case, it is 480 bytes. the shape of the series and the attribute to empty the series is displayed then.
Conclusion
Pandas is among the strongest data handling libraries and it provides these classified sets of the data structure for availing the most flexible method of handling all data.
Recommended Articles
We hope that this EDUCBA information on “Pandas Series” was beneficial to you. You can view EDUCBA’s recommended articles for more information.