S
S
san_m_m2021-07-29 11:47:03
Python
san_m_m, 2021-07-29 11:47:03

How to make a list in pandas based on multiple columns?

Good afternoon!

There is a DataFrame

import pandas as pd
df = pd.DataFrame({'movie': [9999999,  2,  3, 1, 9999999],
                  'rating': [3,  2,  9999999, 9999999, 3],
                  'name': [1,  2,  4, 5, 10]})


You need to add one more column, which will contain data according to the following logic...
If the movie column is 9999999, then the value from the rating column is taken, if the rating is 9999999, then name
tried to write the following code, but it seems to me that the idea is fundamentally wrong so I'm here...
kol = []
for l in df:
    for i in l:
        if i['movie'] != 9999999:
            k = i['movie']
        elif i['rating'] != 9999999:
            k = i['rating']
        else:
            k = i['name']
        kol.append(k)
df['sum'] = kol

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2021-07-29
@san_m_m

You can move the calculations into a function and apply apply.

import pandas as pd
df = pd.DataFrame({'movie': [9999999,  2,  3, 1, 9999999],
                  'rating': [3,  2,  9999999, 9999999, 3],
                  'name': [1,  2,  4, 5, 10]})

def process(row):
    result = row['movie']
    if row['movie'] == 9999999:
        if row['rating'] == 9999999:
            result = row['name']
        else:
            result = row['rating']
    return result

df['sum'] = df.apply(process, axis=1)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question