3 回答

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
用途
pandas.Series.value_counts
適用于每一列seaborn.heatmap
將繪制一個(gè)DataFrame
如果提供了 Pandas DataFrame,索引/列信息將用于標(biāo)記列和行。
選項(xiàng)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)
選項(xiàng)2
熱圖有許多可用的樣式選項(xiàng),并且更改顏色
cmap
可以改善可視化效果。Seaborn:調(diào)色板
# counts
counts = df.apply(pd.value_counts).fillna(0)
# plot
sns.heatmap(counts, cmap="GnBu", annot=True)
默認(rèn)顏色
sns.heatmap(counts,?annot=True)

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊
這是我第一次發(fā)表答案,希望有希望
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貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊
# necessary imports
import pandas as pd
import numpy as np
從數(shù)據(jù)框開始:
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將具有您想要的數(shù)據(jù)幀結(jié)構(gòu),但具有所有值0。
填寫數(shù)據(jù)框:
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 |
+----+-----+-----+-----+-----+
添加回答
舉報(bào)