I have a DataFrame of records that looks something like this:
我有一个类似这样的数据档案名:
stocks = pd.Series(['A', 'A', 'B', 'C', 'C'], name = 'stock')
positions = pd.Series([ 100, 200, 300, 400, 500], name = 'positions')
same1 = pd.Series(['AA', 'AA', 'BB', 'CC', 'CC'], name = 'same1')
same2 = pd.Series(['AAA', 'AAA', 'BBB', 'CCC', 'CCC'], name = 'same2')
diff = pd.Series(['A1', 'A2', 'B3' ,'C1', 'C2'], name = 'different')
df = pd.DataFrame([stocks, same1, positions, same2, diff]).T
df
This gives a pandas DataFrame that looks like
这就得到了一个看起来像熊猫的DataFrame
stock same1 positions same2 different
0 A AA 100 AAA A1
1 A AA 200 AAA A2
2 B BB 300 BBB B3
3 C CC 400 CCC C1
4 C CC 500 CCC C2
I'm not interested in the data in 'different' columns and want to sum the positions along the unique other columns. I am currently doing it by:
我对“不同”列中的数据不感兴趣,我想对沿着唯一其他列的位置求和。我目前正在这样做:
df.groupby(['stock','same1','same2'])['positions'].sum()
which gives:
这使:
stock same1 same2
A AA AAA 300
B BB BBB 300
C CC CCC 900
Name: positions
Problem is that this is a pd.Series (with Multi-Index). Currently I iterate over it to build a DataFrame again. I am sure that I am missing a method. Basically I want to drop 1 column from a DataFrame and then "rebuild it" so that one column is summed and the rest of the fields (which are the same) stay in place.
问题是这是一个pd。系列(多索引)。目前,我迭代它以再次构建一个DataFrame。我确信我错过了一个方法。基本上,我想从DataFrame中删除一列,然后“重新构建它”,以便对一列进行求和,其余的字段(它们是相同的)保持不变。
This groupby method breaks if there are empty positions. So I currently use an elaborate iteration over the DataFrame to build a new one. Is there a better approach?
如果位置为空,则此groupby方法将中断。因此,我目前在DataFrame上使用一个精心设计的迭代来构建一个新的迭代。有更好的方法吗?
1 个解决方案
#1
10
Step 1. Use [['positions']] instead of ['positions']:
步骤1。用['position ']代替['position ']:
In [30]: df2 = df.groupby(['stock','same1','same2'])[['positions']].sum()
In [31]: df2
Out[31]:
positions
stock same1 same2
A AA AAA 300
B BB BBB 300
C CC CCC 900
Step 2. And then use reset_index
to move the index back to the column
步骤2。然后使用reset_index将索引移回列
In [34]: df2.reset_index()
Out[34]:
stock same1 same2 positions
0 A AA AAA 300
1 B BB BBB 300
2 C CC CCC 900
EDIT
Seems my method is not so good.
看来我的方法不太好。
Thanks to @Andy and @unutbu , you can achieve your goal by more elegant ways:
感谢@Andy和@unutbu,您可以通过更优雅的方式实现您的目标:
method 1:
方法1:
df.groupby(['stock', 'same1', 'same2'])['positions'].sum().reset_index()
method 2:
方法2:
df.groupby(['stock', 'same1', 'same2'], as_index=False)['positions'].sum()
#1
10
Step 1. Use [['positions']] instead of ['positions']:
步骤1。用['position ']代替['position ']:
In [30]: df2 = df.groupby(['stock','same1','same2'])[['positions']].sum()
In [31]: df2
Out[31]:
positions
stock same1 same2
A AA AAA 300
B BB BBB 300
C CC CCC 900
Step 2. And then use reset_index
to move the index back to the column
步骤2。然后使用reset_index将索引移回列
In [34]: df2.reset_index()
Out[34]:
stock same1 same2 positions
0 A AA AAA 300
1 B BB BBB 300
2 C CC CCC 900
EDIT
Seems my method is not so good.
看来我的方法不太好。
Thanks to @Andy and @unutbu , you can achieve your goal by more elegant ways:
感谢@Andy和@unutbu,您可以通过更优雅的方式实现您的目标:
method 1:
方法1:
df.groupby(['stock', 'same1', 'same2'])['positions'].sum().reset_index()
method 2:
方法2:
df.groupby(['stock', 'same1', 'same2'], as_index=False)['positions'].sum()