I have a for
loop to iterate through every row of a data frame. Now, when certain expressions match, I want to add the matching row to a new data frame.
This is what I have done:
dfWithSand3 = pd.DataFrame()
for index, row in df.iterrows():
if row['embarked'] == 'S' and row['pclass'] == 3:
dfWithSand3.append(row)
print(dfWithSand3)
Here, pd
is pandas
and df
is the data frame.
Whenever the expression below is True
I want to add the row to the data frame dfWithSand3
.
if row['embarked'] == 'S' and row['pclass'] == 3:
Right now the value of the data frame dfWithSand3
is:
Empty DataFrame
Columns: []
Index: []
Below is what a possible row
value could look like
Unnamed: 0 600
survived 0
pclass 3
sex male
age 42
sibsp 0
parch 0
fare 7.55
embarked S
I don't know if row represents a data frame or not.
Also, how would I add row to the new data frame I have created?
IIUC you can do it this way:
or if you don't want to preserve original index values: