Группировка по количеству и первое использование вместе в python - PullRequest
1 голос
/ 19 апреля 2020


Я использую

df_topics = df.groupby(['string1','theme']).count().reset_index().first()

, сообщая мне эту ошибку

TypeError: first() missing 1 required positional argument: 'offset'

Я просто хочу посчитать дубликаты по группам и хочу выбрать Не NULL в первой строке
Именно поэтому я использую first (). Что дает мне ненулевой первый ряд.

dataframe

string1      theme      type    tool    site 
houses       white      A       phone           
houses       black      B               cloud
houses       white      A               web
houses       white      A       phone   web

output

string1      theme      Type    tool    site   count
houses       white      A       phone   web    3
houses       black      B               cloud  1

Мой основной фокус - подсчет string1, но с этим я также хочу выбрать строку для в конечном выводе показан тот, кто имеет меньшее ненулевое значение.

Как решить эту проблему?

1 Ответ

3 голосов
/ 19 апреля 2020

Вы можете создать словарь столбцов без string1 с функцией first и добавить count для string1, передать GroupBy.agg и последний столбец переименования:

d = dict.fromkeys(df.columns.difference(['string1','theme']), 'first')
d['string1'] = 'count'
df_topics = (df.groupby(['string1','theme'], sort=False)
               .agg(d)
               .rename(columns={'string1':'count'})
               .reset_index())
print (df_topics)

  string1  theme   site   tool type  count
0  houses  white    web  phone    A      3
1  houses  black  cloud    NaN    B      1

Подробно :

print (d)
{'site': 'first', 'tool': 'first', 'type': 'first', 'string1': 'count'}

Или используйте именованные агрегаты:

df_topics = (df.groupby(['string1','theme'], sort=False)
              .agg(type=('type','first'),
                   tool=('tool','first'),
                   site=('site', 'first'),
                   count=('string1','count'))
              .reset_index())
print (df_topics)
  string1  theme type   tool   site  count
0  houses  white    A  phone    web      3
1  houses  black    B    NaN  cloud      1

То же самое, что генерировать значения динамически:

d = {x: (x, 'first') for x in df.columns.difference(['string1','theme'])}
d['count'] = ('string1','count')


df_topics = (df.groupby(['string1','theme'], sort=False)
               .agg(**d)
               .reset_index())
print (df_topics)
  string1  theme   site   tool type  count
0  houses  white    web  phone    A      3
1  houses  black  cloud    NaN    B      1

EDIT1:

g = df.groupby(['string1','theme'], sort=False)
df1 = g.size()
df_topics = g.first()

df_topics = pd.concat([df_topics, df1.rename("count")], axis=1, sort=False).reset_index() 
print (df_topics)
  string1  theme type   tool   site  count
0  houses  white    A  phone    web      3
1  houses  black    B    NaN  cloud      1
...