-1

I have two series, one having the company stock volume for all the many stocks across many exchanges (a lot of the stocks trade in all the exchanges). The other series is of the standard deviation of each stock (each company, irrespective of the exchanges they are traded in). Now, I have been trying to create a loop, to divide the volume of the respective stock (in first series) with the combined standard deviation that is in the second series. I made the following loop:

#for standard deviation of volume of each stock across all exchanges.(it is working properly)

stdev_volume = Main_df_retvol.groupby(['pair_name'], sort=False)['volume'].std()

#loop to divide the volume by the standard deviation of volume of respective stock.(loop not working)

df_vol_std = []
for i in range(len(stdev_volume)):
    if stdev_volume[i]['pair_name'] == Main_df_retvol['pair_name']:
        df_vol_std = Main_df_retvol['vol'].divide(other = stdev_volume['Volume'])
print(df_vol_std)

Any help would be really appreciated.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Do you know that Pandas, the library you are using but never mentioned by name, is not part of Python itself? I've tagged your question as Pandas, because that is really what you're asking about. It's going to be hard to answer your question without example input data. – John Zwinck Jul 26 '20 at 05:38
  • _A proper question **MUST** provide **ALL** of the information necessary in order for a proper answer to be given._ Please [create a reproducible copy of the DataFrame with `df.head(20).to_clipboard(sep=',')`](https://stackoverflow.com/questions/52413246/how-to-provide-a-copy-of-your-dataframe-with-to-clipboard), **[edit] your question**, and paste the clipboard into a code block. – Trenton McKinney Jul 26 '20 at 05:46

1 Answers1

0

Let's break it down... Getting an index to select each row in stdev

for i in range(len(stdev_volume)):

Comparing a scalar value from a row/col in stdev to a full column from main (which will raise an exception)

if stdev_volume[i]['pair_name'] == Main_df_retvol['pair_name']:

And taking a variable you had initialized as a list and overwriting with a full column/ column division ( regardless of the intended row, and will only keep the value from the last time around the loop anyways)

df_vol_std = Main_df_retvol['vol'].divide(other = stdev_volume['Volume'])

So, instead of that loop i would suggest something like:

main.join(stdev, on='pair_name')

Or go even further to when you build stdev and add it as a column on main:

main = main.assign(stdev=
    main.groupby('pair_name').volume.transform('std'))
main = main.assign(volbystd=
    main.volume.div(main.stdev))

If you provide a sample of your data we can test if this works

RichieV
  • 5,103
  • 2
  • 11
  • 24
  • Thanks a lot Richie. Your logic and code both worked perfectly. – Beginner_python Jul 26 '20 at 22:51
  • Glad you made it work... I would recommend paying attention to the comments up on your question, this is a great site and should make an effort to keep it clean – RichieV Jul 26 '20 at 23:31