pythonpython-3.xpandasffill

How to fill the rightmost column with values in pandas


There is an unknown number of columns, and each row has exactly one value.

However, I cannot tell which column the number is in.

I would like to know how to fill one value from each row into the rightmost column.

The example below consists of three columns, but I don't know how many there actually are.

import pandas as pd
import io

temp = u"""
col1,col2,col3
nan,nan,3
nan,4,nan
1,nan,nan
"""

data = pd.read_csv(io.StringIO(temp), sep=",")

# data
# Out[31]: 
#    col1  col2  col3
# 0   NaN   NaN   3.0
# 1   NaN   4.0   NaN
# 2   1.0   NaN   NaN


What I want:

# data2
#     col3
# 0    3.0
# 1    4.0
# 2    1.0

Solution

  • Since you know you have exactly one value in each row, you can add all the rows.

    import pandas as pd
    import io
    
    temp = u"""
    col1,col2,col3
    nan,nan,3
    nan,4,nan
    1,nan,nan
    """
    
    data = pd.read_csv(io.StringIO(temp), sep=",")
    

    data['col4'] = data.sum(axis=1, numeric_only=True)
    

    One more way is to :

    data['col4'] = data.loc[:,[*data.columns.values]].sum(axis=1)
    

    Output

    enter image description here

    pandas.DataFrame.sum