I would like to create an empty dataframe with multiindexes columns with Pandas, and then fill values later on. How could I do that ?
In this example I can create the dataframe but can't assign value to it with .loc.
segment = 'segment_1'
tuples = [(segment, 'market', 'symbol'),
(segment, 'market', 'base'),
(segment, 'market', 'quote'),
(segment, 'market', 'wallet'),
(segment, 'market', 'type'),
(segment, 'market', 'margined'),
(segment, 'market', 'derivative'),
(segment, 'funds', 'wallet'),
(segment, 'funds', 'code'),
(segment, 'type', 'action'),
(segment, 'type', 'transfer'),
(segment, 'type', 'priority'),
]
index = pd.MultiIndex.from_tuples(tuples, names=["level_1", "level_2", 'level_3'])
df = pd.DataFrame(columns=index)
df.loc[:, ('segment_1', 'market', 'symbol')] = 1
df
level_1 segment_1
level_2 market funds type
level_3 symbol base quote wallet type margined derivative wallet code action transfer priority
As you can it's not possible to set value to this dataframe. What is the reason for this ? do I need to create an index first ?