0

I have the below python list:

w=[[['I=427', 'PLAN=1'], 'A=0PDB'],
 [['I=427', 'PLAN=1'], 'B=40NGC'],
 [['I=427', 'PLAN=1'], 'C=21#NGA'],
 [['I=429', 'PLAN=1'], 'A=0PDB'],
 [['I=429', 'PLAN=1'], 'B=18C'],
 [['I=429', 'PLAN=1'], 'C=28TGD'],
 [['I=429', 'PLAN=1'], 'D=18TGA'],
 [['I=429', 'PLAN=1'], 'E=1A'],
 [['I=429', 'PLAN=2'], 'A=0PDB'],
 [['I=429', 'PLAN=2'], 'B=17C']]

How can I convert it to the below pandas DataFrame:

enter image description here

So, from the second string in the list I want to select the first string, the number after equal sign and the last string. For example in B=40NGC, I want to choose B,40,C and put it into the DataFrame.

2
  • For the site_no and plan columns, do you want the entries to say "I=429" and "PLAN=1" or just 429 and 1? Commented Mar 9, 2022 at 0:05
  • either is fine. it doesn't matter. Commented Mar 9, 2022 at 0:10

1 Answer 1

2

Here's one approach:

Rework w a bit to create a list of lists and build a DataFrame. Then extract a digits from green_time column:

out = []
for lst, s in w:
    phase, rest = s.split('=')
    green_time, next_phase = rest[:-1], rest[-1]
    out.append(lst + [phase, green_time, next_phase])
out = pd.DataFrame(out, columns=['site_no', 'plan', 'phase', 'green_time','next_phase'])
out['green_time'] = out['green_time'].str.extract('(\d+)')

Alternatively, we could pass w to the DataFrame constructor and use str.extract to extract the relevant items in columns:

df = pd.DataFrame(w)
df = df.join(pd.DataFrame(df[0].tolist(), columns=['site_no', 'plan']))
df[['phase', 'green_time','next_phase']] = df[1].str.extract('(\w)=(\d+)([^0-9]+)')
df['next_phase'] = df['next_phase'].str[-1]
df = df.drop(columns=[0,1])

Output:

  site_no    plan phase green_time next_phase
0   I=427  PLAN=1     A          0          B
1   I=427  PLAN=1     B         40          C
2   I=427  PLAN=1     C         21          A
3   I=429  PLAN=1     A          0          B
4   I=429  PLAN=1     B         18          C
5   I=429  PLAN=1     C         28          D
6   I=429  PLAN=1     D         18          A
7   I=429  PLAN=1     E          1          A
8   I=429  PLAN=2     A          0          B
9   I=429  PLAN=2     B         17          C
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.