• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: How to extract text from every row in a dictionary like column in dataframe?

Resolved: How to extract text from every row in a dictionary like column in dataframe?

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I have been trying this for way to long and can’t seem to figure out a concise way to extract the browser from the string. It is a column in a df so it needs to iterate over all the rows
The column looks like this
0        [{'name': 'Chrome', 'version': '36.0.1985.143'}]
1        [{'name': 'Chrome', 'version': '34.0.1847.137'}]
2         [{'name': 'Chrome', 'version': '29.0.1547.76'}]
3        [{'name': 'Chrome', 'version': '33.0.1750.154'}]
4        [{'name': 'Chrome', 'version': '36.0.1985.143'}]
The column is called browser.
I have tried the following.
df_agent_info['browser'].str.split("\[\{\'[a\-z]\'")
and other worse examples. I appreciate the help.

Answer:

import re

pattern = r"(?<='name': ')[\w ]+"

def match(x):
    if re.findall(pattern, x):
        return re.findall(pattern, x)[0]

df['browser'].apply(match)
(?<='name': ') is a positive lookahead: it looks for matches that follow in this case 'name': '

If you have better answer, please add a comment about this, thank you!

dataframe pandas python substring
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘crm.email’ doesn’t exist (Connection: mysql, SQL: select count(*) as aggregate from `email`

02/04/2023

Resolved: Regex: Curly braces in reverse? Match X characters before a string?

02/04/2023

Resolved: build.gradle.kts task to return string

02/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.