[英]Sum and groupby if date is between two dates in two other columns and create new groupby data frame - pandas
[英]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.