我正在处理一个报告自动化任务,我使用了groupby函数,它生成了一个表。
function_d= {"AvgLoadCur": 'mean'}
newdf=df.groupby(['sitename']).agg(function_d)站点名AvgLoadCur Biocon-SEZD-66/ 11 24V 11 23.0 Biocon-GT 1 120 V DC 24.2 Biocon-GT 2 120 V DC 23.9 Biocon-SEZD-PLC 24V 21.4
df只包含4个站点名,因此groupby表也只包含这4个站点,如何追加存储在另一个数据站点‘sitename’中的两个站点名?
sitename
Biocon-SEZD-GT 1 120V DC
Biocon-SEZD-GT 2 120V DC
Biocon-SEZD-SCADA UPS
Biocon-SEZD-66/11KV SS 11
Biocon-SEZD-PLC 24V DC
BIOCON SEZ-HT PANEL 220 V最后的Dataframe应该看起来像
站点名AvgLoadCur Biocon-SEZD-66/ 11 SS 11 23.0 Biocon-GT 1 120 V DC 24.2 Biocon-GT 2 120 V DC 23.9 Biocon-SEZD-PLC 24V 21.4 Biocon-SEZD-HT面板220 V- Biocon-SEZD-SCADA UPS
简而言之,如何从另一个dataframe中追加不存在于groupby表中的元素
组表:
Fruit Price
apple 34df表
Fruit
--------
apple
orange最终群表
Fruit Price
apple 34
orange --发布于 2020-05-20 09:43:46
我希望这能回答你的问题:
df = pd.DataFrame([['apple',1],['orange',2]],columns=['Fruit','Price'])
df2 = pd.DataFrame(['guava','apple','orange'],columns=['Fruit'])
for value in df2['Fruit'].values:
if value not in df['Fruit'].values:
df = df.append({'Fruit':value,'Price':'--'},ignore_index=True)
df输出
Fruit Price
0 apple 1
1 orange 2
2 guava --发布于 2020-05-20 09:50:23
您可以首先合并您的数据,然后组。
df = pd.DataFrame({'Fruit': {0: 'apple'}, 'Price': {0: 34}})
df2 = pd.DataFrame({'Fruit': {0: 'apple', 1: 'orange'}})
(
pd.merge(df,df2,on='Fruit',how='right')
.groupby('Fruit')
.agg(avg=('Price', 'mean'))
.reset_index()
)
Fruit avg
0 apple 34.0
1 orange NaNhttps://stackoverflow.com/questions/61909614
复制相似问题