在python上呆了两个月,我现在正集中精力于Pandas。在我目前的工作岗位上,我在数据帧上使用VBA,因此学习VBA可以慢慢地取代它,并进一步推进我的职业生涯。到目前为止,我认为我真正的问题是缺乏对关键概念的理解。任何帮助都将不胜感激。
我的问题是:
我在哪里可以学到更多关于如何做这样的事情来进行更精确的过滤。我非常接近,但我需要一个关键的方面。
目标(S)
的主要目标--我需要跳过ID列中的某些值。下面的代码取出破折号"-“并且只读取9位数。但是,我需要跳过某些ID,因为它们是唯一的。
之后,我将开始比较多张表。
我需要跳过的唯一in在两个数据帧中都是相同的,但格式完全不同,从000,000-000-#12,000,000-000-35,或0- 000-000-000_z不等。
除唯一的ID外,我将在每个ID上使用的代码:
dfSS["ID"] = dfSS["ID"].str.replace("-", "").str[:9]但是我想使用一个if语句,比如(这不起作用)。
lst = ["000-000-000_#69B", "000-000-000_a", "etc.. random IDs", ]
if ~dfSS["ID"].isin(lst ).any()
dfSS["ID"] = dfSS["ID"].str.replace("-", "").str[:9]
else:
pass为了得到更多的澄清,我输入的是:
ID Street # Street Name
0 004-330-002-000 2272 Narnia
1 021-521-410-000_128 2311 Narnia
2 001-243-313-000 2235 Narnia
3 002-730-032-000 2149 Narnia
4 000-000-000_a 1234 Narnia和我希望这样做作为输出:
ID Street # Street Name
0 004330002 2272 Narnia
1 021-521-410-000_128 2311 Narnia
2 001243313000 2235 Narnia
3 002730032000 2149 Narnia
4 000-000-000_a 1234 Narnia备注:
这里是我研究这个的地方:
发布于 2021-06-10 21:46:29
有许多方法可以做到这一点。这里的第一种方法不需要编写函数。
# Create a placeholder column with all transformed IDs
dfSS["ID_trans"] = dfSS["ID"].str.replace("-", "").str[:9]
dfSS.loc[~dfSS["ID"].isin(lst), "ID"] = dfSS.loc[~dfSS["ID"].isin(lst), "ID_trans"] # conditional indexing第二种方法是编写一个有条件地转换it的函数,它的速度不如第一个方法。
def transform_ID(ID_val):
if ID_val not in lst:
return ID_val.replace("-", "")[:9]
dfSS['ID_trans'] = dfSS['ID'].apply(transform_ID)发布于 2021-06-17 20:54:13
这是基于@xyzxyzjayne的答案,但我有两个问题我无法解决。
第一期
我收到警告了吗:(见编辑)
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead您将在下面的代码中看到,我试图放入.loc,但我似乎无法找到如何正确使用.loc来消除此警告。还在学习。不,我不会忽视它,即使它起作用。我说这是一个学习的机会。
第二期
我不明白这部分代码。我知道左边应该是行,右边是列。那就是说为什么这个能起作用?当此代码为rune时,ID是列而不是行。我做身份证明:
df.loc[~df["ID "].isin(uniqueID ), "ID "] = df.loc[~df["ID "].isin(uniqueID ), "Place Holder"]我还不明白的区域是这个部分的逗号(,)的左边:
df.loc[~df["ID "].isin(uniqueID), "ID "]这就是说,这是最后的结果,基本上就像我说的它的XZY的帮助把我带到了这里,但我正在添加更多的.locs,并使用文档,直到我可以消除警告。
uniqueID = [ and whole list of IDs i had to manually enter 1000+ entries that
will go in the below code. These ids get skipped. example: "032-234-987_#4256"]
# gets the columns i need to make the DateFrame smaller
df = df[['ID ', 'Street #', 'Street Name', 'Debris Finish', 'Number of Vehicles',
'Number of Vehicles Removed', 'County']]
#Place holder will make our new column with this filter
df.loc[:, "Place Holder"] = df.loc[:,"ID "].str.replace("-", "").str[:9]
#the next code is the filter that goes through the list and skips them. Work in progress to fully understand.
df.loc[~df["ID "].isin(uniqueID ), "ID "] = df.loc[~df["ID "].isin(uniqueID ), "Place Holder"]
#Makes the ID our index
df = df.set_index("ID ")
#just here to add the date to our file name. Must import time for this to work
todaysDate = time.strftime("%m-%d-%y")
#make it an excel file
df.to_excel("ID TEXT " + todaysDate + ".xlsx")将编辑这个一旦我摆脱警告,并找出左侧,以便我可以解释给每一个谁需要/看到这篇文章。
编辑: SettingWithCopyWarning:
修正了这个链接索引问题,在过滤之前复制了一个原始数据库,并像XYZ帮助我做的那样做了所有的.loc。在开始筛选之前,请使用DataFrame.copy(),其中DataFrame是您自己的数据name的名称。
https://stackoverflow.com/questions/67928557
复制相似问题