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
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)
numpy.where
, которую можно вкладывать в себя, как обычные условные операторы:import numpy as np
import pandas as pd
if __name__ == '__main__':
df = pd.DataFrame({
'movie': [9999999, 2, 3, 1, 9999999],
'rating': [9999999, 2, 9999999, 9999999, 3],
'name': [1, 2, 4, 5, 10]})
df['result'] = np.where(
df['movie'] != 9999999,
df['movie'],
np.where(
df['rating'] != 9999999,
df['rating'],
df['name']))
print(df)
movie rating name result
0 9999999 9999999 1 1
1 2 2 2 2
2 3 9999999 4 3
3 1 9999999 5 1
4 9999999 3 10 3