0

I have a dataframe which has several columns with values as dictionaries. These columns also contain some None (not sure if it's a string or just missing values).

df = pd.DataFrame([[{0: 300}, {0: 157}, {0: 456}],[{0: 298}, None, {0: 498}],[None, None, {0: 987}]],   columns=['col1', 'col2', 'col3'])

All dictionaries have key = 0, values = number from 100 to 10000.

I need to loop through all columns and rows and extract only the value. Preferably, i will overwrite the columns with only the values

So, end result should look like this:

df = pd.DataFrame([[300, 157, 456],[298, None, 498],[None, None, 987]],   columns=['col1', 'col2', 'col3'])

The number is actually an ID which I will use later on for a "vlookup" into another dataframe.

I tried with lambda functions:

df['col1'] = df['col1'].apply(lambda x: x.values() if x is not None else x)

I did manage to extract the values. Issue is the type of these values is recognized as a dictionary value (they look like this when i print them: (300)).

I need them as integers. I tried chaining an astype(int) but i do get an error (something like, you can't do that on a dictionary's value) Any thoughts?

1
  • Can you share a reproducible example as text and a clear expected output ? Commented Dec 6, 2022 at 22:24

1 Answer 1

1

Use DataFrame.applymap for processing all columns with get first values of dict values:

df = df.applymap(lambda x: list(x.values())[0] if isinstance(x, dict) else x)
print (df)
    col1   col2  col3
0  300.0  157.0   456
1  298.0    NaN   498
2    NaN    NaN   987

If need integers convert values to Int64:

df = (df.applymap(lambda x: list(x.values())[0] if isinstance(x, dict) else x)
       .astype('Int64'))
print (df)
   col1  col2  col3
0   300   157   456
1   298  <NA>   498
2  <NA>  <NA>   987
Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting this error: AttributeError: 'int' object has no attribute 'values'
@santee - Answer was edited.
Nevermind. You solution works. I was just looping through my entire dataframe (which has also columns of other types). I restricted to the ones with dictionaries, and now works. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.