'Is it a correct way of doing loops in python?
df = pd.read_excel("file.xlsx", sheet_name = "Sheet1")
for i in range(0, len(df)):
cnt = 0
while True:
cnt += 1
driver.get(df['urls'][0])
'df' is a variable where the excel file contains with a heading 'urls', and it contains 4 urls in a column.
I'd like to open them one by one using loops but I'm not sure this is the correct way of doing it. I tried to run it but didn't work out.
I'll be appreciated if you give some helpful tips cheers!
Solution 1:[1]
It seems like you haven't read the documentation for pandas here. In there you will see that there are builtin methods for dataframes
df = pd.read_excel("file.xlsx", sheet_name = "Sheet1")
for i, row in df.iterrows():
driver.get(row.url)
Or you can to it even simpler!
df.apply(lambda row: driver.get(row.url), axis=1)
This applies the lambda-function to each row in the entiry dataframe.
Everything you will need to know to get startet with python can be found in here https://wiki.python.org/moin/BeginnersGuide
Solution 2:[2]
For while True to work, you need a condition where you end the loop. The solution I'm giving is a valid way to iterate over loops in general. DataFrames have their own iterators that are potentially more performant, but it seems like some basic Python loops will be beneficial to you.
In your code you are doing a for loop with a while loop inside with a counter, but then never referencing that index in your DataFrame. You can just use a for loop without the while loop.
arr = [1, 2, 3]
for i in range(len(arr)):
print(arr[i])
The 0 is implied and you use the i variable to index into your array.
Another way is to use enumerate. It will give you the index and the value every iteration.
for i, value in enumerate(arr):
print(value)
Now if you don't need the index, you can just iterate over the array directly.
for value in arr:
print(value)
For dataframes, they have some iterators that you can use in a for loop. I don't think this matches your data model from your description, but it would give you a general case.
for row in df.iterrows():
print(row['urls'])
You may be able to do:
for url in df['urls']:
print(url)
if 'urls' is a column.
For reference, when you use a while loop, it would need to look something like this.
this is not the right way to iterate over this array
cnt = 0
while cnt < len(arr):
print(arr[cnt])
cnt += 1
Or for while True
cnt = 0
while True:
if cnt >= len(arr):
break
print(arr[cnt])
cnt += 1
Hopefully you can see why the for loops are preferred. Less code and it's cleaner and less prone to mistakes.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | saquintes |
