Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please Guide to Loop through a DataFrame while Pushing to Firebase

I have a CSV file of data's which I want to push to Firebase using Python. The CSV has two columns title and description . I am successful in pushing the title columns, but unable to figure out how to push the description column as well.

This is my code :

import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
import pandas as pd

df = pd.read_csv('csv_data.csv')
mylist.append(df)
ref = db.reference('testnode')
for item in df['title'] : 
    ref.push().set({
        'title' : item,
        'description' : 'description here'
    })

This is the Output :

please check screenshot here

As you can see, the description key is hardcoded as : description here

How can i write the description to Firebase in the same way im writing the title values ?

like image 572
Sainita Avatar asked Feb 24 '26 11:02

Sainita


2 Answers

Your code just reading the title to read the rest you need to provide column name.

from csv import DictReader
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
with open('csv_data.csv', 'r') as read_obj:
    csv_dict_reader = DictReader(read_obj)
    for row in csv_dict_reader:
        ref.push().set({
            'title' : row['title'],
            'description' : row['description']
        })

For Specific Panda library :

for index, row in df.iterrows() : 
    ref.push().set({
        'title' : row['title'],
        'description' : row['description']
    })
like image 97
Ashish Avatar answered Feb 27 '26 00:02

Ashish


As you have both the item and the description in your DataFrame you could iterate over the complete row. Then row will hold a Series with the fields 'title' and 'description' which you can access as shown below

for _, row in df.iterrows() : 
    ref.push().set({
        'title' : row['title'],
        'description' : row['description']
    })

The _ is necessary as DataFrame.iterrows() returns the number of the row as well and you ignore it with this.

like image 23
maow Avatar answered Feb 27 '26 00:02

maow