映射到pandas中的substring键

时间:2017-03-29 11:14:39

标签: python pandas

<artifactId>AuctionWebsiteM</artifactId>
<packaging>war</packaging>
<name>AuctionWebsiteM</name>
<description>An auction website</description>
<version>1.0</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
</parent>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>

    <!-- Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>


    <!-- Web with Tomcat + Embed -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- JSTL -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

    <!-- Need this to compile JSP -->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Need this to compile JSP -->
    <dependency>
        <groupId>org.eclipse.jdt.core.compiler</groupId>
        <artifactId>ecj</artifactId>
        <version>4.6.1</version>
        <scope>provided</scope>
    </dependency>

    <!-- Optional, for bootstrap -->
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.7</version>
    </dependency>

            <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>

    </dependency>
             <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
            </dependency>

            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
                    <version>5.2.9.Final</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-commons</artifactId>
            </dependency>
 <dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-jpa</artifactId>
  <type>jar</type>
 </dependency>
        <dependency>
                <groupId>unknown.binary</groupId>
                <artifactId>mysql-connector-java-5.1.23-bin</artifactId>
                <version>SNAPSHOT</version>
        </dependency>
</dependencies>
<build>
    <plugins>
        <!-- Package as an executable jar/war -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
    <repositories>
            <repository>
                    <id>unknown-jars-temp-repo</id>
                    <name>A temporary repository created by NetBeans for libraries and jars it could not identify. Please replace the dependencies in this repository with correct ones and delete this repository.</name>
                    <url>file:${project.basedir}/lib</url>
            </repository>
    </repositories>

需要将b列中的每个键映射到文件中的值(示例NSE_FO_BHP_1703与文件中的NSE_FO_BHP_1703_ *匹配) 如果文件中的键包含LONG作为子字符串,则在列d中减去值 如果文件中的键包含SHORT作为子字符串,则在列d

中添加值

>>> print(df) out_0 out_1 b c e d 0 out_GFD_994 out_GFD_Part2 NSE_FO_BHP_1703 -27022 361.45 -13200 1 out_GFD_994 out_GFD_Part2 NSE_FO_BHP_1704 30132 399.98 13200 2 out_GFD_994 out_GFD_Part2 NSE_FO_MQG_1703 -145173 1177.97 -11200 3 out_GFD_994 out_GFD_Part2 NSE_FO_MQG_1704 158747 1180.55 12300 4 out_GFD_994 out_GFD_Part2 NSE_FO_TLS_1703 23316 1247.50 3200 5 out_GFD_994 out_GFD_Part2 NSE_FO_TLS_1704 -19333 1253.62 -3200 更改NSE_FO_BHP_1703_MAXLONGPOS = 200000 changes to 213200。分别更改NSE_FO_BHP_1703_MAXSHORTPOS to 186800。 这将针对df的b列中的每个键完成。

MAXLONGEXPOSURE and MAXSHORTEXPOSURE to 263200 and 236800

1 个答案:

答案 0 :(得分:1)

我认为如果所有数据都成对,我可以使用 - *LONG**SHORT*

from pandas.compat import StringIO

temp=u"""
NSE_FO_BHP_1703_MAXLONGPOS = 200000
NSE_FO_BHP_1703_MAXSHORTPOS = 200000
NSE_FO_BHP_1703_MAXLONGEXPOSURE = 250000
NSE_FO_BHP_1703_MAXSHORTEXPOSURE = 250000
NSE_FO_BHP_1704_MAXLONGPOS = 200000
NSE_FO_BHP_1704_MAXSHORTPOS = 200000
NSE_FO_BHP_1704_MAXLONGEXPOSURE = 250000
NSE_FO_BHP_1704_MAXSHORTEXPOSURE = 250000
NSE_FO_TLS_1703_MAXLONGPOS = 100000
NSE_FO_TLS_1703_MAXSHORTPOS = 100000
NSE_FO_TLS_1703_MAXLONGEXPOSURE = 200000
NSE_FO_TLS_1703_MAXSHORTEXPOSURE = 200000
NSE_FO_TLS_1704_MAXLONGPOS = 100000
NSE_FO_TLS_1704_MAXSHORTPOS = 100000
NSE_FO_TLS_1704_MAXLONGEXPOSURE = 200000
NSE_FO_TLS_1704_MAXSHORTEXPOSURE = 200000
NSE_FO_MQG_1703_MAXLONGPOS = 300000
NSE_FO_MQG_1703_MAXSHORTPOS = 300000
NSE_FO_MQG_1703_MAXLONGEXPOSURE = 400000
NSE_FO_MQG_1703_MAXSHORTEXPOSURE = 400000
NSE_FO_DEF_1704_MAXLONGPOS = 300000
NSE_FO_MQG_1704_MAXSHORTPOS = 300000
NSE_FO_MQG_1704_MAXLONGEXPOSURE = 400000
NSE_FO_MQG_1704_MAXSHORTEXPOSURE = 400000"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df2 = pd.read_csv(StringIO(temp), sep="\s+=\s+", engine='python', names=['A','B'])
#create dict for map
d = df1.set_index('b')['d'].to_dict()
#split by last _ and map
a = df2.A.str.rsplit('_', n=1).str[0].map(d)
#if LONG substract, else add
df2['C'] = np.where(df2.A.str.contains('LONG'), df2.B - a, df2.B + a)
print (df2)
                                   A       B         C
0         NSE_FO_BHP_1703_MAXLONGPOS  200000  213200.0
1        NSE_FO_BHP_1703_MAXSHORTPOS  200000  186800.0
2    NSE_FO_BHP_1703_MAXLONGEXPOSURE  250000  263200.0
3   NSE_FO_BHP_1703_MAXSHORTEXPOSURE  250000  236800.0
4         NSE_FO_BHP_1704_MAXLONGPOS  200000  186800.0
5        NSE_FO_BHP_1704_MAXSHORTPOS  200000  213200.0
6    NSE_FO_BHP_1704_MAXLONGEXPOSURE  250000  236800.0
7   NSE_FO_BHP_1704_MAXSHORTEXPOSURE  250000  263200.0
8         NSE_FO_TLS_1703_MAXLONGPOS  100000   96800.0
9        NSE_FO_TLS_1703_MAXSHORTPOS  100000  103200.0
10   NSE_FO_TLS_1703_MAXLONGEXPOSURE  200000  196800.0
11  NSE_FO_TLS_1703_MAXSHORTEXPOSURE  200000  203200.0
12        NSE_FO_TLS_1704_MAXLONGPOS  100000  103200.0
13       NSE_FO_TLS_1704_MAXSHORTPOS  100000   96800.0
14   NSE_FO_TLS_1704_MAXLONGEXPOSURE  200000  203200.0
15  NSE_FO_TLS_1704_MAXSHORTEXPOSURE  200000  196800.0
16        NSE_FO_MQG_1703_MAXLONGPOS  300000  311200.0
17       NSE_FO_MQG_1703_MAXSHORTPOS  300000  288800.0
18   NSE_FO_MQG_1703_MAXLONGEXPOSURE  400000  411200.0
19  NSE_FO_MQG_1703_MAXSHORTEXPOSURE  400000  388800.0
20        NSE_FO_DEF_1704_MAXLONGPOS  300000       NaN <-no data in df1 - map return NaN
21       NSE_FO_MQG_1704_MAXSHORTPOS  300000  312300.0
22   NSE_FO_MQG_1704_MAXLONGEXPOSURE  400000  387700.0
23  NSE_FO_MQG_1704_MAXSHORTEXPOSURE  400000  412300.0