增加+2pandas,超越

我有一个数据框的列表。 有没有办法每个循环增加+2的增量每次? 所以我的数据框在Excel中看起来像:

while True: writer = pd.ExcelWriter("C:\\xzx.xlsx") worksheet = writer.sheets['Sheet1'] df3.to_excel(writer, startrow=0, startcol=+2, index = False) 

dataframe:

  AB 2000-01-01 0.469112 -0.282863 2000-01-02 1.212112 -0.173215 2000-01-03 -0.861849 -2.104569 2000-01-04 0.721555 -0.706771 2000-01-05 -0.424972 0.567020 2000-01-06 -0.673690 0.113648 2000-01-07 0.404705 0.577046 2000-01-08 -0.370647 -1.157892 

理想:

  ABCDE 0.469112 -0.282863 0.469112 -0.282863 1.212112 -0.173215 1.212112 -0.173215 -0.861849 -2.104569 0.861849 -2.104569 0.721555 -0.706771 0.721555 -0.706771 -0.424972 0.567020 -0.424972 0.567020 -0.673690 0.113648 -0.673690 0.113648 0.404705 0.577046 0.404705 0.577046 -0.370647 -1.157892 -0.370647 -1.157892 

+2也在每个循环中给出相同的输出。

你也可以使用pd.concat

 import pandas as pd data = {'A': {'2000-01-01': 0.469112, '2000-01-02': 1.212112, '2000-01-03': -0.861849, '2000-01-04': 0.7215550000000001, '2000-01-05': -0.424972, '2000-01-06': -0.67369, '2000-01-07': 0.40470500000000004, '2000-01-08': -0.370647}, 'B': {'2000-01-01': -0.28286300000000003, '2000-01-02': -0.173215, '2000-01-03': -2.1045689999999997, '2000-01-04': -0.706771, '2000-01-05': 0.56702, '2000-01-06': 0.113648, '2000-01-07': 0.5770460000000001, '2000-01-08': -1.157892}} # Create dataframe df = pd.DataFrame(data) # Create a series to append "empty" column in loop extra_col = pd.Series(index=df.index,data=[""]*len(df)) # Create loop while True: df = pd.concat((df,extra_col,df),axis=1) # Let's break if more than 10 columns if len(df.T) > 10: break # Optional rename columns alphabet = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ") df.columns = (alphabet[i] for i in list(range(len(df.columns)))) # Output df.to_excel("test.xlsx",header=False,index=False)