With the following csv file
id,name,cy,in
0,MD,4,16
2,MD,10,20
3,YD,5,14
4,ZD,10,14
I have written the following code to create a new dataframe.
df = pd.read_csv('test.csv', usecols=['id', 'name', 'cy', 'in'])
df2 = pd.DataFrame(columns=['N', 'I', 'C'])
ids=[0,2,4]
for i in ids:
row = df.loc[df['id'] == i]
cyc = row.at[row.index[0],'cy']
ins = row.at[row.index[0],'in']
name = row.at[row.index[0],'name']
if df2['N'].str.contains(name):
print("Matched")
else:
new_row = {'N':name, 'I':ins, 'C':cyc}
df_temp = pd.DataFrame([new_row])
df2 = pd.concat([df2, df_temp], axis=0, ignore_index=True)
print(df2)
As you can see, for specified id
, I first get the row from the original dataframe, df
. If I couldn't find the name
in the second dataframe, df2
, then create a new row and add that to the second dataframe. However, on a match, I would like to add the values to the existing row. So, in the end, I expect to see:
N I C
0 MD 36 14
2 ZD 14 10
That if
statement has the following error though:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Howe can I fix that?
CodePudding user response:
Use Series.isin
for get all id
in boolean indexing
and then aggregate sum
in names aggregation:
ids=[0,2,4]
df = (df[df['id'].isin(ids)].groupby('name', as_index=False)
.agg(I=('in','sum'), C=('cy','sum'))
.rename(columns={'name':'N'}))
print (df)
N I C
0 MD 36 14
1 ZD 14 10
CodePudding user response:
You can use isin
then groupby and named aggregation
out = df[df['id'].isin(ids)].groupby('name').agg(I=('in', sum),
C=('cy', sum)).reset_index().rename(columns={'name': 'N'})
print(out)
N I C
0 MD 36 14
1 ZD 14 10