小编Lau*_*ber的帖子

Python - 删除列表中没有的所有子字符串

我想删除df列中不存在于已定义列表中的所有子字符串.例如:

mylist = {good, like, bad, hated, terrible, liked}

Current:                                         Desired:
index      content                               index        content                                          
0          a very good idea, I like it           0            good like
1          was the bad thing to do               1            bad
2          I hated it, it was terrible           2            hated terrible
...                                              ...
k          Why do you think she liked it         k            liked
Run Code Online (Sandbox Code Playgroud)

我已经设法定义了一个函数,它保存所有单词不在列表中,但是不知道如何反转这个函数来实现我想要的:

pat = r'\b(?:{})\b'.format('|'.join(mylist))
df['column1'] = df['column1'].str.contains(pat, '')
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

python string pandas

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

在参数列表中指定嵌套字典键

我有一个迭代字典列表的函数,将指定的键值对返回到新的字典列表中:

data = [
    {'user': {'login': 'foo1', 'id': 'bar2'}, 'body': 'Im not sure', 'other_field': 'value'},
    {'user': {'login': 'foo2', 'id': 'bar3'}, 'body': 'Im still not sure', 'other_field': 'value'},
]

filtered_list = []
keys = ['user','body']

for i in data:
    filt_dict = dict((k, i[k]) for k in keys if k in i)
    filtered_list.append(filt_dict)
Run Code Online (Sandbox Code Playgroud)

user密钥包含一个名为login;的子密钥。如何将其添加到keys参数列表中,而不是key user

示例输出:

filtered_list = [
    {'login': 'foo1', 'body': 'Im not sure'},
    {'login': 'foo2', 'body': 'Im still not sure'},
]
Run Code Online (Sandbox Code Playgroud)

python dictionary nested key list

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

Python - 将一个dicts列表分成单独的dicts

我正在使用HITS算法进行社交网络分析.该算法的使用产生两种不同的度量:hub-score和authority-score.生成一个包含两个基于这些度量的字典的列表,其中一个字典的索引为0,另一个字典的索引为0.

如何删除总体列表以获取两个单独的词典?代码和输出如下:

G = nx.read_weighted_edgelist('data.csv', create_using=nx.DiGraph())
HITS_scores = list(nx.hits(G))

Output:
List = Index     Type      Value
       0         dict      {'node1': 0.023, 'node3': 0.017.....'node17': 0.045}
       1         dict      {'node2': 0.042, 'node4': 0.002.....'node16': 0.032}

Desired Output:
hub_score =      dict      {'node1': 0.023, 'node3': 0.017.....'node17': 0.045}
auth_score =     dict      {'node2': 0.042, 'node4': 0.002.....'node16': 0.032}
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

PS我试过寻找答案,但一直无法找到解决方案

python dictionary list network-analysis

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

Python-'str'和'int'的实例之间不支持'TypeError:'<='

我有一个df列,其值的范围是-5到10。我想将值<= -1更改为negative,将所有0值更改为neutral,将所有值> = 1更改为positive。但是,下面的代码为“负”产生以下错误。

# Function to change values to labels

test.loc[test['sentiment_score'] > 0, 'sentiment_score'] = 'positive'
test.loc[test['sentiment_score'] == 0, 'sentiment_score'] = 'neutral'
test.loc[test['sentiment_score'] < 0, 'sentiment_score'] = 'negative'

Data:                                  Data After Code:
Index     Sentiment                    Index     Sentiment
 0         2                            0         positive
 1         0                            1         neutral
 2        -3                            2         -3
 3         4                            3         positive
 4        -1                            4         -1
 ...                                    ...
 k         5                            k         positive
Run Code Online (Sandbox Code Playgroud)

pandas._libs.ops.scalar_compare TypeError中的文件“ pandas_libs \ ops.pyx”,行98,TypeError:'str'和'int实例之间不支持'<='

我认为这与将负数视为字符串而不是float / int的函数有关,但是我尝试了以下代码来更正此错误,并且它什么都不会改变。任何帮助,将不胜感激。

test['sentiment_score'] = test['sentiment_score'].astype(float) …
Run Code Online (Sandbox Code Playgroud)

python pandas

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

Python - 使用 .mean() 和 .agg() 对多列进行分组

我想对三列进行分组,然后找到在前三列中重复的所有行的第四个数字列的平均值。我可以通过以下功能实现这一点:

df2 = df.groupby(['col1', 'col2', 'col3'], as_index=False)['col4'].mean()
Run Code Online (Sandbox Code Playgroud)

问题是我还想要第五列,它将聚合由 groupby 函数分组的所有行,我不知道如何在前一个函数之上执行此操作。例如:

df 
index    col1        col2       col3       col4       col5
0        Week_1      James      John       1          when and why?
1        Week_1      James      John       3          How?
2        Week_2      James      John       2          Do you know when?
3        Week_2      Mark       Jim        3          What time?
4        Week_2      Andrew     Simon      1          How far is it?
5        Week_2      Andrew     Simon      2          Are you going?


CURRENT(with above function):
index    col1        col2       col3       col4
0        Week_1      James      John       2
1        Week_2      James …
Run Code Online (Sandbox Code Playgroud)

python aggregate mean pandas

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

NLTK的Vader评分文字示例

我希望有人能纠正我对VADER如何评分文字的理解。我在这里已阅读了此过程的说明,但是在重新创建它描述的过程时,我无法将测试句子的综合得分与Vader的输出相匹配。假设我们有这样的句子:

"I like using VADER, its a fun tool to use"
Run Code Online (Sandbox Code Playgroud)

VADER拿起的单词是“喜欢”(+1.5分)和“有趣”(+2.3分)。根据文档,将这些值相加(等于+3.8),然后使用以下函数将其标准化为0到1之间的范围:

(alpha = 15)
x / x2 + alpha 
Run Code Online (Sandbox Code Playgroud)

根据我们的数字,这应该变成:

3.8 / 14.44 + 15 = 0.1290
Run Code Online (Sandbox Code Playgroud)

但是,VADER输出的复合分数如下:

Scores: {'neg': 0.0, 'neu': 0.508, 'pos': 0.492, 'compound': 0.7003}
Run Code Online (Sandbox Code Playgroud)

我的推理哪里出错了?曾多次问过类似的问题,但是尚未提供VADER分类的实际示例。任何帮助,将不胜感激。

python nlp nltk lexicon vader

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

标签 统计

python ×6

pandas ×3

dictionary ×2

list ×2

aggregate ×1

key ×1

lexicon ×1

mean ×1

nested ×1

network-analysis ×1

nlp ×1

nltk ×1

string ×1

vader ×1