Reputation: 395
Well I have a simple csv, that has 2 columns and about 50 rows.
The first column is ip and other is cik, and I want to get how many ip's are there with the different cik. So this is my code that does that, and it work great:
code:
import pandas as pd
csv = pd.read_csv('test.csv')
df = pd.DataFrame(csv)
df = df.groupby('cik').count()
df = pd.DataFrame(df).to_csv('output.csv', index=False)
But the csv output is like:
ip
49
And I want it to be like when I print the df value after groupby and count, something like this:
So I have in the first column the cik and in other the number of ip's that have that cik.
Upvotes: 0
Views: 332
Reputation: 313
Try adding reset_index before you output to_csv.
import pandas as pd
csv = pd.read_csv('test.csv')
df = pd.DataFrame(csv)
df = df.groupby('cik').count().reset_index() #reset_index creates 0...n index and avoids cik as index
df.to_csv('output.csv', index=False)
OR
set the index=True
while outputting to_csv
df.to_csv('output.csv', index=True)
Upvotes: 1
Reputation: 984
Your option index=False
makes the method omit row names which in your case is the 1515671
, save it with simple:
df.to_csv('output.csv')
Upvotes: 2