简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM