id
和st
id = [243,2352,474, 84,443]
st = [1,3,5,9,2,6,7]
我希望使用它们创建一个熊猫数据框df
,以使列表id
的每个值都具有st
列表中的所有值。
我的预期输出是:
id st
243 1
243 3
243 5
243 9
243 2
243 6
243 7
2352 1
2352 3
2352 5
2352 9
2352 2
2352 6
2352 7
以此类推...
如何创建相同的熊猫数据框?
答案 0 :(得分:0)
将itertools.product
与DataFrame
构造函数一起使用:
from itertools import product
#pandas 0.24+
df = pd.DataFrame(product(id, st), columns = ['id','st'])
#pandas below
#df = pd.DataFrame(list(product(id, st)), columns = ['id','st'])
print (df)
id st
0 243 1
1 243 3
2 243 5
3 243 9
4 243 2
5 243 6
6 243 7
7 2352 1
8 2352 3
9 2352 5
10 2352 9
11 2352 2
12 2352 6
13 2352 7
14 474 1
15 474 3
16 474 5
17 474 9
18 474 2
19 474 6
20 474 7
21 84 1
22 84 3
23 84 5
24 84 9
25 84 2
26 84 6
27 84 7
28 443 1
29 443 3
30 443 5
31 443 9
32 443 2
33 443 6
34 443 7
答案 1 :(得分:0)
将list comprehension
与pandas.DataFrame
构造函数一起使用:
df = pd.DataFrame([(i, s) for i in id for s in st], columns=['id', 'st'])
[出]
id st
0 243 1
1 243 3
2 243 5
3 243 9
4 243 2
5 243 6
6 243 7
7 2352 1
8 2352 3
9 2352 5
...
25 84 2
26 84 6
27 84 7
28 443 1
29 443 3
30 443 5
31 443 9
32 443 2
33 443 6
34 443 7
答案 2 :(得分:0)
如果有帮助,请尝试以下代码:
import scrapy
class TmcnfSpider(scrapy.Spider):
name = "teslamotorsclub"
start_urls = ["https://teslamotorsclub.com/tmc/post-ratings/6/posts"]
def parse(self,response):
payload = {'time_chooser':'4','_xfToken':''}
yield scrapy.FormRequest(response.url,formdata=payload,callback=self.parse_results)
def parse_results(self,response):
for items in response.css("h3.title > a::text").getall():
yield {"title":items.strip()}