'Catalog rows according to type conditions

I have a given dataFrame with four columns -

X1 X2 X3 X4
1 1.2 1.2 2
1 1.3 1.2 1.2
1 3.2 4.2 1
1.9 1.2 5.4 3

I want to add a new column by this condition - if X1 and X4 are integers - so 1, else 0 as "bug".

I try this:

x = []
for column in df:
        if isinstance(df['T1'][i], int) == True and isinstance(df['T4'][i], int) == True:
            x.append(0)     
        else: 
            x.append(1)   

Output:

X1 X2 X3 X4 bug
1 1.2 1.2 2 0
1 1.3 1.2 1.2 1
1 3.2 4.2 1 0
1.9 1.2 5.4 3 1

Any suggestions?

Thanks!



Solution 1:[1]

df['new_column'] = df.apply(lambda x: 1 if isinstance(x['X1'],int) and isinstance(x['X4'],int) else 'bug', axis=1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nycho