Selecting first and last row in python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Selecting first and last row in python (/thread-21443.html) |
Selecting first and last row in python - sudhirkaukuntla - Sep-30-2019 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. RE: Selecting first and last row in python - Larz60+ - Sep-30-2019 what have you tried, show code RE: Selecting first and last row in python - sudhirkaukuntla - Oct-01-2019 My code: def test(x): if((df.groupby(['city']).first())== (df.groupby(['city']).last())): df["NEW"]=1 return x df |