1

I would like to check if the string is in the dataframe and amend that particular row.

I know how to find if the string exists in the dataframe, e.g. like this:

if usd_margin_data['acct'].str.contains(row['acct']).any():

But now i want to amend the row that contains the string. How do I do that?

TypeError: first argument must be string or compiled pattern
13
  • 2
    Can you add some sample data? Commented Feb 18, 2019 at 6:20
  • 2
    And what is expected output? Commented Feb 18, 2019 at 6:22
  • I should have unique rows in the column called 'acct' Commented Feb 18, 2019 at 6:23
  • 1
    removed the images and added it as text Commented Feb 18, 2019 at 7:49
  • 1
    @jezrael any solution on how to solve the problem? Commented Feb 18, 2019 at 8:25

1 Answer 1

3

Use:

df11 = (df1.pivot_table(index='acct',
                    columns='exchange',
                    values='spanReq',
                    aggfunc='sum')
      )
print (df11)
exchange        CME         HKEX          OSE          SGX
acct                                                      
10        1728005.0         0.00          NaN          NaN
20          83671.0          NaN  106516141.0  14872000.00
30           6237.0          NaN          NaN          NaN
40         857120.0  36071131.40          NaN   2987340.93
60        2803385.0   3459377.95          NaN   2030260.39
70        9007666.0     81300.00    1800000.0  57172391.00
80              NaN  23698214.00          NaN          NaN
90         119644.0          NaN          NaN          NaN

df2 = df2.replace('-', np.nan).replace(',', '', regex=True).astype(float).set_index('Account')
print (df2)
               SGX       HKEX        OSE        CME      Total
Account                                                       
10.0           NaN        NaN        NaN  1549946.0  1549946.0
20.0      144262.0        NaN  1173680.0    83557.0  1401500.0
30.0           NaN        NaN        NaN     6237.0     6237.0
40.0     3834382.0  4455808.0        NaN   631892.0  8922082.0
60.0     2329690.0   199192.0        NaN  3754770.0  6283652.0
70.0      514978.0    10360.0    16213.0  4777373.0  5318923.0
80.0           NaN  1741612.0        NaN        NaN  1741612.0
90.0           NaN        NaN        NaN   119644.0   119644.0

df3 = df11.combine_first(df2)
print (df3)
            CME         HKEX          OSE          SGX      Total
acct                                                             
10    1728005.0         0.00          NaN          NaN  1549946.0
20      83671.0          NaN  106516141.0  14872000.00  1401500.0
30       6237.0          NaN          NaN          NaN     6237.0
40     857120.0  36071131.40          NaN   2987340.93  8922082.0
60    2803385.0   3459377.95          NaN   2030260.39  6283652.0
70    9007666.0     81300.00    1800000.0  57172391.00  5318923.0
80          NaN  23698214.00          NaN          NaN  1741612.0
90     119644.0          NaN          NaN          NaN   119644.0
Sign up to request clarification or add additional context in comments.

1 Comment

pivot_table is was the key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.