如何向 pandas 中的数据框行添加列表? [重复]

问题描述 投票:0回答:1

在以下代码中,

formation_top
变量具有地层名称及其顶部深度:

formation_top = pd.DataFrame('Formation': ['Balakhany', 'Balakhany X', 'Pereriv A', 'Pereviv B'],'Depth': [2200, 2250, 2300, 2340])

    Formation     Depth
0   Balakhany     2200
1   Balakhany X   2250
2   Pereriv A     2300
3   Pereriv B     2340

formation_top
数据框显示“Balakhany”是从2200到2250,“Balakhany X”是从2250到2300,依此类推。我还有
fluidcode
数据框有“深度”列,范围从 2200 到 2400,增量为 1.0 m 和“流体代码”列,其中包含每个深度的唯一流体代码。

        FLUIDCODE     Depth
    0   3             2200
    1   3             2201
    2   4             2202
    .   .              .
    .   .              .
   149  2             2338
   150  7             2339
   151  np.nan        2340

我想为每个地层深度间隔提取唯一的流体代码,并根据每个地层将这些唯一流体代码列表附加到

formation_top
数据框中的新列。我尝试了以下代码:

self.formation_top['FLUIDCODES'] = np.nan

for i in range(len(self.formation_top) - 1):
    depth_condition = (self.fluidcode['Depth'] >= self.formation_top['Depth'][i]) & (self.fluidcode['Depth'] <= self.formation_top['Depth'][i + 1])
    unique_flcodes = self.fluidcode.loc[depth_condition, 'FLUIDCODE'].unique()
    unique_flcodes = pd.Series([unique_flcodes])
    self.formation_top.at[i, 'FLUIDCODES'] = unique_flcodes

但我收到错误为

ValueError: Incompatible indexer with Series

我想看到如下输出:

    Formation     Depth  FLUIDCODES
0   Balakhany     2200   3, 4, 6
1   Balakhany X   2250   5
2   Pereriv A     2300   np.nan
3   Pereriv B     2340   7, 2

你会怎么做?

python pandas numpy group-by
1个回答
0
投票

IIUC,您可以使用

merge_asof
,然后使用
groupby.agg
map
:

s = (pd.merge_asof(fluidcode, formation_top)
       .dropna()
       .groupby('Formation')['FLUIDCODE']
       .agg(lambda x: ', '.join(str(int(i)) for i in  dict.fromkeys(x) if pd.notna(i)))
     )

formation_top['FLUIDCODES'] = formation_top['Formation'].map(s)

输出:

     Formation  Depth FLUIDCODES
0    Balakhany   2200       3, 4
1  Balakhany X   2250        NaN
2    Pereriv A   2300       2, 7
3    Pereviv B   2340        NaN
© www.soinside.com 2019 - 2024. All rights reserved.