1
1

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.

Pandas.DataFrame 小技集 (行列操作編)

Last updated at Posted at 2020-11-08

空のデータフレームを作成

sample.py
    import pandas as pd

    # カラム名の設定
    cols = ['id', 'name']
    # 空のデータフレームを作成
    df = pd.DataFrame(index=[], columns=cols)
    # データ内容作成
    data = [1, 'test']
    # データフレームにデータを追加
    df.loc[len(df)] = data
    print(df)
    '''
      id  name
    0  1  test
    '''

リストを使って複数行をデータフレームに追加

sample.py
    # カラム名の設定
    cols = ['id', 'name']
    # 空のデータフレームを作成
    df = pd.DataFrame(index=[], columns=cols)
    # リストを作成
    id_list = [1, 2, 3, 4, 5]
    name_list = ['Taro', 'Hanako', 'Akira', 'Hitomi', 'Kenta']
    # 連結用のデータフレーム作成
    df_tmp = pd.DataFrame({'id': id_list ,'name': name_list})
    # データフレームを縦に連結
    df = df.concat([df, df_tmp])
    print(df)
    '''
      id    name
    0  1    Taro
    1  2  Hanako
    2  3   Akira
    3  4  Hitomi
    4  5   Kenta
    '''
1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?