删除索引时,使用Pandas将标头添加到DataFrame

时间:2014-07-24 11:13:22

标签: python pandas

我有一个DataFrame,它被重新采样到一个较小的DataFrame中,该DataFrame保留了datetimeindex。我转换了数据帧,现在希望删除dateindex并将其替换为字符串(标签),然后将其导出为.csv,以便可以通过javascript读取的格式(在python中执行所有数据操作)。

我确实尝试将它写入没有标题的.csv(删除日期),然后再次读取它以添加标签,但这似乎不是很有效。

链接到csv: https://www.dropbox.com/s/qy72yht2m7lk2pg/17_predicted.csv

Python / pandas代码:

import pandas as pd
import numpy as np
from dateutil.parser import parse
from datetime import datetime
from pandas import *

# Load csv into pandas DataFrame
df = pd.read_csv("17_predicted_dummydata.csv", parse_dates=True, dayfirst=False, keep_date_col=True, index_col=0)

#Creating a date range
df.index = pd.to_datetime(pd.date_range(start='1/1/2000 00:30:00', end='1/1/2000 05:00:00', freq='30T'))

#Rename index
df.index.name = 'date'

df_year = df.resample('D', how='sum')
df_year = np.round(df_year, 0)

df_year.index.name = 'label'
df_year.column = ['value']

df_year = df_year.T

print df_year.head()
print df_year.index.name

df_year.to_csv("17_dummy.csv") #drop index through header=False

CSV输入:

    Date/Time,InteriorEquipment:Electricity:Zone:4419 [J](TimeStep),InteriorEquipment:Electricity:Zone:3967 [J](TimeStep),InteriorEquipment:Electricity:Zone:3993 [J](TimeStep)
 01/01  00:30:00,0.583979872,0.428071889,0.044676234
 01/01  01:00:00,0.583979872,0.428071889,0.044676234
 01/01  01:30:00,0.583979872,0.428071889,0.044676234
 01/01  02:00:00,0.583979872,0.428071889,0.044676234
 01/01  02:30:00,0.583979872,0.428071889,0.044676234
 01/01  03:00:00,0.583979872,0.428071889,0.044676234
 01/01  03:30:00,0.583979872,0.428071889,0.044676234
 01/01  04:00:00,0.583979872,0.428071889,0.044676234
 01/01  04:30:00,0.583979872,0.428071889,0.044676234
 01/01  05:00:00,0.583979872,0.428071889,0.044676234

建议的csv输出:

    label,value
InteriorEquipment:Electricity:Zone:4419 [J](TimeStep),6.0
InteriorEquipment:Electricity:Zone:3967 [J](TimeStep),4.0
InteriorEquipment:Electricity:Zone:3993 [J](TimeStep),0.0

我尝试遵循此(Insert a row to pandas dataframe)解决方法,但无法使其正常工作。

感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

您可以直接指定数据框的索引名称和列,以便根据需要进行输出。

In [288]: df_year.index.name = 'label'

In [289]: df_year.columns = ['value']

In [290]: print df_year.to_csv()
label,value
Equipment:Electricity:LGF,79468.0
Equipment:Electricity:GF,66724.0
Equipment:Electricity:1st,30700.0
Equipment:Electricity:2nd,24126.0
Lights:Electricity:LGF,30596.0
Lights:Electricity:GF,30596.0
Lights:Electricity:1st,14078.0
Lights:Electricity:2nd,11063.0
General:Equipment:Electricity,201018.0
General:Lights:Electricity,86334.0
Electricity:Facility,314318.0
Electricity:Building,287352.0
Electricity:Plant,6329.0
Gas:Facility,279252.0
Electricity:HVAC,20637.0
General:Fans:Electricity,3554.0
Cooling:Electricity,17083.0
Pumps:Electricity,3708.0
WaterSystems:Electricity,2621.0