Charles
Charles

Reputation: 201

How to add column values from one Data Frame to the other column in second Data Frame based on conditions in Python?


df1 = pd.DataFrame(zip(l1), columns =['l1'])
df1.l2.value_counts()

df2 = pd.DataFrame(zip(l2), columns =['l2'])
df2.l2.value_counts()

I want to add the column values from l2 to l1 depending on the value count in df1. For example

conditions should be checked in df1 and if a condition is met then values from l2 in df2 should be added to the l1 column of df1

output is given here as well

l1=['aa_#1', 'bb_#1', 'bb_#2', 'aa_#1', 'aa_#1', 'aa_#1', 'bb_#1','aa_#2','bb_#2','aa_#2','bb_#1','bb_#1','bb_#1','bb_#2','bb_#2','cc_#1','bb_#2','bb_#2', 'bb_#2','aa_#2','aa_#2','aa_#2', 'cc_#1','cc_#1','cc_#1','cc_#1']

Please let me know if there is a way to do this in Python. I have 10,000 rows like this to add from l2 to l1 and I don't know how to even begin with it. I am really new to Python.

Upvotes: 1

Views: 77

Answers (1)

chakuRak
chakuRak

Reputation: 650

This is a method that doesn't use pandas. The .count() method returns the value count of an item in a list. The .extend() method appends another to the end of an existing list. Lastly, multiplying a list by an integer duplicates and concats it that many times. ['a'] * 3 == ['a', 'a', 'a']

def extend_list(l1, l2, prefixes, final_prefixes, suffix_1, suffix_2, suffix_3):
    for prefix in prefixes:
        if l1.count(f'{prefix}_{suffix_2}') < l1.count(f'{prefix}_{suffix_1}'):
            l1.extend([f'{prefix}_{suffix_2}'] * l2.count(f'{prefix}_{suffix_3}'))
    for final_prefix in final_prefixes:
        l1.extend([f'{final_prefix}_{suffix_1}'] * 
        (l2.count(f'{final_prefix}_{suffix_2)') + l2.count(f'{final_prefix}_{suffix_3}')))

l1 = ['aa_#1','bb_#1','bb_#2','aa_#1','aa_#1','aa_#1','bb_#1','aa_#2','bb_#2','aa_#2','bb_#1','bb_#1','bb_#1','bb_#2','bb_#2','cc_#1']
l2 = ['aa_#3','aa_#3','aa_#3','bb_#3','bb_#3','bb_#3','cc_#2','cc_#2','cc_#3','cc_#3']

l1 = extend_list(l1, l2, ["aa", "bb"], ["cc", "dd"], "#1", "#2", "#3")

Upvotes: 1

Related Questions