Python-行到列标题中列表中的字符串以及计数为值
熊猫数据帧Python-行到列标题中列表中的字符串以及计数为值,python,pandas,Python,Pandas,熊猫数据帧df看起来像- fileName objectsIdentified objectName file_01.jpg 1, 2, 3 obj1, obj2, obj3 file_02.jpg 2, 3 obj2, obj3 file_03.jpg 1, 2, 4, 2 obj1, obj2, obj4, obj2 类型(df['objectName'].iloc[0]是列表 类型(df['objectNa
df
看起来像-
fileName objectsIdentified objectName
file_01.jpg 1, 2, 3 obj1, obj2, obj3
file_02.jpg 2, 3 obj2, obj3
file_03.jpg 1, 2, 4, 2 obj1, obj2, obj4, obj2
类型(df['objectName'].iloc[0]
是列表类型(df['objectName'].iloc[0][0]
是字符串
问题:
如何将objectName中的项作为单独的列获取,并将计数作为值
预期产出:
fileName objectsIdentified objectName obj1 obj2 obj3 obj4
file_01.jpg 1, 2, 3 obj1, obj2, obj3 1 1 1
file_02.jpg 2, 3 obj2, obj3 1 1
file_03.jpg 1, 2, 4, 2 obj1, obj2, obj4, obj2 1 2 1
您只需使用python中的
concat
和get_dummies
函数即可获得上述内容:
df_result = pd.concat([df[['fileName','objectsIdentified']], pd.get_dummies(df['objectName'].apply(pd.Series))], axis=1)
我假设df
是您的初始数据帧,df\u result
是您的输出数据帧。再添加一行:
df = df.join(pd.get_dummies(pd.DataFrame(df['objectName'].tolist()).stack()).sum(level=0).replace(0, ''))
到您的代码,则df
将变为:
fileName objectsIdentified objectName obj1 obj2 obj3 \
0 file_01.jpg [1, 2, 3] [obj1, obj2, obj3] 1 1 1
1 file_02.jpg [2, 3] [obj2, obj3] 1 1
2 file_03.jpg [1, 2, 4, 2] [obj1, obj2, obj4, obj2] 1 2
obj4
0
1
2 1
@ash16win我看到重复的列,如0_obj2和1_obj2@AccLok介意投票吗?