如何操作每个索引中的值?

huangapple go评论89阅读模式
英文:

How to manipulate value in each index?

问题

我有一列数字。它有大约60000个索引。一些索引具有值,如1.000或5.0或2323.0000或1.446。这些索引的正确值分别是1、5、2323、1446。换句话说,我有两种情况。对于第一种情况,如果一个索引是带有小数点和小数点后面的零的数字值,我需要移除小数点和小数点后面的所有零。第二种情况是当一个索引具有带有小数点的数字值,但小数点后的数字不是零时。我只需要去掉小数点。我该如何实现这个目标?

英文:

I have a column with numbers. It has about 60000 indexes. Some indexes have values like 1.000 or 5.0 or 2323.0000 or 1.446 The correct value for these indexes are 1, 5, 2323, 1446. In other words, i have two cases. For first case, if a index is numeric value with dot and zeros after that dot, i need to remove the dot and all the zeros after the dot. Second case is when a index has a numeric value with dot, but number after dot is not zero. i Just need to get rid of the dot. How would i accomplish this?

答案1

得分: 2

我认为你需要的是:

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
英文:

I think you need:

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))

Or:

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

Or maybe need:

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

答案2

得分: 0

如果该值已经是整数,请参考 https://stackoverflow.com/questions/21583758/how-to-check-if-a-float-value-is-a-whole-number 进行检查。

如果不是整数,您可以简单地乘以一个整数,直到它变成整数。之后,在这两种情况下都可以强制转换为整数。

英文:

To check if the value is integer already check https://stackoverflow.com/questions/21583758/how-to-check-if-a-float-value-is-a-whole-number

If it is not you can just multiply by then until it is. Afterwards you can cast to int in both cases.

答案3

得分: 0

这可能不是最优雅的解决方案,但它简单而干净,没有导入和其他东西,只是基本的Python。

outcome = []
for i in raw:
    if i%1 == 0: # 检查数字是否为整数
        outcome += int(i) # 以整数形式添加
    else:
        outcome += int(str(i).replace('.', '')) # 替换 .
return outcome

可以改写为

return [(int(i) if i.is_integer() else int(str(i).replace('.', ''))) for i in raw]
英文:

this may not be the most elegant solution, but it's simple and clean, no imports and stuff, just basic python

outcome = []
for i in raw:
    if i%1 == 0: # check if number is whole
        outcome += int(i) # append normally as int
    else:
        outcome += int(str(i).replace('.','')) #replace .
return outcome

which could be

return [(int(i) if i.is_integer() else int(str(i).replace('.',''))) for i in raw]

huangapple
  • 本文由 发表于 2020年1月3日 20:00:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定