2

In the table below, I have values and frequencies. I'd like to draw a box-plot using Jupyter Notebook. I googled it but not able to find any answers.

My idea is to create a column, 2,2,2,2,4,4,4,4,4,4,4,... But I think there must be a better way.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

value=np.array([2,4,6,7,10])
freq=np.array([4,7,8,5,2])

# do something here

plt.boxplot(newdata)
plt.show()

enter image description here

shin
  • 31,901
  • 69
  • 184
  • 271

1 Answers1

4

use numpy's repeat:

newdata = np.repeat(value,freq)
Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
  • Wow, I did not know about repeat. This is cool. Thanks. Does Pandas have similar method? – shin Jan 28 '20 at 11:49
  • Sure, [Series.repeat()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.repeat.html). In your case, newdata = df['value'].repeat(df['freq']) – Diziet Asahi Jan 28 '20 at 12:13