👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
I"d like to replace bad values in a column of a dataframe by NaN"s.
mydata = {"x" : [10, 50, 18, 32, 47, 20], "y" : ["12", "11", "N/A", "13", "15", "N/A"]}
df = pd.DataFrame(mydata)
df[df.y == "N/A"]["y"] = np.nan
Though, the last line fails and throws a warning because it"s working on a copy of df. So, what"s the correct way to handle this? I"ve seen many solutions with iloc or ix but here, I need to use a boolean condition.
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from How to set a cell to NaN in a pandas dataframe, check other ast Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano How to set a cell to NaN in a pandas dataframe
- Deutsch How to set a cell to NaN in a pandas dataframe
- Français How to set a cell to NaN in a pandas dataframe
- Español How to set a cell to NaN in a pandas dataframe
- Türk How to set a cell to NaN in a pandas dataframe
- Русский How to set a cell to NaN in a pandas dataframe
- Português How to set a cell to NaN in a pandas dataframe
- Polski How to set a cell to NaN in a pandas dataframe
- Nederlandse How to set a cell to NaN in a pandas dataframe
- 中文 How to set a cell to NaN in a pandas dataframe
- 한국어 How to set a cell to NaN in a pandas dataframe
- 日本語 How to set a cell to NaN in a pandas dataframe
- हिन्दी How to set a cell to NaN in a pandas dataframe
Moscow | 2023-02-08
I was preparing for my coding interview, thanks for clarifying this - How to set a cell to NaN in a pandas dataframe in Python is not the simplest one. I am just not quite sure it is the best method
Texas | 2023-02-08
Thanks for explaining! I was stuck with How to set a cell to NaN in a pandas dataframe for some hours, finally got it done 🤗. I just hope that will not emerge anymore
London | 2023-02-08
Thanks for explaining! I was stuck with How to set a cell to NaN in a pandas dataframe for some hours, finally got it done 🤗. I am just not quite sure it is the best method