Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

python - Dataframe split columns value, how to solve error message?

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).

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.

question from:https://stackoverflow.com/questions/65833286/dataframe-split-columns-value-how-to-solve-error-message

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is not an error, but a warning as you may have probably noticed your script finishes its execution.

edite: Given your comments it seems your issues generate previously in the code, therefore I suggest you use the following:

new_df = new_df.copy(deep=False)

And then proceed to solve it with:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...