我有以下数据框:
export class SearchMain extends React.Component<RouteComponentProps<{}>, State> {
constructor(props: any)
{
super(props);
this.state = {...};
...
}
...
updateTableData(searchParams: SearchParameters) {
this.makeRequest(searchParams);
}
handleShowCalendar(searchParams: SearchParameters) {
this.makeRequest(searchParams);
this.setState({
...this.state,
redirectToCalendar: true
});
}
public render() {
const { loading, showCalendar, redirectToCalendar, searchResults } = this.state;
if (redirectToCalendar)
return (<Redirect to={{
pathname: '/displayCalendar',
search: JSON.stringify(this.state.searchResults)
}} />)
return (<div>
<Row>
<Search search={(searchParams) =>
this.updateTableData(searchParams)} showCalendar={(searchParams) => this.handleShowCalendar(searchParams)} />
{searchResults && <DisplayResults searchResults={searchResults} /> }
</Row>
</div>);
}
我想重塑我的数据框(拆分行),如下所示:
data = {"start_date" : ["2018-04-30", "2018-05-01"]
,"end_date" : ["2018-05-01", "2018-05-02"]
,"budget" : [10, 12]}
df = pd.DataFrame(data)
df["start_date"] = pd.to_datetime(df["start_date"])
df["end_date"] = pd.to_datetime(df["end_date"])
df
budget end_date start_date
0 10 2018-05-01 2018-04-30
1 12 2018-05-02 2018-05-01
答案 0 :(得分:0)
解决方案基于这个主题:
python - Duplicate rows x number of times based on a value in a column
df['Number of days'] = (df['end_date']-df['start_date']).map(lambda x: x.days)
df = df.loc[df.index.repeat(df['Number of days']+1)]
df['days'] =(df.groupby(level=0)['start_date']
.transform(lambda x: pd.date_range(start=x.iat[0], periods=len(x))))
df['daily_average'] = df['budget']/(df['Number of days']+1)
df = df.reset_index(drop=True)
df = df[['daily_average', 'days', 'end_date', 'start_date']]