0

A sample dataframe as mentioned below:

df_A = pd.DataFrame({'field1':[1,2,3,4,5], 'field2':[11,12,13,14,15], 'field3':[c1,c2,c3,c4,c5], 'field4':[m1,m2,m3,m4,m5], 'field5':[21,22,23,24,25], 'field6':[f1,f2,f3,f4,f5], 'field7':[31,32,33,34,35]})

I have a logic as mentioned below:

df_A['field7'] = df_A[(df_A['filed4']== 'abc') & (df_A['field5']== 'def')].groupby(['field1', 'field2','field3'], as_index=False)[['field6']].transform('count')

but in some scenarios the filter might yield no values and I am getting the following error:

ValueError: No objects to concatenate

Though I partially understand what the error is, I am not able to get the null value column as my expected answer(Example to apply groupby on an empty dataframe: Keep columns after a groupby in an empty dataframe)

Kindly let me know if I was wrong in any sense and Thanks in advance!

Edit: Added an example dataframe for the above mentioned case

maninekkalapudi
  • 958
  • 2
  • 10
  • 23
  • For me it working nice in pandas `0.23.4` – jezrael Oct 08 '18 at 06:45
  • I have 0.23.0... I'll update the version and check the results. Hope it'll not break anything. Thanks for the confirmation @jezrael – maninekkalapudi Oct 08 '18 at 06:48
  • I'm still facing the same error in 0.23.4 – maninekkalapudi Oct 08 '18 at 07:56
  • If change `[['field6']]` to `['field6']` same problem? – jezrael Oct 08 '18 at 08:02
  • yes. It's still the same issue – maninekkalapudi Oct 08 '18 at 08:28
  • Yes. Also I tried with .count() instead of .transform('count) and I got the error `Wrong number of items passed 4, placement implies 1` – maninekkalapudi Oct 08 '18 at 08:39
  • Tested with `df_A = pd.DataFrame({ 'field4':list('abcdef'), 'field1':[4] * 6, 'field2':[4] * 6, 'field3':[4] * 6, 'field6':[4] * 6, 'field5':list('aaabbb') })` and no problem – jezrael Oct 08 '18 at 08:55
  • @ManikanthaNekkalapudi, Please provide a **[mcve]**. In this case, that requires a sample of your dataframe which demonstrates the problem. If you need help with this, see [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – jpp Oct 08 '18 at 09:23
  • I'm confused between `.count()` and `transform('count')`. When filter result is empty DataFrame, `[[field6]].count()` __(with double square brackets)__ is working fine but not `transform('count')` and vice-versa if filter result is **NOT** empty DataFrame. Am I missing something here? – maninekkalapudi Oct 09 '18 at 04:17

0 Answers0