Arkam
Arkam

Reputation: 35

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

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

Upvotes: 1

Views: 52

Answers (2)

Deepak Tripathi
Deepak Tripathi

Reputation: 3233

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):
    return 'amount' in x
df["winnings"] = df["a"].apply(fun)

Upvotes: 1

Eren Han
Eren Han

Reputation: 321

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

Upvotes: 1

Related Questions