基于条件的数据框架值更改,即使打印它确实不起作用

发布于 2025-02-13 17:40:33 字数 389 浏览 0 评论 0原文

我有一个带日期,时间和流量的数据框。我可以打印它的选择,并且可以正确捕获相应的数据,但是当我尝试更改值时,它无能为力。

下面的打印语句正确选择了相应的数据。

df["Flow"][ df["Date"] == "2022-02-23" ] 

下面的重新分配无济于事。

df["Flow"][ df["Date"] == "2022-02-23" ] = 100 

但是,如果我如下所示,将选择取出,则可以正确更改该值。

df["Flow"] = 100

我想添加更多条件,但是在其他条件的所有组合中,印刷品总是有效的,并且重新分配却没有。我缺少基本的东西吗?

I have a DataFrame with Date, Time, Flow. I can print selections of it and it grabs the corresponding data correctly, but when I try to change the values, it does nothing.

The print statement below selects the corresponding data correctly.

df["Flow"][ df["Date"] == "2022-02-23" ] 

The reassignment below does nothing.

df["Flow"][ df["Date"] == "2022-02-23" ] = 100 

But if I take the selection out as seen below, it correctly changes the value.

df["Flow"] = 100

I'd like to add more conditions, but in all combinations of the other conditions, the print always works and the reassignment doesn't. Is there something basic I'm missing?

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

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

发布评论

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

评论(2

儭儭莪哋寶赑 2025-02-20 17:40:33

尝试这样的东西,看看它是否有效:

df.loc[df["Date"] == "2022-02-23", "Flow"] = 100

Try something like this and see if it works:

df.loc[df["Date"] == "2022-02-23", "Flow"] = 100
下雨或天晴 2025-02-20 17:40:33

您可以使用列表理解:
df [“ flow”] = [100如果date ==“ 2022-02-23” else df [“ flow”] [i],i,enumerate(df [date'date'date'])]

You could use a list comprehension:
df["Flow"] = [100 if date == "2022-02-23" else df["Flow"][i] for i, date in enumerate(df["Date"])]

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