簡體   English   中英

創建帶有一些邏輯的新列到 Pandas 數據框

[英]Create new column with some logic to pandas dataframe

我需要根據外部表 vprices 添加一個新列“價格”。

我嘗試在示例中添加它,但出現錯誤,因為在括號 df["vol-type"] 內是一個系列變量,而不是系列的第 n 個值,這正是我所需要的。

如何重寫以使用每行的值填充新列“real_size”?

virtsizes = {
  "type1": { "gb": 1.2, "xxx": 0, "yyy": 30 },
  "type2": { "gb": 1.5, "xxx": 2, "yyy": 20  },
  "type3": { "gb": 2.3, "xxx": 0.1, "yyy": 10  },
}
df = pd.read_csv(StringIO(src),names=["vol-id","size","vol-type"])

df["real_size"] = df["size"] * ( virtsizes[df["vol-type"]]["gb"] 

謝謝!

loc選擇的df1行使用map

virtsizes = {
  "type1": { "gb": 1.2, "xxx": 0, "yyy": 30 },
  "type2": { "gb": 1.5, "xxx": 2, "yyy": 20  },
  "type3": { "gb": 2.3, "xxx": 0.1, "yyy": 10  },
}
df1 = pd.DataFrame(virtsizes)
print (df1)
     type1  type2  type3
gb     1.2    1.5    2.3
xxx    0.0    2.0    0.1
yyy   30.0   20.0   10.0

df = pd.DataFrame({'vol-type':['type1','type2']})

df["real_size"] = df["vol-type"].map(df1.loc['gb'])
print (df)
  vol-type  real_size
0    type1        1.2
1    type2        1.5

另一個解決方案是在dict comprehension提取gb

virtsizes = {
  "type1": { "gb": 1.2, "xxx": 0, "yyy": 30 },
  "type2": { "gb": 1.5, "xxx": 2, "yyy": 20  },
  "type3": { "gb": 2.3, "xxx": 0.1, "yyy": 10  },
}
d = {k:v['gb'] for k,v in virtsizes.items()}
print (d)
{'type2': 1.5, 'type1': 1.2, 'type3': 2.3}

df = pd.DataFrame({'vol-type':['type1','type2']})
df["real_size"] = df["vol-type"].map(d)
print (df)
  vol-type  real_size
0    type1        1.2
1    type2        1.5

不像 jezrael 那樣好,但這也有效:

real_size = []
for index, row in df.iterrows():
  real_size.append(row["size"] * virtsizes[["vol-type"]]["gb"])
df["real_size"] = real_size

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM