我有3个数据框,并且已将它们串联为一个数据框。但是,现在我需要在第二列之后插入一个空白列(相关) 在此数据框中,然后将其写入Excel。因此,每个数据帧看起来像:
Variable_Name correlation
Pending_Disconnect 0.553395448
status_Active 0.539464806
days_active 0.414774231
days_pend_disco 0.392915837
prop_tenure 0.074321692
abs_change_3m 0.062267386
在连接之后,然后在空格或空白列后面附加以下格式:
Variable_Name correlation Variable_Name correlation Variable_Name correlation
Pending_Disconnect 0.553395448 Pending_Change 0.043461995 active_frq_N 0.025697016
status_Active 0.539464806 status_Active 0.038057697 active_frq_Y 0.025697016
days_active 0.414774231 ethnic 0.037503202 ethnic 0.025195149
days_pend_disco 0.392915837 days_active 0.037227245 ecgroup 0.023192408
prop_tenure 0.074321692 archetype_grp 0.035761434 age 0.023121305
abs_change_3m 0.062267386 age_nan 0.035761434 archetype_nan 0.023121305
有人可以帮我吗?
答案 0 :(得分:2)
每2列使用range
,而startcol
参数则使用import xlsxwriter
writer = pd.ExcelWriter('pandas_column_formats.xlsx',engine='xlsxwriter')
for col,st_col in zip(range(0,6,2), range(0,7,3)):
df.iloc[:,col:col+2].to_excel(writer, index=False, startcol=st_col)
writer.save()
writer.close()
:
for df,st_col in zip([df1,df2,df3], range(0,7,3)):
df.to_excel(writer, index=False, startcol=st_col)
如果单独有数据帧,则:
from scrapy.crawler import CrawlerRunner
from scrapy_app.scrapy_app.spiders.generic import GenericSpider
....
class MyProcess(object):
def start_my_process(self, _config, _req_obj, site_urls):
runner = CrawlerRunner()
runner.crawl(GenericSpider,
config=_config,
reqObj=_req_obj,
urls=site_urls)
deferred = runner.join()
deferred.addBoth(lambda _: reactor.stop())
reactor.run()
....
答案 1 :(得分:1)
尝试使用“插入”方法。像这样:
N = len(df.columns) - 2 # number of columns, starting 2 before the last one
for i in range(N,2,-2): # going backwards since the column numbers change during insertion
df.insert(i,'','',allow_duplicates=True)