这是文件输入:File Input
使用上述文件,如何计算使用pandas的csv文件中的行出现次数,并在附加了计数的情况下写入另一个文件中?并应删除其余重复的行。
例如:
0 1 1 1 -1 -1 -1 1 yesno
0 1 -1 -1 1 1 1 -1 no
0 1 -1 -1 -1 -1 -1 -1 yes
0 1 1 1 -1 -1 -1 1 yesno
1 0 1 1 -1 -1 -1 1 yesno
1 0 1 1 1 -1 -1 -1 yesno
1 0 1 1 1 -1 -1 -1 yesno
0 1 1 1 -1 -1 -1 1 yesno
0 1 -1 -1 1 1 1 -1 no
0 1 -1 -1 -1 -1 -1 -1 yes
0 1 1 1 -1 -1 -1 1 yesno
1 0 1 1 -1 -1 -1 1 yesno
1 0 1 1 1 -1 -1 -1 yesno
1 0 1 1 1 -1 -1 -1 yesno
输出应为
0 1 1 1 -1 -1 -1 1 yesno 2
0 1 -1 -1 1 1 1 -1 no 2
0 1 -1 -1 -1 -1 -1 -1 yes 2
0 1 1 1 -1 -1 -1 1 yesno 2
1 0 1 1 -1 -1 -1 1 yesno 2
1 0 1 1 1 -1 -1 -1 yesno 2
1 0 1 1 1 -1 -1 -1 yesno 2
答案 0 :(得分:2)
您可以这样做
list_of_lines = open('file.txt', 'r')
content = list_of_lines.readlines()
list_of_lines = list_of_lines.close()
if len(sys.argv) == 2:
if sys.argv[1] == '--delete':
deleted_line = int(sys.argv[2])
list_of_lines = open('file.txt', 'a')
for i, line in enumerate(content):
请看这里 How to count duplicate rows in pandas dataframe?
拥有此文件后,您可以将其写入文件或执行所需的任何操作。