Home>

I want to get data from an Excel file using variables, but I don't know how.

| Group | Number | Product name
| A | 1 | Banana
| A | 2 | Apples
| A | 3 | Orange
| A | 4 | Pineapple
| A | 5 |
| B | 1 | Chinese cabbage
| B | 2 | Radish
| B | 3 |
| B | 4 | Green onion
| B | 5 | Peppers


Please let me know how to get the product name "radish" using the variable from the above Excel file.
The environment is
Windows10
Python3
is.
I'm new to Python. Thank you.

  • Answer # 1

    I don't know exactly what kind of processing I want to get'radish', but it is better to read Excel usingpandasIt's not easy

    https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html

    import pandas as pd
    df = pd.read_excel ('data.xlsx')
    print (df)
    # Group number Product
    # 0 A 1 Banana
    # 1 A 2 Apple
    # 2 A 3 Orange
    # 3 A 4 Pineapple
    # 4 A 5 Mandarins
    # 5 B 1 Chinese cabbage
    # 6 B 2 Radish
    # 7 B 3 Shungiku
    # 8 B 4 Green Onion
    # 9 B 5 peppers
    ret = df.loc [(df ['group'] == 'B')&(df ['number'] == 2), 'product']
    print (ret)
    # 6 Radish
    #Name: Product, dtype: object

  • Answer # 2

    If you go through "python excel", there will be many explanation articles.
    Read it all and ask if you have any questions.