简体   繁体   English

熊猫-DatetimeIndex作为列标题

[英]Pandas - DatetimeIndex as column headings

I have a dataframe that is 438x14401. 我有一个438x14401的数据框。 I have a DateTimeIndex that is the same length (width?) as the columns in the dataframe, which are now just labeled 1 - ... I am trying to replace those values with the dates in my DateTimeIndex. 我有一个与数据框中的列相同长度(宽度?)的DateTimeIndex,现在这些列仅被标记为1-...我试图用我的DateTimeIndex中的日期替换这些值。

在此处输入图片说明

在此处输入图片说明

I am trying to substitude column headings 0 - end with the datetimeindex values, which should match in length. 我正在尝试将列标题0降级-以datetimeindex值结尾,该值的长度应匹配。 I have tried just renaming the columns with the dates and merging them, with no success. 我尝试仅用日期重命名列并合并它们,但没有成功。 Is this at all possible? 这是可能吗?

stations = stations.iloc[:,2:].rename(columns=v, inplace=True)

You can assign your DatetimeIndex directly to stations.columns : 您可以将DatetimeIndex直接分配给stations.columns

Sample df: 样本df:

df = pd.DataFrame({'a': [1,2,3], 'b': [9,8,7], 'c': [7,5,1], 'd':[1,3,5]})
df
   a  b  c  d
0  1  9  7  1
1  2  8  5  3
2  3  7  1  5

some_date_range = pd.date_range('2017-01-01', periods=len(df.columns))

df.columns = some_date_range

df
   2017-01-01  2017-01-02  2017-01-03  2017-01-04
0           1           9           7           1
1           2           8           5           3
2           3           7           1           5

Keeping first two column values (referencing How do i change a single index value in pandas dataframe? since Index doesn't support mutable operations): 保留前两个列的值(由于Index不支持可变操作,请参阅如何更改pandas数据框中的单个索引值? ):

aslist = df.columns.tolist()
aslist[2:] = pd.date_range('2017-01-01', periods=2)
df.columns = aslist
df
   a  b  2017-01-01 00:00:00  2017-01-02 00:00:00
0  1  9                    7                    1
1  2  8                    5                    3
2  3  7                    1                    5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM