我有以下功能:
def match_function(df):
columns = df.columns.tolist()
matches = {}
for column in columns:
df = df.astype(str)
df_new = df.dropna()
df_new = df[column].str.split(',', expand=True)
df_new = df_new.apply(lambda s: s.value_counts(), axis=1).fillna(0)
match = df_new.iloc[:, 0][0] / df_new.sum(axis=1) * 100
match = round(match, 2)
df[column] = match
matches[column] = match
return matches
我希望这个函数对数据帧的每一行完全单独运行。它将循环通过数据帧的第一行,然后停止并再次运行第二行,以此类推
因为它是以一种非常复杂和不专业的方式编写的(因为我是Python新手),当我传递一个数据帧并且它同时在整个数据帧中运行时,结果是错误的。 或者可能会以某种方式更改函数本身,使其只能一行一行地运行
考虑下面的DF:
以及下面的函数
"func"
您可以通过以下方式在行的基础上应用该函数:
比产量:
因此,基本上,对于每一行,我们都应用了命名函数
func()
,即x['a']
+x['b']
相关问题 更多 >
编程相关推荐