I have a pandas dataframe with following schema:
import pandas as pd
# Create a list of data
data = [['market1', 2023, 100, 200],
['market2', 2022, 300, 400],
['market1', 2021, 500, 600],
['market2', 2020, 700, 800]]
# Create a DataFrame
df = pd.DataFrame(data, columns=['market', 'year', 'val1', 'val2'])
Printout:
market year val1 val2
0 market1 2023 100 200
1 market2 2022 300 400
2 market1 2021 500 600
3 market2 2020 700 800
I’d like to groupby
market
column and create a mapping year
–val1
and year
–val2
columns and save them as json strings. where year
is the key and val1
is the value.
Expected output:
market val1_json val2_json
market1 {"2021": 500, "2023": 100} {"2021": 600, "2023": 200}
market2 {"2020": 700, "2022": 300} {"2020": 800, "2022": 400}
Note that the json is sorted ascending
by the year
key.
3
Answers
Maybe not the most elegant solution
Try groupby:
Output:
You can construct a complex output for apply function by forming a pd.Series:
Output: