你好,我有这个文件:
a|b|c|d|
1.05 january february 05-02-2010 Linux Mint
3.458 november december 05-02-2010 Windows 10
我想将pandas与Python一起在csv中转换此文件,如下所示:
a;b;c;d
1.05;january february;05-02-2010;Linux Mint
3.458;november december;05-02-2010;Windows 10
我真的不知道要这么做吗……你能帮我吗?
谢谢!
答案 0 :(得分:1)
您可以这样做:
In [212]: df = pd.read_csv('ff.txt', skiprows=1, header=None, sep=' ')
In [211]: df
Out[211]:
0 1 2 3 4 5
0 1.050 january february 05-02-2010 Linux Mint
1 3.458 november december 05-02-2010 Windows 10
In [232]: df['months'] = df[1].map(str) +' '+ df[2]
In [233]: df['end_cols'] = df[4].map(str) +' '+ df[5]
In [237]: df.drop([1,2,4,5], axis=1, inplace=True)
In [238]: df = df[[0,'months',3,'end_cols']]
In [239]: df
Out[239]:
0 months 3 end_cols
0 1.050 january february 05-02-2010 Linux Mint
1 3.458 november december 05-02-2010 Windows 10
#Now, extract header from original file
In [216]: with open('ff.txt', 'r') as f:
...: header = f.readline()
In [217]: header
Out[217]: 'a|b|c|d\n'
In [245]: df.columns = header.strip().split('|')
# write the data to the file with delimiter =';'
In [224]: df.to_csv('abc.csv', sep=';', index=False)
#Final Output
mayankp@mayank:~/$ cat abc.csv
a;b;c;d
1.05;january february;05-02-2010;Linux Mint
3.458;november december;05-02-2010;Windows 10
答案 1 :(得分:0)
我不清楚您是要在内存中转换还是写回文件。
如果是后者,您可能正在寻找
df.to_csv(out_df='filename', sep=';')