在pandas中绘制特定列的饼图。

huangapple go评论104阅读模式
英文:

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 |

  1. #I wrote the following but it didn't give me my expected result
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. df = pd.read_csv("my_file.csv")
  5. df_col = df.columns
  6. df["house_electricity"].plot(kind="pie")
  1. #I wrote the following but it didn't give me my expected result
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. df = pd.read_csv("my_file.csv")
  5. df_col = df.columns
  6. 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 |

  1. #I wrote the following but it didnt give my my expected result
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. df=pd.read_csv("my_file.csv")
  5. df_col=df.columns
  6. df["house_electricity"].plot(kind="pie")
  1. #I wrote the following but it didnt give my my expected result
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. df=pd.read_csv("my_file.csv")
  5. df_col=df.columns
  6. df["house_electricity"].plot(kind="pie")

答案1

得分: 0

以下是代码的中文翻译部分:

  1. # 创建一个数据框
  2. df = pd.DataFrame({'a':[1,0,np.nan,1,1,1,'',0,0,np.nan]})
  3. df
  4. # 输出数据框
  5. a
  6. 0 1
  7. 1 0
  8. 2 NaN
  9. 3 1
  10. 4 1
  11. 5 1
  12. 6
  13. 7 0
  14. 8 0
  15. 9 NaN
  16. # 使用下面的代码绘制饼图
  17. df["a"].value_counts(dropna=False).plot(kind="pie")

如果您想要将缺失值和空值组合在一起,可以尝试用np.nan替换空值,然后绘制饼图:

  1. # 用 np.nan 替换空值,然后绘制饼图
  2. df["a"].replace("", np.nan).value_counts(dropna=False).plot(kind="pie")

这将生成第二张图示例。

英文:

For a dataframe

  1. df = pd.DataFrame({'a':[1,0,np.nan,1,1,1,'',0,0,np.nan]})
  2. df
  3. a
  4. 0 1
  5. 1 0
  6. 2 NaN
  7. 3 1
  8. 4 1
  9. 5 1
  10. 6
  11. 7 0
  12. 8 0
  13. 9 NaN

The code below will give

  1. df["a"].value_counts(dropna=False).plot(kind="pie")

在pandas中绘制特定列的饼图。

If you want combine na and empty value, try replacing empty values with np.nan, then try to plot

  1. df["a"].replace("", np.nan).value_counts(dropna=False).plot(kind="pie")

在pandas中绘制特定列的饼图。

答案2

得分: 0

以下是翻译好的代码部分:

  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. data = {'customer_id': ['cid01', 'cid02', 'cid03', 'cid04', 'cid05'],
  4. 'house_electricity': [0, 1, None, 1, None],
  5. 'house_refrigerator': [0, None, 1, None, 0]}
  6. df = pd.DataFrame(data)
  7. counts = df['house_electricity'].value_counts(dropna=False)
  8. counts.plot.pie(autopct='%1.1f%%', labels=['0', '1', 'NaN'], shadow=True)
  9. plt.title('house_electricity列的百分比分布')
  10. plt.axis('equal')
  11. plt.show()

希望这对你有所帮助。

英文:

For solution you need to try with this code to generate 3 blocks.

  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. data = {'customer_id': ['cid01', 'cid02', 'cid03', 'cid04', 'cid05'],
  4. 'house_electricity': [0, 1, None, 1, None],
  5. 'house_refrigerator': [0, None, 1, None, 0]}
  6. df = pd.DataFrame(data)
  7. counts = df['house_electricity'].value_counts(dropna=False)
  8. counts.plot.pie(autopct='%1.1f%%', labels=['0', '1', 'NaN'], shadow=True)
  9. plt.title('Percentage distribution of house_electricity column')
  10. plt.axis('equal')
  11. plt.show()

Result:

在pandas中绘制特定列的饼图。

huangapple
  • 本文由 发表于 2023年2月8日 13:39:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定