Junior P
Junior P

Reputation: 39

How in Pandas Dataframe and Python can extract the specific text string after a given word?

I would like to extract the text inside the range "text: ....." from this dataframe and create another column with that value.

This is my Pandas Dataframe

enter image description here

issues_df['new_column'] = issues_df['fields.description.content'].apply(lambda x: x['text'])

However, it returns the following error:

issues_df['new_column'] = issues_df['fields.description.content'].apply(lambda x: x['text'])
TypeError: Object 'float' is not writable.

Any suggestions?

Thanks in advance.

Upvotes: 0

Views: 263

Answers (2)

Pierre D
Pierre D

Reputation: 26311

That could be a good task for the rather efficient json_normalize:

df['new_column'] = pd.json_normalize(
    df['fields.description.content'], 'content'
)['text']

Upvotes: 0

Ynjxsjmh
Ynjxsjmh

Reputation: 30070

Problem is NaN in column, you can try .str accessor

issues_df['new_column'] = issues_df['fields.description.content'].str[0].str['content'].str[0].str['text']

Upvotes: 2

Related Questions