我需要对DataFrame中某一列的每个元素取对数值。同时,我想将结果列添加到原DataFrame中。
这是我的DataFrame
df1=pd.read_csv('doctors.csv',encoding='latin-1')
这些是列名
Index(['PatientID', 'Pregnancies', 'PlasmaGlucose', 'DiastolicBloodPressure', 'TricepsThickness', 'SerumInsulin', 'BMI', 'DiabetesPedigree', 'Age', 'Diabetic', 'Physician'], dtype='object')
我想为’Age’列创建一个新的对数值列。
回答:
我认为需要使用numpy.log10
:
df = pd.DataFrame({'Age':[10,22,33,44,34,56,34]})df['log'] = np.log10(df['Age'])print (df) Age log0 10 1.0000001 22 1.3424232 33 1.5185143 44 1.6434534 34 1.5314795 56 1.7481886 34 1.531479