相关疑难解决方法(0)

Pandas和scikit-learn:KeyError:[....]不在索引中

我不明白为什么KeyError: '[ 1351 1352 1353 ... 13500 13501 13502] not in index'在运行此代码时出现错误:

cv = KFold(n_splits=10)

for train_index, test_index in cv.split(X):
    f_train_X, f_valid_X = X[train_index], X[test_index]
    f_train_y, f_valid_y = y[train_index], y[test_index]
Run Code Online (Sandbox Code Playgroud)

我使用X(一个Pandas数据帧)来分割我cv.split(X).

X.shape
y.shape
Out: (13503, 17)
Out: (13503,)
Run Code Online (Sandbox Code Playgroud)

python pandas scikit-learn

8
推荐指数
1
解决办法
2万
查看次数

KeyError:“ [[”,“]]]都不在“ [列]”中

我想在我的数据框中切两列。

这是我这样做的代码:

import pandas as pd
df = pd.read_csv('source.txt',header=0)
cidf=df.loc[:,['vocab','sumCI']]
print(cidf)
Run Code Online (Sandbox Code Playgroud)

这是数据示例:

ID  vocab   sumCI   sumnextCI   new_diff
450      statu    3.0        0.0       3.0
391     provid    4.0        1.0       3.0
382  prescript    3.0        0.0       3.0
300   lymphoma    2.0        0.0       2.0
405      renew    2.0        0.0       2.0
Run Code Online (Sandbox Code Playgroud)

**首先我收到此错误:**

KeyError: “None of [['', '']] are in the [columns]”'
Run Code Online (Sandbox Code Playgroud)

我尝试过的

以上都不是解决问题的方法。

python slice dataframe pandas

8
推荐指数
1
解决办法
1万
查看次数

标签 统计

pandas ×2

python ×2

dataframe ×1

scikit-learn ×1

slice ×1