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?
CodePudding user response:
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
CodePudding user response:
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.