简体   繁体   English

如何根据 pandas Dataframe 的条件打印值?

[英]How can I print the value based on conditions on pandas Dataframe?

I'm new to pandas dataframe and python.我是 pandas dataframe 和 python 的新手。 Currently, I have a pandas dataframe which I'd like to print the values based on conditions set.目前,我有一个 pandas dataframe ,我想根据设置的条件打印值。

My df looks like this:我的 df 看起来像这样:

ID     Name    Price
1      Apple     1
2      Orange    0.5
3      Pear      0.7

I'd like to code it such that when I ask the user to input the ID, it will return the price.我想对其进行编码,以便当我要求用户输入 ID 时,它会返回价格。 For example, if the user input 2, it should return 0.5.例如,如果用户输入 2,它应该返回 0.5。

inputid = input("Please input ID: ")

What should I do next to get return Price from df based on the input?接下来我应该怎么做才能根据输入从 df 获取返回价格?

One of possible solutions:可能的解决方案之一:

  1. Set ID column in df as its index:df中的ID列设置为其索引:

     df.set_index('ID', inplace=True)
  2. Define the following function:定义如下 function:

     def getPrice(): inputid = input("Please input ID: ") try: return df.loc[int(inputid), 'Price'] except: return 'No such ID'

Then when you call it, executing getPrice() :然后当你调用它时,执行getPrice()

  • An input prompt is displayed.将显示输入提示。
  • The user enters the ID.用户输入 ID。
  • Within try block this function attempts to:try块中,此 function 尝试:
    • convert inputid to int (the index contains int values, but inputid is a string ), so one cause of error can be that the user just pressed Enter without entering any value,inputid转换为int (索引包含int值,但inputidstring ),因此导致错误的一个原因可能是用户只是按Enter而不输入任何值,
    • even if the user entered a number, it is possible that df does not contain such ID (the second cause of error).即使用户输入了一个数字,也有可能df不包含这样的ID (错误的第二个原因)。
  • If everything so far is OK, the function returns the price of interest.如果到目前为止一切正常,function 将返回感兴趣的价格。
  • But if any error occurred, the result is an error message.但是,如果发生任何错误,则结果是错误消息。

The Pandas documentation is very good and will cover everything you need. Pandas 文档非常好,将涵盖您需要的一切。

https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html

Let's call your DataFrame price_df , depending on whether your ID column is set as the index of the dataframe or not.让我们调用您的 DataFrame price_df ,具体取决于您的 ID 列是否设置为 dataframe 的索引。

If it is:如果是:

print(price_df.loc[inputid, "Price"])

If it isn't:如果不是:

print(price_df[price_df["ID"] == inputid]["Price"])

The .loc method is the primary way of accessing data from a DataFrame. .loc方法是从 DataFrame 访问数据的主要方式。

Here is a working solution.这是一个可行的解决方案。

df = pd.DataFrame({'ID' : (1, 2,3),
                  'Name':('Apple', 'Orange', 'Pear'),
                  'Price': (1, 0.5, 0.7)})
df
    ID  Name    Price
0   1   Apple   1.0
1   2   Orange  0.5
2   3   Pear    0.7

userin = input("Please input ID: ")
userin
`1`

df.loc[df['ID'] == float(userin)]

    ID  Name    Price
0   1   Apple   1.0

You need to change the user input to float or int for pandas to read.您需要将用户输入更改为floatint以便 pandas 读取。

You can try:你可以试试:

userin = input("input ID: ")

print("The price for the ID is- ",df[df.ID==float(userin)].Price.values[0])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM