I’m running a simulation which gives me a csv file corresponding in structure to the following pandas data frame:
df = DataFrame({'series': {0: 'A', 1: 'B', 2: 'C', 3: 'A', 4: 'B', 5: 'C', 6: 'A', 7: 'B', 8: 'C', 9: 'A', 10: 'B', 11: 'C', 12: 'A', 13: 'B', 14: 'C'}, 'step': {0: '0', 1: '0', 2: '0', 3: '1', 4: '1', 5: '1', 6: '2', 7: '2', 8: '2', 9: '3', 10: '3', 11: '3', 12: '4', 13: '4', 14: '4'}, 'value': {0: '0', 1: '0', 2: '5', 3: '1', 4: '0', 5: '4', 6: '2', 7: '1', 8: '3', 9: '3', 10: '2', 11: '2', 12: '4', 13: '4', 14: '1'}})
step value series
0 0 0 A
1 0 0 B
2 0 5 C
3 1 1 A
4 1 0 B
5 1 4 C
6 2 2 A
7 2 1 B
8 2 3 C
9 3 3 A
10 3 2 B
11 3 2 C
12 4 4 A
13 4 4 B
14 4 1 C
Given this df, how can I plot the (step, value) pairs for each series?
That is, I would like to get the image

which would be easy to get (just df2.plot("step") if I could get my DataFrame into the format df2
A B C step
0 0 0 5 0
1 1 0 4 1
2 2 1 3 2
3 3 2 2 3
4 4 4 1 4
but I don’t see how I would do the transformation df→df2 or obtain the plot from df (which is what I get from my simulation) in any other way.
I could change my simulation code to write the corresponding data into in my case 16 separate table columns instead of one series and one value column, but that 16 is the value for most simulations, some use not all of those series and in the future I may need to split those further, so this does not look like the best solution to me.
To generate both example DataFrames, I went in the other direction and did
df2 = pandas.DataFrame({"step":xrange(5),"A":xrange(5),"B":[i*(i+1)/5 for i in xrange(5)],"C":xrange(5,0,-1)})
dfA = df2[["step","A"]].rename(columns={"A":"value"})
dfA["series"] = "A"
dfB = df2[["step","B"]].rename(columns={"B":"value"})
dfB["series"] ="B"
dfC = df2[["step","C"]].rename(columns={"C":"value"})
dfC["series"] ="C"
df = dfA.append(dfB).append(dfC).sort("step")
df.index = xrange(15)
Here’s one way to obtain the DataFrame in which you can plot more easily:
If you set the index name to
'step'you can plot as desired:Which gives the desired figure.