Insert values ​​from a DataFrame into another data frame

Ok, my real problem is bigger than this, but I have a simple working example.

>>> import pandas as pd
>>> import numpy as np
>>> a = pd.DataFrame(np.array([[2, 1990], [4,1991], [5,1992]]), \
...                  index=[1,2,3], columns=['var', 'yr'])
>>> a
   var    yr
1    2  1990
2    4  1991
3    5  1992
>>> b = pd.DataFrame(index=a.index, columns=['new_var'])
>>> b
  new_var
1     NaN
2     NaN
3     NaN
>>> b[a.yr<1992].loc[:, 'new_var'] = a[a.yr<1992].loc[:, 'var']
>>> b
  new_var
1     NaN
2     NaN
3     NaN

I need the following output:

>>> b
  new_var
1       2
2       4
3     NaN
+4
source share
3 answers

With this filter material, you create a copy of the slice and therefore do not assign.

Do this instead:

b.loc[a.yr<1992, 'new_year'] = a['var']

+3
source

you can also use assign+ queryto add intuitiveness

b.assign(new_var=a.query('yr < 1992')['var'])

   new_var
1      2.0
2      4.0
3      NaN

This returns dataframewhich you want. You will need to return it bif you want it to be saved.

+1
source

Another "creative" solution:

In [181]: b['new_var'] = np.where(a.yr < 1992, a['var'], b['new_var'])

In [182]: b
Out[182]:
  new_var
1       2
2       4
3     NaN
0
source

Source: https://habr.com/ru/post/1667910/


All Articles