简体   繁体   English

如何在 pandas dataframe 上迭代以在每次迭代中获取多行

[英]How iterate on a pandas dataframe to take a number of rows for each iteration

I have a pandas Dataframe.我有一个 pandas Dataframe。

DF.shape = (13096,27)

I want to iterate on the dataframe and for each iteration I take a shape of (50, 25) .我想在 dataframe 上进行迭代,并且每次迭代我都采用shape of (50, 25) I mean by 25, the first 25 columns.我的意思是 25,前 25 列。

I did it using the following code:我使用以下代码做到了:

for i in test_df.iterrows():
        df1 = test_df.iloc[:50, 0:25]
        df1 = np.array(df1)

        seq_test_array = df1[newaxis, :, :]
        print('df1', seq_test_array.shape)

        #a = np.arange(10)
        #for i in np.nditer(seq_test_array):
        predictions = model.predict_classes(seq_test_array,verbose=1, batch_size=50)
        fig_verify = plt.figure(figsize=(5, 5))
        plt.plot(predictions, color="blue")
        plt.plot(predictions, color="green")
        plt.title('prediction')
        plt.ylabel('value')
        plt.xlabel('row')
        plt.show()

        print('predictions', predictions)
        preds = model.predict(seq_test_array)
        print('preds', preds)
        prediction = np.argmax(preds)
        print('prediction', prediction)

I dislayed the figure but they are empty.我显示了这个数字,但它们是空的。 And the predictions, pred values the same (results of the print):并且预测,pred 值相同(打印结果):

predictions [[1]]
preds [[0.9416911]]
prediction 0
df1 (1, 50, 25)

Is because my code fault?是因为我的代码错误吗?

Could you please help me?请你帮助我好吗? Thanks谢谢

You could try something like this:你可以尝试这样的事情:

sliced=50  
for i in range(0,len(df)-(sliced-1),sliced):
    subdf=df.iloc[i:i+sliced,df.columns[:-2]]
    ....
    #the rest of your code 

So, for example:因此,例如:

import numpy as np
import pandas as pd


N_rows=6
N_cols=5
df = pd.DataFrame(np.zeros((N_rows, N_cols)))
print(df)

sliced=2
for i in range(0,len(df)-(sliced-1),sliced):
    subdf=df.iloc[i:i+sliced,df.columns[:-2]]
    print(subdf)
    print(subdf.shape)

Output: Output:

df
     0    1    2    3    4
0  0.0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0  0.0
3  0.0  0.0  0.0  0.0  0.0
4  0.0  0.0  0.0  0.0  0.0
5  0.0  0.0  0.0  0.0  0.0


Iterations:

     0    1    2
0  0.0  0.0  0.0
1  0.0  0.0  0.0
(2, 3)
     0    1    2
2  0.0  0.0  0.0
3  0.0  0.0  0.0
(2, 3)
     0    1    2
4  0.0  0.0  0.0
5  0.0  0.0  0.0
(2, 3)

So, as you can see, each iteration it takes a shape of (2,3) it means (sliced, len(df.columns)-2) , so in your case it will be (50, 25) .因此,如您所见,每次迭代都采用(2,3)的形状,这意味着(sliced, len(df.columns)-2) ,因此在您的情况下它将是(50, 25)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM