1

I have 2 dataframes (dfA, dfB)

I have another dataframe dfC which has a column called 'SOURCE'. Values of SOURCE will either be dfA or dfB.

I am trying to go row by row on dfC and depending on the SOURCE field, either update dfA or dfB.

To do this I tried to create a variable 'a' which will take the value of SOURCE. The issue is, 'a will equal 'dfA' or 'dfB' (reads it as a string). I cannot use this string to call the correct dataframe (error thrown)

for i in [0, len(dfC.index)-1]:
    a = dfC.loc[i,'Source']
    temp = a[['colA', 'colB', colC']]][(a['movement_id']) == dfC.loc[i,'TEMP']]
babz
  • 469
  • 6
  • 16
  • 1
    can you post your dataframes so we can see the columns and sample data? use df.head() – Kyle Oct 16 '18 at 17:55
  • Rather than passing the DF as a string, why not just pass the variable itself? `'dfA', 'dfB'` to `dfA, dfB` – G. Anderson Oct 16 '18 at 17:59
  • 1
    The goal is not to hardcode as dfC will continuously grow. Is there a way I can convert a string to the variable name? That would solve the problem – babz Oct 16 '18 at 18:00
  • You can use [`locals()`](https://docs.python.org/2/library/functions.html#locals), but keep in mind that it is often a sign of bad design. – 0x5453 Oct 16 '18 at 18:08

2 Answers2

1

Just use if-else in your loop;

for ind in range(dfC.shape[0]):
   if dfC.loc[ind, 'SOURCE'] == 'dfA':
       "do what you want to dfA"
   if dfC.loc[ind, 'SOURCE'] == 'dfB':
           "do what you want to dfB"

Does this sound reasonable?

quest
  • 3,576
  • 2
  • 16
  • 26
1

I suggest to use pandas.DataFrame.update which updates dataframe based on index.

dfa = DataFrame([[2],[4]], index=[1,2], columns=['val'])
dfb = DataFrame([[4],[8]], index=[1,2], columns=['val'])
dfc = DataFrame([['dfa',-1],['dfb',-2],['dfa',-3]], index=[1,1,2], columns='dest','val'])

dfa.update(dfc[dfc['dest']=='dfa'])
dfb.update(dfc[dfc['dest']=='dfb'])

Using for loop in pandas is usually discouraged.

PyJan
  • 88
  • 5