Python 增加+;2只熊猫出类拔萃

Python 增加+;2只熊猫出类拔萃,python,excel,python-3.x,pandas,xlsxwriter,Python,Excel,Python 3.x,Pandas,Xlsxwriter,我有一个数据帧列表。每个循环是否有办法每次增加+2的增量?因此,excel中的数据框如下所示: while True: writer = pd.ExcelWriter("C:\\xzx.xlsx") worksheet = writer.sheets['Sheet1'] df3.to_excel(writer, startrow=0, startcol=+2, index = False) 数据帧: A B 20

我有一个数据帧列表。每个循环是否有办法每次增加+2的增量?因此,excel中的数据框如下所示:

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

                   A         B  
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 
理想的:

               A         B    C     D         E
        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)

这是否在循环和增量中工作?例如While true?无限量。我希望基本上在循环中每次递增+2,作为每次的新数据集。无限循环的原因通常更简单。我将使用if语句从loop@gerrybro好的,我明白了。。现在看看这个解决方案,不是一个无限循环。它有3个循环。