LoginSignup
0
0

pandasによるDataFrame型の値取り出しについて

Last updated at Posted at 2023-10-15

DataFrame型の値取り出しについて

DataFrame型の値を取り出して利用する際に、型でエラーになることが多かったのでノウハウを残す。

エラー理由は下記の条件分だけでは該当数列すべてが抽出され(今回のケースでは1行しかないが)、class 'pandas.core.series.Series' 型なっていたことである。
そのため、.valuesを用いることで class 'str' の値を取り出すことができた。

import pandas as pd

master_df = pd.read_csv("customer_master.csv")

#値のみが取得できていない
target_data = master_df[master_df ["customer_id"] == "IK152942"]["customer_name"]
print(type(target_data))
print(target_data)

#値が取得できている
print(type(target_data.values[0]))
print(target_data.values[0])

下記を参照
https://www.delftstack.com/ja/howto/python-pandas/how-to-get-a-value-from-a-cell-of-a-dataframe/

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