mercredi 31 janvier 2018

How to use pandas isin() with IF statement

I have a column(INT_STATUS) in a data frame(file1) and INT_STATUS has values from A to Z and 1 to 9. If INT_STATUS columns has values in this list ['B','C','F','H','P','R','T','X','Z','8','9'] then I want to create a new column "rcut" and give a value '01' file1['rcut'] == '01'.

 file1=pd.read_csv(os.path.join("rtl_one", sep="\x01")


def risk_pass():
    if file1[file1['INT_STATUS'].isin(['B','C','F','H','P','R','T','X','Z','8','9'])]:
        file1['rcut'] = '01'

    else:
        file1['rcut'] = '00'

risk_pass()

But when I executing the above code I'm getting the following error. Please help

    sys:1: DtypeWarning: Columns (9,21,46,47,52,56) have mixed types. Specify dtype option on import or set low_memory=False.
Traceback (most recent call last):
  File "views.py", line 52, in <module>
    risk_pass()
  File "views.py", line 45, in risk_pass
    if file1[file1['INT_STATUS'].isin(['B','C','F','H','P','R','T','X','Z','8','9'])]:
  File "/opt/anaconda3-4.4.0/lib/python3.6/site-packages/pandas/core/generic.py", line 953, in __nonzero__
    .format(self.__class__.__name__))
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Aucun commentaire:

Enregistrer un commentaire