[英]How to iterate through pandas data frame that meet some condition and append those rows to new data frame?
I'm trying to create a function (get_filtered_df) that given a filter and a dataset, iterates through a specific column and applies some function (get_filter) that looks for rows that meet those conditions and get added to some final data frame.我正在尝试创建一个函数 (get_filtered_df),它给定一个过滤器和一个数据集,遍历特定列并应用一些函数 (get_filter) 来查找满足这些条件的行并添加到某个最终数据框中。
This is what I've accomplished so far:这是我到目前为止所完成的:
def get_filter(filter):
string_container = []
string_container.append(filter.upper())
string_container.append(filter.lower())
# More conditions here
return (string_container)
def get_filtered_df(string_container, df):
filtered_df = pd.DataFrame(df[df['Titles'].str.contains(string_container[0])], columns=df.columns)
return(filtered_df)
The problem comes when instead of a fixed index (0) I try to iterate using a for or while loop as I never get the function to append the rows that are meeting the conditions.当我尝试使用 for 或 while 循环而不是固定索引 (0) 进行迭代时,问题就出现了,因为我从来没有得到函数来附加满足条件的行。
This is the specific instruction that I tried to use:这是我尝试使用的具体说明:
for i in range len(get_filter('star')):
filtered_df.append(df[df['Title'].str.contains(get_filter('star')[i])])
filtered_df returns always empty Filtered_df 返回始终为空
The pd.DataFrame.append method returns a new object. pd.DataFrame.append方法返回一个新对象。 In your code, you are appending a new Dataframe - which is lost at each iteration, hence returning an empty DataFrame.在您的代码中,您要附加一个新的 Dataframe - 它在每次迭代时都会丢失,因此返回一个空的 DataFrame。
You need to update filtered_df
in your loop in order to update filtered_df
:您需要更新filtered_df
在你的循环,以更新filtered_df
:
def get_filtered_df(string_container, df):
filtered_df = pd.DataFrame(data=None, columns=df.columns)
for i in range(len(string_container)):
filtered_df = filtered_df.append(df[df['Titles'].str.contains(string_container[i])], ignore_index=True)
return(filtered_df)
should work.应该管用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.