我要計算這個從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'
我要計算這個從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'
看來你需要loc
與boolean 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
小號orry是否在'df.loc [df ['gender'] =='F','received']'之後? – EdChum
這被稱爲**邏輯索引** – smci