小编gar*_*ett的帖子

按键更新pandas DataFrame

我有一个历史股票交易的数据框.框架包含['ticker','date','cusip','profit','security_type']等列.原来:

trades['cusip'] = np.nan
trades['security_type'] = np.nan
Run Code Online (Sandbox Code Playgroud)

我有历史配置文件,我可以加载到具有['ticker','cusip','date','name','security_type','primary_exchange']等列的框架中.

我想用配置中的cusip和security_type更新交易框架,但仅限于股票代码和日期匹配的地方.

我以为我可以这样做:

pd.merge(trades, config, on=['ticker', 'date'], how='left')
Run Code Online (Sandbox Code Playgroud)

但是这不会更新列,它只是将配置列添加到交易中.

以下是有效的,但我认为必须有更好的方法.如果没有,我可能会在熊猫之外做.

for date in trades['date'].unique():
    config = get_config_file_as_df(date)
    ## config['date'] == date
    for ticker in trades['ticker'][trades['date'] == date]:
        trades['cusip'][ 
                           (trades['ticker'] == ticker)
                         & (trades['date']   == date)
                       ] \
            = config['cusip'][config['ticker'] == ticker].values[0]

        trades['security_type'][ 
                           (trades['ticker'] == ticker)
                         & (trades['date']   == date)
                       ] \
            = config['security_type'][config['ticker'] == ticker].values[0]
Run Code Online (Sandbox Code Playgroud)

python pandas

6
推荐指数
1
解决办法
7443
查看次数

perl打印浮点数的精度是多少?

my $num = log(1_000_000) / log(10);
print "num: $num\n";
print "int(num): " . int($num) . "\n";
print "sprintf(num): " . sprintf("%0.16f", $num) . "\n";
Run Code Online (Sandbox Code Playgroud)

生产:

num: 6
int(num): 5
sprintf(num): 5.9999999999999991
Run Code Online (Sandbox Code Playgroud)

perl 打印浮点数的精度是多少?

使用:v5.8.8为x86_64-linux-thread-multi构建

perl floating-point-precision

5
推荐指数
1
解决办法
719
查看次数

在R中逐行删除数据框的命名数字

我知道你可以使用负索引删除一行.

e <- data.frame(x=seq(1,5,1), y=seq(1,5,1))

e
  x y
1 1 1
2 2 2
3 3 3
4 4 4 
5 5 5

e <- e[-3,]

e
  x y
1 1 1
2 2 2
4 4 4
5 5 5
Run Code Online (Sandbox Code Playgroud)

但现在我想删除编号为4的行.

e <- e[-4,]

e
  x y
1 1 1
2 2 2
4 4 4
Run Code Online (Sandbox Code Playgroud)

它改为删除编号为5的行(但是,我猜,索引为4).

即使它在不同的编号索引中,如何删除"命名"#5行?

row r dataframe

2
推荐指数
1
解决办法
6142
查看次数

标签 统计

dataframe ×1

floating-point-precision ×1

pandas ×1

perl ×1

python ×1

r ×1

row ×1