将X_mean作为pandas数据帧如何优化以下代码? 我想"缩短"数据帧的索引......
a=[]
for i in range(0,len(X_mean.index)):
a.append(X_mean.index[i][0:-2])
X_mean.index = a
答案 0 :(得分:1)
您可以使用indexing with str:
df = pd.DataFrame({'a':[1,2,3]}, index=['aatfd','sgefds','dsd'])
print (df)
a
aatfd 1
sgefds 2
dsd 3
df.index = df.index.str[:-2]
print (df)
a
aat 1
sgef 2
d 3