简体   繁体   English

如何将数据帧的列值转换为具有不同索引的另一个数据帧?

[英]How to transform column values of a dataframe to another dataframe with different indexes?

I have two dataframes with equal number of rows.我有两个行数相同的数据框。

df_a:

index   Id   X   Y
1       10   x1  - 
2       20   x2  -

-----------

df_b:

index   PostId   Text   
3       10       abcd
4       20       efg

Now how can I transform values of df_b['Text'] to df_a['Y'] .现在如何将df_b['Text']值转换为df_a['Y'] resulting this:结果是:

df_a:

index   Id   X   Y
1       10   x1  abcd
2       20   x2  efg

Note that indexes of mentioned dataframes are not the same.请注意,提到的数据帧的索引是不一样的。

Because of the same number of rows, you can assign numpy array:由于行数相同,您可以分配 numpy 数组:

df_a['Y'] = df_b['Text'].to_numpy()

Older pandas versions:较旧的熊猫版本:

df_a['Y'] = df_b['Text'].values

If want to map or merge be free to use some of solution from this answer .如果要映射或合并,可以自由使用此答案中的一些解决方案。

暂无
暂无

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

相关问题 如果索引值相同,如何将一个DataFrame列复制到另一个Dataframe中 - How to copy one DataFrame column in to another Dataframe if their indexes values are the same 如何使用其他数据框列的值转换数据框的列值 - How to transform a column value of a dataframe with values of another dataframe columns 如何将数据框列数据添加到另一个数据框中的一系列索引? - How to add dataframe column data to a range of indexes in another dataframe? 如何转换列的值以扩展数据框? - How to transform the column‘s values to spread the Dataframe? 在 Pandas Dataframe 中,如何根据列中的已知值获取另一列的索引? - In Pandas Dataframe, how to get the indexes of another column base on a known values in a column? 如何从另一个具有不同日期时间索引的 dataframe 获取列值 - How to get column values from another dataframe with a different datetime index 如何用索引对数据框中的值进行排序? - How to rank values in a dataframe with indexes? 如何将 dataframe 列直接转换为 dataframe 列值作为列索引? - How to convert straight forward a dataframe column into a dataframe with column values as column indexes? 如何检查一个数据帧中的列值是否可用或不检查另一数据帧的列中的值? - How to check values of column in one dataframe available or not in column of another dataframe? 如果 dataframe 2 的 A 列与 pandas 的 A 列不同,如何将 dataframe 1 的 A 列中的值归零? - How to zero values in column A of dataframe 1 if they are different from column A of dataframe 2 in pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM