Python Forum
Use pandas to obtain cartesian product between a dataframe of int and equations?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use pandas to obtain cartesian product between a dataframe of int and equations?
#1
I've attached an image further detailing what I'm trying to achieve.

The following is what I came up with; but I'm confident it can be done much simpler with built pandas modules. I'm hoping I won't have to resort to nested 4 loops.

The following is the code I used in the python interpreter:

nestDict =  {   'A': {'Entity 1': 2, 'Entity 2': 1, 'Entity 3': 2},
                'B': {'Entity 1': 4, 'Entity 2': 6, 'Entity 3': 3},
                'C': {'Entity 1': 5, 'Entity 2': 8, 'Entity 3': 4}
            }
frame1 = pd.DataFrame(nestDict)

dict1 = {   'Squared' : 'x**2', 
            'Sqrt' : 'round(math.sqrt(x), 2)', 
            'isPrime' : 'is_Prime_Function(x)'
        }
series1 = pd.Series(dict1)

def is_Prime_Function(n):
    if n == 1:
        return 0        
    for i in range(2,n):
        if (n%i) == 0:
            return 0
    return 1

frame1_sqrd = frame1.applymap(lambda x:eval(series1['Squared']))    
frame1_sqrt = frame1.applymap(lambda x:eval(series1['Sqrt']))
frame1_isPrime = frame1.applymap(lambda x:eval(series1['isPrime']))

# Use nested for loop to retrieve each row from the evaluated frames make results 'readable' from 1 single 'cartesian-like' dataframe.
This is the best or closes I've gotten to getting what I'm looking for.

def triple_Evaluation(x):
    return pd.Series([x**2, round(math.sqrt(x), 2), is_Prime_Function(x)], index=['Squared','Sqrt','Is Prime'])
    
frame1.iloc[[0]].append(frame1.iloc[0].apply(triple_Evaluation).T)

Attached Files

Thumbnail(s)
   
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Add NER output to pandas dataframe dg3000 0 140 Apr-22-2024, 08:14 PM
Last Post: dg3000
  HTML Decoder pandas dataframe column mbrown009 3 1,062 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Pandas Dataframe Filtering based on rows mvdlm 0 1,451 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 2,332 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,274 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 1,814 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,561 Jan-10-2022, 04:42 PM
Last Post: moduki1
Question How can I save cartesian products to a dataframe? noahverner1995 1 1,649 Dec-27-2021, 09:15 AM
Last Post: noahverner1995
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,327 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  empty row in pandas dataframe rwahdan 3 2,463 Jun-22-2021, 07:57 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020