简体   繁体   English

根据另一个 pandas Z6A8064B5DF479C550570 的值填充一个 pandas dataframe 的最快方法是什么?

[英]What is the fastest way to populate one pandas dataframe based on values from another pandas dataframe?

I have a pandas dataframe position我有一个 pandas dataframe position

        row    column
  1      3     Brazil
  2      6     USA
  3      3     USA
  4      7     Canada

and another x和另一个x

        Brazil   Canada  USA
  1     False    False   False
  2     False    False   False
  3     False    False   False
  4     False    False   False
  5     False    False   False
  6     False    False   False
  7     False    False   False

I want to populate the second one based on the values from the first one, so the result is:我想根据第一个值填充第二个,所以结果是:

        Brazil   Canada  USA
  1     False    False   False
  2     False    False   False
  3     True     False   True
  4     False    False   False
  5     False    False   False
  6     False    False   True
  7     False    True    False

I'm doing that using iterrows()我正在使用iterrows()

  for i, r in positions.iterrows():
      x.at[r['row'],r['column']] = True

Is there a faster way to do that?有没有更快的方法来做到这一点?

I will do crosstab with update我会用updatecrosstab

x.update(pd.crosstab(df.row,df.column).eq(1))
x
Out[44]: 
  Brazil Canada    USA
1  False  False  False
2  False  False  False
3   True  False   True
4  False  False  False
5  False  False  False
6  False  False   True
7  False   True  False

You can pivot the positions table:您可以 pivot 的positions表:

s = (df.assign(dummy=True).set_index(['row','column'])
       ['dummy'].unstack(fill_value=False)
    )
x |= s

Output: Output:

   Brazil  Canada    USA
1   False   False  False
2   False   False  False
3    True   False   True
4   False   False  False
5   False   False  False
6   False   False   True
7   False    True  False

searchsorted and slice assignment with iloc使用searchsorted进行iloc排序和切片分配

This assumes that index and columns in x are sorted.这假设x中的indexcolumns已排序。

We'll use iloc and tuples of positions to assign the value of True我们将使用iloc和位置元组来分配True的值


i = tuple(x.index.searchsorted(df.row))
j = tuple(x.columns.searchsorted(df.column))

x.iloc[[i, j]] = True
x


   Brazil  Canada    USA
1   False   False  False
2   False   False  False
3    True   False   True
4   False   False  False
5   False   False  False
6   False   False   True
7   False    True  False

暂无
暂无

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

相关问题 基于来自另一个数据帧的条件填充熊猫数据帧的有效方法 - efficient way to populate pandas dataframe based on conditions from another dataframe 使用 Pandas 将列从一个 DataFrame 复制到另一个 DataFrame 的最快方法? - Fastest way to copy columns from one DataFrame to another using pandas? Pandas - 根据另一个填充一个数据框列 - Pandas - populate one dataframe column based on another 搜索和查找 从一个 dataframe 到另一个 dataframe 搜索值并根据 pandas 中的查找值填充新列 - Search and lookup Search values from one dataframe in another dataframe and populate new column based on look up values in pandas 用另一个数据框的值替换熊猫数据框的多个值的最快方法 - Fastest way to replace multiple values of a pandas dataframe with values from another dataframe 根据行值将单元格从一个 Pandas 数据帧覆盖到另一个 - overwriting cells from one pandas dataframe to another based on row values 根据 Pandas 中的列值将内容从一个 Dataframe 复制到另一个 Dataframe - Copy contents from one Dataframe to another based on column values in Pandas Pandas:根据条件将值从一个 dataframe 合并到另一个 - Pandas: Merge values from one dataframe to another based on condition pandas数据框根据另一数据框中的值将值追加到一列 - pandas dataframe append values to one column based on the values in another dataframe 在 pandas dataframe 中加入 coulmn 值的最快方法? - Fastest way to join coulmn values in pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM