这是昨天问题的后续问题:对于数据框中的[i,j],[j,i],我怎样才能在julia中只获取[i,j]行? 在这样的数据框的情况下
行 | p1 | p2 | s1 | t1 | s2 | t2 |
---|---|---|---|---|---|---|
1 | 355 | 210 | 66541 | 68071 | 65821 | 68071 |
2 | 355 | 210 | 68503 | 74299 | 68503 | 72481 |
3 | 210 | 355 | 68503 | 72481 | 68503 | 74299 |
对于第2行和第3行,信息是相同的,我只想获取其中之一和第1行。 根据昨天提供的答案,我使用了 p1、p2、s1、s2 的总和来尝试获得唯一值。
unique(df, [:p1, :p2, :s1, s2] => ByRow(minmax))
但是我遇到了这样的错误
MethodError: no method matching sum(::Int64, ::Int64, ::Float64, ::Float64)
我在这里发布问题后,一位教授(Martin Kühn)帮助我解决了这个问题。所以我也在这里发布答案,以防其他人也面临类似的问题。
基本上,这个想法是改变p1和p2的顺序,如果p1比p2大,那么其他值也是如此。然后它会得到这个:
行 | p1 | p2 | s1 | t1 | s2 | t2 |
---|---|---|---|---|---|---|
1 | 210 | 355 | 65821 | 68071 | 66541 | 68071 |
2 | 210 | 355 | 68503 | 72481 | 68503 | 74299 |
3 | 210 | 355 | 68503 | 72481 | 68503 | 74299 |
然后使用unique函数进行过滤。
他用Python解决了这个问题,我把代码贴在这里。
df = pd.read_csv("path.csv")
indices_p2grp1 = np.where(df['p2'] < df['p1'])[0]
df_new = pd.DataFrame(columns=['p2', 'p1', 's2', 'e2', 's1', 'e1')
df_new[['p2', 'p1', 's2', 'e2', 's1', 'e1']] = df.iloc[indices_p2grp1].copy()
df_new = df_new[df.columns]
indices_p1gep2 = np.sort(np.array(list(set(list(range(len(df)))).difference(set(list(indices_p2grp1))))))
df_final = pd.concat([df_new, df.iloc[indices_p1gep2, :]], ignore_index=True)