0

Fairly new to coding. I have looked at a couple of other similar questions about appending DataFrames in python but could not solve the problem.

I have the data below (CSV) in an excel xls file:

Venue Name,Cost ,Restriction,Capacity
Cinema,5,over 13,50
Bar,10,over 18,50
Restaurant,15,no restriction,25
Hotel,7,no restriction,100

I am using the code below to try to "filter" out rows which have "no restriction" under the restrictions column. The code seems to work right through to the last line i.e. both print statements are giving me what I would expect.

import pandas as pd
import numpy as np

my_file = pd.ExcelFile("venue data.xlsx")
mydata = my_file.parse(0, index_col = None, na_values = ["NA"])

my_new_file = pd.DataFrame()

for index in mydata.index:
    if "no restriction" in mydata.Restriction[index]:
        print (mydata.Restriction[index])
        print (mydata.loc[index:index])
        my_new_file.append(mydata.loc[index:index], ignore_index = True)
Paul H
  • 65,268
  • 20
  • 159
  • 136
user4029
  • 1
  • 4
  • 1
    Is the problem simply that you think `append` acts in-place? It doesn't, see [this](http://stackoverflow.com/questions/16597265/appending-to-an-empty-data-frame-in-pandas). It's not unreasonable to think that it would, though, because that's how `list.append` works. – DSM Mar 10 '15 at 19:26

1 Answers1

0

Don't loop through dataframes. It's almost never necessary.

Use:

df2 = df[df['Restriction'] != 'no restriction']

Or

df2 = df.query("Restriction != 'no restriction'")
Paul H
  • 65,268
  • 20
  • 159
  • 136