0

In my code, if the list katalogLinks is empty, I want to add a new row to the df with the url and a 0 value for the potential client column.

Else, if the len is more than 0, I want to add a new row with the url and a 1 value for the potential client column.

def findInfo(url, df):
     katalogLinks = ["firdt", "es"]
    if len(katalogLinks) == 0: 
        df["Company URL"] = url
        df["Potential Client"] = 0
    else:
        print("nullfjo")
        df["Company URL"] = url
        df["Potential Client"] = 1

    

df = pd.DataFrame()
df["Company URL"] = ""
df["Potential Client"] = ""
findInfo("https://www.eltako.de/", df)
df.head()

I am passing a new df into my function. However, the df.head() never shows any rows. Perhaps because the changes to the df are not saved. How can I fix this? I donot want to return the df value from the function itself since I might have to use the findInfo function in a loop. Hence, I want that everytime, it just adds values to the next possible row. Is it possible?

I tried this too but I still get an empty df

        df.append(pd.DataFrame({"Company URL":[url],
                    "Potential Client":[1]}))
x89
  • 2,798
  • 5
  • 46
  • 110
  • Does this answer your question? [Create pandas Dataframe by appending one row at a time](https://stackoverflow.com/questions/10715965/create-pandas-dataframe-by-appending-one-row-at-a-time) – Paul Brennan Jun 18 '21 at 12:32
  • Not really, for now I am not using iterations. Just trying to make changes globally. @PaulBrennan – x89 Jun 18 '21 at 13:21

1 Answers1

1

I tried the code you suggested for appending data, and it adds a new row if you test it with a simple string:

        df.append(pd.DataFrame({"Company URL":["ur"l],
                "Potential Client":[0]}))

I think you should try debugging the "getKatalogLinks" function.

Edit

Try this code:

df = pd.DataFrame(columns = ["Company URL","Potential Client"])

katalogLinks = ["firdt", "es"]
def findInfo(url, df):
     if len(katalogLinks) == 0: 
        df = df.append({'Company URL' : url,
                    'Potential Client' : 0} , 
                    ignore_index=True)
     else:
        print("nullfjo")
        df = df.append({'Company URL' : url,
                    'Potential Client' : 1} , 
                    ignore_index=True) 
        return df

findInfo("https://www.eltako.de/", df)
  • it is not the function. I tried replacing the function with a simple list. It is probably how the df is passed or returned. How did you try? Maybe you can try the edited snippet – x89 Jun 18 '21 at 13:32
  • I tried the edited snippet. It returns an empty dataframe. Regarding what I tried before, I used the string "url" and I could append it using the code: df.append(pd.DataFrame({"Company URL":["ur"], "Potential Client":[0]})) – Alexandra Irina Jun 18 '21 at 13:38
  • Yeah, that is what the question is about lol, it should return my return the url and 0/1. Not an empty df – x89 Jun 18 '21 at 13:39
  • append it how? Like within the function I wrote? Or outside it simply. I need to do it within the function – x89 Jun 18 '21 at 13:40
  • @x89, I edited my original answer. Is it working on your end? – Alexandra Irina Jun 18 '21 at 14:23