0

I have the following result variable in a data frame (df) and I am trying to output a csv file for those that starts with "test"

abandoned
static
test_a_1
test_b_2
abandoned
test_b_3

The following code is not working. Thanks in advance for your insights

substr="test"
if substr in df['result']:
    df.to_csv("C:/Projects/result.csv",sep=',',index=False)
2
  • when you say not working do you mean it's not writing at all or writing the wrong thing? Commented Sep 29, 2016 at 15:48
  • Its not writing. The result of substr in df['result'] is false Commented Sep 29, 2016 at 15:51

3 Answers 3

3

Just because "test_a_1" is in the list does not mean that "test" is, in Python logic.

Example of how Python evaluates "if [string] in [list]" statements:

>>> test = 'test1'
>>> testlist = ['test1', 'test2']
>>> if 'test' in test:
...     print('hi')
... 
hi
>>> if 'test' in testlist:
...     print('hi')
... 
>>>

This would work:

substr="test"
for val in df['result']:
    if substr in val:
        # Do stuff
        # And optionally (if you only need one CSV per dataframe rather than one CSV per result):
        break
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Iroh.. This gives the output for all the result values, but I am interested to see test_a_1,test_b_2,test_b_3 rows in csv.
Looks like scomes's solution works for that, plus it's a sexy one-liner. ;)
1

If what you mean is that you want to make a csv that only contains the rows for which result starta with 'test', use the following:

df[df.result.str.contains('^test.*')].to_csv("C:/Projects/result.csv",sep=',',index=False)

Comments

0

This would work :

df['temp'] = [1 if 'test' in df['result'][k] for k in df.index else 0]
df['result'][df['temp']==1].to_csv("/your/path", sep=',', index=False)

Comments

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.