extracting rows from CSV file based on specific keywords

后端 未结 2 435

I have created a code to help me retrieving the data from csv file

  import re
keywords = {\"metal\", \"energy\", \"team\", \"sheet\", \"solar\" \"finan         


        
2条回答
  •  后悔当初
    2021-01-29 05:51

    You can do this using pandas as follows, if you are looking for rows that contain exactly one word from the list of keywords:

    keywords = ["metal", "energy", "team", "sheet", "solar" "financial", "transportation", "electrical", "scientists",
                "electronic", "workers"]
    
    # read the csv data into a dataframe 
    # change "," to the data separator in your csv file 
    df = pd.read_csv("2006-data-8-8-2016.csv", sep=",")
    # filter the data: keep only the rows that contain one of the keywords 
    # in the position or the Job description columns
    df = df[df["position"].isin(keywords) | df["Job description"].isin(keywords)] 
    # write the data back to a csv file 
    df.to_csv("new_data.csv",sep=",", index=False) 
    

    If you are looking for substrings in the rows (e.g looking financial in financial engineering) then you can do the following:

    keywords = ["metal", "energy", "team", "sheet", "solar" "financial", "transportation", "electrical", "scientists",
                "electronic", "workers"]
    searched_keywords = '|'.join(keywords)
    
    # read the csv data into a dataframe 
    # change "," to the data separator in your csv file 
    df = pd.read_csv("2006-data-8-8-2016.csv", sep=",")
    # filter the data: keep only the rows that contain one of the keywords 
    # in the position or the Job description columns
    df = df[df["position"].str.contains(searched_keywords) | df["Job description"].str.contains(searched_keywords)] 
    # write the data back to a csv file 
    df.to_csv("new_data.csv",sep=",", index=False) 
    

提交回复
热议问题