简体   繁体   中英

Return values from pandas dataframe

I want to return all the even and odd numbers in the turns column for this dataframe This is what the data frame looks like

Turns victory_status   winner
13      out of time     white
10      resign          black
119     mate            white
8       outoftime       white
90      mate            black

I tried using

is_even = dd['turns'].astype(int) % 2 == 0
is_even.loc[is_even.turns == True , 'newcol'] = 0 

But its not working out

You can use boolean masking to achieve this (if your 'Turns' column already contains numeric data, you can leave out the astype(str) )

df[df['Turns'].astype(int)%2==0]

    Turns   victory_status  winner
1   10      resign          black
3   8       outoftime       white
4   90      mate            black

df[~df['Turns'].astype(int)%2==0]

    Turns   victory_status  winner
0   13      outoftime       white
2   119     mate            white

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM