在熊猫中将逗号分隔的 CSV 转换为制表符分隔的 CSV

时间:2021-06-14 08:24:22

标签: python csv export-to-csv

我正在使用 python 我有一个 CSV 文件,其中的值由制表符分隔, 我对它的每一行应用了一个规则并创建了一个新的 csv 文件,结果数据框是逗号分隔的,我希望这个新的 csv 也用制表符分隔。我该怎么做 ? 我知道使用 sep = '\t' 可以工作,但我在哪里应用它? 我应用了以下代码,但它也不起作用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Grid and Flexbox</title>

    <link rel="stylesheet" href="canvas.css"/> 
</head>
<body>

<!-- GRID BOXES-->
<div class="grid-container"> 
    <div class="side-bar">  
        <div> Grid title 1</div>
        <div><p>  </p></div>
    
    </div>
    <div class="main">      Grid title 2</div>
    <div class="footer">    Grid title 3</div>
    <div class="ad">        Grid title 4
        <div><p></p></div>
    </div>
</div>

    
</body>
</html>

enter image description here

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:2)

你试过这个吗?

pd.read_csv('file.csv', sep='\t')

答案 1 :(得分:1)

我发现了问题,规则将类型更改为“对象”,因此我无法执行任何进一步的操作。我遵循Remove dtype at the end of numpy array,并将我的数据框转换为一个列表,解决了问题。

df = pd.read_csv('data.csv', header=None)
df_norm= df.apply(lambda x:np.where(x>0,x/x.max(),np.where(x<0,-x/x.min(),x)),axis=1)
df_norm=df_norm.tolist()
df_norm = np.squeeze(np.asarray(df_norm))
np.savetxt('result.csv', df_norm, delimiter=",")