简体   繁体   中英

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

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM