Say I have the pandas DataFarme looks like this:
Name VALUE 0 A 1 1 A 2 2 A 3 3 B 4 4 B 5 5 C 6 6 C 7 7 C 8 8 C 9 9 D 10
I would like to select all the rows with the same name but different values below:
Name Value
0 A 1
1 A 2
2 A 3
Name Value
0 C 6
1 C 7
2 C 8
3 C 9
I tried to use .T to transform the rows and columns, and then use .groupby to select all columns with the same name. But, there are two questions: firstly, .groupby can not choose the same columns; secondly, .groupby actually adds all values in one row with the same name, which is not the result I want.
Then, I tried to use .duplicated(keep=False), but this code only tells me if the data is the same or not.
What code should I use? Actually. I have a thousand data and more than 20 of the same name rows as “A”, “B”, “C”, “D”, up to “Z”. Each name has a thousand different values.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
If you want to get different dataframes then here’s how you can do it:
df_splits = [v for k, v in df.groupby('Name')]
for df_split in df_splits:
print(df_split, sep = 'n')
Output:
Name VALUE 0 A 1 1 A 2 2 A 3 Name VALUE 3 B 4 4 B 5 Name VALUE 5 C 6 6 C 7 7 C 8 8 C 9 Name VALUE 9 D 10
To access dataframes individually use:
df_splits[0], df_splits[1]….
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0