Issue
My inputs are a dataframe and a list of coodinates :
df = pd.DataFrame({
'col1': ['A', 'B', 'C', 'A', 'G'],
'col2': ['B', 'E', 'F', 'F', 'H'],
'col3': ['C', 'D', 'E', 'A', 'I']})
coords = [(2, 0), (3, 2)]
print(df)
col1 col2 col3
0 A B C
1 B E D
2 C F E
3 A F A
4 G H I
Basically I'm trying to select some cells in df
based on the coordinates X
and Y
that we take from the list coords
. And since I may need to invert the selection, I created a function with a boolean so it has two workflows. Until now I was able to produce one of the two selections (the easy one):
def select(df, coords, inverted=False):
if inverted is True:
for c in coords:
df.iat[c[0], c[1]] = ''
return df
Can you guys show me how make the other one ?
My expected output is one of these two :
# inverted = False
col1 col2 col3
0
1
2 C
3 A
4
# inverted = True
col1 col2 col3
0 A B C
1 B E D
2 F E
3 A F
4 G H I
Solution
If you convert your coords to a tuple of transposed coordinates, you can use that directly as an index into an ndarray
. You can use that to either set values in a new array to ''
or the corresponding value from the input dataframe, dependent on the value of inverted
:
def select(df, coords, inverted=False):
coords = tuple(np.array(coords).T)
out = data = np.array(df)
if inverted:
out[coords] = ''
else:
out = np.full(data.shape, '')
out[coords] = data[coords]
return pd.DataFrame(out, columns=df.columns)
select(df, coords, True)
select(df, coords, False)
Output:
col1 col2 col3
0 A B C
1 B E D
2 F E
3 A F
4 G H I
col1 col2 col3
0
1
2 C
3 A
4
Answered By - Nick
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.