简体   繁体   English

添加包含一个值的新列

[英]Add new column with one value

I have the following dataframe: 我有以下数据帧:

a = pd.DataFrame([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]], columns=['a','b','c'])
a
Out[234]: 
    a   b   c
0   1   2   3
1   4   5   6
2   7   8   9
3  10  11  12

I want to add a column with only the last row as the mean of the last 2 values of column c . 我想添加一个只有最后一行的列作为列c的最后2个值的平均值。 Something like: 就像是:

    a   b   c   d
0   1   2   3  NaN
1   4   5   6  NaN
2   7   8   9  NaN
3  10  11  12 mean(9,12)

I tried this but the first part gives an error: 我试过了,但第一部分给出了一个错误:

a['d'].iloc[-1] = a.c.iloc[-2:].values.mean()

You can use .at to assign at a single row/column label pair: 您可以使用.at在单个行/列标签对上进行分配:

ix = a.shape[0]
a.at[ix-1,'d'] = a.loc[ix-2:ix, 'c'].values.mean()

    a   b   c     d
0   1   2   3   NaN
1   4   5   6   NaN
2   7   8   9   NaN
3  10  11  12  10.5

Also note that chained indexing (what you're doing with aciloc[-2:] ) is explicitly discouraged in the docs, given that pandas sees these operations as separate events, namely two separate calls to __getitem__ , rather than a single call using a nested tuple of slices. 还要注意链接索引 (你正在用aciloc[-2:]做什么)在文档中明确不鼓励,因为pandas将这些操作视为单独的事件,即对__getitem__两次单独调用,而不是使用a的单个调用。嵌套的切片元组。

You may set d column beforehand (to ensure assignment): 您可以预先设置d列(以确保分配):

In [100]: a['d'] = np.nan

In [101]: a['d'].iloc[-1] = a.c.iloc[-2:].mean()

In [102]: a
Out[102]: 
    a   b   c     d
0   1   2   3   NaN
1   4   5   6   NaN
2   7   8   9   NaN
3  10  11  12  10.5

We can use .loc , .iloc & np.mean 我们可以使用.loc.ilocnp.mean

a.loc[a.index.max(), 'd'] = np.mean(a.iloc[-2:, 2])

    a   b   c     d
0   1   2   3   NaN
1   4   5   6   NaN
2   7   8   9   NaN
3  10  11  12  10.5

Or just using .loc and np.mean : 或者只使用.locnp.mean

a.loc[a.index.max(), 'd'] = np.mean(a.loc[a.index.max()-1:, 'c'])

    a   b   c     d
0   1   2   3   NaN
1   4   5   6   NaN
2   7   8   9   NaN
3  10  11  12  10.5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于一个值添加新列 - Add a new column based on one value 熊猫:在数据框的最后一行添加一个具有单个值的新列 - Pandas: add a new column with one single value at the last row of a dataframe 从一列的值向DataFrame添加新列 - Add new columns to DataFrame from the value of one column Python:在DataFrame中,在新列中为另一列中具有最高值的行添加值,在第三列中添加相同的字符串 - Python: In DataFrame, add value in a new column for row with highest value in another column and string identical in a third one Pandas,如何将一行中的值与同一列中的所有其他行进行比较,并将其作为新列中的新行值添加? - Pandas, how to compare the value from one row with all other rows in the same column and add it as a new row value in a new column? 如何根据一个时间戳值 python 计算列中值的数量并将计数添加到新列 - How to count number of values in column based on one timestamp value python and add the count to new column 如何将一列的值除以前一行的列(而不是同一列),并将结果作为新维度添加到numpy数组中? - How to divide one columns value by a previous rows column(not the same column) and add the result as a new dimension to a numpy array? 对于列中的相似值,添加新列频率 - For similar value in column add new column frequence 在 Pandas 中拆分字符串值并将拆分值一个接一个地添加到新列中 - Split string value in Pandas & add to a new column the split values one after the other 如何转置并添加具有从其中一列引用的值的新列 - How can I transpose and add a new column with a value referred from one of the columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM