0

I am working with a csv file with pandas module on python3. Csv file consists of 5 columns: job, company's name, description of the job, amount of reviews, location of the job; and i want to plot a frequency histogram , where i pick only the jobs containing the words "mechanical engineer" and find the frequencies of the 5 most frequent locations for the "mechanical engineer" job.

So,i defined a variable engloc which stores all the "mechanical engineer" jobs.

engloc=df[df.position.str.contains('mechanical engineer|mechanical engineering', flags=re.IGNORECASE, regex=True)].location

and did a histogram plot with matplotlib with code i found online

 x = np.random.normal(size = 1000)
 plt.hist(engloc, bins=50)
 plt.gca().set(title='Frequency Histogram ', ylabel='Frequency');

but it printed like this

enter image description here

How can i plot a proper frequency histogram where it plots using only 5 of the most frequent locations for jobs containing "mechanical engineer" words, instead of putting all of the locations in the graph?

This is a sample from the csv file csv data screenshot

4
  • If you share your data somebody may be willing to help you. See minimal reproducible example for a complete explanation. Commented Feb 5, 2020 at 18:52
  • Should i include a screenshot of some part or write in the question a few parameters of the data?
    – Ayano
    Commented Feb 5, 2020 at 18:59
  • A "representative" sample of data, allowing to plot a meaningful histogram, entered as text should do. Link to the full data even better. Commented Feb 5, 2020 at 19:01
  • Post your data as text, not as a screenshot Commented Feb 5, 2020 at 19:57

1 Answer 1

1

Something along the following lines should help you with numerical data:

import numpy as np
counts_, bins_ = np.histogram(englog.values)
filtered = [(c,b) for (c,b) in zip(counts_,bins_) if counts_>=5]
counts, bins = list(zip(*filtered))
plt.hist(bins[:-1], bins, weights=counts)

For a string type try:

from collections import Counter 
coords, counts = list(zip(*Counter(englog.values).most_common(5)))
plt.bar(coords, counts)
3
  • It gave an error like this "TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''.
    – Ayano
    Commented Feb 5, 2020 at 20:02
  • Sadly the second one gave an error too.TypeError: list() takes at most 1 argument (5 given)
    – Ayano
    Commented Feb 5, 2020 at 21:12
  • @Ayano Does this solve your problem? If so you may think about accepting the answer Commented Feb 7, 2020 at 9:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.