删除重复的分隔符熊猫

时间:2020-06-27 20:56:11

标签: python python-3.x pandas dataframe

我有以下几行是我从Excel工作表中读取的

CM              FOTO 1              FOTO 2              FOTO 3              FOTO 4           
some text some  744905_L050_01.jpg  NaN                 744905_B001_01.jpg  NaN
some text some  NaN                 NaN                 NaN                 NaN
some text some  621059_W034_01.jpg  621059_W034_02.jpg  621059_W034_03.jpg  NaN     
some text some  NaN                 NaN                 NaN                 NaN

然后我要编写一个新的csv文件,并创建以下列:

df_["DESCRIPTION"] = df["CM "]
df_["IMAGES_URL"] = df[["FOTO 1 ", "FOTO 2 ", "FOTO 3 ", "FOTO 4 "]].apply(lambda x: x.str.cat(sep='|'), axis=1)
df_["WAREHOUSE"] = "D"

最后我将其存储在一个csv文件中,并使用分号作为分隔符:

df_.to_csv('path/my_file.csv', encoding = 'utf-8', index=False, sep=';')

输出如下:

DESCRIPTION;IMAGES_URL;WAREHOUSE
some text some;744905_L050_01.jpg|744905_B001_01.jpg;D
some text some;;D
some text some;621059_W034_01.jpg|621059_W034_02.jpg|621059_W034_03.jpg;D
some text some;;D

但是,如果所有四个Foto列都具有NaN值,则会将双;;写入csv文件。 如何删除双分号并仅用一个分号代替?

1 个答案:

答案 0 :(得分:1)

如果您人为地替换了“ ;;”带有“;”,您将获得来自诸如Foto 3的数据进入Foto 2列。那真的是你想要的吗?如果没有,您可以使用以下其他方式填充NaN:

    $conditional1 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
    $conditional1->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT);
    $conditional1->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_CONTAINSTEXT);
    $conditional1->getText('A21', 'ID-');
    $conditional1->setText('KEY');
    $conditional1->getStyle()->getFont()->setBold(false);

    $conditional2 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
    $conditional2->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CONTAINSTEXT);
    $conditional2->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_CONTAINSTEXT);
    $conditional1->getText('A21', '00');
    $conditional1->setText('GLOBOPLASTIC');
    $conditional2->getStyle()->getFont()->setBold(true);

    $conditionalStyles = $spreadsheet->getActiveSheet()->getStyle('E2')->getConditionalStyles();
    $conditionalStyles[] = $conditional1;
    $conditionalStyles[] = $conditional2;

    $sheet->getStyle('E2')->setConditionalStyles($conditionalStyles);
    
    $filename = ''.utf8_decode($client['name']).'.xlsx';
        // Redirect output to a client's web browser (Xlsx)
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'.$filename.'"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');
 
        // If you're serving to IE over SSL, then the following may be needed
        ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
        header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header('Pragma: public'); // HTTP/1.
        
        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('myfile.xlsx');
        $writer->save('php://output');