Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14040

how to rename dataframe columns that match regex

$
0
0

I'm trying to rename pandas dataframe columns that match a regular expression and leave others as they are.

I have multiple dataframes and some of them do not contain columns which match the regex at all.

For example:

#no matching columns['Description', 'Image']
# 3 Matching columns['Name (Ka)', 'Name (En)', Name('Ru'), 'Description', 'Image']

Here is my list comprehension and languages dict.

languages = {'ka' : 1,'en' : 2,'ru' : 3}
#in case of non-matching dataframe this comprehensions raises "ValueError Length mismatch: Expected #axis has 2 elements, new values have 0 elements" because it returns empty list df.columns = [m.group(1) +'_lang'+ str(languages[m.group(2)]) for m in (re.match("^([a-zA-Z]+)\s*\(([a-zA-Z]+)\)$", col.strip().lower()) for col in df.columns) if m]

Desired result in case of dataframe with matching columns:

['name_lang1', 'name_lang2', 'name_lang3', 'Description', 'Image']

Is it possible to achieve this in one line ?

Thanks in advance


Viewing all articles
Browse latest Browse all 14040

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>