In Pandas, creating a new data frame using data filtered by a list of lists

In Pandas, creating a new data frame using data filtered by a list of lists

So, I've looked around quite a bit and I haven't been able to find an answer to this problem. I apologize if it is indeed out there.

So, I've looked around quite a bit and I haven't been able to find an answer to this problem. I apologize if it is indeed out there.

I have a DF that looks like this:

a = pd.DataFrame({'Name': ['apple', 'banana', 'orange', 'apple', 'banana','orange'], 
                  'Units': [2,4,6,5,4,3]})

I also have a list of lists like this:

b = [['apple', 'banana'],['orange']]

The goal is to group apple and banana in to 1 column and orange in to another with their respective units summed. The variable in the column will show up as the first item in the sublist. (no sublist will have duplicates).

Here's what I want the output df to look like:

output = pd.DataFrame({'Name': ['apple', 'orange'],
               'Units': [15, 9]})

Here's where I am right now:

for fruit in a['Name']:
for sublist in b:
    if fruit in sublist:
        pd.concat([XYZ,
                   pd.DataFrame({'Name': sublist[0], 'Units': a[a.Name == fruit]['Units'].sum(), index=[0})], 
                  axis=1)

XYZ is an empty data frame with columns= Name and Units that I am trying to populate with the results. I don't really understand how to create a data frame when the fruit is in sublist along with the sum of it's Units.

Any thoughts?

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Python Pandas Tutorial - Data Analysis with Python Pandas

Python Pandas Tutorial - Data Analysis with Python Pandas will help you get started with Python Pandas Library for various applications including Data analysis. You'll learn: Introduction to Pandas; DataFrames and Series; How To View Data? Selecting Data; Handling Missing Data; Pandas Operations; Merge, Group, Reshape Data; Time Series And Categoricals; Plotting Using Pandas

Python Pandas Tutorial - Data Analysis with Python Pandas

Python Pandas Tutorial will help you get started with Python Pandas Library for various applications including Data analysis. Introduction to Pandas. DataFrames and Series. How To View Data? Selecting Data. Handling Missing Data. Pandas Operations. Merge, Group, Reshape Data. Time Series And Categoricals. Plotting Using Pandas

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...