7

I have a csv file as the given picture bellow

enter image description here

I'm trying to find any word that will start with letter A and G or any list that I want

but my code returns an error any Ideas what I'm doing wrong ? this is my code

if len(sys.argv) == 1:
    print("please provide a CSV file to analys")
else:
    fileinput = sys.argv[1]

wdata = pd.read_csv(fileinput)


print( list(filter(startswith("a","g"), wdata)) )

1
  • What error you received ? Commented Jan 27, 2020 at 7:29

3 Answers 3

11

To get relevant rows, extract the first letter, then use isin:

df
  words  frequency
0  what         10
1   and          8
2   how          8
3  good          5
4   yes          7

df[df['words'].str[0].isin(['a', 'g'])]
  words  frequency
1   and          8
3  good          5

If you want a specific column, use loc:

df.loc[df['words'].str[0].isin(['a', 'g']), 'words']
1     and
3    good
Name: words, dtype: object

df.loc[df['words'].str[0].isin(['a', 'g']), 'words'].tolist()
# ['and', 'good']
Sign up to request clarification or add additional context in comments.

3 Comments

first method just returns those which start with a not for some reasons
@programmingfreak I don't understand what you mean, this gives the exact same result as the accepted answer
my sample set is much bigger and for some reason it was just returning the first condition but the second method is interested and works fine for me thanks man
5

Use Series.str.startswith with convert list to tuple and filtering by DataFrame.loc with boolean indexing:

wdata = pd.DataFrame({'words':['what','and','how','good','yes']})

L = ['a','g']
s = wdata.loc[wdata['words'].str.startswith(tuple(L)), 'words']
print (s)
1     and
3    good
Name: words, dtype: object

2 Comments

but this returns anything with a and g not just the start my condition is if it starts with this letter then return
@programmingfreak - Added sample data, for me working nice.
1

it is very easy and handy. you can just use str.startwith in this way:

df[df.Words.str.startswith('G')]



df[df.Words.str.startswith('A')]

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.