如何在 python 3 中同時實作以下功能:
- 將列名和數值資料序列化為二進制檔案
- 重新打開檔案并附加額外的數字資料
例如以下資料:
import numpy as np
columns = ['a', 'b', 'c']
data = np.linspace(0, 1, num=10*3).reshape((10, 3))
data_for_appending = np.linspace(2, 3, num=10*3).reshape((10, 3))
我使用 numpy 的方法
這種方法允許保存資料并附加額外的資料。然而,列名丟失,加載需要多次呼叫 np.load。
# storing the data
with open('out.npy', 'wb') as f:
np.save(f, data)
np.save(f, data_for_appending)
# loading the data
with open('out.npy', 'rb') as f:
data1 = np.load(f)
data2 = np.load(f)
我對熊貓的態度
這種方法保存了資料和標題。但是,似乎不可能在單獨的呼叫中將資料附加到檔案中。
import pandas as pd
df = pd.DataFrame(data, columns=columns)
# storing the data
df.to_pickle('out.pickle')
# loading the data
df2 = pd.read_pickle('out.pickle')
uj5u.com熱心網友回復:
import pickle
# Write first df to pickle
data = {
"name": ["Joe", "Mike", "Tony", "Susan"],
"course": ["Masters", "Doctorate", "Graduate", "Bachelors"],
"age": [27, 23, 21, 19],
}
df = pd.DataFrame(data)
df.to_pickle(path)
# Create new row df
new_row = {"name": "Phil", "course": "Associates", "age": 30}
new_row_df = pd.DataFrame(new_row, index=[0])
print(f"{new_row_df}\n")
# read original df from pickle
pickled_df = pd.read_pickle(path)
# concat dfs
df_appended = pd.concat([new_row_df, pickled_df]).reset_index(drop=True)
# Dump concat df to pickle
with open(path, "wb") as f:
pickle.dump(df_appended, f)
# read concat df from pickle
df = pd.read_pickle(path)
print(df)
您可以在不讀取的情況下附加到檔案,但不會將 dfs 連接起來,它們是單獨的條目。您當然可以在一個回圈中讀取所有條目,并在稍后讀取檔案時進行連接。
# Add new entries
with open(path, "ab") as f:
pickle.dump(new_df, f)
# When ready to read and concat.
with open(path, "rb") as f:
entries = []
while True:
try:
entry = pickle.load(f)
except EOFError:
break
entries.append(entry)
df = pd.concat(entries).reset_index(drop=True)
print(df)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/519658.html
上一篇:兩個陣列之間的Numpy不同元素
