- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a series from a list using Pandas?
Pandas Series can be created in different ways, here we will see how to create a pandas Series object with a python list.
To create a pandas series we have pandas.Series() function from pandas functionalities.
Let’s take an example and create a simple pandas Series using a python list. In order to create a pandas series from the python list, firstly we need to define a python list object.
Example
import pandas as pd # defining a list list_of_values = [2,89,34,78,3] # creating series s = pd.Series(list_of_values) print(s)
Explanation
In the above code, we have imported the pandas package using the python import keyword and changed the pandas package name to ‘pd’ by using the python aliasing keyword ‘as’. Then, created a list with a total of 5 integer values.
By using the pandas Series method we have created a pandas Series object.
Output
0 2 1 89 2 34 3 78 4 3 dtype: int64
This simple pandas Series object was created by using a list of integer values, and the index values are auto-created by the pandas Series object starting from 0-4, the elements all having the same data type int64.
Example
Let’s take another example and create a pandas series object with named indexes, to do this we need to use the index attribute from the pandas Series method.
# defining a list data = ['p','a','n','d','a','s'] # creating series with index values s = pd.Series(data, index=['a','b','c','d','e','f']) print(s)
By using the index attribute from the pandas Series method we can assign labels to our series elements.
Here the variable data is holding a python list object with string elements of length 6.
Output
a p b a c n d d e a f s dtype: object
All the elements of this pandas Series are having object data types. And the index of this above example is named labels defined by using the index attribute of the Series method.
- Related Articles
- How to create a Pandas series from a python dictionary?
- Create a Series from a List, Numpy Array, and Dictionary in Pandas
- How to create a pandas DataFrame using a list?
- Python Pandas - Create a Series from TimeDeltaIndex
- How to create a pandas DataFrame using a list of lists?
- How to create a pandas DataFrame using a list of tuples?
- How to create a pandas DataFrame using a list of dictionaries?
- How to Create Pandas Series from a dictionary with indexes in a specific order?
- How to remove NaN from a Pandas Series?
- How to compare elements of a series by Python list using pandas series.ge() function?
- How to compare elements of a series by Python list using pandas series.gt() function?
- How to remove a specified row From the Pandas Series Using Drop() method?
- How to create a series from a NumPy array?
- How to get few rows from a Series in Pandas?
- How to create a pandas DataFrame using a dictionary?
