简体   繁体   English

在数据帧的第N列之后插入空格或空白列

[英]Inserting space or blank column after N'th column in a dataframe

I have 3 dataframes and I have concatenated them into a single dataframe. 我有3个数据框,并且已将它们串联为一个数据框。 However,now I need to insert a blank column after every 2nd column(correlation) in this dataframe and then write it to excel. 但是,现在我需要在此数据框中的每个第二列(相关性)之后插入一个空白列,然后将其写入excel。 So each one dataframe looks like: 因此,每个数据框如下所示:

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 

And after their concatenation and then space or blank column append they should be of the format: 在连接之后,然后在空格或空白列后面附加以下格式:

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

Can someone please help me with this? 有人可以帮我吗?

Use range one for every 2 columns and one for startcol parameter as: 每2列使用range一,对于startcol参数使用range为:

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()

If you have data frames separately then: 如果单独拥有数据帧,则:

for df,st_col  in zip([df1,df2,df3], range(0,7,3)):
    df.to_excel(writer, index=False, startcol=st_col)

Which will save in excel as: 将在excel中保存为: 产量

Try using the method 'insert'. 尝试使用“插入”方法。 Something like this: 像这样:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM