我如何从嵌套的for循环中创建熊猫数据框。在上述问题中,我想创建一个我在那打印的数据框。

df:
    col1     col2
0   Country   County
1   State     stats
2   City      PARK
3   park      parking
4   site      Cite
from fuzzywuzzy import fuzz
for i in df.col1:
  for j in df.col2:
    print(i,j,fuzz.token_set_ratio(i,j))

最佳答案

append创建列表并传递给DataFrame构造函数:

from fuzzywuzzy import fuzz
L = []
for i in df.col1:
  for j in df.col2:
    L.append([i,j,fuzz.token_set_ratio(i,j)])


或使用列表理解:

from fuzzywuzzy import fuzz
L = [[i,j,fuzz.token_set_ratio(i,j)] for i in df.col1 for j in df.col2]




df = pd.DataFrame(L, columns=['col1','col2','score'])
print (df)
       col1     col2  score
0   Country   County     92
1   Country    stats     17
2   Country     PARK     18
3   Country  parking     14
4   Country     Cite     36
5     State   County     18
6     State    stats     80
7     State     PARK     22
8     State  parking     17
9     State     Cite     44
10     City   County     60
11     City    stats     22
12     City     PARK      0
13     City  parking     18
14     City     Cite     75
15     park   County      0
16     park    stats     22
17     park     PARK    100
18     park  parking     73
19     park     Cite      0
20     site   County     20
21     site    stats     44
22     site     PARK      0
23     site  parking     18
24     site     Cite     75

10-06 00:55