Skip to content

DataFrame.LoadCsv can not load CSV with duplicate column names #6182

Closed

Description

@torronen

Code:
IDataView trainData = DataFrame.LoadCsv(TrainDatasetPath, separator: ';', header: true, guessRows: 100);

Gives exception:
DataFrame already contains a column called Target20 (Parameter 'column')

Suggestion:
It would be nice if LoadCsv would have the option to ignore or auto-rename duplicate columns.
For small CSV files it is not a big problem, but for huge CSV files renaming headers is a hassle.

Activity

  1. ghost added
    untriagedNew issue has not been triaged
    on May 3, 2022
  2. torronen commented on May 3, 2022

    @torronen
    ContributorAuthor

    If anyone has same problem renamed header names can put in the parameter. This solves my issue. I do not know if LoadCsv should have this functionality inbuilt or not (or, issue to be closed or not)

    LoadCsv with renamed columns:

      string line1 = File.ReadLines(TrainDatasetPath).First();
                    string[] arr = line1.Split(';');
                    var duplicatedItems = arr.GroupBy(a => a)
                                           .Where(g => g.Count() > 0)
                                           .ToDictionary(g => g.Key, g => g.Count());
    
                    for (int i = arr.Length - 1; i >= 0; i--)
                    {
                        string item = arr[i];
                        if (!duplicatedItems.ContainsKey(item))
                        {
                            arr[i] = item;
                            continue;
                        }
    
                        if (duplicatedItems[item] > 1)
                        {
                            arr[i] = String.Format("{0}_{1}", item, duplicatedItems[item]);
                        }
                        
                        duplicatedItems[item]--;
                    }
    
                    IDataView trainData = DataFrame.LoadCsv(TrainDatasetPath, separator: ';', header: true, columnNames: arr, guessRows: 100);
    
  3. michaelgsharp commented on May 9, 2022

    @michaelgsharp
    Contributor

    @luisquintanilla @torronen is this something we think should be built in to load? I can see it going both ways honestly. I would probably lean towards having it built in somehow.

  4. luisquintanilla commented on May 9, 2022

    @luisquintanilla
    Contributor

    @michaelgsharp by builtin do you mean duplicate columns are automatically renamed like the snippet @torronen shared?

  5. beccamc commented on May 9, 2022

    @beccamc
    Contributor

    +1 - I also experienced pain around large data files with multiple columns sharing names. I only had less than 50 columns and it was still troublesome to deal with.

  6. added this to the ML.NET Future milestone on Jun 13, 2022
  7. ghost removed
    untriagedNew issue has not been triaged
    on Jun 13, 2022
  8. ghost added
    in-prA pull request is open to resolve this issue
    on Jul 24, 2023
  9. ghost removed
    in-prA pull request is open to resolve this issue
    on Aug 31, 2023
  10. ghost locked as resolved and limited conversation to collaborators on Sep 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions