使用熊猫在另一列中查找列字符串的位置

时间:2018-10-11 16:42:55

标签: python string pandas

我有2列的数据框

   col1     col2
1  cat      the cat
2  dog      a nice dog
3  horse    horse is here

我需要找到col2中每个col1字符串的位置。

解决方案必须是:

   col1     col2          col3
1  cat      the cat        4
2  dog      a nice dog     7
3  horse    horse is here  0

必须有一个简单的解决方案,而不使用痛苦的循环,但我找不到它。

2 个答案:

答案 0 :(得分:5)

numpy.core.defchararray.find

FROM microsoft/dotnet:2.1-aspnetcore-runtime-nanoserver-sac2016 AS base
WORKDIR /app
EXPOSE 33684
EXPOSE 443

FROM microsoft/dotnet:2.1-sdk-nanoserver-sac2016 AS build
WORKDIR /src
COPY ["WebAPI/DockerRestAPI/DockerRestAPI.csproj", "WebAPI/DockerRestAPI/"]
COPY ["RestModel/RestModel.csproj", "RestModel/"]
RUN dotnet restore "WebAPI/DockerRestAPI/DockerRestAPI.csproj"
COPY . .
WORKDIR "/src/WebAPI/DockerRestAPI"
RUN dotnet build "DockerRestAPI.csproj" -c Release -o /app

ENV certPassword "crypticpassword"

# Use opnssl to generate a self signed certificate cert.pfx with password 
$env:certPassword
RUN openssl genrsa -des3 -passout pass:"crypticpassword" -out server.key 
2048
RUN openssl rsa -passin pass:"crypticpassword" -in server.key -out 
server.key
RUN openssl req -sha256 -new -key server.key -out server.csr -subj 
'/CN=localhost'
RUN openssl x509 -req -sha256 -days 365 -in server.csr -signkey server.key - 
out server.crt
RUN openssl pkcs12 -export -out DockerRestAPI.pfx -inkey server.key -in 
server.crt -certfile DockerRestAPI.crt -passout pass:"crypticpassword"

# Expose port 443 for the application.
EXPOSE 443
FROM build AS publish
RUN dotnet publish "DockerRestAPI.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "DockerRestAPI.dll"]

答案 1 :(得分:2)

pandas中使用字符串时,循环或列表解析通常比内置字符串方法要快。在您的情况下,可能会很短:

df['col3'] = [i2.index(i1) for i1,i2 in zip(df.col1,df.col2)]

>>> df
    col1           col2  col3
1    cat        the cat     4
2    dog     a nice dog     7
3  horse  horse is here     0