gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:33:19 24 4
gpt4 key购买 nike

我有以下 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]

最佳答案

使用 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)')

关于Python:使用其他列将值分配给 Pandas 中的新列作为列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50852672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com