Current dataframe is as follows:
df = pd.read_csv('filename.csv', delimiter=',')
print(df)
idx uniqueID String CaseType
0 1 'hello1' 1.0
1 1 'hello2' 1.0
2 1 'goodbye1' 1.0
3 1 'goodbye2' 1.0
4 2 'hello1' 3.0
5 2 'hello2' 3.0
6 2 'hello3' 3.0
7 3 'goodbye1' 1.0
8 3 'goodbye2' 1.0
9 3 'goodbye3' 1.0
10 4 'hello' 2.0
11 4 'goodbye' 2.0
Expected Output: (Please note they are grouped based on uniqueID, and the case Type follows the last string of the uniqueID.)
idx Source Destination
0 'hello1' 'hello2'
1 'hello2' 'goodbye1'
3 'goodbye1' 'goodbye2'
4 'goodbye2' '1.0'
6 'hello1' 'hello2'
7 'hello2' 'hello3'
8 'hello3' '3.0'
10 'goodbye1' 'goodbye2'
11 'goodbye2' 'goodbye3'
12 'goodbye3' '1.0'
13 'hello' 'goodbye'
14 'goodbye' '2.0'
Question: How do I transform the pandas dataframe in this way?
Currently, I am iterating through every row in a for loop, and for each uniqueId, adding each string+CaseType (at the end) to a list, then splitting up that list and adding it to a new dataframe. It is incredibly slow.
Following this, the next step is to get the total counts/occurences for each row of the output. Essentially, if there are duplicate rows of source:destination (ie, we have 3 rows of 'hello' 'goodbye', it would result in 1 row with 'hello' 'goodbye' with their count as the 3rd column)
Example:
Original:
idx Source Destination
0 'hello1' 'hello2'
1 'hello2' 'hello3'
2 'hello1' 'hello2'
3 'hello4' 'goodbye'
Expected Output:
idx Source Destination Count
0 'hello1' 'hello2' 2
1 'hello2' 'hello3' 1
2 'hello4' 'goodbye' 1
I presume the first step is slightly more complex with pandas logic, and the next step is essentially just combining duplicates and getting their count, but I am new to pandas and not entirely sure how to do either. Thank you in advance.