当其他两个列值相等时,如何获取特定的列值?

发布于 2025-02-08 13:17:04 字数 565 浏览 1 评论 0原文

当其他2列值相等时,我想获得一个特定的列值,该如何完成?

用户中心
MARY10001000
JANE99992222

,假设我要获得的列值是“用户”。预期输出是

mary 

I would like to get a specific column value when the other 2 columns values are equal, how can I accomplish this?

usercenterright
mary10001000
jane99992222

Let's say the column value that I want to get is 'user'. Expected output is

mary 

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

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

发布评论

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

评论(4

°如果伤别离去 2025-02-15 13:17:04

另一个选项是使用dataframe.query,尽管它不是典型的用例。

>>> df.query('center == right')['user'].iat[0]

'mary'

Another option is using DataFrame.query, although it's not quite its typical use case.

>>> df.query('center == right')['user'].iat[0]

'mary'
烟雨凡馨 2025-02-15 13:17:04
df[df['center'] == df['right']]['user'][0]
'mary '

或者

df[df['center'] == df['right']]
    user    center  right
0   mary    1000    1000
df[df['center'] == df['right']]['user'][0]
'mary '

OR

df[df['center'] == df['right']]
    user    center  right
0   mary    1000    1000
一指流沙 2025-02-15 13:17:04

我认为 @Naveed的答案非常有效。我建议解决问题的另一种方法是使用“ .loc”方法。

pandas.dataframe.loc

我会写代码像这样:

# Defining the filtering condition
fc1 = df['center'] == df['right']

# Applying the filter condition to obtain the value of the desired column
df.loc[fc1, 'user'].values[0]

结果:

'mary'

希望它有帮助!

I think @Naveed's answer works perfectly. Another way I would suggest solving the problem is with the '.loc' method.

pandas.DataFrame.loc

I would write the code like this:

# Defining the filtering condition
fc1 = df['center'] == df['right']

# Applying the filter condition to obtain the value of the desired column
df.loc[fc1, 'user'].values[0]

Result:

'mary'

Hope it helps!

不弃不离 2025-02-15 13:17:04
df.loc[df['center']==df['right'], 'user'].iloc[0]
'mary'
df.loc[df['center']==df['right'], 'user'].iloc[0]
'mary'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文