Home > OS >  Link Elements in Pandas DataFrame
Link Elements in Pandas DataFrame

Time:09-30

Good Day!

I have a subset of a pandas DataFrame "mapSubset" with non-continuous indices due to filtering. The DataFrame contains 'key' values.

mapSubset
   val  key
0   12    0
2   18    1
4   24    2
6   30    3
8   36    4

In the pandas DataFrame "link", which contains 'key' values corresponding to the 'key' values in the "mapSubset" DataFrame, I want to add the corresponding indices from the "mapSubset" (DataFrame).

link
   key
0    4
1    2
2    0

Expected Output:

link
   key  keyIndex
0    4         8
1    2         4
2    0         0

I Tried the following:

import pandas as pd
import numpy as np

# prepare dummy data
mapFull = pd.DataFrame()
mapFull['val'] = list(range(12,40,3))

mapSubset = mapFull[mapFull.val % 2 == 0]
mapSubset['key'] = list(range(len(mapSubset)))

link = pd.DataFrame()
link['key'] = [4, 2, 0]

# fill 'keyIndex' values into "link" DataFrame
# try No. 1:
# link['keyIndex'] = mapSubset.index[mapSubset.loc[:, 'key'] == link.loc[:, 'key']]
# --> ValueError: Can only compare identically-labeled Series objects

# try No. 2:
link['keyIndex'] = 9999
for pos in range(len(link)):
    ii = mapSubset.index[mapSubset.loc[:,'key'] == link.loc[pos,'key']][0]
    link.loc[pos,'keyIndex'] = ii

Try No. 1 resulted in

ValueError: Can only compare identically-labeled Series objects

I technically succeeded with 'try No. 2' although that's an ugly workaround.

Furthermore

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

is raised for mapSubset['key'] = list(range(len(mapSubset))).

How can I avoid the error messages? How can the expected result be achieved in a nicer way?

CodePudding user response:

You can always create the dict them map it back

link['Keyindex'] = link['key'].map(dict(zip(mapSubset.key,mapSubset.index)))
link
Out[12]: 
   key  Keyindex
0    4         8
1    2         4
2    0         0
  • Related