Ben
Ben

Reputation: 1522

Why can't I copy resp. insert this column into the dataframe?

I am just trying to copy a dataframe column into another dataframe.. but it does not work:

df_transf1['timestamp'][0:3]
2018-02-05 14:00:00   2018-02-05 14:00:00
2018-02-05 15:00:00   2018-02-05 15:00:00
2018-02-05 16:00:00   2018-02-05 16:00:00
Name: timestamp, dtype: datetime64[ns]

X_train1_df['time'] = df_transf1['timestamp'][0:len(X_train1_df)]

type(X_train1_df)
> pandas.core.frame.DataFrame

len(X_train1_df)
> 1700

X_train1_df.head()
time    A_phsA
0   NaT 1.679253
1   NaT 1.705401
2   NaT 1.518297
3   NaT 1.229420
4   NaT 1.178687

When I try to do it with a numeric column A_phsA from df_transf1 it is the same, just with NaNs. What is wrong?

Upvotes: 0

Views: 34

Answers (1)

prhmma
prhmma

Reputation: 953

since there might be some kind of index mismatch, one way to do this is to convert the column to list, then assign it to another dataframe. conversion will happen with values

X_train1_df.insert(2, "time2", df_transf1['timestamp'][:len(X_train1_df)].values)

for educational purposes you can try it like this:

type(df_transf1['timestamp'][:len(X_train1_df)].values)
type(df_transf1['timestamp'][:len(X_train1_df)])

Upvotes: 1

Related Questions