I'm trying to trim a DataFrame based on an input list, but I need to check if the items in the list are in some of the frame's columns.
(data below is random)
The frame I'd like to trim looks like this:
UID S1 S2 ElementHID n1 n2 n3 n4
0 88.340153 -88.340153 144 1 4 5 5
1 66.370153 -66.370153 144 4 1 5 4
2 74.422513 -74.422513 144 2 7 3 6
3 22.324573 -22.324573 144 1 9 8 1
4 14.322413 -14.322413 144 3 4 6 3
Each row represents an element that can have up to 4 nodes.
I have a list of nodes (node_list) that I've filtered from another frame and I'd like to come in here and get which elements touch those nodes (nodes could be in n1, n2, n3, n4). There can be many elements joining on one node, so the 'n' columns have repeating data.
I'd like to avoid looping through the whole frame. I tried that but it took way too long. I'm hoping this can be done through Pandas.
The basic logic per row would be like [row if node in n1 or n2 or n3 or n4], where node is looping through the node_list
So far I've tried going through the columns individually, but I can't keep the columns straight after merging everything in frame_list.
frame_list = []
for n in range(1, 4):
node = 'n' + str(n)
if node in element_frame.columns:
temp_frame = element_frame.merge(pd.DataFrame(node_list), left_on = node, right_on='uid', how = 'inner')
frame_list.append(temp_frame)
else:
continue
I'm hoping there's an easier way!
Thanks,
khu