小编use*_*732的帖子

如何根据多个值的总和删除 Pandas 中的行?

我建立了一个 df:

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(1,6,size=(10, 6)),
              columns=list('ABCDEF'))
df = df.applymap(lambda x: 'Sp'+str(x))
print(df)
Run Code Online (Sandbox Code Playgroud)

给出类似的东西:

     A    B    C    D    E    F
0  Sp4  Sp5  Sp4  Sp4  Sp4  Sp3
1  Sp2  Sp3  Sp5  Sp2  Sp2  Sp3
2  Sp2  Sp3  Sp2  Sp4  Sp5  Sp5
3  Sp5  Sp3  Sp1  Sp4  Sp4  Sp3
4  Sp3  Sp1  Sp1  Sp5  Sp4  Sp1
5  Sp1  Sp4  Sp4  Sp5  Sp4  Sp4
6  Sp2  Sp1  Sp3  Sp4  Sp5  Sp3
7  Sp3  Sp3 …
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas

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

如何根据所有列的条件从熊猫数据框中排除行

我有一个 df:

      population     plot1     plot2     plot3     plot4
0    Population1  Species1  Species1  Species2  Species2
1    Population2  Species4  Species2  Species3  Species4
2    Population3  Species1  Species2  Species1  Species2
3    Population4  Species4  Species4  Species4  Species4
4    Population5  Species2  Species2  Species4  Species2
5    Population6  Species4  Species3  Species3  Species4
6    Population7  Species3  Species4  Species1  Species3
7    Population8  Species4  Species4  Species4  Species4
8    Population9  Species3  Species4  Species2  Species3
9   Population10  Species1  Species3  Species2  Species4
10  Population11  Species2  Species4  Species2  Species4
Run Code Online (Sandbox Code Playgroud)

我想创建一个新的数据框,其中 Species4 出现不止一次的所有行(种群)都被删除。我已经尝试了几种使用.value_counts() 的方法,但无法找到一种方法将它一次应用于整个数据帧,而不仅仅是简单地循环遍历所有行(这在我拥有的大型数据集上需要很长时间) )。 …

python pandas

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

创建词典列表

我有代码生成28个词典的列表.它循环通过28个文件并链接相应字典中每个文件的数据点.为了使我的代码更灵活,我想使用:

tegDics = [dict() for x in range(len(files))]
Run Code Online (Sandbox Code Playgroud)

但是当我运行代码时,前27个字典是空白的,只有最后一个,tegDics [27]有数据.下面是代码,包括我必须使用的笨拙但功能强大的代码生成字典:

x=0
import os
files=os.listdir("DirPath")
os.chdir("DirPath")
tegDics = [{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}] # THIS WORKS!!!
#tegDics = [dict() for x in range(len(files))] - THIS WON'T WORK!!!
allRads=[]
while x<len(tegDics): # now builds dictionaries 
    for line in open(files[x]):
        z=line.split('\t')
        allRads.append(z[2])
        tegDics[x][z[2]]=z[4] # pairs catNo with locNo
    x+=1
Run Code Online (Sandbox Code Playgroud)

有人知道为什么更优雅的代码不起作用.

python

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

如何在单实例情况下杀死所有并行 Bash/Linux 任务

我有一个并行的 Bash 函数:

#!/bin/bash

touch out.txt
vals=$(seq 1 1 20)
task(){
    echo $1 >> out.txt
    x=$(wc -l out.txt | cut -d' ' -f 1) #read current length of file
    if [ $x > 5 ]
    then
    echo $x #NB prints even if <5!!
    KILL_ALL_JOBS_HERE_ON_IF_THEN_CONDITION
    fi
}

export -f task

echo $vals | xargs -P2 -I'{}' -d' ' bash -c 'task "$1"' bash {} 
Run Code Online (Sandbox Code Playgroud)

该函数开始将二十个值回显到单个文本文件。但是,当文件长度超过 5 行时,我希望所有进程/实例停止并且不再处理变量中的值。我该怎么做?(注意一个小问题,if-then 语句也很奇怪 - 它会回显处理后的值 $x,无论它是否 >5)。

linux parallel-processing bash function kill-process

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