我们如何编写函数以获取文件名并在pandas中创建数据帧?(没有pandas内置函数)

2024-05-10 01:12:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我想写一个函数,以CSV文件的名称,然后创建它的熊猫数据帧打印? 我编写的代码太简单,但不起作用:

def ols_reg(filename):
    '''      '''
    df = pd.read_csv("filename.csv")     #assumption: the directory of both files is same  
    print(df) 

1条回答
网友
1楼 · 发布于 2024-05-10 01:12:30

如果只是在字符串中插入“filename”,它不会将其作为变量读取。将其作为f字符串引入,方法是在字符串前面抛出“f”,并将变量括在括号中

def ols_reg(filename):
    df = pd.read_csv(f"{filename}.csv")     #assumption: the directory of both files is same  
    print(df)

相关问题 更多 >