英文:
pie chart drawing for a specific column in pandas python
问题
I have a dataframe df, which has many columns. In df["house_electricity"], there are values like 1, 0 or blank/NA. I want to plot the column in terms of a pie chart, where the percentage of only 1 and 0 will be shown. Similarly, I want to plot another pie chart where the percentage of 1, 0, and blank/N.A all will be there.
| customer_id | house_electricity | house_refrigerator |
| -------- | -------- |
| cid01 | 0 | 0 |
| cid02 | 1 |na |
| cid03 | | 1 |
| cid04 |1 | |
| cid05 |na | 0 |
#I wrote the following but it didn't give me my expected result
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("my_file.csv")
df_col = df.columns
df["house_electricity"].plot(kind="pie")
#I wrote the following but it didn't give me my expected result
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("my_file.csv")
df_col = df.columns
df["house_electricity"].plot(kind="pie")
英文:
I have a dataframe df, which has many columns. In df["house_electricity"], there are values like 1,0 or blank/NA. I want to plot the column in terms of a pie chart, where percentage of only 1 and 0 will be shown. Similarly I want to plot another pie chart where percentage of 1,0 and blank/N.A all will be there.
| customer_id | house_electricity | house_refrigerator |
| -------- | -------- |
| cid01 | 0 | 0 |
| cid02 | 1 |na |
| cid03 | | 1 |
| cid04 |1 | |
| cid05 |na | 0 |
#I wrote the following but it didnt give my my expected result
import pandas as pd
import matplotlib.pyplot as plt
df=pd.read_csv("my_file.csv")
df_col=df.columns
df["house_electricity"].plot(kind="pie")
#I wrote the following but it didnt give my my expected result
import pandas as pd
import matplotlib.pyplot as plt
df=pd.read_csv("my_file.csv")
df_col=df.columns
df["house_electricity"].plot(kind="pie")
答案1
得分: 0
以下是代码的中文翻译部分:
# 创建一个数据框
df = pd.DataFrame({'a':[1,0,np.nan,1,1,1,'',0,0,np.nan]})
df
# 输出数据框
a
0 1
1 0
2 NaN
3 1
4 1
5 1
6
7 0
8 0
9 NaN
# 使用下面的代码绘制饼图
df["a"].value_counts(dropna=False).plot(kind="pie")
如果您想要将缺失值和空值组合在一起,可以尝试用np.nan
替换空值,然后绘制饼图:
# 用 np.nan 替换空值,然后绘制饼图
df["a"].replace("", np.nan).value_counts(dropna=False).plot(kind="pie")
这将生成第二张图示例。
英文:
For a dataframe
df = pd.DataFrame({'a':[1,0,np.nan,1,1,1,'',0,0,np.nan]})
df
a
0 1
1 0
2 NaN
3 1
4 1
5 1
6
7 0
8 0
9 NaN
The code below will give
df["a"].value_counts(dropna=False).plot(kind="pie")
If you want combine na and empty value, try replacing empty values with np.nan, then try to plot
df["a"].replace("", np.nan).value_counts(dropna=False).plot(kind="pie")
答案2
得分: 0
以下是翻译好的代码部分:
import pandas as pd
import matplotlib.pyplot as plt
data = {'customer_id': ['cid01', 'cid02', 'cid03', 'cid04', 'cid05'],
'house_electricity': [0, 1, None, 1, None],
'house_refrigerator': [0, None, 1, None, 0]}
df = pd.DataFrame(data)
counts = df['house_electricity'].value_counts(dropna=False)
counts.plot.pie(autopct='%1.1f%%', labels=['0', '1', 'NaN'], shadow=True)
plt.title('house_electricity列的百分比分布')
plt.axis('equal')
plt.show()
希望这对你有所帮助。
英文:
For solution you need to try with this code to generate 3 blocks.
import pandas as pd
import matplotlib.pyplot as plt
data = {'customer_id': ['cid01', 'cid02', 'cid03', 'cid04', 'cid05'],
'house_electricity': [0, 1, None, 1, None],
'house_refrigerator': [0, None, 1, None, 0]}
df = pd.DataFrame(data)
counts = df['house_electricity'].value_counts(dropna=False)
counts.plot.pie(autopct='%1.1f%%', labels=['0', '1', 'NaN'], shadow=True)
plt.title('Percentage distribution of house_electricity column')
plt.axis('equal')
plt.show()
Result:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论