Last active
May 24, 2018 02:51
-
-
Save seven0525/f8eeef8ede55d8b00f2a19dcadae8292 to your computer and use it in GitHub Desktop.
「自作Python100本ノック」10日目(63本〜70本目) ref: https://qiita.com/ahpjop/items/f92fe9fcd8f39cd44952
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
from datetime import date | |
now = date.today() | |
now_str = now.isoformat() #文字列の形にする | |
with open("today.txt", "wt") as outfile: | |
outfile.write(now_str) | |
now_str |
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 time | |
fmt = '%Y-%m-%d' | |
time.strptime(today_string, fmt) |
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 os | |
os.listdir('.') | |
# 親ディレクトリのファイルのリストを作ろう | |
os.listdir('..') |
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
from datetime import date | |
my_time = date(1998, 5, 11) | |
my_time.weekday() #月曜が0、日曜が6 |
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
from datetime import timedelta | |
my_future = my_time + timedelta(days=10000) | |
my_future |
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
df = pd.DataFrame( | |
{'名前': ['朝倉', '鈴木', '山中', '田中', '山本'], | |
'年齢': [17, 43, 40, 12, 62], | |
'性別':['男', '男', '女', '男', '女']}) | |
df |
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
df_1 = df[df['年齢'] < 35] | |
df_1 |
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
# 行の追加 | |
row = pd.DataFrame({'名前': ['池田'], | |
'年齢': [1989], | |
'性別': '男'}) | |
# 行の追加(行: axis=0, 列: axis=1) | |
df_2 = pd.concat([df,row], axis=0) | |
df_2 | |
# indexを変更 | |
# np.agrangeで0〜6の数字が並んだ配列を生成 | |
df_2.index = np.arange(len(df_2)) | |
df_2 | |
# 列の追加 | |
# 新たな列を代入 | |
df_2['居住地'] = ['東京', '大阪', '北海道', '宮城', '富山', '大分'] | |
df_2 |
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
# 列を削除(行: axis=0, 列: axis=1) | |
df_3 = df_2.drop('性別', axis=1) | |
df_3 |
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
df_4.columns = ['name', 'age', 'residence'] | |
df_4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment