简体   繁体   中英

Creating new dataframe by search result in df

I am reading a txt file for search variable.

I am using this variable to find it in a dataframe.

for lines in lines_list:
    sn = lines
    if sn in df[df['SERIAL'].str.contains(sn)]:
        
        condition = df[df['SERIAL'].str.contains(sn)]
        df_new = pd.DataFrame(condition)
        df_new.to_csv('try.csv',mode='a', sep=',', index=False)  

When I check the try.csv file, it has much more lines the txt file has. The df has a lots of lines, more than the txt file. I want save the whole line from search result into a dataframe or file

I tried to append the search result to a new dataframe or csv.

first create line list

f = open("text.txt", "r")
l = list(map(lambda x: x.strip(), f.readlines()))

write this apply func has comparing values and filtering

def apply_func(x):
    if str(x) in l:
        return x
    return np.nan

and get output

df["Serial"] = df["Serial"].apply(apply_func)
df.dropna(inplace=True)

df.to_csv("new_df.csv", mode="a", index=False)

or try filter method

f = open("text.txt", "r")
l = list(map(lambda x: x.strip(), f.readlines()))

df = df.set_index("Serial").filter(items=l, axis=0).reset_index()
df.to_csv("new_df.csv", mode="a", index=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM