Я искал много вопросов в прошлом, но не смог найти ответ на мою проблему!
Я пытаюсь заменить имена столбцов фрейма данных, передав два списка.Первый список (имена текущих столбцов) в качестве аргумента to_replace и второй список в качестве значения (имена новых столбцов).Согласно документации df.replace, можно передавать списки:
список строк, регулярных выражений или числовых значений:
- First, if `to_replace` and `value` are both lists, they
**must** be the same length.
- Second, if ``regex=True`` then all of the strings in **both**
lists will be interpreted as regexs otherwise they will match
directly. This doesn't matter much for `value` since there
are only a few possible substitution regexes you can use.
- str, regex and numeric rules apply as above.
Myкод:
CurrentColNames=list(df.columns)
NewColNames=['Hello','Hi', ...'Bye'] #Just an example. Lists are of same size and type.
df.rename(columns={c: c.replace(CurrentColNames,NewColNames) for c in df.columns},inplace=True)
Я получаю ошибку:
TypeError: replace () аргумент 1 должен быть str, а не списком
Но документациясказал, можно пройти списки!Я что-то пропустил?Любая помощь?!