Python:使用其他列将Pandas中的新列的值分配为列表

时间:2018-06-14 08:12:56

标签: python pandas

我有以下pandas数据帧:

Name1   Name2   Score1   Score2   
Bruce   Jacob    3        4
Aida    Stephan  0        1 

我想在数据框“list_score”中创建一个新列,这是一个得分1和2的列表

预期结果:

Name1   Name2   Score1   Score2  list_score 
Bruce   Jacob    3        4        [3,4]
Aida    Stephan  0        1        [0,1]

3 个答案:

答案 0 :(得分:3)

使用zip将转换元组转换为列表:

df['list_score'] = [list(x) for x in zip(df['Score1'], df['Score2'])]

或者:

df['list_score'] = list(map(list, zip(df['Score1'], df['Score2'])))
print (df)
   Name1    Name2  Score1  Score2 list_score
0  Bruce    Jacob       3       4     [3, 4]
1   Aida  Stephan       0       1     [0, 1]

性能:

df = pd.concat([df] * 1000, ignore_index=True)

In [105]: %timeit df['list_score'] = [list(x) for x in zip(df['Score1'], df['Score2'])]
851 µs ± 36.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [106]: %timeit df['list_score'] = list(map(list, zip(df['Score1'], df['Score2'])))
745 µs ± 35.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [107]: %timeit df['list_score'] = df[['Score1', 'Score2']].apply(tuple, axis=1).apply(list)
35.5 ms ± 295 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [108]: %timeit df['list_score'] = df[['Score1', 'Score2']].values.tolist()
949 µs ± 105 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

plot1

这是用于生成上述perfplot的设置:

def list_comp(df):
    df['list_score'] = [list(x) for x in zip(df['Score1'], df['Score2'])]
    return df

def map_list(df):
    df['list_score'] = list(map(list, zip(df['Score1'], df['Score2'])))
    return df

def apply(df):
    df['list_score'] = df[['Score1', 'Score2']].apply(tuple, axis=1).apply(list)
    return df

def values(df):
    df['list_score'] = df[['Score1', 'Score2']].values.tolist()
    return df


def make_df(n):
    df = pd.DataFrame(np.random.randint(10, size=(n, 2)), columns=['Score1','Score2'])
    return df

perfplot.show(
    setup=make_df,
    kernels=[list_comp, map_list, apply, values],
    n_range=[2**k for k in range(2, 15)],
    logx=True,
    logy=True,
    equality_check=False,  # rows may appear in different order
    xlabel='len(df)')

答案 1 :(得分:2)

一种方法是使用pd.DataFrame.apply转换为tuple,然后转换为list。如果tuple足够,则可以省略第二部分。

df['list_score'] = df[['Score1', 'Score2']].apply(tuple, axis=1).apply(list)

print(df)

   Name1    Name2  Score1  Score2 list_score
0  Bruce    Jacob       3       4     [3, 4]
1   Aida  Stephan       0       1     [0, 1]

答案 2 :(得分:2)

df['list_score'] = df[['score1', 'score2']].values.tolist()