Created
December 5, 2022 11:46
-
-
Save hamaguchi-amago/fdbfdf4543deba8b32c7e369c8326401 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 | |
# 都道府県名をindexとして読み込む | |
# [m]単位の人口を追加する | |
df = pd.read_csv("data.csv", index_col=0) | |
series_pop_million = df["population"].map(lambda x: x / 100) | |
series_pop_million.name = "population[m]" | |
df = pd.concat([df, series_pop_million], axis=1) | |
# 面積 | |
print(df.loc[:, "area"]) | |
print() | |
# 北海道と青森の面積 | |
print(df.loc[["北海道", "青森"], "area"]) | |
print() | |
# 北海道と青森 の 面積と人口 | |
print(df.loc[["北海道", "青森"], ["area", "population"]]) | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ブログで作成しました。
【Python】pandasのデータフレームに loc 経由でアクセスする方法
https://neko-py.com/pandas-loc