簡體   English   中英

在刪除索引的同時使用Pandas將標題添加到DataFrame

[英]Adding headers to a DataFrame with Pandas while dropping the index

我有一個DataFrame,它被重新采樣為一個較小的DataFrame,它保留了datetimeindex。 我轉置了數據框,現在希望刪除dateindex並將其替換為字符串(標簽),然后將其導出到.csv以使用可被javascript讀取的格式使用(在python中進行所有數據操作)。

我確實嘗試將其寫入不帶標題的.csv中(刪除日期),然后再次讀取以添加標簽,但這似乎不是很有效。

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

Python /熊貓代碼:

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

我嘗試遵循此解決方法( 在pandas dataframe中插入一行 ),但無法使其正常工作。

任何幫助表示贊賞!

您可以直接分配給數據框的索引名稱和列,以使其根據需要輸出。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM