复制将数据列移动到新行的数据框

时间:2018-09-03 13:51:28

标签: python pandas dataframe

我有以下DataFrame:

test_data_frame = pd.DataFrame({"cod1":[1,3,5,7],"cod2":[2,4,6,8],"test":["a","b","c","d"]})

enter image description here

我想从“ cod1”下面的“ cod2”列中移动值,像这样在“ test”中保持相同的值:

enter image description here

我现在的解决方案是遍历DataFrame并将新订单保存到字典中:

但这似乎不是最佳解决方案:

new_frame = {"cod":[],"test":[]}
for row in test_data_frame.itertuples():
    a,cod1,cod2,test = ((row))
    new_frame["cod"].append(cod1)
    new_frame["test"].append(test)
    new_frame["cod"].append(cod2)
    new_frame["test"].append(test)

pd.DataFrame(new_frame)

0 个答案:

没有答案