我有一个.csv文件,其数据类似于:
#file...out/houses.csv
#data...sun may 1 11:20:43 2011
#user...abs12
#host...(null)
#group...class=house
#property..change_per_hour
#limit...0
#interval..10000000
#timestamp,house_0,house_1,house_2,house_3,.....,house_1000
2010-07-01 00:00:00 EDT,1.2,1.3,1.4,1.5,........,9.72
2010-07-01 01:00:00 EDT,2.2,2.3,2.4,2.5,........,19.72
2010-07-01 02:00:00 EDT,3.2,3.3,3.4,3.5,........,29.72
2010-07-01 05:00:00 EDT,5.2,5.3,5.4,5.5,........,59.72
2010-07-01 06:00:00 EDT,6.2,,6.4,,..............,
...
我想将其转换并保存为新的.csv,数据应如下所示:
#file...out/houses.csv
#data...sun may 1 11:20:43 2011
#user...abs12
#host...(null)
#group...class=house
#property..change_per_hour
#limit...0
#interval..10000000
#EntityName,2010-07-01 00:00:00 EDT,2010-07-01 01:00:00 EDT,2010-07-01 02:00:00 EDT,2010-07-01 05:00:00 EDT,2010-07-01 06:00:00 EDT
house_0,1.2,2.2,3.2,5.2,6.2,...
house_1,1.3,2.3,3.3,5.3,,...
house_2,1.4,2.4,3.4,5.4,6.4,...
house_3,1.5,2.5,3.5,5.5,,...
...
house_1000,9.72,19.72,29.72,59.72,
我尝试使用pandas:转换为类似dtDict={'house_0':{'datetimestamp_1':'value_1','datetimestamp_2':'value_2'...}...}
的字典,但我无法转换为字典并使用panda DataFrame
,例如pandas.DataFrame(dtDict)
做转换。我不必使用pandas(你可以在python中使用任何东西)但是认为pandas对csv操作很有用。有什么帮助吗?
答案 0 :(得分:1)
假设它已经在pandas数据框中,这可以工作:
df = pd.DataFrame(
data=[[1, 3], [2, 5]],
index=[0, 1],
columns=['a', 'b']
)
输出:
>>>print(df)
a b
0 1 3
1 2 5
然后,转置数据框:
>>>print(df.transpose())
0 1
a 1 2
b 3 5