如何使用另一列的内容创建新的dataframe列,但前提是另一列满足多个条件?

2024-09-27 00:22:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下几行Python代码:

df['small_homes'] = 0 #creates new column set to zero
df.loc[(df['bathrooms'] == 1) or # if one bathroom 
       (df['bathrooms'] < 2) & # or if less than two bedrooms
       (df['bedrooms'] == 1), # and if one bedroom
       'small_homes'] = 1 # sets small_homes = 1 if true
df.head()

我想在一个名为“Small_homes”的数据框中创建一个新列,该列只包含一个浴室(bathrooms列)或少于两个浴室和一个卧室(Bethrooms列)的列表。我尝试过使用OR,但它返回的值不明确

我怎么修理

谢谢大家!


Tags: orto代码dfnewifcolumnone

热门问题