Python 从常规数据帧向日期时间索引数据帧添加列
我有两个长度相同的数据帧(39014行),一个以datetime作为索引,另一个只是常规索引。我需要将一列复制到另一列中,但复制时会返回Python 从常规数据帧向日期时间索引数据帧添加列,python,pandas,dataframe,Python,Pandas,Dataframe,我有两个长度相同的数据帧(39014行),一个以datetime作为索引,另一个只是常规索引。我需要将一列复制到另一列中,但复制时会返回Nans。我做到了: df_datetime["newcol"]=df_regular["col"] 如果检查df_datetime中的newcol列,则该列将充满Nan,即使df_regular的col列中有数字。为什么会这样?我怎样才能修好它?谢谢 也试过 pd.merge(df_datetime, df_regular[["col"]], left_in
Nans
。我做到了:
df_datetime["newcol"]=df_regular["col"]
如果检查df_datetime
中的newcol
列,则该列将充满Nan
,即使df_regular
的col
列中有数字。为什么会这样?我怎样才能修好它?谢谢
也试过
pd.merge(df_datetime, df_regular[["col"]], left_index=True, right_index=True, how='left')
同样的情况也会发生,这是因为指数没有对齐 当您这样分配一个新列时,
df_datetime.loc[x,'newcol']
的值将与df_regular.loc[x,'newcol']
您需要将
df_datetime
的索引替换为datetime索引。这是因为索引没有对齐
当您这样分配一个新列时,df_datetime.loc[x,'newcol']
的值将与df_regular.loc[x,'newcol']
您需要将df_datetime
的索引替换为datetime索引。如果您这样做:
listOfColumn = list(df_regular["col"])
df_datetime["newcol"] = listOfColumn
如果您这样做会怎么样:
listOfColumn = list(df_regular["col"])
df_datetime["newcol"] = listOfColumn
作为df_datetime和df_regular,我准备了以下数据帧:
N1
Dat
2019-09-01 120
2019-09-02 130
2019-09-03 140
2019-09-04 150
2019-09-05 160
及
两个都有5行
如果要将df_regular中的col列“添加”到df_datetime中
忽略两个数据帧中的索引值,运行:
df_datetime['newcol'] = df_regular.col.values
结果是:
N1 newcol
Dat
2019-09-01 120 23
2019-09-02 130 26
2019-09-03 140 48
2019-09-04 150 51
2019-09-05 160 62
作为df_datetime和df_regular,我准备了以下数据帧:
N1
Dat
2019-09-01 120
2019-09-02 130
2019-09-03 140
2019-09-04 150
2019-09-05 160
及
两个都有5行
如果要将df_regular中的col列“添加”到df_datetime中
忽略两个数据帧中的索引值,运行:
df_datetime['newcol'] = df_regular.col.values
结果是:
N1 newcol
Dat
2019-09-01 120 23
2019-09-02 130 26
2019-09-03 140 48
2019-09-04 150 51
2019-09-05 160 62
请包括
df_regular
和df_datetime
pandas进行“内部数据对齐”的示例,这意味着pandas中的大多数操作都是针对索引和列标题匹配进行的。请包括df_regular
和df_datetime
pandas进行“内部数据对齐”的示例意味着pandas中的大多数操作都是针对索引和列标题匹配完成的。