我正在尝试删除包含某些字符串的行。但是,我得到了错误:
pandas-'dataframe'对象没有属性'str'错误。
这是我的代码:
df = df[~df['colB'].str.contains('Example:')]
我该如何解决?
答案 0 :(得分:1)
第一个问题应该是重复的列名,因此选择colB
后得到的不是Series
,而是DataFrame
:
df = pd.DataFrame([['Example: s', 'as', 2], ['dd', 'aaa', 3]], columns=['colB','colB','colC'])
print (df)
colB colB colC
0 Example: s as 2
1 dd aaa 3
print (df['colB'])
colB colB
0 Example: s as
1 dd aaa
#print (df['colB'].str.contains('Example:'))
#>AttributeError: 'DataFrame' object has no attribute 'str'
解决方案应将列连接在一起:
print (df['colB'].apply(' '.join, axis=1))
0 Example: s as
1 dd aaa
df['colB'] = df.pop('colB').apply(' '.join, axis=1)
df = df[~df['colB'].str.contains('Example:')]
print (df)
colC colB
1 3 dd aaa
第二个问题应该是hidden
MultiIndex:
df = pd.DataFrame([['Example: s', 'as', 2], ['dd', 'aaa', 3]], columns=['colA','colB','colC'])
df.columns = pd.MultiIndex.from_arrays([df.columns])
print (df)
colA colB colC
0 Example: s as 2
1 dd aaa 3
print (df['colB'])
colB
0 as
1 aaa
#print (df['colB'].str.contains('Example:'))
#>AttributeError: 'DataFrame' object has no attribute 'str'
解决方案已重新分配至第一级:
df.columns = df.columns.get_level_values(0)
df = df[~df['colB'].str.contains('Example:')]
print (df)
colA colB colC
0 Example: s as 2
1 dd aaa 3
第三个应该是MultiIndex
:
df = pd.DataFrame([['Example: s', 'as', 2], ['dd', 'aaa', 3]], columns=['colA','colB','colC'])
df.columns = pd.MultiIndex.from_product([df.columns, ['a']])
print (df)
colA colB colC
a a a
0 Example: s as 2
1 dd aaa 3
print (df['colB'])
a
0 as
1 aaa
print (df.columns)
MultiIndex(levels=[['colA', 'colB', 'colC'], ['a']],
codes=[[0, 1, 2], [0, 0, 0]])
#print (df['colB'].str.contains('Example:'))
#>AttributeError: 'DataFrame' object has no attribute 'str'
通过MultiIndex
选择tuple
的解决方案:
df1 = df[~df[('colB', 'a')].str.contains('Example:')]
print (df1)
colA colB colC
a a a
0 Example: s as 2
1 dd aaa 3
或重新分配:
df.columns = df.columns.get_level_values(0)
df2 = df[~df['colB'].str.contains('Example:')]
print (df2)
colA colB colC
0 Example: s as 2
1 dd aaa 3
或删除第二级:
df.columns = df.columns.droplevel(1)
df2 = df[~df['colB'].str.contains('Example:')]
print (df2)
colA colB colC
0 Example: s as 2
1 dd aaa 3
答案 1 :(得分:0)
尝试一下:
df[[~df.iloc[i,:].str.contains('String_to_match').any() for i in range(0,len(df))]]