Python - 当数据不是数字时如何从 csv 文件制作条形图

标签 python pandas matplotlib

我有一个 csv 文件,其中包含已导入数据框中的数据。 'RI_df = pd.read_csv("../Week15/police.csv")'

使用 .head() 我的数据如下所示:

state   stop_date   stop_time   county_name driver_gender   driver_race violation_raw   violation   search_conducted    search_type stop_outcome    is_arrested stop_duration   drugs_related_stop  district
0   RI  2005-01-04  12:55   NaN M   White   Equipment/Inspection Violation  Equipment   False   NaN Citation    False   0-15 Min    False   Zone X4
1   RI  2005-01-23  23:15   NaN M   White   Speeding    Speeding    False   NaN Citation    False   0-15 Min    False   Zone K3
2   RI  2005-02-17  04:15   NaN M   White   Speeding    Speeding    False   NaN Citation    False   0-15 Min    False   Zone X4
3   RI  2005-02-20  17:15   NaN M   White   Call for Service    Other   False   NaN Arrest Driver   

RI_df.head().to_dict()

Out[55]:
{'state': {0: 'RI', 1: 'RI', 2: 'RI', 3: 'RI', 4: 'RI'},
 'stop_date': {0: '2005-01-04',
  1: '2005-01-23',
  2: '2005-02-17',
  3: '2005-02-20',
  4: '2005-02-24'},
 'stop_time': {0: '12:55', 1: '23:15', 2: '04:15', 3: '17:15', 4: '01:20'},
 'county_name': {0: nan, 1: nan, 2: nan, 3: nan, 4: nan},
 'driver_gender': {0: 'M', 1: 'M', 2: 'M', 3: 'M', 4: 'F'},
 'driver_race': {0: 'White', 1: 'White', 2: 'White', 3: 'White', 4: 'White'},
 'violation_raw': {0: 'Equipment/Inspection Violation',
  1: 'Speeding',
  2: 'Speeding',
  3: 'Call for Service',
  4: 'Speeding'},
 'violation': {0: 'Equipment',
  1: 'Speeding',
  2: 'Speeding',
  3: 'Other',
  4: 'Speeding'},
 'search_conducted': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0},
 'search_type': {0: nan, 1: nan, 2: nan, 3: nan, 4: nan},
 'stop_outcome': {0: 'Citation',
  1: 'Citation',
  2: 'Citation',
  3: 'Arrest Driver',
  4: 'Citation'},
 'is_arrested': {0: False, 1: False, 2: False, 3: True, 4: False},
 'stop_duration': {0: '0-15 Min',
  1: '0-15 Min',
  2: '0-15 Min',
  3: '16-30 Min',
  4: '0-15 Min'},
 'drugs_related_stop': {0: False, 1: False, 2: False, 3: False, 4: False},
 'district': {0: 'Zone X4',
  1: 'Zone K3',
  2: 'Zone X4',
  3: 'Zone X1',
  4: 'Zone X3'}}

我正在尝试按 driver_gender 分割交通违规行为

ax = RI_df['violation'].value_counts().plot(kind='bar',
                                    figsize=(10,8),
                                    title="Offenses by gender")
ax.set_xlabel("Violation")
ax.set_ylabel("Freq")

我有这个,但是这不会按性别将其分开。我想我需要在某个地方有一个子图,但我在创建它时遇到了麻烦。我应该为男性和女性制作一个较小的数据框并只制作两个图表吗?

最佳答案

你可以尝试:

ax = (RI_df.groupby('driver_gender')
           .violation.value_counts()
           .unstack('violation', fill_value=0)
           .plot.bar(figsize=(10,8), title='violation by gender' )
     )

输出(随机数据):

enter image description here

或者:

ax = (RI_df.groupby('violation')
           .driver_gender.value_counts()
           .unstack('driver_gender', fill_value=0)
           .plot.bar(figsize=(10,8), title='violation by gender' )
     )

输出:

enter image description here

关于Python - 当数据不是数字时如何从 csv 文件制作条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59205080/

相关文章:

python - 如何使用 Python 显示值构建从正百分比到负百分比的水平堆积条形图

python - 将列表值中的列表替换为字典值

python - 使用Lambda函数进行分组和聚合

python - 没有标签的python中的图例

python - 如何使用 matplotlib 绘制 .txt 文件中的数据

pandas - 尝试基于多索引合并 pandas 中的 2 个数据帧

python - 在运行时从 python Literal 类型中获取文字?

python - 如何将与索引并行创建的多个图像通过管道传输到 ffmpeg,以便它可以匹配图像创建的速度?

python - 在Python中取两个字符串列表作为输入

python - 将 python 与 suds 用于共享点时出现错误请求