直到现在我的数据,

我正在尝试将cols (包含所有列的列表)从0转换为188个 ( cols = list(hdata.columns[ range(0,188) ]) ) (以这种格式表示yyyy-mm )到datetimeIndex。还有其他一些列,它们是“string”名称,因此不能转换为dateTime,所以我尝试这样做,
hdata[cols].columns = pd.to_datetime(hdata[cols].columns) #convert columns to **datetimeindex**但这是,而不是。你能找出这里出了什么问题吗?
Split-Apply-Combine编辑:处理这类数据的更好方法是使用方法。
步骤1:拆分要执行某些特定操作的数据。
nonReqdf = hdata.iloc[:,188:].sort_index()
reqdf= reqdf.drop(['CountyName','Metro','RegionID','SizeRank'],axis=1)步骤2:执行操作。在我的例子中,它将具有年份和月份的dataframe列转换为datetimeIndex。每季度重新整理一次。
reqdf.columns = pd.to_datetime(reqdf.columns)
reqdf = reqdf.resample('Q',axis=1).mean()
reqdf = reqdf.rename(columns=lambda x: str(x.to_period('Q')).lower()).sort_index() # renaming so that string is yyyy**q**<1/2/3/4> like 2012q1 or 2012q2 likewise步骤3:合并了两个分离的数据。(merge可以使用,但可能取决于您想要的内容)。
reqdf = pd.concat([reqdf,nonReqdf],axis=1)发布于 2020-08-19 21:47:00
为了修改索引中的一些标签(无论是行还是列),您需要使用df.rename,如下所示
for i in range(188):
df.rename({df.columns[i]: pd.to_datetime(df.columns[i])},
axis=1, inplace=True)或者,可以通过构建一个完整大小的索引来覆盖所有列,从而避免循环。
df.columns = (
pd.to_datetime(cols) # pass the list with strings to get a partial DatetimeIndex
.append(df.columns.difference(cols)) # complete the index with the rest of the columns
)https://stackoverflow.com/questions/63492730
复制相似问题