Home > Net >  How to Get the values from a Dictionary Column and add it to the column?
How to Get the values from a Dictionary Column and add it to the column?

Time:10-12

This my dataframe and I am trying to get the winningTrans to be incuded with the dataframe.

Cannot include image as reputation is low.

I have tried doing following things:

df2 = pd.concat([df1["_id"].reset_index(drop=True), pd.json_normalize(df1["winningTrans"])], axis=1).fillna(0)

Also Tried:

def MarkWinnings(lst):
    for x in lst:
        if 'amount' in x.keys():
            return True
    return False
df1['Winnings'] =  df1['winningTrans'].apply(MarkWinnings)

Still I am getting error:

AttributeError: 'str' object has no attribute 'keys

CodePudding user response:

df = df.join(df.winningTrans.apply(pd.Series))

CodePudding user response:

One line

df["winnings"] = df["a"].astype(str).str.contains('amount')

Error in your code You should use in operator like this

df = pd.DataFrame({"a": [{"amount": 2000, "hell": True}, {"hell": True}]})
def fun(x):
    if 'amount' in x:
        return True
    return False
df["winnings"] = df["a"].apply(fun)
  • Related