How can I swap selected data in 2 rows based on criteria

FARRAF

I am new to Python.

Suppose I have DataFrame, and want to switch selected data of all rows with similar id.

For example:

|  id  | Gender |  Name   |   Hobby    |   Food    |
+------+--------+---------+------------+-----------+
| 1111 | Male   | Robert  | Swim       | Ice-cream |
| 1112 | Female | Natasha | Football   | Pizza     |
| 1111 | Female | Misa    | Volleyball | Pasta     |
| 1112 | Male   | Rick    | Sleep      | Hamburger |
| 1113 | Male   | Rondo   | Read       | Rice      |
+------+--------+---------+------------+-----------+



Expected output:

+------+--------+---------+------------+-----------+
|  id  | Gender |  Name   |   Hobby    |   Food    |
+------+--------+---------+------------+-----------+
| 1111 | Male   | Robert  | Volleyball | Pasta     |
| 1112 | Female | Natasha | Sleep      | Hamburger |
| 1111 | Female | Misa    | Swim       | Ice-cream |
| 1112 | Male   | Rick    | Football   | Pizza     |
| 1113 | Male   | Rondo   | Read       | Rice      |
+------+--------+---------+------------+-----------+

So what I want to do is to switch data (In this example only Hobby and Food) between people with same id, but name and gender remain the same.

So basicly, on first row, id 1111, Gender is Male, Name is Robert - Hobby will be Volleyball and Food will be Pasta. On second row, id 1111, Gender is female name is Misa - hobby will be swim and food will be ice-cream. And do the same for the id 1112 as well. I do not have any idea at the moment, except for writing some loop and then use empty DataFramme to append and switch. Thank you all!!

Quang Hoang

Instead of swapping other columns, I choose to swap Name and Gender:

def swap(x):
    x[['Gender','Name']] = x[['Gender','Name']].values[::-1]
    return x

df.groupby('id').apply(swap)

Output:

     id  Gender     Name       Hobby       Food
0  1111  Female     Misa        Swim   Icecream
1  1112    Male     Rick    Football      Pizza
2  1111    Male   Robert  Volleyball      Pasta
3  1112  Female  Natasha       Sleep  Hamburger
4  1113    Male    Rondo        Read       Rice

Or if you insist on the correct order, then swap the other columns:

def swap2(x):
    x[['Hobby','Food']] = x[['Hobby','Food']].values[::-1]

    return x

print(df.groupby('id').apply(swap2))

Output:

     id  Gender     Name       Hobby       Food
0  1111    Male   Robert  Volleyball      Pasta
1  1112  Female  Natasha       Sleep  Hamburger
2  1111  Female     Misa        Swim   Icecream
3  1112    Male     Rick    Football      Pizza
4  1113    Male    Rondo        Read       Rice

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I adjust this Flexbox layout based on multiple criteria?

How can I interleave rows from 2 data frames together?

How can I count selected rows in MySQL?

using data-table for listing with select/selectall rows for rows selection by script, then how can i set/get dynamic ids of selected rows

How do I restrict data selected from a csv file with 2 criteria?

How can i efficiently turn columns into 0 based on filter criteria?

How can I display data on a table based on the selected option on the dropdown menu?

How to drop duplicated rows in data frame based on certain criteria?

in R, how can I calculate y/y and w/w in a data frame, based on different columns criteria?

How do i slice rows based in a global criteria?

MYSQL How can I sum based on multiple criteria by date and by response

How can I match values based on criteria?

How to Delete Rows Based on Criteria from 2 Columns in VBA?

How can i make 2d array rows swap in java without using loop?

how can i get a specific value based on different criteria with vlookup?

How can I create arrays of suitable candidates based on certain criteria?

How can I isolate rows in a 2d numpy matrix that match a specific criteria?

How to retrieve data based on this criteria?

How can I automatically number rows in Excel based off two different criteria, one being date?

How can I get the data from MySQL database based on the selected ID using bootstrap and Ajax?

How can I get the selected data on my modal window(on button click) based on the v-for value?

How can i group rows using specific criteria that are not in pandas dataframe?

how can we remove the rows from xts based on the seconds criteria

Sum between rows based on selected criteria (Google Spreadsheet)

How do i edit multible rows in mysql based on a set of criteria

R: How can I add rows based on values of a data frame?

How can I get only certain rows of data based on certain criteria

How can I select a data from another column from rows that have been selected?

How can I generate a new 2D array based on existing array following index criteria in JS?