【发布时间】:2019-12-22 12:34:51
【问题描述】:
我有以下数据框:
sp_id sp_dt v1 v1 v3
x1|x2|x30|x40 2018-10-07 100 200 300
x1|x2|x30|x40 2018-10-14 80 80 90
x1|x2|x30|x40 2018-10-21 34 35 36
x1|x2|x31|x41 2018-10-07 100 200 300
x1|x2|x31|x41 2018-10-14 80 80 90
x1|x2|x31|x41 2018-10-21 34 35 36
....
x1|x2|x39|x49 2018-10-21 340 350 36
以及一个包含以下数据的excel文件(并且excel中的每张表可能包含多个变量,如下图所示的v4、v5,可能在另一张表中包含v6):
Variable sp_partid1 sp_partid2 2018-10-07 ... 2018-10-21
v4 x30 x40 160 ... 154
v4 x31 x41 59 ... 75
....
v4 x39 x49 75 ... 44
v5 x30 x40 16 ... 24
v5 x31 x41 59 ... 79
....
v5 x39 x49 75 ... 34
sp_partid1 和 sp_partid2 是可选列。它们是顶部数据框中的“sp_id 的一部分”列。该文件可以没有,或者在这个特定的示例中,最多有 4 个这样的列,每个列都是顶部数据框中 sp_id 列的一部分。
最终输出应如下所示:
sp_id sp_dt v1 v1 v3 v4 v5
x1|x2|x30|x40 2018-10-07 100 200 300 160 16
x1|x2|x30|x40 2018-10-14 80 80 90 ... ...
x1|x2|x30|x40 2018-10-21 34 35 36 154 24
x1|x2|x31|x41 2018-10-07 100 200 300 59 59
x1|x2|x31|x41 2018-10-14 80 80 90 ... ...
x1|x2|x31|x41 2018-10-21 34 35 36 75 79
....
x1|x2|x39|x49 2018-10-21 340 350 36 44 34
Edit1 开始: 输出是如何产生的?
get a list of variables
check if the variable(say v4 in this case) exists in any sheet
if it does:
does it have any "part of sp_id"
#In the example shown sp_partid1 and sp_partid2 of excel sheets
#are part of sp_id of dataframe.
if yes:
#it means the part of sp_id is common for all values. (x1|x2) in this case.
add a new column to dataframe, v4, which has sp_id, sp_dt and,
the value of that date
if no:
#it means the whol sp_id is common for all values. (x1|x2|x3|x4) in this case and not shown in example.
add a new column to dataframe, v4, and copy the value under the appropriate dates in excel sheet into corresponding v4 values and sp_dt
例如,160 是 2018-10-07 下 v4、x30、x40 的值,因此最终输出中的 v4 在第一行显示 160。
Edit1 结束:
我的代码开始于:
df # is the top data frame which I have not gotten around to using yet
var_value # gets values in a loop like 'v4, v5...'
sheets_dict = {name: pd.read_excel('excel_file.xlsx', sheet_name = name, parse_dates = True) for name in sheets}
for key, value in sheets_dict.items():
if 'Variable' in value.columns:
# 'Variable' column exists in this sheet
if var_value in value['Variable'].values:
# var_value exists in 'Variable' column (say, v4)
for column in value.columns:
if column.startswith('sp_'):
#Do something with column values, then map the values etc
【问题讨论】:
-
我真的不明白你在做什么。你的输出是如何产生的?我不明白它背后的逻辑。
-
我编辑了 OP 以反映有关如何生成输出的逻辑。