如何从文件夹中读取每个文件并为每个文件创建单独的数据帧?

时间:2019-07-09 09:35:51

标签: python pandas file directory

我正在尝试获取代码以读取包含各种文件的文件夹。 我希望Jupyter能够读取该文件夹中的每个文件,并通过将文件名作为数据帧名称来创建单独的数据帧。

到目前为止,我已经有了代码:

import glob

path = r'C:\Users\SemR\Documents\Jupyter\Submissions' 
all_files = glob.glob(path + "/*.csv")

li = []

for filename in all_files:
    df = pd.read_csv(filename, index_col=None, header=0, usecols=['Date', 'Usage'])
    li.append(df)

frame = pd.concat(li, axis=0, ignore_index=True)

此代码将数据连接在一起,但是我想为每个数据框使用单独的数据帧,以便可以分别存储值。有什么可以代替的吗?

以下是CSV文件外观的示例: enter image description here

这些CSV文件位于同一文件夹中,因此我希望在运行代码时,将使用与CSV文件名相同的名称来创建新的数据框。

谢谢。

2 个答案:

答案 0 :(得分:1)

对每个数据框使用不同变量的更好方法是将每个数据框加载到字典中。

可以使用os.path.basename()os.path.splitext()的组合来提取每个文件名的基本名称。

例如:

d = {os.path.splitext(os.path.basename(f))[0] : pd.read_csv(f) for f in glob.glob('*test*.csv')} 

此外,使用*test*可以避免理解中需要if

答案 1 :(得分:1)

从这个问题我可以建议您在列表中存储了不同的DataFrame。

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout17"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/hint_color">


    <android.support.constraint.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/constraintLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:visibility="visible">

        <TextView
            android:id="@+id/numberTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:gravity="top"
            android:text="00000000"
            android:textColor="@color/txt_color"
            android:textSize="18sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent="0.4"
        app:layout_constraintStart_toStartOf="parent">

        <Button
            android:id="@+id/confirmButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_rounded_button"
            android:padding="14dp"
            android:text="Confirm Pin"
            android:textColor="@color/colorPrimary"
            android:textSize="14sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/constraintLayout2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent="0.4"
        app:layout_constraintStart_toStartOf="parent">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/pinView1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.25"
                android:gravity="center"
                android:text="-"
                android:textColor="@color/txt_color"
                android:textSize="36sp" />

            <TextView
                android:id="@+id/pinView2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.25"
                android:gravity="center"
                android:text="-"
                android:textColor="@color/txt_color"
                android:textSize="36sp" />

            <TextView
                android:id="@+id/pinView3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.25"
                android:gravity="center"
                android:text="-"
                android:textColor="@color/txt_color"
                android:textSize="36sp" />

            <TextView
                android:id="@+id/pinView4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.25"
                android:gravity="center"
                android:text="-"
                android:textColor="@color/txt_color"
                android:textSize="36sp" />
        </LinearLayout>
    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>