Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 89350

How to iterate over rows in a pandas dataframe and if the condition is met it is printed in the appropriate excel sheet?

$
0
0

I have a dataframe in which some columns are empty. I have a criteria in the if statement in which if some of the columns in each row are empty the whole row goes to the rejected datasheet starting from row 3 in the excel sheet. Otehrwise, it will be a row in the accepted datasheet starting from row 3 in the excel sheet as well. An extra thing would be to add borders to each printed row in each excel worksheet

Currently the whole dataframe is printed in each excel worksheet.

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")

In the image it is how the accepted contacts worksheet looks like right now with the data This is how the accepted contacts worksheet looks like right now with the data


Viewing all articles
Browse latest Browse all 89350

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>