Let´s say I have 3 dataframes. Each dataframe has strings in it and I want to find all possible combinations of those 3 dataframes and output them as list. I searched stackoverflow for a while, tried lot of codes, but non of them worked. I tried importing intertools, combinations etc, but still got wrong output or non at all.
def combinator(names,cities,streets) #for example
In my last attemp I tried to combine only names with cities with this code :
names.merge(cities, how = 'cross')
names['key'] = 1
cities['key'] = 1
combined = pd.merge(names,cities,on='key').drop('key', axis = 1)
But only got all names copied. The goal is to find all possible combinations of names, cities and streets and return them in one list. Thanks for all suggestions.
Input dataframes
CodePudding user response:
Is this what you wanted?
>>> 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
I am the author of datar
, a package that implements the Grammar of Data Manipulation in python, with pandas and modin as backend.