pandas 将 Dataframe 中包含.“”的列转换为字典

xxb16uws  于 2023-02-14  发布在  其他
关注(0)|答案(2)|浏览(181)

我想把df转换成df1

df = pd.DataFrame({'A': [1], 'in.1': [8977], 'in.2': [8977], 'B': [
    {
        "C.i": 87387460,
        "C.j":233
}]})

df1 = pd.DataFrame({'A': [1], 'in':{'1': [8977], '2': [8977]}, 'B': [
    {"C":{
        "i": 87387460,
        "j":233}
}]})

我尝试使用递归函数,但没有运气.
我的代码:

def convert_df(df):
    if df.shape[0] == 0:
        return []
    elif df.shape[0] == 1 and df.shape[1] == 1:
        return df.iloc[0, 0]
    elif df.shape[1] == 1:
        return [convert_df(pd.DataFrame(val)) for val in df[df.columns[0]].tolist()]
    else:
        return [{col_name: convert_df(pd.DataFrame(val)) for col_name, val in row.to_dict().items()} for i, row in df.iterrows()]
qgelzfjb

qgelzfjb1#

若要将DataFrame中名称中包含点.的列转换为字典,可以执行以下操作:

def convert_to_dict(df, col_name):
    out = {}
    for row in df[col_name].tolist():
        for key, val in row.items():
            sub_keys = key.split('.')
            d = out
            for sub_key in sub_keys[:-1]:
                if sub_key not in d:
                    d[sub_key] = {}
                d = d[sub_key]
            d[sub_keys[-1]] = val
    return out

col_name = 'B' 
df1 = convert_to_dict(df, col_name) 
df1 = pd.DataFrame({col_name: [df1]})
5uzkadbs

5uzkadbs2#

预期的输出是不明确的,但是我假设您可能希望使用to_dict

cols = list(df.filter(like='in'))

df1 = (df.drop(columns=cols)
         .assign(**{'in': pd.Series(df[cols].to_dict('index'))})
      )

输出:

A                              B                            in
0  1  {'C.i': 87387460, 'C.j': 233}  {'in.1': 8977, 'in.2': 8977}

相关问题