首页 > 解决方案 > 如何迭代熊猫数据框中的行,如果满足条件,则将其打印在适当的 Excel 表中?

问题描述

我有一个数据框,其中一些列是空的。我在 if 语句中有一个标准,如果每行中的某些列为空,则整行从 excel 表中的第 3 行开始转到被拒绝的数据表。Otehrwise,它也将是 Excel 表中从第 3 行开始的已接受数据表中的一行。额外的事情是为每个 excel 工作表中的每个打印行添加边框

目前,整个数据框都打印在每个 Excel 工作表中。

df = df[['Lastname', 'Firstname','Company','Title','Willing_to_share','Willing_to_introduce','work_phones','Work_email','Work_Street','Work_City','Work_State','Work_Zip','Personal_Street','Personal_City','Personal_State','Personal_Zip','mobile_phones','Personal_email','Note','Note_Category']]
##print(df)
## Lastname Firstname          Company Title Willing_to_share  \
0       Doe      Jane                                           
1   Malcoun       Joe  8/28/2019 14:29                          
2   Ramirez    Morgan                                           
3     Burki     Roman                                           
4      None    Jordan                                           
5      None                                                     
6  Zachuani     Reemo                                           
7    Suarez   Geraldo     
 Willing_to_introduce work_phones              Work_email  \
0                       5678743546        j@greenbriar.com   
1                             None        ceo@nutshell.com   
2                       3338765438      mramirez@nerdy.com   
3                       5468756098           burki@bvb.com   
4                             None  jordanw45490@gmail.com   
5                             None                   ronny   
6                             None                           
7                             None   

              Work_Street      Work_City Work_State Work_Zip Personal_Street  \
0        54 George street  Ridge Springs         VA    25678                   
1     212 South Fifth Ave      Ann Arbor         MI    48103                   
2              567 one st     Birmingham         AL    45678                   
3  546 fourteen street Nw         Dallas         TX    54678                   
4                                                                              
5                                                                              
6                                                                              
7       456 yellow street                                                      

  Personal_City Personal_State Personal_Zip mobile_phones Personal_email Note  \
0                                              3245687907                       
1                                                    None                       
2                                              6780431874                       
3                                              0983457690                       
4                                                    None                       
5                                                    None                       
6                                                    None                       
7                                                    None                       

  Note_Category  
0                
1                
2                
3                
4                
5                
6                
7                



columns=df.columns
for column, row in df.iterrows():
    if (column[0] != ' ' and column[1] != ' ') and ((column[2] != ' ' 
    and column[3] != ' ') or (column[2] == ' ') and (column[6] != ' ') 
    or (column[16] != ' ') and (column[12] != ' ' and column[13] != ' 
    ' and column[14] != ' ' and column[15] != ' ') or (column[8] != ' 
    ' and column[9] != ' ' and column[10] != ' ' and column[11] != ' ' 
    and (column[7] != ' ' or column[17] != ' '))):
        wb = Workbook()
        ws = wb.active
        wb2 = Workbook()
        ws2 = wb2.active
        for r in dataframe_to_rows(df, index=False, header=False):
             ws.append(r)
             ws2.append(r)
             wb.save("Accepted Contacts.xlsx")
             wb2.save("Rejected Contacts.xlsx")
    else:
        wb = Workbook()
        ws = wb.active
        wb2 = Workbook()
        ws2 = wb2.active
        for r in dataframe_to_rows(df, index=False, header=False):
           ws.append(r)
           ws2.append(r)
           wb.save("Rejected Contacts.xlsx")
           wb2.save("Accepted Contacts.xlsx")

在图像中,它是接受的联系人工作表现在与数据的样子 这就是接受的联系人工作表现在与数据的样子

标签: pythonexcelpandasopenpyxl

解决方案


推荐阅读