Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
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.

Adbot
ADBOT LOVES YOU

axolotl farmer
May 17, 2007

Now I'm going to sing the Perry Mason theme




Thanks a lot for your help :tipshat:

  • Locked thread