小编Man*_*lai的帖子

如何使用 matplotlib 中的分类数据创建饼图?

我有数据如下:

ID  Gender  Country  ...
1   Male    UK
2   Female  US
3   Male    NZ
4   Female  UK
...
Run Code Online (Sandbox Code Playgroud)

性别只有 2 个选项,国家只有 3 个选项。我想为“性别”和“国家”创建一个单独的饼图,以显示每个选项在数据中出现的次数,但我对如何做到这一点感到非常困惑。

数据存储在 pandas 数据框中。

非常感谢任何和所有的帮助!

python matplotlib pandas pie-chart

9
推荐指数
2
解决办法
3万
查看次数

如何使用 str.replace 一次替换多个对?

目前我正在使用以下代码进行替换,这有点麻烦:

df1['CompanyA'] = df1['CompanyA'].str.replace('.','')
df1['CompanyA'] = df1['CompanyA'].str.replace('-','')
df1['CompanyA'] = df1['CompanyA'].str.replace(',','')
df1['CompanyA'] = df1['CompanyA'].str.replace('ltd','limited')
df1['CompanyA'] = df1['CompanyA'].str.replace('&','and')
df1['Address1A'] = df1['Address1A'].str.replace('.','')
df1['Address1A'] = df1['Address1A'].str.replace('-','')
df1['Address1A'] = df1['Address1A'].str.replace('&','and')
df1['Address1A'].str.replace(r'\brd\b', 'road')
df1['Address2A'] = df1['Address2A'].str.replace('.','')
df1['Address2A'] = df1['Address2A'].str.replace('-','')
df1['Address2A'] = df1['Address2A'].str.replace('&','and')
df1['Address2A'].str.replace(r'\brd\b', 'road')
Run Code Online (Sandbox Code Playgroud)

为了使即时更改更容易,我的理想情况是这样的:

df1['CompanyA'] = df1['CompanyA'].str.replace(('&','and'), ('.', ''), ('-','')....)
df1['Address1A'] = df1['Address1A'].str.replace(('&','and'), ('.', ''), ('-','')....)
df1['Address2A'] = df1['Address2A'].str.replace(('&','and'), ('.', ''), ('-','')....)
Run Code Online (Sandbox Code Playgroud)

这样我就可以输入/更改我想为特定列替换的内容,而无需调整多行代码。

这可能吗?

python string replace pandas

2
推荐指数
1
解决办法
782
查看次数

标签 统计

pandas ×2

python ×2

matplotlib ×1

pie-chart ×1

replace ×1

string ×1