问题描述
我有一个438x14401的数据框.我有一个DateTimeIndex,它的长度(宽度?)与数据框中的列相同(现在标记为1-...).我试图将这些值替换为DateTimeIndex中的日期.
I have a dataframe that is 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.
我正在尝试将列标题0降级-以datetimeindex值结尾,该值的长度应匹配.我尝试仅用日期重命名列并合并它们,但没有成功.这有可能吗?
I am trying to substitude column headings 0 - end with the datetimeindex values, which should match in length. 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)
推荐答案
您可以将DatetimeIndex
直接分配给stations.columns
:
样本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
保留前两列的值(引用我如何在熊猫数据框中更改单个索引值?,因为Index
不支持可变操作):
Keeping first two column values (referencing How do i change a single index value in pandas dataframe? since Index
doesn't support mutable operations):
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
这篇关于 pandas -DatetimeIndex作为列标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!