WinFuture-Forum.de: Python Pandas - Kombinieren von mehreren Spalten CSV - WinFuture-Forum.de

Zum Inhalt wechseln

Nachrichten zum Thema: Entwicklung
Seite 1 von 1

Python Pandas - Kombinieren von mehreren Spalten CSV


#1 Mitglied ist offline   Digisven 

  • Gruppe: aktive Mitglieder
  • Beiträge: 250
  • Beigetreten: 27. Februar 04
  • Reputation: 1
  • Geschlecht:Männlich
  • Wohnort:Nähe Hannover

geschrieben 22. Juni 2023 - 15:57

Hallo zusammen. :)

Ich versuche in Python mehrere Spalten aus einer CSV-Datei zusammenzufügen und in eine neue Spalte in einer neuen CSV-Datei.

So würde mein Code aussehen:

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 using the aggregate function 'sum'
df["Name"] = df.groupby(['Name_1'])['Name_2']['Name_3'].transform('sum')

# Save the result to a new CSV file
df.to_csv("Test-new.csv", index=False)



oder evtl. auch so:

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)



oder evtl. auch so:

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["Name1"] + " " + df["Name2"] + " " + df["Name3"]

# Save the result to a new CSV file
df.to_csv("Test-new.csv", index=False)



Leider funktionieren alle 3 nicht so richtig, es gibt nur Fehler.

In der CSV sind die Einträge mit TAB getrennt, so als Information.

Wisst ihr evtl. noch etwas? :)

Danke schonmal. :)
0

Anzeige



#2 Mitglied ist offline   Stef4n 

  • Gruppe: aktive Mitglieder
  • Beiträge: 1.216
  • Beigetreten: 20. August 18
  • Reputation: 251
  • Geschlecht:Männlich
  • Wohnort:RLP ~Mainz
  • Interessen:pc

geschrieben 22. Juni 2023 - 21:28

Ein Beispiel, wie die eingelesene CSV Datei aussieht und welcher Fehler ausgegeben wird, wäre hier etwas hilfreich.
... aber bitte vorher ein Backup machen! ;-)
0

#3 Mitglied ist offline   Digisven 

  • Gruppe: aktive Mitglieder
  • Beiträge: 250
  • Beigetreten: 27. Februar 04
  • Reputation: 1
  • Geschlecht:Männlich
  • Wohnort:Nähe Hannover

geschrieben 23. Juni 2023 - 08:06

Hallo. :)

Stimmt, wäre hilfreich. Hier eine CSV: https://filehorst.de/d/erbwxyuJ

Also davon will ich Name_1 / Name_2 / Name_3 in ein Feld Name bekommen.

Und hier die Fehler:

Zu Code 1 folgender Fehler:

C:\Users\s.123\Downloads\python>Test.py
Traceback (most recent call last):
  File "C:\Users\s.123\Downloads\python\Test.py", line 7, in <module>
    df["Name"] = df.groupby(['Name_1'])['Name_2']['Name_3'].transform('sum')
                 ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\s.123\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\frame.py", line 8252, in groupby
    return DataFrameGroupBy(
           ^^^^^^^^^^^^^^^^^
  File "C:\Users\s.123\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\groupby\groupby.py", line 931, in __init__
    grouper, exclusions, obj = get_grouper(
                               ^^^^^^^^^^^^
  File "C:\Users\s.123\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\groupby\grouper.py", line 985, in get_grouper
    raise KeyError(gpr)
KeyError: 'Name_1'




Zu Code 2 folgender Fehler:

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]"




Zu Code 3 folgender Fehler:

C:\Users\s.123\Downloads\python>Test2.py
Traceback (most recent call last):
  File "C:\Users\s.123\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\indexes\base.py", line 3652, in get_loc
    return self._engine.get_loc(casted_key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "pandas\_libs\index.pyx", line 147, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 176, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 7080, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 7088, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Name1'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\s.123\Downloads\python\Test2.py", line 7, in <module>
    df["Name"] = df["Name1"] + " " + df["Name2"] + " " + df["Name3"]
                 ~~^^^^^^^^^
  File "C:\Users\s.123\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\frame.py", line 3761, in __getitem__
    indexer = self.columns.get_loc(key)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\s.123\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\indexes\base.py", line 3654, in get_loc
    raise KeyError(key) from err
KeyError: 'Name1'



Hoffe das hilft. :)

Danke schonmal.
0

Thema verteilen:


Seite 1 von 1

1 Besucher lesen dieses Thema
Mitglieder: 0, Gäste: 1, unsichtbare Mitglieder: 0