'Only groupby a key and not performing any other changes
Is it possible to just group rows by key without performing any changes to any other column than the key column going to index ? If yes, how is can we do it ?
df = pd.DataFrame({
'id': ['A','A','A','B','B','C','C','C','C'],
'data1': [11,35,46,11,26,25,39,50,55],
'data2': [1,1,1,1,1,2,2,2,2],
})
df
I want a frame where we have ['A', 'B', 'C'] as index and every rows for data1 and data2 stored into index A if id=A, index B if id=B and index C if id=C
something like this :
data1 data2
A 11 1
35 1
46 1
B 11 1
26 1
C 25 2
39 2
50 2
55 2
Solution 1:[1]
If I understand you correctly you can iterate over result of DataFrame.groupby():
for idx, g in df.groupby("id"):
print("Idx =", idx)
print(g)
print("-" * 80)
Prints:
Idx = A
id data1 data2
0 A 11 1
1 A 35 1
2 A 46 1
--------------------------------------------------------------------------------
Idx = B
id data1 data2
3 B 11 1
4 B 26 1
--------------------------------------------------------------------------------
Idx = C
id data1 data2
5 C 25 2
6 C 39 2
7 C 50 2
8 C 55 2
--------------------------------------------------------------------------------
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrej Kesely |
