pandas

<Python, pandas, highcharts> pandas-highcharts

pandas-highcharts入れてみた。 >pip install pandas-highcharts You are using pip version 6.1.1, however version 7.0.3 is available. You should consider upgrading via the 'pip install --upgrade pip' command. Collecting pandas-highcharts Downl…

<Python, pandas, items> DataFrameの要素を1つづつ取り出す。

データフレームDataFrameの要素を1つづつ取り出す方法。 df.itemsか、df.iteritems (どっちも同じっぽい) まずはデータフレーム作成。 In [44]: import pandas as pd In [45]: import numpy as np in [52]: df = pd.DataFrame(np.random.randn(5,3)) In [5…

<Python, pandas> データフレーム(DataFrame)の1要素の値の抽出

データフレームDataFrame中の1要素の値Valueを取りだす方法。 DataFrameを条件で絞っていき、最後にその条件にマッチした要素から値をゲットする。 valuesの属性で値がarrayで出てくるので、[0]で選択する。 In [100]: df = pd.DataFrame(data=[(1,2,3),(1,…

<Python, pandas> DataFrameの表示。 Columnの1つあたりの表示文字数を増やす。

データフレームDataFrameで、カラムColumnの表示文字数を増やす方法。 .set_option('display.max_colwidth') In [53]: df.head() Out[53]: total_bill tip sex smoker day time size 0 16.99 1.01 Female No Sun Dinner 2 1 10.34 1.66 Male No Sun Dinner 3…

<Python, pandas> データフレーム(DataFrame)の表示。 折り返し幅を広げる。

データフレームDataFrameを中身をprintで表示する際に横幅(折り返し幅)を折り返されないように広げる方法。 pd.set_option('line_width', 100) print df ここで教えてもらいました。 Python - pandasでよく使う文法まとめ - Qiitaqiita.com

<Python, pandas> データフレーム(DataFrame)をcsvで出力

データフレームDataFrameをcsvフォーマットで出力する。 fn = 'hoge' df.to_csv(hoge + '.csv') マニュアル。 IO Tools (Text, CSV, HDF5, ...) — pandas 0.16.0 documentation API referenceは、 pandas.DataFrame.to_csv — pandas 0.16.0 documentation

<Python, pandas> カラムがあるか? と カラムの追加の仕方。

カラムcolumnsにその名前があるか?の判定方法と、 空のカラムcolumnsの追加の仕方。 In [81]: a = pd.DataFrame(data=[[1,2,3],[4,5,6]], columns=['a','b','c']) In [82]: a Out[82]: a b c 0 1 2 3 1 4 5 6 In [83]: if not 'd' in a.columns: print('Y')…

<Python, pandas> pivot_tableの使い方とプロット。

過去メモもあるが改めてメモメモ。 In [74]: import pandas as pd In [75]: import numpy as np In [76]: df = pd.DataFrame({'A' : ['one', 'one', 'two', 'three'] * 6, ...: 'B' : ['A', 'B', 'C'] * 8, ...: 'C' : ['foo', 'foo', 'foo', 'bar', 'bar', …

<Python, pdandas> データフレームの上の文字列を切り取る

データフレーム上の文字列値を切り取る。 In [31]: df = pd.DataFrame([['abcdeft','hijklmn'],['0123456','7890ast']]) In [32]: df Out[32]: 0 1 0 abcdeft hijklmn 1 0123456 7890ast In [33]: df[0].str[0] Out[33]: 0 a 1 0 Name: 0, dtype: object In …

<Python, pandas> コマンドラインの出力を取り込み、データフレーム(DataFrame)へ

コマンドライン command lineの出力をpipeして、データフレームDataFrameへ取り込む方法。 いろいろ調べた結果下記で実行可能っぽい。 In [1]: %more aaa.csv a,b,c, 1,2,3, 4,5,6, In [2]: import subprocess In [3]: import pandas as pd In [4]: import i…

<Python, pandas> データフレーム(DataFrame)上の文字列操作

データフレーム DataFrame の中の文字列の操作。 Working with Text Data — pandas 0.15.2 documentation API Reference — pandas 0.16.0 documentation いろいろできるらしい。

Python, pandas, melt

melt 2つ以上の値を格納している列columnを合体して、 1つの値valuesを格納している列columnを作成し、さらに、それに対応した認識子identifier variable(すなわち、もとの列名headerを)格納した列columnを作る。 例。 In [1]: import pandas as pd In [2]…

<Python, pandas> 行、列の置き換え

行指定、列指定でのデータの置き換え方法。 行 row、 indexの場合 locで指定。 列 col、columnsの場合 そのまま。 In [176]: df = pd.DataFrame(data=[[1,2,3],[4,5,6],[7,8,9]]) In [177]: df Out[177]: 0 1 2 0 1 2 3 1 4 5 6 2 7 8 9 In [178]: df0 = pd.…

<Python, pandas> 空のデータフレームをつくる方法。

ちょっと空のDataFrameが必要となり作り方をチェックチェック。 In [151]: df = pd.DataFrame(index=[1,2], columns=[1,2]) In [153]: df Out[153]: 1 2 1 NaN NaN 2 NaN NaN In [154]: df.fillna('aaa') Out[154]: 1 2 1 aaa aaa 2 aaa aaa じゃじゃーん。 …

Python, pandas, seaborn, jointplot ためし。

seabornのjointplot In [36]: import pandas as pd In [37]: import numpy as np In [38]: import seaborn as sns In [45]: df = pd.DataFrame(np.random.randn(100,5)) In [47]: df.head() Out[47]: 0 1 2 3 4 0 0.383344 0.576856 -0.168663 -0.917400 1.1…

Python, seaborn, 信頼区間(confidence interval)を求めて

Pythonのプロットで、なんとかggplotライク のgeom_smoothで描画される、信頼区間 Confidence intervalを探しもとめた結果、次のライブラリがいい感じ。 Seaborn: statistical data visualization — seaborn 0.5.1 documentation pandasのページにもseaborn…

Python, pandas, dtypes, count(), mean(), head()の違い

データフレームのmethodで帰ってくる値が、型がちょい違うので、メモメモ。 In [166]: df = pd.DataFrame(np.random.randn(10,10)) In [167]: a = df.dtypes In [168]: b = df.count() In [169]: c = df.mean() In [170]: d = df.head(1) In [171]: whos Var…

Python, pandas, matplotlib, matplotlibでのベーシックなplot

DataFrameと組み合わせた場合の、いいプロット方法を発見したが、念のため、 matplotlibでのやり方をメモメモしておこう。 In [1]: import pandas as pd In [2]: import numpy as np In [3]: import matplotlib.pyplot as plt In [4]: df = pd.DataFrame(np.…

<Python, pandas> リストをデータフレームにセットするには?

リストをデータフレームにセットする方法。 In [107]: a Out[107]: [1, 2, 3] In [108]: b Out[108]: [2, 3, 4] In [109]: df = pd.DataFrame({0 : a, 1: b}) In [110]: df Out[110]: 0 1 0 1 2 1 2 3 2 3 4 In [111]: df = pd.DataFrame({'a' : a, 'b' : b}…

Python, pandas, ggplot-likeをオンにする時、はまったのでメモ

ggplotのような感じのグラフを作る。   In [1]: import pandas as pd In [2]: import matplotlib.pyplot as plt In [3]: df = pd.DataFrame([(1,2),(3,4)]) In [4]: df.plot() Out[4]: <matplotlib.axes._subplots.AxesSubplot at 0x2b50f7a91048> In [5]: plt.show() ということで、plt.show()が効く。 で、、ggplot</matplotlib.axes._subplots.axessubplot>…

<Python, pandas> データフレームの行列選択方法。 少しわかったこと。

データフレームの行列アクセスで少しわかったこと。 まずはデータフレーム。 index/columnsにの設定が何もない、数字の時。 In [3]: df = pd.DataFrame([(1,2),(3,4)]) In [4]: df Out[4]: 0 1 0 1 2 1 3 4 In [5]: df.ix[0,1] Out[5]: 2 In [6]: df.iloc[0,…

Python, pandas, データフレームとシリーズの文字列のスライス

データフレームでの文字列のスライス。 どうも、シリーズでないとスライスできないらしい。 In [108]: df Out[108]: 0 1 0 10% 5% 1 75% 102% In [109]: df[0].str.slice(0,-1) Out[109]: 0 10 1 75 Name: 0, dtype: object In [110]: df[1].str.slice(0,-1)…

Python, matplotlib, numpy, ヒストグラム作成ではまったこと、と、データフレームで四捨五入する方法

最初に四捨五入方法。 In [18]: import pandas as pd In [19]: import numpy as np In [20]: df = pd.DataFrame(np.random.randn(5,5)) In [21]: df Out[21]: 0 1 2 3 4 0 0.526538 0.935121 -0.380063 1.887691 -1.099712 1 -0.694135 -0.701937 -0.185376 …

Python, pandas, データフレームの操作(メソッド)、count, describe

データフレーム作成。 In [41]: df = pd.DataFrame(np.random.randn(10,5)) In [42]: df Out[42]: 0 1 2 3 4 0 0.077817 -0.759317 -0.229288 1.330958 0.083665 1 0.356439 -0.977023 1.361504 -0.573934 0.391663 2 0.441791 -1.374674 2.725946 0.301030 …

<Python, pandas> データフレームの結合

まずはデータフレーム。 In [31]: df1 = pd.DataFrame([(1,2),(3,4)]) In [32]: df1 Out[32]: 0 1 0 1 2 1 3 4 In [33]: df2 = pd.DataFrame([(5,6),(7,8)]) In [34]: df2 Out[34]: 0 1 0 5 6 1 7 8 縦に合体。 In [35]: df1.append(df2) Out[35]: 0 1 0 1 2…

<Python, pandas> データフレームから欠損値(np.nan)を除く方法

まずはデータフレームDataFrame。 In [16]: df = pd.DataFrame([(1,2,3),(4,np.nan,7),(np.nan,9,10)]) In [17]: df Out[17]: 0 1 2 0 1 2 3 1 4 NaN 7 2 NaN 9 10 欠損値np.nanを全部除く。 欠損値が1つでもある行Rowを除く。 In [18]: df.dropna() Out[18…

Python, pandas, Seriesでキーと値がある場合

ちょっとはまったのでメモメモ。 pandasのSeriesでキーと値がある場合がある。 print(s) aaa 0 bbb 1 ccc 2 キー(Aliasと書いてありますが、)と要素の値にアクセスするには、 print(s.keys()) print(s.values) ディクショナリとはちがうらしい。 valuesはA…

<Python, pandas> hist時のエラー

ヒストグラムを作って時にエラー対処を1つ。 エラーメッセージ。 AttributeError: max must be larger than min in range parameter. やったこと。dropna()を追加。 ax.hist(df.dropna(), bins=100) Nanがあると、min/maxを自動で計算してくれないらしい。…

Python, pandas, データフレーム データタイプの取得

In [1]: df.dtypes Out[1]: a object b int64 c float64 dtype: object In [2]: df.a.dtypes Out[2]: dtype('object') In [3]: print(df.a.dtypes) object

<Python, pandas> データフレーム 最大値を含む行取得+最大値取得

In [1]: a = df.max(columns='hoge') Out[1]: print(a) a 1 b 1 c 1 hoge 30 dtype: object で、 In [2]: print(a['hoge']) Out[2]: 30 と、思ったのだが、よく考えれば、、、 In [3]: max(df['hoge']) Out[3]: 30 でよかった。。。 タイトル変更しとこう。