简体   繁体   中英

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM