Home > Enterprise >  Converting A Columnless Matrix To dict
Converting A Columnless Matrix To dict

Time:01-16

I want to fetch data from a text file that doesn't have columns, but has about 8 entries, and I want to give each column a name as shown in this code, but I want an alternative because it's running very slow. I want an easier solution than this, thank you

Programming language:Python

dataframe = pd.read_csv("drive/MyDrive/Test.TxT", header=None)

#Convert Data
def ConvTDi(lst):
  if type(lst) is dict:
    return lst
  else:
    return {'Col1': lst[0], 'Col2': lst[1], 'Col3': lst[2], 'Col4': lst[3], 'Col5': lst[4], 'Col6': lst[5], 'Col7': lst[6], 'Col8': lst[7]}

def RetCTDV():
  ValC = ConvTDi(dataframe.iloc[0,0:8].values)
  for i in range(len(dataframe.index)):
    if i == 0:
      i = 1
    ValC = np.append(ValC, ConvTDi(dataframe.iloc[i,0:8].values))
  return ValC

input_data = RetCTDV()
output_data = dataframe.iloc[:,8].values

Values Data:

2106040200,275020300,243020300,2640102010,21180204020,156050100,286040200,1640102010,0
275020300,243020300,2640102010,21180204020,156050100,286040200,1640102010,156040200,1 30 130
143040200,2640102010,21180204020,156050100,286040200,1640102010,156040200,2960102030,1 0 160
2640102010,21180204020,156050100,286040200,1640102010,156040200,2960102030,254020200,1 10 150
21180204020,156050100,286040200,1640102010,156040200,2960102030,254020200,2640102010,0
156050100,286040200,1640102010,156040200,2960102030,254020200,2640102010,11080203030,0
286040200,1640102010,156040200,2960102030,254020200,2640102010,11080203030,1117020050,0

I want to come up with this example for each row:

{Col1:275020300,Col2:243020300,Col3:2640102010,Col4:21180204020,Col5:156050100,Col6:286040200,Col7:1640102010,Col8:156040200},1 30 130

CodePudding user response:

Read the data into a dataframe, convert the first eight columns to dictionaries, append the last column:

x = pd.read_table("Test.TxT", header=None, sep=',', 
       names=["Col1","Col2","Col3","Col4","Col5","Col6","Col7","Col8",""])

pd.concat([x.iloc[:,:-1].apply(dict), x.iloc[:,-1]], axis=1)
  • Related