数据帧的不同行之间的乘积



我有几个数据帧如下所示:

time_hr
0.028611 0.028333 0.004722

对我来说,这听起来像是笛卡尔式的产品:

from io import StringIO
#sample data reading
data1 = """
time_hr cell_hour   id  attitude    hour
0.028611    xxx 1   Cruise  1.0
0.028333    xxx 4   Cruise  1.0
0.004722    xxx 16  Cruise  1.0
"""
df = pd.read_csv(StringIO(data1), sep="t")
#filtering dataset to needed columns
df_time = df[["id", "time_hr"]]
df_comb = df_time.merge(df_time, how='cross')
df_comb = df_comb[df_comb["id_x"] != df_comb["id_y"]]
df_comb["time_hr"] = df_comb["time_hr_x"] * df_comb["time_hr_y"]
df_comb.drop(columns=["time_hr_x", "time_hr_y"]).set_index(["id_x", "id_y"])
#               time_hr
#id_x   id_y    
#1      4       0.000811
#       16      0.000135
#4      1       0.000811
#       16      0.000134
#16     1       0.000135
#       4       0.000134

如果你想有更多的蟒蛇代码,你可以自动将其

id_column = "id"
product_columns = ["time_hr"]
df_time = df[[id_column, *product_columns]]
df_comb = df_time.merge(df_time, how='cross')
df_comb = df_comb[df_comb[f"{id_column}_x"] != df_comb[f"{id_column}_y"]]
for column in product_columns:
df_comb[column] = df_comb[f"{column}_x"] * df_comb[f"{column}_y"]
df_comb.set_index([f"{id_column}_x", f"{id_column}_y"])
.drop(columns=[drop for column in product_columns for drop in [f"{column}_x", f"{column}_y"]])

PS。我不确定这是否是您想要实现的,如果不是,请为这3个输入行添加预期的输出数据。

相关内容

  • 没有找到相关文章

最新更新