Reputation: 4608
i have a dataframe like this:
id text
1 one two three
2 one two
3 four
4 six seven
5 84
6 4x2
I wonder if integers like 84 (fifth row) would be a problem ?
the output should be like this (single word rows only):
id text
3 four
5 84
6 4x2
Upvotes: 0
Views: 1020
Reputation: 24314
create a boolean mask via strip()
,split()
and eq()
method:
mask=df['text'].str.strip().str.split(' ').str.len().eq(1)
Finally pass that mask:
out=df[mask]
#OR
out=df.loc[mask]
now if you print out
you will get your desired output
For selecting 2,3 and4 words just make a slight change in mask:
mask=df['text'].str.strip().str.split(' ').str.len()
Finally:
df[(mask>1) & (mask<5)]
#OR
df.loc[(mask>1) & (mask<5)]
Upvotes: 3
Reputation: 781
You could try something like that:
column_ok = [] # Column you want to keep
for column in df:
if column.find(" ")<=0: # If there is not a space in the column name
column_ok.append(column)
df_new = df[column_ok].copy() # Will copy the data frame with the "good columns"
You have to find if there is a space in your column name. The integer will be kept with this method.
Upvotes: 0