Issue
I am collecting data from several tables using pandas. I have a list of values from which I would like to add to each row in the table in a loop in the form of a column. And I can't figure out how to do it.
Now the code for combining tables into one looks like this
result_table = []
for url in urls_list:
response = s2.get(url=url, headers=headers)
soup2 = BS(response.text, 'lxml')
try:
table = pd.read_html(url)
except:
print('table not exist')
continue
result_table.append(table)
final_table = pd.DataFrame()
for t in result_table:
final_table = final_table.append(t)
final_table.to_excel("razmeri.xlsx")
The final_table looks like this:
| 1 | RowTable1 |
| 2 | RowTable1 |
| 3 | RowTable2 |
| 4 | RowTable2 |
| 5 | ......... |
I would like to add list
list = ['259, 178, 305, .....']
and to each row of the table from result_table = value from the list
the final_table should look like this:
| 259 | RowTable1 |
| 259 | RowTable1 |
| 178 | RowTable2 |
| 178 | RowTable2 |
| 305 | RowTable3 |
| 305 | RowTable3 |
I can't figure out how to do it in any way I will be very grateful if someone helps
Version pandas: 1.3.1 python: 3.8.0
Solution
Suppose you have this dataframe:
Col1 Col2
0 1 RowTable1
1 2 RowTable1
2 3 RowTable2
3 4 RowTable2
4 5 RowTable3
Then you can do:
lst = [259, 178, 305]
it = iter(lst)
df["Col1"] = df.groupby("Col2")["Col1"].transform(lambda x: [next(it, None)] * len(x))
print(df)
Prints:
Col1 Col2
0 259 RowTable1
1 259 RowTable1
2 178 RowTable2
3 178 RowTable2
4 305 RowTable3
Answered By - Andrej Kesely
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.