0

I have dataframe with 3 columns (id, parentId, version) and I need to add new column parentVersion (version from parentid) :

id parentid version parentVersion
1 2 1.1 2.1
2 2 2.1 2.1
0

2 Answers 2

3

Let's try creating a mapper out of the id and version columns with set_index then calling Series.map on the parentid column:

df['parentVersion'] = df['parentid'].map(df.set_index('id')['version']) 

df:

 id parentid version parentVersion 0 1 2 1.1 2.1 1 2 2 2.1 2.1 

The mapper:

df.set_index('id')['version'] 

Is a Series of values which associates ids with version:

id 1 1.1 2 2.1 Name: version, dtype: float64 

Then map will match the value in the index of this series (from the parentid column) and replace it will the corresponding value.


Alternatively a self merge on id and parentid then rename and filter out the unneeded columns:

df = df.merge( df, how='left', left_on='parentid', right_on='id', suffixes=('', '_y') ).rename(columns={'version_y': 'parentVersion'}).filter(regex='.*(?<!_y)$') 

df:

 id parentid version parentVersion 0 1 2 1.1 2.1 1 2 2 2.1 2.1 
Sign up to request clarification or add additional context in comments.

Comments

0

Define a dictionary containing the data : data = {"id":[1,2], "parentid":[2,2], "version":[1.1,2.1], }

Convert the dictionary into DataFrame:df = pd.DataFrame(data)

Declare a list that is to be converted into a column: parentVersion= [2.1, 2.1]

Using 'parentVersion' as the column name: df['parentVersion'] = parentVersion

Observe the result: print(df)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.