0

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

1
  • I am not sure if you need dataframe constructor here, print count to see Commented Jan 17, 2019 at 19:11

2 Answers 2

3

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'])
Sign up to request clarification or add additional context in comments.

1 Comment

@meW Nailed it, thanks for the help & the clarification as to why I was receiving the null dataframe.
1

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'})

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.