LoginSignup
0
2

More than 1 year has passed since last update.

pythonさん

Last updated at Posted at 2021-04-27

勉強URL

お勧めサイト

train[train["week"]=="月"] あるデータだけ取り出せる
train[train["y"]>=150].head()

曜日が月曜日の時のyの平均値を見てみましょう
train[train["week"]=="月"]["y"].mean()

.trainのtemperatureが平均以上のデータだけを選択してみよう
train[train["temperature"]>=train["temperature"].mean()]

train[["y","week"]] trainで今度はyとweekの2つのカラムを選択したい

df[(df["week"]=="月") & (df["kcal"]<500)]

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline

年の新しいカラム名はyearとし、月の新しいカラム名はmonthとしましょう  2013-11-18

train["year"]=train["datetime"].apply(lambda x : x.split("-")[0])
train["month"]=train["datetime"].apply(lambda x : x.split("-")[1])

train["y"].plot() 折れ線グラフ

ax = train["y"].plot(figsize=(12,4),title="グラフ")
ax.set_xlabel("time")
ax.set_ylabel("y")

train["y"].plot.hist() ヒストグラム

train[["y","week"]].boxplot(by="week") 箱ひげ図

train.isnull().any() 欠損ち

train.isnull().sum()

train.fillna(0) 欠損地を0で保管

ある列に欠損値があった場合のみ、その行を削除したい場合はオプションとしてsubset=[ ]を使います
train.dropna(subset=["kcal"])

train[["y","temperature"]].corr() 相関関係

train.plot.scatter(x="temperature", y="y", figsize=(5,5)) 相関係数は散布図

def exercise_judge(ex):
if ex <= 3:
return "low"
elif 3<ex<6:
return"mid"
else:
return"high"

df.loc[:,"新カラム名"]=df.loc[:,"前のカラム名"].apply(exercise_judge)

値が「お楽しみメニュー」であれば1、そうでなければ0とする自作関数を作りましょう
自作の関数を作る為には、defを使います
def jisaku1(x):
if x =="お楽しみメニュー":
return 1
else:
return 0

jisaku1関数とapply関数を使って、trainとtestの新たなカラムfunを作りましょう
train["fun"] = train["remarks"].apply(lambda x:jisaku1(x))
test["fun"] = test["remarks"].apply(lambda x:jisaku1(x))

データの型を整数(int)に変換しましょう
データの型を変換したい場合は、astype関数を使います
オプションにはデータの型を入れます。今回は整数に変換したい為、np.intと書きます
変換したものは、もとのカラムに代入しなおしましょう
train["year"] = train["year"].astype(np.int)
train["month"] = train["month"].astype(np.int)

testX = test.copy()

ピンポイントで変える場合

df = df.rename(columns={"平均気温":"平均"}).head()

dura = pd.crosstab(duration_bining,train["y"],margins=True)
dura["rate"] = dura[1]/dura["All"]

train.iloc[ 開始行:終了行, 開始列:終了列 ]と書くことで、列・行を数字で指定して書くことができます
開始から終了まで全てを取り出したい場合は、「:」だけでよく、数字を省略することも可能です
trainX = train.iloc[ :, 0:17 ]

0
2
1

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
0
2