Pandas DataFrame へのデータのロードは、データ分析と操作の一般的なタスクです。これを実現する最も簡単な方法の 1 つは、CSV ファイルからデータを読み取ることです。これを実現する方法は次のとおりです。
pandas.read_csv 関数は、CSV ファイルを Pandas DataFrame に読み取る便利な方法を提供します。次の CSV ファイルについて考えてみましょう:
Date,"price","factor_1","factor_2" 2012-06-11,1600.20,1.255,1.548 2012-06-12,1610.02,1.258,1.554 2012-06-13,1618.07,1.249,1.552 2012-06-14,1624.40,1.253,1.556 2012-06-15,1626.15,1.258,1.552 2012-06-16,1626.15,1.263,1.558 2012-06-17,1626.15,1.264,1.572
このデータを DataFrame にインポートするには、次の Python コードを使用できます:
import pandas as pd # Specify the file path file_path = "data.csv" # Read the CSV file into a DataFrame df = pd.read_csv(file_path) # Print the DataFrame print(df)
このコードは、次の内容で Pandas DataFrame を作成します。構造体:
Date | price | factor_1 | factor_2 |
---|---|---|---|
2012-06-11 | 1600.20 | 1.255 | 1.548 |
2012-06-12 | 1610.02 | 1.258 | 1.554 |
2012-06-13 | 1618.07 | 1.249 | 1.552 |
2012-06-14 | 1624.40 | 1.253 | 1.556 |
2012-06-15 | 1626.15 | 1.258 | 1.552 |
2012-06-16 | 1626.15 | 1.263 | 1.558 |
2012-06-17 | 1626.15 | 1.264 | 1.572 |
以上がCSV データを Pandas DataFrame にインポートするにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。