Plot made of array from a pandas dataset in Python

Question:

The problem is: I have a SQLAlchemy database called NumFav with arrays of favourite numbers of some people, which uses such a structure:

id      name       numbers
 0 Vladislav     [2, 3, 5]
 1   Michael  [4, 6, 7, 9]

numbers is postgresql.ARRAY(Integer)

I want to make a plot which demonstrates id of people on X and numbers dots on Y in order to show which numbers have been chosen like this:

reference figure

I extract data using

df = pd.read_sql(Session.query(NumFav).statement, engine)

How can I create a plot with such data?

Asked By: Vladislav Railes

||

Answers:

Like this:

import matplotlib.pyplot as plt 
for idx, row in df.iterrows():
    plt.plot(row['numbers'])
plt.legend(df['name'])
plt.show()

enter image description here

Answered By: Tao-Lung Huang

You can explode the number lists into "long form":

df = df.explode('numbers')
df['color'] = df.id.map({0: 'red', 1: 'blue'})

# id  name       numbers  color
# 0   Vladislav  2        red
# 0   Vladislav  3        red
# 0   Vladislav  5        red
# 1   Michael    4        blue
# 1   Michael    6        blue
# 1   Michael    7        blue
# 1   Michael    9        blue

Then you can directly plot.scatter:

df.plot.scatter(x='name', y='numbers', c='color')

numbers vs name scatter plot

Answered By: tdy
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.