将pandas数据帧输出到格式化文本文件

时间:2015-09-28 11:34:01

标签: python pandas

# df is dataframe, f_out is a text file I am outputting to
for index, row in df.iterrows():
    f_out.write(('%6d%4d%4d'+6*'%6.2f'+'\n') %
                (row['year'],row['month'],row['day'],
                 row['sr'],row['tma'],row['tmi'],
                 row['apc'],row['rhu'],row['wnd']))

我想将数据帧输出到格式为:

的文本文件
  1979   1   1  3.19 12.51 17.61  1.40  0.77  6.89

如何在不使用iterrows迭代所有行的情况下执行此操作?那种方法真的很慢

- 编辑:

示例数据框在这里: https://www.dropbox.com/s/d109fvj1higo88b/7_9.csv?dl=0

这里的小片段:

            air.2m  ap  day month   rhu sr  swr_diff tma    tmi    apc     wnd  year
1/1/1979    -20.9   0.0 1.0  1.0    0.8 2.3 26.3    -16.1   -20.9   73.4    4.8 1979
1/2/1979    -25.2   0.0 2.0  1.0    0.8 2.2 25.2    -18.6   -25.2   70.6    4.4 1979
1/3/1979    -26.2   0.0 3.0  1.0    0.8 2.5 29.4    -16.4   -26.2   82.1    4.7 1979
1/4/1979    -25.6   0.0 4.0  1.0    0.8 2.0 23.6    -16.1   -25.6   66.2    4.5 1979
1/5/1979    -29.6   0.0 5.0  1.0    0.8 2.8 32.2    -19.0   -29.6   88.1    3.4 1979
1/6/1979    -24.6   0.0 6.0  1.0    0.8 2.7 31.2    -15.7   -24.6   85.3    4.5 1979
1/7/1979    -22.8   0.0 7.0  1.0    0.9 2.6 30.5    -16.0   -22.8   84.1    3.4 1979
1/8/1979    -24.0   0.0 8.0  1.0    0.8 2.6 29.9    -14.2   -24.0   81.9    4.1 1979
1/9/1979    -19.0   0.0 9.0  1.0    0.9 2.8 32.6    -14.4   -19.0   87.8    3.2 1979
1/10/1979   -20.9   0.0 10.0 1.0    0.8 2.4 28.0    -16.5   -20.9   77.1    2.1 1979
1/11/1979   -24.2   3.1 11.0 1.0    0.8 2.2 25.0    -12.8   -24.2   73.4    3.1 1979
1/12/1979   -14.5   1.7 12.0 1.0    0.9 1.8 20.7    -11.1   -14.5   63.0    2.4 1979
1/13/1979   -16.9   6.3 13.0 1.0    0.9 1.9 22.5    -12.3   -16.9   69.6    7.2 1979

1 个答案:

答案 0 :(得分:4)

您可以通过几种方式对列重新排序。一种是在输出时手动分配列名。

SELECT 
    IIF(i.MarkDate IS NULL, r.MarkDate, i.MarkDate) as MarkDate,
    a.MaturityDate,
    IIF(i.ZeroRate IS NULL,
           r.startRate + DATEDIFF ( day , r.begindate, a.MaturityDate ) * rateChange,
           i.ZeroRate) as ZeroRate,
    i.*, r.*
FROM  
    allDates a
LEFT JOIN 
    Inter I ON a.MaturityDate  = I.MaturityDate
CROSS JOIN
    (SELECT 
         MIN(MaturityDate) minDate, MAX(MaturityDate) maxDate
     FROM Inter) AS t
LEFT JOIN  
    (SELECT 
         I1.id, I1.[MarkDate], 
         I1.[MaturityDate] begindate, I2.[MaturityDate] enddate, 
         I1.[ZeroRate] startRate,
         (I2.ZeroRate - I1.ZeroRate) * 1.0 / DATEDIFF ( day , I1.[MaturityDate], I2.[MaturityDate] )  rateChange        
     FROM 
         Inter I1
     INNER JOIN
         Inter I2 ON I1.id = I2.id - 1) r ON a.MaturityDate > r.[begindate]
                                          AND a.MaturityDate < r.[enddate] 
WHERE
    a.MaturityDate >= t.minDate
    AND a.MaturityDate <= t.maxDate;

编写文件时,请使用to_string方法。它将以您想要的均匀间隔格式保存。请参阅here

use_cols=['year','month','day',...other columns]

如果您需要使用特定的浮点或字符串格式格式化各个列,您可以执行类似的操作。请参阅here

with open('filename.txt','w') as outfile:
    df.to_string(outfile,columns=use_cols)