我想编写一个使用CSV文件名的函数,然后创建打印的熊猫数据框?我写了这个太简单的代码,但是不起作用:
def ols_reg(filename):
''' '''
df = pd.read_csv("filename.csv") #assumption: the directory of both files is same
print(df)
如果仅将“文件名”放入字符串中,则不会将其作为变量读取。通过在字符串前面扔'f'并将变量放在方括号中,将其作为f字符串引入。
def ols_reg(filename):
df = pd.read_csv(f"{filename}.csv") #assumption: the directory of both files is same
print(df)