Need Python pandas in below format
Question:
Input DataFrame
a b c d e f
1 1 20 1 0.85 3
1 1 20 2 0.85 5
1 1 20 3 0.85 5
Need output like below
a b c newcol
1 1 1 {1:3,2:5,3:5}
newcol column should have a key value pair (key should be column d from input and value should be column f from input)
How do we achieve this using python pandas
Answers:
Try groupby
and to_dict
:
(df.groupby(['a','b','c'])
.apply(lambda x: x.set_index('d')['f'].to_dict())
.reset_index(name='new_col')
)
Output:
a b c new_col
0 1 1 20 {1: 3, 2: 5, 3: 5}
data.csv:
a b c d e f
1 1 20 1 0.85 3
1 1 20 2 0.85 5
1 1 20 3 0.85 5
Code:
import pandas as pd
df = pd.read_csv('data.csv', delimiter='s+')
print (df)
dct = {k: v for k,v in zip(list(df['d']),list(df['f']))}
print (dct)
Output:
a b c d e f
0 1 1 20 1 0.85 3
1 1 1 20 2 0.85 5
2 1 1 20 3 0.85 5
{1: 3, 2: 5, 3: 5}
Input DataFrame
a b c d e f
1 1 20 1 0.85 3
1 1 20 2 0.85 5
1 1 20 3 0.85 5
Need output like below
a b c newcol
1 1 1 {1:3,2:5,3:5}
newcol column should have a key value pair (key should be column d from input and value should be column f from input)
How do we achieve this using python pandas
Try groupby
and to_dict
:
(df.groupby(['a','b','c'])
.apply(lambda x: x.set_index('d')['f'].to_dict())
.reset_index(name='new_col')
)
Output:
a b c new_col
0 1 1 20 {1: 3, 2: 5, 3: 5}
data.csv:
a b c d e f
1 1 20 1 0.85 3
1 1 20 2 0.85 5
1 1 20 3 0.85 5
Code:
import pandas as pd
df = pd.read_csv('data.csv', delimiter='s+')
print (df)
dct = {k: v for k,v in zip(list(df['d']),list(df['f']))}
print (dct)
Output:
a b c d e f
0 1 1 20 1 0.85 3
1 1 1 20 2 0.85 5
2 1 1 20 3 0.85 5
{1: 3, 2: 5, 3: 5}