带有更多分隔符的 pandas 数据框to_csv [英] Pandas Data Frame to_csv with more separator

查看:108
本文介绍了带有更多分隔符的 pandas 数据框to_csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个40列60万行的文件.在pandas数据帧中处理完之后,我想将数据帧保存到具有不同间距长度的csv中. df.to_csv中有一个Sep kwarg,我尝试过使用正则表达式,但出现错误

I have a file of 40 columns and 600 000 rows. After processing it in pandas dataframe, i would like to save the data frame to csv with different spacing length. There is a sep kwarg in df.to_csv, i tried with regex, but i'm getting error

TypeError:定界符"必须为1个字符的字符串.

TypeError: "delimiter" must be an 1-character string.

我希望输出具有不同的列间距,如下所示

I want the output with different column spacing, as shown below

A    B  C   D    E F  G
1    3  5   8    8 9  8
1    3  5   8    8 9  8
1    3  5   8    8 9  8
1    3  5   8    8 9  8
1    3  5   8    8 9  8

使用以下代码,我将制表符定界.它们都具有相同的间距.

Using the below code i'm getting the tab delimited. which are all with same spacing.

df.to_csv("D:\\test.txt", sep = "\t", encoding='utf-8')

A  B  C  D  E  F  G
1  3  5  8  8  9  8
1  3  5  8  8  9  8
1  3  5  8  8  9  8
1  3  5  8  8  9  8
1  3  5  8  8  9  8

我不想进行循环,因为60万行可能会花费很多时间.

I don't want to do looping, It might take lot of time for 600k lines.

推荐答案

感谢您的评论,它对我有所帮助. 下面是代码.

Thank you for comments, It helped me. Below is the code.

import pandas as pd

#Create DataFrame
df = pd.DataFrame({'A':[0,1,2,3],'B':[0,11,2,333],'C':[0,1,22,3],'D':[00,1,2,33]})

#Convert the Columns to string
df[df.columns]=df[df.columns].astype(str)

#Create the list of column separator width 
SepWidth = [5,6,3,8]

#Temp dict
tempdf = {}
#Convert all the column to series
for i, eCol in enumerate(df):
    tempdf[i] = pd.Series(df[eCol]).str.pad(width=SepWidth[i])

#Final DataFrame
Fdf = pd.concat(tempdf, axis=1)
#print Fdf
#Export to csv
Fdf.to_csv("D:\\test.txt", sep='\t', index=False, header=False, encoding='utf-8')

test.txt的输出

output of test.txt

0        0    0        0
1       11    1        1
2        2   22        2
3      333    3       33

更新

使用pandas.to_csv时,空格中包括制表符分隔('\ t').代表pandas.to_csv,我正在使用以下代码将其另存为txt.

Tab delimited ('\t') was included in spacing, while using pandas.to_csv. Behalf of pandas.to_csv i'm using below code to save as txt.

numpy.savttxt(file, df.values, fmt='%s')

这篇关于带有更多分隔符的 pandas 数据框to_csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆