1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

任意のディレクトリ配下のCSVファイルをすべてDataFrameに落とし込む

Last updated at Posted at 2020-09-06

DataFrameに落とし込みたいCSVファイルが大量にある場合は
下記コマンドを使うことで『ファイル名_df』という変数名でDataFrameを定義できます。

import os
import pandas as pd

for dirname, _, filenames in os.walk("""任意のディレクトリ名"""):
    for filename in filenames:
        path = os.path.join(dirname, filename)
        # execコマンドを用いてファイル名を変数名として動的に定義していく
        exec("{0}_df = pd.read_csv(path)".format(filename.replace('.csv','')))
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?