繁体   English   中英

使用熊猫中两列之间的差异创建新的数据框

[英]creating a new data frame using differences between two columns in pandas

这是数据帧的子集:

index  id   drug   sentences     SS1   SS2
1      2    lex     very bad      0     1
2      3    gym     very nice     1     1
3      7    effex   hard          1     0 
4      8    cymba   poor          1     1

我想找到SS1和SS2不同的行,然后基于该行创建一个新的数据帧。 输出应该是这样的:

index  id   drug   sentences     SS1   SS2
1      2    lex     very bad      0     1
3      7    effex   hard          1     0 

这是我的代码:

df [['index','id', 'drug', 'sentences', 'SS1', 'SS2' ]] = np.where(df.SS1 != df.SS2)

但是它具有以下错误: ValueError: Must have equal len keys and value when setting with an ndarray

有什么建议吗?

一种可能是以下方式:

df_new = df[df.SS1 != df.SS2]
print(df_new)

输出:

    index  id   drug sentences  SS1  SS2
0      1   2    lex  very bad    0    1
2      3   7  effex      hard    1    0

where使用:

df_new = df.where(df.SS1 != df.SS2).dropna()
print(df_new)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM