I have a dataset that looks like this:
VendorAccount FiscalPeriod LCC
729616, 1, False
729616, 2, False
0, 2, False
1, 4, False
I attempted to use this line:
df['LCC'][(df['VendorAccount'] == 729616) & df['FiscalPeriod'] >1] = True
to make it look like this:
VendorAccount FiscalPeriod LCC
729616, 1, False
729616, 2, True
0, 2, False
1, 4, False
The script runs but no changes are being made. Can anyone advise me on where I am going wrong?
CodePudding user response:
&
operator has higher precedence than >
, hence your original code is equivalent to this:
df['LCC'][((df['VendorAccount'] == 729616) & df['FiscalPeriod']) > 1] = True
To update the dataframe correctly you should use the following code instead:
df['LCC'][(df['VendorAccount'] == 729616) & (df['FiscalPeriod'] > 1)] = True
CodePudding user response:
First You need convert FiscalPeriod
to int
then you can run your check like below:
>>> df['FiscalPeriod'] = df['FiscalPeriod'].str[0].astype(int)
>>> m = (df['VendorAccount'] == '729616,') & (df['FiscalPeriod'] > 1)
>>> df.loc[m, 'LCC'] = True