小编Jyo*_*hsa的帖子

从seaborn图中删除图例部分

使用“tips”数据集作为玩具模型,我生成了以下图:

import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")

cmap = sns.cubehelix_palette(dark=.3, light=.8, as_cmap=True)
g = sns.scatterplot(x="total_bill", y="sex", hue="smoker", size = 'tip',sizes=(320, 600), data=tips)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0., fontsize=13)
plt.show(g)
Run Code Online (Sandbox Code Playgroud)

这张图片正是我所需要的。但是,我想从图例中删除size = 'tip'并只保留吸烟者。本质上,删除那些标记为 0.0 到 12.0 的黑色圆圈。如何确保我的图例只有我选择的一个变量?

在此输入图像描述

python matplotlib legend seaborn

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

由 TF-IDF Vectorizer 函数构建的词云

我有一个名为corpus我正在尝试使用sklearn内置函数的TF-IDF的列表。该列表有 5 个项目。这些项目中的每一个都来自文本文件。我为这个例子生成了一个名为 corpus 的玩具列表。

corpus = ['Hi what are you accepting here do you accept me',
'What are you thinking about getting today',
'Give me your password to get accepted into this school',
'The man went to the tree to get his sword back',
'go away to a far away place in a foreign land']

vectorizer = TfidfVectorizer(stop_words='english')
vecs = vectorizer.fit_transform(corpus)
feature_names = vectorizer.get_feature_names()
dense = vecs.todense()
lst1 = dense.tolist()
df = pd.DataFrame(lst1, columns=feature_names)
df …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

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

删除所有缺失值的变量

我的数据集中有5000变量和观察结果。91,534

我想删除所有缺少所有值的变量:

X1     X2    X3
1      2      .
.      3      .
3      .      .
.      5      .
Run Code Online (Sandbox Code Playgroud)

X1     X2
1      2  
.      3   
3      . 
.      5  
Run Code Online (Sandbox Code Playgroud)

我尝试使用dropmiss 社区贡献的命令,但即使在阅读帮助文件后,它似乎对我不起作用。例如:

dropmiss 
command dropmiss is unrecognized
r(199);

missings dropvars
force option required with changed dataset
Run Code Online (Sandbox Code Playgroud)

相反,按照解决方案之一的建议,我尝试了以下方法:

ssc install nmissing
nmissing, min(91534)  
drop `r(varlist)'
Run Code Online (Sandbox Code Playgroud)

这个社区提供的替代命令似乎对我有用。

但是,我想知道是否有更优雅的解决方案,或者使用dropmiss.

stata stata-macros

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

用一定范围内的随机数替换数据框中的NA

我将以下数据框命名为 cars

Brand      year     mpg        reputation      Luxury
Honda      2010     30            8.5            0.5
Honda      2011     28            8.5            0.6
Dodge      2010     20            6.5            0.6
Dodge      2011     23            7.0            0.7
Mercedes   2010     22            9.5            NA
Mercedes   2011     25            9.0            NA
Run Code Online (Sandbox Code Playgroud)

我想用随机产生的实数替换NA之间 0.9 and 1.0

我正在尝试以下操作,但是它用数字0.9代替了NA。

cars[is.na(cars)] <-  sample(0.9:1, sum(is.na(cars)),replace=TRUE)
Run Code Online (Sandbox Code Playgroud)

数据表将如下所示:

Brand      year     mpg        reputation      Luxury
Honda      2010     30            8.5            0.5
Honda      2011     28            8.5            0.6
Dodge      2010     20            6.5            0.6
Dodge      2011     23            7.0            0.7
Mercedes   2010     22            9.5           *0.91*
Mercedes   2011     25 …
Run Code Online (Sandbox Code Playgroud)

r dataframe

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

将数据框中的上下三角值替换为零,或仅保留对角线值

我有以下DataFrame作为玩具示例:

a = [5,2,6,8]
b = [2,10,19,16]
c = [3,8,15,17]
d = [3,8,12,20]
df  = pd.DataFrame([a,b,c,d], columns = ['a','b','c','d'])
df
Run Code Online (Sandbox Code Playgroud)

我想创建一个df1仅保留对角元素并将新的上下三角值转换为零的新DataFrame 。

我的最终数据集应如下所示:

    a   b   c   d
0   5   0   0   0
1   0   10  0   0
2   0   0   15  0
3   0   0   0   20
Run Code Online (Sandbox Code Playgroud)

python pandas

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

从列表中的句子中删除单个字母单词

我有以下清单:

ip= ['a boy called me z there', 'what u doing b over there ', "come w me t the end']
Run Code Online (Sandbox Code Playgroud)

我想从列表中的每个字符串中删除所有单个字母。

我已经尝试了以下但它不起作用:

x = [[w for w in c if (len(w)>1)] for c in ip]
Run Code Online (Sandbox Code Playgroud)

我想转换我的ip这样我得到以下输出op

op= ['boy called me there', 'what doing over there ', "come me the end']
Run Code Online (Sandbox Code Playgroud)

python python-3.x

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

添加值高于特定值的列数 pandas

假设我有以下玩具模型df

product    customer1    customer2    customer3      
apple           40           110          120
banana         200           150          180
coconut         10             5           25
daq            120            10           30
eclair          45           190           35
Run Code Online (Sandbox Code Playgroud)

我想添加一列来df统计购买至少一百种所列商品的客户数量:

product    customer1    customer2    customer3   atleast100    
apple           40           110          120             2
banana         200           150          180             3
coconut         10             5           25             0
daq            120            10           30             1
eclair          45           190           35             1
Run Code Online (Sandbox Code Playgroud)

python pandas

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

将 df1 中所有列的值减去 df2 中一列中的值

假设我有以下数据框df1

 a    b    c    d
 10   15   20   25
 8    18   28   38
 20   25   30   35
Run Code Online (Sandbox Code Playgroud)

为简单起见,假设我有一个数据集df2

 y
 1
 2
 3
Run Code Online (Sandbox Code Playgroud)

我想df2从中的值中逐行减去中的值df1

因此,我的最终数据集df3= df1-df2将是:

  a    b    c    d
  9   14   19   24
  6   16   26   36
 17   22   27   32
Run Code Online (Sandbox Code Playgroud)

python pandas

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

标签 统计

python ×6

pandas ×3

python-3.x ×2

dataframe ×1

legend ×1

matplotlib ×1

r ×1

seaborn ×1

stata ×1

stata-macros ×1