Python 基于以前的列集创建多个新列(效率更高)
对于我的数据集,我想创建一些新列。这些列由一个比率组成,该比率基于其他两列。以下是我的意思的一个例子:Python 基于以前的列集创建多个新列(效率更高),python,pandas,dataframe,Python,Pandas,Dataframe,对于我的数据集,我想创建一些新列。这些列由一个比率组成,该比率基于其他两列。以下是我的意思的一个例子: import random col1=[0,0,0,0,2,4,6,0,0,0,100,200,300,400] col2=[0,0,0,0,4,6,8,0,0,0,200,900,400, 500] d = {'Unit': [1, 1, 1, 1, 2, 2, 2, 3, 4, 5, 6, 6, 6, 6], 'Year': [2014, 2015, 2016, 2017, 201
import random
col1=[0,0,0,0,2,4,6,0,0,0,100,200,300,400]
col2=[0,0,0,0,4,6,8,0,0,0,200,900,400, 500]
d = {'Unit': [1, 1, 1, 1, 2, 2, 2, 3, 4, 5, 6, 6, 6, 6],
'Year': [2014, 2015, 2016, 2017, 2015, 2016, 2017, 2017, 2014, 2015, 2014, 2015, 2016, 2017], 'col1' : col1, 'col2' : col2 }
df = pd.DataFrame(data=d)
new_df = df.groupby(['Unit', 'Year']).sum()
new_df['col1/col2'] = (new_df.groupby(level=0, group_keys=False)
.apply(lambda x: x.col1/x.col2.shift())
)
col1 col2 col1/col2
Unit Year
1 2014 0 0 NaN
2015 0 0 NaN
2016 0 0 NaN
2017 0 0 NaN
2 2015 2 4 NaN
2016 4 6 1.000000
2017 6 8 1.000000
3 2017 0 0 NaN
4 2014 0 0 NaN
5 2015 0 0 NaN
6 2014 100 200 NaN
2015 200 900 1.000000
2016 300 400 0.333333
2017 400 500 1.000000
然而,这是一个超级简化的df。事实上,我有感冒1到50。我觉得我现在的工作效率非常低:
col1=[0,0,0,0,2,4,6,0,0,0,100,200,300,400]
col2=[0,0,0,0,4,6,8,0,0,0,200,900,400, 500]
col3=[0,0,0,0,4,6,8,0,0,0,200,900,400, 500]
col4=[0,0,0,0,4,6,8,0,0,0,200,900,400, 500]
col5=[0,0,0,0,4,6,8,0,0,0,200,900,400, 500]
col6=[0,0,0,0,4,6,8,0,0,0,200,900,400, 500]
# data in all cols is the same, just for example.
d = {'Unit': [1, 1, 1, 1, 2, 2, 2, 3, 4, 5, 6, 6, 6, 6],
'Year': [2014, 2015, 2016, 2017, 2015, 2016, 2017, 2017, 2014, 2015, 2014, 2015, 2016, 2017], 'col1' : col1, 'col2' : col2, 'col3' : col3, 'col4' : col4, 'col5' : col5, 'col6' : col6}
df = pd.DataFrame(data=d)
new_df = df.groupby(['Unit', 'Year']).sum()
new_df['col1/col2'] = (new_df.groupby(level=0, group_keys=False)
.apply(lambda x: x.col1/x.col2.shift())
)
new_df['col3/col4'] = (new_df.groupby(level=0, group_keys=False)
.apply(lambda x: x.col3/x.col4.shift())
)
new_df['col5/col6'] = (new_df.groupby(level=0, group_keys=False)
.apply(lambda x: x.col5/x.col6.shift())
)
我对创建新列的方法执行了25次。这样做能更有效率吗/
提前谢谢大家,
JenIdea用于列表cols2
中的所有列,并用过滤后的数据框除以列表cols1
:
col1=[0,0,0,0,2,4,6,0,0,0,100,200,300,400]
col2=[0,0,0,0,4,6,8,0,0,0,200,900,400, 500]
d = {'Unit': [1, 1, 1, 1, 2, 2, 2, 3, 4, 5, 6, 6, 6, 6],
'Year': [2014, 2015, 2016, 2017, 2015, 2016, 2017, 2017, 2014, 2015, 2014, 2015, 2016, 2017],
'col1' : col1, 'col2' : col2 ,
'col3' : col1, 'col4' : col2 ,
'col5' : col1, 'col6' : col2 }
df = pd.DataFrame(data=d)
new_df = df.groupby(['Unit', 'Year']).sum()
cols1 = ['col1','col3','col5']
cols2 = ['col2','col4','col6']
new_df = new_df[cols1] / new_df.groupby(level=0)[cols2].shift().values
new_df.columns = [f'{a}/{b}' for a, b in zip(cols1, cols2)]
print (new_df)
col1/col2 col3/col4 col5/col6
Unit Year
1 2014 NaN NaN NaN
2015 NaN NaN NaN
2016 NaN NaN NaN
2017 NaN NaN NaN
2 2015 NaN NaN NaN
2016 1.000000 1.000000 1.000000
2017 1.000000 1.000000 1.000000
3 2017 NaN NaN NaN
4 2014 NaN NaN NaN
5 2015 NaN NaN NaN
6 2014 NaN NaN NaN
2015 1.000000 1.000000 1.000000
2016 0.333333 0.333333 0.333333
2017 1.000000 1.000000 1.000000
你想过用Numpy吗?熊猫实际上是以Numpy为基础的 这就是为什么它工作得这么快。DFs令人惊讶,但对于更深入或更复杂的操作,我只需将其转换为Numpy,然后使用它并转换回pandas:
...
new_df = df.groupby(['Unit', 'Year']).sum()
new_array = new_df.values
print(type(new_array))
[out]: <type 'numpy.ndarray'>
。。。
new_df=df.groupby(['Unit','Year']).sum()
新数组=新值
打印(类型(新数组))
[out]:
祝你好运但是你的答案是错误的,没有像need OP.lol那样工作,错误的答案打开了通往惊人Numpy功能的大门?您可以说它在您的情况下没有用处,但是说Numpy在处理数组时是一个糟糕的选择,这只是对我的“不精确”(如果您愿意)答案的错误回答。OP需要问题的答案,不幸的是,这不是OP的答案。