![](/img/trans.png)
[英]How to get the subset of dataframe based on another dataframe in pandas python
[英]How to get the subset dataframe from the dataframe in python?
我有一個數據框(df):
id CI VaR
0 1 0.600 1000
1 1 0.650 1100
2 1 0.700 1200
3 1 0.750 1300
4 2 0.600 2500
5 2 0.650 2600
6 2 0.700 2700
7 2 0.750 2800
8 3 0.600 1500
9 3 0.650 1600
10 3 0.700 1700
11 3 0.750 1800
我必須從這個 dataframe 創建一個子集 dataframe 我正在這樣做;
for col in range(1,4):
df2 = df1.loc[df1["id"]==col]
print(df2)
output:
id CI VaR
0 1 0.600 1000
1 1 0.650 1100
2 1 0.700 1200
3 1 0.750 1300
和
4 2 0.600 2500
5 2 0.650 2600
6 2 0.700 2700
7 2 0.750 2800
和
8 3 0.600 1500
9 3 0.650 1600
10 3 0.700 1700
11 3 0.750 1800
這將為 id 1,2,3 提供單獨的 dataframe 現在我想要所有數據幀(1,2 和 3)的 VaR 值,並按它們的順序將所有值和 append 添加到相應的 Z6A8060D755DF47C55555550 中。 喜歡:
obj = 0
for col in range(1,4):
df2 = df1.loc[df1["id"]==col]
obj = obj + df1["VaR"] # error is here
print(df2)
但這對我不起作用
我需要像這樣的輸出;
id CI VaR capital
0 1 0.600 1000 5000
1 1 0.650 1100 5300
2 1 0.700 1200 5600
3 1 0.750 1300 5900
資本價值 5000 來自添加 1000 + 2500 + 1500 (這些都是相關 ID 的第一個值) 資本價值 5300 來自添加 1100 + 2600 + 1600 (這些都是相關 ID 的第二個值)等等......我需要所有的id;
4 2 0.600 2500 5000
5 2 0.650 2600 5300
6 2 0.700 2700 5600
7 2 0.750 2800 5900
和
8 3 0.600 1500 5000
9 3 0.650 1600 5300
10 3 0.700 1700 5600
11 3 0.750 1800 5900
謝謝你的時間:)
我希望我已經正確理解了你的問題。 如果您需要重復每組中的第一個、第二個、第三個......值的總和:
vals = df.groupby(df.groupby("id").cumcount())["VaR"].sum()
df["capital"] = [*vals] * df["id"].nunique()
print(df)
印刷:
id CI VaR capital
0 1 0.60 1000 5000
1 1 0.65 1100 5300
2 1 0.70 1200 5600
3 1 0.75 1300 5900
4 2 0.60 2500 5000
5 2 0.65 2600 5300
6 2 0.70 2700 5600
7 2 0.75 2800 5900
8 3 0.60 1500 5000
9 3 0.65 1600 5300
10 3 0.70 1700 5600
11 3 0.75 1800 5900
通過 np.tile 的選項和通過np.tile
划分np.arraysplit
的不同方法:
(假設:所有id分組長度相等,分組總數等於每分組的行數)
from pprint import pprint
import numpy as np
import pandas as pd
df = pd.DataFrame({
'id': [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3],
'CI': [0.6, 0.65, 0.7, 0.75, 0.6, 0.65, 0.7, 0.75, 0.6, 0.65, 0.7, 0.75],
'VaR': [1000, 1100, 1200, 1300, 2500, 2600, 2700, 2800, 1500, 1600, 1700,
1800]
})
unique_count = df['id'].nunique()
df['capital'] = np.tile(
df.groupby(df.groupby("id").cumcount())["VaR"].sum(),
unique_count
)
dfs = np.array_split(df, unique_count)
pprint(dfs)
dfs
:
[ id CI VaR capital
0 1 0.60 1000 5000
1 1 0.65 1100 5300
2 1 0.70 1200 5600
3 1 0.75 1300 5900,
id CI VaR capital
4 2 0.60 2500 5000
5 2 0.65 2600 5300
6 2 0.70 2700 5600
7 2 0.75 2800 5900,
id CI VaR capital
8 3 0.60 1500 5000
9 3 0.65 1600 5300
10 3 0.70 1700 5600
11 3 0.75 1800 5900]
讓我們將groupby
與transform
一起使用:
df['capital'] = df.groupby(df.groupby('id').cumcount())['VaR'].transform('sum')
Output:
id CI VaR capital
0 1 0.60 1000 5000
1 1 0.65 1100 5300
2 1 0.70 1200 5600
3 1 0.75 1300 5900
4 2 0.60 2500 5000
5 2 0.65 2600 5300
6 2 0.70 2700 5600
7 2 0.75 2800 5900
8 3 0.60 1500 5000
9 3 0.65 1600 5300
10 3 0.70 1700 5600
11 3 0.75 1800 5900
細節:
cumcount
得到每個組中的 positiontransform
求和
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.