My code seems to be outputting the list I want, however, when I try printing the list to CSV I do not get the same result on the .csv file for some reason. I am sure there’s something not right at the end of my code. Could anyone please shed some light? Thanks in advance.
JavaScript
x
10
10
1
import pandas as pd
2
df = pd.read_csv('microRuleSet-row.csv')
3
deduplicated_list = list()
4
for index, row in df.iterrows():
5
for item in row:
6
if item not in deduplicated_list:
7
deduplicated_list.append(item)
8
print(deduplicated_list)
9
df.to_csv('microRuleSet-row-noDupes.csv', index=False)
10
Advertisement
Answer
I have not used pandas before. But it looks like you are outputting to csv the original microRuleSet-row.csv that you loaded. You have to export the deduplicated_list to csv. OK so each row must have no duplicated items. This code will do that. The first (header) row is now numbered 0 to 5. This can be changed to to the original heading, and adding placeholders for the extra empty csv cells.
JavaScript
1
14
14
1
import pandas as pd
2
3
df = pd.read_csv('microRuleSet-row.csv')
4
no_duplicates_list = []
5
for index, row in df.iterrows():
6
new_row = []
7
for item in row:
8
if item not in new_row:
9
new_row.append(item)
10
no_duplicates_list.append(new_row)
11
print(no_duplicates_list)
12
df2 = pd.DataFrame(no_duplicates_list)
13
df2.to_csv('microRuleSet-row-noDupes.csv', index=False)
14