Home>

I want to extract only the data from Excel in Excel by specifying conditions.
Then I want to write the extracted data to Excel.
In the middle of that, I was trying to create a data frame with Pandas, and when I tried to change the data to Excel and assign a column name in the data frame definition part, the following error message occurred. It is written in the part of # that i am trying to do, but please also see if the code matches it.
Thank you.

Error message
The truth value of a Series is ambiguous.Use a.empty, a.bool (), a.item (), a.any () or a.all ().
Applicable source code
import pandas as pd
df_A = pd.read_excel ("pandas_practice.xlsx", sheet_name = "Sheet1", index_col = [0,1])
# pd.read_excel (EXCEL file name, sheet_name = sheet name, index_col = column name/column number specified for index)
Columns1 = ["CogDeg", "i", "j", "L", "B", "V"]
#Column name
df_B = pd.DataFrame (data = df_A, columns = Columns1)
#Create a data frame with a named column
df_C = df_B [df_B ["i"] == 0 and df_B ["j"] == 0]
# Extract all rows where both column names i and j are 0 from the data frame
df_C = pd.to_excel ("C: \ Desktop \ Python \ pandas_practice.xlsx", sheet_name = "Sheet2")
# pd.to_excel (Destination directory + EXCEL file name, sheet_name = Sheet name)

I didn't understand even after examining it.

Supplemental information (FW/tool version etc.)

Please provide more detailed information here.

  • Answer # 1

    df_C = df_B [df_B ["i"] == 0 and df_B ["j"] == 0]
    The condition specified in the

    part is incorrect.

    df_C = df_B [(df_B ["i"] == 0)&(df_B ["j"] == 0)]
    Please use

    .

    Reference
    [Python] Dealing with errors when multiple conditions are specified in pandas.DataFrame-Qiita
    What to do when an error like The truth value of ... is ambiguous. occurs in numpy or pandas-A quiet name (拙 Article)

  • Answer # 2

    ☓ df_C = pd.to_excel (.. ....
    ◯ df_C.to_excel (......