gpt4 book ai didi

python - 返回包含附加列的 pandas 数据框

转载 作者:行者123 更新时间:2023-11-30 22:46:36 25 4
gpt4 key购买 nike

我有一个数据帧列表,我想创建一个新的数据帧列表,该列表与前一个数据帧列表相同,但使用函数方法添加了一个列。

这是我的想法和我的尝试:

# I know this is possible. It will add a new column of True.
df['new_column'] = True

# However, it edits the dataframe, I want a functional approach that returns a new modified dataframe.
# Something like this.
df + ('new_column', True)

# Then I can use it in list comprehension. consider dfs is a list of dataframes.
[df + ('new_column', True) for df in dfs]

最佳答案

使用assign()方法:

[df.assign(new_column = True) for df in dfs]

关于python - 返回包含附加列的 pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40824863/

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