如何在空单元格中填充数据帧内容NA值?

时间:2016-11-21 07:09:21

标签: python pandas dataframe multiple-columns nan

我有一个数据帧df:

                  Open    Volume  Adj Close     Ticker  
Date                                                                   
2006-11-22  140.750000   45505300   114.480649   SPY 

我想将df更改为另一个数据框打开价格如下:

                  SPY    AGG  
Date                                                                   
2006-11-22  140.750000   NA

它只使用开放式数据和两个代码,那么如何将一个数据帧更改为另一个?

2 个答案:

答案 0 :(得分:1)

我认为您可以DataFrame ticker列表L使用L = ['SPY','AGG'] df1 = pd.DataFrame({'SPY': [df.Open.iloc[0]]}, index=[df.index[0]]) df1 = df1.reindex(columns=L) print (df1) SPY AGG 2006-11-22 140.75 NaN 构造函数:

Tickers

您可以使用reindex查找df2 = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies', header=0)[0] #print (df2) #filter only Ticker symbols starts with SP df2 = df2[df2['Ticker symbol'].str.startswith('SP')] print (df2) Ticker symbol Security SEC filings \ 407 SPG Simon Property Group Inc reports 415 SPGI S&P Global, Inc. reports 418 SPLS Staples Inc. reports GICS Sector GICS Sub Industry \ 407 Real Estate REITs 415 Financials Diversified Financial Services 418 Consumer Discretionary Specialty Stores Address of Headquarters Date first added CIK 407 Indianapolis, Indiana NaN 1063761 415 New York, New York NaN 64040 418 Framingham, Massachusetts NaN 791519 #convert column to list, add SPY because missing L = ['SPY'] + df2['Ticker symbol'].tolist() print (L) ['SPY', 'SPG', 'SPGI', 'SPLS'] df1 = pd.DataFrame({'SPY': [df.Open.iloc[0]]}, index=[df.index[0]]) df1 = df1.reindex(columns=L) print (df1) SPY SPG SPGI SPLS 2006-11-22 140.75 NaN NaN NaN 的搜索列表:

// Make sure you specify a valid callable with two ':'
$app->post('/yell', 'UserController::postYell')->setName('yell');

答案 1 :(得分:0)

假设您有不同代码的数据框df_list列表,并且列表中的每个项目都与示例中的df具有相同的外观

您可以先使用

将它们连接成一个框架
df1 = pd.concat(df_list)

然后用

df1[["Open", "Ticker"]].reset_index().set_index(["Date", "Ticker"]).unstack()

它应该为您提供类似

的输出
            Open
Ticker      AGG     SPY
Date        
2006-11-22  NAN  140.75