newman
newman

Reputation: 9

Parse a string of floats in Python

list=[['10, 0.01, 0.0428, 120; 30, 0.1, 2, 33; 50, 0.023, 0.31, 0.65'],
      ['10, 0.7, 0.5428, 2.31'],
      ['50, 0.3, 0.35, 0.1'],
      ['-10, 0.2, 0.048, 124; -30, 0.11, 24, 3; -50, 0.02, 0.1, 0.60; 0, 0, 0, 0; 10, 0.1, 2, 33; 
       20, 0.023, 0.31, 0.66']]

df=pd.DataFrame(list)

I have a dataframe df from which I am trying to get the 3rd value after each semicolon sign if the column name matches with the 1st value after the semicolon sign. The expected output is as below. Any clue on how to tackle this in a simple way?

enter image description here

Upvotes: 0

Views: 114

Answers (1)

Corralien
Corralien

Reputation: 120391

Use nested loops:

d = {}
for r, i in enumerate(l):
    for j in i[0].split(';'):
        k = j.split(',')
        c, v = int(k[0]), float(k[2])
        d[(r, c)] = v

df = pd.Series(d).unstack(fill_value=0)

Output:

>>> df
   -50   -30    -10   0       10    20   30    50
0  0.0   0.0  0.000  0.0  0.0428  0.00  2.0  0.31
1  0.0   0.0  0.000  0.0  0.5428  0.00  0.0  0.00
2  0.0   0.0  0.000  0.0  0.0000  0.00  0.0  0.35
3  0.1  24.0  0.048  0.0  2.0000  0.31  0.0  0.00

Upvotes: 1

Related Questions