Home > OS >  How can I delete a specific row and column pointing to a cell from a dataframe having a special char
How can I delete a specific row and column pointing to a cell from a dataframe having a special char

Time:09-30

For example, the dataframe is:

data = [['@Why', 1, 2], ['Stack', 1 ,'Example'], ['Overflow', 2, 'Anything'], ['row&', 3, 'Group' ]]
df = pd.DataFrame(data, columns=['A', 'B', 'C'])

From here I want to delete the @why and row& rows, but I want to keep the rows and columns data the same, like it should not delete the entire row or column. It should only delete the cell value.

CodePudding user response:

Code:

df['A'] = df['A'].apply(lambda x: '' if any(c in "!@#$%^&*()- ?_=,<>/" for c in x) else x)
df

Output:

        A       B   C
0               1   2
1   Stack       1   Example
2   Overflow    2   Anything
3               3   Group
  • Related