如何找到 3 个数据帧的所有组合并将它们 return 作为列表
How to find all combinations of 3 dataframes and return them as list
假设我有 3 个数据帧。每个数据帧中都有字符串,我想找到这 3 个数据帧的所有可能组合并将它们输出为列表。我在 Whosebug 上搜索了一段时间,尝试了很多代码,但没有一个有效。我尝试导入 intertools、组合等,但仍然有错误的输出或根本没有输出。
def combinator(names,cities,streets) #for example
在我最后一次尝试中,我尝试使用以下代码仅将名称与城市结合起来:
names.merge(cities, how = 'cross')
names['key'] = 1
cities['key'] = 1
combined = pd.merge(names,cities,on='key').drop('key', axis = 1)
但只复制了所有名称。目标是在一个列表中找到名称、城市和街道的所有可能组合,并 return 它们。感谢所有建议。
输入数据帧
这是你想要的吗?
>>> from datar.all import f, tibble, bind_cols, expand, nesting
>>>
>>> df1 = tibble(
... name=["John", "Nick", "Eric"], job=["engineer", "architect", "deisgner"]
... )
>>> df2 = tibble(
... city=["London", "Montresor", "Esslingen"],
... bigness=["captical", "villege", "town"],
... )
>>> df3 = tibble(
... street=["street1", "street2", "street3"],
... population=["high", "low", "average"],
... )
>>>
>>> df = bind_cols(df1, df2, df3)
>>> df >> expand(
... nesting(f.name, f.job),
... nesting(f.city, f.bigness),
... nesting(f.street, f.population),
... )
name job city bigness street population
<object> <object> <object> <object> <object> <object>
0 John engineer London captical street1 high
1 John engineer London captical street2 low
2 John engineer London captical street3 average
3 John engineer Montresor villege street1 high
4 John engineer Montresor villege street2 low
5 John engineer Montresor villege street3 average
6 John engineer Esslingen town street1 high
7 John engineer Esslingen town street2 low
8 John engineer Esslingen town street3 average
9 Nick architect London captical street1 high
10 Nick architect London captical street2 low
11 Nick architect London captical street3 average
12 Nick architect Montresor villege street1 high
13 Nick architect Montresor villege street2 low
14 Nick architect Montresor villege street3 average
15 Nick architect Esslingen town street1 high
16 Nick architect Esslingen town street2 low
17 Nick architect Esslingen town street3 average
18 Eric deisgner London captical street1 high
19 Eric deisgner London captical street2 low
20 Eric deisgner London captical street3 average
21 Eric deisgner Montresor villege street1 high
22 Eric deisgner Montresor villege street2 low
23 Eric deisgner Montresor villege street3 average
24 Eric deisgner Esslingen town street1 high
25 Eric deisgner Esslingen town street2 low
26 Eric deisgner Esslingen town street3 average
我是 datar
的作者,这是一个在 python 中实现数据操作语法的包,以 pandas 和 modin 作为后端。
假设我有 3 个数据帧。每个数据帧中都有字符串,我想找到这 3 个数据帧的所有可能组合并将它们输出为列表。我在 Whosebug 上搜索了一段时间,尝试了很多代码,但没有一个有效。我尝试导入 intertools、组合等,但仍然有错误的输出或根本没有输出。
def combinator(names,cities,streets) #for example
在我最后一次尝试中,我尝试使用以下代码仅将名称与城市结合起来:
names.merge(cities, how = 'cross')
names['key'] = 1
cities['key'] = 1
combined = pd.merge(names,cities,on='key').drop('key', axis = 1)
但只复制了所有名称。目标是在一个列表中找到名称、城市和街道的所有可能组合,并 return 它们。感谢所有建议。
输入数据帧
这是你想要的吗?
>>> from datar.all import f, tibble, bind_cols, expand, nesting
>>>
>>> df1 = tibble(
... name=["John", "Nick", "Eric"], job=["engineer", "architect", "deisgner"]
... )
>>> df2 = tibble(
... city=["London", "Montresor", "Esslingen"],
... bigness=["captical", "villege", "town"],
... )
>>> df3 = tibble(
... street=["street1", "street2", "street3"],
... population=["high", "low", "average"],
... )
>>>
>>> df = bind_cols(df1, df2, df3)
>>> df >> expand(
... nesting(f.name, f.job),
... nesting(f.city, f.bigness),
... nesting(f.street, f.population),
... )
name job city bigness street population
<object> <object> <object> <object> <object> <object>
0 John engineer London captical street1 high
1 John engineer London captical street2 low
2 John engineer London captical street3 average
3 John engineer Montresor villege street1 high
4 John engineer Montresor villege street2 low
5 John engineer Montresor villege street3 average
6 John engineer Esslingen town street1 high
7 John engineer Esslingen town street2 low
8 John engineer Esslingen town street3 average
9 Nick architect London captical street1 high
10 Nick architect London captical street2 low
11 Nick architect London captical street3 average
12 Nick architect Montresor villege street1 high
13 Nick architect Montresor villege street2 low
14 Nick architect Montresor villege street3 average
15 Nick architect Esslingen town street1 high
16 Nick architect Esslingen town street2 low
17 Nick architect Esslingen town street3 average
18 Eric deisgner London captical street1 high
19 Eric deisgner London captical street2 low
20 Eric deisgner London captical street3 average
21 Eric deisgner Montresor villege street1 high
22 Eric deisgner Montresor villege street2 low
23 Eric deisgner Montresor villege street3 average
24 Eric deisgner Esslingen town street1 high
25 Eric deisgner Esslingen town street2 low
26 Eric deisgner Esslingen town street3 average
我是 datar
的作者,这是一个在 python 中实现数据操作语法的包,以 pandas 和 modin 作为后端。