python - 从 sql 响应中提取值
问题描述
我目前正在整理一个熊猫数据框。为此,我查询我的 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";}
我需要“就业
非常感谢您的支持!
解决方案
你的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";} ja
推荐阅读
- windows - Postgres 安装失败
- excel-formula - How to match same values in three columns and display a value of 3rd column into new column using Excel?
- swift - 在饼图外显示值
- html - Twitter-Card 元标签问题
- ta-lib - 在 Mac 上的 python 3.7 中安装 ta-lib 时遇到问题
- mapbox - 计算地图面积并动态设置缩放级别
- python - 如何将 JSON 数据转换为指定的 Pandas DataFrame
- php - 类和扩展类中的 PHP 'USE' 运算符
- typescript - Sharepoint Online 使用 PNP 库,我从 AD(对象)与用户共享了一个文件,问题是我无法取消共享
- r - R中的光栅包问题:找不到对象'_raster_availableRAM'