Apply function to every subset combination and return square matrix The Next CEO of Stack OverflowMatrix transpose functionString Matching and ClusteringSquare spiral matrixShrinking and expanding squareComputing rowMeans for every combination of columnsPython Pandas Apply with a Lambda FunctionLongest sequence of same subsequent number in a square matrixCreating every possible combination until a code word is foundFilling a matrix with square number digitsGroupby, apply custom function to data, return results in new columns

Workaholic Formal/Informal

How do I go from 300 unfinished/half written blog posts, to published posts?

If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?

A "random" question: usage of "random" as adjective in Spanish

Sending manuscript to multiple publishers

Why does standard notation not preserve intervals (visually)

How fast would a person need to move to trick the eye?

What is ( CFMCC ) on ILS approach chart?

"and that skill is always a class skill for you" - does "always" have any meaning in Pathfinder?

Extending anchors in TikZ

Won the lottery - how do I keep the money?

Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?

Can I equip Skullclamp on a creature I am sacrificing?

Contours of a clandestine nature

What happened in Rome, when the western empire "fell"?

Why am I allowed to create multiple unique pointers from a single object?

What exact does MIB represent in SNMP? How is it different from OID?

What flight has the highest ratio of time difference to flight time?

Preparing Indesign booklet with .psd graphics for print

Why do airplanes bank sharply to the right after air-to-air refueling?

Is it my responsibility to learn a new technology in my own time my employer wants to implement?

Is there a difference between "Fahrstuhl" and "Aufzug"

Why has the US not been more assertive in confronting Russia in recent years?

Novel about a guy who is possessed by the divine essence and the world ends?



Apply function to every subset combination and return square matrix



The Next CEO of Stack OverflowMatrix transpose functionString Matching and ClusteringSquare spiral matrixShrinking and expanding squareComputing rowMeans for every combination of columnsPython Pandas Apply with a Lambda FunctionLongest sequence of same subsequent number in a square matrixCreating every possible combination until a code word is foundFilling a matrix with square number digitsGroupby, apply custom function to data, return results in new columns










0












$begingroup$


I don't know how to do this without four nested for loops.



I'd like to apply a function to every possible combination of subsets for hour and day, return that value, and then pivot the data frame into a square matrix. However, these for loops seem unnecessary so I'm looking for a more efficient way to do this. The data I have is fairly large and takes a long time so any gain in speed would be beneficial.



I took a stab at compression lists but that seems excessive too.



Note: this code runs but will produce NA because all possible combinations are not available.



Sample data



dat = pd.DataFrame('day': 0: 10, 1: 10, 2: 10, 3: 11, 4: 11, 5: 13, 6: 14, 7: 14, 8: 14, 9: 15, 10: 16, 11: 16, 12: 16, 13: 17, 14: 17, 15: 18, 16: 19, 17: 20, 18: 20, 19: 20, 'hour': 0: 0, 1: 19, 2: 22, 3: 14, 4: 16, 5: 5, 6: 1, 7: 18, 8: 20, 9: 8, 10: 6, 11: 14, 12: 15, 13: 2, 14: 6, 15: 12, 16: 22, 17: 0, 18: 3, 19: 4, 'distance': 0: 1.2898851269657656, 1: 0.0, 2: 0.8371526423804061, 3: 0.8703856587273138, 4: 0.6257425922449789, 5: 0.0, 6: 0.0, 7: 0.0, 8: 1.2895328696587023, 9: 0.0, 10: 0.6875527848294374, 11: 0.0, 12: 0.0, 13: 0.9009031833559706, 14: 0.0, 15: 1.1040652963428623, 16: 0.0, 17: 0.0, 18: 0.0, 19: 0.0)


Code



def mean_diff(x, y):
x = pd.Series(x)
y = pd.Series(y)
return x.mean() - y.mean()


dmat = pd.DataFrame()
for i in dat['hour'].unique():
for j in dat['hour'].unique():
for k in dat['day'].unique():
for l in dat['day'].unique():
x = dat[(dat['hour'] == i) & (dat['day'] == k)].distance
y = dat[(dat['hour'] == j) & (dat['day'] == l)].distance

# Calculate difference
jds = mean_diff(x, y)

# Build data frame and append
outdat = pd.DataFrame('day_hour_a': f"k_i", 'day_hour_b': f"l_j", 'jds': [round(jds, 4)])
dmat = dmat.append(outdat, ignore_index=True)

# Pivot data to get matrix
distMatrix = dmat.pivot(index='day_hour_a', columns='day_hour_b', values='jds')








share









$endgroup$
















    0












    $begingroup$


    I don't know how to do this without four nested for loops.



    I'd like to apply a function to every possible combination of subsets for hour and day, return that value, and then pivot the data frame into a square matrix. However, these for loops seem unnecessary so I'm looking for a more efficient way to do this. The data I have is fairly large and takes a long time so any gain in speed would be beneficial.



    I took a stab at compression lists but that seems excessive too.



    Note: this code runs but will produce NA because all possible combinations are not available.



    Sample data



    dat = pd.DataFrame('day': 0: 10, 1: 10, 2: 10, 3: 11, 4: 11, 5: 13, 6: 14, 7: 14, 8: 14, 9: 15, 10: 16, 11: 16, 12: 16, 13: 17, 14: 17, 15: 18, 16: 19, 17: 20, 18: 20, 19: 20, 'hour': 0: 0, 1: 19, 2: 22, 3: 14, 4: 16, 5: 5, 6: 1, 7: 18, 8: 20, 9: 8, 10: 6, 11: 14, 12: 15, 13: 2, 14: 6, 15: 12, 16: 22, 17: 0, 18: 3, 19: 4, 'distance': 0: 1.2898851269657656, 1: 0.0, 2: 0.8371526423804061, 3: 0.8703856587273138, 4: 0.6257425922449789, 5: 0.0, 6: 0.0, 7: 0.0, 8: 1.2895328696587023, 9: 0.0, 10: 0.6875527848294374, 11: 0.0, 12: 0.0, 13: 0.9009031833559706, 14: 0.0, 15: 1.1040652963428623, 16: 0.0, 17: 0.0, 18: 0.0, 19: 0.0)


    Code



    def mean_diff(x, y):
    x = pd.Series(x)
    y = pd.Series(y)
    return x.mean() - y.mean()


    dmat = pd.DataFrame()
    for i in dat['hour'].unique():
    for j in dat['hour'].unique():
    for k in dat['day'].unique():
    for l in dat['day'].unique():
    x = dat[(dat['hour'] == i) & (dat['day'] == k)].distance
    y = dat[(dat['hour'] == j) & (dat['day'] == l)].distance

    # Calculate difference
    jds = mean_diff(x, y)

    # Build data frame and append
    outdat = pd.DataFrame('day_hour_a': f"k_i", 'day_hour_b': f"l_j", 'jds': [round(jds, 4)])
    dmat = dmat.append(outdat, ignore_index=True)

    # Pivot data to get matrix
    distMatrix = dmat.pivot(index='day_hour_a', columns='day_hour_b', values='jds')








    share









    $endgroup$














      0












      0








      0





      $begingroup$


      I don't know how to do this without four nested for loops.



      I'd like to apply a function to every possible combination of subsets for hour and day, return that value, and then pivot the data frame into a square matrix. However, these for loops seem unnecessary so I'm looking for a more efficient way to do this. The data I have is fairly large and takes a long time so any gain in speed would be beneficial.



      I took a stab at compression lists but that seems excessive too.



      Note: this code runs but will produce NA because all possible combinations are not available.



      Sample data



      dat = pd.DataFrame('day': 0: 10, 1: 10, 2: 10, 3: 11, 4: 11, 5: 13, 6: 14, 7: 14, 8: 14, 9: 15, 10: 16, 11: 16, 12: 16, 13: 17, 14: 17, 15: 18, 16: 19, 17: 20, 18: 20, 19: 20, 'hour': 0: 0, 1: 19, 2: 22, 3: 14, 4: 16, 5: 5, 6: 1, 7: 18, 8: 20, 9: 8, 10: 6, 11: 14, 12: 15, 13: 2, 14: 6, 15: 12, 16: 22, 17: 0, 18: 3, 19: 4, 'distance': 0: 1.2898851269657656, 1: 0.0, 2: 0.8371526423804061, 3: 0.8703856587273138, 4: 0.6257425922449789, 5: 0.0, 6: 0.0, 7: 0.0, 8: 1.2895328696587023, 9: 0.0, 10: 0.6875527848294374, 11: 0.0, 12: 0.0, 13: 0.9009031833559706, 14: 0.0, 15: 1.1040652963428623, 16: 0.0, 17: 0.0, 18: 0.0, 19: 0.0)


      Code



      def mean_diff(x, y):
      x = pd.Series(x)
      y = pd.Series(y)
      return x.mean() - y.mean()


      dmat = pd.DataFrame()
      for i in dat['hour'].unique():
      for j in dat['hour'].unique():
      for k in dat['day'].unique():
      for l in dat['day'].unique():
      x = dat[(dat['hour'] == i) & (dat['day'] == k)].distance
      y = dat[(dat['hour'] == j) & (dat['day'] == l)].distance

      # Calculate difference
      jds = mean_diff(x, y)

      # Build data frame and append
      outdat = pd.DataFrame('day_hour_a': f"k_i", 'day_hour_b': f"l_j", 'jds': [round(jds, 4)])
      dmat = dmat.append(outdat, ignore_index=True)

      # Pivot data to get matrix
      distMatrix = dmat.pivot(index='day_hour_a', columns='day_hour_b', values='jds')








      share









      $endgroup$




      I don't know how to do this without four nested for loops.



      I'd like to apply a function to every possible combination of subsets for hour and day, return that value, and then pivot the data frame into a square matrix. However, these for loops seem unnecessary so I'm looking for a more efficient way to do this. The data I have is fairly large and takes a long time so any gain in speed would be beneficial.



      I took a stab at compression lists but that seems excessive too.



      Note: this code runs but will produce NA because all possible combinations are not available.



      Sample data



      dat = pd.DataFrame('day': 0: 10, 1: 10, 2: 10, 3: 11, 4: 11, 5: 13, 6: 14, 7: 14, 8: 14, 9: 15, 10: 16, 11: 16, 12: 16, 13: 17, 14: 17, 15: 18, 16: 19, 17: 20, 18: 20, 19: 20, 'hour': 0: 0, 1: 19, 2: 22, 3: 14, 4: 16, 5: 5, 6: 1, 7: 18, 8: 20, 9: 8, 10: 6, 11: 14, 12: 15, 13: 2, 14: 6, 15: 12, 16: 22, 17: 0, 18: 3, 19: 4, 'distance': 0: 1.2898851269657656, 1: 0.0, 2: 0.8371526423804061, 3: 0.8703856587273138, 4: 0.6257425922449789, 5: 0.0, 6: 0.0, 7: 0.0, 8: 1.2895328696587023, 9: 0.0, 10: 0.6875527848294374, 11: 0.0, 12: 0.0, 13: 0.9009031833559706, 14: 0.0, 15: 1.1040652963428623, 16: 0.0, 17: 0.0, 18: 0.0, 19: 0.0)


      Code



      def mean_diff(x, y):
      x = pd.Series(x)
      y = pd.Series(y)
      return x.mean() - y.mean()


      dmat = pd.DataFrame()
      for i in dat['hour'].unique():
      for j in dat['hour'].unique():
      for k in dat['day'].unique():
      for l in dat['day'].unique():
      x = dat[(dat['hour'] == i) & (dat['day'] == k)].distance
      y = dat[(dat['hour'] == j) & (dat['day'] == l)].distance

      # Calculate difference
      jds = mean_diff(x, y)

      # Build data frame and append
      outdat = pd.DataFrame('day_hour_a': f"k_i", 'day_hour_b': f"l_j", 'jds': [round(jds, 4)])
      dmat = dmat.append(outdat, ignore_index=True)

      # Pivot data to get matrix
      distMatrix = dmat.pivot(index='day_hour_a', columns='day_hour_b', values='jds')






      python performance





      share












      share










      share



      share










      asked 5 mins ago









      AmstellAmstell

      8111




      8111




















          0






          active

          oldest

          votes












          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          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: "196"
          ;
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fcodereview.stackexchange.com%2fquestions%2f216508%2fapply-function-to-every-subset-combination-and-return-square-matrix%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • 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.

          Use MathJax to format equations. MathJax reference.


          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%2fcodereview.stackexchange.com%2fquestions%2f216508%2fapply-function-to-every-subset-combination-and-return-square-matrix%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

          名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

          格濟夫卡 參考資料 导航菜单51°3′40″N 34°2′21″E / 51.06111°N 34.03917°E / 51.06111; 34.03917ГезівкаПогода в селі 编辑或修订