繁体   English   中英

将数据帧转换为csv时出现问题

[英]Have an issue converting a dataframe to csv

我编写了一个函数来清理csv文件,使用正则表达式函数并将其导出到csv。 函数的输入fn_file将从文件夹'x'获取文件,文件名为file.csv,并处理该文件并将处理后的文件作为'file_processed.csv'导出到'x'文件夹中。 将数据帧转换为csv时,会显示以下错误。 如何将标题作为列添加到文件中

Function
--------
process_file

Use regex to create a file with title,date and header

Parameters
----------
fn_file : str
    Name of file scraped from WDET

fn_out : str
    Name of new, reformatted, file

Returns
-------
Nothing returned. Just creates output file.

Example
-------
process_file('E:/data.csv' , 'E:/data_processed.csv')

错误在行

提出ValueError('DataFrame构造函数未正确调用!')ValueError:未正确调用DataFrame构造函数!

s_df = pd.DataFrame(data = fn_file,columns = [header])

我的代码如下


def process_file(fn_file , fn_csv_out):

    s = re.compile(r'.*?word.+?\(\d{1,2}[ap]m-\d{1,2}[ap]m\)\s+$')

    date = re.compile(r'(Sunday)\s+(\w+\s+\d+,\s+(2010))')

    he = re.compile(r'\t\w+.\t\w+\t\w+\t\w+\s\(\w+\)\t\w+$')

    son = re.compile(r'^.*\t\d+\t.+\t')

    # Initialize counters
    num_lines = 0
    num_s = 0
    num_date = 0
    num_he = 0
    num_son = 0
    num_unmatched = 0

    # Initialize empty list to store output lines
    sonlines = []

    # Initialize string vars to use for the show title and date
    title = ''
    date = ''

    with open(fn_file) as f:

        # Loop over the lines in the file
        for line in f:

            num_lines +=1


            line = line.rstrip('\n')

            m_s = re.match(s, line)
            m_date = re.match(date, line)
            m_he = re.match(he, line)
            m_son = re.match(son, line)


            if m_s:


                num_s += 1

                # Get the show title
                ti =  m_s.group()

            elif m_date:
                # it's a date line
                num_date += 1
                show_day = m_date.group(1)
                s_date = m_date.group(2)

            elif m_he:
                # it's a header line
                num_he += 1
                heline = m_he.group()

            elif m_son:

                num_son += 1
                son_group = m_son.group()
                son = re.split(r'\t+', son_group)
                son.insert(0,ti)
                son.insert(1,s_date)
                sonlines.append(son)


    header = re.split(r'\t+',heline.rstrip('\t'))           
    header[0] = 'b'               
    header.insert(0,'ti')       
    header.insert(1,'s_date')    

    # Create pandas dataframe and export to csv

```lines throwing error
    s_df = pd.DataFrame(data = fn_file, columns = [header])
    s_df.to_csv(fn_csv_out, sep='\t', index= False)

Last two lines are throwing error, Can you please help on the error. Thanks in advance.

问题在评论中得到了解决。 该变量被传递给DataFrame构造函数。 修复它所以改变它

s_df = pd.DataFrame(data = sonlines, columns = [header])

代替

s_df = pd.DataFrame(data = fn_file, columns = [header])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM