Python.transpose()在转换字典数据时出错

Python.transpose()在转换字典数据时出错,python,pandas,Python,Pandas,AttributeError:“非类型”对象没有属性“转置” 我一直在尝试将单元格提取为字典(从pandas dataframe),并尝试与现有数据连接 例如,我有一个csv文件,其中包含两列id,device\u type。列device\u type中的每个单元格都包含字典数据。我试图用原始数据进行拆分和添加 试着做下面的事情 --csv文件示例 ID,device_type 3c30ee03047b478,{"060379800281":11,"061110053031":5,"060

AttributeError:“非类型”对象没有属性“转置”

我一直在尝试将单元格提取为字典(从pandas dataframe),并尝试与现有数据连接

例如,我有一个csv文件,其中包含两列
id
device\u type
。列
device\u type
中的每个单元格都包含字典数据。我试图用原始数据进行拆分和添加

试着做下面的事情

--csv文件示例

ID,device_type
3c30ee03047b478,{"060379800281":11,"061110053031":5,"060372062002":5}
f5d639a64a88496099,{}
--查找如下所示的输出

id,device_type,ttype,tvalue
3c30ee03047b478,{"060379800281":11,"061110053031":5,"060372062002":5},"060379800281",11
3c30ee03047b478,{"060379800281":11,"061110053031":5,"060372062002":5},"061110053031",5
3c30ee03047b478,{"060379800281":11,"061110053031":5,"060372062002":5},"060372062002",5
f5d639a64a88496099,{},NIL,NIL

sf['visitor_home_cbgs'].fillna("{}", inplace=True)
sf.transpose()
避免原地=真

sf['visitor_home_cbgs'].fillna("{}").transpose()
当您给出
inplace=True
时,它将转换相同的数据帧并返回null。 如果要使用
inplace=True
,请执行以下操作

id,device_type,ttype,tvalue
3c30ee03047b478,{"060379800281":11,"061110053031":5,"060372062002":5},"060379800281",11
3c30ee03047b478,{"060379800281":11,"061110053031":5,"060372062002":5},"061110053031",5
3c30ee03047b478,{"060379800281":11,"061110053031":5,"060372062002":5},"060372062002",5
f5d639a64a88496099,{},NIL,NIL

sf['visitor_home_cbgs'].fillna("{}", inplace=True)
sf.transpose()
从列值创建行的步骤 一种解决方案是迭代数据帧行,并使用所需的列和值创建新的数据帧

import json
def extract_JSON(row):
    df2 = pd.DataFrame(columns=['ID', 'device_type', 'ttype', 'tvalue'])
    device_type = row['device_type']
    dict = json.loads(device_type)
    for key in dict:
        df2.loc[len(df2)] = [row['ID'], row['device_type'], key, dict[key]]
    if df2.empty:
        df2.loc[0] = [row['ID'], row['device_type'], '', '']
    return df2
df3 = pd.DataFrame(columns=['ID', 'device_type', 'ttype', 'tvalue'])
for _, row in df.iterrows():
    df3 = df3.append(extract_JSON(row))
df3

fillna函数不返回任何内容,即None,您正试图对None对象调用转置函数。这就是出现此错误的原因。是否可以将分区单元格拆分为若干列?在我的示例中,有3行?。我有更新的问题与样本数据