如何从熊猫索引器获得一致的返回类型?

时间:2018-01-08 20:45:44

标签: python pandas

df = pd.DataFrame(
    {"a":[11, 11, 22],
    },
    index = [1,1,2]
)

print type(df.loc[1,"a"])
print type(df.loc[2,"a"])

获得:

<class 'pandas.core.series.Series'>
<type 'numpy.int64'>

...意味着调用者必须(总是,总是!)检查结果是否是集合的值。这闻起来像是一个可怕的设计错误。

是否有一个总是回放集合的索引器,以便我可以编写安全的程序?

1 个答案:

答案 0 :(得分:2)

传递一个集合:

print(type(df.loc[[2],"a"]))
<class 'pandas.core.series.Series'>