在循环中添加数据框时,列名会重复出现吗?

时间:2019-02-27 04:29:03

标签: python pandas dataframe

我有两个Dataframe,它们正在for循环内。当我尝试附加两个数据框时,在行之间填充了列名。 我做了一些研究并使用了列表,但是我无法使列表适合我,这是我的代码,

def generatesalesreport3():
if g_month is not None and g_year is not None:
    if (g_month == '1: January to March'):
        r_month = ['January','February','March']
        r_year = int(g_year)
    elif(g_month == '2: April to June'):
        r_month = ['April', 'May', 'June']
        r_year = int(g_year)
    elif(g_month == '3: July to Septempber'):
        r_month = ['July', 'August', 'September']
        r_year = int(g_year)
    else:
         r_month = ['October', 'November', 'December']
         r_year = int(g_year)

    ra = list()
    rb= list()
    for qmonth in r_month:
        monnum = month_dict[qmonth]
        resultb1 = df_b1.loc[(df_b1['month'] == monnum) & (df_b1['year'] == r_year)]
        resultb2 = df_b2.loc[(df_b2['month'] == monnum) & (df_b2['year'] == r_year)]
        resultb1 = resultb1.append(resultb2)
        print(resultb1)

Image to the result of above code please check

所以,我的要求是我不想在结果之间获得这些列名,之后,我将整个数据框合并为一个数据框,因此我必须根据 Model 进行一些操作>列。

1 个答案:

答案 0 :(得分:0)

只需从“ resultb2”数据框中过滤出列,然后追加即可。喜欢:

resultb1 = resultb1.append(resultb2[["column1","column2","column3",......]])
print(resultb1)