mgnfcnt2
mgnfcnt2

Reputation: 413

How to Add new Column From Frozen Tuple Map?

I have a cassandra database table and it has a column as paramaters and its type:

parameters map<text, frozen<tuple<text, text, bigint, double, text>>>,

I am trying to create a pandas df from this cassandra table:

def read_measurement_data():
    query = "select * from measurement"
    return pd.DataFrame(list(session.execute(query)))

measurement_df = read_measurement_data()

And after creating measurement_df I need to extract a column from frozen tuple map its' name is Speed.

How can I add this new column?

Upvotes: 0

Views: 98

Answers (2)

mgnfcnt2
mgnfcnt2

Reputation: 413

I solved my problem with following way:

def add_column(row):

    xnhct1 = row['parameters'].get('speed')

    if xnhct1 is not None:
        return xnhct1[3]

    return 0

measurement_table['speed'] = measurement_table.apply(lambda row: add_column(row), axis=1)

Upvotes: 0

Alex Ott
Alex Ott

Reputation: 87069

the values of CQL's tuple types are returned as Python tuple type, so you just need to extract your data correctly, and access data in tuple by index. Something like this:

rows = session.execute('SELECT parameters FROM test.p1')
for row in rows:
   print row.parameters.values()

I recommend first extract data from query, and only after that generate Pandas data frame

Upvotes: 1

Related Questions