0

Here is the file input: File Input

Using the above file, how I can count the line occurences in a csv file using pandas and write in another file with the count appended? and should remove the rest duplicate lines.

For example:

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

Output should be

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
5
  • Please define "count". Commented Jun 21, 2018 at 11:48
  • count the line in the complete file and removing the duplicate and write the number of occurrences before of the line of the row of the csv.. I hope you got it. Commented Jun 21, 2018 at 11:52
  • @jpp you can check the example I have define in the question Commented Jun 21, 2018 at 11:53
  • Is yesno / no / etc final column included in determining a repeat? Commented Jun 21, 2018 at 11:58
  • @jpp Yes it is the final one Commented Jun 21, 2018 at 12:29

1 Answer 1

2

You can do something like this

df.groupby(df.columns.tolist(),as_index=False).size()

Please see this How to count duplicate rows in pandas dataframe?

Once you have this, you can write it to file or do whatever is needed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.