python - create a plot with all dates in pandas -
i need create plot of dataframe several columns. have dates on x-axis. how possible make on plot there dates? data shown period of once every 5 months. in columns, data small, important me visible on plot. dataframe looks this.
date col1 col2 col3 col4 col5 col6 20.05.2016 1091,06 12932,31 0 9343,334 23913,74 0 27.05.2016 1086,66 11845,64 0 9786,654 23913,74 0 03.06.2016 1083,04 10762,59 0 9786,654 23913,74 0 10.06.2016 1083,96 9678,630 4000 9786,654 23913,74 0 17.06.2016 1087,31 22718,40 0 9786,654 23913,74 1412 24.06.2016 1089,78 21628,62 0 9786,654 23828,96 0 01.07.2016 1083,70 20544,92 0 9749,567 23828,96 0 08.07.2016 1081,92 19463 0 9749,567 23828,96 0 ...
my code looks this:
df.plot(figsize=(20,10), x='date', y=['col1', 'col2', 'col3', 'col4', 'col5', 'col6']) plt.show()
i grateful advice.
first use set_index
, if need subset []
if need filter columns names:
cols = ['col1', 'col2', 'col3', 'col4', 'col5', 'col6'] df.set_index('date')[cols].plot(figsize=(20,10))
and columns of df
omit it:
df.set_index('date').plot(figsize=(20,10))
but if need columns without 0
use boolean indexing
loc
, filter columns ne
(!=
) , all
true
s per columns:
#replace decimals , . , floats, check notice solution df['date'] = pd.to_datetime(df['date']) df = df.set_index('date').replace(',', '.', regex=true).astype(float) print (df.ne(0)) col1 col2 col3 col4 col5 col6 date 2016-05-20 true true false true true false 2016-05-27 true true false true true false 2016-03-06 true true false true true false 2016-10-06 true true true true true false 2016-06-17 true true false true true true 2016-06-24 true true false true true false 2016-01-07 true true false true true false 2016-08-07 true true false true true false print (df.ne(0).all()) col1 true col2 true col3 false col4 true col5 true col6 false dtype: bool
df = df.loc[:, df.ne(0).all()] print (df) col1 col2 col4 col5 date 2016-05-20 1091.06 12932.31 9343.334 23913.74 2016-05-27 1086.66 11845.64 9786.654 23913.74 2016-03-06 1083.04 10762.59 9786.654 23913.74 2016-10-06 1083.96 9678.63 9786.654 23913.74 2016-06-17 1087.31 22718.40 9786.654 23913.74 2016-06-24 1089.78 21628.62 9786.654 23828.96 2016-01-07 1083.70 20544.92 9749.567 23828.96 2016-08-07 1081.92 19463.00 9749.567 23828.96 df.plot(figsize=(20,10))
notice:
there problem decimals, need parameter decimal
in read_csv
or replace
astype
used in solution above:
df = pd.read_csv('filename', index_col=['date'], decimal=',', parse_dates=['date']) print (df) col1 col2 col3 col4 col5 col6 date 2016-05-20 1091.06 12932.31 0 9343.334 23913.74 0 2016-05-27 1086.66 11845.64 0 9786.654 23913.74 0 2016-03-06 1083.04 10762.59 0 9786.654 23913.74 0 2016-10-06 1083.96 9678.63 4000 9786.654 23913.74 0 2016-06-17 1087.31 22718.40 0 9786.654 23913.74 1412 2016-06-24 1089.78 21628.62 0 9786.654 23828.96 0 2016-01-07 1083.70 20544.92 0 9749.567 23828.96 0 2016-08-07 1081.92 19463.00 0 9749.567 23828.96 0
Comments
Post a Comment