I have a dataframe as shown below:
df =
A B
timestamp
2022-04-08 10:07:00 29.504 50 0.85
2022-04-08 10:07:01 29.731 52 0.83
2022-04-08 10:07:02 29.393 53 0.84
I have a list as shown:
B =
[[0, 5],
[10, 1],
[1,40]]
I want to append the list to the dataframe like this
df =
A B 0 1
timestamp
2022-04-08 10:07:00 29.504 50 0.85 0 5
2022-04-08 10:07:01 29.731 52 0.83 10 1
2022-04-08 10:07:02 29.393 53 0.84 1 40
I order to do this, I converted the list to a dataframe
C = pd.DataFrame(B)
C =
0 1
0 0 5
1 10 1
2 1 40
In order to obtain the desired result, I tried pd.concat and pd.append. Both of the techniques did not work. The dataframe df has a timestamp as index and the dataframe C does not have timestamp index. Therefore I am finding it difficult to concatenate or append these two dataframe. Can somebody help me with a solution?
>Solution :
You can reset_index the first df and then do pd.concat like this,
pd.concat([df.reset_index(), pd.DataFrame(B)], axis=1).set_index("timestamp")
You can later set the index for the resulting dataframe as timestamp