Analyzing Farm produce data From Government of India website Using Python
By: Pankaj Kumar
Tags: Government Of India, Data Visualization, Python3, Agriculture Data, Pandas

Photo by Dan Loran on Unsplash

I recently came across data published by government of India on farm produce for different crops in different season district wise from year 1996 to 2015. I wanted to use this data to answer some questions I was interested one was the analysis of per unit area production of a particular crop across different state. I choose wheat for my analysis. but you can use any other based on the given data. According to the given data I am not sure if area is in acres or hectares and produce is in quintals or tons but that doesn’t affect our analysis if units are consistent which is my assumptions.

You can follow following steps to download data and do further analysis.

Step 1 downloading data

Go to India government data website

https://data.gov.in/

you can navigate through sector or go to the following link

https://data.gov.in/sectors/Agriculture-9212

You should be able to see following page :

Select the first file

District-wise, season-wise crop production statistics

You can save the file in whatever directory you want I have stored it in ./data/apy.csv file. if your location is different you can change path variable in following analysis.

Step 2 Analysis of data in Python

import pandas as pd

path ='./data/apy.csv'

import warnings

warnings.filterwarnings('ignore')

import matplotlib.pyplot as plt dfs = pd.read_csv(path) dfs.head()

dfs.info <bound method DataFrame.info of State_Name District_Name Crop_Year Season \

0 Andaman and Nicobar Islands NICOBARS 2000 Kharif

1 Andaman and Nicobar Islands NICOBARS 2000 Kharif

2 Andaman and Nicobar Islands NICOBARS 2000 Kharif

3 Andaman and Nicobar Islands NICOBARS 2000 Whole Year

4 Andaman and Nicobar Islands NICOBARS 2000 Whole Year

... ... ... ... ...

246086 West Bengal PURULIA 2014 Summer

246087 West Bengal PURULIA 2014 Summer

246088 West Bengal PURULIA 2014 Whole Year

246089 West Bengal PURULIA 2014 Winter

246090 West Bengal PURULIA 2014 Winter



Crop Area Production

0 Arecanut 1254.0 2000.0

1 Other Kharif pulses 2.0 1.0

2 Rice 102.0 321.0

3 Banana 176.0 641.0

4 Cashewnut 720.0 165.0

... ... ... ...

246086 Rice 306.0 801.0

246087 Sesamum 627.0 463.0

246088 Sugarcane 324.0 16250.0

246089 Rice 279151.0 597899.0

246090 Sesamum 175.0 88.0



[246091 rows x 7 columns]>

dfs.Season.unique() array(['Kharif ', 'Whole Year ', 'Autumn ', 'Rabi ',

'Summer ', 'Winter '], dtype=object) dfs.Crop.unique() array(['Arecanut', 'Other Kharif pulses', 'Rice', 'Banana', 'Cashewnut',

'Coconut ', 'Dry ginger', 'Sugarcane', 'Sweet potato', 'Tapioca',

'Black pepper', 'Dry chillies', 'other oilseeds', 'Turmeric',

'Maize', 'Moong(Green Gram)', 'Urad', 'Arhar/Tur', 'Groundnut',

'Sunflower', 'Bajra', 'Castor seed', 'Cotton(lint)', 'Horse-gram',

'Jowar', 'Korra', 'Ragi', 'Tobacco', 'Gram', 'Wheat', 'Masoor',

'Sesamum', 'Linseed', 'Safflower', 'Onion', 'other misc. pulses',

'Samai', 'Small millets', 'Coriander', 'Potato',

'Other Rabi pulses', 'Soyabean', 'Beans & Mutter(Vegetable)',

'Bhindi', 'Brinjal', 'Citrus Fruit', 'Cucumber', 'Grapes', 'Mango',

'Orange', 'other fibres', 'Other Fresh Fruits', 'Other Vegetables',

'Papaya', 'Pome Fruit', 'Tomato', 'Rapeseed &Mustard', 'Mesta',

'Cowpea(Lobia)', 'Lemon', 'Pome Granet', 'Sapota', 'Cabbage',

'Peas (vegetable)', 'Niger seed', 'Bottle Gourd', 'Sannhamp',

'Varagu', 'Garlic', 'Ginger', 'Oilseeds total', 'Pulses total',

'Jute', 'Peas & beans (Pulses)', 'Blackgram', 'Paddy', 'Pineapple',

'Barley', 'Khesari', 'Guar seed', 'Moth',

'Other Cereals & Millets', 'Cond-spcs other', 'Turnip', 'Carrot',

'Redish', 'Arcanut (Processed)', 'Atcanut (Raw)',

'Cashewnut Processed', 'Cashewnut Raw', 'Cardamom', 'Rubber',

'Bitter Gourd', 'Drum Stick', 'Jack Fruit', 'Snak Guard',

'Pump Kin', 'Tea', 'Coffee', 'Cauliflower', 'Other Citrus Fruit',

'Water Melon', 'Total foodgrain', 'Kapas', 'Colocosia', 'Lentil',

'Bean', 'Jobster', 'Perilla', 'Rajmash Kholar',

'Ricebean (nagadal)', 'Ash Gourd', 'Beet Root', 'Lab-Lab',

'Ribed Guard', 'Yam', 'Apple', 'Peach', 'Pear', 'Plums', 'Litchi',

'Ber', 'Other Dry Fruit', 'Jute & mesta'], dtype=object)

For some advances exploratory data analysis you can use packages like pandas profiling for now I am just happy with simple explorations. Ideas is not to run machine learning model its just to explore data.

In following Code section I have selected Wheat crop for Rabi season to check how production varies across states. As we can see the production in data is on district level which I have summed on on state level and created a new column for per unit area production called per_hect. My assumption is that area is in hectare which might not be the case. Analysis still holds.

dfs2= dfs[(dfs['Crop']=='Wheat') &(dfs.Season.str.strip()=='Rabi') ]

## sum values across all districts of state

grouped = dfs2.groupby(['State_Name' , 'Season', 'Crop','Crop_Year']).sum()

import math

## group dataframe

gr = grouped.reset_index().groupby(['State_Name' , 'Season','Crop'])

num_cols = 3



for i, (idx , df1) in enumerate(gr):



df1 = df1.reset_index().set_index('Crop_Year')

df1 = df1.reindex(range(1993, 2016), method='pad')

df1['per_hect'] = df1.Production/ df1.Area

df1[['Production', 'Area' ,'per_hect']].plot( kind = 'bar' , subplots = True ,

title = ' '.join(idx))

#break

Analysis

We can see the highest production per unit area comes from the states of Punjab , and Haryana and also in most of the states the area and production has been stable for last few years. In some states the area and production varies across the varies maybe due to some seasonal factors. We can do similar analysis for other Crops by changing filter criteria.