1

How to add first rows from two different columns of a dataframe together, such that if column A's first row is NaN, replace with value from balance's first row, otherwise, add column A's first row and balance's first row. And so likewise column B. The idea is using balnce first row to transform first row of other columns' A and B. I attempted using df.iloc[0] to get the first row but I'm unable to set the value or add it:

data = {'id': [1, 2, 3, 4, 5, 6],
        'A': [None, None, 20, 10, 39, 30],
        'B': [13, 98, 23, 45, 64, 10],
        'balance': [23, 41, 12, 22, 32, 0]}

df = pd.DataFrame(data)
df = df.set_index('id')
print df

    A   B    balance
id                 
1  NaN  13   23
2  NaN  98   41
3   20  23   12
4   10  45   22
5   39  64   32
6   30  10   0

for i in df.columns:
    if i not in ['balance']:
        if df[i].iloc[0] == None:
            df[i].iloc[0] = df['balance'].iloc[0]
        else:
            df[i].iloc[0] = df[i].iloc[0] + df['balance'].iloc[0]

        print df[i]


id
1   NaN
2   NaN
3    20
4    10
5    39
6    30
Name: A, dtype: float64


id
1    36
2    98
3    23
4    45
5    64
6    10
Name: B, dtype: int64
#A[0] should be 23, and B[0] should be 13 + 23 = 36

desired output:

id  A   B     balance               
1   23  36       23
2  NaN  98       41
3   20  23       12
4   10  45       22
5   39  64       32
6   30  10        0
5
  • Only the first row? Commented Jul 6, 2016 at 17:33
  • Your prose explanation doesn't match your sample code. You are setting A to balance when A is empty. Commented Jul 6, 2016 at 17:38
  • What is desired output? Column balance? Commented Jul 6, 2016 at 17:49
  • @ayhan yes, only the first row Commented Jul 6, 2016 at 19:22
  • @jezrael my bad - I made corrections to the post. The idea is to use balance to transform the first rows of columns A and B. If the columns are NaN, replace both A and B with balance. If otherwise: balance + A, balance + B`. This is only for the first rows. Commented Jul 6, 2016 at 19:29

1 Answer 1

3

IIUC you need combine_first or fillna if need replace NaN by values of other column:

print (df.A.combine_first(df.B))
id
1    13.0
2    98.0
3    20.0
4    10.0
5    39.0
6    30.0
Name: A, dtype: float64

Or:

print (df.A.fillna(df.B))
id
1    13.0
2    98.0
3    20.0
4    10.0
5    39.0
6    30.0
Name: A, dtype: float64


print (df.A.combine_first(df.B) + df.B)
id
1     26.0
2    196.0
3     43.0
4     55.0
5    103.0
6     40.0
dtype: float64

If need sum two columns with replacing NaN to 0 use add with parameter fill_value:

print (df.A.add(df.B, fill_value=0))
id
1     13.0
2     98.0
3     43.0
4     55.0
5    103.0
6     40.0
dtype: float64

EDIT:

You need:

df.ix[1,'A'] = df.ix[1,'balance']
print (df)
       A   B  balance
id                   
1   23.0  13       23
2    NaN  98       41
3   20.0  23       12
4   10.0  45       22
5   39.0  64       32
6   30.0  10        0

EDIT1:

df.ix[1,'A'] = df.ix[1,'balance']
df.ix[1,'B'] = df.ix[1,'B'] + df.ix[1,'balance']

print (df)
       A   B  balance
id                   
1   23.0  36       23
2    NaN  98       41
3   20.0  23       12
4   10.0  45       22
5   39.0  64       32
6   30.0  10        0
Sign up to request clarification or add additional context in comments.

7 Comments

OP states that "A[0] should be 23".
My solution is only guessing, if output is bad or all is bad, I remove it.
OP's wording of question is poor. Assuming the bottom comment in their code "A[0] should be 23, and B[0] should be 13 + 23 = 36" correctly describes what they want, I think using df.A.fillna(0) would provide the desired answer. Question needs to be better articulated.
@Grr - thank you for comment - I am not sure what OP exactly want too.
@jezrael, only teh first row gets transformed. I included the desired output
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.