在函数内部过滤 pandas 数据框和过滤后的数据框 return
Filter pandas dataframe inside of function and return filtered dataframe
我有一个包含在线和离线数据的 class,我想使用一个可以根据现有列过滤数据的功能。
以下代码有效:
class Exp:
def __init__(self):
d = {'col1': [1, 2], 'col2': [3, 4], 'col3' : [5,6]}
d2 = {'col1': [7, 8], 'col2': [9, 10], 'col3' : [11,12]}
self.online = pd.DataFrame(d)
self.offline = pd.DataFrame(d2)
def filter_cols(self, typ, cols):
if typ == "on":
self.online = self.online.filter(items= cols)
if typ == "off":
self.offline = self.offline.filter(items = cols)
print("before")
a = Exp()
print(a.online)
a.filter_cols("on", cols = ['col1', 'col2'])
print("after")
print(a.online)
结果:
before
col1 col2 col3
0 1 3 5
1 2 4 6
after
col1 col2
0 1 3
1 2 4
但我想让我的函数更通用,这样我就不必在函数中指定 if 和 else 语句。所以基本上我想要像下面这样的东西工作
class Exp:
.
.
.
def filter_cols(self, data, cols):
data.filter(items= cols)
print("before")
a = Exp()
print(a.online)
a.filter_cols(a.online, cols = ['col1', 'col2'])
print("after")
print(a.online)
但结果还是一样:
before
col1 col2 col3
0 1 3 5
1 2 4 6
after
col1 col2 col3
0 1 3 5
1 2 4 6
像这样使用 setattr
和 getattr
:
def filter_cols(self, typ, cols):
setattr(self, typ + 'line', getattr(self, typ + 'line').filter(items= cols))
完整代码:
class Exp:
def __init__(self):
d = {'col1': [1, 2], 'col2': [3, 4], 'col3' : [5,6]}
d2 = {'col1': [7, 8], 'col2': [9, 10], 'col3' : [11,12]}
self.online = pd.DataFrame(d)
self.offline = pd.DataFrame(d2)
def filter_cols(self, typ, cols):
setattr(self, typ + 'line', getattr(self, typ + 'line').filter(items= cols))
print("before")
a = Exp()
print(a.online)
a.filter_cols("on", cols = ['col1', 'col2'])
print("after")
输出:
before
col1 col2 col3
0 1 3 5
1 2 4 6
after
col1 col2
0 1 3
1 2 4
我有一个包含在线和离线数据的 class,我想使用一个可以根据现有列过滤数据的功能。 以下代码有效:
class Exp:
def __init__(self):
d = {'col1': [1, 2], 'col2': [3, 4], 'col3' : [5,6]}
d2 = {'col1': [7, 8], 'col2': [9, 10], 'col3' : [11,12]}
self.online = pd.DataFrame(d)
self.offline = pd.DataFrame(d2)
def filter_cols(self, typ, cols):
if typ == "on":
self.online = self.online.filter(items= cols)
if typ == "off":
self.offline = self.offline.filter(items = cols)
print("before")
a = Exp()
print(a.online)
a.filter_cols("on", cols = ['col1', 'col2'])
print("after")
print(a.online)
结果:
before
col1 col2 col3
0 1 3 5
1 2 4 6
after
col1 col2
0 1 3
1 2 4
但我想让我的函数更通用,这样我就不必在函数中指定 if 和 else 语句。所以基本上我想要像下面这样的东西工作
class Exp:
.
.
.
def filter_cols(self, data, cols):
data.filter(items= cols)
print("before")
a = Exp()
print(a.online)
a.filter_cols(a.online, cols = ['col1', 'col2'])
print("after")
print(a.online)
但结果还是一样:
before
col1 col2 col3
0 1 3 5
1 2 4 6
after
col1 col2 col3
0 1 3 5
1 2 4 6
像这样使用 setattr
和 getattr
:
def filter_cols(self, typ, cols):
setattr(self, typ + 'line', getattr(self, typ + 'line').filter(items= cols))
完整代码:
class Exp:
def __init__(self):
d = {'col1': [1, 2], 'col2': [3, 4], 'col3' : [5,6]}
d2 = {'col1': [7, 8], 'col2': [9, 10], 'col3' : [11,12]}
self.online = pd.DataFrame(d)
self.offline = pd.DataFrame(d2)
def filter_cols(self, typ, cols):
setattr(self, typ + 'line', getattr(self, typ + 'line').filter(items= cols))
print("before")
a = Exp()
print(a.online)
a.filter_cols("on", cols = ['col1', 'col2'])
print("after")
输出:
before
col1 col2 col3
0 1 3 5
1 2 4 6
after
col1 col2
0 1 3
1 2 4