[英]Create new Pandas columns based on conditional counts of all other columns
[英]create new column based on conditional of other columns
我有一張顯示小時、總數和公里長度的表格:
type hour km_length total
A 1 1
B 2 1
我想添加顯示匯率的列。 速率可以通過兩列計算,有 2 種情況:
(1) rate = (hour x 100) / total
(2) rate = (km_length x 1000000) / total
當行在小時上有值時,它將使用第一個等式。 如果該行在 km_length 上具有值,則它將使用第二個等式。
那么表格將是這樣的:
type hour km_length total rate
A 1 1 100
B 2 1 2000000
無論如何我可以通過使用python來實現它嗎?
謝謝你。
使用帶有axis==1
pandas.DataFrame.bfill
:
# df = df.replace("", np.nan) # In case it's whitespace(s) instead of np.nan
df["hour"] = df["hour"] * 100
df["km_length"] = df["km_length"] * 1000000
df["rate"] = df.bfill(1)["hour"]/df["total"]
print(df)
輸出:
type hour km_length total rate
0 A 100.0 NaN 1 100
1 B NaN 2000000.0 1 2e+06
我想知道您是否正在尋找將這些案例組合成一個等式。 如果是這樣,請使用np.nan_to_num()
函數:
import pandas as pd
import numpy as np
df = pd.DataFrame({'type': ['A','B'],'hour': [1,np.NaN], 'km_length': [np.NaN,2], 'total' : [1,1]})
df['rate'] = ( np.nan_to_num(df['hour'])*100 + np.nan_to_num(df['km_length'])*1000000 ) / df['total']
print(df)
type hour km_length total rate
0 A 1.0 NaN 1 100.0
1 B NaN 2.0 1 2000000.0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.