[英]creating a new data frame using differences between two columns in pandas
This is a subset of a data frame: 这是数据帧的子集:
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
I would like to find rows that SS1 and SS2 are different and then create a new data frame based on that. 我想找到SS1和SS2不同的行,然后基于该行创建一个新的数据帧。 The output should be like that:
输出应该是这样的:
index id drug sentences SS1 SS2
1 2 lex very bad 0 1
3 7 effex hard 1 0
This is my code: 这是我的代码:
df [['index','id', 'drug', 'sentences', 'SS1', 'SS2' ]] = np.where(df.SS1 != df.SS2)
But it has the following error: ValueError: Must have equal len keys and value when setting with an ndarray
但是它具有以下错误:
ValueError: Must have equal len keys and value when setting with an ndarray
Any suggestion? 有什么建议吗?
One way may be following: 一种可能是以下方式:
df_new = df[df.SS1 != df.SS2]
print(df_new)
Output: 输出:
index id drug sentences SS1 SS2
0 1 2 lex very bad 0 1
2 3 7 effex hard 1 0
Using where
: where
使用:
df_new = df.where(df.SS1 != df.SS2).dropna()
print(df_new)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.