我有一个数据框列表。有没有办法让每个循环每次增加+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在每个循环中也给出相同的输出。
答案 0 :(得分:1)
您也可以使用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)