0

I have a panda dataframe with the following columns:

             Stock     ROC5   ROC20  ROC63    ROCmean
        0    IBGL.SW  -0.59   3.55   6.57     3.18
        0    EHYA.SW   0.98   4.00   6.98     3.99
        0    HIGH.SW   0.94   4.22   7.18     4.11
        0    IHYG.SW   0.56   2.46   6.16     3.06
        0    HYGU.SW   1.12   4.56   7.82     4.50
        0    IBCI.SW   0.64   3.57   6.04     3.42
        0    IAEX.SW   8.34  18.49  14.95    13.93
        0    AGED.SW   9.45  24.74  28.13    20.77
        0    ISAG.SW   7.97  21.61  34.34    21.31
        0    IAPD.SW   0.51   6.62  19.54     8.89
        0    IASP.SW   1.08   2.54  12.18     5.27
        0    RBOT.SW  10.35  30.53  39.15    26.68
        0    RBOD.SW  11.33  30.50  39.69    27.17
        0    BRIC.SW   7.24  11.08  75.60    31.31
        0    CNYB.SW   1.14   4.78   8.36     4.76
        0     FXC.SW   5.68  13.84  19.29    12.94
        0   DJSXE.SW   3.11   9.24   6.44     6.26
        0  CSSX5E.SW  -0.53   5.29  11.85     5.54

How can I write in the dataframe a new columns "Symbol" with the stock without ".SW". Example first row result should be IBGL (modified value IBGL.SW). Example last row result should be CSSX5E (splited value SSX5E.SW).

Is there a way to sort the values in the columns ROCmean ascending (sort last columns ascending)?

If I send the following command:

new_df['Symbol'] = new_df.loc[:, ('Stock')].str.split('.').str[0]

Than I receive an error message: :3: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

 new_df['Symbol'] = new_df.loc[:, ('Stock')].str.split('.').str[0]

How can I solve this problem?

Thanks a lot for your support.

Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
Yvar
  • 155
  • 4
  • 14

1 Answers1

1

Use Series.str.split:

In [1908]: df['Symbol'] = df['Stock'].str.split('.').str[0]

In [1909]: df
Out[1909]: 
       Stock   ROC5  ROC20  ROC63  ROCmean  Symbol
0    IBGL.SW  -0.59   3.55   6.57     3.18    IBGL
0    EHYA.SW   0.98   4.00   6.98     3.99    EHYA
0    HIGH.SW   0.94   4.22   7.18     4.11    HIGH
0    IHYG.SW   0.56   2.46   6.16     3.06    IHYG
0    HYGU.SW   1.12   4.56   7.82     4.50    HYGU
0    IBCI.SW   0.64   3.57   6.04     3.42    IBCI
0    IAEX.SW   8.34  18.49  14.95    13.93    IAEX
0    AGED.SW   9.45  24.74  28.13    20.77    AGED
0    ISAG.SW   7.97  21.61  34.34    21.31    ISAG
0    IAPD.SW   0.51   6.62  19.54     8.89    IAPD
0    IASP.SW   1.08   2.54  12.18     5.27    IASP
0    RBOT.SW  10.35  30.53  39.15    26.68    RBOT
0    RBOD.SW  11.33  30.50  39.69    27.17    RBOD
0    BRIC.SW   7.24  11.08  75.60    31.31    BRIC
0    CNYB.SW   1.14   4.78   8.36     4.76    CNYB
0     FXC.SW   5.68  13.84  19.29    12.94     FXC
0   DJSXE.SW   3.11   9.24   6.44     6.26   DJSXE
0  CSSX5E.SW  -0.53   5.29  11.85     5.54  CSSX5E
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
  • :1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead I get the following warning message: See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy new_df['Symbol'] = new_df['Stock'].str.split('.').str[0] – Yvar Jan 21 '21 at 17:14
  • @Yvar This is just a `caveat` or a `warning`. Code runs fine. – Mayank Porwal Jan 22 '21 at 03:53