我想把一個資料框架分成幾塊(例如:如果我們有100行,我把它們分成20塊),對于每個有5個值的塊,我需要在這個塊的資料上應用5個更新查詢(5個不同的表)。
我怎樣才能完成這個任務,因為我是個新手,一邊作業一邊學習,你能建議一下這個方法嗎?
for item in np.array_split(df1, 10)。
print(item) ##I was able to divide into chunks.
for i,j in item.iterrows()。
print(item.iloc[i]['ColumnName'] )
我的想法是在這個列印陳述句之后添加更新查詢行。
但是這段代碼給出了一個例外。
Traceback (most recent call last):
檔案 "/Users/gd/Documents/myproj/test.py", line 63, in < module>
func()
檔案 "/Users/gd/Documents/myproj/test.py", line 45, in dedupe_pe
print(item.iloc[i]['ColumnName'] )
檔案 "/Users/gd/Documents/myproj/lib/python3.9/Site-packages/pandas/core/indexing.py", line 931, in __getitem__
return self._getitem_axis(may_callable, axis=axis)
檔案 "/Users/gd/Documents/myproj/lib/python3.9/Site-packages/pandas/core/indexing.py", line 1566, in _getitem_axis
self._validate_integer(key, axis)
檔案 "/Users/gd/Documents/myproj/lib/python3.9/Site-packages/pandas/core/indexing.py", line 1500, in _validate_integer
raise IndexError("單一位置的索引器超出范圍")
IndexError: single positional indexer is out-of-bound
uj5u.com熱心網友回復:
item.iterrows()產生一個行索引,和行本身,所以你可以嘗試如下:
for item in np.array_split(df1, 10)。)
print(item) ##I was able to divide into chunks.
item["sql"] = "UPDATE" item["table_name"] " SET column1 = '" item["ColumnName_DATA"] "' WHERE condition"
for i, j in item.iterrows()。
print(j['ColumnName'/span>])
print(j['sql'])
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/309321.html
標籤:
下一篇:多個圖表不能用子圖函式作業?蟒蛇
