我的数据在pandas dataFrame中,如下所示
Employee ID Employee Name Day 1 Day 2 ... Day 30 Day 31
0 1 EMPLOYEE1 8.75 8.75 ... 8.75 8.75
1 2 EMPLOYEE2 0.00 8.00 ... 8.00 8.00
我需要如下输出:
1 Day 1 EMPLOYEE1 8.75
1 Day 2 EMPLOYEE1 8.75
.
.
1 Day 31 EMPLOYEE1 8.75
2 Day 1 EMPLOYEE2 0.00
2 Day 2 EMPLOYEE2 8.00
.
.
2 Day 31 EMPLOYEE2 8.00
这是代码
# Copy ID and Name to new dataFrame
df_EID = df[['Employee ID', 'Employee Name']].copy()
# transpose the and copy the original dataframe to a new one
df_transpose = df.transpose()
# make necessary changes to the transpose so that the column headers are the
# Employee IDs and also delete the first index which has those IDs
df_transpose = df_transpose.rename(columns=df_transpose.iloc[0])
df_transpose.drop(df_transpose.index[0])
# run Query to check just for the first employee
df_transpose[df_EID['Employee ID'][0:]]
#iterate
预期结果
1 Day 1 EMPLOYEE1 8.75
1 Day 2 EMPLOYEE1 8.75
.
.
1 Day 31 EMPLOYEE1 8.75
2 Day 1 EMPLOYEE2 0.00
2 Day 2 EMPLOYEE2 8.00
.
.
2 Day 31 EMPLOYEE2 8.00
.
.
实际结果
1 ... 100
Employee ID 1 ... 100
Employee Name EMPLOYEE1 ... EMPLOYEE100
Day 1 8.75 ... 8.75
.
.
.
Day 30 8.75 ... 8.75
Day 31 8.75 ... 8.75
Total Hours 188.25 ... 191.5
看来我实际上要回到正题了。你能帮我吗?您还能指出我的方法中也出错了吗?提前致谢。 如果将输出放到另一个数据框中,这样可以更好地将其写为与预期输出相同的方式,那就更好了