根据现有列(熊猫)中的信息创建新列

时间:2021-01-22 10:30:55

标签: python pandas dataframe

我是第一次使用 Pandas(并且已经从 csv 文件/重命名的列中删除了信息),并且正在尝试使用代码中的方程创建一个额外的列。其他一些尝试被排除在外。任何有关为什么它们都不起作用的帮助将不胜感激。

import pandas as pd 
#data = pd.read_csv("tem180.csv")
#data.head()


def tem180df():
    print('Skipped top 35 lines and last 3 lines')    
    tem180df = pd.DataFrame
    tem180df = pd.read_csv('tem180.csv', skiprows=35, skipfooter=3, engine='python')
    tem180df.columns =['DREF', 'TREF', 'TMEL', 'TAD']
    tem180df['DREF']= tem180df['DREF'].str.extract(r'(\d+)', expand= False)
    tem180df['TREF']= tem180df['TREF'].str.extract(r'(\d+)', expand= False)
    tem180df['TMEL']= tem180df['TMEL'].str.extract(r'(\d+)', expand= False)
    tem180df['TAD']= tem180df['TAD'].str.extract(r'(\d+)', expand= False)
    print(tem180df)

tem180df.head()

alpha = 3.28E-5
density_mantle = 3330 
    

tem180df['density_mantle_at_T'] = density_mantle * (1- alpha * tem180df["TREF"])


#tem180df['density_mantle_at_T'] = density_mantle * (1- alpha * tem180df['TREF'])

#temp180df['density_mantle_at_TREF']= tem180df.eval('density_mantle * (1- alpha * TREF)')

#tem180df.apply(lambda row: density_mantle * (1- alpha*row.TREF), axis =1)

#fn = lambda row: density_mantle * (1- alpha * row.TREF)
#col = tem180df. apply(fn, axis =1)
#tem180df = tem180df.assign(c= col.values)

0 个答案:

没有答案