0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). エラーが発生する

0
Posted at

この記事は過去のエラー解決メモを整理したものです。
現在の推奨手順とは異なる可能性があります。
公式ドキュメントを確認して最新情報と差分がないかを確認してください。

事象

DataFrameの行と列を入れ替えるために foo.stack() を実行したところ、次のエラーが発生した。

raise ValueError(
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

環境

  • Windows 10
  • Python 3.9.6
  • pandas
  • VSCode 1.63.0

原因

  • 元のデータ foopandas.DataFrame ではなく pandas.Series になっていた。lociloc で1行だけ取り出すと、Seriesになる場合がある。

対策

  • pd.DataFrame(foo_series)pandas.DataFrame に変換してから、行と列を入れ替える。
def call_sql_to_df(wait, table, i):
    call_df = sql_to_df(table)
    try:
        call_df_series_row = call_df.loc[i]
    except KeyError:
        print("KeyError")

    else:
        user_info_grade_number = call_df_series_row["user_info_grade_number"]
        class_number = call_df_series_row["user_info_class_number"]
        student_number = call_df_series_row["user_info_student_number"]
        student_name = call_df_series_row["user_info_student_name"]
        body_temperature = call_df_series_row["user_info_body_temperature"]
        is_run_code = call_df_series_row["is_run_code"]

        engine = get_connection_with_engine()

        if is_run_code == 1:
            if user_info_grade_number == 3:
                enter_kekou_kansatu(wait, class_number, student_number, student_name, body_temperature)
                call_df_series_row["user_info_last_run_at"] = datetime_to_str()

                call_df_dataFrame = pd.DataFrame(call_df_series_row)  # pandas.Seriesだとエラーになるためpandas.DataFrameに変換
                npd_T = call_df_dataFrame.T  # 行と列をひっくり返す

参考情報

numpyやpandasでThe truth value of ... is ambiguous.のようなエラーが出たときの対処 - 静かなる名辞

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?