I have following pandas dataframe:
countryCode Name number myprice prices
DZ name1 number1 US.p nan
DZ name1 number1 AU.currency 45
DZ name1 number1 DZ.currency 55
DZ name1 number1 DZ.p 62
DZ name1 number1 AU.p 73
DZ name1 number1 US.currency nan
AU name1 number1 US.p nan
AU name1 number1 AU.currency 77
I would like to select rows only when the string in countryCode lies in the column myprice.
Required:
countryCode Name number myprice prices
DZ name1 number1 DZ.currency 55.0
DZ name1 number1 DZ.p 62.0
AU name1 number1 AU.currency 77.0
I tried following:
df = pd.read_clipboard()
df[df.countryCode.isin(df.myprice)]
df[df.myprice.str.contains(df.countryCode.str)]
I have been trying for a while but to no avail.
How can this be done?
答案 0 :(得分:2)
looking at your example you can try:
df[df.countryCode.eq(df.myprice.str.split(".").str[0])]
countryCode Name number myprice prices
2 DZ name1 number1 DZ.currency 55.0
3 DZ name1 number1 DZ.p 62.0
7 AU name1 number1 AU.currency 77.0