lundi 25 juillet 2016

Python/Pandas if statement and index

I'm sure there is a simple solution to this problem but I cannot seem to find it.

I am trying to check if an age from a list is in the age column of my dataframe. However, it is only comparing to the index and not the column.

Here is a simplified piece of code from my program:

def findages(data,ages):
    for age in ages:
        if age in data['age']:
            print('yes')
        else:
            print('no')

I have also tried this:

def findages(data,ages):
    for age in ages:
        if age in data.loc[data['age']]:
            print('yes')
        else:
            print('no')

the dataframe looks like this

                 age     x     Lambda             L
0       1.258930e+05  0.01       91.0  5.349000e+25
1       1.258930e+05  0.01       94.0  1.188800e+26
2       1.258930e+05  0.01       96.0  1.962700e+26
3       1.258930e+05  0.01       98.0  3.169400e+26   
4       1.258930e+05  0.01      100.0  5.010800e+26

and the list like this:

ages = ([125893.0, 4e7,5e9])

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire