如何将Dataframe数据存储到Firebase Storage? [英] How to store Dataframe data to Firebase Storage?
问题描述
鉴于其中包含一些数据的pandas数据框,什么是将这些数据存储到Firebase的最佳方法?
Given a pandas Dataframe which contains some data, what is the best to store this data to Firebase?
我应该将数据框转换为本地文件(例如.csv,.txt),然后将其上传到Firebase存储,还是可以直接存储熊猫数据框而不进行转换?还是有更好的最佳做法?
Should I convert the Dataframe to a local file (e.g. .csv, .txt) and then upload it on Firebase Storage, or is it also possible to directly store the pandas Dataframe without conversion? Or are there better best practices?
更新01/03 -到目前为止,我已经提供了此解决方案,该解决方案要求在本地写入一个csv文件,然后读取并上传,然后删除本地文件.但是我怀疑这是最有效的方法,因此我想知道它是否可以做得更好,更快?
Update 01/03 - So far I've come with this solution, which requires writing a csv file locally, then reading it in and uploading it and then deleting the local file. I doubt however that this is the most efficient method, thus I would like to know if it can be done better and quicker?
import os
import firebase_admin
from firebase_admin import db, storage
cred = firebase_admin.credentials.Certificate(cert_json)
app = firebase_admin.initialize_app(cred, config)
bucket = storage.bucket(app=app)
def upload_df(df, data_id):
"""
Upload a Dataframe as a csv to Firebase Storage
:return: storage_ref
"""
# Storage location + extension
storage_ref = data_id + ".csv"
# Store locally
df.to_csv(data_id)
# Upload to Firebase Storage
blob = bucket.blob(storage_ref)
with open(data_id,'rb') as local_file:
blob.upload_from_file(local_file)
# Delete locally
os.remove(data_id)
return storage_ref
推荐答案
如果只想减少代码长度以及创建和删除文件的步骤,则可以使用 upload_from_string
:
if you just want to reduce code length and the steps of creating and deleting files, you can use upload_from_string
:
import firebase_admin
from firebase_admin import db, storage
cred = firebase_admin.credentials.Certificate(cert_json)
app = firebase_admin.initialize_app(cred, config)
bucket = storage.bucket(app=app)
def upload_df(df, data_id):
"""
Upload a Dataframe as a csv to Firebase Storage
:return: storage_ref
"""
storage_ref = data_id + '.csv'
blob = bucket.blob(storage_ref)
blob.upload_from_string(df.to_csv())
return storage_ref
这篇关于如何将Dataframe数据存储到Firebase Storage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!