根据条件找到列值

发布于 2025-02-10 04:09:09 字数 410 浏览 2 评论 0原文

我需要找到独特的名称,谁的年龄= 2,而使用python pandas = 9?

名称年龄频道CC
A293
B282
C391
A296

I need to find unique name, whose age=2 and and cond=9 using python pandas?

nameagecondcc
a293
b282
c391
a296

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

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

发布评论

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

评论(3

骄傲 2025-02-17 04:09:10

pandas query> query 过滤数据框。然后在结果上使用unique()返回唯一名称。

rows = df.query('age == 2 and cond == 9')
print(rows["name"].unique())

有关更多查询示例,请参见在这里

The Pandas query function allows for SQL-like queries to filter a data frame. Then use unique() on the results to return the unique name.

rows = df.query('age == 2 and cond == 9')
print(rows["name"].unique())

For more query examples, see here.

相对绾红妆 2025-02-17 04:09:10

这将找到所有不同的行,其中年龄= 2,cond = 9

df.loc[(df['age'] == 2) & (df['cond'] == 9)][['name', 'cc']].drop_duplicates()

This will find all distinct rows where age = 2 and cond = 9

df.loc[(df['age'] == 2) & (df['cond'] == 9)][['name', 'cc']].drop_duplicates()
兮颜 2025-02-17 04:09:10

一种潜在的解决方案是将列放入zip()中,然后通过您的数据框架进行迭代:

for name, age, cond in zip(df['name'], df['Age'], df['cond']):
    if(age == 2 and cond ==9):
      print(name)

One potential solution is to put the columns in a zip() and then iterate through your dataframe like so:

for name, age, cond in zip(df['name'], df['Age'], df['cond']):
    if(age == 2 and cond ==9):
      print(name)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文