1

I have a pandas.Dataframe where after processing it looks like this:

Type|Fiscal quarter
A|FY18 Q3
A|FY18 Q4
A|FYn Q3

I would like to clear data where date = FYn type

So it will look like this:

Type|Fiscal quarter
A|FY18 Q3
A|FY18 Q4
A|

How do I do this?

3 Answers 3

3

Pandas offers pd.replace, so you can use:

df['Fiscal quarter']=df['Fiscal quarter'].replace('FYn.*','',regex=True)

This will achieve the desired purpose using the regex FYn.*

Sign up to request clarification or add additional context in comments.

Comments

1

You can test the value of the Fiscal quarter column and use it to select the values to replace like:

Code:

df['Fiscal quarter'][df['Fiscal quarter'].str.startswith('FYn')] = ''

Test Code:

import pandas as pd

df = pd.DataFrame([
    ('A', 'FY18 Q3'),
    ('A', 'FY18 Q4'),
    ('A', 'FYn Q3'),
], columns=['Type', 'Fiscal quarter'])

print(df)

df['Fiscal quarter'][df['Fiscal quarter'].str.startswith('FYn')] = ''
print(df)

Results:

  Type Fiscal quarter
0    A        FY18 Q3
1    A        FY18 Q4
2    A         FYn Q3

  Type Fiscal quarter
0    A        FY18 Q3
1    A        FY18 Q4
2    A               

Comments

0

One can use easily understandable for loop to check and change each row:

import pandas as pd
newvals = []                          # create new list for new dataframe
for v in df.values:                   # v will be as: ['A' 'FY18 Q3']
    if v[1].startswith("FYn"):        # remove entry if criteria satisfied
        v[1] = ""
    newvals.append(v)
df = pd.DataFrame(data=newvals, columns=df.columns)
print(df)

Output:

  Type Fiscal quarter
0    A        FY18 Q3
1    A        FY18 Q4
2    A       

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.