当使用seaborn barplot时,我可以指定一个alpha,它使条形半透明.但是,当我用Seaborn regplot尝试这个时,我得到一个错误,说这是一个意想不到的争论.
我在线阅读文档并没有找到太多,有人能指出我正确的方向......
基本上我试图与如何使用列名和列值从pandas DataFrame生成列表相反?
借用这个例子,我想从形式出发:
data = [['Name','Rank','Complete'],
['one', 1, 1],
['two', 2, 1],
['three', 3, 1],
['four', 4, 1],
['five', 5, 1]]
Run Code Online (Sandbox Code Playgroud)
应该输出:
Name Rank Complete
One 1 1
Two 2 1
Three 3 1
Four 4 1
Five 5 1
Run Code Online (Sandbox Code Playgroud)
但是当我做类似的事情时:
pd.DataFrame(data)
Run Code Online (Sandbox Code Playgroud)
我得到一个数据框,其中第一个列表应该是我的colnames,然后每个列表的第一个元素应该是rowname
编辑:
To clarify, I want the first element of each list to be the row name. I am scrapping data so it is formatted this way...
我有一个熊猫数据框,类似于:
ColA ColB
1 1
1 1
1 1
1 2
1 2
2 1
3 2
Run Code Online (Sandbox Code Playgroud)
我想要一个与Counter具有相同功能的输出。我需要知道每行出现多少次(所有列都相同)。
在这种情况下,正确的输出将是:
ColA ColB Count
1 1 3
1 2 2
2 1 1
3 2 1
Run Code Online (Sandbox Code Playgroud)
我已经尝试过某种方式了:
df.groupby(['ColA','ColB']).ColA.count()
Run Code Online (Sandbox Code Playgroud)
但这给我一些难看的输出,我在格式化时遇到麻烦
抱歉,我刚刚问了这个问题: Pythonic Way to have multiple Or's whenconditioning in a dataframe但过早将其标记为已回答,因为它通过了我过于简单的测试用例,但不能更普遍地工作。(如果可以合并并重新打开这个问题,那就太好了......)
这是完整的问题:
sum(data['Name'].isin(eligible_players))
> 0
sum(data['Name'] == "Antonio Brown")
> 68
"Antonio Brown" in eligible_players
> True
Run Code Online (Sandbox Code Playgroud)
基本上,如果我理解正确的话,我会展示安东尼奥·布朗在符合条件的球员中并且他在数据框中。但是,由于某种原因, .isin() 无法正常工作。
正如我在上一个问题中所说,我正在寻找一种方法来检查许多 ors 以选择正确的行
____ 编辑 ____
In[14]:
eligible_players
Out[14]:
Name
Antonio Brown 378
Demaryius Thomas 334
Jordy Nelson 319
Dez Bryant 309
Emmanuel Sanders 293
Odell Beckham 289
Julio Jones 288
Randall Cobb 284
Jeremy Maclin 267
T.Y. Hilton 255
Alshon Jeffery 252
Golden Tate 250
Mike Evans 236 …Run Code Online (Sandbox Code Playgroud) 正如问题所问,我有一个要保存的熊猫数据帧字典,这样下次我启动ipython笔记本时就不必重新采样数据了。我尝试过一些简单的方法,该方法以前在其他情况下都可以使用:
import json
with open('result.json', 'w') as fp:
json.dump(d, fp)
Run Code Online (Sandbox Code Playgroud)
但是我得到了这个错误:
[1001 rows x 6 columns] is not JSON serializable
Run Code Online (Sandbox Code Playgroud)
我认为这与我的熊猫数据框有关,但是任何帮助将不胜感激
由于我的数据要复杂得多,我制作了一个较小的样本数据集(我离开了重塑,以显示我是如何生成数据的).
set.seed(7)
x = rep(seq(2010,2014,1), each=4)
y = rep(seq(1,4,1), 5)
z = matrix(replicate(5, sample(c("A", "B", "C", "D"))))
temp_df = cbind.data.frame(x,y,z)
colnames(temp_df) = c("Year", "Rank", "ID")
head(temp_df)
require(reshape2)
dcast(temp_df, Year ~ Rank)
Run Code Online (Sandbox Code Playgroud)
这导致......
> dcast(temp_df, Year ~ Rank)
Using ID as value column: use value.var to override.
Year 1 2 3 4
1 2010 D B A C
2 2011 A C D B
3 2012 A B D C
4 2013 D A C B
5 2014 C A B …Run Code Online (Sandbox Code Playgroud) 如果我的输入文字是
a
b
c
d
e
f
g
Run Code Online (Sandbox Code Playgroud)
我希望我的输出文本是:(带双引号)
"a b c d e f g"
Run Code Online (Sandbox Code Playgroud)
这一步后我该去哪里:
" ".join([a.strip() for a in b.split("\n") if a])
Run Code Online (Sandbox Code Playgroud) 我有 2 个数据框。首先(我知道如何做到这一点)我想找到两个数据帧之间匹配的行(整行)。因此,我可以在 A 中创建一列,告诉我整行是否在 B 中。但是,我不知道该怎么做的部分是查找 B 中的哪些索引。
TL 博士;我需要在 A 中创建一个新列,如果整行不在 B 中,它会告诉我 FALSE,或者给我该行在 B 中的索引。
a = as.data.frame(matrix(data = 1:10,nrow=5))
b = as.data.frame(matrix(data = c(1:5,10,7,6,9,8), nrow=5))
set.seed(02138)
b = b[sample(nrow(b)),]
rownames(b) = 1:nrow(b)
a_ = do.call("paste", a[,1:2])
b_ = do.call("paste", b[,1:2])
# Gets me TRUE/FALSE of whether there is a complete row-wise match
a$InB = a_ %in% b_
# Gets me which rows they are in b
b[b_ %in% a_,]
# Now is where I need help …Run Code Online (Sandbox Code Playgroud) 这是我第一次使用 Selenium Web Driver(使用 Python),我试图转到搜索框,输入内容并点击提交。
相关的 HTML 如下所示:
<iframe id="mainFrame" class="" width="100%" height="" frameborder="0" title="Main content frame" onload="initFrame();" scrolling
....
<tr>
<td style="vertical-align:middle;">
<input id="terms" type="text" onblur="RevertText(this,'Enter Search Terms');" onfocus="AllowType(this,'Enter Search Terms')" value="Enter Search Terms" style="color: rgb(164, 164, 164);"></input>
</td>
<td style="vertical-align:middle;">
<input id="srchButt" class="buttAcdmicOne" type="submit" value="Search"></input>
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
我的代码如下所示:
driver.switch_to.frame("mainFrame")
elem = driver.find_element_by_id("terms")
elem.send_keys("Test")
Run Code Online (Sandbox Code Playgroud)
但这不起作用,我不知道为什么它不起作用......(可能有多个带有术语 id 的东西?)
编辑:
我收到的错误是:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"id","selector":"terms"}
Run Code Online (Sandbox Code Playgroud)
我收到的新错误是:
selenium.common.exceptions.NoSuchFrameException: Message: no such frame: element is not a frame
Run Code Online (Sandbox Code Playgroud)
<iframe id="mainFrame" …Run Code Online (Sandbox Code Playgroud)显示这个的最简单方法是一个例子:
def hat(a, b):
return a+b
def cat(c, d):
return c % 2, d % 2
Run Code Online (Sandbox Code Playgroud)
如果我输入cat(4,5)我正确得到(0,1).但是,如果我尝试帽子(猫(4,5))而不是1,我会得到一个错误,说帽子需要更多的值.在我的实际函数中,我处理的是2个以上的参数,所以解决这个问题的正确方法是什么
python ×8
pandas ×4
r ×2
automation ×1
counter ×1
function ×1
indexing ×1
json ×1
matching ×1
python-2.7 ×1
seaborn ×1
selenium ×1
string ×1
unique ×1
web-scraping ×1