一個與format
可能的解決方案,如果唯一單調指數(0,1,2,...
):
for i, row in testDF.iterrows():
print("Currently on row: {}; Currently iterrated {}% of rows".format(i, (i + 1)/len(testDF.index) * 100))
樣品:
np.random.seed(1332)
testDF = pd.DataFrame(np.random.randint(10, size=(10, 3)))
print (testDF)
0 1 2
0 8 1 9
1 4 3 5
2 0 1 3
3 1 8 6
4 7 4 7
5 7 5 3
6 7 9 9
7 0 1 2
8 1 3 4
9 0 0 3
for i, row in testDF.iterrows():
print("Currently on row: {}; Currently iterrated {}% of rows".format(i, (i + 1)/len(testDF.index) * 100))
Currently on row: 0; Currently iterrated 10.0% of rows
Currently on row: 1; Currently iterrated 20.0% of rows
Currently on row: 2; Currently iterrated 30.0% of rows
Currently on row: 3; Currently iterrated 40.0% of rows
Currently on row: 4; Currently iterrated 50.0% of rows
Currently on row: 5; Currently iterrated 60.0% of rows
Currently on row: 6; Currently iterrated 70.0% of rows
Currently on row: 7; Currently iterrated 80.0% of rows
Currently on row: 8; Currently iterrated 90.0% of rows
Currently on row: 9; Currently iterrated 100.0% of rows
EDI T:
如果一些自定義的索引值,溶液zip
和numpy.arange
通過length of index
什麼是相同的length of df
:
np.random.seed(1332)
testDF = pd.DataFrame(np.random.randint(10, size=(10, 3)), index=[2,4,5,6,7,8,2,1,3,5])
print (testDF)
0 1 2
2 8 1 9
4 4 3 5
5 0 1 3
6 1 8 6
7 7 4 7
8 7 5 3
2 7 9 9
1 0 1 2
3 1 3 4
5 0 0 3
for i, (idx, row) in zip(np.arange(len(testDF.index)), testDF.iterrows()):
print("Currently on row: {}; Currently iterrated {}% of rows".format(idx, (i + 1)/len(testDF.index) * 100))
Currently on row: 2; Currently iterrated 10.0% of rows
Currently on row: 4; Currently iterrated 20.0% of rows
Currently on row: 5; Currently iterrated 30.0% of rows
Currently on row: 6; Currently iterrated 40.0% of rows
Currently on row: 7; Currently iterrated 50.0% of rows
Currently on row: 8; Currently iterrated 60.0% of rows
Currently on row: 2; Currently iterrated 70.0% of rows
Currently on row: 1; Currently iterrated 80.0% of rows
Currently on row: 3; Currently iterrated 90.0% of rows
Currently on row: 5; Currently iterrated 100.0% of rows
爲什麼你要使用循環開始?最有可能是更好的方法。如果必須,那麼可以使用'enumerate'輕鬆計算進度,該枚舉返回當前行的索引(以及行本身),它可以除以總行數。 (testDF.iterrows()):... progress = index/len(testDF)' – DeepSpace
我正在使用iterrows循環,因爲我使用地理編碼數據創建了一個新列。大部分允許您進行地理編碼的服務都有限制,因此我在循環中也添加了0.1秒的延遲。 – christaylor