You can add a data frame to you as follows:
my_home_city = {"near":{"bike":1, "car":0},"far":{"bike":0, "car":1}} dataframe.append(pd.DataFrame.from_dict(my_home_city).unstack().rename('my_home_city'))
Output:
distance near far vehicle bike car bike car city NaN NaN NaN NaN my_home_city 1 0 0 1
The trick is to create a dataframe row with from_dict , then unstack to get the structure of the original frame with multiple columns, and then rename to get the index and append .
Or, if you do not want to create an empty framework first, you can use this method to create a dataframe with new data.
pd.DataFrame.from_dict(my_home_city).unstack().rename('my_home_city').to_frame().T
Output:
far near bike car bike car my_home_city 0 1 1 0
Explanations:
pd.DataFrame.from_dict(my_home_city) far near bike 0 1 car 1 0
Now let's unlock the creation of a multi-index and move on to this new data structure in the structure of the original frame.
pd.DataFrame.from_dict(my_home_city).unstack() far bike 0 car 1 near bike 1 car 0 dtype: int64
We use renaming to give this series a name that becomes the index mark of this row of the data frame when added to the original data frame.
far bike 0 car 1 near bike 1 car 0 Name: my_home_city, dtype: int64
Now, if you converted this series into a frame and transfer it, it will be very similar to a new line, however, there is no need to do this because Pandas performs internal data alignment, so adding this series to the data file will automatically align and add a new data record.
dataframe.append(pd.DataFrame.from_dict(my_home_city).unstack().rename('my_home_city')) distance near far vehicle bike car bike car city NaN NaN NaN NaN my_home_city 1 0 0 1