0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Digisven
  • 1
  • 1
  • Supposing you just want to concatenate the columns separated by a space, you can simply write : `df["Name"] = df["Name1"] + " " + df["Name2"] + " " + df["Name3"]` – luca Jun 22 '23 at 14:33
  • Oh i forgot, It´s separated by a tabulator. – Digisven Jun 22 '23 at 14:37
  • for more discussion on concatenation of columns , [combine two columns and create new](https://stackoverflow.com/questions/19377969/combine-two-columns-of-text-in-pandas-dataframe) – simpleApp Jun 22 '23 at 14:40
  • Please provide a MRE (see [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/14311263)), instead of images. (We need samples to **work** with, not only to **look** at.) – Timus Jun 22 '23 at 17:47
  • Ok, thank you for editing. I hope now it´s ok with my new informations. – Digisven Jun 23 '23 at 07:20

1 Answers1

0

You can join multiple string columns together nice & neatly using .apply.

To concatenate each value with one space in between:

df['Name'] = df[['Name_1', 'Name_2', 'Name_3']].apply(' '.join, axis=1)
acrobat
  • 812
  • 4
  • 7