在循环 pandas 中添加列 [英] Adding Columns in loop pandas

查看:59
本文介绍了在循环 pandas 中添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2个数据帧,每个数据帧有2列(两个df中的名称相同),我想将它们加在一起构成第三列.

I have a 2 dataframes each with 2 columns (named the same in both df's) and I want to add them together to make a third column.

df1['C']=df1[['A','B']].sum(axis=1) 
 df1['D']=df1[['E','G']].sum(axis=1)
df2['C']=df2[['A','B']].sum(axis=1) 
 df2['D']=df2[['E','G']].sum(axis=1)

但是实际上它比这更复杂.那么我可以将它们放入字典并循环吗?

However in reality its more complicated than this. So can I put these in a dictionary and loop?

我仍在寻找如何为此类问题构建字典的方法,因此任何建议都是不错的选择.

I'm still figuring out how to structure dictionarys for this type of problem, so any advice would be great.

这就是我想要做的:

all_dfs=[df1,df2]

for df in all_dfs:

dict={Out=['C'], in=['A','B]
    Out2=['D'], in2=['E','G]
}

for i in dict:
    df[i]=df[['i[1....

我对如何构建最后一点有点迷茫

I'm a bit lost in how to build this last bit

推荐答案

首先更改字典名称,因为dict是python代码字,然后通过带有输出列的键更改它,并通过输入列的列表的值更改它,最后通过items()方法:

First change dictionary name because dict is python code word, then change it by key with output column and value by list of input columns and last loop by items() method:

d= {'C':['A','B'],'D': ['E','G']}

for k, v in d.items():
    #checking key and value of dict
    print (k)
    print (v) 
    df[k]=df[v].sum(axis=1)

在这里使用DataFrames词典更简单,使用sum并最后创建DataFrames的注释者词典:

Here is simplier working with dictionary of DataFrames, use sum and last create anoter dictionary of DataFrames:

all_dfs= {'first': df1, 'second':df2}
out = {}

for name, df in all_dfs.items():
    d= {'C':['A','B'],'D': ['E','G']}
    for k, v in d.items():
        df[k]=df[v].sum(axis=1)
        #fill empty dict by name
        out[name] = df

print (out)
print (out['first'])
print (out['second'])

这篇关于在循环 pandas 中添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆