I want to store a DataFrame
object as a value of the column of a row:
Here's a simplified analogy of what I want to achieve.
>>> df = pd.DataFrame([[1,2,3],[2,4,6]], columns=list('DEF'))
>>> df
166: D E F
0 1 2 3
1 2 4 6
I created a new DataFrame and add a new column on the go as I insert the new DataFrame
object as a value of the new column. Please refer to the code.
>>> df_in_df = pd.DataFrame([[11,13,17],[19, 23, 31]], columns=list('XYZ'))
>>> df.loc[df['F'] == 6, 'G'] = df_in_df
>>> df
D E F G
0 1 2 3 NaN
1 2 4 6 NaN
>>> df.loc[df['F'] == 6, 'G'].item()
nan
>>> # But the below works fine, i.e. when I insert an integer
>>> df.loc[df['F'] == 6, 'G'] = 4
>>> df
>>> D E F G
0 1 2 3 NaN
1 2 4 6 4.0
>>> # and to verify
>>> df.loc[df['F'] == 6, 'G'].item()
4.0
BTW I have managed to find a workaround over this by pickling the DataFrame into a string but I don't feel any good about it:
df.loc[df['F'] == 6, 'G'] = pickle.dumps(df_in_df)
>>> df
187: D E F G
0 1 2 3 NaN
1 2 4 6 ccopy_reg\n_reconstructor\np0\n(cpandas.core.f...
>>> revive_df_from_df = pickle.loads(df.loc[df['F'] == 6, 'G'].item())
>>> revive_df_from_df
191: X Y Z
0 11 13 17
1 19 23 31
I started using pandas today itself after referring through pandas in 10 mins, So I don't know the conventions, Any better ideas ? Thanks!