Iterating over two pandas lists












-2















I'm working on cleaning up some of my code to make it a bit more pythonic, but I'm wondering if the below could be written in a nicer way with something like an itertools or pandas method. The below code works, however I'm hoping to remove the double for-loop and consolidate a bit of the code for performance reasons.



Ultimately, I'm working with a list of indices that call a Pandas column.



def foo(dataset):
api_reshaped = pd.DataFrame(columns=['foo', 'bar'])
k = 0

for index, _ in dataset.iterrows():
for key in dataset.iloc[index][0][0]:
api_reshaped.loc[k, 'foo'] = key
api_reshaped.loc[k, 'bar'] = dataset.iloc[index][0][0][key]
k += 1
return api_reshaped


Below is the expected input/output from this function:



 foo_input = pd.dataframe({
'batch_data': [{'foo_query': [{'bar_query': 'data'}]}],
'query_spell': ['foo']
})

print foo_input(foo_input)
# expected_output = pd.dataframe({
# 'foo': 'foo_query',
# 'bar': [{'bar_query': 'data'}]
# })


Many thanks!










share|improve this question




















  • 2





    Could you show some data input and expected output?

    – Franco Piccolo
    Nov 13 '18 at 4:48











  • Yes, of course. I just added some additional information on this. I believe I simplified it enough to drop the complexities but please let me know if any additional information will be helpful. Many thanks, Franco.

    – sokeefe
    Nov 13 '18 at 5:06






  • 1





    There are typos there could you validate the that code runs and it gives the expected input and output?

    – Franco Piccolo
    Nov 13 '18 at 5:11











  • Yes, good catch. Believe I just fixed them all...

    – sokeefe
    Nov 13 '18 at 5:21






  • 2





    @sokeefe, Nope you haven't fixed them at all. Your function doesn't work with foo_input. In fact, you haven't tested your code at all, e.g. pd.dataframe doesn't work.

    – jpp
    Nov 13 '18 at 15:11
















-2















I'm working on cleaning up some of my code to make it a bit more pythonic, but I'm wondering if the below could be written in a nicer way with something like an itertools or pandas method. The below code works, however I'm hoping to remove the double for-loop and consolidate a bit of the code for performance reasons.



Ultimately, I'm working with a list of indices that call a Pandas column.



def foo(dataset):
api_reshaped = pd.DataFrame(columns=['foo', 'bar'])
k = 0

for index, _ in dataset.iterrows():
for key in dataset.iloc[index][0][0]:
api_reshaped.loc[k, 'foo'] = key
api_reshaped.loc[k, 'bar'] = dataset.iloc[index][0][0][key]
k += 1
return api_reshaped


Below is the expected input/output from this function:



 foo_input = pd.dataframe({
'batch_data': [{'foo_query': [{'bar_query': 'data'}]}],
'query_spell': ['foo']
})

print foo_input(foo_input)
# expected_output = pd.dataframe({
# 'foo': 'foo_query',
# 'bar': [{'bar_query': 'data'}]
# })


Many thanks!










share|improve this question




















  • 2





    Could you show some data input and expected output?

    – Franco Piccolo
    Nov 13 '18 at 4:48











  • Yes, of course. I just added some additional information on this. I believe I simplified it enough to drop the complexities but please let me know if any additional information will be helpful. Many thanks, Franco.

    – sokeefe
    Nov 13 '18 at 5:06






  • 1





    There are typos there could you validate the that code runs and it gives the expected input and output?

    – Franco Piccolo
    Nov 13 '18 at 5:11











  • Yes, good catch. Believe I just fixed them all...

    – sokeefe
    Nov 13 '18 at 5:21






  • 2





    @sokeefe, Nope you haven't fixed them at all. Your function doesn't work with foo_input. In fact, you haven't tested your code at all, e.g. pd.dataframe doesn't work.

    – jpp
    Nov 13 '18 at 15:11














-2












-2








-2








I'm working on cleaning up some of my code to make it a bit more pythonic, but I'm wondering if the below could be written in a nicer way with something like an itertools or pandas method. The below code works, however I'm hoping to remove the double for-loop and consolidate a bit of the code for performance reasons.



Ultimately, I'm working with a list of indices that call a Pandas column.



def foo(dataset):
api_reshaped = pd.DataFrame(columns=['foo', 'bar'])
k = 0

for index, _ in dataset.iterrows():
for key in dataset.iloc[index][0][0]:
api_reshaped.loc[k, 'foo'] = key
api_reshaped.loc[k, 'bar'] = dataset.iloc[index][0][0][key]
k += 1
return api_reshaped


Below is the expected input/output from this function:



 foo_input = pd.dataframe({
'batch_data': [{'foo_query': [{'bar_query': 'data'}]}],
'query_spell': ['foo']
})

print foo_input(foo_input)
# expected_output = pd.dataframe({
# 'foo': 'foo_query',
# 'bar': [{'bar_query': 'data'}]
# })


Many thanks!










share|improve this question
















I'm working on cleaning up some of my code to make it a bit more pythonic, but I'm wondering if the below could be written in a nicer way with something like an itertools or pandas method. The below code works, however I'm hoping to remove the double for-loop and consolidate a bit of the code for performance reasons.



Ultimately, I'm working with a list of indices that call a Pandas column.



def foo(dataset):
api_reshaped = pd.DataFrame(columns=['foo', 'bar'])
k = 0

for index, _ in dataset.iterrows():
for key in dataset.iloc[index][0][0]:
api_reshaped.loc[k, 'foo'] = key
api_reshaped.loc[k, 'bar'] = dataset.iloc[index][0][0][key]
k += 1
return api_reshaped


Below is the expected input/output from this function:



 foo_input = pd.dataframe({
'batch_data': [{'foo_query': [{'bar_query': 'data'}]}],
'query_spell': ['foo']
})

print foo_input(foo_input)
# expected_output = pd.dataframe({
# 'foo': 'foo_query',
# 'bar': [{'bar_query': 'data'}]
# })


Many thanks!







python pandas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 5:16







sokeefe

















asked Nov 13 '18 at 4:45









sokeefesokeefe

399422




399422








  • 2





    Could you show some data input and expected output?

    – Franco Piccolo
    Nov 13 '18 at 4:48











  • Yes, of course. I just added some additional information on this. I believe I simplified it enough to drop the complexities but please let me know if any additional information will be helpful. Many thanks, Franco.

    – sokeefe
    Nov 13 '18 at 5:06






  • 1





    There are typos there could you validate the that code runs and it gives the expected input and output?

    – Franco Piccolo
    Nov 13 '18 at 5:11











  • Yes, good catch. Believe I just fixed them all...

    – sokeefe
    Nov 13 '18 at 5:21






  • 2





    @sokeefe, Nope you haven't fixed them at all. Your function doesn't work with foo_input. In fact, you haven't tested your code at all, e.g. pd.dataframe doesn't work.

    – jpp
    Nov 13 '18 at 15:11














  • 2





    Could you show some data input and expected output?

    – Franco Piccolo
    Nov 13 '18 at 4:48











  • Yes, of course. I just added some additional information on this. I believe I simplified it enough to drop the complexities but please let me know if any additional information will be helpful. Many thanks, Franco.

    – sokeefe
    Nov 13 '18 at 5:06






  • 1





    There are typos there could you validate the that code runs and it gives the expected input and output?

    – Franco Piccolo
    Nov 13 '18 at 5:11











  • Yes, good catch. Believe I just fixed them all...

    – sokeefe
    Nov 13 '18 at 5:21






  • 2





    @sokeefe, Nope you haven't fixed them at all. Your function doesn't work with foo_input. In fact, you haven't tested your code at all, e.g. pd.dataframe doesn't work.

    – jpp
    Nov 13 '18 at 15:11








2




2





Could you show some data input and expected output?

– Franco Piccolo
Nov 13 '18 at 4:48





Could you show some data input and expected output?

– Franco Piccolo
Nov 13 '18 at 4:48













Yes, of course. I just added some additional information on this. I believe I simplified it enough to drop the complexities but please let me know if any additional information will be helpful. Many thanks, Franco.

– sokeefe
Nov 13 '18 at 5:06





Yes, of course. I just added some additional information on this. I believe I simplified it enough to drop the complexities but please let me know if any additional information will be helpful. Many thanks, Franco.

– sokeefe
Nov 13 '18 at 5:06




1




1





There are typos there could you validate the that code runs and it gives the expected input and output?

– Franco Piccolo
Nov 13 '18 at 5:11





There are typos there could you validate the that code runs and it gives the expected input and output?

– Franco Piccolo
Nov 13 '18 at 5:11













Yes, good catch. Believe I just fixed them all...

– sokeefe
Nov 13 '18 at 5:21





Yes, good catch. Believe I just fixed them all...

– sokeefe
Nov 13 '18 at 5:21




2




2





@sokeefe, Nope you haven't fixed them at all. Your function doesn't work with foo_input. In fact, you haven't tested your code at all, e.g. pd.dataframe doesn't work.

– jpp
Nov 13 '18 at 15:11





@sokeefe, Nope you haven't fixed them at all. Your function doesn't work with foo_input. In fact, you haven't tested your code at all, e.g. pd.dataframe doesn't work.

– jpp
Nov 13 '18 at 15:11












1 Answer
1






active

oldest

votes


















0














You can use list comprehension with transpose:



# your input data
foo_input = pd.DataFrame({
'batch_data': [{'foo_query': [{'bar_query': 'data'}]}],
'query_spell': ['foo']
})

# use list comprehension with transpose
df = pd.DataFrame([item for item in foo_input['batch_data']]).T.reset_index()

# rename your columns
df.columns = ['Foo', 'Bar']

Foo Bar
0 foo_query [{'bar_query': 'data'}]


you can use applymap with a lambda function if you want to remove the list and just have a dict






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%2f53273967%2fiterating-over-two-pandas-lists%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can use list comprehension with transpose:



    # your input data
    foo_input = pd.DataFrame({
    'batch_data': [{'foo_query': [{'bar_query': 'data'}]}],
    'query_spell': ['foo']
    })

    # use list comprehension with transpose
    df = pd.DataFrame([item for item in foo_input['batch_data']]).T.reset_index()

    # rename your columns
    df.columns = ['Foo', 'Bar']

    Foo Bar
    0 foo_query [{'bar_query': 'data'}]


    you can use applymap with a lambda function if you want to remove the list and just have a dict






    share|improve this answer






























      0














      You can use list comprehension with transpose:



      # your input data
      foo_input = pd.DataFrame({
      'batch_data': [{'foo_query': [{'bar_query': 'data'}]}],
      'query_spell': ['foo']
      })

      # use list comprehension with transpose
      df = pd.DataFrame([item for item in foo_input['batch_data']]).T.reset_index()

      # rename your columns
      df.columns = ['Foo', 'Bar']

      Foo Bar
      0 foo_query [{'bar_query': 'data'}]


      you can use applymap with a lambda function if you want to remove the list and just have a dict






      share|improve this answer




























        0












        0








        0







        You can use list comprehension with transpose:



        # your input data
        foo_input = pd.DataFrame({
        'batch_data': [{'foo_query': [{'bar_query': 'data'}]}],
        'query_spell': ['foo']
        })

        # use list comprehension with transpose
        df = pd.DataFrame([item for item in foo_input['batch_data']]).T.reset_index()

        # rename your columns
        df.columns = ['Foo', 'Bar']

        Foo Bar
        0 foo_query [{'bar_query': 'data'}]


        you can use applymap with a lambda function if you want to remove the list and just have a dict






        share|improve this answer















        You can use list comprehension with transpose:



        # your input data
        foo_input = pd.DataFrame({
        'batch_data': [{'foo_query': [{'bar_query': 'data'}]}],
        'query_spell': ['foo']
        })

        # use list comprehension with transpose
        df = pd.DataFrame([item for item in foo_input['batch_data']]).T.reset_index()

        # rename your columns
        df.columns = ['Foo', 'Bar']

        Foo Bar
        0 foo_query [{'bar_query': 'data'}]


        you can use applymap with a lambda function if you want to remove the list and just have a dict







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 15:20

























        answered Nov 13 '18 at 15:06









        ChrisChris

        2,1812418




        2,1812418






























            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%2f53273967%2fiterating-over-two-pandas-lists%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

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation