So I have two rather large excel file that I have converted into two dataframes (df for the current week & df2 for the previous week.). There are a total of 128 rows that are identical in both of the dataframes, so I've used created a new variable:
onlyWon = df.loc[df['Sales stage'] == "Won"]
Thereafter, I am trying to create a new dataframe that only contains the values in df2 that match the Sales number in the onlyWon dataframe. For example, if I were to do this with only one item the code would be:
df2.loc[df2['Sales No'] == "B3M-RB-03"])
Which works for one column, but when I try to for example iterate over the onlyWon dataframe and append the data to a new dataframe, I run into errors.
Examples on how I want it to work:
DF2:
+------------------+----------+-------------+-----------+
| Customer | Sales No | Sales Stage | Deal Size |
+------------------+----------+-------------+-----------+
| Stackoverflow | A1 | Identified | 100 |
| Guido van Rossum | B2 | Lost | 1000 |
+------------------+----------+-------------+-----------+
OnlyWon:
+---------------+----------+-------------+-----------+
| Customer | Sales No | Sales Stage | Deal Size |
+---------------+----------+-------------+-----------+
| Stackoverflow | A1 | WON | 100 |
+---------------+----------+-------------+-----------+
New dataframe:
+---------------+----------+-------------+-----------+
| Customer | Sales No | Sales Stage | Deal Size |
+---------------+----------+-------------+-----------+
| Stackoverflow | A1 | Identified | 100 |
+---------------+----------+-------------+-----------+
What I tried to do
Declaring a new empty dataframe (df3) that contains all the same headers, but is empty.
Creating a list out of all the 'Sales No':
onlyWonSales = []
for salesNo in onlyWon['Sales No']:
onlyWonSales.append(salesNo)
Then looping over the list and appending to the new dataframe:
for item in onlyWonSales:
df3 = df3.append(df2.loc[df2['Sales No'] == item)
This adds a lot of duplicates and doesn't work (even though it doesn't create any errors (The onlyWonSales list is around 1000 and the df3 is around 4000).