ChRz
ChRz

Reputation: 11

Import all csv and write them to a dataframe

i'm new to python and my trying to convert some csv files in my workspace to a dataframe to use them later on for a pca analysis. Somehow I cant even import the files to a frame. What did I do wrong?

   ### Imports####

import os
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd


########FileImport##########

filepath= '.'  #  Dateipfad, Absolut vom Notebook aus
files = os.listdir(filepath) # File colllection
csvfiles = list(filter(lambda x: '.csv' in x, files)), #  File filter just csv
for filename in csvfiles:
    # Read data from a csv
    df = pd.read_csv(filename)
    data = df.append

The error i get is : Invalid file path or buffer object type:

Upvotes: 0

Views: 32

Answers (1)

Mike Xydas
Mike Xydas

Reputation: 489

I ran your code and it seems the comma (",") at the end of the line when you create the csvfiles list makes it a tuple of a list.

I changed:

csvfiles = list(filter(lambda x: '.csv' in x, files)),    # Wrong

to:

csvfiles = list(filter(lambda x: '.csv' in x, files))    # Correct

Full corrected code:

   ### Imports####

import os
import pandas as pd


########FileImport##########

filepath= '.'  #  Dateipfad, Absolut vom Notebook aus
files = os.listdir(filepath) # File colllection
csvfiles = list(filter(lambda x: '.csv' in x, files)) # REMOVED the comma,  File filter just csv
for filename in csvfiles:
    print(csvfiles)
    # Read data from a csv
    df = pd.read_csv(filename)
    data = df.append

Upvotes: 1

Related Questions