Skip to content

Series of (List Data) in Pandas ​

  • Example directory: demo-examples/Python/pandas/series_list_data

List convert to Series ​

list_to_series.py

py
import pandas as pd

my_list = [10, 12, 13]

# Series constructor
my_series = pd.Series(
    my_list
)

print(my_series)

Ouptut:

  • It arranged in a single column already.
  • index start from 0.
  • The dtype can be: bool, object (str), int64, float64
0    10
1    12
2    13
dtype: int64

Custom index on Series ​

custom_index_series.py

py
import pandas as pd

my_list = [10, 12, 13]

# Series constructor
my_series = pd.Series(
    my_list,
    index=["a", "b", "c"]
)

print(my_series)

Ouptut:

a    10
b    12
c    13
dtype: int64

Retrive Data / Locate data (loc) ​

loc_data_series.py

  • Get the data based on the index.
  • If the index not existsed, it will return KeyError.
py
import pandas as pd

my_list = [10, 12, 13]

# Series constructor
my_series = pd.Series(
    my_list,
    index=["a", "b", "c"]
)

# Get data with index a
print(my_series.loc["a"])

Ouptut:

10

Update data based on the index ​

update_data_based_index_series.py

py
import pandas as pd

my_list = [10, 12, 13]

# Series constructor
my_series = pd.Series(
    my_list,
    index=["a", "b", "c"]
)

# Update c to 15
my_series.loc["c"] = 15

print(my_series)

Output:

a    10
b    12
c    15
dtype: int64

indexloc - get data based on actual index value (iloc) ​

series_iloc.py

py
import pandas as pd

my_list = [10, 12, 13]

# Series constructor
my_series = pd.Series(
    my_list,
    index=["a", "b", "c"]
)

# Update c to 15
my_series.loc["c"] = 15

print(my_series.iloc[1]) # 1 =  b

Output:

12

Filters by value ​

filter_by_values.py

py
import pandas as pd

my_list = [10, 12, 13, 20, 25, 27]

# Series constructor
my_series = pd.Series(
    my_list,
    index=["a", "b", "c", "d", "e", "f"]
)

# Value >= 20
print(my_series[my_series >= 20])

Output:

d    20
e    25
f    27
dtype: int64