Hello Stack Community!!
I am here with a beginner question, I am new to python and trying to practice using functions to make my code cleaner.
I am trying to modify all of the column names in a df via for loop and append those new delimited column names to a list. I am hoping I can then use that list to rename all the columns at once.
I know I could type this out and map it as a dictionary but there has to be a better way!
MY CODE:
you can see the unsuccessful output. Each column name should have a '_' to delimit the words.
any help is greatly appreciated.
I have tried several things and feel as though I am missing something very simple. I have struggled through the documentation and self learning resources I have.
For the sake of speed and trying to become more relevant in this community I figured I'd give stackoverflow a shot!
Your main issues are you are using a built-in name "list" as an arg and you never .split() on " " and finally you append the original name variable since you didn't reassign after .join().
You can use a list comprehension together with .split() and .join() and assign the return list to df.columns:
Fake Data:
import pandas as pd
data = {"Foo Spam": [1, 2, 3], "Ham Eggs": [4, 5, 6]}
RAW_edu = pd.DataFrame(data=data)
To make your code work:
def add_delimiter(cols: list) -> list:
d = "_"
delimited_cols = []
for name in cols:
name = d.join(name.split(" "))
delimited_cols.append(name)
return delimited_cols
RAW_edu.columns = add_delimiter(cols=RAW_edu.columns)
print(RAW_edu)
Using a generator and list comp Code:
def add_delimiter(cols: list) -> list:
return ["_".join(iter(x.split(" "))) for x in cols]
RAW_edu.columns = add_delimiter(cols=RAW_edu.columns)
print(RAW_edu)
Output:
Foo_Spam Ham_Eggs
0 1 4
1 2 5
2 3 6