Last active
October 2, 2020 08:45
-
-
Save gyosit/b10ddf7b9a75256b031bd2114639c31f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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のリストを作る |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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]