pandas 删除文本并保留panda中的数字

aiqt4smr  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(143)

我有一个类似于这样的框架:


的数据
我想删除文本,只保留该框架中每列的数字。
预期的输出类似于这样:



到目前为止,我已经尝试了这个:

import json
import requests
import pandas as pd
URL = 'https://xxxxx.com'
req = requests.get(URL,auth=('xxx', 'xxx') )
text_data= req.text
json_dict= json.loads(text_data)
df = pd.DataFrame.from_dict(json_dict["measurements"])
cols_to_keep =['source','battery','c8y_TemperatureMeasurement','time','c8y_DistanceMeasurement']
df_final = df[cols_to_keep]
df_final = df_final.rename(columns={'c8y_TemperatureMeasurement': 'Temperature Or T','c8y_DistanceMeasurement':'Distance'})
for col in df_final:
 df_final[col] = [''.join(re.findall("\d*\.?\d+", item)) for item in df_final[col]]

字符串

xkrw2x1b

xkrw2x1b1#

您的代码缺少 import pandas as pd,并且无法访问数据,因为它需要凭据。
可以使用pandas.DataFrame.replace
示例数据:

df = pd.DataFrame({'a':['abc123abc', 'def456678'], 'b':['123a', 'b456']})

字符串
数据框架:

a           b
0   abc123abc   123a
1   def456678   b456


[^0-9.]替换所有非数字字符。

df.replace('[^0-9.]', '', regex=True)


输出量:

a       b
0   123     123
1   456678  456


编辑:这里的问题实际上是关于嵌套的JSON,而不是关于替换框架中的值。上面的语句不起作用的原因是因为数据在框架中保存为dicts。但是由于上面提到的解决方案通常是正确的,它不会编辑它。
修改后的答案:

from pandas.io.json import json_normalize
import requests
import pandas as pd

URL = 'https://wastemanagement.post-iot.lu/measurement/measurements?source=83512& pageSize=1000000000&dateFrom=2019-10-26&dateTo=2019-10-28'
req = requests.get(URL,auth=('xxxx', 'xxxx') )
text_data= req.text
json_dict= json.loads(text_data)
df= json_normalize(json_dict['measurements'])
df = df_final.rename(columns={'source.id': 'source', 'battery.percent.value': 'battery', 'c8y_TemperatureMeasurement.T.value': 'Temperature Or T','c8y_DistanceMeasurement.distance.value':'Distance'})
cols_to_keep =['source' ,'battery', 'Temperature Or T', 'time', 'Distance']
df_final = df[cols_to_keep]


输出量:

source  battery Temperature Or T    time                        Distance
0   83512   98.0    NaN                 2019-10-26T00:00:06.494Z    NaN
1   83512   NaN     23.0                2019-10-26T00:00:06.538Z    NaN
2   83512   NaN     NaN                 2019-10-26T00:00:06.577Z    21.0
3   83512   98.0    NaN                 2019-10-26T00:30:06.702Z    NaN
4   83512   NaN     23.0                2019-10-26T00:30:06.743Z    NaN

相关问题