python - 按条件删除行并填充 pandas 数据框中的新列

标签 python pandas dataframe

我想对原始数据帧中的第6行进行条件选择

原始数据框:

      B1   B2   B3  B4        BCS  ULCA             MIMO
 3   26A   1A                 0,1     .               1A
 4   28A   1A                 0,1     .               1A
 5   19A   3A   1A              0     .           1A, 3A
 6    3A   1A                 0,1     .    1A, 3A, 1A-3A

步骤1。对 BCSMIMO

进行行扩展
         B1   B2   B3  B4  BCS ULCA    MIMO  
  4     26A   1A            0    .      1A    
  5     26A   1A            1    .      1A  
  6     28A   1A            0    .      1A 
  7     28A   1A            1    .      1A   
  8     19A   3A   1A       0    .      1A  
  9     19A   3A   1A       0    .      3A   
  10     3A   1A            0    .      1A   
  11     3A   1A            1    .      1A   
  12     3A   1A            0    .      3A    
  13     3A   1A            1    .      3A 
  14     3A   1A            0    .   1A-3A   
  15     3A   1A            1    .   1A-3A   

Step.2 然后将列B1-B4MIMO对比,如果相等:则将4放入新列(Bx_m) ,如果没有,则输入 2

cols = ['B1','B2','B3','B4']
arr = np.where(b[cols].eq(b['MIMO'], axis=0), '4','2')
b = b.join(pd.DataFrame(arr, columns=cols, index=b.index).add_suffix('_m'))


      B1   B2   B3  B4  BCS ULCA    MIMO  B1_m  B2_m  B3_m  B4_m
4    26A   1A             0    .      1A    2     4     2    2
5    26A   1A             1    .      1A    2     4     2    2
6    28A   1A             0    .      1A    2     4     2    2
7    28A   1A             1    .      1A    2     4     2    2
8    19A   3A   1A        0    .      1A    2     2     4    2
9    19A   3A   1A        0    .      3A    2     4     2    2
10    3A   1A             0    .      1A    2     4     2    2
11    3A   1A             1    .      1A    2     4     2    2
12    3A   1A             0    .      3A    4     2     2    2
13    3A   1A             1    .      3A    4     2     2    2
14    3A   1A             0    .   1A-3A    2     2     2    2
15    3A   1A             1    .   1A-3A    2     2     2    2
<小时/>

要求

但这里对原始数据帧中第 6 行 的格式有一个特殊要求。
规则:
MIMO中的每个值交替填入对应Bx_m中的4
如果两个值同时存在(1A-3A),则只需在Bx_m中同时填写4即可

即:
如果值格式类似于 MIMO 列中的 1A, 3A, 1A-3A(而不是 1A, 3A)
那么输出只需要保留Step.1中的1A-3A即可
并在Step.2中的B1_m和B2_n列中同时填写4

原始数据:

      B1   B2   B3  B4        BCS  ULCA             MIMO
 6    3A   1A                 0,1     .    1A, 3A, 1A-3A

原始输出(想要更改):(6行)

          B1   B2   B3  B4  BCS ULCA    MIMO  B1_m  B2_m  B3_m  B4_m
    10    3A   1A             0    .      1A    2     4     2    2
    11    3A   1A             1    .      1A    2     4     2    2
    12    3A   1A             0    .      3A    4     2     2    2
    13    3A   1A             1    .      3A    4     2     2    2
    14    3A   1A             0    .   1A-3A    2     2     2    2
    15    3A   1A             1    .   1A-3A    2     2     2    2

需要目标:(仅2行。B1_m和B2_m均填写4)

          B1   B2   B3  B4  BCS ULCA    MIMO  B1_m  B2_m  B3_m  B4_m
    14    3A   1A             0    .   1A-3A    4     4     2    2
    15    3A   1A             1    .   1A-3A    4     4     2    2

请帮我解决一下。谢谢。

<小时/>

更新

df = pd.concat([b1.set_index('index'),b2.set_index('index')]).sort_index()
print(df)


        B1   B2   B3  B4 BCS ULCA    MIMO B1_m B2_m B3_m B4_m
index                                                        
0      42A  19A            0    .       .    2    2    2    2
1      18A   1A            0    .      1A    2    4    2    2
10      3A   1A            0    .      3A    4    2    2    2
100    41A  28A   3A       0    .      3A    2    2    4    2
101    41A  28A   3A       0    .     41A    4    2    2    2
102    42A  28A   3A       0    .      3A    2    2    4    2
103    42A  41A   3A       0    .      3A    2    2    4    2
104    42A  41A   3A       0    .     41A    2    4    2    2
105    41C   3A            0    .      3A    2    4    2    2
106    41C   3A            0    .     41C    4    2    2    2
107    41C   3A            0    .  3A-41C    4    4    2    2
108    42C   3A            0    .      3A    2    4    2    2
109    42C  41A            0    .     41A    2    4    2    2
11      3A   1A            1    .      3A    4    2    2    2

最佳答案

用途:

from  itertools import product
#convert index to strings and then to column for last sorting by index - proper ordering
df = df.rename(str).reset_index()
#check if - in column MIMO
m = df['MIMO'].str.contains('-').copy()

#solution process only rows with - filtered by boolene indexing
df1 = df[m].fillna('').apply(lambda x: x.str.split(',\s*'))

b = pd.DataFrame([j for i in df1.values for j in product(*i)], columns=df1.columns)
#remove non - rows
b1 = b[b['MIMO'].str.contains('-')].copy()
print (b1)
  index  B1  B2 B3 B4 BCS ULCA   MIMO
2     6  3A  1A         0    .  1A-3A
5     6  3A  1A         1    .  1A-3A

#check substrings per rows
b1['B1_m'] = np.where([i in j for i, j in zip(b1['B1'], b1['MIMO'])], '4', '2')
b1['B2_m'] = np.where([i in j for i, j in zip(b1['B2'], b1['MIMO'])], '4', '2')
b1['B3_m'] = np.where(b1['B3'] == b1['MIMO'], '4', '2')
b1['B4_m'] = np.where(b1['B4'] == b1['MIMO'], '4', '2')
print (b1)
  index  B1  B2 B3 B4 BCS ULCA   MIMO B1_m B2_m B3_m B4_m
2     6  3A  1A         0    .  1A-3A    4    4    2    2
5     6  3A  1A         1    .  1A-3A    4    4    2    2
<小时/>
#processes rows with no -
df2 = df[~m].fillna('').apply(lambda x: x.str.split(',\s*'))

b2 = pd.DataFrame([j for i in df2.values for j in product(*i)], columns=df2.columns)
print (b2)
  index   B1  B2  B3 B4 BCS ULCA MIMO
0     3  26A  1A          0    .   1A
1     3  26A  1A          1    .   1A
2     4  28A  1A          0    .   1A
3     4  28A  1A          1    .   1A
4     5  19A  3A  1A      0    .   1A
5     5  19A  3A  1A      0    .   3A

cols = ['B1','B2','B3','B4']
arr = np.where(b2[cols].eq(b2['MIMO'], axis=0), '4','2')
b2 = b2.join(pd.DataFrame(arr, columns=cols, index=b2.index).add_suffix('_m'))
print (b2)
  index   B1  B2  B3 B4 BCS ULCA MIMO B1_m B2_m B3_m B4_m
0     3  26A  1A          0    .   1A    2    4    2    2
1     3  26A  1A          1    .   1A    2    4    2    2
2     4  28A  1A          0    .   1A    2    4    2    2
3     4  28A  1A          1    .   1A    2    4    2    2
4     5  19A  3A  1A      0    .   1A    2    2    4    2
5     5  19A  3A  1A      0    .   3A    2    4    2    2
<小时/>
#join together, convert index values to integers and sorting
df = pd.concat([b1.set_index('index'), b2.set_index('index')]).rename(int).sort_index()
print (df)
        B1  B2  B3 B4 BCS ULCA   MIMO B1_m B2_m B3_m B4_m
index                                                    
3      26A  1A          0    .     1A    2    4    2    2
3      26A  1A          1    .     1A    2    4    2    2
4      28A  1A          0    .     1A    2    4    2    2
4      28A  1A          1    .     1A    2    4    2    2
5      19A  3A  1A      0    .     1A    2    2    4    2
5      19A  3A  1A      0    .     3A    2    4    2    2
6       3A  1A          0    .  1A-3A    4    4    2    2
6       3A  1A          1    .  1A-3A    4    4    2    2

关于python - 按条件删除行并填充 pandas 数据框中的新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53150472/

相关文章:

python - Docker Compose 和 Postgres 连接被拒绝

python - 使用多索引附加两个数据帧

r - 如何在 R 中使用三个 data.frames 创建双种子 "if"循环?

Python - 遇到 x_test y_test 拟合错误

Python:将代码应用于整个数据框列

dataframe - 删除相同的行,以便在 Julia DataFrames 中对一列中的值进行求和

python - 用 Python 进行费马分解

python - 如何将 base64 字符串作为 Python 脚本的参数传递?

python - 简短的 Python 代码说 "Pick the lower value"?

python - Pandas :如何分组并计算给定列中的唯一性?