0

i have data that is displayed like that :

Name1 : Name1 - {'Name1.1': ('data1', data2,data3), 'Name1.2': ('data1', data2,data3)}

Name2 : Name2 - {'Name2.1': ('data1', data2,data3)}

Name3 : Name3 - {'Name3.1': ('data1', data2,data3), 'Name3.2': ('data1', data2,data3),'Name3.3': ('data1', data2,data3)}

i want to convert it into table excel to that form : enter image description here

i have tried many combinations but i failed as i still a beginner can anyone help ? thank you in advance.

Mort
  • 3,379
  • 1
  • 25
  • 40
nazzlire
  • 57
  • 8
  • what do you mean with "displayed like that", that is not a valid python data storage format. You also said you tried manny things, please show at least the most promising approach you tried so far. – Andreas Aug 27 '21 at 11:43
  • Please explain clearly what you want to achieve. – ql.user2511 Aug 27 '21 at 11:44
  • @Andreas I'm listing data like that with "print("{} : {} - {}".format(name1, name2, data))" and the data is "data[name]=data1,data2,data3" – nazzlire Aug 27 '21 at 11:56
  • Please try to provide a good sample: [how-to-make-good-reproducible-pandas-examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) otherwise it will take longer fror us to reproduce your problem then to actually solve it. – Andreas Aug 27 '21 at 12:17

1 Answers1

2

If you were to improve your input you could get close:

Example:

import ast
import pandas as pd

# string as given is not valid...
s = '''
Name1 : Name1 - {'Name1.1': ('data1', data2,data3), 'Name1.2': ('data1', 'data2','data3')}
Name2 : Name2 - {'Name2.1': ('data1', data2,data3)}
Name3 : Name3 - {'Name3.1': ('data1', data2,data3), 'Name3.2': ('data1', 'data2','data3'),'Name3.3': ('data1', 'data2','data3')}
'''


# valid as all the dataX strings are in quotes...
s = '''
Name1 : Name1 - {'Name1.1': ('data1', 'data2','data3'), 'Name1.2': ('data1', 'data2','data3')}
Name2 : Name2 - {'Name2.1': ('data1', 'data2','data3')}
Name3 : Name3 - {'Name3.1': ('data1', 'data2','data3'), 'Name3.2': ('data1', 'data2','data3'),'Name3.3': ('data1', 'data2','data3')}
'''


ds = []

for l in s.splitlines():
    d = l.split('-')
    if len(d) > 1:
        df = pd.DataFrame(ast.literal_eval(d[1].strip()))
        ds.append(df)
        
for df in ds:
    df.reset_index(drop=True, inplace=True)

df = pd.concat(ds, axis= 1)

cols = df.columns

cols = [((col.split('.')[0], col)) for col in df.columns]

df.columns=pd.MultiIndex.from_tuples(cols)

print(df.T)

                   0      1      2
Name1 Name1.1  data1  data2  data3
      Name1.2  data1  data2  data3
Name2 Name2.1  data1  data2  data3
Name3 Name3.1  data1  data2  data3
      Name3.2  data1  data2  data3
      Name3.3  data1  data2  data3

enter image description here

MDR
  • 2,610
  • 1
  • 8
  • 18
  • please how do i fix the probleme that he is not displaying name1,name2 ... ?? for more [details](https://stackoverflow.com/questions/68962900/how-to-put-data-into-table-with-pandas) on the question – HiFAR Aug 28 '21 at 10:06