我试图编写一个函数来交换pandas数据帧中目标字典的结果。我想匹配一组值并交换出新值。我试着跟着那排,但我没选。我觉得我错过了一些关键的功能。在
import pandas
testData=pandas.DataFrame([["Cats","Parrots","Sandstone"],["Dogs","Cockatiels","Marble"]],columns=["Mammals","Birds","Rocks"])
target=("Mammals","Birds")
swapVals={("Cats","Parrots"):("Rats","Canaries")}
for x in swapVals:
#Attempt 1:
#testData.loc[x,target]=swapVals[x]
#Attempt 2:
testData[testData.loc[:,target]==x,target]=swapVals[x]
这是用Python2编写的,但是基本思想应该对您有用。它使用应用功能:
请注意,如果将其他键加载到dict中,则可以在不使用swapper函数的情况下执行apply:
^{pr2}$相关问题 更多 >
编程相关推荐