一个基于列值的新列中对应列的添加值

Posted

技术标签:

【中文标题】一个基于列值的新列中对应列的添加值【英文标题】:one Column value based add value of corresponding column in new column 【发布时间】:2019-08-06 01:52:27 【问题描述】:

我有 2 个两个数据框 df1 和 df2 ,在 df2 我有 4 列。我想如果 df2 column1 值为 0 ,代码应在 df1 中添加相应的 3 列值,列名为 col2_0 、col3_0 和 col4_0 (注意:此过程也需要为值 -1、-2、-3、-4 执行, -5),如果可以解决这个问题,但我正在寻找熊猫简单快捷的方法来处理这个问题

这里是 df2

【问题讨论】:

您的问题缺少详细信息。请阅读此主题:***.com/questions/20109391/… 【参考方案1】:

我将使用一个最初为空的 df1 和一些额外的行来做这个例子:

df2 = pd.DataFrame('#timestamp':[-5,-4,-3,-2,-1,0],
                    'grid_U1':   [413.714,413.797,413.926,414.037,414.066,414.064],
                    'grid_U2':   [415.796,415.909,416.117,416.093,416.163,416.183],
                    'grid_U3':   [416.757,416.853,417.09,417.158,417.175,417.085])

df1 = pd.DataFrame(index=range(0,10), columns=['col2_0','col3_0','col4_0'])

如果你想匹配行索引(从 df2 中的给定行号复制到 df1 中的相同行号),那么你可以使用这个:

In [403]: df1[['col2_0','col3_0','col4_0']] = df2[df2['#timestamp'].isin(range(-5,1))][['grid_U1','grid_U2','grid_U3']]

In [404]: df1
Out[404]: 
    col2_0   col3_0   col4_0
0  413.714  415.796  416.757
1  413.797  415.909  416.853
2  413.926  416.117  417.090
3  414.037  416.093  417.158
4  414.066  416.163  417.175
5  414.064  416.183  417.085
6      NaN      NaN      NaN
7      NaN      NaN      NaN
8      NaN      NaN      NaN
9      NaN      NaN      NaN

我将通过选择顶部未出现的时间戳值来确认这是匹配的行号:

In [405]: df1[['col2_0','col3_0','col4_0']] = df2[df2['#timestamp'].isin([-3,-1])][['grid_U1','grid_U2','grid_U3']]

In [406]: df1
Out[406]: 
    col2_0   col3_0   col4_0
0      NaN      NaN      NaN
1      NaN      NaN      NaN
2  413.926  416.117  417.090
3      NaN      NaN      NaN
4  414.066  416.163  417.175
5      NaN      NaN      NaN
6      NaN      NaN      NaN
7      NaN      NaN      NaN
8      NaN      NaN      NaN
9      NaN      NaN      NaN

如果您想改为从 df1 的顶部填写,您可以在末尾添加对 reset_index 的调用(您需要 drop=True 以避免在其中添加额外的索引列):

In [412]: df1[['col2_0','col3_0','col4_0']] = df2[df2['#timestamp'].isin([-3,-1])][['grid_U1','grid_U2','grid_U3']].reset_index(drop=True)

In [413]: df1
Out[413]: 
    col2_0   col3_0   col4_0
0  413.926  416.117  417.090
1  414.066  416.163  417.175
2      NaN      NaN      NaN
3      NaN      NaN      NaN
4      NaN      NaN      NaN
5      NaN      NaN      NaN
6      NaN      NaN      NaN
7      NaN      NaN      NaN
8      NaN      NaN      NaN
9      NaN      NaN      NaN

【讨论】:

以上是关于一个基于列值的新列中对应列的添加值的主要内容,如果未能解决你的问题,请参考以下文章

如何访问 pandas 数据框列中的字典元素并对其进行迭代以创建填充有各自值的新列?

在配置单元中添加具有恒定值的新列

Scala DataFrame,将非空列的值复制到新列中

Pandas - 在保留原始列的同时创建具有替换值的新列

PySpark - 根据另一列值的降序添加递增的整数排名值

基于字典键创建具有值的新列