a1234
a1234

Reputation: 821

Adding columns to dataframe

I have the following:

count = a[(a.type == 'exact') & (a.score == 1.0)].groupby('id').count()['type'] countbyid = pd.DataFrame(data = count)

Which gives me the following:

id type 102116 578 1256 1211 126805 215 31560 12 329401 2310 49923 375 9691 2409

When I try to add column names to countbyid = pd.DataFrame(data = count, columns = ['customer', 'department']) NO data is returned, only the column names:

customer department

Upvotes: 0

Views: 50

Answers (2)

meW
meW

Reputation: 3967

With your given approach, you are inserting the dataframe which already has column names and when you assign the columns inside, no values against those columns are assigned, hence you receive a null dataframe.

So two approaches to tackle this problem -

First:

countbyid = pd.DataFrame(data = countbyid.values)
countbyid.columns = ['customer', 'department']

Second (as mentioned in comment by ScottBoston):

countbyid = pd.DataFrame(data= countbyid.values, columns = ['customer', 'department'])

Upvotes: 3

Vaishali
Vaishali

Reputation: 38425

Based on your code, I assume that a is a dataframe, you just need to reset index and rename columns. No need to call DataFrame constructor again.

a[(a.type == 'exact') & (a.score == 1.0)].groupby('id').type.count().reset_index()\
.rename({'id':'customer', 'type':'department'})

Upvotes: 1

Related Questions