How do I remove float and NaN
Question:
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
Answers:
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]
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
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
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]
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