- axolotl farmer
- May 17, 2007
-
Now I'm going to sing the Perry Mason theme
|
I'm using pandas to update a column in a dataframe.
My rules are if there is a value in the New column, that becomes the Current value.
If there is a NaN in the New column, the value in the Old column becomes the Current value
code:In[]: df = pd.DataFrame([[1, 2,np.nan],[3, 2,np.nan],[7, np.nan,np.nan], [np.nan, 8,np.nan]], columns=['Old', 'New', 'Current'])
In[]: df
Out[]:
Old New Current
0 1.0 2.0 NaN
1 3.0 2.0 NaN
2 7.0 NaN NaN
3 NaN 8.0 NaN
I try to put in the values from New and then replace the NaN from Old.
code:In[]df.Current=df.New
In[]df.Current=df.Current.loc[(df.Current.isnull() & (df.Old.notnull()))] = df.Old
In[]df
Out[]:
Old New Current
0 1.0 2.0 1.0
1 3.0 2.0 3.0
2 7.0 NaN 7.0
3 NaN 8.0 NaN
Welp, this just replaces all the values in Current with Old.
Please help, I'm bad and new at this.
|
#
¿
Nov 18, 2016 18:00
|
|
- Adbot
-
ADBOT LOVES YOU
|
|
#
¿
May 29, 2024 08:06
|
|