我有一个pandas.Dataframe
,处理后看起来像这样:
Type|Fiscal quarter
A|FY18 Q3
A|FY18 Q4
A|FYn Q3
我想清除date = FYn type
的数据所以它看起来像这样:
Type|Fiscal quarter
A|FY18 Q3
A|FY18 Q4
A|
我该怎么做?
答案 0 :(得分:3)
Pandas提供pd.replace
,因此您可以使用:
df['Fiscal quarter']=df['Fiscal quarter'].replace('FYn.*','',regex=True)
这将使用正则表达式FYn.*
答案 1 :(得分:1)
您可以测试Fiscal quarter
列的值,并使用它来选择要替换的值,如:
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
答案 2 :(得分:0)
可以使用易于理解的for
循环来检查和更改每一行:
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)
输出:
Type Fiscal quarter
0 A FY18 Q3
1 A FY18 Q4
2 A