gpt4 book ai didi

python - 如何避免 python 在屏蔽数据后使用 UserWarning 进行隐式修复

转载 作者:行者123 更新时间:2023-12-01 08:32:25 28 4
gpt4 key购买 nike

当我用另一组数据屏蔽我的数据集时,它会显示用户警告: bool 系列键将被重新索引以匹配 DataFrame 索引。我该如何避免这种情况? Python 会自动重新索引它,但该列的标题是空白的,我似乎无法重命名它,所以我可以在我的代码中引用该列。我也不想依赖这种隐式修正。

我尝试通过 pd.DataFrame.columns() 或 pd.DataFrame.rename() 两种方式手动重命名列。由于某种原因,我要么收到一个错误,指出它需要 3 个元素而不是 4 个,要么添加的空列索引不会被重命名。

# select data and filter it which results in the error which fixes the dataframe but leaves the column name empty

stickData = data[['Time','Pitch Stick Position(IN)','Roll Stick Position (IN)']]
filteredData = stickData[contactData['CONTACT'] == 1]

# moving forward from the error I tried using rename which does not error but also does nothing
filteredData.rename(index={0:'Index'})

# I also tried this
filteredData.rename(index={'':'Old_Index'})

# I even went and tried to add the names of the dataframe like so which resulted in ValueError: Length mismatch: Expected axis has 3 elements, new values have 4 elements
filteredData.columns = ['Old_Index','Time','Pitch Stick Position(IN)','Roll Stick Position (IN)']

经过 python 隐式修正后,filteredData.head() 的当前数据帧如下所示:

Index              Time          Pitch Stick Position(IN)  Roll Stick Position (IN)
0 1421 240:19:06:40.200 0.007263 -0.028500
1 1422 240:19:06:40.400 0.022327 0.139893
2 1423 240:19:06:40.600 -0.016409 0.540756
3 1424 240:19:06:40.800 -0.199329 0.279971
4 1425 240:19:06:41.000 0.013719 -0.018069

但我想显示带有 Old_index 标签的内容,并且更多地不依赖隐式更正:

Index   Old_index   Time          Pitch Stick Position(IN)  Roll Stick Position (IN)
1 1421 240:19:06:40.200 0.007263 -0.028500
2 1422 240:19:06:40.400 0.022327 0.139893
3 1423 240:19:06:40.600 -0.016409 0.540756
4 1424 240:19:06:40.800 -0.199329 0.279971
5 1425 240:19:06:41.000 0.013719 -0.018069

最佳答案

您的代码中有一些错误:

  1. 不要使用chained indexing 。使用loc/iloc相反,访问器。
  2. Assign back to variables当使用无法到位的方法时。
  3. 一般情况下,不要使用从其他数据帧派生的 bool 索引器。如果可以保证行对齐,则通过 pd.Series.values 提取 NumPy 数组表示形式.

例如,假设 contactData 中的行与 filteredData 中的行对齐,这是可行的

cols = ['Time','Pitch Stick Position(IN)','Roll Stick Position (IN)']

filteredData = data.loc[(contactData['CONTACT'] == 1).values, cols]\
.rename(index={0:'Index'})

请注意,我们可以链接方法,例如locrename,而不是每次都显式分配回filteredData .

关于python - 如何避免 python 在屏蔽数据后使用 UserWarning 进行隐式修复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53856748/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com