使用 loc 与 upper() 函数。

huangapple go评论58阅读模式
英文:

using loc with upper()

问题

我试图获取所有值,在大写情况下等于"EU"的值。下面是我的解决方案,但不起作用。有谁知道我应该如何做到这一点?

companies_df.loc[(companies_df["location"].str.upper() == 'EU')].head(10)
英文:

I'm trying to bring back all values that, in upper case, equal "EU". My solution below doesn't work. Does anybody know how I would do this?

companies_df.loc[(upper(companies_df["location"]) == 'EU')].head(10)

答案1

得分: 2

问题出在你的代码中,upper 函数没有正确应用于 location 列。在 pandas 中,你可以使用 str 访问器来对列应用字符串方法。

companies_df.loc[companies_df["location"].str.upper() == 'EU'].head(10)
英文:

The issue with your code is that the upper function is not applied correctly to the location column. In pandas, you can use the str accessor to apply string methods to a column.

companies_df.loc[companies_df["location"].str.upper() == 'EU'].head(10)

huangapple
  • 本文由 发表于 2023年7月3日 02:19:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定