下面是@flyingmeatball's answer演示:
讓我們生成測試DF:
In [56]: df = pd.DataFrame(np.random.rand(15, 3), columns=list('abc'))
In [57]: df
Out[57]:
a b c
0 0.022079 0.901965 0.282529
1 0.596452 0.096204 0.197186
2 0.034127 0.992500 0.523114
3 0.659184 0.447355 0.246932
4 0.441517 0.853434 0.119602
5 0.779707 0.429574 0.744452
6 0.105255 0.934440 0.545421
7 0.216278 0.217386 0.282171
8 0.690729 0.052097 0.146705
9 0.828667 0.439608 0.091007
10 0.988435 0.326589 0.536904
11 0.687250 0.661912 0.318209
12 0.829129 0.758737 0.519068
13 0.500462 0.723528 0.026962
14 0.464162 0.364536 0.843899
並保存到HDFStore(注意:不要忘記爲了使用data_columns=True
(或data_columns=[list_of_columns_to_index]
)以索引的所有列,我們希望where
子句中使用):
In [58]: store = pd.HDFStore(r'd:/temp/test_removal.h5')
In [59]: store.append('test', df, format='t', data_columns=True)
In [60]: store.close()
解決方案:
In [61]: store = pd.HDFStore(r'd:/temp/test_removal.h5')
的.remove()
方法應該返回刪除的行#:
In [62]: store.remove('test', where="a > 0.5")
Out[62]: 9
讓我們追加更改的行(由100
乘以):
In [63]: store.append('test', df.loc[df.a > 0.5] * 100, format='t', data_columns=True)
測試:
In [64]: store.select('test')
Out[64]:
a b c
0 0.022079 0.901965 0.282529
2 0.034127 0.992500 0.523114
4 0.441517 0.853434 0.119602
6 0.105255 0.934440 0.545421
7 0.216278 0.217386 0.282171
14 0.464162 0.364536 0.843899
1 59.645151 9.620415 19.718557
3 65.918421 44.735482 24.693160
5 77.970749 42.957446 74.445185
8 69.072948 5.209725 14.670545
9 82.866731 43.960848 9.100682
10 98.843540 32.658931 53.690360
11 68.725002 66.191215 31.820942
12 82.912937 75.873689 51.906795
13 50.046189 72.352794 2.696243
敲定:
In [65]: store.close()
爲什麼標記'r'? – akrun
當你說「大熊貓不支持更新記錄」時,你是指即時更新嗎?難道你不能把HDFStore中的表加載到df中,更新5%,並將整個表寫回(將模式更改爲'w'而不是追加)? – flyingmeatball
該表太大而無法完全加載到內存中。如果有一些解決辦法,我可以做到這一點? – CodeGeek123