Skip to content
Advertisement

Extracting features from dataframe

I have pandas dataframe like this

    ID  Phone          ex

0   1   5333371000     533
1   2   5354321938     535
2   3   3840812        384
3   4   5451215        545
4   5   2125121278     212

For example if “ex” start to 533,535,545 new variable should be :

Sample output :

   ID    Phone         ex          iswhat

0   1   5333371000     533         personal
1   2   5354321938     535         personal
2   3   3840812        384         notpersonal
3   4   5451215        545         personal
4   5   2125121278     212         notpersonal

How can i do that ?

Advertisement

Answer

You can use np.where:

df['iswhat'] = np.where(df['ex'].isin([533, 535, 545]), 'personal', 'not personal')
print(df)

# Output
   ID       Phone   ex        iswhat
0   1  5333371000  533      personal
1   2  5354321938  535      personal
2   3     3840812  384  not personal
3   4     5451215  545      personal
4   5  2125121278  212  not personal

Update

You can also use your Phone column directly:

df['iswhat'] = np.where(df['Phone'].astype(str).str.match('533|535|545'), 
                        'personal', 'not personal')

Note: If Phone column contains strings you can safely remove .astype(str).

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement