• Skip to main content
  • Skip to primary sidebar

学習記録

Pandas

EXCELの読み込み

2020年1月19日 by 河副 太智 Leave a Comment

EXCELを読み込む際はxlrdを予めインポート

1
2
3
4
5
6
7
import xlrd
import pandas as pd
 
wb = xlrd.open_workbook('cn_hslist.xlsx')
 
file = pd.ExcelFile(wb, engine='xlrd')
df = pd.read_excel(file)

上記例は中国語のエクセルを開いたものなので、日本語の場合は
file = pd.ExcelFile(filename, encoding=’utf8′)
で読み込む

Filed Under: Pandas

データフレームの値をカンマ区切りにする

2018年11月28日 by 河副 太智 Leave a Comment

1
2
3
4
#金額をカンマ区切りにする
for i in range(len(df3['Trade Value (US$)'])):
    a = df3['Trade Value (US$)'][i]
    df3['Trade Value (US$)'][i] = "{:,}".format(float(a))

 

Filed Under: Pandas

DFカラム並べ替え

2018年11月13日 by 河副 太智 Leave a Comment

1
2
3
4
5
6
7
8
9
10
11
12
13
df例
'''
    a   b   c  
A   0   1   2  
S   5   6   7  
'''
並べ替え
df.loc[:,['a','c','b']]
'''
 
    a   c   b  
A   0   2   1
S   5   7   6

 

Filed Under: Pandas

DFカラムを複数削除する

2018年11月13日 by 河副 太智 Leave a Comment

1
2
3
4
5
to_remove = [1,2,'Classification','Period Desc."','Aggregate Level','Is Leaf Code','Trade Flow Code','Reporter Code',
             'Partner Code','Partner','Partner ISO','2nd Partner Code','2nd Partner','2nd Partner ISO','Customs Proc. Code',
             'Customs','Mode of Transport Code"','Mode of Transport','Commodity Code','Commodity','Qty Unit Code','Flag','value/unit',
            'CIF Trade Value (US$)','FOB Trade Value (US$)','Gross weight (kg)','Mode of Transport Code','','','']
df = df[df.columns.difference(to_remove)]

 

Filed Under: Pandas

条件を満たす値のみをデータフレームから取得

2018年9月3日 by 河副 太智 Leave a Comment

以下のようなcsvファイルがあった場合

1
2
3
4
Name  Salary  Age
0    John   50000   34
1   Sally  120000   45
2  Alyssa   80000   27

年齢が30以上の人のデータのみを取得した場合は

1
2
aaa = df["Age"] > 30
print(df[aaa])

以下のように年齢が30以上のデータが表示されます。

>>>
Name Salary Age
0 John 50000 34
1 Sally 120000 45

 

True か Falseですべての情報をチェックしたい場合は
以下のようなコードになります。

1
2
aaa = df["Age"] > 30
print([aaa])

>>>
[0 True
1 True
2 False
Name: Age, dtype: bool]

Filed Under: Pandas Tagged With: pandas, 指定, 条件

データフレームに値を追加

2018年2月10日 by 河副 太智 Leave a Comment

numpyで2次元配列に行要素を追加していき、
最後にdataframeに変換

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import pandas as pd
import numpy as np
 
data = [[1,2],[3,4]]
data = np.append(data,[6, 4])
        
 
arr = np.empty((0,2), int)
arr = np.append(arr, np.array([[1,2]]), axis=0)
arr = np.append(arr, np.array([[3,4]]), axis=0)
print(arr)
 
 
data_frame = pd.DataFrame(arr, columns=['resolt', 'value'])
 
data_frame

 

Filed Under: Numpy, Pandas Tagged With: df, データフレーム, 要素, 追加, 配列

  • Page 1
  • Page 2
  • Page 3
  • Interim pages omitted …
  • Page 8
  • Go to Next Page »

Primary Sidebar

カテゴリー

  • AWS
  • Bootstrap
  • Dash
  • Django
  • flask
  • GIT(sourcetree)
  • Plotly/Dash
  • VPS
  • その他tool
  • ブログ
  • プログラミング
    • Bokeh
    • css
    • HoloViews
    • Jupyter
    • Numpy
    • Pandas
    • PosgreSQL
    • Python 基本
    • python3
      • webアプリ
    • python3解説
    • scikit-learn
    • scipy
    • vps
    • Wordpress
    • グラフ
    • コマンド
    • スクレイピング
    • チートシート
    • データクレンジング
    • ブロックチェーン
    • 作成実績
    • 時系列分析
    • 機械学習
      • 分析手法
      • 教師有り
    • 異常値検知
    • 自然言語処理
  • 一太郎
  • 数学
    • sympy
      • 対数関数(log)
      • 累乗根(n乗根)
    • 暗号学

Copyright © 2025 · Genesis Sample on Genesis Framework · WordPress · Log in