将带有标签的Pandas DF写入influxdb [英] Write pandas DF with tags to influxdb

查看:104
本文介绍了将带有标签的Pandas DF写入influxdb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 [3526行x 5列] DF,其中 col0 是时间, col1-col3 是标签,而col4 是我的价值.

I have this [3526 rows x 5 columns] DF, where col0 is time, col1-col3 are tags and col4 is my value.

                    0             1                  2         3      4
0     2017-09-29 22:41:51     10.2.95.5   C1195_LF470_SARF   0.0.1.1  11993
1     2017-09-29 22:41:37     10.2.52.7   CF643_RCZ70_SARM  0.0.1.16  12102
2     2017-09-29 22:41:39    10.2.102.7   C1345_BQS70_SARF  0.0.1.17  18173
3     2017-09-29 22:41:41   10.2.23.212   CN165_FS470_SAR8   0.0.0.7  23525
4     2017-09-29 22:41:38     10.2.96.4   CF832_UY570_SARM   0.0.1.4   6162

因此,我想将该DF写入influxdb.我会的...

So, I want to write that DF into influxdb. I'll do ...

timeValues  = df[ ['col0','col4'] ]
tags        = { 'col1': df[['col1']], 'col2': df[['col2']], 'col3':df[['col3']] }

dbConnDF = DataFrameClient(dbAddress, dbPort, dbUser, dbPassword, dbName)
dbConnDF.write_points(dbName, tbName, timeValues, tags = tags)

在那之后,我得到了错误

After that, I get the error

必须是带有Datetime或PeriodIndex的DataFrame

Must be DataFrame with Datetime or PeriodIndex

但是,如果我使用此命令逐行插入...

However, if I do insert row by row using this...

dbConnQRY = InfluxDBClient(dbAddress, dbPort, dbUser, dbPassword, dbName)
dbConnQRY.write_points(bodyDB)

其中:

bodyDB = [{
    "measurement": tbName,
    "tags":
    {
        "col1": col1,
        "col2": col2,
        "col3": col3
    },
    "time": col0,
    "fields":
    {
        "col4": col4
    }
}]

...我一点都没错.因此,当我尝试一次插入整个DF时,就会出现问题.

... I get no error at all. So the problem appears when I try to insert the whole DF at once.

如何告诉influxdb我的索引是 col0 以避免错误?

How do I tell influxdb that col0 is my index to avoid the error?

谢谢!

推荐答案

Create an index column for dataframe
timeValues  = df[ ['col4'] ]
timeValues.index  = df[ ['col0'] ]

跟着

dbConnDF = DataFrameClient(dbAddress, dbPort, dbUser, dbPassword, dbName)
dbConnDF.write_points(dbName, tbName, timeValues, tags = tags)

那应该解决索引问题.

这篇关于将带有标签的Pandas DF写入influxdb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆