"nativecountry"列の "United-States"という値を持つ行のデータフレームをフィルタリングしたいとします。これは簡単なことのようですが、私が試したことは失敗しました。ここではデータフレームを作成するための私のコードだ:列の特定の値を持つ列のPandasデータフレーム
import pandas as pd
url = 'https://archive.ics.uci.edu/ml/machine-learning-
databases/adult/adult.data'
col_names = ['age', 'workclass', 'fnlwgt', 'education', 'educationnum',
'maritalstatus', 'occupation', 'relationship',
'race', 'sex', 'capitalgain', 'capitalloss',
'hoursperweek', 'nativecountry', 'income']
df_adult = pd.read_csv(url, header = None, names = col_names)
私は「米国、国のための「nativecountry」をフィルタリングするため、以下の事を試してみた:
#This returns an empty dataframe
df_US = df_adult[df_adult["nativecountry"] == 'United-States']
#Code from this source: https://chrisalbon.com/python/pandas_index_select_and_filter.html
#This returns the error: name 'United' is not defined
df_US = df_adult.query("nativecountry == United-States")
#Code from this source: https://pythonspot.com/en/pandas-filter/
#And this doesn't work either, for some reason
df_adult.useSQLInstead(SELECT * FROM df_adult WHERE nativecountry=United-States)
...just kidding.
任意の考えを?ありがとう。そのためnativecountry
の値の
米国 - 米国のスペースインフロントがあります。この小さなトリックをすることでこれを見ることができます。 'df_adult.head()。to_dict()' –