I am trying to learn Pandas for DA and I have managed to figure out merge and merge 2 DF together, but I'm trying to sum a new column whilst column x != column y
merged_by_country["total_shows"] = merged_by_country["show_id"] merged_by_country["count"] #(lambda x, y: x != y)
test = merged_by_country.copy()
def set_shows(df):
for r in df.iloc:
if r[1] == r[3]:
r[-1] = r[1]
else:
r[-1] = r[1] r[3]
return df
set_shows(test)
But I'm getting 'SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame' which I want it to do, but whenever I call the DF again, nothing changes... even tried copying the df in the func then performing the loop and passing back the new copied df but nothing happens.
country show_id first_country count total_shows
Jordan 1 Jordan 1 2
Philippines 1 Philippines 1 2
United Arab Emirates 1 United Arab Emirates 1 2
Switzerland 1 Switzerland 1 2
Senegal 1 Senegal 1 2
edited with the sample of merged_by_country dataframe.
CodePudding user response:
IIUC, you can use np.where
:
# Renamed 'merged_by_country' to 'df' for readability
df['total_shows'] = np.where(df['show_id'] == df['count'],
df['show_id'],
df['show_id'] df['count'])
CodePudding user response:
df['total_shows'] = df.show_id
idx = df[df.country!=df.first_country].index
df.loc[idx, 'total_shows'] = df.loc[idx, 'show_id'] df.loc[idx, 'count']