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 ?
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']
})
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With