为什么 `.loc()` 函数返回一个空的 Series?

huangapple go评论52阅读模式
英文:

why is the .loc() function returning an empty Series

问题

当我执行basic_data.loc[2:61, 'v_00']时,返回Series([], Name: v_00, dtype: float64)

为什么会这样?

我想知道为什么我得到了一个空的Series。

英文:

为什么 `.loc()` 函数返回一个空的 Series?

when I excute
basic_data.loc[2:61, 'v_00']
it return
Series([], Name: v_00, dtype: float64)

Why?

I wonder why I got empty series.

答案1

得分: 2

2:61 将切片介于行索引 2 和行索引 61 之间的行(按位置),但这两者之间没有行。它不会考虑整数 2 到 61 之间的数值

我认为您想要使用 between 方法:

basic_data.loc[basic_data.index.to_series().between(2, 61), 'v_00']

# 或
basic_data.loc[(basic_data.index >= 2) & (basic_data.index <= 61), 'v_00']

或者首先对索引进行排序:

basic_data = basic_data.sort_index()
basic_data.loc[2:61, 'v_00']
英文:

2:61 will slice the rows (by position) that are between the row index 2 and the row index 61, which are none. It will not consider the values between the integer 2 and 61.

I believe you want between:

basic_data.loc[basic_data.index.to_series().between(2, 61), &#39;v_00&#39;]

# or
basic_data.loc[(basic_data.index &gt;= 2) &amp; (basic_data.index &lt;= 61), &#39;v_00&#39;]

Or first sort your index:

basic_data = basic_data.sort_index()
basic_data.loc[2:61, &#39;v_00&#39;]

huangapple
  • 本文由 发表于 2023年2月8日 11:51:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定