簡體   English   中英

將列表項拆分為單獨的列 - pandas 數據框

[英]Splitting list items into separate columns - pandas data-frame

我有看起來像這樣的初始 pandas 數據幀 - 每個單元格都是初始輸入值的列表

Python 腳本 - 獲取初始 dataframe - 就像 Ian Thompson 在這個答案中提到的那樣 -

import pandas as pd

df_out1 = pd.DataFrame({
    0: [
        [None, 'A', 'B', 'C', 'D'],
        [None, 'A1', 'B1', 'C1', 'D1'],
        [None, 'A2', 'B2', 'C2', 'D2'],
    ],
    1: [
        [None] * 5,
        [None] * 5,
        [None] * 5,
    ],
    2: [
        ['V', 'W', 'X', 'Y', 'Z'],
        ['V1', 'W1', 'X1', 'Y1', 'Z1'],
        ['V2', 'W2', 'X2', 'Y2', 'Z2'],
    ]
})

我想像這樣格式化它 - 對於每一行 - 列表中的每個項目 forms 一列並對所有重復/迭代執行此操作 -所需的 output

我的原始輸入數據集非常龐大 - 10,000 行和 40 列。 我在 python 腳本下執行 - 盡管它正在工作並提供所需的 output - 當我運行它 2000 行和 40 列時 - 運行時間接近 1800 秒,我認為這是更高的一面。

Python 腳本:df_out1 是初始數據幀

d = pd.DataFrame()
for x in range(len(df_out1)):
    for y in range(len(df_out1.columns)):
        d = d.append(pd.Series(df_out1[y][x]), ignore_index=True)
d.to_csv('inter_alm_output_' + str(time.strftime("%Y%m%d-%H%M%S")) + '.csv')

有沒有辦法在更短的時間內實現這一目標,換句話說,優化它?

如果這是您的起始 dataframe:

df = pd.DataFrame({
    0 : [
        [None, 'A', 'B', 'C', 'D'],
        [None, 'A1', 'B1', 'C1', 'D1'],
        [None, 'A2', 'B2', 'C2', 'D2'],
    ],
    1 : [
        [None]*5,
        [None]*5,
        [None]*5,
    ],
    2 : [
        ['V', 'W', 'X', 'Y', 'Z'],
        ['V1', 'W1', 'X1', 'Y1', 'Z1'],
        ['V2', 'W2', 'X2', 'Y2', 'Z2'],
    ]
})

您可以通過應用pd.Series並連接結果來重新格式化列。

print(pd.concat([
    df[i].apply(pd.Series) for i in df.columns
]).sort_index().reset_index(drop=True))

      0     1     2     3     4
0  None     A     B     C     D
1  None  None  None  None  None
2     V     W     X     Y     Z
3  None    A1    B1    C1    D1
4  None  None  None  None  None
5    V1    W1    X1    Y1    Z1
6  None    A2    B2    C2    D2
7  None  None  None  None  None
8    V2    W2    X2    Y2    Z2

另一種不使用pd.concat方法:

print(df.stack().reset_index(drop=True).apply(pd.Series))

      0     1     2     3     4
0  None     A     B     C     D
1  None  None  None  None  None
2     V     W     X     Y     Z
3  None    A1    B1    C1    D1
4  None  None  None  None  None
5    V1    W1    X1    Y1    Z1
6  None    A2    B2    C2    D2
7  None  None  None  None  None
8    V2    W2    X2    Y2    Z2

第一種方法在

  • 3.93 ms ± 154 µs per loop (mean ± std. dev. of 7 runs, 100 loops each

第二種方法完成

  • 2.34 ms ± 66.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

您的原始代碼在

  • 15 ms ± 340 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

IIUC,你可以得到你想要的結果。

輸入

                   group    count                       value
0   [None, A, B, C, D]      [None, None, None, None]    [v, w, x, y, z]
1   [None, A1, B1, C1, D1]  [None, None, None, None]    [v1, w1, x1, y1, z1]
2   [None, A2, B2, C2, D2]  [None, None, None, None]    [v2, w2, x2, y2, z2]

代碼

df1 = df.stack().droplevel(1).reset_index(name='col').drop('index',axis=1)
pd.DataFrame(df1['col'].values.tolist(), columns=['M','N','O','P','Q'])

Output

      M     N   O   P   Q
0   None    A   B   C   D
1   None    None    None    None    None
2   v   w   x   y   z
3   None    A1  B1  C1  D1
4   None    None    None    None    None
5   v1  w1  x1  y1  z1
6   None    A2  B2  C2  D2
7   None    None    None    None    None
8   v2  w2  x2  y2  z2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM