3 回答
TA贡献1804条经验 获得超8个赞
用途
pandas.Series.value_counts
适用于每一列seaborn.heatmap
将绘制一个DataFrame
如果提供了 Pandas DataFrame,索引/列信息将用于标记列和行。
选项1
import seaborn as sns
import pandas as pd
# dataframe setup
data = {'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 column
counts = df.apply(pd.value_counts)
# display(count)
A B C D
1 2.0 NaN 1.0 NaN
2 1.0 NaN NaN NaN
3 NaN 2.0 NaN 1.0
4 NaN NaN NaN 1.0
5 NaN NaN NaN 1.0
7 NaN 1.0 NaN NaN
9 NaN NaN 1.0 NaN
10 NaN NaN 1.0 NaN
# plot
sns.heatmap(counts)
选项2
热图有许多可用的样式选项,并且更改颜色
cmap
可以改善可视化效果。Seaborn:调色板
# counts
counts = df.apply(pd.value_counts).fillna(0)
# plot
sns.heatmap(counts, cmap="GnBu", annot=True)
默认颜色
sns.heatmap(counts, annot=True)
TA贡献1858条经验 获得超8个赞
这是我第一次发表答案,希望有希望
import seaborn as sns
import pandas as pd
import numpy as np
data = {'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)
TA贡献1831条经验 获得超4个赞
# necessary imports
import pandas as pd
import 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 |
+----+-----+-----+-----+-----+
添加回答
举报