简体   繁体   English

根据数据框中另一列的值为列表中的数据框列分配值

[英]Assigning values to dataframe columns from a list based on value of another column in dataframe

Assigning values to dataframe columns from a list based on value of another column in dataframe根据数据框中另一列的值为列表中的数据框列分配值

I have two dataframes, df1 and df2, where我有两个数据帧,df1 和 df2,其中

df1 = pd.DataFrame(np.array([['a', 'b', 'c','d'], [1, 2, 3, 4]]),
                   columns=['x', 'y'])


df2 = pd.DataFrame(np.array([['a', 'b', 'c', 'a', 'c', 'b','b'], [4, 5, 6, 1, 32, 1, 8]]),
                   columns=['x', 'z'])


I want to create a dataframe df3 based on df2 with the appropriate values of df1['y'] assigned.我想创建一个基于 df2 的数据框 df3,并分配了适当的 df1['y'] 值。 So for example I would like the result to look like:例如,我希望结果如下所示:

df3 = pd.DataFrame(np.array([['a', 'b', 'c', 'a', 'c', 'b','b'], [4, 5, 6, 1, 32, 1, 8] , [1, 2, 3, 1, 3, 2, 2 ] ]),
                   columns=['x', 'z', 'y'])

In my real case there are many thousand possible values of x so I would like to avoid if df3['x'] == a: df3['y'] = 1 style solutions if possible.在我的实际情况中,x 有数千个可能的值,因此if df3['x'] == a: df3['y'] = 1可能,我想避免if df3['x'] == a: df3['y'] = 1样式解决方案。

您可以通过合并实现此目的:

df3 = df2.merge(df1, how="left", on="x")

Setup:设置:

import pandas as pd
import numpy as np

data={'x':['a', 'b', 'c','d'],
      'y':[1, 2, 3, 4]}
df1 = pd.DataFrame(data)

data2={'x':['a', 'b', 'c', 'a', 'c', 'b','b'],
       'z':[4, 5, 6, 1, 32, 1, 8]}
df2 = pd.DataFrame(data2)

data3={'x':['a', 'b', 'c', 'a', 'c', 'b','b'],
       'z':[4, 5, 6, 1, 32, 1, 8]}
df3 = pd.DataFrame(data3)

Dictionary method:字典法:

# Make a dictionary
dict = df1.set_index('x').to_dict()
# Map to dictionary
df3 = df2.assign(x=df2['x'].map(*dict.values()))

You have to unzip dict because it actually comes out as 2 dicts, but you only want the second here.您必须解压缩dict因为它实际上是 2 个dict ,但您只想要第二个。

暂无
暂无

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

相关问题 根据数据框中许多其他列上的值在列中分配值 - Assigning value in a column based on values on many other columns in dataframe Pivot a dataframe 保留所有列并根据另一列为每列分配后缀和值 - Pivot a dataframe keeping all the columns and assigning suffixes and values to each column based on another column 如何根据来自 onw dataframe 的两列的值与另一个 dataframe 的列的值和名称进行比较来获取正确的值 - how to pick up correct value based on values from tow columns of onw dataframe comparing with value and name of the column of another dataframe Pandas Dataframe:根据将一列的每个值与另一列的所有值进行比较来分配新列 - Pandas Dataframe: assigning a new column based comparing each value of one column to all the values of another 根据另一列中的值组合数据框的列 - Combining columns of dataframe based on value in another column 根据来自单个列的值将值添加到 dataframe 列 - Adding values to dataframe columns based on value from a single column 为另一列中的唯一值的熊猫数据框值赋值 - Assigning value to pandas dataframe values for unique values in another column 根据另一个数据帧的列值的条件将数据添加到数据帧中的列 - Adding data to columns in a dataframe based on condition on column values of another dataframe 根据另一个 dataframe 中的列值创建 dataframe 列 - Create a dataframe column based on values that are columns in another dataframe 根据列中的值填充另一个 dataframe 的值 - Filling values from another dataframe based of value in a column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM