0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonで「年」と「月」の情報を抽出する。

Posted at

やりたいこと

2021-04-01のような日付の表記から「年」や「月」の情報を抽出する

環境

Google Colaboratoryを使用しました。

使用した関数

・pandas.Series.apply()
 →Pandasの各要素に関数を適用する

・str.split()
 →Pandasの文字列を区切り文字で分割

## コード

###Step1:DataFrameの作成

#日付型のカラムの作成
import pandas as pd 

list1=["2021-04-01","2021-04-01","2021-04-01"]
columns1=["日付"]
df=pd.DataFrame(data=list1,columns=columns1)
df

###Step2:-(ハイフン)で日付を分割し、「年」と「月」のカラムに代入する。

df['']= df['日付'].apply(lambda x: x.split('-')[0])
df['']= df['日付'].apply(lambda x: x.split('-')[1])

df.head()
0
0
0

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?