3
6

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 5 years have passed since last update.

DataFrame型かndarray型かどうかをif文で判定する方法

Last updated at Posted at 2019-10-03

まえがき

pythonでdataFrame型かどうかをif文で判定、または、ndarray型かどうかをif文で判定したい場合に、この記事が役に立ちます。
ちなみに、DataFrame型かどうかをif文で判定する参考サイトが全然出てこなかったのですが、自分だけですかね・・・。

実行環境

python 3
jupyter lab

実際のコード

以下をのコードをさくっと、コピペして下さい。

[in]
import numpy as np
import pandas as pd

# 変数名をそのままprint関数内で表示させる関数
def chkprint(*args):
    for obj in args:
        for k, v in globals().items():
            if id(v) == id(obj):
                target = k
                break          
    return target

# データがどのデータ型か、列数、行数を表示する関数
def typeInfo(targetData):
    if (type(targetData) is pd.core.frame.DataFrame):
        print("{} は DataFrame型".format(chkprint(targetData)))
        print("{} の行数, 列数・・・{}\n".format(chkprint(targetData), targetData.shape))     # shapeの表示内容は、(行数, 列数)となる
    if (type(targetData) is list):
        print("{} は list型".format(chkprint(targetData)))
        print("{} の行数, 列数・・・{}\n".format(chkprint(targetData), pd.DataFrame(targetData).shape))    # shapeの表示内容は、(行数, 列数)となる
    if (type(targetData) is np.ndarray):
        print("{} は ndarray型".format(chkprint(targetData)))
        print("{} の行数, 列数・・・{}\n".format(chkprint(targetData), targetData.shape))     # shapeの表示内容は、(行数, 列数)となる
    if (type(targetData) is pd.core.series.Series):
        print("{} は Series型".format(chkprint(targetData)))
        print("{} の行数, 列数・・・{}\n".format(chkprint(targetData), targetData.shape))     # shapeの表示内容は、(行数, 列数)となる

確認方法

以上のコードに続いて、以下のコードを実行して出力を確認して下さい。

[in]
data_list = [[1.0, 2.0, 3.0], [11.0, 12.0, 13.0]]
data_ndarray = np.array(data_list)      #listをndarrayに変換する
data_Series = pd.Series(data_list)      #listをSeriesに変換する
data_df = pd.DataFrame(data_list)       #listをDataFrameに変換する

typeInfo(data_list)
typeInfo(data_ndarray) 
typeInfo(data_Series)
typeInfo(data_df)

出力結果

[out]
data_list は list型
data_list の行数, 列数・・・(2, 3)

data_ndarray は ndarray型
data_ndarray の行数, 列数・・・(2, 3)

data_Series は Series型
data_Series の行数, 列数・・・(2,)

data_df は DataFrame型
data_df の行数, 列数・・・(2, 3)

Seriesのみ、列数が出ません。Seriesは一次元だからです。

Series型とDataFrame型を表示して比較してみる

おまけ。Seriesは一次元だからと言って、表示してみると以下のようになります。

[in]
print("\nSeriesを表示する")
print(data_Series)
print("\nDataFrameを表示する")
print(data_df)

出力結果

[out]
Seriesを表示する
0       [1.0, 2.0, 3.0]
1    [11.0, 12.0, 13.0]
dtype: object

DataFrameを表示する
      0     1     2
0   1.0   2.0   3.0
1  11.0  12.0  13.0

あとがき

もっと良いやり方があったら、コメントして下さい。

3
6
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
3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?