I want to combine the 3 columns Name_1
, Name_2
and Name_3
to one Name
.
Here is the new script:
import pandas as pd
# Load the CSV file into a Pandas DataFrame
df = pd.read_csv("Test.csv", sep=r'\\t', engine='python')
# Combine cells in a specific column
df['Name'] = df[['Name_1', 'Name_2', 'Name_3']].apply(' '.join, axis=1)
# Save the result to a new CSV file
df.to_csv("Test-new.csv", index=False)
CSV example as file: https://filehorst.de/d/erbwxyuJ
CSV example as code (separated by tabs):
LFNR Name_1 Name_2 Name_3 Strasse Plz Ort Telefon Telefax
4 Wxxxxx.xe KxxxxY- XxY XxxxxxxxxxxY XxxxxxxxxxxxxxxxxY 25 11111 XxxxxxY 0123-1231230 0123-1231231
24 Fxxxx Kxxxx Rxxxxxxxxxxxxxxxk xxY Hxxxxxxxxxxxf Xxxx X xx. XX Ix Xxxxxxh 5 0123 KxxxY 012312-12312 012312-123123
25 Bxxxxxxxxxx Kxxxxxx Sxxxxxxxxxxxx Sxxxxx 123 0123 Cxxxxxx 0123 123123 0123 1231231
When I start the script I get this error:
C:\Users\s.123\Downloads\python>Test3.py
Traceback (most recent call last):
File "C:\Users\s.123\Downloads\python\Test3.py", line 7, in <module>
df['Name'] = df[['Name_1', 'Name_2', 'Name_3']].apply(' '.join, axis=1)
~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\s.123\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\frame.py", line 3767, in __getitem__
indexer = self.columns._get_indexer_strict(key, "columns")[1]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\s.123\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\indexes\base.py", line 5876, in _get_indexer_strict
self._raise_if_missing(keyarr, indexer, axis_name)
File "C:\Users\s.123\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\indexes\base.py", line 5935, in _raise_if_missing
raise KeyError(f"None of [{key}] are in the [{axis_name}]")
KeyError: "None of [Index(['Name_1', 'Name_2', 'Name_3'], dtype='object')] are in the [columns]"
What can I do to fix that?