我正在尝试通过将字符串的一部分分组为一列来构建一个新的数据框。
import pandas
df = pandas.DataFrame([{'A': 'string_300_bla1', 'B': "Hi", 'C': 3},
{'A': 'string_300_blaa2', 'B': "Hello", 'C': 4},
{'A': 'string_487_blaaa1', 'B': "nice", 'C': 9},
{'A': 'string_487_blaaa2', 'B': "day", 'C': 6}])
我想从字符串的这一部分进行分组
string_ 300 _bla1
我尝试过:
import re
dfs = df['A'].str.contains('.*_\d+_.*', re.IGNORECASE).groupby(df['B'])
我的输出:
<pandas.core.groupby.generic.SeriesGroupBy object at 0x00000279EFD009E8>
好的输出:
dfs = pandas.DataFrame([{'A': 'string_300', 'B': "Hi\n\nHello"},
{'A': 'string_487', 'B': "nice\n\nday"}])
答案 0 :(得分:5)
我们可以做到:
import React, { PureComponent } from 'react'
import "./Card.css"
class Card extends PureComponent {
constructor(props) {
super(props)
this.state = {
active:false
}
this.toggleClass=this.toggleClass.bind(this)
this.handleClick=this.handleClick.bind(this)
}
toggleClass(){
const currentState = this.state.active;
this.setState({ active: !currentState });
}
handleClick(){
this.props.delete();
}
render() {
return (
<div className="cardbox">
<div className="cardtitle">{this.props.title}</div>
<div>{this.props.category}</div>
<div>{this.props.likes / this.props.dislikes}</div>
<i onClick={this.toggleClass} className={this.state.active?"fa fa-thumbs-up":"fa fa-thumbs-down"}></i>
<button onClick={this.handleClick}>Delete</button>
</div>
)
}
输出:
(df.groupby(df.A.str.extract('(\w+_\d+)')[0])
.agg({'B':'\n\n'.join, 'C':'sum'})
.reset_index()
)
@CharlesGleason指出,这是提取数字部分的解决方案:
0 B C
0 string_300 Hi\n\nHello 7
1 string_487 nice\n\nday 15
答案 1 :(得分:4)
您可以使用ERROR: query has no destination for result data
HINT: If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT: PL/pgSQL function iweekday(character varying) line 4 at SQL statement
str.rsplit