How to convert a Cassandra map to a Pandas Dataframe

I want to read data from a cassandra type column family map<string, int>and want to convert it to a Pandas dataframe. What else do I want to use to train the model in python, as mentioned here in the classification of iris views.

If I used csv to train the model. Then it would look like this:

label,  f1, f2, f3, f4, f5
  0  ,  11 , 1, 6 , 1,  2  
  1  ,  5,   5, 1 , 2,  6
  0  ,  12,  9, 3 , 6,  8
  0  ,  9,  3,  8,  1,  0 

Cassandra Column Family:

                  FeatureSet                    |   label

{'f1': 11, 'f2': 1, 'f3': 6, 'f4': 1, 'f5': 2}  |     0
{'f1': 5, 'f2':  5, 'f3': 1, 'f4': 2, 'f5': 6}  |     1
{'f1': 12, 'f2': 9, 'f3': 3, 'f4': 6, 'f5': 8}  |     0
{'f1': 9, 'f2': 3, 'f3': 8, 'f4': 1, 'f5': 0}   |     0

The code:

import pandas as pd
from sklearn2pmml import PMMLPipeline
from sklearn.tree import DecisionTreeClassifier
from cassandra.cluster import Cluster

CASSANDRA_HOST = ['172.16.X.Y','172.16.X1.Y1'] 
CASSANDRA_PORT = 9042
CASSANDRA_DB = "KEYSPACE"
CASSANDRA_TABLE = "COLUMNFAMILY"

cluster = Cluster(contact_points=CASSANDRA_HOST, port=CASSANDRA_PORT)
session = cluster.connect(CASSANDRA_DB)

sql_query = "SELECT * FROM {}.{};".format(CASSANDRA_DB, CASSANDRA_TABLE)

df = pd.DataFrame()

for row in session.execute(sql_query):  
            What should i write here and get X_train, Y_train in pandas dataframe 



iris_pipeline = PMMLPipeline([
    ("classifier", DecisionTreeClassifier())
])
iris_pipeline.fit(X_train, Y_train)
+2
source share
3 answers

You can use this approach :

import pandas as pd
from cassandra.cluster import Cluster

def pandas_factory(colnames, rows):
    return pd.DataFrame(rows, columns=colnames)

CASSANDRA_HOST = ['172.16.X.Y','172.16.X1.Y1'] 
CASSANDRA_PORT = 9042
CASSANDRA_DB = "KEYSPACE"
CASSANDRA_TABLE = "COLUMNFAMILY"

cluster = Cluster(contact_points=CASSANDRA_HOST, port=CASSANDRA_PORT)
session = cluster.connect(CASSANDRA_DB)

session.row_factory = pandas_factory
session.default_fetch_size = None

query = "SELECT * FROM {}.{};".format(CASSANDRA_DB, CASSANDRA_TABLE)

rslt = session.execute(query, timeout=None)
df = rslt._current_rows
+1
source

, OrderedMapSerializedKey Cassandra dict .


EDIT:

(0-) Cassandra (rows - , ​​Cassandra)

from cassandra.util import OrderedMapSerializedKey

def pandas_factory(colnames, rows):

    # Convert tuple items of 'rows' into list (elements of tuples cannot be replaced)
    rows = [list(i) for i in rows]

    # Convert only 'OrderedMapSerializedKey' type list elements into dict
    for idx_row, i_row in enumerate(rows):

        for idx_value, i_value in enumerate(i_row):

            if type(i_value) is OrderedMapSerializedKey:

                rows[idx_row][idx_value] = dict(rows[idx_row][idx_value])

    return pd.DataFrame(rows, columns=colnames)
+1

In addition to MaxU's answer, if you want to see your result as a data frame, all you have to do is add another line:

df = pd.DataFrame (rslt._current_rows)

0
source

Source: https://habr.com/ru/post/1685023/


All Articles