简体   繁体   English

如何将列值从一个 dataframe 提取到另一个?

[英]How do I extract column values from one dataframe to another?

I have two pandas data frames df1 and df2我有两个 pandas 数据帧 df1 和 df2

   **df1**                                **df2**  
cat  id  frequency            id   (other cols)    A    B    C
A    23    2                  23   .............  nan  nan  nan
A    43    8                  43   .............  nan  nan  nan
B    23    56                 30   .............  nan  nan  nan
C    30    4

I am looking for a way on how to extract information form df1 to df2 resulting in the format below, where the values of columns A, B and C are the frequency values from df1我正在寻找一种方法来从 df1 到 df2 中提取信息,从而得到以下格式,其中 A、B 和 C 列的值是 df1 的频率值

       **df2**
id  (other cols)  A  B   C 
30   ..........   0  0   4
23   ..........   2  56  0
43   ..........   8  0   0

Use DataFrame.pivot with DataFrame.combine_first :DataFrame.pivotDataFrame.combine_first一起使用:

df11 = df1.pivot('cat', 'id', 'frequency')

#if id is column
df = df2.set_index('id').combine_first(df11)

#if id is index
df = df2.combine_first(df11)

暂无
暂无

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

相关问题 如何从一个数据框中的列中提取特定值并将它们附加到另一个数据框中的列中? - 熊猫 - How do you extract specific values from a column in one dataframe and append them to a column in another dataframe? - Pandas 如何在DataFrame中使用另一列中的值减去一列中的值? - How do I Substract values in one column with the values of another in a DataFrame? 如果使用熊猫在另一个数据帧中不存在列值,如何将它们从一个数据帧合并到另一个数据帧 - How do I merge column values from one dataframe to another if they are not present in another using pandas 如何将 map 一个 dataframe 中的一列字符串值转换为另一个 dataframe 中的另一列? - How do I map a column of string values in one dataframe to another column in another dataframe? 如何通过在 Python DataFrame 中保持某些列值不变来将数据从一行合并到另一行 - How do I merge data from one row to another by keeping some column values unchanged in Python DataFrame 如何用另一个数据框列切片中的值替换数据框列的切片? - How do I replace a slice of a dataframe column with values from another dataframe column slice? 如何根据 pandas dataframe 中另一列的多个值在一列中创建值列表? - How do I create a list of values in a column from several values from another column in a pandas dataframe? 如何用另一个 dataframe 的值替换 dataframe 列中的一组值? - how do i replace set of values in a dataframe column with values from another dataframe? 试图从Python中的pandas数据框中提取一个看起来像JSON的列,我如何实现这一点? - Trying to extract one column that seems to be JSON from a pandas dataframe in Python , how do I achieve this? 如何从另一个 dataframe 创建一个 dataframe,每个值列只有最后一个非负值? - How do I create a dataframe from another dataframe with only the last non negative values for each value column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM