简体   繁体   English

如何从两个不同的pandas dataframe列中添加值

[英]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_firstfillna如果需要用其他列的值替换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 : 如果需要将两列替换为NaN0使用参数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.

相关问题 如何比较 pandas dataframe 的两个不同列的值? - How to compare values of two different columns of a pandas dataframe? 如何根据不同列中的值向 pandas dataframe 添加一列? - How to add one column to pandas dataframe based on values in different columns? 如何遍历 pandas dataframe 中的两列以将值添加到列表中 - How to iterate through two columns in a pandas dataframe to add the values to a list 从 2 个不同的数据帧熊猫添加两列的值 - add values of two columns from 2 different dataframes pandas 如何将同一 position 中的列与两个不同的 pandas dataframe 相乘? - How to multiply columns in the same position from two different pandas dataframe? 根据不同数据框中的匹配值,将摘要列添加到pandas数据框中 - Add summary columns to a pandas dataframe based on matching values in a different dataframe 如何在 Pandas dataframe 的动态列中添加值? - How to add values in dynamic columns in Pandas dataframe? Python Pandas - 如何将 dataframe 的两列的值与另一个 Dataframe 的列进行比较? - Python Pandas - How to compare values from two columns of a dataframe to another Dataframe columns? 如何比较不同数据框熊猫中的两列并用其他列替换值 - how to compare two columns in different dataframe pandas and replacing values with other columns 如何根据 pandas dataframe 中多个其他列的值添加两个新列? - How do I add two new columns on the basis of the values of multiple other columns in a pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM