你好,我正在做作业,遇到了一个无法回答的问题。问题是创建另一个DataFrame df_urban,该DataFrame由原始数据集的所有列组成,但仅由其Property_Area属性中具有城市身份(不包括农村和半城市(且ApplicantIncome至少为10000新元的申请人组成。重置行索引并显示此DataFrame的最后10行。
问题图片
然而,我的代码不符合申请人收入至少为10000的标准,也不符合该地区的城市身份。
df_urban=dfdf_urban.iloc[-10:[11]]
我想知道这个问题的解决办法是什么。数据图片
您可以使用'&'操作员通过多列条件限制数据:
df_urban = df[(df[col]==<condition>) & (df[col] >= <condition>)]
下面是一个简单的代码片段,它在提取主数据帧的一个子集以生成仅为";城市;地点。
import pandas as pd
df=pd.read_csv('Applicants.csv',delimiter='t')
print(df)
df_urban = df[(df['Property_Area'] == 'Urban')]
print(df_urban)
使用一个简单构建的CSV文件,这里有一个输出示例。
ApplicantIncome CoapplicantIncome LoanAmount Loan_Term Credit_History Property_Area
0 4583 1508 128000 360 1 Rural
1 1222 0 55000 360 1 Rural
2 8285 0 64000 360 1 Urban
3 3988 1144 75000 360 1 Rural
4 2588 0 84700 360 1 Urban
5 5248 0 48550 360 1 Rural
6 7488 0 111000 360 1 SemiUrban
7 3252 1112 14550 360 1 Rural
8 1668 0 67500 360 1 Urban
ApplicantIncome CoapplicantIncome LoanAmount Loan_Term Credit_History Property_Area
2 8285 0 64000 360 1 Urban
4 2588 0 84700 360 1 Urban
8 1668 0 67500 360 1 Urban
希望能有所帮助。
谨致问候。
请参阅下文。我把如何重置索引交给你。您可能需要查看.tail((来显示最后一行。
df_urban = df[(df['ApplicantIncome'] > 10000) & (df['Property_Area'] == 'Urban')]