将Pandas数据框插入Cassandra表 [英] Insert Pandas dataframe into Cassandra Table

查看:86
本文介绍了将Pandas数据框插入Cassandra表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档中,有一种方法可以将数据插入表中:

From the documentation , there is a way to insert data into table:

session.execute(
    """
    INSERT INTO users (name, credits, user_id)
    VALUES (%s, %s, %s)
    """,
    ("John O'Reilly", 42, uuid.uuid1())
)

必须在此处说明列名称.但是,就我而言,我有一个仅包含标题行和数据行的数据框,例如: "sepal_length" : 5.1,"sepal_width" : 3.5,"petal_length" : 1.4 ,"petal_width" : 0.2, "species" : "Iris".

The column name have to be stated there. However, in my case, I have a dataframe which has only a header row and a row of data, for example: "sepal_length" : 5.1,"sepal_width" : 3.5,"petal_length" : 1.4 ,"petal_width" : 0.2, "species" : "Iris" .

用户将为我的API提供信息以连接到他们特定的Cassandra数据库的表,该表包含存储在数据框中的列名.我该如何相对于映射到表的列标题插入数据帧的数据,而不必像文档中所述那样对列名称进行实际的硬编码,因为标题在不同情况下是不同的.

The user will provide the information for my API to connect to their particular Cassandra database's table which contain the columns name that stored in the dataframe. How can I insert the dataframe's data with respect to the column header mapped to the table without actually hardcode the column name like stated in the documentation since the headers are not the same for different cases.

我正在尝试实现以下目标:

I am trying to achieve something like this:

def insert_table(df, table_name, ... #connection details):
    #Set up connection and session
    session.execute(
        """
        INSERT INTO table_name(#df's column header)
        VALUES (%s, %s, %s)
        """,
        (#df's data for the only row)
    ) 

我发现了,但实际上我只需要一个简单的插入操作即可.

I discovered this but I actually just need a simple insert operation.

推荐答案

您可以使用以下命令获取数据框的列名

You can get the Dataframe's column names with the following

column_names = list(my_dataframe.columns.values)

您可以重写insert_table(...)以接受列名称列表作为参数.

You could rewrite insert_table(...) to accept the list of column names as an argument.

例如,可以使用字符串替换来形成CQL语句:

For example, string substitution can be used to form the CQL statement:

cql_query = """
    INSERT INTO {table_name} ({col_names})
    VALUES (%s, %s, %s)
    """.format(table_name="my_table", col_names=','.join(map(str, column_names)))
...

这篇关于将Pandas数据框插入Cassandra表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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