python - 从一个数据帧创建几个新的数据帧或字典
问题描述
我有一个这样的数据框:
evt pcle bin_0 bin_1 bin_2 ... bin_49
1 pi 1 0 0 0
1 pi 1 0 0 0
1 k 0 0 0 1
1 pi 0 0 1 0
2 pi 0 0 1 0
2 k 0 1 0 0
3 J 0 1 0 0
3 pi 0 0 0 1
3 pi 1 0 0 0
3 k 0 1 0 0
...
5000 J 0 0 1 0
5000 pi 0 1 0 0
5000 k 0 0 0 1
有了这些信息,我想创建几个其他数据框 df_{evt} (或者字典应该更好?):
df_1 :
pcle cant bin_0 bin_1 bin_2 ... bin_49
pi 3 2 0 1 0
k 1 0 0 0 1
df_2 :
pcle cant bin_0 bin_1 bin_2 ... bin_49
pi 1 0 0 1 0
k 0 1 0 0 0
总共将有 5000 个数据帧(每个 evt 1 个),其中每个数据帧:
*the column "cant" has the ocurrences of "pcle" in the particular "evt".
*bin_0 ... bin_49 have the sum of the values for this particular "pcle" in
the particular "evt".
实现这一目标的最佳方法是什么?
解决方案
这是一个可能的解决方案:
import pandas as pd
import numpy as np
columns = ["evt", "pcle", "bin_0", "bin_1", "bin_2", "bin_3"]
data = [[1, "pi", 1, 0, 0, 0],
[1, "pi", 0, 0, 0, 0],
[1, "k", 0, 0, 0, 1],
[1, "pi", 0, 0, 1, 0],
[2, "pi", 0, 0, 1, 0],
[2, "k", 0, 1, 0, 0],
[3, "J", 0, 1, 0, 0],
[3, "pi", 0, 0, 0, 1],
[3, "pi", 1, 0, 0, 0],
[3, "k", 0, 1, 0, 0]]
df = pd.DataFrame(data=data, columns=columns)
# group your data by the columns you want
grouped = df.groupby(["evt", "pcle"])
# compute the aggregates for the bin_X
df_t = grouped.aggregate(np.sum)
# move pcle from index to column
df_t.reset_index(level=["pcle"], inplace=True)
# count occurrences of pcle
df_t["cant"] = grouped.size().values
# filter evt with .loc
df_t.loc[1]
如果你想把它变成字典,那么你可以运行:
d = {i:j.reset_index(drop=True) for i, j in df_t.groupby(df_t.index)}
推荐阅读
- c - 错误:未定义对 `ruserok` 的引用 - 在为 arm64 编译 PAM-1.1.8 时
- json - 我想在 POSTMAN 中使用 JSON 文件声明响应正文
- c# - 无法创建资源目录路径 Azure
- python - 如何获取焦点窗口的名称?
- android - 如果(条件)更改按钮中的图标
- xamarin.forms - 如何从后台线程更新 Ui - xamarin?
- azure-devops - 从存储库中的现有 YML 文件创建新管道 (Azure Pipelines)
- ios - Realm Swift嵌套递归列表未正确保存数据
- javascript - 调整浏览器大小时,引导导航栏不起作用
- php - $_POST 可以读取但值不是