简体   繁体   中英

Removing brackets and commas from a Dataframe column (28k rows) with no header Python 3.8

I have a CSV file with two columns (no header) like this,

Input.csv

0,"[3001, 12029, 14145, 14270, 14581, 16976, 25564]"
1,"[17, 34, 190, 875, 951, 1370, 2003, 2039, 2211, 2514, 3153, 3290, 3364, 3490, 4069, 5011, 5789]"
2,"[32, 808, 3354, 9835, 10082, 14276, 18084, 24576, 26177]"
3,"[3421, 3585, 5150, 5607, 9093, 12034, 15401, 16049, 24280]"
4,"[1116, 5203, 5252, 6347, 10838, 14995, 16304, 17462, 23757, 24023, 24122]"
5,"[872, 1971, 2040, 2518, 4081, 5786, 7029, 7224, 8596, 8775, 9798, 11385, 11780]"
6,[935]
...
28212,[28259]

I want to remove the brackets and commas of each array in the column and the comma, which separates each column. I would like something like this,

output.csv

0 3001 12029 14145 14270 14581 16976 25564
1 17 34 190 875 951 1370 2003 2039 2211 2514 3153 3290 3364 3490 4069 5011 5789
2 32 808 3354 9835 10082 14276 18084 24576 26177
3 3421 3585 5150 5607 9093 12034 15401 16049 24280
4 1116 5203 5252 6347 10838 14995 16304 17462 23757 24023 24122
5 872 1971 2040 2518 4081 5786 7029 7224 8596 8775 9798 11385 11780
6 935
...
28212 28259

I have tried str.replace and str.strip, but it does not work. I have also tried this Removing brackets and comma's in list and this Removing brackets from a DataFrame column when exporting to CSV without success.

fout = open('output.csv','w')
for line in open('input.csv'):
    fout.write( line.replace('"','').replace(' ','').replace('[','').replace(']','') )

Remember, however, that once you do this, you can't read it into pandas, because it won't have a constant number of columns.

A slightly cleaner answer in my opinion than all the calls to replace (in the above answer) might just be to do this with regex:

import re
re.sub("\[|,|\]", "", line)

which will get rid of all brackets and commas

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM