gpt4 book ai didi

python - Pandas:更好的交叉连接方式?

转载 作者:行者123 更新时间:2023-12-01 00:11:40 24 4
gpt4 key购买 nike

我有以下代码,用于交叉连接两个Pandas数据帧。这是最好的方法吗?这可以以更快、更有效的方式完成吗?

# Cross join in Pandas

import pandas as pd
import numpy as np

d1 = {'Year': [2019, 2019, 2019, 2019, 2019, 2019],
'Week': [1, 2, 3, 5, 5, 6],
'Part': ['A', 'A', 'A', 'A', 'B', 'B'],
'Plant': [100, 100, 200, 200, 100, 100],
'Static': [20, 20, 20, 20, 40, 40],
'Value': [np.nan, 10, np.nan, 50, 30, np.nan]}

d2 = {'Year': [2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019],
'Week': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}

df1 = pd.DataFrame(d1)
df2 = pd.DataFrame(d2)

df3 = (df2.assign(Key=1)
.merge(pd.DataFrame({'Part': df1['Part'].unique(), 'Key': 1}), on='Key')
.drop('Key', 1)
)

df4 = (df3.assign(Key=1)
.merge(pd.DataFrame({'Plant': df1['Plant'].unique(), 'Key': 1}), on='Key')
.drop('Key', 1)
)

df5 = df4.merge(df1, on=['Year', 'Week', 'Part', 'Plant'], how='left')
df5 = df5.sort_values(by=['Part', 'Plant', 'Year', 'Week'])
df5.reset_index()

print(df5)

最佳答案

另一个解决方案 itertools.productDataFrame 构造函数和左连接:

from  itertools import product

df = pd.DataFrame(list(product(df2['Year'].unique(),
df2['Week'].unique(),
df1['Part'].unique(),
df1['Plant'].unique())), columns=['Year','Week','Part','Plant'])

df = df.merge(df1, how='left')

关于python - Pandas:更好的交叉连接方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59602243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com