我的数据框如下:
Car_Modal Color Number_Passenger
Proton Black 5
Proton Black 7
Perudua White 5
Perudua White 7
Perudua Red 7
Honda 5
由于本田行在“颜色”列中具有Null值,因此在使用以下代码时显示错误:
df["Join"]=df.groupby("Car_Modal")["Color"].transform(lambda x :'<br>'.join(x.unique()))
预期输出:
Car_Modal Color Number_Passenger Join
Proton Black 5 Black
Proton Black 7 Black
Perudua White 5 White<br>Red
Perudua White 7 White<br>Red
Perudua Red 7 White<br>Red
Honda 5
任何人都可以分享我如何解决此问题的想法?
答案 0 :(得分:2)
尝试过滤不为空的数据
df["Join"]=df[~df["Color"].isnull()].groupby("Car_Modal")["Color"] \
.transform(lambda x :'<br>'.join(x.unique()))
答案 1 :(得分:2)
您可以使用pandas.Series.notnull构建一个过滤器,以基于特定列中是否存在空值进行过滤:
filter_Color_null=df['Color'].notnull()
df["Join"]=df[filter_Color_null].groupby("Car_Modal")["Color"].transform(lambda x :'<br>'.join(x.unique()))
您还可以使用DataFrame.notnull按DataFrame的任何列进行过滤:
filter_null=df.notnull().all(axis=1)
df["Join"]=df[filter_null].groupby("Car_Modal")["Color"].transform(lambda x :'<br>'.join(x.unique()))