[英]How to add values from two different columns of pandas dataframe
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. 如何将数据帧的两个不同列中的第一行添加到一起,这样如果列A的第一行是
NaN
,则用balance
的第一行中的值替换,否则,添加列A的第一行和balance
的第一行。 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: 同样是列B.这个想法是使用
balnce
第一行来转换其他列'A和B的第一行。我尝试使用df.iloc[0]
来获取第一行但是我无法设置值或添加它:
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
IIUC you need combine_first
or fillna
if need replace NaN
by values of other column: IIUC你需要
combine_first
或fillna
如果需要用其他列的值替换NaN
:
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
: 如果需要将两列替换为
NaN
为0
使用参数fill_value
add
:
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: 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.