Reputation: 1285
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?
Upvotes: 1
Views: 43
Reputation: 24535
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
Upvotes: 0
Reputation: 1549
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.*
Upvotes: 3
Reputation: 49774
You can test the value of the Fiscal quarter
column and use it to select the values to replace like:
df['Fiscal quarter'][df['Fiscal quarter'].str.startswith('FYn')] = ''
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)
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
Upvotes: 1