将列作为副本添加到Pandas DataFrame [英] Adding a column to a Pandas DataFrame as a copy

查看:65
本文介绍了将列作为副本添加到Pandas DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 original 的pandas DataFrame,我想为其添加一个新列,并将结果DataFrame保存在一个名为 modified 的变量中.我怎么做?

I have a pandas DataFrame called original and I would like to add a new column to it and save the resultant DataFrame in a variable called modified. How do I do that?

import pandas as pd
import numpy as np
original = pd.DataFrame(np.random.randn(5, 2), columns=['a', 'b'])

此处非常相似的问题中给出的解决方案是执行以下操作:

The solution given in the very similarly named questions here is to do something like:

original['c'] = original['b'].abs()

这对我不起作用,因为它修改了原始数据框.一种可能的解决方案是使用联接,但这不允许我命名它,也不允许它用标量值填充:

This does not work for me because it modifies the original DataFrame. A potential solution is to use join, but that does not allow me to name it nor does it allow it be filled with a scalar values:

modified = original.join(original['b'].abs(),rsuffix='_abs')

目的是能够在没有临时变量的情况下将列添加到一行中,以达到以下效果:

The aim is to able to add the column in a single line without temp variables to achieve the following effect:

modified = original.some_op() \
    .a_different_op() \
    .add_a_column() \ # <- the step I can't figure out
    .another_op() \
    .final_op()

推荐答案

使用pandas.DataFrame.assign方法在此处进行描述

Use pandas.DataFrame.assign method it is described here http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.assign.html

这篇关于将列作为副本添加到Pandas DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆