Anna
Anna

Reputation: 39

ValueError when appending dataframe row

When I try to interrogate some column values and append that row to another dataframe, I get a ValueError.

Let's say we have two dataframes:

df_a = pd.DataFrame([['a','Y','name1','5Y'],['b','N','name2','10Y'],['c','N','name3','3Y'],['d','Y', 'name4','10Y']], columns=['c1','c2','c3','c4'])

df_b = pd.DataFrame(columns=['c1','c2','c3','c4'])

Which are: df_a:

c1 c2 c3 c4
a Y name1 5Y
b N name2 10Y
c N name3 3Y
d Y name4 10Y

df_b:

c1 c2 c3 c4

I want to interrogate columns c2 and c4 of df_a, so that when c2=Y and c4=10Y to copy that row to df_b.

This is done by using:

df_b.append(df_a.loc[(df_a['c2'] == 'Y') and (df_a['c4'] == '10Y')])

This returns: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Upvotes: 0

Views: 39

Answers (1)

zerOpRiME
zerOpRiME

Reputation: 380

Try df_b.append(df_a.loc[(df_a['c2'] == 'Y') & (df_a['c4'] == '10Y')])

Upvotes: 0

Related Questions