How To Order Dataframe For Plotting 3d Bar In Pandas
I am trying to create a chart with multiple bars in 3d from pandas. Reviewing some examples on the web, I see that the best way to accomplish this is to get a dataframe like this:
Solution 1:
Here is the code to do count:
importpandasaspdtext="""Date_inicio,Date_Fin,Date_Max,Clase2004-04-09 23:00:00,2004-04-10 04:00:00,2004-04-10 02:00:00,MBCCM2004-04-12 23:00:00,2004-04-13 04:00:00,2004-04-13 00:00:00,MBSCL2004-04-24 04:00:00,2004-04-24 12:00:00,2004-04-24 09:00:00,SCL2004-05-02 07:00:00,2004-05-02 14:00:00,2004-05-02 11:00:00,SCL2004-05-30 05:00:00,2004-05-30 08:00:00,2004-05-30 07:00:00,MBCCM2004-05-31 03:00:00,2004-05-31 07:00:00,2004-05-31 05:00:00,MBCCM2004-06-08 00:00:00,2004-06-08 05:00:00,2004-06-08 03:00:00,MBSCL2004-06-12 22:00:00,2004-06-13 12:00:00,2004-06-13 06:00:00,CCM2004-06-13 03:00:00,2004-06-13 08:00:00,2004-06-13 06:00:00,MBCCM2004-06-14 00:00:00,2004-06-14 03:00:00,2004-06-14 02:00:00,MBSCL2004-06-14 03:00:00,2004-06-14 09:00:00,2004-06-14 07:00:00,MBSCL2004-06-17 08:00:00,2004-06-17 14:00:00,2004-06-17 11:00:00,MBCCM2004-06-17 12:00:00,2004-06-17 17:00:00,2004-06-17 14:00:00,MBCCM2004-06-22 00:00:00,2004-06-22 08:00:00,2004-06-22 06:00:00,SCL2004-06-22 08:00:00,2004-06-22 14:00:00,2004-06-22 11:00:00,MBCCM2004-06-22 23:00:00,2004-06-23 09:00:00,2004-06-23 06:00:00,CCM2004-07-01 05:00:00,2004-07-01 09:00:00,2004-07-01 06:00:00,MBCCM2004-07-02 00:00:00,2004-07-02 04:00:00,2004-07-02 02:00:00,MBSCL2004-07-04 12:00:00,2004-07-04 15:00:00,2004-07-04 13:00:00,MBCCM2004-07-06 04:00:00,2004-07-06 13:00:00,2004-07-06 07:00:00,SCL2004-07-07 04:00:00,2004-07-07 12:00:00,2004-07-07 10:00:00,CCM2004-07-08 03:00:00,2004-07-08 06:00:00,2004-07-08 05:00:00,MBCCM2004-07-08 12:00:00,2004-07-08 17:00:00,2004-07-08 13:00:00,MBCCM2004-07-08 02:00:00,2004-07-08 06:00:00,2004-07-08 04:00:00,MBCCM2004-07-09 05:00:00,2004-07-09 12:00:00,2004-07-09 08:00:00,CCM2004-07-11 18:00:00,2004-07-12 12:00:00,2004-07-11 21:00:00,MBSCL2004-07-11 23:00:00,2004-07-12 05:00:00,2004-07-12 02:00:00,MBSCL2004-07-15 11:00:00,2004-07-15 19:00:00,2004-07-15 12:00:00,CCM2004-07-16 12:00:00,2004-07-16 16:00:00,2004-07-16 14:00:00,MBCCM2004-07-17 02:00:00,2004-07-17 06:00:00,2004-07-17 05:00:00,MBCCM"""importiodf=pd.read_csv(io.BytesIO(text),skipinitialspace=True)df.drop(["Clase"],axis=1,inplace=True)df=df.apply(lambdas:s.str[11:13]).convert_objects(convert_numeric=True)df2=df.apply(lambdas:s.value_counts())printdf2
Here is the code that draw 3d bars:
import pandas as pd
text="""Horas Frec_Inicio Frec_Max Frec_Fin
1 2 0 1
2 3 8 1
3 5 3 2
4 6 2 6
5 6 6 5
6 5 6 4
7 5 7 2
8 2 4 5
9 1 6 6
10 0 3 2
11 2 5 5
12 4 1 9
13 2 4 2
14 3 2 4
15 0 2 3
16 1 1 3
17 0 2 3
18 1 1 1
19 0 0 3
20 1 1 1
21 1 1 0
22 3 1 0
23 9 1 0
24 8 3 2"""import io
df = pd.read_csv(io.BytesIO(text), skipinitialspace=True, delim_whitespace=True)
df.set_index("Horas", inplace=True)
columns_name = [x.replace("_", " ") for x in df.columns]
df.columns = [0, 2, 4]
x, y, z = df.stack().reset_index().values.T
import visvis as vv
app = vv.use()
f = vv.clf()
a = vv.cla()
bar =vv.bar3(x, y, z, width=0.8)
bar.colors = ["r","g","b"] * 24
a.axis.yTicks = dict(zip(df.columns, columns_name))
app.Run()
the output:
Post a Comment for "How To Order Dataframe For Plotting 3d Bar In Pandas"