I now have my function working as follows: Even though it executes the proper solution I end up with a huge error in my CMD line https://i.stack.imgur.com/PBTwz.png (ValueError: Invalid file path or buffer object type: < class 'pandas.core.frame.DataFrame' >). Does anyone know why I am getting this error even though the output is executing correctly?
import pandas as pd
import numpy as np
import argparse
target_col = "CountryRefs"
sep = ","
input_file = 'Test_set'
def arg_parse():
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--input_file", required = True)
parser.add_argument("-s", "--sep", required=True,)
parser.add_argument("-t", "--target_col", required=True)
args=parser.parse_args()
return vars(args)
def splitter(input_file, target_col, sep, new_col = None, *argv):
df = pd.read_csv(input_file)
df[target_col] = df[target_col].str.split(sep)
exploded = df.explode(target_col)
exploded[target_col].replace(r'^\s*$', np.nan, regex=True, inplace = True)
exploded.dropna(subset=[target_col], inplace=True)
if new_col == None:
return(pd.DataFrame(exploded[[target_col,*argv]]))
else:
exploded[new_col] = exploded[target_col]
return(pd.DataFrame(exploded[[new_col,*argv]]))
if __name__ == '__main__':
args = arg_parse()
print(splitter(**args))