• Skip to main content
  • Skip to primary sidebar

学習記録

プログラミング

DataFrame 内部結合 共通データのみ結合

2017年11月29日 by 河副 太智 Leave a Comment

 print(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
   amount      fruits  year
0       1       apple  2001
1       4      orange  2002
2       5      banana  2001
3       6  strawberry  2008
4       3   kiwifruit  2006
 
print(2)
       fruits  price  year
0       apple    150  2001
1      orange    120  2002
2      banana    100  2001
3  strawberry    250  2008
4       mango   3000  2007
 
 
<span role="presentation"><span class="cm-variable">df3</span> = <span class="cm-variable">pd</span>.<span class="cm-property">merge</span>(<span class="cm-variable">1</span>, <span class="cm-variable">2</span>, <span class="cm-variable">on</span>=<span class="cm-string">"fruits"</span>, <span class="cm-variable">how</span>=<span class="cm-string">"inner"</span>)
<span class="cm-builtin">print</span>(<span class="cm-variable">df3</span>)</span>
   amount      fruits  year_x  price  year_y
0       1       apple    2001    150    2001
1       4      orange    2002    120    2002
2       5      banana    2001    100    2001
3       6  strawberry    2008    250    2008

Filed Under: Pandas

DataFrameをcsvに変換

2017年11月29日 by 河副 太智 Leave a Comment

import pandas as pd

data = {'city': ['Nagano', 'Sydney', 'Salt Lake City', 'Athens', 'Torino', 'Beijing', 'Vancouver', 'London', 'Sochi', 'Rio de Janeiro'],
'year': [1998, 2000, 2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016],
'season': ['winter', 'summer', 'winter', 'summer', 'winter', 'summer', 'winter', 'summer', 'winter', 'summer']}

df = pd.DataFrame(data)

df.to_csv("csv.csv")
これを実行するとcsv.csvというCSVデータが同じディレクトリに作成

Filed Under: Pandas

csvを作成して追加

2017年11月29日 by 河副 太智 Leave a Comment

import csv

import time
from datetime import datetime

with open(‘csv0.csv’, ‘w’) as csvfile:
writer = csv.writer(csvfile, lineterminator=’\n’)
writer.writerow([‘2’, ‘3’, ‘5’])
writer.writerow([‘7′, ’11’, ’13’])

writer.writerow([datetime.now(),value])#関数、変数もカンマ区切りで入る

csv0.csvというファイルが同じディレクトリに生成される

Filed Under: Python 基本

CSVの読み込み

2017年11月29日 by 河副 太智 Leave a Comment

 

1
2
3
4
5
6
7
# データセット読込
from pandas import read_csv
dataframe = read_csv('aaa.csv',
usecols=[1],engine='python',  skipfooter=3)
 
dataset = dataframe.values
dataset = dataset.astype('float32')

usecols=[1]は読み込むcsvの列を指定[1]であれば2列目を縦に読み込む
engine=’python’ か’c’のどちらか
skipfooter=3はデータ末尾の3行はフッターとみなし読み込まない

1
2
#カラム(一行目にそれぞれの数値が何を表しているか)
dataset.columns=["sepal length", "sepal width", "petal length", "petal width", "class"]

 

わかりやすい一覧↓
Pandasのread_csvの全引数を解説

Filed Under: Pandas

Jupyterの文、単語を検索

2017年11月29日 by 河副 太智 Leave a Comment

find ./ -type f -print | xargs grep ‘キーワード’

Filed Under: Python 基本

辞書の文字数カウント collectionsモジュールのdefaultdict(型)

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

jisho = defaultdict(intかlist等の型)

 

 #これで変数jishoが辞書になる

例

from collections import defaultdict

jisho = defaultdict(int)
lst = [“a”, “b”, “c”, “b”, “a”, “e”]

for key in lst:
jisho[key] += 1

print (jisho)

 例:2
1
<span role="presentation"><span class="cm-keyword">from</span> <span class="cm-variable">collections</span> <span class="cm-keyword">import</span> <span class="cm-variable">defaultdict</span> </span>

1
<span role="presentation"><span class="cm-comment"># 文字列description</span></span>

1
<span role="presentation"><span class="cm-variable">description</span> = <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"AFL intends to import  PVC-insulated  and  XLPE-insulated  bunched copper wire from Honduras.  "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"The insulated wire will be used in the U.S. in the production of automotive wire harnesses. "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"Copper strands, classifiable under subheading 7413.00, Harmonized Tariff System of the United States  "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"(HTSUS); polyvinyl chloride (PVC) pellets, classifiable under subheading 3904.21, HTSUS; and "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"cross-link polyethylene (XLPE) pellets, classifiable under subheading 3901.30, HTSUS, all of U.S. origin will be exported to Honduras.  "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"The copper strands will be annealed in the U.S. and will be wound onto reels,  "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"containing between six and 14 untwisted and non-bunched copper strands. In Honduras,  "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"the strands are fed into a buncher, which arranges the strands in specific geometric orientations,  "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"with specific twist lengths ( lay lengths ) based on the specifications of the end use application "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"( lay plate ). The strands then enter the  compaction die  which sets the diameter and length of  "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"the specific products, resulting in the creation of  bunched wire.  By using strands of various "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"sizes and numbers of strands, different sizes of bunched wire are produced. It is "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"stated that the size of the bunched wire determines its electrical current carrying capacity  "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"and physical strength. The bunched wire will contain between seven and 104 strands, with American "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"Wire Gauge (AWG) measurements varying between 10 and 22. It is stated that the AWG level identifies the  "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"diameter, electrical current capacity and temperature  rating,  or maximum temperature levels within  "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"which the wire can function. From the compaction die, the bunched wire enters the  bow , which rotates "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"and twists the bunched wire, based on the needs of the given end use of the wire. "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"Next, the bunched wire is wound onto a reel and prepared for insulation with either PVC or XLPE.  "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"The compound is melted and pumped through the extruder line. In the extruder line, a  "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"rotating wheel pulls the copper wire, and the compound is extruded over the wire.  "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"If PVC is used, the material is cooled following a controlled cooling with water,  "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"which solidifies the compound around the bunched wire. It is stated that PVC-insulated  "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"wire has a lower temperature rating and is more likely to be used in applications  "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"where it is subject to less intense heat. If XLPE is used, a steam vulcanization process  "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"is required which causes a chemical reaction in the extruded XLPE, causing  "</span> <span class="cm-operator">+</span> <span class="cm-error">\</span></span>

1
<span role="presentation"><span class="cm-string">"crosslinking  in the underlying polymer chain. It is stated that  crosslinking"</span></span>

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

1
<span role="presentation"><span class="cm-comment"># defaultdictを定義</span></span>

1
<span role="presentation"><span class="cm-variable">moji</span> = <span class="cm-variable">defaultdict</span>(<span class="cm-builtin">int</span>)<span class="cm-comment">#これが辞書になる</span></span>

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

1
<span role="presentation"><span class="cm-comment"># 文字の出現回数を記録</span></span>

1
<span role="presentation"><span class="cm-keyword">for</span> <span class="cm-variable">key</span> <span class="cm-keyword">in</span> <span class="cm-variable">description</span>:</span>

1
<span role="presentation">    <span class="cm-variable">moji</span>[<span class="cm-variable">key</span>] += <span class="cm-number">1</span></span>

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

1
<span role="presentation"><span class="cm-comment"># ソートし、上位10要素を出力して下さい</span></span>

1
<span role="presentation"><span class="cm-builtin">print</span>(<span class="cm-builtin">sorted</span>(<span class="cm-variable">moji</span>.<span class="cm-property">items</span>(), <span class="cm-variable">key</span>=<span class="cm-keyword">lambda</span> <span class="cm-variable">x</span>: <span class="cm-variable">x</span>[<span class="cm-number">1</span>], <span class="cm-variable">reverse</span>=<span class="cm-keyword">True</span>)[:<span class="cm-number">10</span>])</span>

1
<span role="presentation">​</span>
 結果
1
[(' ', 428), ('e', 256), ('t', 170), ('i', 163), ('n', 153), ('s', 128), ('a', 124), ('r', 120), ('d', 100), ('o', 98)]

Filed Under: データクレンジング

  • « Go to Previous Page
  • Page 1
  • Interim pages omitted …
  • Page 34
  • Page 35
  • Page 36
  • Page 37
  • Page 38
  • Interim pages omitted …
  • Page 55
  • 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