2015-12-21 79 views
0

我目前使用此代碼:的Python:打印數據框到csv

import pandas as pd 
AllDays = ['a','b','c','d'] 
TempDay = pd.DataFrame(np.random.randn(4,2)) 
TempDay['Dates'] = AllDays 
TempDay.to_csv('H:\MyFile.csv', index = False, header = False) 

但它打印時它打印帶表頭行的日期前陣。我正在尋找在TemperatureArray之前打印日期並且沒有標題行。

編輯: 該文件是與溫度陣列後跟日期:[溫度陣列,日期]。

-0.27724356949570034,-0.3096554106726788,a 
-0.10619546908708237,0.07430127684522048,b 
-0.07619665345406437,0.8474460146082116,c 
0.19668718143436803,-0.8072994364484335,d 

我期待打印:[日期TemperatureArray]

a,-0.27724356949570034,-0.3096554106726788 
b,-0.10619546908708237,0.07430127684522048 
c,-0.07619665345406437,0.8474460146082116 
d,0.19668718143436803,-0.8072994364484335 

回答

2

pandas.Dataframe.to_csv方法有一個關鍵字參數,header=True可以被關閉,以禁用頭。

但是,它有時不起作用(來自經驗)。 與index=False配合使用可以解決您的問題。

例如,該段應解決您的問題:

TempDay.to_csv('C:\MyFile.csv', index=False, header=False) 

這裏是展示它如何禁用標題行一個完整的例子:

>>> import pandas as pd 
>>> import numpy as np 
>>> df = pd.DataFrame(np.random.randn(6,4)) 
>>> df 
      0   1   2   3 
0 1.295908 1.127376 -0.211655 0.406262 
1 0.152243 0.175974 -0.777358 -1.369432 
2 1.727280 -0.556463 -0.220311 0.474878 
3 -1.163965 1.131644 -1.084495 0.334077 
4 0.769649 0.589308 0.900430 -1.378006 
5 -2.663476 1.010663 -0.839597 -1.195599 

>>> # just assigns sequential letters to the column 
>>> df[4] = [chr(i+ord('A')) for i in range(6)] 
>>> df 
      0   1   2   3 4 
0 1.295908 1.127376 -0.211655 0.406262 A 
1 0.152243 0.175974 -0.777358 -1.369432 B 
2 1.727280 -0.556463 -0.220311 0.474878 C 
3 -1.163965 1.131644 -1.084495 0.334077 D 
4 0.769649 0.589308 0.900430 -1.378006 E 
5 -2.663476 1.010663 -0.839597 -1.195599 F 

>>> # here we reindex the headers and return a copy 
>>> # using this form of indexing just requires you to provide 
>>> # a list with all the columns you desire and in the order desired 
>>> df2 = df[[4, 1, 2, 3]] 
>>> df2 
    4   1   2   3 
0 A 1.127376 -0.211655 0.406262 
1 B 0.175974 -0.777358 -1.369432 
2 C -0.556463 -0.220311 0.474878 
3 D 1.131644 -1.084495 0.334077 
4 E 0.589308 0.900430 -1.378006 
5 F 1.010663 -0.839597 -1.195599 

>>> df2.to_csv('a.txt', index=False, header=False) 
>>> with open('a.txt') as f: 
...  print(f.read()) 
... 
A,1.1273756275298716,-0.21165535441591588,0.4062624848191157 
B,0.17597366083826546,-0.7773584823122313,-1.3694320591723093 
C,-0.556463084618883,-0.22031139982996412,0.4748783498361957 
D,1.131643603259825,-1.084494967896866,0.334077296863368 
E,0.5893080536600523,0.9004299653290818,-1.3780062860066293 
F,1.0106633581546611,-0.839597332636998,-1.1955992812601897 

如果您需要動態調整列,最後一列移動到第一個,你可以做如下:

# this returns the columns as a list 
columns = df.columns.tolist() 
# removes the last column, the newest one you added 
tofirst_column = columns.pop(-1) 
# just move it to the start 
new_columns = [tofirst_column] + columns 

# then you can the rest 
df2 = df[new_columns] 

這只是人使您無法獲取當前列的列表,從當前列中構建一個Python列表,並重新對這些標題進行重新索引,而無需事先了解這些標題。

+0

感謝您的建議。更大的問題是它打印溫度數組後的日期,但是我需要溫度數組之前的日期。 – Zanam

+0

你能提供一個小樣本嗎?我以爲你主要是想關閉標題。除非你可以提供一個小的Dataframe作爲例子,否則調試會有點困難。 –

+0

或者你只是在查找按時間順序排列的「之前」,即溫度數組中的日期之前發生的所有日期)。 –