将 df 列转换为 json df 列

convert df column into json df column

我有一个df

clearance_info
4431,by category 2,2022-02-03
4231,by category 1,2022-02-03
4331,by category 3,2022-02-03
4431,by category 4,2022-02-03

如何转换成

clearance_info
{"price": 4431 ,"category" : "by category 2","timestamp" : "2022-02-03"}
{"price": 4231 ,"category" : "by category 1","timestamp" : "2022-02-03"}
{"price": 4331 ,"category" : "by category 3","timestamp" : "2022-02-03"}
{"price": 4431 ,"category" : "by category 4","timestamp" : "2022-02-03"}

尝试了 orient split 但没有按预期工作。请提出建议。

Orient 仅在单独存在列时提供帮助,我希望这些像提到的那样固定。

你可以试试:

df[['price', 'category','timestamp']] = df['clearance_info'].str.split(',',expand=True,n=3)
df['price'] = df['price'].astype(int)
df['clearance_info'] = df[['price', 'category','timestamp']].apply(lambda row: row.to_json(), axis=1)
df.drop(['price', 'category','timestamp'], axis=1, inplace=True)

输出:

    clearance_info
0   {"price":4431,"category":"by category 2","time...
1   {"price":4231,"category":"by category 1","time...
2   {"price":4331,"category":"by category 3","time...
3   {"price":4431,"category":"by category 4","time...

to_dictorient="records"

cols = dict(enumerate(['price', 'category', 'timestamp']))

df.assign(
    clearance_info=
    df.clearance_info.str
      .split(',', expand=True)
      .rename(columns=cols)
      .to_dict('records')
)
    
                                      clearance_info
0  {'price': '4431', 'category': 'by category 2',...
1  {'price': '4231', 'category': 'by category 1',...
2  {'price': '4331', 'category': 'by category 3',...
3  {'price': '4431', 'category': 'by category 4',...

如果你想让它在 df

中持续存在
df = df.assign(
    clearance_info=
    df.clearance_info.str
      .split(',', expand=True)
      .rename(columns=cols)
      .to_dict('records')
)