Home > Back-end >  how to check for condition going row by row in a dataframe pandas
how to check for condition going row by row in a dataframe pandas

Time:08-10

I have a dataframe where I have to check the value for every row and modify the column respectively.

I have a table where have a Col1 . IF Col1 has AAA, it has to be in {"AAA":today's date}, if it is BBB then different format, if it is CCC then timestamp of todays date with 16 in hour.

ID  Col1  Col2  
1   AAA   1234
2   BBB   1456
3   CCC   4567

Final format for Col1 is

ID  Col1                            Col2  
1   {"AAA":20220809}                1234
2   {"BBB":True}                    1456
3   {"CCC":"20220809T160000.000000"}4567

Currently I have a code to modify just for AAA. I also have to make sure i check for notnull values only.

if set(['Col1']).issubset(df_csv_generator.columns):
    mask_tif=df_csv_generator.Col1.notnull()
    result_tif = df_csv_generator.loc[mask_tif,'Col1'].str.split("=").apply(lambda cond:{term: int(getdate) for term in cond})
    df_csv_generator.loc[mask_tif, 'Col1'] = result_tif

How can i use np.select() to check for multiple values or go row by row ?

CodePudding user response:

You can try Series.map

d = {
    'AAA': {'AAA': 'format1'},
    'BBB': {'BBB': 'format2'},
    'CCC': {'CCC': 'format3'},
}

df['out'] = df['Col1'].map(d)
print(df)

   ID Col1  Col2                 out
0   1  AAA  1234  {'AAA': 'format1'}
1   2  BBB  1456  {'BBB': 'format2'}
2   3  CCC  4567  {'CCC': 'format3'}

CodePudding user response:

You can solve this logic with pandas' replace.

replacements = {'AAA':{'AAA':str(datetime.now().date()).replace("-",""),
                'BBB':{'BBB':True}
                'CCC':{'CCC':str(datetime.now().date()).replace("-","") "T160000"}
  • Related