使用@azro 代码并仅修改 for 的索引。它不会标记我的错误答案,代码工作正常,但我仍然没有得到预期的结果,我把我的完整代码。
arch is dataframe
channels=['ch1','ch1']
targets=['op1','op2']
l=[]
m = []
n = []
for ch in channels:
for tar in targets:
for j in range(1,22):
df=arch[(arch.Año.isin(año)) & (arch['Channel'] == ch) & (rcha['Week'] == j)]
l.append(df[['time',tar]].set_index('time').rename(columns={'time' : 'time' + str(j)}))
m.append(l)
n.append(m)
a = []
for i in range(len(n)):
for j in range(len(m)):
for k in range(len(l)):
a.append(n[i][j][k])
a = pd.concat(a, axis=1).reset_index()
通过打印“a”我得到
time | op1 | op1 |...| op2 | op2 |..| op1 | op1 | op2 |..| op2
1 | 0.2 | 0.1 | | 0.2 | 0.1 |..| 0.1 | 0.1 | 0.2 |..| 0.8
2 | 0.3 | 0.4 | | 0.1 | 0.3 |..| 0.2 | 0.7 | 0.9 |..| 0.3
3 | 0.7 | 0.8 | | 0.9 | 0.11|..| 0.4 | 0.8 | 0.7 |..| 0.8
我是这样的,因为我在“通道”中有两个元素,在“目标”中有两个元素,我需要为“ch1 with op1”、“ch1 with op2”、“ch2 with op1”、“ch2”打印一个表与 op2”。
for ch1
time | op1 | op1 |..
1 | 0.2 | 0.1 |..
2 | 0.3 | 0.4 |..
3 | 0.7 | 0.8 |
time | op2 | op2 |..
1 | 0.2 | 0.1 |..
2 | 0.1 | 0.3 |..
3 | 0.9 | 0.11|..
for ch2
time | op1 | op1 | ..
1 | 0.1 | 0.1 | ..
2 | 0.2 | 0.7 | ..
3 | 0.4 | 0.8 | ..
time | op2 |..| op2
1 | 0.2 |..| 0.8
2 | 0.9 |..| 0.3
3 | 0.7 |..| 0.8
繁星点点滴滴
蓝山帝景
杨__羊羊
相关分类