Python dataframe rotate row path to column
I have a DataFrame
Pandas Python as below:
df = pd.DataFrame({'id':[1, 1, 1, 2, 2], 'm':['email', 'organic', 'cpc', 'cpc', 'direct']})
print df
id m
0 1 email
1 1 organic
2 1 cpc
3 2 cpc
4 2 direct
After conversion, I want to get
df_transformed = pd.DataFrame({'id':[1,2], 'path' :['p1-p3-p2', 'p2-p1']})
print df_transformed
id path
0 1 p1-p3-p2
1 2 p2-p1
Note that the length of the page path for each ID may be different.
How can I get this? thank.
+3
source to share
1 answer
I don't know what you are doing exactly.
So, I try more combination:
1.Input with "p"
from revising your answer, I get: d = {'id':[1, 1, 1, 2, 2], 'path':['p1', 'p3', 'p2', 'p2', 'p1']}
then the code:
import numpy as np
import pandas as pd
d = {'id':[1, 1, 1, 2, 2], 'path':['p1', 'p3', 'p2', 'p2', 'p1']}
o = pd.DataFrame({'id':[1,2], 'path' :['p1-p3-p2', 'p2-p1']})
df = pd.DataFrame(d)
print df
print o
df = df.groupby('id').agg({'path': lambda x: '-'.join(x)})
print df
path
id
1 p1-p3-p2
2 p2-p1
2.output with "m words"
it means:
o = pd.DataFrame({'id':[1,2], 'm' :['email-organic-cpc', 'cpc-direct']})
then the code:
import numpy as np
import pandas as pd
d = {'id':[1, 1, 1, 2, 2], 'm':['email', 'organic', 'cpc', 'cpc', 'direct']}
o = pd.DataFrame({'id':[1,2], 'm' :['email-organic-cpc', 'cpc-direct']})
df = pd.DataFrame(d)
print df
print o
df = df.groupby('id').agg({'m': lambda x: '-'.join(x)})
print df
m
id
1 email-organic-cpc
2 cpc-direct
3.reassignment - from "m" to "p"
(I need to change the last element direct
to email
because the reassignment would be wrong)
import numpy as np
import pandas as pd
d = {'id':[1, 1, 1, 2, 2], 'm':['email', 'organic', 'cpc', 'cpc', 'email']}
o = pd.DataFrame({'id':[1,2], 'path' :['p1-p3-p2', 'p2-p1']})
dictionary = {'email': 'p1', 'cpc': 'p2', 'organic': 'p3'}
df = pd.DataFrame(d)
print df
df = df.replace({'m': dictionary})
df = df.rename(columns = { 'm':'path'})
print df
print o
df = df.groupby('id').agg({'path': lambda x: '-'.join(x)})
print df
path
id
1 p1-p3-p2
2 p2-p1
+1
source to share