Mar-10-2024, 09:12 AM
There is a problem with your data, not all lines have the same number of items.
Lines C and D have 8 items, but you only have 7 column names, so where will you put item 8? E has 6 items and F has 5 items. Bit of a problem.
You should make sure your data is correct before you start. I changed the data a bit.
This will give you a database, then perhaps you can explain what it is that you want to do more clearly?
Lines C and D have 8 items, but you only have 7 column names, so where will you put item 8? E has 6 items and F has 5 items. Bit of a problem.
You should make sure your data is correct before you start. I changed the data a bit.
This will give you a database, then perhaps you can explain what it is that you want to do more clearly?
import pandas as pd # this will cause problems because you only have 7 column names # but C and D have 8 items E has 6 items and F has 5 items datastring = """A A1 Banking Financial 4.486 3.522 4.419 B BBB3 Banking Financial 0.445 0.559 0.701 C A1 Financial Services Financial 0.821 0.994 1.274 D A3 Financial Services Financial 2.744 3.445 4.415 E A2 Financial Services Financial 1.919 F A1 Insurance Financial 0.393""" # revised data datastring = '''A A1 Banking Financial 4.486 3.522 4.419 B BBB3 Banking Financial 0.445 0.559 0.701 C A1 Financial Services 0.821 0.994 1.274 D A3 Financial Services 2.744 3.445 4.415 E A2 Financial Services 1.919 5.791 2.348 F A1 Insurance Financial 0.393 0.998 3.726''' cols = ['Security', 'CompRating', 'Level_3', 'Level_2', 'parent_weight', 'child_weight', 'Total'] # 7 column names lists = [s.split() for s in datastring.split('\n')] df = pd.DataFrame(lists, columns=cols)You could make an empty dataframe and then read the lists in 1 by 1, but you still need another column name!