Pandas 优化-将集合类型的列拆分为多个列

Pandas 优化-将集合类型的列拆分为多个列,pandas,Pandas,我想基于columnCol1的元素创建新列,该列的类型为set。每个元素都有一个对应的列名,该列名存储在dict中。以下是完整代码: import numpy as np import pandas as pd np.random.seed(123) N = 10**4 #number of rows in the dataframe df = pd.DataFrame({'Cnt': np.random.randint(2,10,N)}) # generate lists of ra

我想基于column
Col1
的元素创建新列,该列的类型为
set
。每个元素都有一个对应的列名,该列名存储在dict中。以下是完整代码:

import numpy as np
import pandas as pd

np.random.seed(123)

N = 10**4 #number of rows in  the dataframe

df = pd.DataFrame({'Cnt': np.random.randint(2,10,N)})

# generate lists of random length
def f(x):
    return set(np.random.randint(101,120,x))
df['Col1'] = df['Cnt'].apply(f)

# dictionary with column names for each element in list
d = {'Item_1':101, 'Item_2':102, 'Item_3':103, 'Item_4':104, 'Item_5':105, 'Item_6':106, 'Item_7':107, 'Item_8':108, 
        'Item_9':109, 'Item_10':110, 'Item_11':111, 'Item_12':112, 'Item_13':113, 'Item_14':114, 'Item_15':115, 'Item_16':116, 
        'Item_17':117, 'Item_18':118, 'Item_19':119, 'Item_20':120}

def elem_in_set(x,e):
    return 1 if e in x else 0

def create_columns(input_data, d):
    df = input_data.copy()
    for k, v in d.items():
        df[k] = df.apply(lambda x: elem_in_set(x['Col1'], v), axis=1)
    return df

%timeit create_columns(df, d)
#5.05 s ± 78.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
问题是,生产数据帧大约有40万行,而我的解决方案根本不能很好地扩展—我的机器上大约需要10分钟。包含所有元素(
Col1
)的列可以是类型
list
,而不是
set
,但这不会提高性能。

有没有更快的解决方案?

我对您的
创建列
应用
做了一个小改动。看起来它现在工作得快多了

import numpy as np
import pandas as pd

np.random.seed(123)

N = 10**4 #number of rows in  the dataframe

df = pd.DataFrame({'Cnt': np.random.randint(2,10,N)})

# generate lists of random length
def f(x):
    return set(np.random.randint(101,120,x))
df['Col1'] = df['Cnt'].apply(f)

# dictionary with column names for each element in list
d = {'Item_1':101, 'Item_2':102, 'Item_3':103, 'Item_4':104, 'Item_5':105, 'Item_6':106, 'Item_7':107, 'Item_8':108, 
        'Item_9':109, 'Item_10':110, 'Item_11':111, 'Item_12':112, 'Item_13':113, 'Item_14':114, 'Item_15':115, 'Item_16':116, 
        'Item_17':117, 'Item_18':118, 'Item_19':119, 'Item_20':120}

def create_columns(input_data, d):
    df = input_data.copy()
    for k, v in d.items():
        df[k] = df.Col1.apply(lambda x: 1 if v in x else 0)
    return df

create_columns(df, d)
#191 ms ± 15.6 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)