My data is given as 0.1, 0.2, 0.2, 0.2, 0.4, and 0.3. My goal is to draw dot plot using matplotlib scaterplot by plottting these points which is the solution: (0.1,1) (0.2,1) (0.2,2) (0.2,3) (0.4,1) (0.3,1). Is there a neat way to obtain these points above? ※The 0.2 appears three time in my data, therefore (0.2,1) (0.2,2) and (0.2,3) in the solution. I've tried Counter class as below, because it counts occurence, it shows only (0.1,1) (0.2,3) (0.4,1) (0.3,1) which is the subset of the solution.
x = pd.DataFrame({"X":[0.1, 0.2, 0.2, 0.2, 0.4, 0.3]}).X
counter=Counter(x) # How to get {0.1: 1,0.2: 1, 0.2: 2, 0.2: 3, 0.4: 1, 0.3: 1} instead of {0.1: 1, 0.2: 3, 0.4: 1, 0.3: 1}
df=pd.DataFrame.from_dict(counter, orient='index').reset_index().rename(columns={'index':'event', 0:'count'})
plt.scatter(x=df['event'], y=df['count'],alpha=0.3) # This dot plot is not complete. Because there are no points (0.2,1) and (0.2,2)
CodePudding user response:
Simply the list appending
lstevent=[];lstcount=[];
type(lstevent)
for row in df.itertuples():
# print(row[1],row[2])
# lstevent.append(row[1]);lstcount.append(row[2]);
for k in range(row[2]):
lstevent.append(row[1]);lstcount.append(k 1);
# print(lstevent,lstcount)
df_for_dot=pd.DataFrame({'event': lstevent,'count':lstcount})
df_for_dot