我的 Pandas 函数返回“无”,而不是我尝试使用我编写的函数过滤的数据帧。为什么会这样?我该如何解决这个问题?谢谢!
import pandas as pd
nz_data = pd.read_csv('research-and-development-survey-2016-2019-csv.csv', index_col = 2)
def count_of_mining_biz():
if "B_Mining" in nz_data[["Breakdown_category"]] and "Count of businesses" in nz_data[["Units"]]:
return nz_data.loc["2019", "RD_Value"]
print(count_of_mining_biz())
Here is how the data looks like.
我正在尝试找出 2019 年采矿业的 RD 价值。我之所以必须为“单位”列设置条件,是因为还有另一种类型的数据不是所提到的业务的计数。
答案 0 :(得分:2)
.loc[..., ...]
表示 .loc[row_index, col_index]
,但没有名为 2019
的行索引。
在这种情况下尝试将 .loc
与布尔掩码一起使用:
def count_of_mining_biz():
category = nz_data['Breakdown_category'] == 'B_Mining'
units = nz_data['Units'] == 'Count of businesses'
year = nz_data['Year'] == 2019
return nz_data.loc[category & units & year].RD_Value