在数据框中创建新列的问题

发布于 2025-01-13 19:30:02 字数 523 浏览 2 评论 0原文

我帮助将以下各列添加到数据框中,非常感谢任何指导。我创建了变量: no_outlier_pe_go ['industry_marketCap']= _outlier_pe_g.groupby('industry')['marketCap'].transform(' sum') 但一旦我尝试创建后续列(下面的说明和屏幕截图),就会不断出现错误。

警告

使用此变量创建行业 PE 作为行业股票的市值加权 PE,并将其命名为 'ind_pe'。 (提示:您需要一列反映行业中每只股票的市值权重,'wt_in_industry',另一列用于反映跟踪PE与其行业权重的乘积,并将其命名为pe_ind_wt。使用转换方法在后一个变量上创建 ind_pe 作为 pe_ind_wt 的总和)。

I help with adding the below columns to a data frame, any guidance is much appreciated.I created the variable: no_outlier_pe_go ['industry_marketCap']= _outlier_pe_g.groupby('industry')['marketCap'].transform('sum') but keep getting errors once I try to create the subsequent columns, (instructions and screenshot below).

Warning

Use this variable to create an industry PE as a marketCap weighted PE of stocks in the industry and name it 'ind_pe'. (Hint: you need a column that reflect the marketCap weight of each stock in the industry, 'wt_in_industry', another column for the product of trailingPE by its industry weight and name it pe_ind_wt. Use the transform method on the latter variable to create ind_pe as the sums to pe_ind_wt ).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无力看清 2025-01-20 19:30:03

我怀疑您将 no_outlier_pe_g 创建为另一个 DataFrame 的副本。您可以通过 df._is_copy 进行检查,如下所示:

import pandas as pd


df = pd.DataFrame(
    [
        ["SWN US EQUITY", "Oil & Gas E & P", 1.610607e09],
        ["AMCR US EQUITY", "Packaging & Containers", 1.762973e10],
    ],
    columns=["symbol", "industry", "marketCap"],
)

# create a copy
no_outlier_pe_g = df.iloc[0:, :]

# ...
df2 = no_outlier_pe_g

no_outlier_pe_g["industry_marketCap"] = no_outlier_pe_g.groupby("industry")[
    "marketCap"
].transform("sum")

# check if copy
print(df2._is_copy)

从技术上讲,这只是一个警告而不是错误。 SettingWithCopyWarning 警告您,您正在处理 DataFrame 的副本,这会使原始 DataFrame 保持不变,并可能导致意外结果,例如链接的结果运营。它已在

在您的用例中,如果您意识到可能存在的不一致,那就没问题。

I suspect you created no_outlier_pe_g as a copy of another DataFrame. You can check so by df._is_copy, as shown here:

import pandas as pd


df = pd.DataFrame(
    [
        ["SWN US EQUITY", "Oil & Gas E & P", 1.610607e09],
        ["AMCR US EQUITY", "Packaging & Containers", 1.762973e10],
    ],
    columns=["symbol", "industry", "marketCap"],
)

# create a copy
no_outlier_pe_g = df.iloc[0:, :]

# ...
df2 = no_outlier_pe_g

no_outlier_pe_g["industry_marketCap"] = no_outlier_pe_g.groupby("industry")[
    "marketCap"
].transform("sum")

# check if copy
print(df2._is_copy)

Technically, it's just a warning not an error. The SettingWithCopyWarning warns you that you work on a copy of the DataFrame, which leaves the original DataFrame unaltered and might lead to unexpected results e.g. with chained operations. It has been discussed extensively in the docs.

In your use case it's fine, if you are aware of the possible inconsistencies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文