Reputation: 223
I'm trying to find matching values in a pandas dataframe. Once a match is found I want to perform some operations on the row of the dataframe.
Currently I'm using this Code:
import pandas as pd
d = {'child_id': [1,2,5,4,7,8,9,10],
'parent_id': [3,4,1,3,11,6,12,13],
'content': ["thon","pan","py","das","ten","sor","js","on"]}
df = pd.DataFrame(data=d)
df2 = pd.DataFrame(columns = ("content_child", "content_parent"))
for i in range(len(df)):
for j in range(len(df)):
if str(df['child_id'][j]) == str(df['parent_id'][i]):
content_child = str(df["content"][i])
content_parent = str(df["content"][j])
s = pd.Series([content_child, content_parent], index=['content_child', 'content_parent'])
df2 = df2.append(s, ignore_index=True)
else:
pass
print(df2)
This Returns:
content_child content_parent
0 pan das
1 py thon
I tried using df.loc functions, but I only succeed in getting either Content from child or Content from parent:
df.loc[df.parent_id.isin(df.child_id),['child_id','content']]
Returns:
child_id content
1 2 pan
2 5 py
Is there an fast alternative to the loop I have written?
Upvotes: 1
Views: 2126
Reputation: 862511
For improve performance use map
:
df['content_parent'] = df['parent_id'].map(df.set_index('child_id')['content'])
df = (df.rename(columns={'content':'content_child'})
.dropna(subset=['content_parent'])[['content_child','content_parent']])
print (df)
content_child content_parent
1 pan das
2 py thon
Or merge
with default inner join:
df = (df.rename(columns={'child_id':'id'})
.merge(df.rename(columns={'parent_id':'id'}),
on='id',
suffixes=('_parent','_child')))[['content_child','content_parent']]
print (df)
content_child content_parent
0 py thon
1 pan das
Upvotes: 1
Reputation: 2908
You can use just join
data frames with condition if left part child_id
is equal to right part parent_id
.
df.set_index('parent_id').join(df.set_index('child_id'), rsuffix='_').dropna()
this code will create two data tables with ids parent_id
and child_id
. Then join them as usual SQL join. After all drop NaN values and get content
column. Which is what you want. There are 2 content columns. one of them is parent content and second is child content.
Upvotes: 1