Prasad
Prasad

Reputation: 175

write csv in python based on sub-string condition

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)

Upvotes: 0

Views: 76

Answers (3)

MMF
MMF

Reputation: 5921

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)

Upvotes: 0

scomes
scomes

Reputation: 1846

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)

Upvotes: 1

Ben Quigley
Ben Quigley

Reputation: 727

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

Upvotes: 3

Related Questions