-
胡说叔叔
用途pandas.Series.value_counts适用于每一列seaborn.heatmap将绘制一个DataFrame如果提供了 Pandas DataFrame,索引/列信息将用于标记列和行。选项1import seaborn as snsimport pandas as pd# dataframe setupdata = {'A': [1, 2, 1], 'B': [3, 3, 7], 'C': [10, 1, 9], 'D': [4, 5, 3]}df = pd.DataFrame(data)# create a dataframe of the counts for each columncounts = df.apply(pd.value_counts)# display(count) A B C D1 2.0 NaN 1.0 NaN2 1.0 NaN NaN NaN3 NaN 2.0 NaN 1.04 NaN NaN NaN 1.05 NaN NaN NaN 1.07 NaN 1.0 NaN NaN9 NaN NaN 1.0 NaN10 NaN NaN 1.0 NaN# plotsns.heatmap(counts)选项2热图有许多可用的样式选项,并且更改颜色cmap可以改善可视化效果。Seaborn:调色板# countscounts = df.apply(pd.value_counts).fillna(0)# plotsns.heatmap(counts, cmap="GnBu", annot=True)默认颜色sns.heatmap(counts, annot=True)
-
猛跑小猪
这是我第一次发表答案,希望有希望import seaborn as snsimport pandas as pdimport numpy as npdata = {'A': [1, 2, 1], 'B': [3, 3, 7], 'C': [10, 1, 9], 'D': [4, 5, 3]}df = pd.DataFrame(data)df1 = pd.DataFrame(data = None , index = np.arange(11),columns = df.columns) for value in df.columns: df1[value]= df[value].value_counts() df1.fillna(0)
-
慕容708150
# necessary importsimport pandas as pdimport numpy as np从数据框开始:df = pd.DataFrame({'A': [1, 2, 1], 'B': [3, 3, 7], 'C': [10,1, 9], 'D': [4, 5, 3]}, index=[0, 1, 2])然后你可以这样做:d = pd.DataFrame(0, index=np.arange(10), columns=['A','B','C','D'])或者,更一般地说:d = pd.DataFrame(0, index=np.arange(10), columns=df.columns)d将具有您想要的数据帧结构,但具有所有值0。填写数据框:for col in df.columns: d[col]=df[col].value_counts()s0被NaNs 取代。再次制作它们0:d.replace(np.nan, 0, inplace=True)这会给你:+----+-----+-----+-----+-----+| | A | B | C | D ||----+-----+-----+-----+-----|| 0 | 0 | 0 | 0 | 0 || 1 | 2 | 0 | 1 | 0 || 2 | 1 | 0 | 0 | 0 || 3 | 0 | 2 | 0 | 1 || 4 | 0 | 0 | 0 | 1 || 5 | 0 | 0 | 0 | 1 || 6 | 0 | 0 | 0 | 0 || 7 | 0 | 1 | 0 | 0 || 8 | 0 | 0 | 0 | 0 || 9 | 0 | 0 | 1 | 0 |+----+-----+-----+-----+-----+