移除浮点数和NaN值。

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

How do I remove float and NaN

问题

I can provide the translated code part as requested:

我试图将一个具有超过20行的列表转换为数据框但我遇到了问题当我将我的列表转换时数据框中的一些数字会返回浮点数和NaN有没有办法解决这个问题
例如下面是原始列表

lst = [[2, 25, 26], [5], [10, 19, 19], [20, 18, 18]]

我尝试了以下代码 df1 = pd.DataFrame(lst)但它不起作用
原始数据框

   0     1     2
0  2  25.0  26.0
1  5   NaN   NaN
2  10  19.0  19.0
3  20  18.0  18.0

我想要删除NaN小数和零但仍然保留值和相同的索引

处理后的数据框

   0   1   2
0  2  25  26
1  5
2  10  19  19
3  20  18  18

Please note that this is a translation of the code and relevant information, and I have excluded the non-code parts as requested.

英文:

I'm trying to convert a list with over 20 rows and convert into a data frame but I'm running into an issue. When I convert my list, it returns a float and nan in some of the numbers in my data frame. Is there a way to fix this issue?
For example, below

 lst = [[2,25,26],[5],[10,19,19],[20,18,18]]


I tried this code df1 = pd.DataFrame(lst) but it doesn't work. 
 
   0     1     2
0  2  25.0  26.0
1  5   NaN   NaN
2  10  19.0  19.0
3  20  18.0  18.0

I would like to remove Nan, decimal and zero but still keep the values and the same index.

       0    1    2
   0   2   25   26
   1   5          
   2  10   19   19
   3  20   18   18

答案1

得分: 1

NaN 是数据框中的缺失值。它表示没有值 - 另一种思考方式是Null。但是,即使存在NaN,你仍然可以将数据类型更改为int。你可以使用以下代码实现:

df1 = df1.astype("Int64")

这将得到以下结果:

    0   1   2
0   2  25  26
1   5 NaN NaN
2  10  19  19
3  20  18  18
英文:

NaN is a missing value in a data frame. It's the representation that nothing is there - another way of thinking about it would be Null. You can however, change the data type to int even though there is NaN present. You can do this with the following:

df1 = df1.astype("Int64")

Which gives the following:

    0     1     2
0   2    25    26
1   5  <NA>  <NA>
2  10    19    19
3  20    18    18

答案2

得分: 0

NaN值是缺失值,因此表示该位置没有任何内容。

另一方面,值5仍然在相同的位置[1][0]

英文:

NaN values are missing values, so indicates that there is nothing in that position

On the other hand, the value 5 still has the same position [1][0]

huangapple
  • 本文由 发表于 2023年3月12日 09:42:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710634.html
匿名

发表评论

匿名网友

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

确定