0

so I have a column('col_name') in my dataframe(df), and it contains several 'NaN's. I'm trying to put all of the values in the column into a list. But then I realized that the NaN value is also included in the list as nan. Please see the code below:

sizes = list(df['col_name'])

The result is like this:

[235, 235, 250, 255, nan, nan, nan, nan]

How do I get rid of all the nans?

Ryan Oh
  • 597
  • 5
  • 21
  • Use `sizes = list(df['col_name'].dropna())` – jezrael Jan 15 '21 at 06:16
  • @jezrael Well, I get ```'method' object is not iterable``` error. What do you think is the problem? – Ryan Oh Jan 15 '21 at 06:17
  • hmmm, maybe `nan` are strings? How working `sizes = list(df.loc[df['col_name'].ne('nan'), 'col_name'])` – jezrael Jan 15 '21 at 06:19
  • @jezrael Welll, I don't get any errors now, but I still get ```nan```s in the list. Maybe the ```list()``` could be the problem? – Ryan Oh Jan 15 '21 at 06:24
  • No, `list` is OK. What is `print ((df.loc[4, 'col)name']))`, `print (type(df.loc[4, 'col)name']))` for test `nan` type ? It is `float?` string? – jezrael Jan 15 '21 at 06:26
  • @jezrael It gives ``````. Hmm – Ryan Oh Jan 15 '21 at 06:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/227368/discussion-between-jezrael-and-ryan-oh). – jezrael Jan 15 '21 at 06:30

0 Answers0