简体   繁体   English

更快地从一个数据帧获取行数据(基于条件)并合并到另一个b pandas python上

[英]Faster way to get row data (based on a condition) from one dataframe and merge onto another b pandas python

I have two dataframes with different indices and lengths. 我有两个具有不同索引和长度的数据帧。 I'd like to grab data from asset column from asset_df if the row matches the same ticker and year. 如果行匹配相同的股票代码和年份,我想从asset_df的资产列中获取数据。 See below. 见下文。

I created a simplistic implementation using for-loops, but I imagine there are fancier, faster ways to do this? 我使用for循环创建了一个简单的实现,但我想有更好的,更快的方法来做到这一点?

Ticker_df Ticker_df

Year   Ticker    Asset    Doc
2011   Fb        NaN      doc1
2012   Fb        NaN      doc2

asset_df asset_df

Year   Ticker    Asset
2011   FB        100
2012   FB        200
2013   GOOG      300

Ideal result for ticker_df ticker_df的理想结果

Year   Ticker    Asset    Doc     
2011   Fb        100      doc1
2012   Fb        200      doc2

My Sucky implementation: 我的Sucky实施:

for i in ticker_df.Name.index:

    c_asset = asset_df[asset_df.tic == ticker_df.Name.ix[i]]
    if len(c_asset) > 0:
    #This checks to see if there is actually asset data on this company
        asset = c_asset[c_asset.fyear == ticker_df.Year.ix[i]]['at']

        if len(asset) > 0:
            asset =  int(asset)
            print 'g', asset, type(asset)
            ticker_df.asset.ix[i] = asset
            continue

        else:
            ticker_df.asset.ix[i] = np.nan
            continue

    if len(c_asset) == 0:
        ticker_df.asset.ix[i] = np.nan
        continue

You can use the update method. 您可以使用更新方法。 Just get the indices aligned first. 只需首先对齐索引。

In [23]: ticker_df['Ticker'] = ticker_df.Ticker.str.upper()

In [24]: ticker_df = ticker_df.set_index(idx)

In [25]: asset_df = asset_df.set_index(idx)

In [26]: ticker_df.update(asset_df)

In [27]: ticker_df
Out[27]: 
             Asset   Doc
Year Ticker             
2011 FB        100  doc1
2012 FB        200  doc2

[2 rows x 2 columns]

暂无
暂无

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

相关问题 Pandas:根据条件将值从一个 dataframe 合并到另一个 - Pandas: Merge values from one dataframe to another based on condition pandas | 根据条件从另一个 dataframe 获取数据 - pandas | get data from another dataframe based on condition 一种高效(快速)的方法,可以根据从Python Pandas中另一个DataFrame获取的范围将一个DataFrame中的连续数据分组? - Efficient (fast) way to group continuous data in one DataFrame based on ranges taken from another DataFrame in Python Pandas? pandas - 有没有办法根据条件将值从一个 dataframe 列复制到另一个列? - pandas - Is there a way to copy a value from one dataframe column to another based on a condition? 熊猫:根据时间条件将行从一个数据框映射到另一个数据框 - Pandas: Map rows from one dataframe to another based on a time condition 根据条件用一个python pandas dataframe列的值替换为另一个python pandas dataframe列的值 - Substitute the values of one python pandas dataframe column by values from another based on a condition 根据Python Pandas中另一个数据框中的数据选择一个数据框中的行 - Selecting rows in one dataframe based on data in another dataframe in Python Pandas 从一个数据框中的另一个数据框中搜索关键字并将两者合并。 熊猫蟒 - Search keywords from one dataframe in another and merge both . Pandas Python 根据行值将单元格从一个 Pandas 数据帧覆盖到另一个 - overwriting cells from one pandas dataframe to another based on row values 更快地根据条件更改数据框的行值 - Faster way to change dataframe's row value based upon condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM