Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Plotting some data in a weird format in excel/python

I have some data that is in this format:

Position 2012 2013 2014
1 A B A
2 C A C
3 B D E

Is there a way to plot this data? The way it is set up is that the table is sorta like a line chart, I know its weird but that’s the way the data came out as.

If not excel, then maybe in python?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I don’t know how to plot this data. It is already in a kind of format as if it is a graph though, the cells are like a point of data on a line graph.

>Solution :

You can use pandas and matplotlib to pivot your dataframe and plot your figure:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

# Read your data
# df = pd.read_excel('data.xlsx')
data = {'Position': [1, 2, 3],
        '2012': ['A', 'C', 'B'],
        '2013': ['B', 'A', 'D'],
        '2014': ['A', 'C', 'E']}
df = pd.DataFrame(data)

# Reshape your table
piv = (df.melt('Position', var_name='Year', value_name='ID')
         .pivot(index='Year', columns='ID', values='Position'))

# Plot data
ax = piv.plot(xlabel='Year', ylabel='Position', title='Rank by year')
ax.yaxis.set_major_locator(ticker.MultipleLocator())
ax.invert_yaxis()
plt.show()

Output:

enter image description here

Pivoted dataframe:

>>> piv
ID      A    B    C    D    E
Year                         
2012  1.0  3.0  2.0  NaN  NaN
2013  2.0  1.0  NaN  3.0  NaN
2014  1.0  NaN  2.0  NaN  3.0

Note: you probably do the same with Excel.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading