Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我们如何编写函数以获取文件名并在pandas中创建数据帧?(没有pandas内置函数)_Python_Pandas_Function_Writing - Fatal编程技术网

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

Python 我们如何编写函数以获取文件名并在pandas中创建数据帧?(没有pandas内置函数),python,pandas,function,writing,Python,Pandas,Function,Writing,我想写一个函数,以CSV文件的名称,然后创建它的熊猫数据帧打印? 我编写的代码太简单,但不起作用: def ols_reg(filename): ''' ''' df = pd.read_csv("filename.csv") #assumption: the directory of both files is same print(df) 如果只是在字符串中插入“filename”,它不会将其作为变量读取。将其作为f字符串引入,方法是在字符串

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

def ols_reg(filename):
    '''      '''
    df = pd.read_csv("filename.csv")     #assumption: the directory of both files is same  
    print(df) 
如果只是在字符串中插入“filename”,它不会将其作为变量读取。将其作为f字符串引入,方法是在字符串前面抛出“f”,并将变量括在括号中

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

你的问题有点不清楚。你到底想干什么?将文件名传递给函数,然后该函数对该文件执行某些操作?
但不起作用:
-这是什么意思?你能再详细一点吗?它有什么不足之处?其结果与您的要求有何不同?