dataframeにcsvデータを読み込む方法
とりあえず読み込みするだけなら以下でOK
df.read_csv('読み込みファイルpath')
サンプル
デスクトップにある[sample.csv]というファイルを読み込むサンプルコード
,no,name,level,type1,type2,helditem
0,25,pika,88,Electric,,
1,131,rara,80,Water,Ice,
2,143,gon,82,Normal,,
3,3,fushi,84,Grass,Poison,
4,6,kage,84,Fire,Flying,
5,9,kame,84,Water,,
import os
import pandas as pd
import numpy as np
# デスクトップパス取得
desktop_path = os.path.expanduser('~/Desktop')
# 読み込むファイル名を追加して読み込むパスを設定
in_path = os.path.join(desktop_path,r'sample.csv')
# csv読み込み
df = pd.read_csv(in_path)
# 読み込んだdf確認
print(df)
"""
Unnamed: 0 no name level type1 type2 helditem
0 0 25 pika 88 Electric NaN NaN
1 1 131 rara 80 Water Ice NaN
2 2 143 gon 82 Normal NaN NaN
3 3 3 fushi 84 Grass Poison NaN
4 4 6 kage 84 Fire Flying NaN
5 5 9 kame 84 Water NaN NaN
"""
仕様
pandas.read_csv(filepath_or_buffer, *, sep=_NoDefault.no_default, delimiter=None, header=’infer’, names=_NoDefault.no_default, index_col=None, usecols=None, squeeze=None, prefix=_NoDefault.no_default, mangle_dupe_cols=True, dtype=None, engine=None, converters=None, true_values=None, false_values=None, skipinitialspace=False, skiprows=None, skipfooter=0, nrows=None, na_values=None, keep_default_na=True, na_filter=True, verbose=False, skip_blank_lines=True, parse_dates=None, infer_datetime_format=False, keep_date_col=False, date_parser=None, dayfirst=False, cache_dates=True, iterator=False, chunksize=None, compression=’infer’, thousands=None, decimal=’.’, lineterminator=None, quotechar='”‘, quoting=0, doublequote=True, escapechar=None, comment=None, encoding=None, encoding_errors=’strict’, dialect=None, error_bad_lines=None, warn_bad_lines=None, on_bad_lines=None, delim_whitespace=False, low_memory=True, memory_map=False, float_precision=None, storage_options=None)
https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html
コメント