Converting comma separated string with key-value form into pandas Dataframe

Question:

I would like to convert the following string into a pandas dataframe:

data = "key=iApFK, age=58, key=234das, age=64, key=89snkj, age=47"

The dataframe would look like this:

            key        age    
0           iApFK      58  
1           234das     64
2           89snkj     47

I tried doing it with pandas.read_csv and io.SringIO but I am confused regarding the extraction of the values and the delimiter. Any ideas?

Asked By: John Mantios

||

Answers:

pd.DataFrame(re.findall("key=(w+), age=(w+),?", data), columns=["key", "age"])

you can find fields with re.findall, then pass the result to pd.DataFrame along with the column names:

In [32]: data
Out[32]: 'key=iApFK, age=58, key=234das, age=64, key=89snkj, age=47'

In [33]: re.findall("key=(w+), age=(w+),?", data)
Out[33]: [('iApFK', '58'), ('234das', '64'), ('89snkj', '47')]

In [34]: pd.DataFrame(re.findall("key=(w+), age=(w+),?", data), columns=["key", "age"])
Out[34]:
      key age
0   iApFK  58
1  234das  64
2  89snkj  47
Answered By: Mustafa Aydın

Sorry, I don’t have reputation to comment so posting.
Mustafa Aydın answered correctly but for some easier cases

d = pd.read_csv(StringIO(data), sep='=', header=None, lineterminator=',')

will work.

Answered By: Garuda
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.