为什么我们要使用式式键来获取钥匙,而不是获得值

发布于 2025-02-05 09:24:13 字数 455 浏览 0 评论 0原文

我在此示例中有一个问题:

Country1 = {'Japan': 80, 'China': 450, 'India': 200, 'USA': 250}
Country2 = {'Brazil': 100,'China': 500, 'India': 210,'USA': 260}

# Convert into Pandas Series

sales_Q1 = pd.Series(Country1)
sales_Q2 = pd.Series(Country2)

sales_Q1.keys()  #=====> Index(['Japan', 'China', 'India', 'USA'], dtype='object')
sales_Q1.values  #=====> array([ 80, 450, 200, 250], dtype=int64)

问题:为什么我们要使用括号来获取钥匙而不获得值?

I have a question in this example:

Country1 = {'Japan': 80, 'China': 450, 'India': 200, 'USA': 250}
Country2 = {'Brazil': 100,'China': 500, 'India': 210,'USA': 260}

# Convert into Pandas Series

sales_Q1 = pd.Series(Country1)
sales_Q2 = pd.Series(Country2)

sales_Q1.keys()  #=====> Index(['Japan', 'China', 'India', 'USA'], dtype='object')
sales_Q1.values  #=====> array([ 80, 450, 200, 250], dtype=int64)

Question: Why do we use brackets to get keys and not to get values?

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

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

发布评论

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

评论(1

北陌 2025-02-12 09:24:13

使用.keys()称为方法,.values称为属性。

这是一个简单的示例,可以让您了解什么是方法和属性。

# when you call pandas you actually call its class (in this example called Greeting)
class Greeting:

  def __init__(self, name="adam"):
    # This is example of attribute
    self.name = name

  def hello(self):
    # This is example of method
    print("hello " + self.name)

pd = Greeting()
# example of set attribute
pd.name = "Edward"

# This is example of calling attribute
print(pd.name)
# This is example of calling method
print(pd.hello())

因此,要回答您的问题,为什么我们要使用括号来获取键而不是获得值,这是因为pandas codebase设计,使用.keys()作为方法和方法.values作为属性

Using .keys() known as a method, and .values known as an attribute.

Here's a simple example to give you clearance about what are method and attributes.

# when you call pandas you actually call its class (in this example called Greeting)
class Greeting:

  def __init__(self, name="adam"):
    # This is example of attribute
    self.name = name

  def hello(self):
    # This is example of method
    print("hello " + self.name)

pd = Greeting()
# example of set attribute
pd.name = "Edward"

# This is example of calling attribute
print(pd.name)
# This is example of calling method
print(pd.hello())

So to answer your question why do we use parentheses to get keys and not to get values, it's because of the pandas codebase design, that uses .keys() as method and .values as attribute

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