#332 Remove duplicates by columns and keep the row with the highest value in the selected column by pandas
Jan 5, 2023 | pandas, python |
table.txt
11 | QUAD 4 23 3.3 10.0 30.0 |
12 | QUAD 4 24 12.2 11.0 31.0 |
13 | QUAD 1 25 1.1 12.0 32.0 |
ex332.py
2 | df = pd.read_csv( "table.txt" , sep = " " ) |
11 | df1 = df.sort_values( 'fx' , ascending = False ).drop_duplicates( 'eid' ).sort_index() |
12 | df1 = df1.sort_values( 'eid' , ascending = True ) |
13 | print ( "Result max fx:" ) |
16 | df1 = df.sort_values( 'fx' , ascending = True ).drop_duplicates( 'eid' ).sort_index() |
17 | df1 = df1.sort_values( 'eid' , ascending = True ) |
18 | print ( "Result min fx:" ) |
22 | df1 = df.sort_values( 'fy' , ascending = False ).drop_duplicates( 'eid' ).sort_index() |
23 | df1 = df1.sort_values( 'eid' , ascending = True ) |
24 | print ( "Result max fy:" ) |
27 | df1 = df.sort_values( 'fy' , ascending = True ).drop_duplicates( 'eid' ).sort_index() |
28 | df1 = df1.sort_values( 'eid' , ascending = True ) |
29 | print ( "Result min fy:" ) |
33 | df1 = df.sort_values( 'fxy' , ascending = False ).drop_duplicates( 'eid' ).sort_index() |
34 | df1 = df1.sort_values( 'eid' , ascending = True ) |
35 | print ( "Result max fxy:" ) |
38 | df1 = df.sort_values( 'fxy' , ascending = True ).drop_duplicates( 'eid' ).sort_index() |
39 | df1 = df1.sort_values( 'eid' , ascending = True ) |
40 | print ( "Result min fxy:" ) |
Output:
2 | quad lcid eid fx fy fxy |
3 | 0 QUAD 1 23 1.2 1.0 21.0 |
4 | 1 QUAD 4 24 2.6 2.0 22.0 |
5 | 2 QUAD 1 25 3.2 3.0 23.0 |
6 | 3 QUAD 2 23 4.6 4.0 24.0 |
7 | 4 QUAD 4 24 5.6 5.0 25.0 |
8 | 5 QUAD 2 25 6.2 6.0 26.0 |
9 | 6 QUAD 3 23 7.2 7.0 27.0 |
10 | 7 QUAD 3 24 6.2 8.0 28.0 |
11 | 8 QUAD 2 25 5.2 9.0 29.0 |
12 | 9 QUAD 4 23 3.3 10.0 30.0 |
13 | 10 QUAD 4 24 12.2 11.0 31.0 |
14 | 11 QUAD 1 25 1.1 12.0 32.0 |
17 | quad lcid eid fx fy fxy |
18 | 6 QUAD 3 23 7.2 7.0 27.0 |
19 | 10 QUAD 4 24 12.2 11.0 31.0 |
20 | 5 QUAD 2 25 6.2 6.0 26.0 |
22 | quad lcid eid fx fy fxy |
23 | 0 QUAD 1 23 1.2 1.0 21.0 |
24 | 1 QUAD 4 24 2.6 2.0 22.0 |
25 | 11 QUAD 1 25 1.1 12.0 32.0 |
27 | quad lcid eid fx fy fxy |
28 | 9 QUAD 4 23 3.3 10.0 30.0 |
29 | 10 QUAD 4 24 12.2 11.0 31.0 |
30 | 11 QUAD 1 25 1.1 12.0 32.0 |
32 | quad lcid eid fx fy fxy |
33 | 0 QUAD 1 23 1.2 1.0 21.0 |
34 | 1 QUAD 4 24 2.6 2.0 22.0 |
35 | 2 QUAD 1 25 3.2 3.0 23.0 |
37 | quad lcid eid fx fy fxy |
38 | 9 QUAD 4 23 3.3 10.0 30.0 |
39 | 10 QUAD 4 24 12.2 11.0 31.0 |
40 | 11 QUAD 1 25 1.1 12.0 32.0 |
42 | quad lcid eid fx fy fxy |
43 | 0 QUAD 1 23 1.2 1.0 21.0 |
44 | 1 QUAD 4 24 2.6 2.0 22.0 |
45 | 2 QUAD 1 25 3.2 3.0 23.0 |
Like this:
Like Loading...
Related
Recent Comments