Sep-30-2019, 02:39 PM
Hi All,
Im trying to compare first and last value in pandas dataframe. My requirement is
if first value of column(id) group by city = last value of column(id) group by city, then create new field NEW and assign value as 1
ex: id city population
1234 chennai 2000
1234 hyd 3000
2345 calcutta 4000
4567 mumbai 5000
ouput should be:
id city population new
1234 chennai 2000 0
1234 hyd 3000 0
2345 calcutta 4000 1
4567 mumbai 5000 1
Please suggest the code in python.
Im trying to compare first and last value in pandas dataframe. My requirement is
if first value of column(id) group by city = last value of column(id) group by city, then create new field NEW and assign value as 1
ex: id city population
1234 chennai 2000
1234 hyd 3000
2345 calcutta 4000
4567 mumbai 5000
ouput should be:
id city population new
1234 chennai 2000 0
1234 hyd 3000 0
2345 calcutta 4000 1
4567 mumbai 5000 1
Please suggest the code in python.