python - Pandas:向现有数据框添加新行,在所有列中保持相同的分布
问题描述
我正在使用熊猫数据框。我想将我的数据框的大小从 1000 增加到 4432(不完全是 n 倍,n 是自然数)。我想确保增加大小后每列中的值分布保持不变。例如,如果我有Car
给定分布的列名,现有 100 行。
Maruti 30%
Ford 10%
Tata 40%
Others 10%
我想在将大小增加到 4432 后保持这个份额不变
该列可以是范围、数字、分类。更多的例子是Age
像这样的分布
20-30 20%
30-40 40%
40-50 25%
50-60 15%
同样,我想在增加 Dataframe 大小的同时保持这个分布不变。
解决方案
以下函数对每个唯一值的目标行数进行四舍五入,因此与仅复制整个数据帧相比,分布更接近所需的分布。在以下示例中,对于乘数 1.5,您实际上可以保留分布,即使简单concat
不会为您1.5x
提供原始数据帧。
def increase_df(df, column, multiplier):
new_value_counts = (df[column].value_counts() * multiplier).apply(lambda value: int(round(value)))
values = sum(([value] * count for value, count in new_value_counts.to_dict().items()), [])
return pd.DataFrame(values)
df = pd.DataFrame(["Mumbai"] * 4 + ["Kolkata"] * 2 + ["Chennai"] * 2 + ["Delhi"] * 4, columns=['city'])
print df
city
0 Mumbai
1 Mumbai
2 Mumbai
3 Mumbai
4 Kolkata
5 Kolkata
6 Chennai
7 Chennai
8 Delhi
9 Delhi
10 Delhi
11 Delhi
# here the distribution can be preserved exactly
print increase_df(df, 'city', 1.5)
0
0 Kolkata
1 Kolkata
2 Kolkata
3 Chennai
4 Chennai
5 Chennai
6 Delhi
7 Delhi
8 Delhi
9 Delhi
10 Delhi
11 Delhi
12 Mumbai
13 Mumbai
14 Mumbai
15 Mumbai
16 Mumbai
17 Mumbai
# here it can't, because the target number of rows per value is fractional.
# The function rounds that number to the nearest int, so the distribution is as close to the original one as it can get.
print increase_df(df, 'city', 1.8)
0
0 Kolkata
1 Kolkata
2 Kolkata
3 Kolkata
4 Chennai
5 Chennai
6 Chennai
7 Chennai
8 Delhi
9 Delhi
10 Delhi
11 Delhi
12 Delhi
13 Delhi
14 Delhi
15 Mumbai
16 Mumbai
17 Mumbai
18 Mumbai
19 Mumbai
20 Mumbai
21 Mumbai
推荐阅读
- c++ - 通过构造函数创建的所有对象都具有相同的向量
- react-dates - airbnb/react-dates 中未显示箭头(在移动屏幕中是,不在桌面中)
- windows - 如何从 CMD 执行添加到 WSL2 内的 $PATH 的二进制文件?
- python - 比较python中的两个列表元素
- r - 当字符串带有空格时,agrep 不起作用,用于字符串匹配
- sql - 每个组的本地最大值的 SQL 排序结果
- android - java.lang.NullPointerException:尝试插入到 Telephony.Sms.Conversations 时,Uri 不能为空
- mysql - 如何在 Django 模型中声明 JSON 字段?
- azure - 部署到存储帐户的静态网站的正确 scm?
- c++ - 为生成的代码扩展 C++ 模板参数类型名称