Check and Re-Arrange  Cols Pandas Package
reformatCols
In [ ]:
import pandas as pd
In [ ]:
df = pd.read_csv('/content/clean_agri_ds.csv')
In [ ]:
df.head(1)
Out[ ]:
Unnamed: 0 State_Name District_Name Crop_Year Season Crop Area Production Len State Len Dist Len Season Len Crop
0 4351 Andhra Pradesh KRISHNA 2005 Kharif Moong(Green Gram) 12334.0 9991.0 14 7 6 17
In [ ]:
reqd_list = ['Season', 'District_Name', 'Area', 'Production', 'Crop_Year' ]
In [ ]:
input_list = []
input_list = df.columns
In [ ]:
input_list
Out[ ]:
Index(['Unnamed: 0', 'State_Name', 'District_Name', 'Crop_Year', 'Season',
       'Crop', 'Area', 'Production', 'Len State', 'Len Dist', 'Len Season',
       'Len Crop'],
      dtype='object')
In [ ]:
reqd_list[2]
Out[ ]:
'Season'
In [ ]:
hasCols = []
for item in reqd_list:
    if item in input_list:
        print(" order =>", item)
        hasCols.append(item)
    else:
        print("not in order =>", item)
#        df.drop[item, inplace = True]
print (hasCols)
 order => Season
 order => District_Name
 order => Area
 order => Production
 order => Crop_Year
['Season', 'District_Name', 'Area', 'Production', 'Crop_Year']
In [ ]:
hasCols
Out[ ]:
['State_Name', 'Crop_Year', 'Season', 'Crop_Year']
In [ ]:
ndf = df[['Season', 'Crop', 'State_Name', 'Crop_Year']]
In [ ]:
ndf.head(2)
Out[ ]:
Season Crop State_Name Crop_Year
0 Kharif Moong(Green Gram) Andhra Pradesh 2005
1 Kharif Papaya Assam 2013
In [ ]:
new = df.filter(hasCols, axis=1)
In [ ]:
new.head(2)
Out[ ]:
Season Crop State_Name Crop_Year
0 Kharif Moong(Green Gram) Andhra Pradesh 2005
1 Kharif Papaya Assam 2013
In [ ]:
all(i == j for i, j in zip(reqd_list, input_list))
Out[ ]:
False
In [ ]: