【发布时间】:2018-11-10 05:20:01
【问题描述】:
我是 Pandas 和一般编程的新手。如果这很重要,我正在使用 Anaconda。
我手上有以下东西:
臭名昭著的泰坦尼克号生存数据集。
所以,我的想法是搜索数据框,找到“姓名”列中存在字符串“夫人”的行。并且同时“年龄”将是 NaN(在这种情况下,“年龄”列中的值需要更改为 32)。另外,在单元格中找到“Miss”,另外两列中的值为零。
我的主要问题是我不知道如何告诉 Pandas 替换同一行中的值或删除整行。
#I decided to collect the indexes of rows with the "Age" value == NaN to further use the
#indices to search through the "Names column."
list_of_NaNs = df[df['Age'].isnull()].index.tolist()
for name in df.Name:
if "Mrs." in name and name (list_of_NaNs):#if the string combination "Mrs."
#can be found within the cell...
df.loc['Age'] = 32.5 #need to change the value in the
#column IN THE SAME ROW
elif "Miss" in name and df.loc[Parch]>0: #how to make a
#reference to a value IN THE SAME ROW???
df.loc["Age"] = 5
elif df.SibSp ==0 and Parch ==0:
df.loc["Age"] = 32.5
else:
#mmm... how do I delete entire row so that it doesn't
#interfere with my future actions?
【问题讨论】:
标签: pandas