Ian
Ian

Reputation: 25

Python if/else statement in dictionary / dataframe

I'm creating a data frame using a dictionary and I need to use an if else statement within it. I'm not sure how to do this. Do I need to create a logical function to do it?

Also I'm open to recommendations on a better way to create this data frame.

`Data1 = pd.DataFrame({ 'Name1' : [category.NUMBER1[0]],
                    'Name2' : [category.DATE[0]],
                    'Name3' : [category.NUMBER2[0]],
                    'Name4' : [item],
                    'Name5' :[ if value1 == 'string1' or value2 == 'string2':
                                    [variable1]
                               else: [0]]


                    })`

I'm getting invalid syntax for the if statement.

Upvotes: 0

Views: 69

Answers (1)

goodvibration
goodvibration

Reputation: 6206

Try this:

'Name5': [variable1 if value1 == 'string1' or value2 == 'string2' else 0]

Your attempt suggests that you want either [[variable1]] or [[0]], but I've assumed that what you actually want is either [variable1] or [0].

Upvotes: 1

Related Questions