Answer the question
In order to leave comments, you need to log in
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]})
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
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 questionAsk a Question
731 491 924 answers to any question