Pandas tuple with value and dictionary into dataframe












0















Currently, I have data in the following form:



ie.



[ ('ab', {'a' : [apple1], 'b': [ball1]}), ('cd', {'a' : [apple2], 'b':   [ball2]})]  


List[Tuple[Any, dict{'key':List}]]



The goal is to create a pandas data frame in the following form:



start   a             b
ab apple1 ball1
cd apple2 ball2


I have tried to it the following way:



df = pd.DataFrame(columns=['start', 'a', 'b'])
for start, details in mylist:
df = df.append({'start' : start}, ignore_index= True)
df = df.append({'a' : details['a']} , ignore_index= True)
df = df.append({'b': details['b']}, ignore_index=True)


I'm trying to figure out an optimized way to do this.










share|improve this question




















  • 1





    Updated with the tried code

    – ajroot
    Nov 21 '18 at 17:34
















0















Currently, I have data in the following form:



ie.



[ ('ab', {'a' : [apple1], 'b': [ball1]}), ('cd', {'a' : [apple2], 'b':   [ball2]})]  


List[Tuple[Any, dict{'key':List}]]



The goal is to create a pandas data frame in the following form:



start   a             b
ab apple1 ball1
cd apple2 ball2


I have tried to it the following way:



df = pd.DataFrame(columns=['start', 'a', 'b'])
for start, details in mylist:
df = df.append({'start' : start}, ignore_index= True)
df = df.append({'a' : details['a']} , ignore_index= True)
df = df.append({'b': details['b']}, ignore_index=True)


I'm trying to figure out an optimized way to do this.










share|improve this question




















  • 1





    Updated with the tried code

    – ajroot
    Nov 21 '18 at 17:34














0












0








0








Currently, I have data in the following form:



ie.



[ ('ab', {'a' : [apple1], 'b': [ball1]}), ('cd', {'a' : [apple2], 'b':   [ball2]})]  


List[Tuple[Any, dict{'key':List}]]



The goal is to create a pandas data frame in the following form:



start   a             b
ab apple1 ball1
cd apple2 ball2


I have tried to it the following way:



df = pd.DataFrame(columns=['start', 'a', 'b'])
for start, details in mylist:
df = df.append({'start' : start}, ignore_index= True)
df = df.append({'a' : details['a']} , ignore_index= True)
df = df.append({'b': details['b']}, ignore_index=True)


I'm trying to figure out an optimized way to do this.










share|improve this question
















Currently, I have data in the following form:



ie.



[ ('ab', {'a' : [apple1], 'b': [ball1]}), ('cd', {'a' : [apple2], 'b':   [ball2]})]  


List[Tuple[Any, dict{'key':List}]]



The goal is to create a pandas data frame in the following form:



start   a             b
ab apple1 ball1
cd apple2 ball2


I have tried to it the following way:



df = pd.DataFrame(columns=['start', 'a', 'b'])
for start, details in mylist:
df = df.append({'start' : start}, ignore_index= True)
df = df.append({'a' : details['a']} , ignore_index= True)
df = df.append({'b': details['b']}, ignore_index=True)


I'm trying to figure out an optimized way to do this.







python pandas numpy dictionary dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 17:38









jpp

94.1k2155106




94.1k2155106










asked Nov 21 '18 at 16:50









ajrootajroot

2817




2817








  • 1





    Updated with the tried code

    – ajroot
    Nov 21 '18 at 17:34














  • 1





    Updated with the tried code

    – ajroot
    Nov 21 '18 at 17:34








1




1





Updated with the tried code

– ajroot
Nov 21 '18 at 17:34





Updated with the tried code

– ajroot
Nov 21 '18 at 17:34












2 Answers
2






active

oldest

votes


















3














pd.DataFrame.from_dict



Pandas works well with a dictionary or a list of dictionaries. You have something in between. In this case, converting to a dictionary is trivial:



L = [('ab', {'a' : ['apple1'], 'b': ['ball1']}),
('cd', {'a' : ['apple2'], 'b': ['ball2']})]

res = pd.DataFrame.from_dict(dict(L), orient='index')
res = res.apply(lambda x: x.str[0])

print(res)

a b
ab apple1 ball1
cd apple2 ball2





share|improve this answer
























  • What's the purpose of res.apply(lambda x: x.str[0])?

    – ajroot
    Nov 21 '18 at 17:50








  • 1





    You have lists in your inner dictionaries, this is one way to extract the first (and only) element from each list.

    – jpp
    Nov 21 '18 at 17:52











  • Nice one @jpp +1

    – pygo
    Nov 22 '18 at 6:36



















3














Like this:



form = [ ('ab', {'a' : ['apple1'], 'b': ['ball1']}), ('cd', {'a' : ['apple2'], 'b':   ['ball2']})]

# separate 'start' from rest of data - inverse zip
start, data = zip(*form)

# create dataframe
df = pd.DataFrame(list(data))

# remove data from lists in each cell
df = df.applymap(lambda l: l[0])

df.insert(loc=0, column='start', value=start)

print(df)
start a b
0 ab apple1 ball1
1 cd apple2 ball2


or, if you want start to be the index of the dataframe:



# separate 'start' from rest of data - inverse zip
index, data = zip(*form)

# create dataframe
df = pd.DataFrame(list(data), index=index)
df.index.name = 'start'

# remove data from lists in each cell
df = df.applymap(lambda l: l[0])

print(df)
start a b
ab apple1 ball1
cd apple2 ball2





share|improve this answer

























    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
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416925%2fpandas-tuple-with-value-and-dictionary-into-dataframe%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









    3














    pd.DataFrame.from_dict



    Pandas works well with a dictionary or a list of dictionaries. You have something in between. In this case, converting to a dictionary is trivial:



    L = [('ab', {'a' : ['apple1'], 'b': ['ball1']}),
    ('cd', {'a' : ['apple2'], 'b': ['ball2']})]

    res = pd.DataFrame.from_dict(dict(L), orient='index')
    res = res.apply(lambda x: x.str[0])

    print(res)

    a b
    ab apple1 ball1
    cd apple2 ball2





    share|improve this answer
























    • What's the purpose of res.apply(lambda x: x.str[0])?

      – ajroot
      Nov 21 '18 at 17:50








    • 1





      You have lists in your inner dictionaries, this is one way to extract the first (and only) element from each list.

      – jpp
      Nov 21 '18 at 17:52











    • Nice one @jpp +1

      – pygo
      Nov 22 '18 at 6:36
















    3














    pd.DataFrame.from_dict



    Pandas works well with a dictionary or a list of dictionaries. You have something in between. In this case, converting to a dictionary is trivial:



    L = [('ab', {'a' : ['apple1'], 'b': ['ball1']}),
    ('cd', {'a' : ['apple2'], 'b': ['ball2']})]

    res = pd.DataFrame.from_dict(dict(L), orient='index')
    res = res.apply(lambda x: x.str[0])

    print(res)

    a b
    ab apple1 ball1
    cd apple2 ball2





    share|improve this answer
























    • What's the purpose of res.apply(lambda x: x.str[0])?

      – ajroot
      Nov 21 '18 at 17:50








    • 1





      You have lists in your inner dictionaries, this is one way to extract the first (and only) element from each list.

      – jpp
      Nov 21 '18 at 17:52











    • Nice one @jpp +1

      – pygo
      Nov 22 '18 at 6:36














    3












    3








    3







    pd.DataFrame.from_dict



    Pandas works well with a dictionary or a list of dictionaries. You have something in between. In this case, converting to a dictionary is trivial:



    L = [('ab', {'a' : ['apple1'], 'b': ['ball1']}),
    ('cd', {'a' : ['apple2'], 'b': ['ball2']})]

    res = pd.DataFrame.from_dict(dict(L), orient='index')
    res = res.apply(lambda x: x.str[0])

    print(res)

    a b
    ab apple1 ball1
    cd apple2 ball2





    share|improve this answer













    pd.DataFrame.from_dict



    Pandas works well with a dictionary or a list of dictionaries. You have something in between. In this case, converting to a dictionary is trivial:



    L = [('ab', {'a' : ['apple1'], 'b': ['ball1']}),
    ('cd', {'a' : ['apple2'], 'b': ['ball2']})]

    res = pd.DataFrame.from_dict(dict(L), orient='index')
    res = res.apply(lambda x: x.str[0])

    print(res)

    a b
    ab apple1 ball1
    cd apple2 ball2






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 21 '18 at 17:36









    jppjpp

    94.1k2155106




    94.1k2155106













    • What's the purpose of res.apply(lambda x: x.str[0])?

      – ajroot
      Nov 21 '18 at 17:50








    • 1





      You have lists in your inner dictionaries, this is one way to extract the first (and only) element from each list.

      – jpp
      Nov 21 '18 at 17:52











    • Nice one @jpp +1

      – pygo
      Nov 22 '18 at 6:36



















    • What's the purpose of res.apply(lambda x: x.str[0])?

      – ajroot
      Nov 21 '18 at 17:50








    • 1





      You have lists in your inner dictionaries, this is one way to extract the first (and only) element from each list.

      – jpp
      Nov 21 '18 at 17:52











    • Nice one @jpp +1

      – pygo
      Nov 22 '18 at 6:36

















    What's the purpose of res.apply(lambda x: x.str[0])?

    – ajroot
    Nov 21 '18 at 17:50







    What's the purpose of res.apply(lambda x: x.str[0])?

    – ajroot
    Nov 21 '18 at 17:50






    1




    1





    You have lists in your inner dictionaries, this is one way to extract the first (and only) element from each list.

    – jpp
    Nov 21 '18 at 17:52





    You have lists in your inner dictionaries, this is one way to extract the first (and only) element from each list.

    – jpp
    Nov 21 '18 at 17:52













    Nice one @jpp +1

    – pygo
    Nov 22 '18 at 6:36





    Nice one @jpp +1

    – pygo
    Nov 22 '18 at 6:36













    3














    Like this:



    form = [ ('ab', {'a' : ['apple1'], 'b': ['ball1']}), ('cd', {'a' : ['apple2'], 'b':   ['ball2']})]

    # separate 'start' from rest of data - inverse zip
    start, data = zip(*form)

    # create dataframe
    df = pd.DataFrame(list(data))

    # remove data from lists in each cell
    df = df.applymap(lambda l: l[0])

    df.insert(loc=0, column='start', value=start)

    print(df)
    start a b
    0 ab apple1 ball1
    1 cd apple2 ball2


    or, if you want start to be the index of the dataframe:



    # separate 'start' from rest of data - inverse zip
    index, data = zip(*form)

    # create dataframe
    df = pd.DataFrame(list(data), index=index)
    df.index.name = 'start'

    # remove data from lists in each cell
    df = df.applymap(lambda l: l[0])

    print(df)
    start a b
    ab apple1 ball1
    cd apple2 ball2





    share|improve this answer






























      3














      Like this:



      form = [ ('ab', {'a' : ['apple1'], 'b': ['ball1']}), ('cd', {'a' : ['apple2'], 'b':   ['ball2']})]

      # separate 'start' from rest of data - inverse zip
      start, data = zip(*form)

      # create dataframe
      df = pd.DataFrame(list(data))

      # remove data from lists in each cell
      df = df.applymap(lambda l: l[0])

      df.insert(loc=0, column='start', value=start)

      print(df)
      start a b
      0 ab apple1 ball1
      1 cd apple2 ball2


      or, if you want start to be the index of the dataframe:



      # separate 'start' from rest of data - inverse zip
      index, data = zip(*form)

      # create dataframe
      df = pd.DataFrame(list(data), index=index)
      df.index.name = 'start'

      # remove data from lists in each cell
      df = df.applymap(lambda l: l[0])

      print(df)
      start a b
      ab apple1 ball1
      cd apple2 ball2





      share|improve this answer




























        3












        3








        3







        Like this:



        form = [ ('ab', {'a' : ['apple1'], 'b': ['ball1']}), ('cd', {'a' : ['apple2'], 'b':   ['ball2']})]

        # separate 'start' from rest of data - inverse zip
        start, data = zip(*form)

        # create dataframe
        df = pd.DataFrame(list(data))

        # remove data from lists in each cell
        df = df.applymap(lambda l: l[0])

        df.insert(loc=0, column='start', value=start)

        print(df)
        start a b
        0 ab apple1 ball1
        1 cd apple2 ball2


        or, if you want start to be the index of the dataframe:



        # separate 'start' from rest of data - inverse zip
        index, data = zip(*form)

        # create dataframe
        df = pd.DataFrame(list(data), index=index)
        df.index.name = 'start'

        # remove data from lists in each cell
        df = df.applymap(lambda l: l[0])

        print(df)
        start a b
        ab apple1 ball1
        cd apple2 ball2





        share|improve this answer















        Like this:



        form = [ ('ab', {'a' : ['apple1'], 'b': ['ball1']}), ('cd', {'a' : ['apple2'], 'b':   ['ball2']})]

        # separate 'start' from rest of data - inverse zip
        start, data = zip(*form)

        # create dataframe
        df = pd.DataFrame(list(data))

        # remove data from lists in each cell
        df = df.applymap(lambda l: l[0])

        df.insert(loc=0, column='start', value=start)

        print(df)
        start a b
        0 ab apple1 ball1
        1 cd apple2 ball2


        or, if you want start to be the index of the dataframe:



        # separate 'start' from rest of data - inverse zip
        index, data = zip(*form)

        # create dataframe
        df = pd.DataFrame(list(data), index=index)
        df.index.name = 'start'

        # remove data from lists in each cell
        df = df.applymap(lambda l: l[0])

        print(df)
        start a b
        ab apple1 ball1
        cd apple2 ball2






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 '18 at 17:54

























        answered Nov 21 '18 at 17:40









        RobertRobert

        33429




        33429






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416925%2fpandas-tuple-with-value-and-dictionary-into-dataframe%23new-answer', 'question_page');
            }
            );

            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







            Popular posts from this blog

            To store a contact into the json file from server.js file using a class in NodeJS

            Redirect URL with Chrome Remote Debugging Android Devices

            Dieringhausen