pandas 将无列矩阵转换为dict

enxuqcxy  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(183)

我想从一个文本文件中获取数据,这个文件没有列,但是有大约8个条目,我想给予每一列一个名字,如这段代码所示,但是我需要一个替代方案,因为它运行得非常慢。
编程语言: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

数值数据:

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

我想为每一行做一个例子:

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

e5nszbig1#

将数据读入 Dataframe ,将前8列转换为字典,追加最后一列:

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)

相关问题