3

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 ?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Sainita
  • 332
  • 1
  • 4
  • 16

2 Answers2

3

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']
    })
Ashish
  • 6,791
  • 3
  • 26
  • 48
2

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.

maow
  • 2,712
  • 1
  • 11
  • 25
  • Can you please explain a little, how it works. Want to learn – Sainita Dec 14 '20 at 12:09
  • 1
    I think there are several places where this is already explained very well. See for example https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas or the (excellent) pandas User guide https://pandas.pydata.org/pandas-docs/stable/user_guide/basics.html#iteration – maow Dec 14 '20 at 12:26