我想对具有相同名称开头的列进行求和。
例子 :
import pandas as pd
import numpy as np
df=pd.DataFrame({'product':['TV','COMPUTER','SMARTPHONE'],
'price_2012':np.random.randint(100,300,3),
'price_2013':np.random.randint(100,300,3),
'price_2014':np.random.randint(100,300,3),
'price_2015':np.random.randint(100,300,3),
'price_2016':np.random.randint(100,300,3)})
Run Code Online (Sandbox Code Playgroud)
对于这个例子,我想创建一个新列price_2012_2016,等于2013年到2016年的价格总和,而不列出所有列。
PS:在SAS中我喜欢这样:price_2012_2016=sum(of prix_2012-prix-2016);
诚挚的,劳伦特 A.
小智 8
您可以简单地执行以下操作:
df['price_2012_2016'] = df[[col for col in df.columns if col.startswith('price_')]].sum(axis=1)
Run Code Online (Sandbox Code Playgroud)
这仅对df DataFrame中以“price_”开头的列进行求和,并将结果保存为price_2012_2016列。axis =1参数用于在列轴而不是行上计算总和,请参见下文:
| 归档时间: |
|
| 查看次数: |
6229 次 |
| 最近记录: |