[英]Convert Pandas DataFrame to & from In-Memory Feather
使用pandas==0.25.2
可以通過以下方式實現:
import pandas
import io
df = pandas.DataFrame(data={'a': [1, 2], 'b': [3.0, 4.0]})
buf = io.BytesIO()
df.to_feather(buf)
output = pandas.read_feather(buf)
然后調用output.head(2)
返回:
a b
0 1 3.0
1 2 4.0
如果您的DataFrame
具有多個索引,則可能會看到類似以下的錯誤
ValueError:feather不支持對索引進行序列化; 您可以.reset_index()使索引成為列
在這種情況下,你需要調用.reset_index()
之前to_feather
,並調用.set_index([...])
后read_feather
我要添加的最后一件事是,如果您正在使用BytesIO
,則需要在寫入羽化字節后返回0。 例如:
buffer = io.BytesIO()
df.reset_index(drop=False).to_feather(buffer)
buffer.seek(0)
s3_client.put_object(Body=buffer, Bucket='bucket', Key='file')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.