我有一列数字。它具有约60000个索引。一些索引的值如1.000或5.0或2323.0000或1.446。这些索引的正确值为1、5、2323、1446。换句话说,我有两种情况。对于第一种情况,如果索引是带点的数字值,并且该点后有零,那么我需要删除该点以及该点后的所有零。第二种情况是索引具有带点的数值,但点后的数字不为零。我只需要摆脱点。我该怎么做?

最佳答案

我认为您需要:

df = pd.DataFrame({'a':range(5)}, index=[1.0,5.0,2323.0,1.446,10.5])
print (df)
          a
1.000     0
5.000     1
2323.000  2
1.446     3
10.500    4




df.index = df.index.map(lambda x: int(x) if x.is_integer() else int(x * 1000))


要么:

df.index = np.where(df.index.astype(int) == df.index,df.index, df.index * 1000).astype(int)




print (df)
       a
1      0
5      1
2323   2
1446   3
10500  4


或可能需要:

df.index = df.index.astype(str).str.replace('\.0','').str.replace('.','').astype(int)
print (df)
      a
1     0
5     1
2323  2
1446  3
105   4

关于python - 如何操纵每个索引中的值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59578275/

10-12 16:50
查看更多