我正在尝试按数据框中不同列中的类别汇总一些数据。这是数据。
feature1 feature2 featurem
brand1 good none good
brand2 bad good bad
brand.. none none good
brandn good none none
我想要一个表格,显示每个功能我有多少 [good,bad,none]。所以这会告诉我有多少品牌在功能 1 上有优势,在功能 1 上有问题,在功能 1 上没有,等等。
例如,我知道
df["feature1"].value_counts()
我可以分别为每个特征获取这样的值,但我想将它添加到我拥有所有特征的新数据框中。我该怎么做?
生成的表格如下所示:
请您参考如下方法:
试试这个,使用 apply 并传递 pd.Series.value_counts:
df = pd.DataFrame({'feature '+str(i):np.random.choice(['Good','Bad','none'], 20) for i in range(1,10)})
df.apply(pd.Series.value_counts)
输出:
feature 1 feature 2 feature 3 feature 4 feature 5 feature 6 \
Bad 6 12 6 10 6 4
Good 6 2 8 5 6 9
none 8 6 6 5 8 7
feature 7 feature 8 feature 9
Bad 3 6 7
Good 3 6 4
none 14 8 9
