I have these two dataframes:
Field1 | Field2 |
---|---|
0.5 | 0.7 |
2 | 1 |
3 | 0.1 |
4 | 0.4 |
and
Date | Time |
---|---|
2022-08-01 | 1 |
2022-08-01 | 2 |
and a I need to obtain the following:
Field1 | Field2 | Date | Time |
---|---|---|---|
0.5 | 0.7 | 2022-08-01 | 1 |
2 | 1 | 2022-08-01 | 2 |
3 | 0.1 | 2022-08-01 | 1 |
4 | 0.4 | 2022-08-01 | 2 |
Thanks in advance
CodePudding user response:
You can elongate your second dataframe to match dimentions, and then concatenate it with first dataframe.
import pandas as pd
df1 = pd.DataFrame({'Field1': [0.5, 2, 3, 4], 'Field2': [0.7, 1, 0.1, 0.4]})
print(df1)
# Field1 Field2
# 0 0.5 0.7
# 1 2.0 1.0
# 2 3.0 0.1
# 3 4.0 0.4
df2 = pd.DataFrame({'Date': ['2022-08-01', '2022-08-01'], 'Time': [1, 2]})
print(df2)
# Date Time
# 0 2022-08-01 1
# 1 2022-08-01 2
n = int(df1.size / df2.size)
df3 = pd.concat([df2] * n, axis=0).reset_index(drop=True)
print(df3)
# Date Time
# 0 2022-08-01 1
# 1 2022-08-01 2
# 2 2022-08-01 1
# 3 2022-08-01 2
df4 = pd.concat([df1, df3], axis=1)
print(df4)
# Field1 Field2 Date Time
# 0 0.5 0.7 2022-08-01 1
# 1 2.0 1.0 2022-08-01 2
# 2 3.0 0.1 2022-08-01 1
# 3 4.0 0.4 2022-08-01 2
or shorter:
df4 = pd.concat([
df1,
pd.concat(
[df2] * int(df1.size / df2.size),
axis=0
).reset_index(drop=True)
], axis=1)
CodePudding user response:
Here is a possible solution (df1
is your first dataframe, df2
is your second dataframe):
result = pd.concat(
[df1,
pd.concat(
[df2] * (len(df1) // len(df2) 1),
ignore_index=True,
).head(len(df1))
],
axis=1,
)
Here is the result:
>>> result
Field1 Field2 Date Time
0 0.5 0.7 2022-08-01 1
1 2.0 1.0 2022-08-01 2
2 3.0 0.1 2022-08-01 1
3 4.0 0.4 2022-08-01 2