stacked
stacked

Reputation: 1

excluding specific rows in pandas dataframe

I am trying to create a new dataframe selecting only those rows which a specific column value does not start with a capital S. I have tried the following options:

New_dataframe = dataframe.loc[~dataframe.column.str.startswith(('S'))]

filter = dataframe['column'].astype(str).str.contains(r'^\S')
New_dataframe  = dataframe[~filter]

However both options return an empty dataframe. Does anybody have a better solution?

Upvotes: 0

Views: 1949

Answers (1)

Corralien
Corralien

Reputation: 120429

Your code works well:

dataframe = pd.DataFrame({'ColA': ['Start', 'Hello', 'World', 'Stop'],
                          'ColB': [3, 4, 5, 6]})
New_dataframe = dataframe.loc[~df['ColA'].str.startswith('S')]
print(New_dataframe)

Output:

>>> New_dataframe
    ColA  ColB
1  Hello     4
2  World     5

>>> dataframe
    ColA  ColB
0  Start     3
1  Hello     4
2  World     5
3   Stop     6

Upvotes: 1

Related Questions