读取表格数据,按照固定列排序

淩亂°似流年 2022-09-16 06:16 279阅读 0赞
  1. import csv
  2. import pandas as pd
  3. import numpy as np
  4. # This is a sample Python script.
  5. # Press Shift+F10 to execute it or replace it with your code.
  6. # Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
  7. '''
  8. 查找相同子公共子序列 获得索引
  9. '''
  10. def print_hi(name):
  11. list1 = []
  12. list2 = []
  13. name = []
  14. listfull1 = []
  15. listfull2 = []
  16. # Use a breakpoint in the code line below to debug your script.
  17. print(f'Hi, {
  18. name}') # Press Ctrl+F8 to toggle the breakpoint.
  19. with open('/home/philtell/Desktop/0514_biogas_p1_asv.csv','r',encoding = "ISO-8859-1") as csvfile:
  20. reader = csv.reader(csvfile)
  21. # list1 = [row for row in reader]
  22. list1 = [row[8] for row in reader]
  23. with open('/home/philtell/Desktop/0514_biogas_p1_asv.csv','r',encoding = "ISO-8859-1") as csvfile:
  24. reader = csv.reader(csvfile)
  25. listfull1 = [row for row in reader]
  26. with open('/home/philtell/Desktop/0514_biogas_p2_asv.csv','r',encoding = "ISO-8859-1") as csvfile:
  27. reader = csv.reader(csvfile)
  28. list2 = [row[8] for row in reader]
  29. with open('/home/philtell/Desktop/0514_biogas_p2_asv.csv','r',encoding = "ISO-8859-1") as csvfile:
  30. reader = csv.reader(csvfile)
  31. listfull2 = [row for row in reader]
  32. # print(listfull2)
  33. listsize1 = len(list1)
  34. listsize2 = len(list2)
  35. mm = listsize2;
  36. nn = listsize1;
  37. sameIndex = []
  38. AsameNumIndex = set() # 表格1 的具有相同seq的无序数据集合
  39. BsameNumIndex = []
  40. for i in range(1,mm):
  41. sub_list = [] # 表格1和表格2 具有相同seq 键值对
  42. sub_list.append(i)
  43. for j in range(1,nn):
  44. if(list2[i]==list1[j]):
  45. sub_list.append(j)
  46. AsameNumIndex.add(i)
  47. BsameNumIndex.append(j)
  48. sameIndex.append(sub_list)
  49. print(len(AsameNumIndex))
  50. print(len(sameIndex))
  51. print(sameIndex)
  52. newlist = []
  53. t1 = set()
  54. t2 = set()
  55. list3 = []
  56. for kk in sameIndex:
  57. j = 0
  58. for oo in kk:
  59. if len(kk)==2:
  60. if j==0:
  61. # print(oo)
  62. t2.add(oo)
  63. # print(listfull2[oo])
  64. list3.append(listfull2[oo])
  65. else:
  66. t1.add(oo)
  67. list3.append(listfull1[oo])
  68. # print(listfull1[oo])
  69. j = j+1
  70. for i in range(1,len(list1)):
  71. if i not in t1:
  72. list3.append(listfull1[i])
  73. for i in range(1,len(list2)):
  74. if i not in t2:
  75. list3.append(listfull2[i])
  76. print(len(list3))
  77. # print(len(newlist))
  78. # print(newlist)
  79. # print(sameIndex)
  80. # sameIndex2 = []
  81. # for i in range(nn):
  82. # for j in range(mm):
  83. # if (list2[i] == list1[j]):
  84. # sameIndex2.append(j)
  85. # print(len(sameIndex2))
  86. #
  87. '''
  88. '''
  89. # with open('/home/philtell/Desktop/0514_biogas_p1_asv.csv','r',encoding = "ISO-8859-1") as csvfile:
  90. # reader = csv.reader(csvfile)
  91. # list1 = [row[8] for row in reader]
  92. df = pd.read_csv("/home/philtell/Desktop/0514_biogas_p2_asv.csv",low_memory=False)
  93. data = np.array(df.loc[:, :])
  94. labels = list(df.columns.values)
  95. # temp = []
  96. # temp.append("序号")
  97. # for la in labels:
  98. # temp.append(la)
  99. # with open('/home/philtell/Desktop/0514_biogas_p2_asv.csv','r',encoding = "ISO-8859-1") as csvfile:
  100. # reader = csv.reader(csvfile)
  101. # count = 0
  102. # test = []
  103. # print(type(reader))
  104. # for row in reader:
  105. # if count in sameIndex:
  106. # test.append(row)
  107. # count = count + 1
  108. # # print(test)
  109. # #
  110. print(list3)
  111. # print(len(test))
  112. print(labels)
  113. test = pd.DataFrame(data=list3)
  114. test.to_csv('/home/philtell/Desktop/mylove.csv',encoding='utf-8')
  115. # Press the green button in the gutter to run the script.
  116. if __name__ == '__main__':
  117. print_hi('PyCharm')
  118. # See PyCharm help at https://www.jetbrains.com/help/pycharm/

发表评论

表情:
评论列表 (有 0 条评论,279人围观)

还没有评论,来说两句吧...

相关阅读

    相关 oracle 按照固定顺序排序

    项目组统计报表需要,按照固定的公司名称顺序排序,这种排序一般使用的很少,但是针对特殊的场景就没办法,只能采用这种费力笨重的方法。 主要利用oracle的 decode函数