Reputation: 1536
I'm trying to perform calculations based on the entries in a pandas
dataframe. The dataframe looks something like this:
and it contains 1466 rows. I'll have to run similar calculations on other
df
s with more rows later.
What I'm trying to do, is calculate something like mag
='(U-V)/('R-I)'
(but ignoring any values that are -999), put that in a new column, and then z_pred
=10**((mag-c)m)
in a new column (mag
, c
and m
are just hard-coded variables). I have other columns I need to add too, but I figure that'll just be an extension of the same method.
I started out by trying
for i in range(1):
current = qso[:]
mag = (U-V)/(R-I)
name = current['NED']
z_pred = 10**((mag - c)/m)
z_meas = current['z']
but I got either a Series for z
, which I couldn't operate on, or various type errors when I tried to print the values or write them to a file.
I found this question which gave me a start, but I can't see how to apply it to multiple calculations, as in my situation.
How can I achieve this?
Upvotes: 0
Views: 166
Reputation: 1272
Conditionally adding calculated columns row wise are usually performed with numpy
's np.where
;
df['mag'] = np.where(~df[['U', 'V', 'R', 'I']].eq(-999).any(1), (df.U - df.V) / (df.R - df.I), -999)
Note; assuming here that when any of the columns contain '-999' it will not be calculated and a '-999' is returned.
Upvotes: 1