我使用pandas來讀取大尺寸的檔案,所以我使用:
我使用pandas來讀取大尺寸的檔案。
for df_small in pd. read_csv("largefile.txt"/span>, chunksize=1000,
iterator=True, low_memory=False)。
我需要為每個塊添加一個列'seqnum',它將是所有塊的一致索引:
for df_small in pd. read_csv("largefile.txt"/span>, chunksize=1000,
iterator=True, low_memory=False)。
df_small ['seqnum'] == df_small .index.value
因此,對于第一塊,df_small ['seqnum']將是:
0
1
2
...
999 999 ...
但是第二塊的df_small ['seqnum']仍將是:
。0
1
2
...
999 999 ...
這不是我想要的,第二塊的理想df_small ['seqnum'] 應該是:
1000。
1001
1002
...
1999 1999 ...
有什么辦法可以做到這一點嗎?
uj5u.com熱心網友回復:
使用df_small的索引:
for df_small in pd. read_csv("data1.csv", chunksize=3,
iterator=True, low_memory=False)。
df_small['seqnum'] = df_small.index.value
print(df_small)
輸出:
Name seqnum # <- 1st iteration。
0 A 0
1 B 1
2 C 2
名稱seqnum # <- 2nd iteration
3 D 3
4 E4
5 F 5
名稱seqnum # <- 第三次迭代。
6 G 6
7 H 7
8 I 8
名稱seqnum # <- 第四次迭代
9 J 9
10 K10
11 L 11
uj5u.com熱心網友回復:
只需創建一個變數來跟蹤下一個塊的起始索引,如下所示:
seq_num = 0。
for df_small in pd.read_csv("largefile.txt"/span>, chunksize=1000,
iterator=True, low_memory=False)。
df['seqnum'] = df.index seq_num
seq_num = df.index[-1] 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/307670.html
標籤:
