小编BKS*_*BKS的帖子

Identifying root parents and all their children in trees

I have a pandas dataframe as such:

parent   child   parent_level   child_level
A        B       0              1
B        C       1              2
B        D       1              2
X        Y       0              2
X        D       0              2 
Y        Z       2              3
Run Code Online (Sandbox Code Playgroud)

This represents a tree that looks like this

       A  X
      /  / \
     B  /   \
    /\ /     \
   C  D       Y
              |
              Z
Run Code Online (Sandbox Code Playgroud)

I want to produce something that looks like this:

root    children
A       [B,C,D]
X       [D,Y,Z]
Run Code Online (Sandbox Code Playgroud)

or

root   child
A      B
A …
Run Code Online (Sandbox Code Playgroud)

python pandas

11
推荐指数
2
解决办法
181
查看次数

如何生成单词频率直方图,其中条形根据其高度排序

我有很长的单词列表,我想生成列表中每个单词频率的直方图.我能够在下面的代码中这样做:

import csv
from collections import Counter
import numpy as np

word_list = ['A','A','B','B','A','C','C','C','C']

counts = Counter(merged)

labels, values = zip(*counts.items())

indexes = np.arange(len(labels))

plt.bar(indexes, values)
plt.show()
Run Code Online (Sandbox Code Playgroud)

但是,它不按等级显示分档(即按频率显示,因此最高频率是左边的第一个分箱,依此类推),即使我打印counts它时也为我命令Counter({'C': 4, 'A': 3, 'B': 2}).我怎么能实现这一目标?

python matplotlib ranking histogram python-2.7

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

如何在pandas数据帧中从groupby的结果生成所有值对

我有一个pandas数据帧df:

ID     words
1      word1
1      word2
1      word3
2      word4
2      word5
3      word6
3      word7
3      word8
3      word9
Run Code Online (Sandbox Code Playgroud)

我想生成另一个数据帧,它将生成每个组中的所有单词对.所以上面的结果将是:

ID     wordA    wordB
1      word1    word2
1      word1    word3
1      word2    word3
2      word4    word5
3      word6    word7
3      word6    word8
3      word6    word9
3      word7    word8
3      word7    word9
3      word8    word9
Run Code Online (Sandbox Code Playgroud)

我知道我可以用来df.groupby['words']获取每个内容ID.

我也知道我可以用

iterable = ['word1','word2','word3']
list(itertools.combinations(iterable, 2))
Run Code Online (Sandbox Code Playgroud)

获得所有可能的成对组合.但是,如上所示,我对生成结果数据帧的最佳方法有点迷失.

python combinations python-2.7 pandas

8
推荐指数
2
解决办法
2559
查看次数

带圆圈的热图指示人口规模

嗨,我想用 Python 生成一个热图,类似于所示的热图,其中圆圈的大小表示该单元格中样本的大小。我查看了 seaborn 的画廊并找不到任何东西,而且我认为我无法用 matplotlib 做到这一点。

示例热图

python data-visualization matplotlib seaborn

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

Networkx统计推断

我有一个有向加权图,我使用networkx成功创建.

我试图在这个网络上产生一些统计推断,但我遇到了麻烦.他们来了:

(i)网络的平均程度.(我唯一能找到的是average_degree_connectivity,它返回一个字典而不是一个浮点数,具有整个网络的平均度)

(ii)网络的平均加权程度.(与上述相同)

(iii)网络的平均聚类系数.(我知道我必须使用nx.average_clustering(g),但是如何考虑它是加权有向图这一事实?我不断收到错误:NetworkXError :('Clustering algorithms are not defined','for有向图.'))

谢谢!

python graph social-networking networkx

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

Groupby.transform在dask数据帧中不起作用

我正在使用以下dask.dataframe AID:

   AID FID  ANumOfF
0    1   X        1
1    1   Y        5
2    2   Z        6
3    2   A        1
4    2   X       11
5    2   B       18
Run Code Online (Sandbox Code Playgroud)

我知道在我可以使用的pandas数据帧中:

AID.groupby('AID')['ANumOfF'].transform('sum')
Run Code Online (Sandbox Code Playgroud)

要得到:

0     6
1     6
2    36
3    36
4    36
5    36
Run Code Online (Sandbox Code Playgroud)

我想在dask.dataframes中使用相同的功能,它通常使用与pandas数据帧相同的功能,但在这个实例中给出了以下错误:

AttributeError: 'SeriesGroupBy' object has no attribute 'transform'
Run Code Online (Sandbox Code Playgroud)

它可能是两件事之一,要么dask不支持它,要么是因为我使用的是python 3?

我尝试了以下代码:

AID.groupby('AID')['ANumOfF'].sum()
Run Code Online (Sandbox Code Playgroud)

但这只是给了我这样的每个组的总和:

AID
1     6
2    36
Run Code Online (Sandbox Code Playgroud)

我需要它像上面那样在每一行中重复一次总和.我的问题是,如果不支持变换,还有另一种方法可以达到相同的结果吗?

python dataframe python-3.x pandas dask

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

如何在pandas dataframe列中查找一组值之间的相关性

我有一个数据帧df:

ID    Var1     Var2
1     1.2        4
1     2.1        6
1     3.0        7
2     1.3        8
2     2.1        9
2     3.2        13
Run Code Online (Sandbox Code Playgroud)

我想找到每个之间Var1和之间的皮尔森相关系数值Var2ID

所以结果应该是这样的:

ID    Corr_Coef
1     0.98198
2     0.97073
Run Code Online (Sandbox Code Playgroud)

更新:

必须确保所有变量列都是intfloat

python dataframe pandas

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

Python 中的和弦图

您好,有一个 DataFrame:

Source   Target    Value     
A        B         10       
A        C         5    
A        D         15
A        E         20
A        F         3
B        A         3
B        G         15
F        D         13
F        E         2
E        A         20
E        D         6
Run Code Online (Sandbox Code Playgroud)

并想使用 Python 绘制这个和弦图:

在此输入图像描述

我在以下链接中找到了这个和弦图: https: //www.data-to-viz.com/graph/chord.html。它指出该图是使用 circlize 库(我相信它是一个 R 库)制作的。在 Python 中也有办法做到这一点吗?

我还希望能够为每个元素选择颜色(在我的例子中为 A 到 G)并在圆周上书写,如示例图像所示。

这是另一个带有颜色的 DataFrame 例如:

Name   Color
A      Red
B      Orange
C      Yellow
D      Green
E      Blue
F      Purple
Run Code Online (Sandbox Code Playgroud)

如果可能的话,还有一个箭头提示可以帮助区分源和目标,如示例图像中所示。

我在 python 中找不到一个现成的可用库可以为我做到这一点。

python chord-diagram

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

在熊猫DataFrame中计算h指数(作者出版物的影响/生产率)的有效方法

我对熊猫还很陌生,但是我一直在阅读有关它的知识以及处理大数据的速度。

我设法创建了一个数据框,现在有一个熊猫数据框,看起来像这样:

    0     1
0    1    14
1    2    -1
2    3  1817
3    3    29
4    3    25
5    3     2
6    3     1
7    3    -1
8    4    25
9    4    24
10   4     2
11   4    -1
12   4    -1
13   5    25
14   5     1
Run Code Online (Sandbox Code Playgroud)

Columns 0是作者的ID,column 1是该作者在出版物上被引用的次数(-1表示零被引用)。每行代表一个作者的不同出版物。

我正在尝试h-index为每个作者计算。h-index定义为作者被引用至少h次的h出版物的数量。所以对于作者:

作者1的h-index为1

作者2的h-index为0

作者3的h-index为3

作者4的h-index为2

作者5的h-index为1

这是我当前正在执行的方法,其中涉及很多循环:

current_author=1
hindex=0

for index, row in df.iterrows():
    if row[0]==current_author:
        if row[1]>hindex:
            hindex+=1
    else:
        print …
Run Code Online (Sandbox Code Playgroud)

python dataframe python-2.7 pandas

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

数据帧编码

有没有办法编码我的数据帧的索引?我有一个数据框,索引是国际会议的名称.

df2= pd.DataFrame(index=df_conf['Conference'], columns=['Citation1991','Citation1992'])

我一直在: KeyError: 'Leitf\xc3\xa4den der angewandten Informatik'

每当我的代码引用具有未知ascii字母的外国会议名称时.

我试过了:

df.at[x.encode("utf-8"), 'col1']

df.at[x.encode('ascii', 'ignore'), 'col']
Run Code Online (Sandbox Code Playgroud)

有办法解决吗?我试着看看我是否可以在创建时对数据帧进行编码,但似乎我也无法做到.

dataframe python-2.7 pandas

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