我有一本这样的字典:
{'1956': 21,
'2188': 76,
'1307': 9,
'1305': 22,
'2196': 64,
'3161': 1,
'1025': 22,
'321': 60,
'1959': 1,
'1342': 7,
'3264': 2}
Run Code Online (Sandbox Code Playgroud)
如果每个键都是唯一的,我想从字典中获取值大于 60 的键。
输出看起来像:
['2188','2196']
Run Code Online (Sandbox Code Playgroud)
我可以使用带有if条件的get('key')进行循环迭代来完成此操作,但这是一个漫长的过程,更有效地执行此操作的捷径是什么?
我有一个像这样的数据框:
df:
col1 col2
A blue berry
B nice water bottle
Run Code Online (Sandbox Code Playgroud)
我想从 col2 值中删除第一个单词,最终的数据框将如下所示:
df1:
col1 col2
A berry
B water bottle
Run Code Online (Sandbox Code Playgroud)
如何使用 pandas 以最有效的方式做到这一点
我有一个像这样的数据框:
df
col1 col2
A 1
B 1
C 2
D 3
D 2
B 1
D 5
Run Code Online (Sandbox Code Playgroud)
我已经看到带有 B 和 D 的 col1 值在数据框中出现了不止一次。
我想保留那些出现次数超过一次的值,最终的数据框将如下所示:
col1 col2
B 1
D 3
D 2
B 1
D 5
Run Code Online (Sandbox Code Playgroud)
如何使用 pandas/python 以最有效的方式做到这一点?
我正在尝试从网站上抓取数据,有一个网址可以让我进入特定页面,那里有一些项目的链接,如果我点击这些链接,它会在一个新选项卡中打开,我可以从那里提取数据,
但是提取数据后,我想关闭选项卡返回主页并单击另一个链接。
我正在使用 selenium 和 chrome web 驱动程序。我尝试过以下代码:
#links which lands me to a new tab
browser.find_element_by_xpath('//*[@id="data"]/div[1]/div[2]/a').click()
browser.switch_to.window(browser.window_handles[0])
browser.close() #it closes the main page, not the new tab I want
and following code,
browser.find_element_by_css_selector('body').send_keys(Keys.CONTROL + 'w') #this code didn't work.
Run Code Online (Sandbox Code Playgroud)
如何使用 selenium 和 python 关闭新建的选项卡?
python selenium web-scraping selenium-chromedriver selenium-webdriver
我有一个这样的清单:
l=[1,2,2,3,4,5,5,5]
Run Code Online (Sandbox Code Playgroud)
我们可以看到列表列表包含 5 个唯一值,总共有 8 个值。
我想要列表格式的列表中唯一值的索引。
所以输出看起来像:
indexes=[0,1,3,4,5]
Run Code Online (Sandbox Code Playgroud)
如何使用 python 以最有效的方式做到这一点?
我有一个这样的数据框,
df
col1 col2
A 'the value is zero'
B 'this is a cat'
C 'the value is one'
D 'nothing is here'
E 'the colour is blue'
F 'this is dog'
G 'empty sequence'
H 'the colour is red'
I 'the colour is green' 1
Run Code Online (Sandbox Code Playgroud)
现在我想要类似类型的字符串标记为 1,其他标记为零,所以最终的数据框应该是这样的,
col1 col2 col1
A 'the value is zero' 1
B 'this is a cat' 1
C 'the value is one' 1
D 'nothing is here' 0
E 'the colour is blue' 1 …Run Code Online (Sandbox Code Playgroud) 我有一个这样的数据框,
df
col1 col2 col3
1 2 3
2 5 6
7 8 9
10 11 12
11 12 13
13 14 15
14 15 16
Run Code Online (Sandbox Code Playgroud)
现在,当两个连续行的 col1 差异大于 1 时,我想从上面创建多个数据框。因此结果数据框将如下所示,
df1
col1 col2 col3
1 2 3
2 5 6
df2
col1 col2 col3
7 8 9
df3
col1 col2 col3
10 11 12
11 12 13
df4
col1 col2 col3
13 14 15
14 15 16
Run Code Online (Sandbox Code Playgroud)
我可以使用 for 循环并存储索引来做到这一点,但这会增加执行时间,寻找一些 pandas 快捷方式或 pythonic 方法来最有效地做到这一点。
我有一个这样的数据框,
df
col1 col2
A 1
B 3
C 2
D 5
E 6
F 8
G 10
Run Code Online (Sandbox Code Playgroud)
我想添加 col2 的特定值的上一个和下一个 n 值并将其存储到一个新列中,
所以,如果 n=2,那么数据框应该是这样的,
col1 col2 col3
A 1 6 (only below 2 values are there no upper values, so adding 3 numbers)
B 3 11 (adding one prev, current and next two)
C 2 17(adding all 4 values)
D 5 24(same as above)
E 6 31(same as above)
F 8 29(adding two prev and next one as only …Run Code Online (Sandbox Code Playgroud) 我有两个清单,
l1 = [1,4,3,2,5]
l2 = [4,3,2,10]
Run Code Online (Sandbox Code Playgroud)
现在我想找到列表之间的公共元素,所以我使用以下代码,
list(set(l1).intersection(set(l2)))
>> [2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
但它改变了 的顺序l1,我不想改变顺序所以结果应该是,
>> [4, 3, 2]
Run Code Online (Sandbox Code Playgroud)
寻找在不改变顺序的情况下执行此操作的最快方法?
我想使用循环将元素存储在 csv 文件中,例如,
for i in range(0,10):
#I want to append each i values in new rows of that csv file.
Run Code Online (Sandbox Code Playgroud)
输出的最终 csv 文件看起来像,
0
1
2
3
4
5
7
8
9
Run Code Online (Sandbox Code Playgroud)
如何高效地做到这一点?
我有一个这样的清单
l=[1,1,1,1,2,2,2,3,3,3,3,3,3,4,4,5,6,6,6,5,5,5,7,7,8,8,8,8,8,9,9,9,10,10]
Run Code Online (Sandbox Code Playgroud)
现在我只想连续选择一个重复值,所以输出应该是这样的,
l=[1,2,3,4,5,6,5,7,8,9,10]
Run Code Online (Sandbox Code Playgroud)
我可以使用 for 循环检查下一个值和前一个值并附加到列表中,但在这种情况下执行时间很长,寻找最有效的快捷方式。
python ×11
pandas ×5
dataframe ×4
list ×3
dictionary ×2
csv ×1
file ×1
file-io ×1
fuzzy-search ×1
indexing ×1
iterator ×1
key-value ×1
python-3.x ×1
rolling-sum ×1
selenium ×1
web-scraping ×1