I have a problem with 'loc' and'iloc' .So I have a list and a data frame, I want to check if the data in list and date in data frame of a column is matching. if matching I need to copy it to a new data frame. I am half way but I don't know how to copy the rows from a data frame to new one based on the index. How do we do that?
Code:
d = {'col1': [1, 2,3,5,6], 'col2': [3, 4,5,6,7]}
cols = list(df.columns)
df1=pd.DataFrame(columns=cols)
lst=['1','2']
df1 = pd.DataFrame()
for index,v in df['col1'].iteritems():
for l in lst:
if l == v:
df1 = df.loc[index]
which gives nothing(empty data frame):
col1 col2
Actual output:
df1 =
col1 col2
0 1 3
1 2 4
lst=[1,2]isinand boolean indexingdf.loc[df['col1'].isin(lst)].