'Looping and append to dataframe with column names in Pyhton
I had this challenge of looping through and appending to a data frame in 2 separate columns. I would like the result of col1 and col2 to be appended to the data frame under the columns 'col1' & 'col2'.
What i have so far is the below:
result = pd.DataFrame(columns = ['col1', 'col2'])
for i in range(5):
col1 = i + 1
col2 = i + 100
result.append(pd.concat([str(col1), col2]))
Which outputs:
TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid
My desired output is:
| col1 | col2 |
|---|---|
| 1 | 101 |
| 2 | 102 |
| 3 | 103 |
| 4 | 104 |
| 5 | 105 |
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
