Python Pandas:将所有其他列值附加到前3列

时间:2019-08-30 03:09:25

标签: python pandas

我试图将所有列值附加到前三列,但找不到任何方法。

输入:

cv              cv      new_col       mg                mg  new_col sa              sa   new_col
5g              5g      cv            0% zinsen         zin mg      a-series-owner  ase  sa
2xopticalcam2x  zoom     cv           24 hour battery   hba mg      all             all  sa

预期输出:

cv          cv   new_col        
5g              5g       cv           
2xopticalcam2x  zoom     cv           
0% zinsen       zin      mg
24 hour battery hba      mg
a-series-owner  ase      sa
all             all      sa

我的方法:

oneCol = []
colLength = len(df)
for k in range(colLength):
    oneCol.append(df[k])

combined = pd.concat(oneCol, ignore_index=True)

print(combined)

但是我没有得到预期的输出。

2 个答案:

答案 0 :(得分:1)

IIUC

std::vector<std::string>

HDESK activeDesktop = OpenInputDesktop(0, FALSE, DESKTOP_READOBJECTS | DESKTOP_JOURNALPLAYBACK | DESKTOP_JOURNALRECORD);

    if (activeDesktop)
    {
        WCHAR desktopName[MAX_PATH];
        DWORD lengthNeeded;
        if (GetUserObjectInformation(activeDesktop, UOI_NAME, desktopName, ARRAYSIZE(desktopName), &lengthNeeded))
        {
            OutputDebugString(desktopName);
            OutputDebugString(L"\r\n");
        }
        else
        {
            OutputDebugString(L"Failed 1\r\n");
        }
        CloseDesktop(activeDesktop);
    }
    else
    {
        OutputDebugString(L"Failed 2\r\n");
    }

答案 1 :(得分:1)

您可以通过创建previous answer的默认列名称来更改DataFrame.set_axis

df = pd.concat([v.set_axis(range(len(v.columns)), inplace=False, axis=1).assign(new_col=k)
                 for k, v in df.groupby(axis=1, level=0)], axis=0)

或者如果每组总是3个值:

df1 = pd.DataFrame(df.values.reshape(-1, 3))
相关问题