Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将DF中存在的列与另一个DF进行比较,然后填充_Python_Pandas_Dataframe - Fatal编程技术网

Python 将DF中存在的列与另一个DF进行比较,然后填充

Python 将DF中存在的列与另一个DF进行比较,然后填充,python,pandas,dataframe,Python,Pandas,Dataframe,我有两个DF,我想检查df1[A]是否在df2中。如果没有,则用0填充df2[A] 我得到它和丑陋的循环,我试图优化这一点,但我无法找到如何做到这一点 testing_list = list(testing_df.columns) for i in range(len(training_df.columns)): if not training_df.columns[i] in testing_list: testing_df[training_df.columns[i

我有两个DF,我想检查df1[A]是否在df2中。如果没有,则用0填充df2[A]

我得到它和丑陋的循环,我试图优化这一点,但我无法找到如何做到这一点

testing_list = list(testing_df.columns)

for i in range(len(training_df.columns)):
    if not training_df.columns[i] in testing_list:
        testing_df[training_df.columns[i]] = 0
与以下人员创建的新列一起使用:

如果要为具有排序列的两个数据帧添加列,请使用:


又是你!为什么sort=False?@Ragnar-仅对于避免排序列,您可以测试是否删除它-输出类似于第二个答案。如果我想做相反的操作,如果列在列中不可用,请在测试中删除它。简单的testing_df=testing_df[training_df.columns]不适用于不存在的列keyrorr我想按列检查,而不是按index@Ragnar-然后使用cols=testing\u df.columns.intersectiontraining\u df.columns,sort=False
testing_df = pd.DataFrame({
        'A':list('abcdef'),
         'B':[4,5,4,5,5,4],
         'F':list('aaabbb')
})

training_df = pd.DataFrame({
        'A':list('abcdef'),
         'C':[7,8,9,4,2,3],
         'D':[1,3,5,7,1,0],

})

cols = testing_df.columns.union(training_df.columns, sort=False)
df = testing_df.reindex(cols, axis=1, fill_value=0)
print (df)
   A  B  F  C  D
0  a  4  a  0  0
1  b  5  a  0  0
2  c  4  a  0  0
3  d  5  b  0  0
4  e  5  b  0  0
5  f  4  b  0  0
testing_df, training_df = testing_df.align(training_df, fill_value=0)
print (testing_df)
   A  B  C  D  F
0  a  4  0  0  a
1  b  5  0  0  a
2  c  4  0  0  a
3  d  5  0  0  b
4  e  5  0  0  b
5  f  4  0  0  b

print (training_df)
   A  B  C  D  F
0  a  0  7  1  0
1  b  0  8  3  0
2  c  0  9  5  0
3  d  0  4  7  0
4  e  0  2  1  0
5  f  0  3  0  0