Lambda + Apply在Pandas中获得两个新专栏

时间:2018-12-03 12:24:08

标签: python pandas lambda apply

我的df看起来像:

Name                 Item
A,D,B,B,C,C      I1,I2,I3,I1,I2
X,Y,Z,Z,Z            I4,I1,I1,I88,I4 

预期输出:

Name            Item         Unique_Name  Unique_Item Count_Unique_name  Count_Unique_Item
A,D,B,B,C,C  I1,I2,I3,I1,I2    A,B,C,D      I1,I2,I3         4             3             
X,Y,Z,Z,Z   I4,I1,I1,I88,I4    X,Y,Z       I4,I1,I88         3             4

代码:

new_items_df['Unique_Name'] = new_items_df['Name'].apply(lambda x: set(x.lower().split(",")))
new_items_df['Unique_Item'] = new_items_df['Item'].apply(lambda x: set(x.lower().split(",")))
new_items_df['Count_Unique_Name'] = new_items_df['Unique_Modifier'].apply(lambda x: len(x))
new_items_df['Count_Unique_Item'] = new_items_df['Unique_Item'].apply(lambda x: len(x))

上面的代码工作正常,但我正在执行相同的操作,并且两次运行相同的代码。当我尝试合并第一两行代码时,如下所示:

new_items_df[['Unique_Name','Unique_Item']] = new_items_df[['Name','Item']].apply(lambda x: set(x.str.lower().str.split(",")),axis =1)
  

TypeError :(“不可散列的类型:'列表'“,'发生在索引0')

我也尝试使用.unique(),并尝试使用[]将该列表转换为列表,但是似乎没有任何效果,我遇到一个错误或另一个错误

总结:

那么,我可以将我的4行代码合并为1行吗?

2 个答案:

答案 0 :(得分:2)

您可以使用applymap处理标量:

c = ['Name','Item']
#python 3.6+ solution
c1 = [f'Unique_{x}' for x in c]
c2 = [f'Count_Unique_{x}' for x in c]
#python bellow 3.6
#c1 = ['Unique_{}'.format(x) for x in c]
#c2 = ['Count_Unique_{}'.format(x) for x in c]

new_items_df[c1] = new_items_df[c].applymap(lambda x: set(x.lower().split(",")))
new_items_df[c2] = new_items_df[c1].applymap(len)

print (new_items_df)
              Name             Item   Unique_Name    Unique_Item  \
0  A,A,A,B,B,B,C,D   I1,I2,I3,I1,I2  {c, b, d, a}   {i1, i2, i3}   
1        X,Y,Z,Z,Z  I4,I1,I1,I88,I4     {y, x, z}  {i1, i4, i88}   

   Count_Unique_Name  Count_Unique_Item  
0                  4                  3  
1                  3                  3  

答案 1 :(得分:2)

按如下方式将Compilermerge一起使用

lambda

完整的例子是

df1 = df.merge(df.apply(lambda row: extractRow(row), axis=1), left_index=True, right_index=True)
相关问题