【发布时间】:2023-02-03 22:40:17
【问题描述】:
filtered = Series([True, False, True], index=df.index)
condition_loc = df.loc[df. LoanAmount.head() < 500]
boolean_i = df.iloc[[True , False , True ]]
boolean = df.loc[['True' , 'False' , 'True' ]].values
产生错误
IndexError: Boolean index has wrong length: 3 instead of 614
KeyError: "None of [Index(['True', 'False', 'True'], dtype='object', name='Loan_ID')] are in the [index]"
IndexingError(
pandas.core.indexing.IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).
raise ValueError(
ValueError: Length of values (3) does not match length of index (614)
数据快照
Loan_ID Gender Married Dependents Education Self_Employed ApplicantIncome CoapplicantIncome LoanAmount Loan_Amount_Term Credit_History Property_Area Loan_Status
0 LP001002 Male No 0 Graduate No 5849 0 100 360 1 Urban Y
1 LP001003 Male Yes 1 Graduate No 4583 1508 128 360 1 Rural N
2 LP001005 Male Yes 0 Graduate Yes 3000 0 66 360 1 Urban Y
3 LP001006 Male Yes 0 Not Graduate No 2583 2358 120 360 1 Urban Y
数据为 [614 行 x 12 列] 目的是生成给定的布尔值列表,选择值为 true 的行 已尝试由上述任何错误生成的每个可用链接。 似乎没有人未能使用上述语法生成值。 请将我指向可以解决此问题的链接。 已尝试尽可能多地解释。 大熊猫的新手。 谢谢你的时间!
编辑:
filtered = Series([True, False, True] )
删除索引解决了第一个问题。
编辑 2:
df.loc[Series([True, False, True])]
给
raise IndexingError(
pandas.core.indexing.IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).
建议的链接仅讨论系列,而不讨论如何将其与 loc 或 iloc 结合使用。
编辑 3:
import pandas as pd
mydict = [
{"a": 1, "b": 2, "c": 3, "d": 4},
{"a": 100, "b": 200, "c": 300, "d": 400},
{"a": 1000, "b": 2000, "c": 3000, "d": 4000},
]
df = pd.DataFrame(mydict)
print(df)
print(df.iloc[[True, False, True]])
给
a b c d
0 1 2 3 4
1 100 200 300 400
2 1000 2000 3000 4000
a b c d
0 1 2 3 4
2 1000 2000 3000 4000
适用于上面的代码,其中行等于布尔值但在
print(df.iloc[[True, True]])
编辑 4:
condition_loc = list(filter(lambda x:x.head()>500,df.loc))
给
KeyError: 0
The above exception was the direct cause of the following exception:
raise KeyError(key) from errKeyError: 0
编辑 5:
boolean = list(compress(loan_df, list1))
print(boolean )
打印列名!
编辑 6:
list1 = [True , False , True ]
boolean = list(compress(df, list1))
for i in boolean :
print(df.loc[boolean])
给
raise KeyError(f"None of [{key}] are in the [{axis_name}]")
KeyError: "None of [Index(['Gender', 'Dependents'], dtype='object', name='Loan_ID')] are in the [index]"
编辑 7: iloc 问题已解决
all_rows_df = list(range(0, len(df))) # gives integer values
boolean = list(compress(all_rows_df, list1)) # selects values by comparison
print(boolean)
for i in boolean :
print(i)
print(df.iloc[i]) # Index position of rows in integer or list of integer
给
[0, 2]
Gender Male
Married No
Dependents 0
Education Graduate
Self_Employed No
ApplicantIncome 5849
CoapplicantIncome 0.0
LoanAmount NaN
Loan_Amount_Term 360.0
Credit_History 1.0
Property_Area Urban
Loan_Status Y
Name: LP001002, dtype: object
Gender Male
Married Yes
Dependents 0
Education Graduate
Self_Employed Yes
ApplicantIncome 3000
CoapplicantIncome 0.0
LoanAmount 66.0
Loan_Amount_Term 360.0
Credit_History 1.0
Property_Area Urban
Loan_Status Y
Name: LP001005, dtype: object
但是上面的方法在 loc 上报错
[0, 2]
0
KeyError: 0
The above exception was the direct cause of the following exception:
return self._getitem_axis(maybe_callable, axis=axis)
return self._get_label(key, axis=axis)
return self.obj.xs(label, axis=axis)
loc = index.get_loc(key)
raise KeyError(key) from errKeyError: 0
目前我坚持这个
【问题讨论】:
-
这个:
filtered = Series([True, False, True], index=df.index)。你的df有 614 行。它如何映射到您正在创建的Series中的 3 个布尔值? -
我如何将 loc 和 iloc 与系列一起使用?
-
为什么要将前 5 行与整个数据框进行比较?
df.loc[df. LoanAmount.head() < 500]? -
Intention is to generate given a list of boolean values select rows where value is true您是否尝试过使用长度与数据集中的行数相同的布尔值列表?
标签: python-3.x pandas dataframe boolean