我不明白为什么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) 我想在我的数据框中切两列。
这是我这样做的代码:
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)
我尝试过的
header用index 0我尝试使用以下代码重命名列:
df.rename(columns=df.iloc[0],inplace=True)
Run Code Online (Sandbox Code Playgroud)我也试过这个:
df.columns = df.iloc[1]
df=df.reindex(df.index.drop(0))
Run Code Online (Sandbox Code Playgroud)在此链接中也尝试过评论
以上都不是解决问题的方法。