Ann*_*nna 2 python python-3.x pandas
I have a DataFrame that looks like this:
| Age | Married | OwnsHouse |
| 23 | True | False |
| 35 | True | True |
| 14 | False | False |
| 27 | True | True |
Run Code Online (Sandbox Code Playgroud)
I want to find the highest age of anyone who is married and owns a house. The answer here would be 35. My first thought was to do:
df_subset = df[df['Married'] == True and df['OwnsHouse'] == True]
max_age = df_subset.max()
Run Code Online (Sandbox Code Playgroud)
However, dataset is big (50MB) and I fear this will be computationally expensive as it goes through the dataset twice.
My second thought was to do:
max_age = 0
for index, row in df.iterrows():
if(row[index]['Married] and row['index']['OwnsHouse'] and row[index]['Age] > max_age):
max_age = row[index]['Age']
Run Code Online (Sandbox Code Playgroud)
Is there a faster way of doing this?
您的第一种方法是可靠的,但这是一个简单的选择:
df[df['Married'] & df['OwnsHouse']].max()
Age 35.0
Married 1.0
OwnsHouse 1.0
dtype: float64
Run Code Online (Sandbox Code Playgroud)
或者,只是年龄:
df.loc[df['Married'] & df['OwnsHouse'], 'Age'].max()
# 35
Run Code Online (Sandbox Code Playgroud)
如果您有多个布尔列,我建议您进行一些扩展,
df[df[['Married', 'OwnsHouse']].all(axis=1)].max()
Age 35.0
Married 1.0
OwnsHouse 1.0
dtype: float64
Run Code Online (Sandbox Code Playgroud)
哪里,
df[['Married', 'OwnsHouse']].all(axis=1)
0 False
1 True
2 False
3 True
dtype: bool
Run Code Online (Sandbox Code Playgroud)
一样,
df['Married'] & df['OwnsHouse']
0 False
1 True
2 False
3 True
dtype: bool
Run Code Online (Sandbox Code Playgroud)
但是,.all与其手动查找N个布尔掩码的与,不如为您做。
query 是另一种选择:
df.query("Married and OwnsHouse")['Age'].max()
# 35
Run Code Online (Sandbox Code Playgroud)
它不需要计算遮罩的中间步骤。
您的方法足够快,但是如果要进行微优化,可以使用numpy进行以下操作:
# <= 0.23
df[(df['Married'].values & df['OwnsHouse'].values)].max()
df[df[['Married', 'OwnsHouse']].values.all(axis=1)].max()
# 0.24+
df[(df['Married'].to_numpy() & df['OwnsHouse'].to_numpy())].max()
df[df[['Married', 'OwnsHouse']].to_numpy().all(axis=1)].max()
Age 35.0
Married 1.0
OwnsHouse 1.0
dtype: float64
Run Code Online (Sandbox Code Playgroud)
虽然您可能只想年龄。做这个
df.loc[(df['Married'].to_numpy() & df['OwnsHouse'].to_numpy()), 'Age'].max()
# 35
Run Code Online (Sandbox Code Playgroud)
如果您想要更多的numpy,请执行以下操作:
df.loc[(
df['Married'].to_numpy() & df['OwnsHouse'].to_numpy()), 'Age'
].to_numpy().max()
# 35
Run Code Online (Sandbox Code Playgroud)
还是更好,丢掉熊猫,
df['Age'].to_numpy()[df['Married'].to_numpy() & df['OwnsHouse'].to_numpy()].max()
# 35
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57 次 |
| 最近记录: |