2017-04-21 77 views
0

我要計算這個從r相當於大熊貓:根據邏輯條件,R的子類等價物是什麼?

df$received[df$gender == 'F'] 

在熊貓,如果我使用:

df['received'][df['gender'] == 'F'] 

這是行不通的。

或者,如果我嘗試:

df['received'] & df['gender'] == 'F' 

它給了我:unsupported operand type(s) for &: 'float' and 'bool'

+1

小號orry是否在'df.loc [df ['gender'] =='F','received']'之後? – EdChum

+1

這被稱爲**邏輯索引** – smci

回答

1

看來你需要locboolean indexing

df.loc[df['gender'] == 'F', 'received'] 

樣品:

df = pd.DataFrame({'gender':['F','M','F'], 'received':[10,20,50]}) 
print (df) 
    gender received 
0  F  10 
1  M  20 
2  F  50 

print (df.loc[df['gender'] == 'F', 'received']) 
0 10 
2 50 
Name: received, dtype: int64 
+0

Hmm.strange ...我有3000個記錄'F'和'M'.1500他們是'F'。上述解決方案給我3000記錄而不是1500。但在你的例子中它有效。 – George

+0

不,whitespace.Just'df.loc [df ['gender'] =='F','received']' – George

+0

這似乎是數據的問題。 – jezrael

相關問題