• Skip to main content
  • Skip to primary sidebar

学習記録

split()でデータフレームを任意の文字列で分割

2017年12月5日 by 河副 太智 Leave a Comment

データフレームのカラムのcol1にa,bを交互に7個づつ振る

groupbyを実行する
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import pandas as pd
import datetime
 
 
df = pd.DataFrame({'dt1': [datetime.datetime(2014, 10, 1),
                           datetime.datetime(2014, 10, 2),
                           datetime.datetime(2014, 10, 3),
                           datetime.datetime(2014, 10, 4),
                           datetime.datetime(2014, 10, 5),
                           datetime.datetime(2014, 10, 6),
                           datetime.datetime(2014, 10, 7),
                           datetime.datetime(2014, 11, 1),
                           datetime.datetime(2014, 11, 2),
                           datetime.datetime(2014, 11, 3),
                           datetime.datetime(2014, 11, 4),
                           datetime.datetime(2014, 11, 5),
                           datetime.datetime(2014, 11, 6),
                           datetime.datetime(2014, 11, 7)],
                   'col1': 'a b'.split() * 7})
print(df)
 
x = df.groupby('col1').groups
 
print(x)
 
print (len((x["b"])))

 

結果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
   col1        dt1
0     a 2014-10-01
1     b 2014-10-02
2     a 2014-10-03
3     b 2014-10-04
4     a 2014-10-05
5     b 2014-10-06
6     a 2014-10-07
7     b 2014-11-01
8     a 2014-11-02
9     b 2014-11-03
10    a 2014-11-04
11    b 2014-11-05
12    a 2014-11-06
13    b 2014-11-07
{'b': Int64Index([1, 3, 5, 7, 9, 11, 13], dtype='int64'), 'a': Int64Index([0, 2, 4, 6, 8, 10, 12], dtype='int64')}
7

x = df.groupby(‘col1’).groups

でcol1をベースとしたグループ分けされたリストを渡し、

 

print (len(x[“b”])
bにグループ分けされたものの個数だけ表示

Filed Under: Pandas

groupby()でベースを決めてグループ分け

2017年12月5日 by 河副 太智 Leave a Comment

データフレームのカラムのcol1にa,bを交互に振っていき
それをaとbのグループ分けにgroupby()を使用する。

groupbyを実行する
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import pandas as pd
import datetime
 
 
df = pd.DataFrame({'dt1': [datetime.datetime(2014, 10, 1),
                           datetime.datetime(2014, 10, 2),
                           datetime.datetime(2014, 10, 3),
                           datetime.datetime(2014, 10, 4),
                           datetime.datetime(2014, 10, 5),
                           datetime.datetime(2014, 10, 6),
                           datetime.datetime(2014, 10, 7),
                           datetime.datetime(2014, 11, 1),
                           datetime.datetime(2014, 11, 2),
                           datetime.datetime(2014, 11, 3),
                           datetime.datetime(2014, 11, 4),
                           datetime.datetime(2014, 11, 5),
                           datetime.datetime(2014, 11, 6),
                           datetime.datetime(2014, 11, 7)],
                   'col1': 'a b'.split() * 7})
print(df)
 
x = df.groupby('col1').groups
 
print(x)
 
print (len((x["b"])))

 

結果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
   col1        dt1
0     a 2014-10-01
1     b 2014-10-02
2     a 2014-10-03
3     b 2014-10-04
4     a 2014-10-05
5     b 2014-10-06
6     a 2014-10-07
7     b 2014-11-01
8     a 2014-11-02
9     b 2014-11-03
10    a 2014-11-04
11    b 2014-11-05
12    a 2014-11-06
13    b 2014-11-07
{'b': Int64Index([1, 3, 5, 7, 9, 11, 13], dtype='int64'), 'a': Int64Index([0, 2, 4, 6, 8, 10, 12], dtype='int64')}
7

x = df.groupby(‘col1’).groups

でcol1をベースとしたグループ分けされたリストを渡し、

 

print (len(x[“b”])
bにグループ分けされたものの個数だけ表示

Filed Under: Pandas

Seriesできること

2017年12月3日 by 河副 太智 Leave a Comment

1
import pandas as pd

​
■通常の辞書

1
2
3
<span class="cm-variable">fruits</span> = {<span class="cm-string">"banana"</span>: <span class="cm-number">3</span>, <span class="cm-string">"orange"</span>: <span class="cm-number">2</span>}
<span class="cm-builtin">print</span>(<span class="cm-variable">pd</span>.<span class="cm-property">Series</span>(<span class="cm-variable">fruits</span>))
 

>>>
banana 3
orange 2
dtype: int64

■辞書どうしの結合

1
2
3
4
<span role="presentation">index = ["cup", "chair", "table", "kagaku", "pictre"]
data = [7012, 9406, 9609, 3824,9701]
 
</span>

1
<span role="presentation"><span class="cm-variable">series</span> = <span class="cm-variable">pd</span>.<span class="cm-property">Series</span>(<span class="cm-variable">data</span>, <span class="cm-variable">index</span>=<span class="cm-variable">index</span>)</span>

1
<span role="presentation">​</span>print(<span class="cm-variable">series</span>)&gt;&gt;&gt;

1
2
3
4
5
6
cup       7012
chair     9406
table     9609
kagaku    3824
pictre    9701
dtype: int64

■条件抽出 フィルタリング

1
<span role="presentation"><span class="cm-comment"># series内の要素のうち、値が5以上10未満の要素を含むSeriesを作り、seriesに再代入</span></span>

1
<span role="presentation"><span class="cm-variable">series</span> = <span class="cm-variable">series</span>[<span class="cm-variable">series</span> <span class="cm-operator">&gt;</span>= <span class="cm-number">5</span>][<span class="cm-variable">series</span> <span class="cm-operator">&lt;</span> <span class="cm-number">10</span>]</span>

1
<span role="presentation">​</span><span role="presentation"><span class="cm-builtin">print</span><span class=" CodeMirror-matchingbracket">(</span><span class="cm-variable">series</span><span class=" CodeMirror-matchingbracket">)</span></span>

■インデックス参照,key,キーから取得

1
2
3
4
5
<span role="presentation"><span class="cm-comment"># seriesの2から4までの3つをitems1に代入
 
index = ["apple", "orange", "banana", "strawberry", "kiwifruit"]
data = [10, 5, 8, 12, 3]
series = pd.Series(data, index=index)</span></span>

items1 = series[1:4] 結果 orange 5 banana 8 strawberry 12 dtype: int64

■値,value,キーワードから取得

index = [“apple”, “orange”, “banana”, “strawberry”, “kiwifruit”]
data = [10, 5, 8, 12, 3]
series = pd.Series(data, index=index)

1
<span role="presentation"><span class="cm-variable">items2</span>=<span class="cm-variable">series</span>[[<span class="cm-string">"apple"</span>,<span class="cm-string">"banana"</span>,<span class="cm-string">"kiwifruit"</span>]]</span>

1
<span role="presentation">​</span>

1
結果

apple 10
banana 8
kiwifruit 3
dtype: int64

■ソート

index = [“a”, “b”, “c”, “d”, “e”]
data = [10, 5, 8, 12, 3]
series = pd.Series(data, index=index)

# seriesをインデックスに基づいてアルファベット順にソートしたSeriesをitems1に代入
items1 = series.sort_index()

# seriesをデータについて値の大きさを昇順にソートしたSeriesをitems2に代入
items2 = series.sort_values()

Filed Under: Pandas

ブロードキャスト

2017年12月3日 by 河副 太智 Leave a Comment

1
<span role="presentation"><span class="cm-keyword">import</span> <span class="cm-variable">numpy</span> <span class="cm-keyword">as</span> <span class="cm-variable">np</span></span>

1
 

1
<span role="presentation"><span class="cm-comment"># Numpy配列xを生成</span></span>

1
<span role="presentation"><span class="cm-variable">x</span> = <span class="cm-variable">np</span>.<span class="cm-property">arange</span>(<span class="cm-number">20</span>)</span>

1
<span role="presentation"><span class="cm-variable">x</span> = <span class="cm-variable">x</span>.<span class="cm-property">reshape</span>(<span class="cm-number">4</span>, <span class="cm-number">5</span>)</span>

1
2
<span role="presentation">​
&gt;&gt;&gt;</span>

1
 

1
2
3
4
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]

1
2
3
4
<span role="presentation">
 
 
</span>

1
<span role="presentation"><span class="cm-comment"># xの全ての要素に10を足す</span></span>

1
2
3
4
5
6
7
8
9
10
11
12
<span role="presentation"><span class="cm-variable">x</span> = <span class="cm-variable">x</span> <span class="cm-operator">+</span> <span class="cm-number">10
 
&gt;&gt;&gt;
[[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]]
 
 
 
 
</span></span>

1
 

1
<span role="presentation">​</span>

1
<span role="presentation"><span class="cm-comment"># 0から4の整数値をもつ1×4のNumpy配列yを生成</span></span>

1
<span role="presentation"><span class="cm-variable">y</span> = <span class="cm-variable">np</span>.<span class="cm-property">array</span>([<span class="cm-variable">np</span>.<span class="cm-property">arange</span>(<span class="cm-number">5</span>)])</span>

1
<span role="presentation">​&gt;&gt;&gt;[[0 1 2 3 4]]</span>

1
 

1
<span role="presentation">​</span>

1
<span role="presentation"><span class="cm-comment"># xのn番目の列のすべての行からnだけ引く</span></span>

1
<span role="presentation"><span class="cm-variable">x</span> = <span class="cm-variable">x</span> <span class="cm-operator">-</span> <span class="cm-variable">y</span></span>
1
2
3
4
[[10 10 10 10 10]
[15 15 15 15 15]
[20 20 20 20 20]
[25 25 25 25 25]]

Filed Under: Numpy

ブールインデックス

2017年12月3日 by 河副 太智 Leave a Comment

import numpy as np

arr = np.array([2, 3, 4, 5, 6, 7])
print(arr % 2 == 0)>>>[ True False True False True False]

print(arr[arr%2==0])>>>[2 4 6]

print(arr[arr%2 == False])>>>[2 4 6]

Filed Under: Pandas

slice [x:y] スライス 破壊的操作を防ぐcopy()

2017年12月3日 by 河副 太智 Leave a Comment

import numpy as np

arr = np.arange(10)
print (arr)>>>[0 1 2 3 4 5 6 7 8 9]

print (arr[3])>>>3
#リストはゼロから開始しているので指定時にマイナス1は不要

print(arr[5:8])>>>[5 6 7]

arr[5:8] = 99
print(arr)>>>[ 0 1 2 3 4 99 99 99 8 9]

slice = arr[5:8]
slice[1] = 55555 #0から数えるのでほしい数マイナス1の数を入れる
print (arr)>>>[ 0 1 2 3 4 99 55555 99 8 9]

slice[:] = 11111
print (arr)>>>[ 0 1 2 3 4 11111 11111 11111 8 9]

 

破壊的操作となるため、もとのデータをそのままにしたい場合は
arr[5:8].copy()
とする

1
例 copy()を使用

import numpy as np

arr = np.arange(10)
print (arr)

print (arr[0])#リストはゼロから開始しているので指定時にマイナス1は不要

print(arr[5:8].copy())
print(arr)

abc=arr[5:8].copy()
print(abc)

slice = arr.copy()
slice[:] = 11111
print (slice)

print(arr)

Filed Under: Numpy Tagged With: copy()

  • « Go to Previous Page
  • Page 1
  • Interim pages omitted …
  • Page 41
  • Page 42
  • Page 43
  • Page 44
  • Page 45
  • Interim pages omitted …
  • Page 66
  • 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