小编Isa*_*odo的帖子

matplotlib 条形图仅突出显示值

嗨,我想得到这种条形图。问题是如何通过选择设置相应的xlables?

在此处输入图片说明

我编码如下以删除不需要的国家/地区标签,但图表也有 nan 作为标签。

countries=['United States','Mexico','Japan','China','Korea,Rep.','Ireland','France','Italy']
new_index=list(df.index)
for i in range(len(new_index)):
    if new_index[i] not in countries :
        new_index[i]=np.nan
Run Code Online (Sandbox Code Playgroud)

这是我的结果,标签中为 nan,条形之间的距离更宽: 在此处输入图片说明

对于数据:

import numpy as np
import pandas as pd

#Overall Country list
Countries=['United States','Mexico','Japan','China','Korea,Rep.','Ireland','France','Italy','Czech Republic',
 'Austria',
 'Slovak Republic',
 'Slovenia',
 'Germany',
 'Portugal',
 'Hungary',
 'Colombia',
 'New Zealand',
 'Norway',
 'Latvia']

#Countries to highlight
Desired=['United States','Mexico','Japan','China','Korea,Rep.','Ireland','France','Italy']

np.random.seed(0)
Value=np.random.rand(len(Countries))
df = pd.DataFrame({'Countries': Countries,'Value': Value,})
df.sort_values(['Value'],inplace=True)

df.set_index('Countries',drop=True,inplace=True)
ax_1 = df['Value'].plot(kind='bar', title ="graph", figsize=(10, 6), fontsize=12)
ax_1.set_xlabel("Country Name", fontsize=12)
plt.show()
Run Code Online (Sandbox Code Playgroud)

python visualization data-visualization matplotlib pandas

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

更新 Node JS 中 .env 文件中的属性

我正在编写一个普通的 .env 文件,如下所示:

VAR1=VAL1
VAR2=VAL2
Run Code Online (Sandbox Code Playgroud)

我想知道是否有一些模块可以在 NodeJS 中使用来产生一些效果,例如:

somefunction(envfile.VAR1) = VAL3
Run Code Online (Sandbox Code Playgroud)

结果.env文件将是

VAR1=VAL3
VAR2=VAL2
Run Code Online (Sandbox Code Playgroud)

即在其他变量不变的情况下,只更新选定的变量。

javascript node.js

6
推荐指数
2
解决办法
8174
查看次数

如何使用 float64 nan 选择行?

我有一个来自 excel 的数据框,其中行中有几个 NaN。我想用另一个基线行替换值全部为 NaN 的行。

原始数据框是这样的:

                    Country Name  Years  tariff1_1  tariff1_2  tariff1_3  
830                 Hungary       2004   9.540313   6.287314  13.098201   
831                 Hungary       2005   9.540789   6.281724  13.124401 
832                 Hungary       2006   NaN        NaN       NaN 
833                 Hungary       2007   NaN        NaN       NaN 
834                 eu            2005   8.55       5.7       11.4
835                 eu            2006   8.46       5.9       11.6
836                 eu            2007   8.56       5.3       11.9
Run Code Online (Sandbox Code Playgroud)

因此,如果特定年份匈牙利的关税均为 NaN,则应根据确切年份,用欧盟数据替换该行。

理想的结果是:

                    Country Name  Years  tariff1_1  tariff1_2  tariff1_3  
830                 Hungary       2004   9.540313   6.287314  13.098201   
831                 Hungary       2005   9.540789   6.281724  13.124401 
832                 Hungary       2006   8.46 …
Run Code Online (Sandbox Code Playgroud)

python dataframe python-3.x pandas

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

如何使用“conda list”输出txt安装conda包

收到一个package_conda.txt格式如下的文件。

# packages in environment at /scratch/xxxx/anaconda3:
#
# Name                    Version                   Build  Channel
_ipyw_jlab_nb_ext_conf    0.1.0                    py37_0  
absl-py                   0.7.1                    pypi_0    pypi
alabaster                 0.7.12                   py37_0  
...
...
Run Code Online (Sandbox Code Playgroud)

该文件是通过命令生成的conda list > package_conda.txt

我尝试安装列出的软件包,conda install --file package_conda.txt但收到错误消息:

CondaValueError: could not parse '_ipyw_jlab_nb_ext_conf    0.1.0                    py37_0' in: package_conda.txt
Run Code Online (Sandbox Code Playgroud)

python anaconda

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

如何使用 inotifywait 等待文件创建并写入超时内容

我在 redhat ubi-minimal 中使用 shell 命令。我想监视一个文件“./hello.txt”,该文件尚不存在。我想用来inotifywait监视这个文件,并等待直到./hello.txt创建并在其中写入一些非空内容,或者固定超时。

以下是inotifywait手册页,我尝试过:

inotifywait -m -e create -r --fromfile ./hello.txt -t 30
Run Code Online (Sandbox Code Playgroud)

-m -e create指定该命令无限期执行直到create发生。

但它只给出:

Couldn't open ./hello.txt: No such file or directory
No files specified to watch!
Run Code Online (Sandbox Code Playgroud)

我想知道如何指定 inotifywait 的标志来实现我的期望。

inotify

4
推荐指数
1
解决办法
9913
查看次数

如何重复运行一个函数直到它返回 true 或超时?

我有一个函数checkSuccess(),如果任务完成则返回 true。

我想checkSuccess()每 1 秒调用一次并中断,直到它返回true或超时。

我现在所拥有的是使用 goroutine 运行 for 循环,其中我checkSuccess()每 1 秒调用一次。在主流程中,我用来time.After检查整个检查持续时间是否已超时。

func myfunc (timeout time.Duration) {

    successChan := make(chan struct{})
    timeoutChan := make(chan struct{})

    // Keep listening to the task.
    go func() {
        for {
            select {
            // Exit the forloop if the timeout has been reached
            case <-timeoutChan:
                return
            default:
            }
            // Early exit if task has succeed.
            if checkSuccess() {
                close(successChan)
                return
            }
        time.Sleep(time.Second)
        }
    }()

    // …
Run Code Online (Sandbox Code Playgroud)

go

4
推荐指数
1
解决办法
1730
查看次数

如何使用临时环境变量运行脚本?

如果我有一个带有简单行的 python 脚本,如下所示:

import os

name = os.environ.get('BEAR_NAME')
if name != "":
  print("hello, ", name)
Run Code Online (Sandbox Code Playgroud)

我想在运行此脚本时设置一个临时环境变量。请注意,如果我这样做

export BEAR_NAME="sleepybear"
python hello.py
Run Code Online (Sandbox Code Playgroud)

一旦 python 程序完成,环境变量BEAR_NAME仍然具有值,这是不希望的。sleepybear以docker为例,我们可以docker run -e SOME_VAR=SOME_VAL设置环境变量SOME_VAR。运行 python 脚本时是否有类似的方法?

python shell

3
推荐指数
1
解决办法
4637
查看次数

如何避免 NodeJS 中 switch case 中重复的 try/catch

现在我的代码是这样的:

const androidChannel = "android";
const iosChannel = "ios";
const smsChannel = "sms";
const emailChannel = "email";

switch(channel) {
    case iosChannel:
        try{
            output = await apnsAdaptor.processRequest(notificationRequest)
            console.log(output);
        } catch(err) {
            console.log("IOS with err: " + err);
        } 
        break;
        
    case androidChannel:
        try{
            output = await androidAdaptor.processRequest(notificationRequest)
            console.log(output);
        } catch(err) {
            console.log("Android with err: " + err);
        }
        break;
    
    case smsChannel:
        try{
            output = await smsAdaptor.processRequest(notificationRequest)
            console.log(output);
        } catch(err) {
            console.log("Sms with err: " + err);
        }
        break;
    
    default:
        console.log("This is the …
Run Code Online (Sandbox Code Playgroud)

javascript node.js

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