0

I'm trying to use Pandas to filter the dataframe. So in the dataset I have 1982-01 to 2019-11. I want to filter data based on year 2010 onwards ie. 2010-01 to 2019-11.

mydf = pd.read_csv('temperature_mean.csv')
df1 = mydf.set_index('month')
df= df1.loc['2010-01':'2019-11']

Example of Dataset

I had set the index = month, and I'm able to get the mean temperature for the filtered data. However I need to get the indexes as my x label for line graph. I'm not able to do it. I tried to use reg to get data from 201x onwards, but there's still an error.

How do I get the label for the months i.e. 2010-01, 2010-02......2019-10, 2019-11, for the line graph.

Thanks!

user12550148
  • 53
  • 1
  • 5
  • I'm not sure what your desired result is. If you need the month column in your dataframe df, then you can call reset_index() on df – Damian Vu Jan 18 '20 at 07:02
  • I want to get the filtered months dataframe e.g. 2010-01, 2010-02, 2010-03......2019-10, 2019-11 etc. (Just the month and the year) – user12550148 Jan 18 '20 at 07:05
  • Can you tell me what you are using to plot your line graph? Or edit in your post exactly what the error is? If the issue is that you need two columns to plot your line graph, then I would change your call to df= df1.loc['2010-01':'2019-11'].reset_index() then plot df – Damian Vu Jan 18 '20 at 07:11
  • Post full working example code. See [here](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) how to do that. (From Review). – ZF007 Jan 18 '20 at 07:11
  • @user12550148 I've added an answer, let me know if it solves your problem. I'm still not very sure, what is the exact problem that you wanna solve. – Rohit Lal Jan 18 '20 at 07:20

1 Answers1

1
mydf = pd.read_csv('temperature_mean.csv')
    month       mean_temp
______________________
0   1982-01-01      39
1   1985-04-01      29
2   1999-03-01      19
3   2010-01-01      59
4   2013-05-01      32
5   2015-04-01      34
6   2016-11-01      59
7   2017-08-01      14
8   2017-09-01      7
df1 = mydf.set_index('month')
df= df1.loc['2010-01':'2019-11']
            mean_temp
month   
______________________
2010-01-01         59
2013-05-01         32
2015-04-01         34
2016-11-01         59
2017-08-01         14
2017-09-01         7

Drawing the line plot (default x & y arguments):

df.plot.line()

Line plot

If for some reason, you want to manually specify the column name

df.reset_index().plot.line(x='month', y='mean_temp')

Line plot

Rohit Lal
  • 2,791
  • 1
  • 20
  • 36