3

I am trying to generate links using a for loop and trying to add them to my empty dataframe like the example below:

linkdf = pd.DataFrame(columns=['Link'])


for i in range(1,10):
  l = 'https://google.com/assets/' + str(i)
  linkdf = linkdf.append(l)

but I am getting errors like below:

TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid

is there a way to add this to the empty dataframe with header already provided.

want result like below:

        Link
0.  https://google.com/assets/1
1   https://google.com/assets/2
2.  https://google.com/assets/3

Appreciate any help Thanks

2
  • Replace your append statement by : linkdf.loc[len(linkdf)] = [l] Commented Sep 20, 2021 at 10:13
  • Is there a reason why you really want to use a loop? Commented Sep 20, 2021 at 10:21

2 Answers 2

1

First create list and then pass to DataFrame like:

L = ['https://google.com/assets/' + str(i) for i in range(1,10)]
linkdf = pd.DataFrame(L,  columns=['Link'])

Your solution:

L = []
for i in range(1,10):
  l = 'https://google.com/assets/' + str(i)
  L.append(l)


print (L)
['https://google.com/assets/1', 'https://google.com/assets/2', 
 'https://google.com/assets/3', 'https://google.com/assets/4',  
 'https://google.com/assets/5', 'https://google.com/assets/6', 
 'https://google.com/assets/7', 'https://google.com/assets/8', 
 'https://google.com/assets/9']

linkdf = pd.DataFrame(L,  columns=['Link'])
print (linkdf)
                          Link
0  https://google.com/assets/1
1  https://google.com/assets/2
2  https://google.com/assets/3
3  https://google.com/assets/4
4  https://google.com/assets/5
5  https://google.com/assets/6
6  https://google.com/assets/7
7  https://google.com/assets/8
8  https://google.com/assets/9

If really need add data to empty DataFrame (it is slow):

linkdf = pd.DataFrame(columns=['Link'])

for i in range(1,10):
  l = 'https://google.com/assets/' + str(i)
  linkdf = linkdf.append({'Link':l}, ignore_index=True)
Sign up to request clarification or add additional context in comments.

3 Comments

hi jezrael thank you for getting back i want to use your second solution but got this error"TypeError: append() takes no keyword arguments"
@Zac - There is used L.append(l) instead linkdf = linkdf.append ?
@Zac - What is print (L) ? It is list?
1

Or why not just np.char.add:

linkdf['Link'] = np.char.add('https://google.com/assets/', np.arange(1, 10).astype(str))

>>> linkdf
                          Link
0  https://google.com/assets/1
1  https://google.com/assets/2
2  https://google.com/assets/3
3  https://google.com/assets/4
4  https://google.com/assets/5
5  https://google.com/assets/6
6  https://google.com/assets/7
7  https://google.com/assets/8
8  https://google.com/assets/9
>>> 

3 Comments

sorry u12-forward this might not be the solution i am looking for but thank you for giving time
@Zac Hmmm, why?
this is small part issue of my large code and changing it with your solution will have to change my whole code thank you again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.