{"id":73,"date":"2024-10-29T10:08:19","date_gmt":"2024-10-29T09:08:19","guid":{"rendered":"http:\/\/blog.ctr24.co.pl\/?p=73"},"modified":"2024-11-06T08:52:05","modified_gmt":"2024-11-06T07:52:05","slug":"iteracja-po-pandas","status":"publish","type":"post","link":"https:\/\/blog.ctr24.co.pl\/?p=73","title":{"rendered":"Iteracja po pandas"},"content":{"rendered":"\n<p>Na podstawie: <a href=\"https:\/\/youtu.be\/CG3EV7UBELA?list=PLt6QwRlruAr_AKW1iCyHVdCiL_Jh2POkA\" data-type=\"link\" data-id=\"https:\/\/youtu.be\/CG3EV7UBELA?list=PLt6QwRlruAr_AKW1iCyHVdCiL_Jh2POkA\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/youtu.be\/CG3EV7UBELA?list=PLt6QwRlruAr_AKW1iCyHVdCiL_Jh2POkA<\/a><\/p>\n\n\n\n<p>Metoda 1.<br>Iteracja za pomoc\u0105 p\u0119tli:<\/p>\n\n\n<div style=\"margin-bottom:50px\" class=\"wp-block-cblocks-coder-block\"><pre class=\"im-coder-block\" data-mode=\"python\" data-theme=\"twilight\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\">def iterrow_example(df, col):\n    for i, row in df.iterrows():\n        val = row[col]\n        if val &lt; .5:\n           df.at[i,col] =0\n        else:\n           df.at[i,col] =1\ntimeit iterrow_example(df,'A') ->528 ms<\/pre><\/div>\n\n\n<p>Metoda 2.<br>Iteracja przed index:<\/p>\n\n\n<div style=\"margin-bottom:50px\" class=\"wp-block-cblocks-coder-block\"><pre class=\"im-coder-block\" data-mode=\"python\" data-theme=\"twilight\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\">def iloc_example(df, col):\n    for i in df.index:\n        val = df[col].iloc[i]\n        if val &lt; .5:\n           df.at[i,col] =0\n        else:\n           df.at[i,col] =1\ntimeit iloc_example(df,'B') ->170 ms<\/pre><\/div>\n\n\n<p>Metoda 3<br>U\u017cycie Lambdy<\/p>\n\n\n<div style=\"margin-bottom:50px\" class=\"wp-block-cblocks-coder-block\"><pre class=\"im-coder-block\" data-mode=\"python\" data-theme=\"twilight\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\">timeit df['C'] = df['C'].apply(labmda x : 0 if x &lt; .5 else 1) ->3.24 ms<\/pre><\/div>\n\n\n<p>Metoda 4<br>U\u017cycie metody numpy WHERE<\/p>\n\n\n<div style=\"margin-bottom:50px\" class=\"wp-block-cblocks-coder-block\"><pre class=\"im-coder-block\" data-mode=\"python\" data-theme=\"twilight\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\">timeit df['D'] = np.where(df['D'] &lt; .5 , 0, 1) ->207 us<\/pre><\/div>\n\n\n<p>Metoda 5<br>U\u017cycie metody numpy WHERE i danych jako wartosci (.values)<\/p>\n\n\n<div style=\"margin-bottom:50px\" class=\"wp-block-cblocks-coder-block\"><pre class=\"im-coder-block\" data-mode=\"python\" data-theme=\"twilight\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\">timeit df['D'] = np.where(df['D'].values &lt; .5 , 0, 1) ->92.1 us<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Na podstawie: https:\/\/youtu.be\/CG3EV7UBELA?list=PLt6QwRlruAr_AKW1iCyHVdCiL_Jh2POkA Metoda 1.Iteracja za pomoc\u0105 p\u0119tli: Metoda 2.Iteracja przed index: Metoda 3U\u017cycie Lambdy Metoda 4U\u017cycie metody numpy WHERE<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[25,26,22,24,21,23],"class_list":["post-73","post","type-post","status-publish","format-standard","hentry","category-python","tag-df-index","tag-df-iterrow","tag-iteracja","tag-lambda","tag-pandas","tag-python"],"_links":{"self":[{"href":"https:\/\/blog.ctr24.co.pl\/index.php?rest_route=\/wp\/v2\/posts\/73","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ctr24.co.pl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ctr24.co.pl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ctr24.co.pl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ctr24.co.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=73"}],"version-history":[{"count":10,"href":"https:\/\/blog.ctr24.co.pl\/index.php?rest_route=\/wp\/v2\/posts\/73\/revisions"}],"predecessor-version":[{"id":87,"href":"https:\/\/blog.ctr24.co.pl\/index.php?rest_route=\/wp\/v2\/posts\/73\/revisions\/87"}],"wp:attachment":[{"href":"https:\/\/blog.ctr24.co.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ctr24.co.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ctr24.co.pl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}