lundi 30 avril 2018

Why does an IF inside a FOR loop not work on a DataFrame? Python

I am looping over a DataFrame, using an if statement inside a for loop as follows. However, of all the elements i that enter the for loop, only the first one reaches the if statement. My code works correctly when I would pass lists to it, but not when I pass DataFrames. How to solves this? I have tried the solutions mentioned in Pythonic way to combine FOR loop and IF statement. But they do not work for my DataFrame.

DataFrame:

   'Sentence'                                   'First2'     
0  If this is a string what does it say?        what does    
1  And this a string, should it say more?       should it    
2  This is yet another string.          

My code looks as follows:

import pandas as pd    
a = df1['Sentence']
b = df2['First2'] 

def func(r):
    for i in b:
        if i in r:
            q = r[r.index(i):] #The code seems to loop over all r's but not over all i's. 
            return q
        else:
            return ''

df1['Clauses'] = a.apply(func)

This is the result:

what does it say?

This is correct but incomplete. The code seems to loop over all r's but not over all i's. How to get the desired result, as below?

what does it say?
should it say more?

Aucun commentaire:

Enregistrer un commentaire