1
L1 = ['a','b','c','a','b','c']
L2 = ['Cat','Fish','Crow','Dog','Frog','Eagle']

Desired Output 1: D1 = {'a':['Cat','Dog'],
                    'b':['Fish','Frog'],
                    'c':['Crow','Eagle']}

Desired Output 2: DF1 =   A        B        C   
                     Cat      Fish      Crow
                     Dog      Frog      Eagle

I only used from a to c for reference, I've more than 100 columns in DataFrame.

Could someone please help me with this?

2 Answers 2

2

Try:

L1 = ["a", "b", "c", "a", "b", "c"]
L2 = ["Cat", "Fish", "Crow", "Dog", "Frog", "Eagle"]

out = {}
for a, b in zip(L1, L2):
    out.setdefault(a, []).append(b)

print(out)

Prints:

{"a": ["Cat", "Dog"], "b": ["Fish", "Frog"], "c": ["Crow", "Eagle"]}

Then you can do:

out_df = pd.DataFrame(out)
out_df.columns = out_df.columns.str.upper()

print(out_df)

Prints:

     A     B      C
0  Cat  Fish   Crow
1  Dog  Frog  Eagle
Sign up to request clarification or add additional context in comments.

Comments

0

Here is a way by creating a blank dictionary and then appending each item to it.

l = list(set(l1))
d = {key:[] for key in l}
for i,j in zip(l1,l2):
    d.get(i).append(j)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.