Python - How to make a bar chart from csv file when the data is not numeric

pstaeubs

I have a csv file with data that I have imported into a dataframe. 'RI_df = pd.read_csv("../Week15/police.csv")'

Using .head() my data looks like this:

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'}}

I am trying to break down the traffic violations by the 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")

I have this however this does not break it apart by gender. I think I need a subplot somewhere but I'm having trouble creating it. Should I make a smaller dataframe, for males and females and just make two graphs?

Quang Hoang

You can try:

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

Output (random data):

enter image description here

Or:

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

output:

enter image description here

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Highcharts not displaying chart from csv file

분류에서Dev

Python - how to make CSV data samples without creating files?

분류에서Dev

Calling data from a csv file with its header Python

분류에서Dev

Flot Bar Chart Data Conversion

분류에서Dev

How can I read from a CSV file into 2 ArrayLists depending on the data type I have got in the File?

분류에서Dev

Add data to rows in csv file python

분류에서Dev

how to read data from different cells of Excel (csv) file in C++..?

분류에서Dev

Python read a file and make a nth list from the

분류에서Dev

Dimple.js - Add data labels to each bar of the bar chart

분류에서Dev

How to split a csv file on date using python

분류에서Dev

R ggplot How to plot a bar chart with different colours inside depicting 3 differents columns from the dataframe?

분류에서Dev

cassandra skip columns on copy data from csv file

분류에서Dev

Retrive data from MongoDB using mongoose and generate CSV/XLSX file

분류에서Dev

How to create a bar chart/histogram with bar per discrete value?

분류에서Dev

when unloading a table from amazon redshift to s3, how do I make it generate only one file

분류에서Dev

Import data from a txt file via python

분류에서Dev

Load rectangle data from txt file with Python?

분류에서Dev

Load rectangle data from txt file with Python?

분류에서Dev

Error converting data type from varchar to numeric

분류에서Dev

Obtain and store adjacent values from a .csv file (Python)

분류에서Dev

Python unpack binary data, numeric of length 12

분류에서Dev

How to get data from JSON file?

분류에서Dev

Data Grouping hiding data from Chart?

분류에서Dev

How to find all numeric columns in data

분류에서Dev

How to check username and password from a CSV. file

분류에서Dev

How to save the scraped results into a CSV file from multiple website pages?

분류에서Dev

How to fix character encoding with é read from csv file

분류에서Dev

How to update big table from csv file/another table in Oracle

분류에서Dev

How to filter range of date from a particular column of a csv file?

Related 관련 기사

  1. 1

    Highcharts not displaying chart from csv file

  2. 2

    Python - how to make CSV data samples without creating files?

  3. 3

    Calling data from a csv file with its header Python

  4. 4

    Flot Bar Chart Data Conversion

  5. 5

    How can I read from a CSV file into 2 ArrayLists depending on the data type I have got in the File?

  6. 6

    Add data to rows in csv file python

  7. 7

    how to read data from different cells of Excel (csv) file in C++..?

  8. 8

    Python read a file and make a nth list from the

  9. 9

    Dimple.js - Add data labels to each bar of the bar chart

  10. 10

    How to split a csv file on date using python

  11. 11

    R ggplot How to plot a bar chart with different colours inside depicting 3 differents columns from the dataframe?

  12. 12

    cassandra skip columns on copy data from csv file

  13. 13

    Retrive data from MongoDB using mongoose and generate CSV/XLSX file

  14. 14

    How to create a bar chart/histogram with bar per discrete value?

  15. 15

    when unloading a table from amazon redshift to s3, how do I make it generate only one file

  16. 16

    Import data from a txt file via python

  17. 17

    Load rectangle data from txt file with Python?

  18. 18

    Load rectangle data from txt file with Python?

  19. 19

    Error converting data type from varchar to numeric

  20. 20

    Obtain and store adjacent values from a .csv file (Python)

  21. 21

    Python unpack binary data, numeric of length 12

  22. 22

    How to get data from JSON file?

  23. 23

    Data Grouping hiding data from Chart?

  24. 24

    How to find all numeric columns in data

  25. 25

    How to check username and password from a CSV. file

  26. 26

    How to save the scraped results into a CSV file from multiple website pages?

  27. 27

    How to fix character encoding with é read from csv file

  28. 28

    How to update big table from csv file/another table in Oracle

  29. 29

    How to filter range of date from a particular column of a csv file?

뜨겁다태그

보관