Python Forum

Full Version: adding a calculated column
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have noticed that when I have added a column to my data frame made up of 2 existing column divided by each other the NaN values in the data frame go, is this anotherway of getting rid of NaN values, or are they still there in the data frame
From your description I wrote this:
import pandas as pd
from numpy import nan


data = pd.DataFrame({"X": [1, 2, 3, nan], "Y": [2, 0, 3, 4]})
print(data)
data["Z"] = data["X"] / data["Y"]
print(data)
Output:
X Y 0 1.0 2 1 2.0 0 2 3.0 3 3 NaN 4 X Y Z 0 1.0 2 0.5 1 2.0 0 inf 2 3.0 3 1.0 3 NaN 4 NaN
I have a dataframe with two columns, one of which contains a NaN. I divide one of the columns by the ther to get a third column. The Nan is still there, and now there is another one.

Please provide an examle that demonstrates what you are talking about.