I have a dataframe:
df = pd.DataFrame({'A':np.random.randint(1,10, 10), 'B':np.random.randint(1,10, 10)})
def sumf(row):
result = None
if row['A']>= row['B']:
result = row['A'] - row['B']
else:
result = row['B'] - row['A']
return result
df.loc[:,'C'] = df.apply(sumf, axis = 1)
df['D'] = df.apply(sumf, axis = 1)
my_var = 'zero'
df['E'] = my_var
What would be the difference in terms of view/copy for column C
and D
? And is it the right way to fill column E
with zero
?
I have a similar data frame with the same data and logic (just in another jupyter notebook), but there I am getting a warning:
/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:19: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead
When I try these lines:
df['D'] = df.apply(sumf, axis = 1)
my_var = 'zero'
df['E'] = my_var