我目前正在组装一个熊猫数据帧。为此,我查询我的sql以准备数据。
mycursor.execute("select post_id,meta_value from wp_postmeta where meta_key = 'berufsgruppe' and post_id = %s",[id])
df_berufsgruppe=pd.DataFrame(data=mycursor.fetchall(), columns=['post_id','berufsgruppe'])以下值包含在sql中的几个位置。我要怎么做才能得到这些值呢?
a:1:{i:0;s:10: "employed";}
a:1:{i:0;s:5:"Miete";}
a:1:{i:0;s:2:"ja";}我需要“受雇”
非常感谢您的支持!
发布于 2021-06-27 14:40:03
你的df是这样的吗?
berufsgruppe
0 a:1:{i:0;s:10: "employed";}
1 a:1:{i:0;s:5:"Miete";}
2 a:1:{i:0;s:2:"ja";}如果是这样,那么您可以使用.str.extract()提取这些值。
df['value'] = df['berufsgruppe'].str.extract(r'.+"(.+)".+')
print(df)
berufsgruppe value
0 a:1:{i:0;s:10: "employed";} employed
1 a:1:{i:0;s:5:"Miete";} Miete
2 a:1:{i:0;s:2:"ja";} jahttps://stackoverflow.com/questions/68145941
复制相似问题