简体   繁体   English

Python - 如何将两列中的值计算到每行中自己的列中?

[英]Python - How can you calculate values from two columns into its own column in each row?

I'm having a problem with calculating results from certain columns and appending it to a separate column.我在计算某些列的结果并将其附加到单独的列时遇到问题。

For an example, I'm trying to calculate the EV Ratio by dividing b / a and appending those results to col_e (EV/Ratio).例如,我试图通过除以 b / a 并将这些结果附加到 col_e (EV/Ratio) 来计算 EV 比率。

I believe one of the problems is calculating e, there are going to be tickers that do not have numbers which appends None or 'NaN'.我相信问题之一是计算 e,会有没有附加 None 或“NaN”的数字的股票代码。

I know in Yfinance you can get the ev/ratio, but the point is to do other calculations in the future that is not provided by Yfinance.我知道在 Yfinance 中您可以获得 ev/ratio,但重点是在未来进行 Yfinance 未提供的其他计算。

Thanks!谢谢!

start = time.time()
col_a = []  
col_b = []  
col_c = []  
col_d = []  
col_e = []

print('Loading Data... Please wait for results')

def do_something(tickers):
    print('---', tickers, '---')
    all_info = yf.Ticker(tickers).info
    try:
        a = all_info.get('ebitda')
        b = all_info.get('enterpriseValue')
        c = all_info.get('trailingPE')
        d = all_info.get('sector')
        e = (float(b)/float(a))
    except:
        None

    col_a.append(a)  
    col_b.append(b)  
    col_c.append(c)  
    col_d.append(d)  
    col_e.append(e)   
    return
with concurrent.futures.ThreadPoolExecutor() as executer:
    executer.map(do_something, tickers)
        

# Dataframe Set Up
pd.set_option("display.max_rows", None)    
df = pd.DataFrame({
    'Ticker': tickers,
    'Ebitda': col_a,  
    'EnterpriseValue' :col_b,  
    'PE Ratio': col_c,  
    'Sector': col_d,
    'EV/Ratio': col_e
})
print(df.dropna())
print('It took', time.time()-start, 'seconds.')

Normal Output without EV Ratio calculation没有 EV 比率计算的正常输出

   Ticker        Ebitda  EnterpriseValue    PE Ratio              Sector
0       A -6.810958e+07     8.829677e+07         NaN  Consumer Defensive
1      AA           NaN     7.848941e+08         NaN         Real Estate
2     AAC -2.015600e+07     1.971329e+08    1.006808  Financial Services
3    AACG  8.132960e+08     1.228469e+09    9.518116   Consumer Cyclical
4   AACIU -2.217800e+07     3.088700e+08         NaN          Technology
5    AADI           NaN     2.441449e+09   60.060978  Financial Services
6    AAIC           NaN              NaN         NaN                None

Desired Output (xxx represents the ratio)期望输出(xxx 代表比率)

   Ticker        Ebitda  EnterpriseValue    PE Ratio              Sector    EV/Ratio
0       A -6.810958e+07     8.829677e+07         NaN  Consumer Defensive    xxx
1      AA           NaN     7.848941e+08         NaN         Real Estate    xxx
2     AAC -2.015600e+07     1.971329e+08    1.006808  Financial Services    xxx
3    AACG  8.132960e+08     1.228469e+09    9.518116   Consumer Cyclical    xxx
4   AACIU -2.217800e+07     3.088700e+08         NaN          Technology    xxx
5    AADI           NaN     2.441449e+09   60.060978  Financial Services    xxx
6    AAIC           NaN              NaN         NaN                None    xxx

Output Using .dropna()使用 .dropna() 输出

0       A  1.178790e+08     3.501286e+09   56.152172         Industrials
2     AAC -2.015600e+07     1.971329e+08    1.006808  Financial Services
5    AADI  1.762000e+09     5.311271e+10   61.948406          Healthcare
6    AAIC  8.132960e+08     1.228469e+09    9.518116   Consumer Cyclical
12   AAOI  1.239876e+09     1.609877e+10   25.678375   Consumer Cyclical
15   AAPL  1.891750e+08     3.665276e+09  137.295380         Real Estate
17    AAT  2.305600e+07     1.175756e+08    7.616487  Financial Services
19    AAU  1.109350e+11     2.489513e+12   33.713196          Technology
20   AAWW  8.486470e+08     4.013744e+09    5.528439         Industrials
22    ABB  3.621000e+09     7.813488e+10   13.841295         Industrials
24    ABC  2.767000e+10     2.758342e+11   40.154480          Healthcare
import yfinance as yf
import pandas as pd

tickers = ['MSFT', 'AAPL', 'TSLA']

def create_df(tickers):
    all_info = []
    for each_ticker in tickers:
        all_info.append(yf.Ticker(each_ticker).info)
        
    df = pd.DataFrame.from_records(all_info)
    df = df[['symbol','ebitda', 'enterpriseValue', 'trailingPE', 'sector']]
    df.dropna(inplace=True)
    df['EV/Ratio'] = df['enterpriseValue'] / df['ebitda']
    return df
    
df = create_df(tickers)
df.head()
|    | symbol   |       ebitda |   enterpriseValue |   trailingPE | sector            |   EV/Ratio |
|---:|:---------|-------------:|------------------:|-------------:|:------------------|-----------:|
|  0 | MSFT     |  85745000448 |     2476438192128 |      37.6686 | Technology        |    28.8814 |
|  1 | AAPL     | 120233000960 |     2534678462464 |      26.7362 | Technology        |    21.0814 |
|  2 | TSLA     |   7266999808 |     1033331408896 |     337.498  | Consumer Cyclical |   142.195  |

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Pandas计算每一行的列中值的数量并将其放入列中 - Python Pandas Calculate Number of values in columns for each row and put it in columns 如何在 dataframe 中选择两行,计算每列中两个值的平均值并将新行与 dataframe 中的平均值相加 - How to choose two rows in a dataframe, calculate the average of both values in each columns and add the new row with the averages in the dataframe 计算矩阵中每一行的最大值(不包括其自己的列)的更有效方法是什么? - What's a more efficient way to calculate the max of each row in a matrix excluding its own column? 计算每行值python - Calculate each row values python 如何在 Python 的 Dash package 中创建两个按钮,可以修改两个卡体,每个按钮都有自己的方式? - How to create two buttons in the Dash package of Python which can modify both a cardbody, each in its own way? 如何在Python的多列中从行组中找到2个最大值,并显示其行和列索引而没有重复项 - How to find 2 largest values from group of rows in multiple columns in Python and also show its row and column index without duplicates 如何 pivot 将列分为两列,同时保留列名和值作为 python 中的行条目 - How to pivot multipe columns into two columns while retaining both the column names and values as row entries in python 如何为两列中的每个唯一值计算 Python 中的加权平均值? - How to calculate a weighted average in Python for each unique value in two columns? Python如何在保留每列中的行顺序的同时合并具有多列的两个数据帧? - Python how to merge two dataframes with multiple columns while preserving row order in each column? 如何根据其他列中的数据替换python大熊猫中的某些值? - How do you replace certain values from row to row in python pandas based on data in other columns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM