简体   繁体   English

如何使用python可视化从grafana导出csv数据

[英]How to visualize csv data export from grafana using python

When i export csv data from grafana with API latency i saw two kind of value is seconds and millisecond, how to convert it to seconds using python.当我以 API 延迟从 grafana 导出 csv 数据时,我看到两种值是秒和毫秒,如何使用 python 将其转换为秒。

time                 latency    
2022-09-30 06:20:00  957 ms
2022-09-30 07:25:00  6.63 s
2022-09-30 07:30:00  634 ms

More thing, with csv how i can visualize this data using python. plz更多信息,对于 csv,我如何使用 python 可视化此数据。plz

You can upload all the csv data into a pandas dataframe (you would use pd.read_csv() for this. From this you can use the df.apply() method to convert the seconds into miliseconds.您可以将所有 csv 数据上传到pandas dataframe (为此您可以使用pd.read_csv() 。由此您可以使用df.apply()方法将秒数转换为毫秒数。

The df.apply() calls the user defined convert() function and you can see how that splits the latency column into numbers and strings . df.apply()调用用户定义的convert() function ,您可以看到它如何将latency列拆分为numbersstrings

This would work:这会起作用:

import io
import pandas as pd


x = '''
time                 latency    
2022-09-30 06:20:00  957 ms
2022-09-30 07:25:00  6.63 s
2022-09-30 07:30:00  634 ms

'''

data = io.StringIO(x)

df = pd.read_csv(data, sep='\s\s+', engine='python')

def convert(x:str):
    ''' convert string to number ms'''
    a, b = x.split(' ')
    a = float(a)
    if b =='s':
        a = a*1000
    return a

df['ms'] = df['latency'].apply(convert)
df

result结果

                 time latency      ms
0  2022-09-30 06:20:00  957 ms   957.0
1  2022-09-30 07:25:00  6.63 s  6630.0
2  2022-09-30 07:30:00  634 ms   634.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM