-4

I have 2 two data frames df1 and df2 , in df2 i have 4 columns . i want if df2 column1 value is 0 ,code should add corresponding 3 column values in df1 with column name col2_0 ,col3_0, and col4_0(Note: this process also need to do for value -1,-2,-3,-4,-5), with if else can be done this problem but i am looking for pandas easy and fast way to handle this problem

Here is df2

enter image description here

Nickel
  • 580
  • 4
  • 19
  • Your question is missing details. Please read this thread: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – ulmefors Mar 15 '19 at 06:45

1 Answers1

1

I'll use an initially empty df1 with some extra rows for this example:

df2 = pd.DataFrame({'#timestamp':[-5,-4,-3,-2,-1,0],
                    'grid_U1':   [413.714,413.797,413.926,414.037,414.066,414.064],
                    'grid_U2':   [415.796,415.909,416.117,416.093,416.163,416.183],
                    'grid_U3':   [416.757,416.853,417.09,417.158,417.175,417.085]})

df1 = pd.DataFrame(index=range(0,10), columns=['col2_0','col3_0','col4_0'])

If you want to match row indices (copy from a given row number in df2 to the same row number in df1), then you can use this:

In [403]: df1[['col2_0','col3_0','col4_0']] = df2[df2['#timestamp'].isin(range(-5,1))][['grid_U1','grid_U2','grid_U3']]

In [404]: df1
Out[404]: 
    col2_0   col3_0   col4_0
0  413.714  415.796  416.757
1  413.797  415.909  416.853
2  413.926  416.117  417.090
3  414.037  416.093  417.158
4  414.066  416.163  417.175
5  414.064  416.183  417.085
6      NaN      NaN      NaN
7      NaN      NaN      NaN
8      NaN      NaN      NaN
9      NaN      NaN      NaN

I'll confirm this is matching row numbers by selecting for timestamp values that don't occur at the top:

In [405]: df1[['col2_0','col3_0','col4_0']] = df2[df2['#timestamp'].isin([-3,-1])][['grid_U1','grid_U2','grid_U3']]

In [406]: df1
Out[406]: 
    col2_0   col3_0   col4_0
0      NaN      NaN      NaN
1      NaN      NaN      NaN
2  413.926  416.117  417.090
3      NaN      NaN      NaN
4  414.066  416.163  417.175
5      NaN      NaN      NaN
6      NaN      NaN      NaN
7      NaN      NaN      NaN
8      NaN      NaN      NaN
9      NaN      NaN      NaN

If you want to instead fill in from the top of df1, you can tack a call to reset_index on the end (you need drop=True to avoid adding an extra index column in):

In [412]: df1[['col2_0','col3_0','col4_0']] = df2[df2['#timestamp'].isin([-3,-1])][['grid_U1','grid_U2','grid_U3']].reset_index(drop=True)

In [413]: df1
Out[413]: 
    col2_0   col3_0   col4_0
0  413.926  416.117  417.090
1  414.066  416.163  417.175
2      NaN      NaN      NaN
3      NaN      NaN      NaN
4      NaN      NaN      NaN
5      NaN      NaN      NaN
6      NaN      NaN      NaN
7      NaN      NaN      NaN
8      NaN      NaN      NaN
9      NaN      NaN      NaN