Pandas Data Frame to_csv with more separator

筅森魡賤 提交于 2019-12-06 14:12:27

问题


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: "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

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


回答1:


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')

output of test.txt

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

UPDATE

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')



来源:https://stackoverflow.com/questions/45983286/pandas-data-frame-to-csv-with-more-separator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!