<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: Python FOR loop does'nt work fine in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/python-for-loop-does-nt-work-fine/qaa-p/14091770#M4912595</link>
    <description>&lt;P&gt;The logic of the code is slightly confusing for me, like why do you assign&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;data.loc[i,'HIERCUST4'] = w_cust&lt;/LI-CODE&gt;&lt;P&gt;to all rows in every loop oj `j`...&lt;/P&gt;&lt;P&gt;...but if I read your requirements right, and `data` and `data_h` are both Pandas dataframes, then how about trying this code?&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Iterate through each customer from data_h
for _, row_h in data_h.iterrows():
    w_cust = row_h['KUNNR']

    # Boolean mask for matching HKUNNR
    mask = data['HKUNNR'] == w_cust

    # Conditional assignments only where the mask is True
    data.loc[mask, 'HIERCUST1'] = data.loc[mask, 'HKUNNR']
    data.loc[mask, 'HIERCUST2'] = data.loc[mask, 'KUNNR']
    data.loc[mask, 'HIERCUST4'] = w_cust&lt;/LI-CODE&gt;&lt;P&gt;Regards.&lt;/P&gt;</description>
    <pubDate>Fri, 02 May 2025 08:44:39 GMT</pubDate>
    <dc:creator>Vitaliy-R</dc:creator>
    <dc:date>2025-05-02T08:44:39Z</dc:date>
    <item>
      <title>Python FOR loop does'nt work fine</title>
      <link>https://community.sap.com/t5/technology-q-a/python-for-loop-does-nt-work-fine/qaq-p/14091716</link>
      <description>&lt;DIV&gt;Hi Experts&lt;/DIV&gt;&lt;DIV&gt;I'm facing an issue when I try to use the FOR Loop in my datasphere data flow script. My goal is to fill a&amp;nbsp;table with hierarchies links of father-childrens from KNVH. I use a dataphrame with 9000 rows.&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;For the test, a filter on the father&amp;nbsp;&lt;SPAN&gt;0001010970 is done. As you can see, we have 8 childrens&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="client pere.png" style="width: 486px;"&gt;&lt;img src="https://community.sap.com/t5/image/serverpage/image-id/257000iBB83C8912E7D7C86/image-size/large?v=v2&amp;amp;px=999" role="button" title="client pere.png" alt="client pere.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;The first step was to be sure that the FOR Loop runs fine and it is.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image (14).png" style="width: 764px;"&gt;&lt;img src="https://community.sap.com/t5/image/serverpage/image-id/257001i4778E045B9136557/image-size/large?v=v2&amp;amp;px=999" role="button" title="image (14).png" alt="image (14).png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image (15).png" style="width: 945px;"&gt;&lt;img src="https://community.sap.com/t5/image/serverpage/image-id/257004i4B7A726A0D1786A7/image-size/large?v=v2&amp;amp;px=999" role="button" title="image (15).png" alt="image (15).png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;For each row, the link is done.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;But the goal is to iterate through a table of parents and find all the childrens. When we move on to the normal code, we notice that some lines are not being processed...&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image (16).png" style="width: 638px;"&gt;&lt;img src="https://community.sap.com/t5/image/serverpage/image-id/257003i28448CB77772999A/image-size/large?v=v2&amp;amp;px=999" role="button" title="image (16).png" alt="image (16).png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image (17).png" style="width: 945px;"&gt;&lt;img src="https://community.sap.com/t5/image/serverpage/image-id/257002i43D2DB4266DA16B7/image-size/large?v=v2&amp;amp;px=999" role="button" title="image (17).png" alt="image (17).png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;As you can see some lines are not processed... They don't even go through the FOR loop. I don't know why they are ignored...&lt;/DIV&gt;&lt;DIV&gt;I've turned the code around in circles without success. I've used different instructions without success.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Do you have any Ideas please? Need your help&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Best regards&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 02 May 2025 21:12:27 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/python-for-loop-does-nt-work-fine/qaq-p/14091716</guid>
      <dc:creator>Ricobye</dc:creator>
      <dc:date>2025-05-02T21:12:27Z</dc:date>
    </item>
    <item>
      <title>Re: Python FOR loop does'nt work fine</title>
      <link>https://community.sap.com/t5/technology-q-a/python-for-loop-does-nt-work-fine/qaa-p/14091770#M4912595</link>
      <description>&lt;P&gt;The logic of the code is slightly confusing for me, like why do you assign&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;data.loc[i,'HIERCUST4'] = w_cust&lt;/LI-CODE&gt;&lt;P&gt;to all rows in every loop oj `j`...&lt;/P&gt;&lt;P&gt;...but if I read your requirements right, and `data` and `data_h` are both Pandas dataframes, then how about trying this code?&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Iterate through each customer from data_h
for _, row_h in data_h.iterrows():
    w_cust = row_h['KUNNR']

    # Boolean mask for matching HKUNNR
    mask = data['HKUNNR'] == w_cust

    # Conditional assignments only where the mask is True
    data.loc[mask, 'HIERCUST1'] = data.loc[mask, 'HKUNNR']
    data.loc[mask, 'HIERCUST2'] = data.loc[mask, 'KUNNR']
    data.loc[mask, 'HIERCUST4'] = w_cust&lt;/LI-CODE&gt;&lt;P&gt;Regards.&lt;/P&gt;</description>
      <pubDate>Fri, 02 May 2025 08:44:39 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/python-for-loop-does-nt-work-fine/qaa-p/14091770#M4912595</guid>
      <dc:creator>Vitaliy-R</dc:creator>
      <dc:date>2025-05-02T08:44:39Z</dc:date>
    </item>
    <item>
      <title>Re: Python FOR loop does'nt work fine</title>
      <link>https://community.sap.com/t5/technology-q-a/python-for-loop-does-nt-work-fine/qaa-p/14091784#M4912598</link>
      <description>Hello&amp;nbsp;&lt;a href="https://community.sap.com/t5/user/viewprofilepage/user-id/2101125"&gt;@Ricobye&lt;/a&gt;&amp;nbsp;, Can you please include the whole script in the question?&amp;nbsp; It is much easier to debug if we can see the whole thing. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Fri, 02 May 2025 08:59:02 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/python-for-loop-does-nt-work-fine/qaa-p/14091784#M4912598</guid>
      <dc:creator>CLTGravesen</dc:creator>
      <dc:date>2025-05-02T08:59:02Z</dc:date>
    </item>
    <item>
      <title>Re: Python FOR loop does'nt work fine</title>
      <link>https://community.sap.com/t5/technology-q-a/python-for-loop-does-nt-work-fine/qaa-p/14092166#M4912651</link>
      <description>&lt;P&gt;Hi Vitaliy-R,&lt;/P&gt;&lt;P&gt;Thank you for your back. I tried your code but it doesnt work. As you can see, I got the same result...Just four rows are valueted&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ricobye_0-1746218847402.png" style="width: 634px;"&gt;&lt;img src="https://community.sap.com/t5/image/serverpage/image-id/257299i598DF628E6085659/image-dimensions/634x241?v=v2" width="634" height="241" role="button" title="Ricobye_0-1746218847402.png" alt="Ricobye_0-1746218847402.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best regards&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 May 2025 20:48:09 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/python-for-loop-does-nt-work-fine/qaa-p/14092166#M4912651</guid>
      <dc:creator>Ricobye</dc:creator>
      <dc:date>2025-05-02T20:48:09Z</dc:date>
    </item>
    <item>
      <title>Re: Python FOR loop does'nt work fine</title>
      <link>https://community.sap.com/t5/technology-q-a/python-for-loop-does-nt-work-fine/qaa-p/14092786#M4912724</link>
      <description>Are there any additional filters outside of the block code that you shared? Or is there any split into packages of the input datasets before the code is executed?</description>
      <pubDate>Sun, 04 May 2025 16:50:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/python-for-loop-does-nt-work-fine/qaa-p/14092786#M4912724</guid>
      <dc:creator>Vitaliy-R</dc:creator>
      <dc:date>2025-05-04T16:50:01Z</dc:date>
    </item>
    <item>
      <title>Re: Python FOR loop does'nt work fine</title>
      <link>https://community.sap.com/t5/technology-q-a/python-for-loop-does-nt-work-fine/qaa-p/14092802#M4912727</link>
      <description>&lt;P&gt;Hi Vitaliy-R&lt;/P&gt;&lt;P&gt;Here is my code:&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def transform(data):
    """
    This function body should contain all the desired transformations on incoming DataFrame. Permitted builtin functions
    as well as permitted NumPy and Pandas objects and functions are available inside this function.
    Permitted NumPy and Pandas objects and functions can be used with aliases 'np' and 'pd' respectively.
    This function executes in a sandbox mode. Please refer the documentation for permitted objects and functions. Using
    any restricted functions or objects would cause an internal exception and result in a pipeline failure.
    Any code outside this function body will not be executed and inclusion of such code is discouraged.
    :param data: Pandas DataFrame
    :return: Pandas DataFrame
    """
    #####################################################
    # Provide the function body for data transformation #
    #####################################################
    data["HIERCUST1"] = ""
    data["HIERCUST2"] = ""
    data["HIERCUST3"] = ""
    data["HIERCUST4"] = ""
    data["HIERCUST5"] = "" 

	H = int(0)
    H1 = int(0)
    nb_ind = int(0)
    w_cust = ""

    #---------------------------------------------------------------------
    #   Noeuds sans parents, tête de hierarchie niveau 1
    #---------------------------------------------------------------------
    data.sort_values('HKUNNR')
    
    for i in range(data.shape[0]):
        if data['HKUNNR'][i] == "":
            nb_ind = nb_ind + 1
        
    ind = np.arange(nb_ind)
    
    #-----Création d'une table temporaire pour les entetes de hierarchie
    
    data_h = pd.DataFrame(columns=['KUNNR'], index = ind)
    
    #-----Alimentation de la table de têtes de hierarchie
    
    for i in range(data.shape[0]):
        if data['HKUNNR'][i] == "":
            data.loc[i,'HIERCUST1'] = data['KUNNR'][i]

            new_row = 'KUNNR':data['KUNNR'][i]
            data_h.loc[len(data_h)] = new_row
			

    #---------------------------------------------------------------------
    #   Hierarchie niveau 2, enfants tête de hierarchie niveau 1
    #---------------------------------------------------------------------
  
    data.sort_values('KUNNR')
    data_h.sort_values('KUNNR')
    
    """    
    for i, row in data.iterrows():
        if row ['HKUNNR'] == "0001010970":
            data.loc[i,'HIERCUST1'] = row ['HKUNNR']
            data.loc[i,'HIERCUST2'] = row ['KUNNR']
    

    for j in list(range(0, len(data_h))):
    #for j, row in data_h.iterrows():
    #    w_cust = row ['KUNNR']

        w_cust = "0001010970"
        for i, row in data.iterrows():
            data.loc[i,'HIERCUST4'] = w_cust
            
            if row ['HKUNNR'] == w_cust:
                data.loc[i,'HIERCUST1'] = row ['HKUNNR']
                data.loc[i,'HIERCUST2'] = row ['KUNNR']
    
    """
   
    #----------------------------SAP forum-----------
    
    for _, row_h in data_h.iterrows():
        w_cust = row_h['KUNNR']

    # Boolean mask for matching HKUNN
        mask = data['HKUNNR'] == w_cust

    # Conditional assignments only where the mask is True
        data.loc[mask, 'HIERCUST1'] = data.loc[mask, 'HKUNNR']
        data.loc[mask, 'HIERCUST2'] = data.loc[mask, 'KUNNR']
        data.loc[mask, 'HIERCUST4'] = w_cust
		
	#----------------------------SAP forum end-------
	
    return data	&lt;/LI-CODE&gt;&lt;P&gt;As you can see, there is no filter...&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best regards&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 04 May 2025 19:21:06 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/python-for-loop-does-nt-work-fine/qaa-p/14092802#M4912727</guid>
      <dc:creator>Ricobye</dc:creator>
      <dc:date>2025-05-04T19:21:06Z</dc:date>
    </item>
    <item>
      <title>Re: Python FOR loop does'nt work fine</title>
      <link>https://community.sap.com/t5/technology-q-a/python-for-loop-does-nt-work-fine/qaa-p/14093169#M4912764</link>
      <description>&lt;P&gt;Could this be related to the fact that the data arrives in multiple batches? the code might never see the full dataset.&lt;/P&gt;&lt;P&gt;"In a data flow, the script operator may receive the incoming table in multiple batches of rows, depending on the size of the table. This means that the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class=""&gt;transform&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;function is called multiple times, for each batch of rows, and that its&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class=""&gt;data&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;parameter contains only the rows for data given batch.&lt;/P&gt;&lt;P class=""&gt;Hence, the operations that require the complete table within the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class=""&gt;data&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;parameter are not possible. For example, removing duplicates."&lt;/P&gt;&lt;P&gt;&lt;A href="https://help.sap.com/docs/SAP_DATASPHERE/c8a54ee704e94e15926551293243fd1d/f3e2570966ac4036b552ebd998274af1.html" target="_blank" rel="noopener"&gt;https://help.sap.com/docs/SAP_DATASPHERE/c8a54ee704e94e15926551293243fd1d/f3e2570966ac4036b552ebd998274af1.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 May 2025 08:51:13 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/python-for-loop-does-nt-work-fine/qaa-p/14093169#M4912764</guid>
      <dc:creator>AndreasForster</dc:creator>
      <dc:date>2025-05-05T08:51:13Z</dc:date>
    </item>
  </channel>
</rss>

