0

I am using pd.read_fwf and am provided with a list of widths for each of the columns. There are several columns name "Filler" which prevents me from reading in the dataframe because ValueError: Duplicate names are not allowed. How can I generate suffixes for each of the Filler colnames so that I can properly use pd.read_fwf?

I have a dataframe of Column Names and Width:

col_widths = pd.DataFrame({'name': ['Filler', 'Col A', 'Filler', 'Col B'],'width': [2, 8, 4, 6]})
    name    width
0   Filler  2
1   Col A   8
2   Filler  4
3   Col B   6
cpage
  • 119
  • 6
  • 27

1 Answers1

0

I'm not sure if I understand correctely the question, but if you have a dataframe with the column names, you can work on it to rename the duplicates like this example: gbtimmon.

And testing read_fmf it automatically adds a number in the end of the column name if the file has duplicated column names. For example, to file a.txt:

a  a  c  d
1  3  9  8
2  3  8  5
3  3  6  4
4  4  1  9

pd.read_fwf("a.txt").columns
>>> Index(['a', 'a.1', 'c', 'd'], dtype='object')