[英]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)
)您可以使用布尔掩码来实现这一点(如果您的 'Turns' 列已经包含数字数据,您可以
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.