gpt4 book ai didi

python - 在循环中附加数据帧时列名会重复吗?

转载 作者:太空宇宙 更新时间:2023-11-03 21:13:12 26 4
gpt4 key购买 nike

我有两个数据帧,我正在将它们放入 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

所以,我的要求是我不想在结果中获取这些列名称,之后我将完整的数据帧放入一个数据帧中,我必须根据模型进行一些操作> 栏目。

最佳答案

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

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

关于python - 在循环中附加数据帧时列名会重复吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54898066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com