w3resource

Pandas Data Series: Create and display a one-dimensional array-like object containing an array of data


1. Creating a Series

Write a Pandas program to create and display a one-dimensional array-like object containing an array of data.

Sample Solution :

Python Code :

import pandas as pd
ds = pd.Series([2, 4, 6, 8, 10])
print(ds)

Sample Output:

0     2                                                                
1     4                                                                
2     6                                                                
3     8                                                                
4    10                                                                
dtype: int64

Explanation:

ds = pd.Series([2, 4, 6, 8, 10]): The code creates a Pandas Series object named 'ds' which contains a sequence of five integers: 2, 4, 6, 8, and 10.

print(ds): This line prints the contents of the Series object to the console.

The numbers on the left-hand side are the index of each value in the Series, which by default starts at 0 and increases by 1 for each subsequent element. The ‘dtype’ indicates the data type of the values in the Series, in this case, 64-bit integers.


For more Practice: Solve these Related Problems:

  • Write a Pandas program to create a Series from a list containing different data types and then filter out elements of a specific type.
  • Write a Pandas program to create a Series with a custom index from a NumPy array and then reorder the series based on the index in descending order.
  • Write a Pandas program to create a Series from a dictionary with duplicate values and then remove duplicates.
  • Write a Pandas program to create a Series from a range of dates and assign random numbers as values, then select a subset based on a condition on the index date.

Python-Pandas Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Python Pandas Data Series, DataFrame Exercises Home.
Next: Write a Pandas program to convert a Panda module Series to Python list and it’s type.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.