因此,基本上,我有一個看起來像這樣的DataFrame:
我們的任務是以0.1的步長增加 "深度"(增加新的行),同時對 "值 "進行相應的插值。
它應該看起來像這樣。 (由于尺寸問題,底部被裁剪)
這是我寫的代碼草案:
import pandas as pd
df = pd. DataFrame({'Name': ['AS', 'AS', 'AS', 'DB', 'DB', 'DB'] 。
'Depth': [15, 16, 17, 10, 11, 12] 。
'Value'。[100, 200, 300, 200, 300, 400]})
df['Depth']= ... #make it here with increment 0.1。
df['Value'] = df['Value'].interpolate(method=linear)
df['Name'] = ... #為每個空行復制它。
df.to_csv('Interpolated values.csv')
uj5u.com熱心網友回復:
這里有一個解決方案,它將允許你用任何變化的步長來插值(假設步長整齊地落在整數之間),并且在插值方面有更大的靈活性:
my_df_list = [] 。
step=0.1
for label, group in df.sort_values('Depth').groupby('Name')。
# 創建一個查找字典用于插值查找。
lookup_dict = {x[0] 。 x[1] for x in group[['Depth', 'Value']] 。 值}。
# 使用np.linespace,因為開始和結束值的嚴格性。
new_index = np.linspace(
start = group['Depth'].min()。
stop = group['Depth'].max()。
num = int(1/step) * np.tp(group['Depth']) 1)
)
new_values = pd.Series(
lookup_dict.get(round(x, 1) for x in new_index
).interpolate()
# 用你的值創建一個tmp df。
df_tmp = pd.DataFrame.from_dict({
'Name': [標簽] * len(new_index)。
'深度': new_index,
'Value':new_values
})
my_df_list.append(df_tmp)
# 最后,合并所有dfs。
df_final = pd.concat(my_df_list, ignore_index=True)
名稱 深度 價值
0 AS 15.0 100.0
1 AS 15.1 110.0 >。
...
19 AS16.9 290.0
20 AS 17.0 300.0[/span]。
21 DB 10.0 200.0[/span]。
22 DB 10.1 210.0
...
39 DB 11.8 380.0 ...
40 DB 11.9 390.0 >。
41 DB 12.0 400.0
uj5u.com熱心網友回復:
第一部分:
我選擇不使用迭代,而是給整個列分配新的值。
import pandas as pd
import numpy as np
df = pd. DataFrame({'Name': ['AS'/span>, 'AS'/span>, 'AS'/span>, 'DB', 'DB', 'DB'] 。
'Depth': [15, 16, 17, 10, 11, 12] 。
'Value'。 [100, 200, 300, 200, 300, 400]
})
輸出:
Name Depth Value
0 AS 15 100
1 AS 16 200
2 AS 17 300
3 DB 10 200
4 DB 11 300
5 DB 12 400
使用
len來獲得該列的長度。df[column][0]來獲得初始值。如果你確實有一個特定的初始值,那么就跳過這一步。將你的初始值分配給它。
ini_1 = df['Depth'][0] #初始值。
ini_2 = df['Value'][0] # Initial value
length = len(df)
step_1=0.1
step_2 = 10
df['Depth'] = np.arange(ini_1, ini_1 length*step_1, step_1)
df['Value'] = np.range(ini_2, ini_2 length*step_2, step_2)
輸出
Name Depth Value
0 AS 15.0 100
1 AS 15.1 110
2 AS 15.2 120
3 DB 15.3 130
4 DB15.4 140
5 DB 15.5 150
因為我們不知道Name和Depth之間的變體調節,但這是另一個避免迭代到每一行的方面。
第二部分:假設每個名稱-深度組擴展到10個專案 并在
Depth和Value上分別遵循0.1和10的增量。
下面是步驟:
- 加載資料框架
- 將
df擴展到10倍: - 這是一個算術級數:
對于Deptha = 0, d = 0.1, length = 10
對于Valuea = 0, d = 10, length = 10
將它們作為矢量(1D陣列)在每個名稱-深度組中求和: - 主要部分
import pandas as pd
import numpy as np
df = pd. DataFrame({'Name': ['AS', 'AS', 'AS', 'DB', 'DB', 'DB'] 。
'Depth': [15, 16, 17, 10, 11, 12] 。
'Value'。 [100, 200, 300, 200, 300, 400]
})
dfn = pd.concat([df]*10,ignore_index=False).sort_index()
a = 0
d_depth=0.1
d_value =10
length=10
arithmetric_1 = [round(a d_depth * (n - 1), 2) for n in range(1, length 1) ] # 深度的算術進階系列]
arithmetric_2 = [round(a d_value * (n - 1), 2) for n in range(1, length 1) ] #算術級數系列的值。
for i in set(dfn.index)。
dfn.loc[i,'Depth'] = dfn.loc[i,'Depth'].array arithmetric_1
dfn.loc[i,'Value'] = dfn.loc[i,'Value'].array arithmetric_2
summary:
現在你得到的資料框架dfn是基于假設的結果。這種操作試圖減少回圈時間,并使用矢量方面來處理這個問題(如果你有巨大的資料集)。
Name Depth Value
0 AS 15.0 100
0 AS 15.1 110
0 AS 15.2 120
0 AS 15.3 130
0 AS15.4 140
0 AS15.5 150
0 AS15.6 160
0 AS15.7 170
0 AS15.8 180
0 AS15.9 190
1 AS16.0 200
1 AS16.1 210
1 AS 16.2 220
1 AS 16.3 230
1 AS 16.4 240
1 AS 16.5 250
1 AS 16.6 260
1 AS16.7 270
1 AS 16.8 280
1 AS 16.9 290
2 AS17.0 300
2 AS 17.1 310
:
uj5u.com熱心網友回復:
下面給出的解決方案將解決這個問題。
import pandas as pd
df = pd. DataFrame({'Name': ['AS', 'AS', 'AS', 'DB', 'DB', 'DB'] 。
'Depth': [15, 16, 17, 10, 11, 12] 。
'Value'。 [100, 200, 300, 200, 300, 400]})
counter = 0.0 0.0
def add(val)。
global計數器
if counter <=0.9:
val = val counter
counter =0.1: val = val counter
return val
else:
counter=0.1。
return val
# 將每條記錄復制10次,并使用索引進行排序。
df = pd.concat([df]*10).sort_index()
# 在深度上應用add函式。
df['Depth'] = df['Depth'].apply(add)
# 重置索引。
df= df.reset_index(drop=True)
# 在最后一個值的基礎上增加10的值。
for idx in range(1,len(df) )。
df.loc[idx, 'Value'] = df.loc[idx-1,'Value'] 10。
輸出:
Name Depth Value
0 AS 15.0 100
1 AS 15.1 110
2 AS 15.2 120
3 AS 15.3 130
4 AS 15.4 140
5 AS 15.5 150
6 AS 15.6 160
7 AS 15.7 170
8 AS 15.8 180
9 AS 15.9 190
10 AS 16.0 200
11 AS16.1 210
12 AS 16.2 220
13 AS 16.3 230
14 AS 16.4 240
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/332419.html
標籤:
上一篇:這個非數字的蛞蝓重碼有什么問題?


