question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a better way to rename columns in a DataFrame than the following code? I could not find any information for renaming columns in the Guide or API doc. In this code, I’m trying to rename columns “Like This” to “like_this”.

slugify = (s) => s.toLowerCase().replace(' ','_').replace('/','_or_')

rawData = 
    DataForge.readFileSync('data.csv')
    .parseCSV({ dynamicTyping: true })

for(let name of rawData.getColumnNames()) {
    let s = rawData.getSeries(name)
    rawData = rawData.dropSeries(name).withSeries(slugify(name), s)
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
ashleydaviscommented, Jun 7, 2018

You are correct! It should be using Map already. Thanks for leaving feedback and pointing it out.

I’ll leave this issue open until I get it fixed.

1reaction
ashleydaviscommented, Jun 7, 2018

Yeah there is! Are you able to use the function renameSeries?

const renamedDf = rawData.renameSeries({ "OldColumnName": "NewColumnName" });

You might be able to use it like this:

let rawData = 
    DataForge.readFileSync('data.csv')
    .parseCSV({ dynamicTyping: true })

let renameSpec = {};

for(let name of rawData.getColumnNames()) {
   renameSpec[name] = slugify(name);
}

rawData = rawData.renameSeries(renameSpec);

Otherwise I’ll have to think about this, seems like there should be a better way to do it!

Read more comments on GitHub >

github_iconTop Results From Across the Web

pandas.DataFrame.rename — pandas 1.5.2 documentation
Dict-like or function transformations to apply to that axis' values. Use either mapper and axis to specify the axis to target with mapper...
Read more >
How to Rename Columns in Pandas (With Examples)
You can use one of the following three methods to rename columns in a pandas DataFrame: Method 1: Rename Specific Columns df.rename(columns ......
Read more >
How to rename columns in Pandas DataFrame - GeeksforGeeks
One way of renaming the columns in a Pandas Dataframe is by using the rename() function. This method is quite useful when we...
Read more >
Renaming column names in Pandas - python - Stack Overflow
Rename Specific Columns. Use the df.rename() function and refer the columns to be renamed. Not all the columns have to be renamed:
Read more >
Rename Data Frame Columns in R - Datanovia
In this tutorial, you will learn how to rename the columns of a data frame in R.This can be done easily using the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found