Create a Series from a List
Create a Series from a NumPy Array:
Create a Series from a Dictionary:
Create a Series with Custom Index:
Create a Series with a Scalar Value:
Create a Series with Date Range:
Create a Series from a List:
import pandas as pd
my_list = [10, 20, 30, 40, 50]
series = pd.Series(my_list)
print(series)
Output:
0 10
1 20
2 30
3 40
4 50
Create a Series from a NumPy Array:
import numpy as np
import pandas as pd
my_array = np.array([10, 20, 30, 40, 50])
series = pd.Series(my_array)
print(series)
Output:
0 10
1 20
2 30
3 40
4 50
Create a Series from a Dictionary:
import pandas as pd
my_dict = {'A': 10, 'B': 20, 'C': 30, 'D': 40, 'E': 50}
series = pd.Series(my_dict)
print(series)
Output:
A 10
B 20
C 30
D 40
E 50
Create a Series with Custom Index:
import pandas as pd
my_list = [10, 20, 30, 40, 50]
my_index = ['A', 'B', 'C', 'D', 'E']
series = pd.Series(my_list, index=my_index)
print(series)
Output:
A 10
B 20
C 30
D 40
E 50
import pandas as pd
my_list = [10, 20, 30, 40, 50]
series = pd.Series(my_list)
print(series)
output
0 10
1 20
2 30
3 40
4 50
Create a Series with a Scalar Value:
import pandas as pd
scalar_value = 5
series = pd.Series(scalar_value, index=['A', 'B', 'C', 'D', 'E'])
print(series)
Output:
A 5
B 5
C 5
D 5
E 5
Create a Series with Date Range:
import pandas as pd
start_date = '2023-01-01'
end_date = '2023-01-07'
date_range = pd.date_range(start=start_date, end=end_date)
series = pd.Series(range(len(date_range)), index=date_range)
print(series)
Output:
2023-01-01 0
2023-01-02 1
2023-01-03 2
2023-01-04 3
2023-01-05 4
2023-01-06 5
2023-01-07 6
Freq: D, dtype: int64
These are some of the ways you can create a series using pandas. Choose the method that suits your data and requirements.
series from set
import pandas as pd
my_set = {10, 20, 30, 40, 50}
series = pd.Series(my_set)
print(series)
Output
0 40
1 10
2 50
3 20
4 30
Top comments (0)