我有两个数据帧。 我想用df2中的ID名称在df1中创建列,并在其中分配df2中特定列的值,但要考虑日期。
数据说明:
df1
ID Date X
A 07/16/2019 ..
A 07/19/2019 ..
B 07/28/2019 ..
df2
ID2 Day Y P
123 07/16/2019 .. 1.5
123 07/19/2019 .. 2.0
123 07/28/2019 .. 3.0
456 07/16/2019 .. 1.8
456 07/19/2019 .. 2.1
456 07/28/2019 .. 3.2
789 07/16/2019 .. 1.6
789 07/19/2019 .. 4.5
预期结果(df1):
ID Date X 123_P 456_P 789_P
A 07/16/2019 .. 1.5 1.8 1.6
A 07/19/2019 .. 2.0 2.1 4.5
B 07/28/2019 .. 3.0 3.2 NaN
答案 0 :(得分:1)
#Setup
import pandas as pd
col = ["ID2", "Day","P"]
data = [
(123, '07/16/2019', 1.5),
(123, '07/19/2019', 2.0),
(123, '07/28/2019', 3.0),
(456, '07/16/2019', 1.8),
(456, '07/19/2019', 2.1),
(456, '07/28/2019', 3.2),
(789, '07/16/2019', 1.6),
(789, '07/19/2019', 4.5),
]
df2 = pd.DataFrame(data,columns=col)
#Solution
piv_col = list(df2['ID2'].unique())
piv_col_renamed = [str(col) + '_P' for col in piv_col]
rename_dict = dict(zip(piv_col, piv_col_renamed))
df2 = df2.pivot(index='Day',values='P', columns='ID2').reset_index()
df2.rename(rename_dict,axis=1,inplace=True)
merged_df = pd.merge(df1,df2, how='left',left_on='Date',right_on='Day')
答案 1 :(得分:1)
这是两个数据帧:
raw_data1 = {'ID': ['A', 'B', 'C'],
'Date':['07/16/2019', '07/19/2019', '07/28/2019'],
'X': ['x', 'x', 'x']}
df1 = pd.DataFrame(raw_data1)
raw_data2 = {'ID': [123, 123, 123, 456, 456, 456, 789, 789],
'Date': ['07/16/2019', '07/19/2019', '07/28/2019', '07/16/2019',
'07/19/2019', '07/28/2019', '07/16/2019', '07/19/2019'],
'P': [1.5, 2.0, 3.0, 1.8, 2.1, 3.2, 1.6, 4.5]}
df2 = pd.DataFrame(raw_data2)
我执行了以下步骤:
## first make a new column that will end up being your column header
df2['colheader'] = df2['ID'].astype(str) + 'P'
# next 'pivot' over Date as index, colheader as columns and P as values
df2 = df2.pivot(index='Date',columns='colheader',values='P')
# next 'clean up' the dataset - this makes the headers easier to read
df2.columns.name = None
df2 = df2.reset_index()
# now merge with df1
df = df1.merge(df2,left_on='Date', right_on='Date')
df
结果:
答案 2 :(得分:1)
这是使用 import React, { Component, Fragment } from "react";
import "./App.css";
export default class App extends Component {
constructor(props){
super(props)
this.state = {
data: [
{
id: 1,
Firstname: "Jill",
Lastname: ["john", "hobss", "smith"],
Age: [1, 2, 3],
company: ["facebook", "google", "netflix"],
skills: ["python", "java", "scala"]
},
{
id: 2,
Firstname: "Jill",
Lastname: ["john", "hobss", "smith"],
Age: [1, 2, 3],
company: ["facebook", "google", "netflix"],
skills: ["python", "java", "scala"]
},
{
id: 3,
Firstname: "Jill",
Lastname: ["john", "hobss", "smith"],
Age: [1, 2, 3],
company: ["facebook", "google", "netflix"],
skills: ["python", "java", "scala"]
},
{
id:4,
Firstname: "Jill",
Lastname: ["john", "hobss", "smith"],
Age: [1, 2, 3],
company: ["facebook", "google", "netflix"],
skills: ["python", "java", "scala"]
}
]
}
}
handleChange = (id, company, event) => {
const data = this.state.data;
for(let d of data){
if(d.id === id){
for(let c of d.company){
if(c === company){
c = event.target.value
}
}
}
}
}
render() {
return (
<div>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>company</th>
<th>skills</th>
</tr>
{
this.state.data.map(td => {
return (
<tr>
<td>{td.Firstname}</td>
<td>
<table>
<tr>
<td>{td.Lastname[0]}</td>
<td>{td.Lastname[1]}</td>
<td>{td.Lastname[2]}</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>{td.Age[0]}</td>
<td>{td.Age[1]}</td>
<td>{td.Age[2]}</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>
<input type="text" value={td.company[0]} onChange={(e) => this.handleChange(td.id, td.company[0], e)} />
</td>
<td>
<input type="text" value={td.company[1]} onChange={(e) => this.handleChange(td.id, td.company[1], e)}/>
</td>
<td>
<input type="text" value={td.company[2]} onChange={(e) => this.handleChange(td.id, td.company[2], e)}/>
</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>{td.skills[0]}</td>
<td>{td.skills[0]}</td>
<td>{td.skills[0]}</td>
</tr>
</table>
</td>
</tr>
)
})
}
</table>
</div>
)
}
}
和merge
的另一种方式:
unstack()
m=df.merge(df2,left_on='Date',right_on='Day').query('Date==Day').drop(['Day','Y'],1)
m=m.set_index(['ID','Date','X','ID2']).unstack().sort_index(level=1,axis=1)
m.columns=[f'{a}_{b}' for a,b in m.columns]
m=m.reset_index()
print(m)