我必须重命名各列,并使用以下代码提取我正在执行的数字:-
df.rename(columns={
0: 'latency_msecs.xmpp_login', 1: 'latency_msecs.total', 2: 'latency_msecs.message_send',
3: 'latency_msecs.admin_login', 4: 'latency_msecs.join_echo_channel',
5: 'latency_msecs.message_query',},
inplace=True)
df['latency_msecs.xmpp_login'] = df['latency_msecs.xmpp_login'].str.extract(r'(\d+.\d+)').astype('float')
df['latency_msecs.total'] = df['latency_msecs.total'].str.extract(r'(\d+.\d+)').astype('float')
df['latency_msecs.message_send'] = df['latency_msecs.message_send'].str.extract(r'(\d+.\d+)').astype('float')
df['latency_msecs.admin_login'] = df['latency_msecs.admin_login'].str.extract(r'(\d+.\d+)').astype('float')
df['latency_msecs.join_echo_channel'] = df['latency_msecs.join_echo_channel'].str.extract(r'(\d+.\d+)').astype('float')
df['latency_msecs.message_query'] = df['latency_msecs.message_query'].str.extract(r'(\d+.\d+)').astype('float')
它给了我一个干净的df:-
如何使重命名和提取动态化,以便如果我的列值为"latency_msecs.message_query": 68858.58869552612
,则可以使列名称为latency_msecs.message_query
,而值be 68858.58869552612
无关紧要如果有5行或500行。
我想放弃手动重命名和提取。
答案 0 :(得分:1)
您需要将json转换为pandas数据框。检查以下代码:
import json
import pandas as pd
sample_row = '{"latency_msecs.total": 71815.09041786194, "latency_msecs.message_query": 68858.58869552612, "latency_msecs.join_echo_channel": 10.111570358276367, "latency_msecs.message_send": 9.004592895507812, "latency_msecs.xmpp_login": 1941.108226776123, "latency_msecs.admin_login": 17.666339874267578}'
df = pd.json_normalize(json.loads(sample_row))
print(df)