Код: Выделить всё
DeprecationWarning: DataFrameGroupBy.apply operated on the grouping columns.
This behavior is deprecated, and in a future version of pandas the grouping columns will be excluded from the operation.
Either pass `include_groups=False` to exclude the groupings or explicitly select the grouping columns after groupby to silence this warning.
< /code>
Это воспроизводимый пример: < /p>
import pandas as pd
# Sample DataFrame
df = pd.DataFrame({
'locale': ['en_US', 'en_US', 'fr_FR', 'fr_FR', 'fr_FR', 'zh_CN', 'zh_CN'],
'query': ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
'score': [1, 2, 3, 4, 5, 6, 7]
})
# Function to sample 5 rows per group
def sample_per_locale(df):
sampled_df = df.groupby("locale", group_keys=False) \
.apply(lambda x: x.sample(min(5, len(x)), random_state=42)) \
.reset_index(drop=True)
return sampled_df # Keeping locale but still getting the warning
# Run function
sampled_df = sample_per_locale(df)
print(sampled_df)
с использованием include_groups = false (но версия моей панды не поддерживает ее):
df.groupby("locale", group_keys=False, include_groups=False) # TypeError
< /code>
Явно выбрал локал после Apply (): < /p>
sampled_df[['locale'] + [col for col in sampled_df.columns if col != 'locale']]
< /code>
Это все еще запускает предупреждение. Изменение в будущих версиях?
Любая помощь будет очень оценена! < /p>
Подробнее здесь: https://stackoverflow.com/questions/795 ... uping-colu