Home > database >  Creating new dataframe by search result in df
Creating new dataframe by search result in df

Time:01-28

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.

CodePudding user response:

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)
  • Related