我试图按数据框的不同列中的类别汇总一些数据。这是数据。
feature1 feature2 featurem
brand1 good none good
brand2 bad good bad
brand.. none none good
brandn good none none
Run Code Online (Sandbox Code Playgroud)
我想要一张表格,显示我每个功能有多少(好,坏,无)。这样就可以告诉我,有多少品牌在功能1上有优势,在功能1上有劣势,在功能1上没有优势,依此类推。
我知道,例如
df["feature1"].value_counts()
Run Code Online (Sandbox Code Playgroud)
我可以分别为每个功能获取这些值,但是我想将其添加到具有所有功能的新数据框中。我怎样才能做到这一点?
结果表如下所示:
试试看,使用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)
Run Code Online (Sandbox Code Playgroud)
输出:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37 次 |
| 最近记录: |