0

I have pandas column, one sample value looks like this

 colName1 {'key1': {'key2': {'ke3': {'label': '3 minutes, 16 seconds'}}, 'simpleText': '3:16'}, 'style': 'DEFAULT'}

tried this but din't work.

df1['XYZ'] = df1['colName1'].apply(lambda x: x['key1']['key2']['simpleText'])

How can I retrieve the value of 'simpleText' which equals to 3:16

2
  • please provide an unambiguous reproducible input. Commented Aug 31, 2022 at 12:25
  • this nested json is the input for 1 dataframe with 1 row{'key1': {'key2': {'ke3': {'label': '3 minutes, 16 seconds'}}, 'simpleText': '3:16'}, 'style': 'DEFAULT'} Commented Aug 31, 2022 at 12:27

3 Answers 3

1

Let's try

df1['XYZ'] = df1['colName1'].str['key1'].str['simpleText']
Sign up to request clarification or add additional context in comments.

Comments

0

Use

x['key1']['simpleText']

key2 is redundant.

4 Comments

its giving me error TypeError: 'float' object is not subscriptable
Then probably some value of your colName1 is not in the format that you've provided here
thecolName1 is showing object as datatype.
df['colName1'] contains many values (yes, objects)
0

#Try this:

val['XYZ'] = val['key1']['simpleText']
print(val['XYZ'])

#output: 3:16

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.