0

I have a pandas series with this type of format:

    date
2017-03-15     1236.43
2017-03-16     1118.96
2017-03-17     1063.48
2017-03-18      940.18
2017-03-19      967.31
2017-03-20     1005.05
2017-03-21     1043.87
2017-03-22      997.78
2017-03-23     1022.02
2017-03-24      927.35
2017-03-25      890.43
2017-03-26      946.65
2017-03-27      961.80
2017-03-28     1015.45
               ...  
2018-03-06    10589.28
2018-03-07     9470.73
2018-03-08     9534.02
Name: BTC, Length: 1382, dtype: float64

I can't find a good way to split this by month, I have already tried with groupby and it gave me a pretty good output but it also assembles datas from different years and that's a problem

IN[]: dflist_BTC = []
for group in data.BTC.groupby(df.index.month):
    dflist_BTC.append(group[1])

print(dflist_BTC)

OUT[]: [date
2018-01-01    12877.67
2018-01-02    12934.16
2018-01-03    14579.71
2018-01-04    14244.67
              ...
2018-01-28    11407.94
2018-01-29    11089.52
2018-01-30     9871.21
2018-01-31     9698.13
Name: BTC, dtype: float64, date
2018-02-01     8726.95
2018-02-02     7786.20
2018-02-03     8194.68
               ...
2018-02-27    10154.24
2018-02-28    10303.14
Name: BTC, dtype: float64, date
2017-03-15     1236.43
2017-03-16     1118.96
2017-03-17     1063.48
2017-03-18      940.18
2017-03-19      967.31
2017-03-20     1005.05
2017-03-21     1043.87
2017-03-22      997.78
2017-03-23     1022.02
2017-03-24      927.35
2017-03-25      890.43
2017-03-26      946.65
2017-03-27      961.80
2017-03-28     1015.45
2017-03-29     1008.34
2017-03-30     1020.93
2017-03-31     1035.18
#Here there is the problem, it combines 2017 and 2018
2018-03-01    10247.56
2018-03-02    10801.45
2018-03-03    11043.12
2018-03-04    11084.01
2018-03-05    11431.55
2018-03-06    10589.28
2018-03-07     9470.73
2018-03-08     9534.02
Name: BTC, dtype: float64, date
2017-04-01    1067.47
2017-04-02    1074.21
              ...
2017-12-30    11962.09
2017-12-31    12359.43
Name: BTC, dtype: float64]

I'm new in Stackoverflow and in coding in general, so I'm sorry if I didn't explain myself in a better way. I'll be grateful if you can help me

Jerry Fanelli
  • 81
  • 1
  • 2
  • 6

1 Answers1

0

I think if need separated DataFrames is possible convert groupby object by YYYY-MM to dict of DataFrames:

dfs = dict(tuple(data.BTC.groupby(data.index.strftime('%Y-%m'))))
print (dfs['2017-03'])
2017-03-15    1236.43
2017-03-16    1118.96
2017-03-17    1063.48
2017-03-18     940.18
2017-03-19     967.31
2017-03-20    1005.05
2017-03-21    1043.87
2017-03-22     997.78
2017-03-23    1022.02
2017-03-24     927.35
2017-03-25     890.43
2017-03-26     946.65
2017-03-27     961.80
2017-03-28    1015.45
Name: BTC, dtype: float64
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252