Python pandas join on with overwrite -
i realize question similar join or merge overwrite in pandas, accepted answer not work me since want use on='keys'
df.join()
.
i have dataframe df
looks this:
keys values 0 0 0.088344 1 0 0.088344 2 0 0.088344 3 0 0.088344 4 0 0.088344 5 1 0.560857 6 1 0.560857 7 1 0.560857 8 2 0.978736 9 2 0.978736 10 2 0.978736 11 2 0.978736 12 2 0.978736 13 2 0.978736 14 2 0.978736
then have series s
(which result df.groupy.apply()
) same keys:
keys 0 0.183328 1 0.239322 2 0.574962 name: new_values, dtype: float64
basically want replace 'values' in df
values in series, keys
every keys
block gets same new value. currently, follows:
df = df.join(s, on='keys') df['values'] = df['new_values'] df = df.drop('new_values', axis=1)
the obtained (and desired) result then:
keys values 0 0 0.183328 1 0 0.183328 2 0 0.183328 3 0 0.183328 4 0 0.183328 5 1 0.239322 6 1 0.239322 7 1 0.239322 8 2 0.574962 9 2 0.574962 10 2 0.574962 11 2 0.574962 12 2 0.574962 13 2 0.574962 14 2 0.574962
that is, add new column , using on='keys'
gets corrects shape. assign values
new_values
, remove new_values
column. of course works perfectly, problem being find extremely ugly.
is there better way this?
you try like:
df = df[df.columns[df.columns!='values']].join(s, on='keys')
make sure s named 'values' instead of 'new_values'.
to knowledge, pandas doesn't have ability join "force overwrite" or "overwrite warning".
Comments
Post a Comment