Skip to content

Instantly share code, notes, and snippets.

@gyosit
Last active October 2, 2020 08:45
Show Gist options
  • Save gyosit/b10ddf7b9a75256b031bd2114639c31f to your computer and use it in GitHub Desktop.
Save gyosit/b10ddf7b9a75256b031bd2114639c31f to your computer and use it in GitHub Desktop.
import pandas as pd
import numpy as np
## リスト
print("\n===LIST===\n")
# リストの作成
list_a = [1,3,5]
print(list_a)
# 連続したリストの作成
list_b = range(10)
print(list_b)
# リストの一つ取り出し
part_a = list_a[0]
print(part_a)
# リストの一部取り出し
part_b = list_b[0:5]
print(part_b)
# リストの追加
list_a.append(100)
print(list_a)
# Numpyリストの作成
list_c = np.array([2,4,6])
print(list_c)
# 連続したNumpyリストの作成
list_d = np.arange(10)
print(list_d)
# Numpyリストの一つ取り出し
part_c = list_c[0]
print(part_c)
# Numpyリストの一部取り出し
part_d = list_d[0:5]
print(part_d)
# Numpyリストの追加
list_c = np.append(list_c, 100)
print(list_c)
# リスト内表記
list_e = np.arange(10)
list_2e = [part_e*2 for part_e in list_e]
print(list_e)
print(list_2e)
## pandas
print("\n===PANDAS===\n")
# csvファイルの読み込み
df = pd.read_csv("weather.csv", sep=',')
print(df)
# 列の取り出し
df_col = df["MinTemp"]
print(df_col)
# 行の取り出し
df_row = df.iloc[0]
print(df_row)
# 指定行列の取り出し
df_matrix = df.iloc[[1, 2], [11, 12]]
print(df_matrix)
# もしくは、
df_matrix = df.iloc[1:3, 11:13]
print(df_matrix)
import pandas as pd
import numpy as np
# weather.csvの指定10行を取り出し、最高気温と最低気温の差が10度以上の行番号のリストを作る
ind = [0,2,4,6,7,8,9,12,14,20] # 指定10行
df = pd.read_csv("weather.csv", sep=',')
df_min = df["MinTemp"][ind] # 最低気温
df_max = df["MaxTemp"][ind] # 最高気温
df_delta = df_max - df_min # 気温差
res = [] # 結果のリスト
for i,v in enumerate(df_delta):
if(v >= 10): # v(気温差)が10度以上
the_ind = ind[i] # 条件に一致した行番号
print(the_ind, v)
res.append(the_ind) # 行番号をresに追加
print(res)
# Q1. weather.csvの"先頭"10行を取り出し、降水量が0の日のデータのリストを作る
ind = np.arange(xxx) # 先頭10行
df = pd.read_csv("weather.csv", sep=',')
df_rain = df["xxx"][xxx] # 降水量
res = [] # 結果のリスト
for i,v in xxx:
if(xxx): # v(降水量)が0
res.append(df.iloc[xxx]) # 行をresに追加
print(res)
# Q2. weather.csvの0から20未満の偶数行を取り出し、Sunshineが5以上の日のMinTempとMaxTempのリストを作る
@gyosit
Copy link
Author

gyosit commented Oct 2, 2020

Q1の出力結果==
[MinTemp 8
MaxTemp 24.3
Rainfall 0
Evaporation 3.4
Sunshine 6.3
WindGustDir NW
WindGustSpeed 30
WindDir9am SW
WindDir3pm NW
WindSpeed9am 6
WindSpeed3pm 20
Humidity9am 68
Humidity3pm 29
Pressure9am 1019.7
Pressure3pm 1015
Cloud9am 7
Cloud3pm 7
Temp9am 14.4
Temp3pm 23.6
RainToday No
RISK_MM 3.6
RainTomorrow Yes
Name: 0, dtype: object, MinTemp 6.2
MaxTemp 16.9
Rainfall 0
Evaporation 5.8
Sunshine 8.2
WindGustDir SE
WindGustSpeed 44
WindDir9am SE
WindDir3pm E
WindSpeed9am 20
WindSpeed3pm 24
Humidity9am 70
Humidity3pm 57
Pressure9am 1023.8
Pressure3pm 1021.7
Cloud9am 7
Cloud3pm 5
Temp9am 10.9
Temp3pm 14.8
RainToday No
RISK_MM 0.2
RainTomorrow No
Name: 5, dtype: object, MinTemp 8.3
MaxTemp 17
Rainfall 0
Evaporation 5.6
Sunshine 4.6
WindGustDir E
WindGustSpeed 41
WindDir9am SE
WindDir3pm E
WindSpeed9am 11
WindSpeed3pm 24
Humidity9am 65
Humidity3pm 57
Pressure9am 1026.2
Pressure3pm 1024.2
Cloud9am 6
Cloud3pm 7
Temp9am 12.1
Temp3pm 15.5
RainToday No
RISK_MM 0
RainTomorrow No
Name: 7, dtype: object, MinTemp 8.8
MaxTemp 19.5
Rainfall 0
Evaporation 4
Sunshine 4.1
WindGustDir S
WindGustSpeed 48
WindDir9am E
WindDir3pm ENE
WindSpeed9am 19
WindSpeed3pm 17
Humidity9am 70
Humidity3pm 48
Pressure9am 1026.1
Pressure3pm 1022.7
Cloud9am 7
Cloud3pm 7
Temp9am 14.1
Temp3pm 18.9
RainToday No
RISK_MM 16.2
RainTomorrow Yes
Name: 8, dtype: object]

@gyosit
Copy link
Author

gyosit commented Oct 2, 2020

Q2の出力結果
[MinTemp 8
MaxTemp 24.3
Name: 0, dtype: object, MinTemp 7.6
MaxTemp 16.1
Name: 4, dtype: object, MinTemp 6.1
MaxTemp 18.2
Name: 6, dtype: object, MinTemp 9.1
MaxTemp 25.2
Name: 10, dtype: object, MinTemp 10.1
MaxTemp 27.9
Name: 12, dtype: object, MinTemp 10.1
MaxTemp 31.2
Name: 14, dtype: object, MinTemp 13.8
MaxTemp 31.2
Name: 16, dtype: object, MinTemp 12.4
MaxTemp 32.3
Name: 18, dtype: object]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment