Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert string which illustrates some list into pandas dataframe?

I have the following string:

s = '[[[1],1,¬q,"A",[]],[[2],2,p→q,"A",[]],[[3],3,p,"A",[]],[[2,3],4,q,"→E",[2,3]],[[1,2,3],5,q∧ ¬q,"∧I",[1,4]],[[1,2],6,¬p,"¬I",[3,5]]]'

My aim now is to convert this into some pandas dataframe with columns:

        df = pd.DataFrame(
                   columns=['Assumptions', 'Index', 'Proposition', 'Premisses', 'Rule'])

which can be illustrated in console as follows:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

How can I do that?

>Solution :

Looks like a case for regular expressions.

import re
from ast import literal_eval
import pandas as pd

s = ('[[[1],1,¬q,"A",[]],[[2],2,p→q,"A",[]],[[3],3,p,"A",[]],'
     '[[2,3],4,q,"→E",[2,3]],[[1,2,3],5,q∧ ¬q,"∧I",[1,4]],[[1,2],6,¬p,"¬I",[3,5]]]')

rows = []

# split at ',' followed by two closing ]]
for x in re.split(r"(?<=\]\]),", s[1:-1]):
    
    # split at ',' after closing ] OR between '"' and opening [ 
    left, middle, right = re.split(r"(?<=\]),(?=\d)|(?<=\"),(?=\[)", x[1:-1])

    # split the middle part at ','
    middle = middle.split(",")
    
    rows.append([literal_eval(left), *middle, literal_eval(right)])
    


df = pd.DataFrame(rows, columns=['Assumptions', 'Index', 'Proposition', 'Premisses', 'Rule'])
df["Index"] = df.Index.astype(int)
df["Premisses"] = df.Premisses.str.strip('"')

Result:

  Assumptions  Index Proposition Premisses    Rule
0         [1]      1          ¬q         A      []
1         [2]      2         p→q         A      []
2         [3]      3           p         A      []
3      [2, 3]      4           q        →E  [2, 3]
4   [1, 2, 3]      5       q∧ ¬q        ∧I  [1, 4]
5      [1, 2]      6          ¬p        ¬I  [3, 5]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading