I have a dataset which looks like following-
| year | state |district| Party|
|---------------|-------------|--------|------|
| 2010 | haryana |kaithal | INC|
| 2010 | haryana |kaithal | bjp|
| 2010 | haryana |kaithal |NOTA|
| 2010 | goa |panji | AAP|
| 2010 | goa |panji | INC|
| 2010 | goa |panji | BJP|
| 2013 | up |meerut | INC|
| 2013 | up |meerut | SP |
| 2015 | haryana |kaithal |INC |
| 2015 | haryana |kaithal |BJP |
| 2015 | haryana |kaithal |AAP |
I want to rename INC to major for year 2010 and BJP to major in 2015. I need data in the following manner-
| year | state | district | Party |
|---|---|---|---|
| 2010 | haryana | kaithal | major |
| 2010 | haryana | kaithal | bjp |
| 2010 | haryana | kaithal | NOTA |
| 2010 | goa | panji | AAP |
| 2010 | goa | panji | major |
| 2010 | goa | panji | BJP |
| 2013 | up | meerut | major |
| 2013 | up | meerut | SP |
| 2015 | haryana | kaithal | INC |
| 2015 | haryana | kaithal | major |
| 2015 | haryana | kaithal | AAP |
I am using the code-
for state in df['state']:
if state=='haryana':
for year in df['year']:
if year==2010:
df['party'].replace('INC','major',inplace=True)
else:
continue
if year==2015:
df['party'].replace('BJP','major',inplace=True)
else:
continue
But this code is taking a lot of time to run and not giving the desired results as it is just considering replacing INC to major in all years and does not replace BJP.
df.state=='haryana'?df.state=='goa'ordf.state=='up'if state=='haryana':is wrong there in question?print (df[df['PARTY']=='JD(U)')])return correct rows?