Вот пример того, как мне удалось извлечь числовые значения из измерений и умножить их, чтобы вернуть объем:
import pandas as pd
# create a dict
d = {'model': ['merc','ford'], 'dimensions': ['4.31 m x 2 m x 3.222 m', '2 m']}
# create data frame from dict
df = pd.DataFrame(data=d)
# this extracts all instances of numbers but creates a new data frame with each num in new row
x = df['dimensions'].str.extractall(r'(\d*\.?\d+)')
# converts all numeric strings to float
x[0] = x[0].astype(float)
#multiplies the dimensions of the van
y = x.loc[0].prod(axis=0)
print(y)
Вот моя попытка повторить пример из приведенного выше кода, но вернуть его в новый столбец в фрейме данных.
def my_function(col,row):
out = 0
if col.str.extractall(r'(\d*\.?\d+)') == True:
out = col.str.extractall(r'(\d*\.?\d+)')
col[0] = col[0].astype(float)
z = col.loc[row].prod(axis=0)
return z
# logic to create new column based on function and existing data.
df['volume'] = df.apply(lambda x: my_function(df['dimensions'], df.index)
Может ли кто-нибудь помочь мне вернуть данные этого тома в исходный фрейм данных в виде нового столбца.