Код: Выделить всё
df = pl.DataFrame(
{"a": [1, 2, 3], "b": [4, 5, 6], "c": [1, 1, 1]}
)
Код: Выделить всё
def col_operations(column: pl.Series):
col_stats = {
"count_unique": column.n_unique(),
"count_nans": column.null_count(),
"nan_frac": column.null_count()/len(column)
}
return col_stats
def profile(df: pl.DataFrame):
profiling_dict = {}
for col in df.columns:
profiling_dict[col] = col_operations(df[col])
return pd.DataFrame(profiling_dict) # NOTE: pandas
profile(df)
Код: Выделить всё
a b c
count_unique 3.0 3.0 1.0
count_nans 0.0 0.0 0.0
nan_frac 0.0 0.0 0.0
Я пытался использовать pl.all в качестве аргумента df.select, но получаю ошибку.
Код: Выделить всё
df.select(pl.all().sum(), pl.all().mean())
Код: Выделить всё
# DuplicateError: the name 'a' is duplicate
Код: Выделить всё
df.select(pl.all().sum().name.suffix("_sum"), pl.all().first().name.suffix("_first"))
Код: Выделить всё
shape: (1, 6)
┌───────┬───────┬───────┬─────────┬─────────┬─────────┐
│ a_sum ┆ b_sum ┆ c_sum ┆ a_first ┆ b_first ┆ c_first │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │
╞═══════╪═══════╪═══════╪═════════╪═════════╪═════════╡
│ 6 ┆ 15 ┆ 3 ┆ 1 ┆ 4 ┆ 1 │
└───────┴───────┴───────┴─────────┴─────────┴─────────┘
С Pandas я могу сделать это с помощью .apply:
Код: Выделить всё
def col_operations(column: pd.Series):
col_stats = {
"count_unique": column.nunique(),
"count_nans": column.isna().sum(),
"nan_frac": column.isna().sum()/len(column)
}
return pd.Series(col_stats)
Код: Выделить всё
df.to_pandas().apply(col_operations, axis=0)
Код: Выделить всё
a b c
count_unique 3.0 3.0 1.0
count_nans 0.0 0.0 0.0
nan_frac 0.0 0.0 0.0
Подробнее здесь: https://stackoverflow.com/questions/758 ... -dataframe
Мобильная версия