简体   繁体   English

如何在 Pandas 中将值从一个数据帧移动到另一个数据帧?

[英]How to move values from one dataframe to another in pandas?

I have a df1 that looks like this:我有一个 df1 看起来像这样:

           Symbol Order  Shares
Date                           
2009-01-14   AAPL   BUY     150
2009-01-21   AAPL  SELL     150
2009-01-21    IBM   BUY     400

And df2 looks like this: df2 看起来像这样:

           GOOG AAPL  XOM  IBM Cash
Date                               
2009-01-14  NaN  NaN  NaN  NaN  NaN
2009-01-21  NaN  NaN  NaN  NaN  NaN

I want to move the values in the first DF to the second so that the amount of shares populates under the appropriate stock symbol.我想将第一个 DF 中的值移动到第二个,以便在适当的股票代码下填充股票数量。 So the above would look like:所以上面看起来像:

           GOOG AAPL  XOM  IBM Cash
Date                               
2009-01-14  NaN  150  NaN  NaN  NaN
2009-01-21  NaN  -150 NaN  400  NaN

How would I move all values that I have in my first dataframe to the second dataframe?如何将第一个数据框中的所有值移动到第二个数据框中?

You really don't need df2 here.你真的不需要df2在这里。 You can compute the result directly from df using some simple reshaping functions set_index , unstack and reindex .您可以直接从计算结果df使用一些简单的重塑功能set_indexunstackreindex You just need the symbols list.您只需要符号列表。

(df.assign(Shares=np.where(df.Order == 'BUY', df.Shares, -df.Shares))
   .drop('Order', 1)
   .set_index('Symbol', append=True)['Shares']
   .unstack(1)
   .reindex(df2.columns, axis=1))  # you can replace df2.columns with a list 

            GOOG   AAPL  XOM    IBM  Cash
Date                                     
2009-01-14   NaN  150.0  NaN    NaN   NaN
2009-01-21   NaN -150.0  NaN  400.0   NaN

Use np.select to convert numbers to negative if Order == 'SELL' then update如果Order == 'SELL'然后update则使用np.select将数字转换为负数

df['Shares'] = np.select([df['Order'] == 'SELL'], [-df['Shares']], df['Shares'])
df2.update(df.pivot(None, 'Symbol', 'Shares'))


            GOOG   AAPL  XOM    IBM  Cash
Date                                     
2009-01-14   NaN  150.0  NaN    NaN   NaN
2009-01-21   NaN -150.0  NaN  400.0   NaN

暂无
暂无

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

相关问题 如何基于与熊猫匹配的列值将数据从一个数据帧移动到另一个数据帧? - How to move data from one dataframe to another based on matching column values with pandas? 如何使用熊猫中另一个数据框的值更新一个数据框 - How to update one dataframe using values from another dataframe in pandas Pandas dataframe - 将 N 行从一个 dataframe 移动到另一个 - Pandas dataframe - move N number of rows from one dataframe to another 如何使用pandas从另一个数据框中查找值? - How to find values from one dataframe in another using pandas? 将某些 pandas dataframe 列值从一列移到另一列,并将旧的 position 替换为 Nan - Move certain pandas dataframe column values from one column to another and replace old position with Nan 用另一个数据框中的值替换一个熊猫数据框中的值 - Replacing values in one pandas dataframe with values from another dataframe 如何从一个 pandas dataframe 获取行值并将它们用作从另一个 dataframe 获取值的参考 - How to take row values from one pandas dataframe and use them as reference to get values from another dataframe pandas Dataframe,将特定值从一个单元格移动到另一个单元格 - pandas Dataframe, MOVE Specific Value from One Cell to Another 将pandas DataFrame中的对象从一个“单元”移动到另一个 - Move object in pandas DataFrame from one “cell” to another Pandas:如何将一个 DataFrame 中的所有值除以另一个 DataFrame 中的值? - Pandas: how to divide all values in one DataFrame by the values in another DataFrame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM