英文:
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]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论