I have a function that takes two inputs, the year and an identifier associated with a specific CSV file. It then (using a try and except loop), does the following:
- Reads in the csv.
- Creates a new column based on the input of the function.
- Uses several list comprehensions to check if certain variables appear in column names.
- Assigns new columns based on the output of these 3 list comprehensions.
For some reason, everything is fine until I assign the new columns. Once I add that step in, I return a Nonetype Dataframe. I am at a loss as to why this is happening and also how to fix it?
def read_csv_create_cols(id, year=2020):
subs_2 = ["_rate"]
path = os.path.join(path1, path2/{id}/{year})
try:
new_df = pd.read(csv(path, low_memory=False)
new_df["id"] = id
col_name_1 = [col for col in new_df.columns if "product" in col]
col_name_2 = [col for col in new_df.columns if "date" in col]
col_name_3 = [col for col in new_df.columns if all (sub in column for sub in subs_2)]
new_df["col_name_1"] = new_df[col_name_1]
new_df["col_name_2"] = new_df[col_name_2]
new_df["col_name_3"] = new_df[col_name_3]
except:
return None
question from:
https://stackoverflow.com/questions/65837791/how-to-read-in-a-csv-and-create-new-columns-in-the-resulting-dataframe-based-on 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…