How to check if a column exists in Pandas

前端 未结 3 1894
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 18:33

Is there a way to check if a column exists in a Pandas DataFrame?

Suppose that I have the following DataFrame:

>>> import pandas as pd
>&         


        
3条回答
  •  难免孤独
    2020-11-28 18:53

    This will work:

    if 'A' in df:
    

    But for clarity, I'd probably write it as:

    if 'A' in df.columns:
    

提交回复
热议问题