Notice: Constant automattic\jetpack\extensions\social_previews\FEATURE_NAME already defined in /home/beaczwhx/dggulaitutorial.com/wp-content/plugins/jetpack/extensions/blocks/social-previews/social-previews.php on line 14
How To Create Pandas Series | Pandas Tutorial - Dggul AI Tutorial

How To Create Pandas Series | Pandas Tutorial


Notice: Undefined index: limited_lang in /home/beaczwhx/dggulaitutorial.com/wp-content/plugins/code-syntax-highlighter/inc/src/rendrer.php on line 297

Series are the most important data structure of pandas. This is built on top of NumPy that’s why this is very fast. In this Create Pandas Series tutorial, you will learn about pandas series and different ways to create it.  

Series is a one dimensional object which can hold any data type such as integers, floats, strings etc. It contains homogeneous data. Its size is immutable. So in this tutorial i will cover following topics –

  • What is pandas series
  • Syntax to create series 
  • Different ways of creating pandas series

Now let’s move ahead to understand all of the above concepts in detail.

How To Create Pandas Series

Pandas series can only have a single list with index. We can say a column of an excel sheet is a pandas series.

Now let’s see practically how to create series in pandas.

I am writing code on jupyter notebook so if you are working on jupyter notebook then create a new python file. And if you have not installed jupyter notebook then you can check this step by step guide to install jupyter notebook.

So without wasting time let’s get started. 

Syntax Of Creating Pandas Series

So if we talk about the syntax of creating pandas series then a series can be created using the following constructor.

pandas.Series( data, index, dtype, name, copy)  

Now let’s understand the parameters of this constructor.

  • data : This contains data stored in series. It could be in various forms like list, dict, array etc.
  • index : It’s values must be hashable and have the same length as data. If range will be not provided then it will take default values RangeIndex (0, 1, 2, …, n).
  • dtype : It indicates data type of output series. If this is not provided then this will be inferred from data. It is optional.
  • name : It is specified for giving the name to the series. It is optional.
  • copy : It is specified for copying input data. It is also optional.

Different Ways To Create Pandas Series

There are various ways to create pandas series. A series can be created from list, ndarray, dictionary etc. So let’s learn them one by one.

Creating An Empty Series

Creating an empty series is a very basic way of creating a series. So let’s see how to create this.

</span></p>
<p><span style="font-size: 14pt; font-family: arial, helvetica, sans-serif; color: #000000;">

import pandas as pd 

myseries = pd.Series()
 
print(myseries)


  • First of all we have imported pandas library. Here we are using an alias of pandas as pd because we will use pandas many times in our code.  
  • And then we have called pd.Series().
  • And finally printed the series. 

And now it’s time to see the output. So it’s output is as below-

create pandas series

 

Creating Series From List

We can create a series from a list in two ways. One of the ways is to create a list and then call it and the second is direct passing the list while calling Series(). Let’s see how to do them.

import pandas as pd 

my_list = [8,9,5,7] 

series_from_list = pd.Series(my_list) 

print(series_from_list)

  • Here, firstly we have created a list then passed it as a parameter to Series().

Now let’s check the output.

create pandas series

Now we will create a series from list in another way. So write the following code.

import pandas as pd 

series2_from_list = pd.Series([7,9,8,2]) 

print(series2_from_list)

  • Here we have directly passed the list to the Series() method.

This is a short method to create series from a list. And its output is following.

create pandas series

Creating Series From Dictionary

Now we will create series from dictionary. So to create series from dictionary, we have to pass a dictionary as a parameter in Series() method.

Let’s see the code.

import pandas as pd 

dict_series = pd.Series({'a':10,'b':20}) 

print(dict_series)

And its output is following.

Here you can see the key of dictionary is treated as index.

Creating Series From Scalar Values

We can also create series from scalar values. Scalar values means single value. You can give any scalar value such as integer, float etc. Let’s see how to create series from scalar values.

import pandas as pd

scalar_series = pd.Series(90)

print(scalar_series)
  • We have just passed a single value which is integer type.

Let’s check its output.

If you want to get more values then you have to mention index. So let’s do it practically.

import pandas as pd 

scalar_series = pd.Series(90, index=[1,2,3]) 

print(scalar_series)

Here is its output.

So you can see a series of 3 data values have been created from a scalar value.

I am wrapping up create pandas series tutorial here. I hope you didn’t get any problem while following this tutorial. But anyway, if you have any queries then let me know your problem, I will be happy to help you. And if you found it helpful then give me a favour by sharing this post to your friends. THANKS 

Leave a Comment