python - 使用另一列上的拆分有条件地填充新列

标签 python pandas dataframe split

我有一个数据框

df = pd.DataFrame({'col1': [1,2,1,2], 'col2': ['aa bb cc', 'ee-ff-gg', 'hh ii kk', 'll-mm-nn']})

我想要:

  • 在“”上拆分 col2,其中 col1==1
  • 在“-”处拆分,其中 col1==2
  • 将此数据附加到 3 个新列:(col20、col21、col22)

理想情况下,代码如下所示:

subdf=df.loc[df['col1']==1]
#list of columns to use
col_list=['col20', 'col21', 'col22']
#append to dataframe new columns from split function
subdf[col_list]=(subdf.col2.str.split(' ', 2, expand=True)

但这并没有奏效。

我尝试过使用合并加入,但是:

  • 如果列已填充,则连接不起作用
  • 如果不是,则合并不起作用。

我也尝试过:

#subset dataframes
subdf=df.loc[df['col1']==1]
subdf2=df.loc[df['col1']==2]

#trying the join method, only works if columns aren't already present
subdf.join(subdf.col2.str.split(' ', 2, expand=True).rename(columns={0:'col20', 1:'col21', 2: 'col22'}))
#merge doesn't work if columns aren't present
subdf2=subdf2.merge(subdf2.col2.str.split('-', 2, expand=True).rename(columns={0:'col20', 1:'col21', 2: 'col22'}))
subdf2

运行时的错误消息:

subdf2=subdf2.merge(subdf2.col2.str.split('-', 2, expand=True).rename(columns={0:'col20', 1:'col21', 2: 'col22'})

MergeError: No common columns to perform merge on. Merge options: left_on=None, right_on=None, left_index=False, right_index=False

编辑马克对正则表达式的评论后给出的信息

我原来的 col1 实际上是我用来从某些字符串中提取 col2 的正则表达式组合。

#the combination I used to extract the col2
combinations= ['(\d+)[-](\d+)[-](\d+)[-](\d+)', '(\d+)[-](\d+)[-](\d+)'... ]

这是原始数据框

col1                          col2 
(\d+)[-](\d+)[-](\d+)[-](\d+) 350-300-50-10 
(\d+)[-](\d+)[-](\w+)(\d+)    150-180-G31   

然后我创建了一个字典,将每个组合连接到 col2 的分割值所代表的内容:

filtermap={'(\d+)[-](\d+)[-](\w+)(\d+)': 'thickness temperature sample', '(\d+)[-](\d+)[-](\d+)[-](\d+)': 'thickness temperature width height' }

使用这个过滤器我想要:

  • 根据正则表达式组合对数据帧进行子集化
  • 在 col2 上使用 split 来查找与使用 filtermap 的组合相对应的值(厚度温度..)
  • 将这些值添加到数据框上的新列
col1                          col2           thickness temperature width length sample
(\d+)[-](\d+)[-](\d+)[-](\d+) 350-300-50-10  350       300         50    10
(\d+)[-](\d+)[-](\w+)(\d+)    150-180-G31    150       180                        G31   

既然您提到了正则表达式,也许您知道直接执行此操作的方法?

编辑2;输入输出

在输入中有这样的字符串:

'this is the first example string 350-300-50-10 ', 
'this is the second example string 150-180-G31'

格式为:

  • 数字-数字-数字-数字(350-300-50-10)中包含以下有序信息:厚度(350)-温度(300)-宽度(50)-长度(10)

  • 数字-数字-字母数字 (150-180-G31 ) 中包含以下有序信息:厚度-温度-样本

期望的输出:

col2,          thickness, temperature, width, length, sample 

350-300-50-10  350        300          50     10       None
150-180-G31    150        180          None   None     G31

我用过例如:

re.search('(\d+)[-](\d+)[-](\d+)[-](\d+)')) 

查找字符串中的 col2

最佳答案

您可以使用np.where来简化这个问题。

import pandas as pd
import numpy as np
df = pd.DataFrame({'col1': [1,2,1,2],
                   'col2': ['aa bb cc', 'ee-ff-gg', 'hh ii kk', 'll-mm-nn']
                   })


temp = np.where(df['col1'] == 1, #a boolean array/series indicating where the values are equal to 1.
                df['col2'].str.split(' '), #Use the output of this if True
                df['col2'].str.split('-') #Else use this.
                )

temp_df = pd.DataFrame(temp.tolist()) #create a new dataframe with the columns we need
#Output:
    0   1   2
0  aa  bb  cc
1  ee  ff  gg
2  hh  ii  kk
3  ll  mm  nn

现在只需将结果赋回原始 df 即可。您可以使用 concat 或 join,但简单的赋值也足够了。

df[[f'col2_{i}' for i in temp_df.columns]] = temp_df

print(df)
   col1      col2 col2_0 col2_1 col2_2
0     1  aa bb cc     aa     bb     cc
1     2  ee-ff-gg     ee     ff     gg
2     1  hh ii kk     hh     ii     kk
3     2  ll-mm-nn     ll     mm     nn
<小时/>

编辑:解决两个以上的条件分割

如果您需要两个以上的条件,np.where 仅设计用于二元选择。您可以选择“自定义”方法,该方法可在此处处理任意数量的拆分。

splits = [ ' ', '-', '---']
all_splits = pd.DataFrame({s:df['col2'].str.split(s).values for s in splits})
#Output:
                            -         ---
0  [aa, bb, cc]    [aa bb cc]  [aa bb cc]
1    [ee-ff-gg]  [ee, ff, gg]  [ee-ff-gg]
2  [hh, ii, kk]    [hh ii kk]  [hh ii kk]
3    [ll-mm-nn]  [ll, mm, nn]  [ll-mm-nn]

首先,我们在所有拆分上拆分df['col2'],而不进行扩展。现在,问题只是根据df['col1']

的值选择正确的list

我们可以使用 numpy 的高级索引来实现此目的。

temp = all_splits.values[np.arange(len(df)), df['col1']-1]

此后,步骤应与上面相同,从创建 temp_df

关于python - 使用另一列上的拆分有条件地填充新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57648959/

相关文章:

Python - 我无法停止程序运行

Python Sqlite 一次插入一个数字字符串

python - 通过列表理解的频率数据框?

python - 如何将数据框列的文本拆分为多列?

python - 如何通过另一个数组过滤 numpy 点数组

python - 替换Dataframe的某个索引值

python - 如何根据列值的长度过滤数据框行

python - 将 NaN 作为 value_counts() 的第一个值

python pandas groupby排序和连接

python - 使用 df.apply() 将带参数的函数应用于每一行