小编yat*_*atu的帖子

遮罩二维数组保留形状

我有这样的二维 numpy 数组:

arr = np.array([[1,2,4],
                [2,1,1],
                [1,2,3]])
Run Code Online (Sandbox Code Playgroud)

和一个布尔数组:

boolarr = np.array([[True, True, False],
                    [False, False, True],
                    [True, True,True]])
Run Code Online (Sandbox Code Playgroud)

现在,当我尝试根据 boolarr 对 arr 进行切片时,它给了我

arr[boolarr]
Run Code Online (Sandbox Code Playgroud)

输出:

array([1, 2, 1, 1, 2, 3])
Run Code Online (Sandbox Code Playgroud)

但我希望有一个二维数组输出。所需的输出是

[[1, 2],
 [1],
 [1, 2, 3]]
Run Code Online (Sandbox Code Playgroud)

python numpy slice

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

使用 nx.degree_histogram 绘制图形的度数分布

我尝试使用以下代码来绘制 的度分布networkx.DiGraph G

def plot_degree_In(G):
    in_degrees = G.in_degree()
    in_degrees=dict(in_degrees)
    in_values = sorted(set(in_degrees.values()))
    in_hist = [list(in_degrees.values()).count(x) for x in in_values]

    plt.figure() 
    plt.grid(False)
    plt.loglog(in_values, in_hist, 'r.') 
    #plt.loglog(out_values, out_hist, 'b.') 
    #plt.legend(['In-degree', 'Out-degree'])
    plt.xlabel('k')
    plt.ylabel('p(k)')
    plt.title('Degree Distribution')
    plt.xlim([0, 2*100**1])
Run Code Online (Sandbox Code Playgroud)

但后来我意识到这不是正确的做法,所以我将其更改为:

def plot_degree_dist(G):
    degree_hist = nx.degree_histogram(G) 
    degree_hist = np.array(degree_hist, dtype=float)
    degree_prob = degree_hist/G.number_of_nodes()
    plt.loglog(np.arange(degree_prob.shape[0]),degree_prob,'b.')
    plt.xlabel('k')
    plt.ylabel('p(k)')
    plt.title('Degree Distribution')
    plt.show()
Run Code Online (Sandbox Code Playgroud)

但这给了我一个没有数据的空图。

python directed-graph matplotlib histogram networkx

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

Pandas:当其中一列中有列表时,如何按列中的值进行分组

我正在尝试按"value_1"列中的值进行分组.但我的最后一栏是由列表组成的.当我尝试使用"value_1"列进行分组时,由列表组成的列将消失.

数据帧:

 value_1:        value_2:           value_3:               list: 
 american     california, nyc      walmart, kmart      [supermarket, connivence] 
 canadian         toronto            dunkinDonuts      [coffee]
 american          texas                               [state]
 canadian                             walmart          [supermarket] 
   ...              ...                 ...              ....
Run Code Online (Sandbox Code Playgroud)

我的预期输出是:

value_1:        value_2:              value_3:             list: 
american   california, nyc, texas   walmart, kmart      [supermarket, connivence, state] 
canadian         toronto         dunkinDonuts, walmart  [coffee, supermarket]
Run Code Online (Sandbox Code Playgroud)

谢谢!

python pandas

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

仅带有-1和1的随机列表

我需要创建仅由-1和1组成的随机列表,如-1,1,1,-1,-1(不包含零)。我当前的编码添加了0,这不适合我。

import random
for x in range(10):
    print (random.randint(-1,1))
Run Code Online (Sandbox Code Playgroud)

python random list

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

检查数据帧组是否包含列表中的值

如何根据另一列的值打印/返回值?

analyse = input[['SR. NO', 'COUNTRY_NAME']]
print(analyse)

SR. NO    COUNTRY_NAME
     2          Norway
     2         Denmark
     2         Iceland
     2         Finland
     3         Denmark
     3         Iceland
     4         Finland
     4          Norway
Run Code Online (Sandbox Code Playgroud)

在这里,我想检查一下挪威 丹麦 是否出现每个SR 中。否,请返回未找到这两个国家/地区之一的序列号!我尝试使用 groupby 并迭代国家/地区,但这没有帮助。我被困在这一点上。

因此,预期输出是:

[3,4]
Run Code Online (Sandbox Code Playgroud)

python dataframe python-3.x pandas

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

从 Pandas 数据帧创建一个 json 对象

      Groups sub-groups selections
    0   sg1    csg1       sc1
    1   sg1    csg1       sc2
    2   sg1    csg2       sc3
    3   sg1    csg2       sc4
    4   sg2    csg3       sc5
    5   sg2    csg3       sc6
    6   sg2    csg4       sc7
    7   sg2    csg4       sc8
Run Code Online (Sandbox Code Playgroud)

我有上面提到的数据框,我正在尝试创建一个 JSON 对象,如下所示:

{
  "sg1": {
    "csg1": ['sc1', 'sc2'],
    "csg2": ['sc3', 'sc4']
  },
  "sg2": {
    "csg3": ['sc5', 'sc6'],
    "csg4": ['sc7', 'sc8']
  }
}
Run Code Online (Sandbox Code Playgroud)

我尝试将熊猫 to_json 和 to_dict 与 orient 参数一起使用,但没有得到预期的结果。我还尝试按列分组,然后创建列表并将其转换为 JSON。

任何帮助深表感谢。

python dictionary multi-index pandas

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

pandas 中的 groupby 和函数调用

我有一个数据框,其中有一列“名称”。名称有多个值,例如sample1、sample2、sample3。我想将函数应用于“名称”列中的值相同的所有组。

输出:

   Name  Value  Result
0  Name1      2       5
1  Name1      3       5
2  Name2      1      11
3  Name2      4      11
4  Name2      6      11
5  Name3      8      10
6  Name3      2      10
Run Code Online (Sandbox Code Playgroud)

python python-3.x pandas

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

计算每组中 NaN 的数量

在此数据框中,我尝试计算每种颜色有多少个 NaNcolor

这就是示例数据的样子。实际上,有 100k 行。

   color     value  
0  blue      10 
1  blue      NaN  
2  red       NaN
3  red       NaN
4  red       8
5  red       NaN
6  yellow    2
Run Code Online (Sandbox Code Playgroud)

我希望输出看起来像这样:

   color     count  
0  blue      1 
1  red       3
2  yellow    0
Run Code Online (Sandbox Code Playgroud)

python pandas

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

如何根据数据框另一列中的条件在列中找到最小值?

我有一个如下所示的数据框:

Number  Req Response 
0       3    6
1       5    0
2       33   4
3       15   3
4       12   2
Run Code Online (Sandbox Code Playgroud)

我想在“请求”为 15 之前确定最小“响应”值。

我尝试了以下代码:

min_val=[]
for row in range(len(df)):
#if the next row of 'Req' contains 15, append the current row value of'Response'
  if(df[row+1].loc[df[row+1]['Req'] == 15]): 
         min_val.append(df['Response'].min())
  else:
         min_val.append(0)
Run Code Online (Sandbox Code Playgroud)

我收到“无效类型比较”错误。

我期望以下输出:

Min value of df['Response'] is: 0
Run Code Online (Sandbox Code Playgroud)

python pandas

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

计算字符串中的字母数(不是字符,只有字母)

我试过寻找答案,但我只找到如何计算字符数。我需要知道如何计算字符串中的字母数。还需要知道如何计算字符串中数字的数量。

例如:

"abc 12"
Run Code Online (Sandbox Code Playgroud)

输出将是

字母:3 数字:2

python

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