How can I split a dataframe column into two parts such that the value in dataframe column is later replaced by the splitted value.
For example, I have a dataframe like :
col1 col2
"abc" "A, BC"
"def" "AX, Z"
"pqr" "P, R"
"xyz" "X, YZ"
I want to extract values before , and replace that cell with the extracted value.
So, the output should look like :
col1 col2
abc A
def AX
pqr P
xyz X
I am trying to do it as :
df['col2'].apply(lambda x: x.split(',')[0])
But it gives me error. Please suggest how can I get the desired output.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…