I have two pandas dataframes: One with premium customers, df_premium_customer
and one with all sold items, df_sold
, that has as columns
"customerID"(containing the ID's of premium customers as well as others),"ArticleID", "Date"and several others.
This is how df_premium_customer
looks
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<h2>Bordered Table</h2>
<p>Use the CSS border property to add a border to the table.</p>
<table style="width:100%">
<tr>
<th>Premium_CustomerID</th>
</tr>
<tr>
<td>34674324</td>
</tr>
<tr>
<td>18634345</td>
</tr>
<tr>
<td>99744336</td>
</tr>
</table>
</body>
</html>
and this is df_sold
looks
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<h2>Bordered Table</h2>
<p>Use the CSS border property to add a border to the table.</p>
<table style="width:100%">
<tr>
<th>CustimerID</th>
<th>ArticleID</th>
<th>Date</th>
</tr>
<tr>
<td>34674324</td>
<td>3467434</td>
<td>20140302</td>
</tr>
<tr>
<td>98674342</td>
<td>3454234</td>
<td>20140822</td>
</tr>
<tr>
<td>74644334</td>
<td>4444434</td>
<td>20150321</td>
</tr>
</table>
</body>
</html>
For each customer I need to make a datastructure (preliminarily I chose a dict), that shows what has been sold to each premium customer.
So far I'm using the following Python 3 code:
sold_to_customer = {}
for customer in df_premium_customer["CustomerID"]:
#generate the list of indexes of this this customers appears in df_sold
cust_index = df_sold.index[df_sold['CustomerID'] == customer].tolist()
#add this customers as key to the dict
sold_to_customer[customer] = []
for ind in cust_index:
#add the name of the things he bought,when, and for how much as values to this key
sold_to_customer[customer].append(list(df_sold[ind][["ArticleID","Date"]]))
This is way to slow!
Letting it run for a bit and extrapolating it would need 16 hours to complete, since I have 300k premium customers and several millions rows of entries in the sold items dataframe.