最近我一直在使用Pandas的数据框架,但在分析一些多维数据时遇到了困难。
假设我有以下数据:
order | sample | feature1 | feature2
-------------------------------------
1234 | A | 0.20 | 0.45
1234 | B | 0.71 | 0.08
1234 | C | 0.21 | 0.02
1234 | D | 0.87 | 0.88
5678 | A | 0.76 | 0.42
5678 | B | 0.01 | 0.03
5678 | C | 0.29 | 0.91
5678 | D | 0.70 | 0.78
我希望您能将所有内容按订单分组,并将每个特征总结为样本:
order | feature1 | feature2
| A | B | C | D | A | B | C | D
------------------------------------------------------------
1234 | 0.20 | 0.71 | 0.21 | 0.87 | 0.45 | 0.08 | 0.02 | 0.88
5678 | 0.76 | 0.01 | 0.29 | 0.70 | 0.42 | 0.03 | 0.91 | 0.78
这是我目前的进展:
这里是我目前的进展:
from pandas import *
df = DataFrame({"order": [1234, 1234, 1234, 1234, 5678, 5678, 5678, 5678], "sample": ["A", "B", "C", "D", "A", "B", "C", "D"], "feature1": [0.20, 0.71, 0.21, 0.87, 0.76, 0.01, 0.29, 0.70], "feature2": [0.45, 0.08, 0.02, 0.88, 0.42, 0.03, 0.91, 0.78]})
byorder = df.groupby("order")
# not sure how to go from 1 groupby object to a new dataframe having what i need
你有没有想过如何得到一个新的DataFrame,其中包含我需要的聚合数据?也许DataFrames并不是执行这种操作的最佳工具?请注意,不要删除HTML标签。