Skip to content

Series of (Dict Data) in Pandas ​

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

Dict to Series ​

dict_to_series.py

  • The index value no longer is 0, 1, 2, ...
  • The index value will be the Key-value of dict.
py
import pandas as pd

my_dict = {
   "apple": 1.50,
   "orange": 1.70,
   "grape": 5.00,
   "lemon": 1.20 
}

my_series = pd.Series(
    data=my_dict,
)

print(
    my_series
)

Output:

apple     1.5
orange    1.7
grape     5.0
lemon     1.2
dtype: float64

Get data (loc) ​

dict_loc.py

py
import pandas as pd

my_dict = {
   "apple": 1.50,
   "orange": 1.70,
   "grape": 5.00,
   "lemon": 1.20 
}

my_series = pd.Series(
    data=my_dict,
)

print(
    my_series["lemon"]
)

Output:

1.2

Update Value ​

update_value.py

py
import pandas as pd

my_dict = {
   "apple": 1.50,
   "orange": 1.70,
   "grape": 5.00,
   "lemon": 1.20 
}

my_series = pd.Series(
    data=my_dict,
)

# Grape price increases to 5.50
my_series["grape"] = 5.50

print(
    my_series
)

Output:

apple     1.5
orange    1.7
grape     5.5
lemon     1.2
dtype: float64

Filters by value ​

filters_by_value.py

py
import pandas as pd

my_dict = {
   "apple": 1.50,
   "orange": 1.70,
   "grape": 5.00,
   "lemon": 1.20 
}

my_series = pd.Series(
    data=my_dict,
)

# Grape price increases to 5.50
my_series["grape"] = 5.50

# Fruit price less than 1.70
print(
    my_series[my_series < 1.70]
)

Output:

apple    1.5
lemon    1.2
dtype: float64