我在努力重新录制一些标签。这里是我最起码的例子。在import pandas as pd
testDict = {'Col1' : pd.Categorical(["a", "b", "c", "d", "e"]),
'Col2' : pd.Categorical(["1", "2", "3", "4", "5"])}
testDF = pd.DataFrame.from_dict(testDict)
testDF
testDF['Col1'].value_counts()
def letter_recode(Col1):
if(Col1=="a")|(Col1=="b"):
return "ab"
elif (Col1=="c")|(Col1=="d"):
return "cd"
else:
return Col1
testDF['Col3'] = testDF['Col1'].apply(letter_recode)
testDF['Col3'].value_counts()
testDF
我想改变这个测向:
^{pr2}$
为此:Col1 Col2 Col3
0 a 1 ab
1 b 2 ab
2 c 3 cd
3 d 4 cd
<