2

I have a data frame with the temperatures recorded per day/month/year. Then I find the lowest temperature from each month using groupby and min functions, which gives a data series with multiple index.

How can I drop a value from a specific year and month? eg. year 2005 month 12?

# Find the lowest value per each month
[In] low = df.groupby([df['Date'].dt.year,df['Date'].dt.month])['Data_Value'].min()

[In] low
[Out] 
Date  Date
2005  1       -60
      2      -114
      3      -153
      4       -13
      5       -14
      6        26
      7        83
      8        65
      9        21
      10       36
      11      -36
      12      -86
2006  1       -75
      2       -53
      3       -83
      4       -30
      5        36
      6        17
      7        85
      8        82
      9        66
      10       40
      11       -2
      12      -32
2007  1       -63
      2       -42
      3       -21
      4       -11
      5        28
      6        74
      7        73
      8        61
      9        46
      10      -33
      11      -37
      12      -97
[In] low.index
[Out] MultiIndex(levels=[[2005, 2006, 2007], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]],
           labels=[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]],
           names=['Date', 'Date']) 
Jinglebell
  • 55
  • 1
  • 1
  • 5
  • This [SO answer](https://stackoverflow.com/questions/22233488/pandas-drop-a-level-from-a-multi-level-column-index) may help – amanb May 06 '19 at 10:48

1 Answers1

0

This works.

#dummy data
mux = pd.MultiIndex.from_arrays([
    (2017,)*12 + (2018,)*12,
    list(range(1, 13))*2
], names=['year', 'month'])

df = pd.DataFrame({'value': np.random.randint(1, 20, (len(mux)))}, mux)

Then just use drop.

df.drop((2017, 12), inplace=True)

>>> print(df)

            value
year month       
2017 1         18
     2         13
     3         14
     4          1
     5          8
     6         19
     7         19
     8          8
     9         11
     10         5
     11         7 <<<
2018 1          9
     2         18
     3          9
     4         14
     5          7
     6          4
     7          6
     8         12
     9         12
     10         1
     11        19
     12        10
Chris
  • 1,287
  • 12
  • 31
  • Thanks!! It works! ```()``` is the trick, I tried so many times with ```df.drop([2017,12])``` in different ways... – Jinglebell May 07 '19 at 11:57