Python/Pandas - building a new column based in columns comparison

2024/10/13 23:24:43

I have this dataframe:

df:CNPJ     Revenues 2016    Revenues 2015     Revenues 2014
0     01.637.895/0001-32     R$ 12.696.658              NaN     R$ 10.848.213   
1     02.916.265/0001-60               NaN   R$ 162.914.526    R$ 142.412.432
2     02.932.074/0001-91               NaN              NaN      R$ 1.928.312
3     03.853.896/0001-40     R$ 19.333.453    R$ 18.891.833     R$ 12.645.986

Each row represents a company and each "Revenues" column represent the companies revenues in the referenced year.

I want to make a new column called "last_revenues" that will have the last value of revenues. If 2016 is the last we've got, will be 2016, if we don't have 2016 but have 2015, 2015 will be the one. If we don't have neither 2016 or 2015, last_revenues will have the 2014 value.

It would have to look like this:

                    CNPJ     last_revenues
0     01.637.895/0001-32     R$ 12.696.658
1     02.916.265/0001-60    R$ 162.914.526
2     02.932.074/0001-91      R$ 1.928.312
3     03.853.896/0001-40     R$ 19.333.453

Can someone suggest a way of doing it?

Answer
df1 = df.set_index('CNPJ')
df1['last_revenues'] = df1.fillna(method='bfill',axis=1).iloc[:,0]

or as DSM suggests we can shorten this to

df1['last_revenues'] = df1.bfill(axis=1).iloc[:,0]
df1.reset_index()

Output:

                 CNPJ  Revenues 2016   Revenues 2015   Revenues 2014  \
0  01.637.895/0001-32  R$ 12.696.658             NaN   R$ 10.848.213   
1  02.916.265/0001-60            NaN  R$ 162.914.526  R$ 142.412.432   
2  02.932.074/0001-91            NaN             NaN    R$ 1.928.312   
3  03.853.896/0001-40  R$ 19.333.453   R$ 18.891.833   R$ 12.645.986   last_revenues  
0   R$ 12.696.658  
1  R$ 162.914.526  
2    R$ 1.928.312  
3   R$ 19.333.453  
https://en.xdnf.cn/q/118022.html

Related Q&A

Present blank screen, wait for key press -- how?

lo,I am currently trying to code a simple routine for an experiment we are planning to run. The experiment starts by entering a subject number and creating a bunch of files. I got that part working. Ne…

Images appearing in all but 1 flask page

I am creating a web app in flask, python, mysql. When viewing every other page on my website my images load, but when viewing one specific page, I cant get any images to load using already working code…

Python: Write nested list objects to csv file

Im trying to write data from a list of lists to a csv file. This is a simplified version of what I haveclass Point(object): def __init__(self, weight, height):self.weight = weightself.height = heightde…

Entire module is not detected by __init__.py

I have a relatively small python program which is setup like thisRoot--Network--DTOLots of py files which contain classes.Other py files in the projectBoth in the Network and the DTO folder there is an…

Python output to terminal during ssh login

I have been looking everywhere for this and have not found a solution. I am using python 2.5.1 on an Apple iPod and I am trying to connect to a host with SSH. First I start off with import os. Next I o…

Unable to find SIFT or xfeatures2d in OpenCV Python [duplicate]

This question already has answers here:PyCharm: Installation of non-free OpenCV modules for operations like SIFT, SURF(2 answers)Closed 6 years ago.I recently switch back to python for facial detection…

Django paginate for django 2

I need to use pagination to a Django list but I couldnt find any help online,, only old docs from Django version 1.3 here are my files : views.pydef home(request):all_dress = Item.objects.all().filter(…

AttributeError Button object has no attribute scrlFBtn

from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.label import Label from kivy.core.window import Window from kivy.uix.scrollview import ScrollView from kivy.effects.scrol…

How to set interpreter of WinPython as the vim default python interpreter?

I use a Python distribution named WinPython. Now I want my vim to use the python interpreter in WinPython as its default interpreter. I tried add the F:\WinPython\python-2.7.3.amd64 into my windows env…

how to deal with Python BaseHTTPServer killed,but the port is still be occupied?

I use python BaseHTTPServer,it can handle do_GET,do_POST methods,in do_POST method,i execute linux shell using os.system,when i kill the python script,but the listening port still occupied,so i cant ru…