Pandas: append dataframe to another df
<p>I have a problem with appending of dataframe.
I try to execute this code</p>

<pre><code>df_all = pd.read_csv('data.csv', error_bad_lines=False, chunksize=1000000)
urls = pd.read_excel('url_june.xlsx')
substr = urls.url.values.tolist()
df_res = pd.DataFrame()
for df in df_all:
    for i in substr:
        res = df[df['url'].str.contains(i)]
        df_res.append(res)
</code></pre>

<p>And when I try to save <code>df_res</code> I get empty dataframe.
<code>df_all</code> looks like </p>

<pre><code>ID,"url","used_at","active_seconds"
b20f9412f914ad83b6611d69dbe3b2b4,"mobiguru.ru/phones/apple/comp/32gb/apple_iphone_5s.html",2015-10-01 00:00:25,1
b20f9412f914ad83b6611d69dbe3b2b4,"mobiguru.ru/phones/apple/comp/32gb/apple_iphone_5s.html",2015-10-01 00:00:31,30
f85ce4b2f8787d48edc8612b2ccaca83,"4pda.ru/forum/index.php?showtopic=634566&amp;view=getnewpost",2015-10-01 00:01:49,2
d3b0ef7d85dbb4dbb75e8a5950bad225,"shop.mts.ru/smartfony/mts/smartfon-smart-sprint-4g-sim-lock-white.html?utm_source=admitad&amp;utm_medium=cpa&amp;utm_content=300&amp;utm_campaign=gde_cpa&amp;uid=3",2015-10-01 00:03:19,34
078d388438ebf1d4142808f58fb66c87,"market.yandex.ru/product/12675734/spec?hid=91491&amp;track=char",2015-10-01 00:03:48,2
d3b0ef7d85dbb4dbb75e8a5950bad225,"avito.ru/yoshkar-ola/telefony/mts",2015-10-01 00:04:21,4
d3b0ef7d85dbb4dbb75e8a5950bad225,"shoppingcart.aliexpress.com/order/confirm_order",2015-10-01 00:04:25,1
d3b0ef7d85dbb4dbb75e8a5950bad225,"shoppingcart.aliexpress.com/order/confirm_order",2015-10-01 00:04:26,9
</code></pre>

<p>and <code>urls</code> looks like</p>

<pre><code>url
shoppingcart.aliexpress.com/order/confirm_order
ozon.ru/?context=order_done&amp;number=
lk.wildberries.ru/basket/orderconfirmed
lamoda.ru/checkout/onepage/success/quick
mvideo.ru/confirmation?_requestid=
eldorado.ru/personal/order.php?step=confirm
</code></pre>

<p>When I print <code>res</code> in a loop it doesn't empty. But when I try print in a loop <code>df_res</code> after append, it return empty dataframe.
I can't find my error. How can I fix it?</p>

<p>If you look at <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.append.html" rel="nofollow">the documentation for <code>pd.DataFrame.append</code></a></p>

<blockquote>
  <p>Append rows of other to the end of this frame, <strong>returning a new object</strong>. Columns not in this frame are added as new columns.</p>
</blockquote>

<p>(emphasis mine). </p>

<p>Try </p>

<pre><code>df_res = df_res.append(res)
</code></pre>

<hr>

<p>Incidentally, note that pandas isn't that efficient for creating a DataFrame by successive concatenations. You might try this, instead:</p>

<pre><code>all_res = []
for df in df_all:
    for i in substr:
        res = df[df['url'].str.contains(i)]
        all_res.append(res)

df_res = pd.concat(all_res)
</code></pre>

<p>This first creates a list of all the parts, then creates a DataFrame from all of them once at the end.</p>

python pandas