我知道这个问题已经在很多地方被问过,但我没有看到确切的答案。
所以我试图借助正则表达式从 R 中的字符串(“尝试”)中准确提取第二个单词。我不想使用 unlist(strsplit)
sen= "I am trying to substring here something, but I am not able to"
str_extract(sen, "trying to\\W*\\s+((?:\\S+\\s*){2})")
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想将“此处”作为输出,但我得到“尝试在此处进行子串”
我有一个像下面这样的组,我怎样才能知道每个观察值与其组最小值的差异
GROUP VALUE
1 5
2 2
1 10
2 20
1 7
Run Code Online (Sandbox Code Playgroud)
所以,我想要的输出应该是这样的
GROUP VALUE diff
1 5 3
2 2 0
1 10 5
2 20 18
1 7 5
Run Code Online (Sandbox Code Playgroud)
我怎样才能在 pandas 的帮助下实现它
感谢所有的帮助
我正在尝试从 Pandas 数据帧创建散点图,但我不想使用 matplotlib plt。以下是脚本
df:
group people value
1 5 100
2 2 90
1 10 80
2 20 40
1 7 10
Run Code Online (Sandbox Code Playgroud)
我想在 x 轴上创建一个带有索引的散点图,只使用熊猫数据框
df.plot.scatter(x = df.index, y = df.value)
Run Code Online (Sandbox Code Playgroud)
它给了我一个错误
Int64Index([0, 1, 2, 3, 4], dtype='int64') not in index
Run Code Online (Sandbox Code Playgroud)
我不想使用
plt.scatter(x = df.index, y = df.value)
Run Code Online (Sandbox Code Playgroud)
如何使用熊猫数据框执行此图
我使用带有if语句的Python正则表达式:如果匹配为None,则应转到else子句。但它显示此错误:
AttributeError: 'NoneType' object has no attribute 'group'
脚本是:
import string
chars = re.escape(string.punctuation)
sub='FW: Re: 29699'
if re.search("^FW: (\w{10})",sub).group(1) is not None :
d=re.search("^FW: (\w{10})",sub).group(1)
else:
a=re.sub(r'['+chars+']', ' ',sub)
d='_'.join(a.split())
Run Code Online (Sandbox Code Playgroud)
每个帮助都是巨大的帮助!
我有一个以下示例 pyspark 数据帧,在 groupby 之后我想计算平均值,以及多列中的第一个,在实际情况下,我有 100 个列,所以我无法单独执行此操作
sp = spark.createDataFrame([['a',2,4,'cc','anc'], ['a',4,7,'cd','abc'], ['b',6,0,'as','asd'], ['b', 2, 4, 'ad','acb'],
['c', 4, 4, 'sd','acc']], ['id', 'col1', 'col2','col3', 'col4'])
+---+----+----+----+----+
| id|col1|col2|col3|col4|
+---+----+----+----+----+
| a| 2| 4| cc| anc|
| a| 4| 7| cd| abc|
| b| 6| 0| as| asd|
| b| 2| 4| ad| acb|
| c| 4| 4| sd| acc|
+---+----+----+----+----+
Run Code Online (Sandbox Code Playgroud)
这就是我正在尝试的
mean_cols = ['col1', 'col2']
first_cols = ['col3', 'col4']
sc.groupby('id').agg(*[ f.mean for col in mean_cols], *[f.first for col in first_cols])
Run Code Online (Sandbox Code Playgroud)
但它不起作用。我怎样才能用 …
我有以下两个数据框
Data Set A
ID type msg
1 High Lets do
2 Low whats it
3 Medium thats it
Data Set B
ID Accounttype
2 Facebook
3 Linkedin
Run Code Online (Sandbox Code Playgroud)
我如何在加入熊猫的帮助下获得更新的表,它看起来应该像
Updated DatasetA
ID Account type msg
1 High Lets do
2 Facebook Low whats it
3 Linkedin Medium thats it
Run Code Online (Sandbox Code Playgroud)
我可以轻松地在SQL中使用Update和内部联接来完成它,如何在pandas中执行它,我试图做到这一点,但是大多数操作都是针对append / merge的。任何帮助将不胜感激
我正在阅读一个带有pd.read_html的HTML表,但结果出现在列表中,我想将其转换为pandas数据帧,因此我可以继续进行相同的操作.我使用以下脚本
import pandas as pd
import html5lib
data=pd.read_html('http://www.espn.com/nhl/statistics/player/_/stat/points/sort/points/year/2015/seasontype/2',skiprows=1)
Run Code Online (Sandbox Code Playgroud)
由于我的结果是1列表,我试图将其转换为数据框
data1=pd.DataFrame(Data)
Run Code Online (Sandbox Code Playgroud)
结果为0
0 0 1 2 3 4...
Run Code Online (Sandbox Code Playgroud)
并且由于结果作为列表,我不能应用任何函数,如rename,dropna,drop.
我将感激你的每一个帮助
我正在使用 Pandas 从我的 sql server 读取表,例如
df= pd.read_sql('table1', engine)
Run Code Online (Sandbox Code Playgroud)
引擎是我的 pyodbc 连接,然后我再次将它推送到 sql server
df.to_sql('table2', engine, if_exists='replace')
Run Code Online (Sandbox Code Playgroud)
这给了我一个错误
ValueError: duplicate name in index/columns: cannot insert level_0, already exists
Run Code Online (Sandbox Code Playgroud)
当我尝试删除该列时,它又给了我一些错误,这无论如何都不是一种有效的方法。我也试过这个,这也不起作用
df= df.reset_index(drop=True)
Run Code Online (Sandbox Code Playgroud)
每一个帮助都很重要
我有一个只包含数字列的pandas数据框,并且我正在尝试为所有功能创建单独的直方图
ind group people value value_50
1 1 5 100 1
1 2 2 90 1
2 1 10 80 1
2 2 20 40 0
3 1 7 10 0
3 2 23 30 0
Run Code Online (Sandbox Code Playgroud)
但是在我的现实生活数据中有50多个列,如何为所有列创建单独的图
我试过了
df.plot.hist( subplots = True, grid = True)
Run Code Online (Sandbox Code Playgroud)
它给了我一个重叠的不清楚的情节。
如何使用pandas subplots = True排列它们。下面的示例可以帮助我获取(2,2)网格中四列的图形。但对于所有50列来说,这都是一个漫长的方法
fig, [(ax1,ax2),(ax3,ax4)] = plt.subplots(2,2, figsize = (20,10))
Run Code Online (Sandbox Code Playgroud) 我试图在R中创建一个hashtag提取函数.这个函数将从一个帖子中提取一个hashtags,如果有的话,否则会给出一个空白.我的功能就像
hashtag_extract= function(text){
match = str_extract_all(text,"#\\S+")
if (match) {
return match
}else{
return ''}}
String="#letsdoit #Tonewbeginnign world is on a new#route
Run Code Online (Sandbox Code Playgroud)
但我的功能不起作用,显示了大量的错误.就像第一个错误一样
Error: unexpected symbol in:
" if (match) {
return match"
Run Code Online (Sandbox Code Playgroud)
所以我想把它作为
hashatag_extract(string)
Run Code Online (Sandbox Code Playgroud)
答案应该是这样的
#letsdoit ##Tonewbeginnign #route
Run Code Online (Sandbox Code Playgroud)
最后我将使用sapply在整列上应用此函数,这就是If部分很重要的原因.请忽略我对R的缩进,因为它对R不重要,但每个建议都会有所帮助