【发布时间】:2023-02-06 23:56:21
【问题描述】:
假设我有一个这样的数据框:
| colors | animals |
|---|---|
| yellow | cat |
| yellow | cat |
| red | cat |
| red | cat |
| blue | cat |
我想为每一列创建一个列,显示每个值发生的频率:
| colors | colors_frequency | animals | animals_frequency |
|---|---|---|---|
| yellow | 40% | cat | 100% |
| yellow | 40% | cat | 100% |
| red | 40% | cat | 100% |
| red | 40% | cat | 100% |
| blue | 20% | cat | 100% |
我试过
frequency = list()
for column in df.columns:
series = (df[column].value_counts(normalize=True, dropna=True)*100)
overview.append(series)
#overview list
o_colors = overview[0]
o_animals = overview[1]
df['animals_frequency'] = o_animals
如果我尝试
df.info()
它返回
| Column | Non-Null | Count | Dtype |
|---|---|---|---|
| animals_frequency | 0 | non-null | float64 |
【问题讨论】:
标签: python pandas data-analysis exploratory-data-analysis