Python 将具有多个值的字符串转换为dict?
我有一个数据帧,例如:Python 将具有多个值的字符串转换为dict?,python,string,pandas,dictionary,Python,String,Pandas,Dictionary,我有一个数据帧,例如: Path Attribute /home/accessbile/wheelchair type:threshold;width:42;gaurded:no /home/accessible/wheelcahir type:threshold;width:45 /home/accessbile/armchair weight(lbs):100;width:30 /home/accessible/armchai
Path Attribute
/home/accessbile/wheelchair type:threshold;width:42;gaurded:no
/home/accessible/wheelcahir type:threshold;width:45
/home/accessbile/armchair weight(lbs):100;width:30
/home/accessible/armchair type:foldind;weight(lbs):100;width:30
我需要获取valuestype、width、weight、gaurded等属性中的唯一计数,例如。。关于路径
输出:
PATH Attributes count
/home/accessbile/wheelchair type 2
/home/accessbile/wheelchair width 2
/home/accessbile/wheelchair gaurded 1
/home/accessbile/armchair weight(lbs) 2
/home/accessbile/armchair width 2
/home/accessbile/armchair type 1
已尝试通过ast.eval将属性转换为dict,并将键作为唯一计数。。但不起作用首先,检查路径中所有行的拼写,一旦修复,您可以执行以下操作:
df.Attribute=df.Attribute.str.split(";")
df_new=pd.DataFrame([[x] + [z] for x, y in df.values for z in y],columns=df.columns)
df_new['Attribute']=df_new.Attribute.str.split(":").str[0]
df_new.groupby('Path')['Attribute'].value_counts().reset_index(0).\
rename(columns={'Attribute':'Count'}).reset_index()
Attribute Path Count
0 type /home/accessbile/wheelchair 2
1 width /home/accessbile/wheelchair 2
2 gaurded /home/accessbile/wheelchair 1
3 weight(lbs) /home/accessible/armchair 2
4 width /home/accessible/armchair 2
5 type /home/accessible/armchair 1