Create a dict of dict with 2 columns and with values from a third column in pandas
I have a dataframe in python that looks like somewhat following:
df = pd.DataFrame({
'tag':['php','image-processing','file-upload','upload','mime-types'] * 2,
'probability':np.arange(10),
'token':['check'] * 5 + ['imag'] * 5
}).set_index(['tag','token'])
print (df)
probability
tag token
php check 0
image-processing check 1
file-upload check 2
upload check 3
mime-types check 4
php imag 5
image-processing imag 6
file-upload imag 7
upload imag 8
mime-types imag 9
I need to crate a dicts of dicts and the values being probability. For example for php
{'php': {'check': 0,
'imag': 1,
....},
'image-processing': {....},
'file-upload': {....},
}
I don't need the internal dicts in any order of the probability values. I can code this up easily but I need to know if there is any pandas trick that can be applied here.
python pandas dictionary
add a comment |
I have a dataframe in python that looks like somewhat following:
df = pd.DataFrame({
'tag':['php','image-processing','file-upload','upload','mime-types'] * 2,
'probability':np.arange(10),
'token':['check'] * 5 + ['imag'] * 5
}).set_index(['tag','token'])
print (df)
probability
tag token
php check 0
image-processing check 1
file-upload check 2
upload check 3
mime-types check 4
php imag 5
image-processing imag 6
file-upload imag 7
upload imag 8
mime-types imag 9
I need to crate a dicts of dicts and the values being probability. For example for php
{'php': {'check': 0,
'imag': 1,
....},
'image-processing': {....},
'file-upload': {....},
}
I don't need the internal dicts in any order of the probability values. I can code this up easily but I need to know if there is any pandas trick that can be applied here.
python pandas dictionary
add a comment |
I have a dataframe in python that looks like somewhat following:
df = pd.DataFrame({
'tag':['php','image-processing','file-upload','upload','mime-types'] * 2,
'probability':np.arange(10),
'token':['check'] * 5 + ['imag'] * 5
}).set_index(['tag','token'])
print (df)
probability
tag token
php check 0
image-processing check 1
file-upload check 2
upload check 3
mime-types check 4
php imag 5
image-processing imag 6
file-upload imag 7
upload imag 8
mime-types imag 9
I need to crate a dicts of dicts and the values being probability. For example for php
{'php': {'check': 0,
'imag': 1,
....},
'image-processing': {....},
'file-upload': {....},
}
I don't need the internal dicts in any order of the probability values. I can code this up easily but I need to know if there is any pandas trick that can be applied here.
python pandas dictionary
I have a dataframe in python that looks like somewhat following:
df = pd.DataFrame({
'tag':['php','image-processing','file-upload','upload','mime-types'] * 2,
'probability':np.arange(10),
'token':['check'] * 5 + ['imag'] * 5
}).set_index(['tag','token'])
print (df)
probability
tag token
php check 0
image-processing check 1
file-upload check 2
upload check 3
mime-types check 4
php imag 5
image-processing imag 6
file-upload imag 7
upload imag 8
mime-types imag 9
I need to crate a dicts of dicts and the values being probability. For example for php
{'php': {'check': 0,
'imag': 1,
....},
'image-processing': {....},
'file-upload': {....},
}
I don't need the internal dicts in any order of the probability values. I can code this up easily but I need to know if there is any pandas trick that can be applied here.
python pandas dictionary
python pandas dictionary
edited Nov 20 '18 at 11:17
jezrael
340k25294365
340k25294365
asked Nov 20 '18 at 10:54
Mayukh SarkarMayukh Sarkar
915821
915821
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Use dictionary comprehension
:
d = {k:v.reset_index(level=0, drop=True).to_dict()
for k, v in df.groupby(level=0)['probability']}
Another solution:
d = {k: dict(v.values) for k, v in df.reset_index(level=1).groupby(level=0)}
print (d)
{'file-upload': {'check': 2, 'imag': 7},
'image-processing': {'check': 1, 'imag': 6},
'mime-types': {'check': 4, 'imag': 9},
'php': {'check': 0, 'imag': 5},
'upload': {'check': 3, 'imag': 8}}
add a comment |
I think this is the fastest method:
Your example:
df.reset_index(level=1, inplace=True)
df_dict = df.to_dict('your_index')
Simple example:
import pandas as pd
df2 = pd.DataFrame({'id':['php','php','php','c'],'col1':['a','b','c','a'],'col2':[1,2,3,4]}).set_index('id')
df2.to_dict('id')
pandas.DataFrame.to_dict
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391438%2fcreate-a-dict-of-dict-with-2-columns-and-with-values-from-a-third-column-in-pand%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use dictionary comprehension
:
d = {k:v.reset_index(level=0, drop=True).to_dict()
for k, v in df.groupby(level=0)['probability']}
Another solution:
d = {k: dict(v.values) for k, v in df.reset_index(level=1).groupby(level=0)}
print (d)
{'file-upload': {'check': 2, 'imag': 7},
'image-processing': {'check': 1, 'imag': 6},
'mime-types': {'check': 4, 'imag': 9},
'php': {'check': 0, 'imag': 5},
'upload': {'check': 3, 'imag': 8}}
add a comment |
Use dictionary comprehension
:
d = {k:v.reset_index(level=0, drop=True).to_dict()
for k, v in df.groupby(level=0)['probability']}
Another solution:
d = {k: dict(v.values) for k, v in df.reset_index(level=1).groupby(level=0)}
print (d)
{'file-upload': {'check': 2, 'imag': 7},
'image-processing': {'check': 1, 'imag': 6},
'mime-types': {'check': 4, 'imag': 9},
'php': {'check': 0, 'imag': 5},
'upload': {'check': 3, 'imag': 8}}
add a comment |
Use dictionary comprehension
:
d = {k:v.reset_index(level=0, drop=True).to_dict()
for k, v in df.groupby(level=0)['probability']}
Another solution:
d = {k: dict(v.values) for k, v in df.reset_index(level=1).groupby(level=0)}
print (d)
{'file-upload': {'check': 2, 'imag': 7},
'image-processing': {'check': 1, 'imag': 6},
'mime-types': {'check': 4, 'imag': 9},
'php': {'check': 0, 'imag': 5},
'upload': {'check': 3, 'imag': 8}}
Use dictionary comprehension
:
d = {k:v.reset_index(level=0, drop=True).to_dict()
for k, v in df.groupby(level=0)['probability']}
Another solution:
d = {k: dict(v.values) for k, v in df.reset_index(level=1).groupby(level=0)}
print (d)
{'file-upload': {'check': 2, 'imag': 7},
'image-processing': {'check': 1, 'imag': 6},
'mime-types': {'check': 4, 'imag': 9},
'php': {'check': 0, 'imag': 5},
'upload': {'check': 3, 'imag': 8}}
edited Nov 20 '18 at 11:25
answered Nov 20 '18 at 11:14
jezraeljezrael
340k25294365
340k25294365
add a comment |
add a comment |
I think this is the fastest method:
Your example:
df.reset_index(level=1, inplace=True)
df_dict = df.to_dict('your_index')
Simple example:
import pandas as pd
df2 = pd.DataFrame({'id':['php','php','php','c'],'col1':['a','b','c','a'],'col2':[1,2,3,4]}).set_index('id')
df2.to_dict('id')
pandas.DataFrame.to_dict
add a comment |
I think this is the fastest method:
Your example:
df.reset_index(level=1, inplace=True)
df_dict = df.to_dict('your_index')
Simple example:
import pandas as pd
df2 = pd.DataFrame({'id':['php','php','php','c'],'col1':['a','b','c','a'],'col2':[1,2,3,4]}).set_index('id')
df2.to_dict('id')
pandas.DataFrame.to_dict
add a comment |
I think this is the fastest method:
Your example:
df.reset_index(level=1, inplace=True)
df_dict = df.to_dict('your_index')
Simple example:
import pandas as pd
df2 = pd.DataFrame({'id':['php','php','php','c'],'col1':['a','b','c','a'],'col2':[1,2,3,4]}).set_index('id')
df2.to_dict('id')
pandas.DataFrame.to_dict
I think this is the fastest method:
Your example:
df.reset_index(level=1, inplace=True)
df_dict = df.to_dict('your_index')
Simple example:
import pandas as pd
df2 = pd.DataFrame({'id':['php','php','php','c'],'col1':['a','b','c','a'],'col2':[1,2,3,4]}).set_index('id')
df2.to_dict('id')
pandas.DataFrame.to_dict
edited Nov 20 '18 at 11:38
answered Nov 20 '18 at 11:12
Rudolf MorkovskyiRudolf Morkovskyi
730117
730117
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391438%2fcreate-a-dict-of-dict-with-2-columns-and-with-values-from-a-third-column-in-pand%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown