Home > Enterprise >  Partially merge pandas data fame columns into a dictionary column
Partially merge pandas data fame columns into a dictionary column

Time:04-12

Given a Pandas df:

        col1      col2      col3   col4
        a         1         2      56
        a         3         4      1
        a         5         6      1
        b         7         8      2
        b         9         10     -11
        c         11        12     9 
        ...

Using pandas how to reshape such data frame such that multiple columns are represented using one a dictionary with column names as keys:

        col1      dict_col
        a         { 'col2':1  ,'col3':2 , 'col4':56 }
        a         { 'col2':3  ,'col3':4 , 'col4':1  }
        a         { 'col2':5  ,'col3':6 , 'col4':1  }
        b         { 'col2':7  ,'col3':8 , 'col4':2  }
        b         { 'col2':9  ,'col3':10, 'col4':-11}
        c         { 'col2':11 ,'col3':12, 'col4':9  }
    
Note that values of that that this transformation needs to be done only with pandas and just for a part of the columns across all the data frame rows.

CodePudding user response:

Try this command:

pd.DataFrame({'col1': df['col1'].values, 'dict_col': df.drop('col1', axis=1).to_dict(orient='records')})

CodePudding user response:

dict_col = df.loc[:, ["col2", "col3","col4"]].to_dict(orient="records")
df2 = pd.DataFrame({"col1": df["col1"], "dict_col": dict_col})
print(df2)

CodePudding user response:

You can use this code :

import pandas as pd
df = pd.DataFrame({
    'col1': ['a', 'a', 'a', 'b', 'b', 'c'],
    'col2': [ 1, 3, 5, 7, 9, 11],
    'col3': [ 2, 4, 6, 8, 10, 12],
    'col4': [ 56, 1, 1, 2, -11, 9]
})

cols = ['col2', 'col3', 'col4']
lst = []
for _, row in df[cols].iterrows():
    lst.append({col: row[col] for col in cols})
df['dict_col'] = lst
df = df[['','dict_col']]

print(df)

Output :

  col1                              dict_col
0    a    {'col2': 1, 'col3': 2, 'col4': 56}
1    a     {'col2': 3, 'col3': 4, 'col4': 1}
2    a     {'col2': 5, 'col3': 6, 'col4': 1}
3    b     {'col2': 7, 'col3': 8, 'col4': 2}
4    b  {'col2': 9, 'col3': 10, 'col4': -11}
5    c   {'col2': 11, 'col3': 12, 'col4': 9}
  • Related