【问题标题】:Conditionally fill a column of a pandas df with values of a different df有条件地用不同 df 的值填充 pandas df 的一列
【发布时间】:2017-06-22 22:39:02
【问题描述】:

我有两个不同形状的 df。一个包含单词及其频率,另一个包含单词及其lemmas

第一个 df 总是将一个词映射到一个频率, 第二个 df 将许多单词映射到一个引理(多次)。例如:

df1:

  word  frequency
    de   33504559
   que   32700217
    no   28263302
     a   21978600
    la   21249418

和df2:

     lemma       word
   zurullo   zurullos
  zurupeto  zurupetos
    zutano     zutana
    zutano    zutanas
    zutano    zutanos

我想将引理信息添加到df1,通过搜索df1 中的每个单词,将其与df2 中的单词进行比较,然后从df2 中提取引理信息以将其添加回@ 987654329@.


when the value is always the same in df1 有一些有用的答案,但由于我想对包含不同单词的每一行执行此操作,所以我不确定如何继续。 (我检查了the merging and concatenating docs section,但重新浮出水面时比以前更加困惑......)

在 just-python 中我会使用循环,例如:

new_df = dict()
# assuming all dfs are dicts
for w, f in df1.items():
    if w in df2.keys():
        new_df[w] = (df2[w], f)

很高兴使用 pandas 数据帧操作了解更多有关此内容的信息。

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    试试这个:

    df1.merge(df2, how='left', on='word')
    

    【讨论】:

      猜你喜欢
      • 2022-08-14
      • 2020-09-16
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 2020-12-29
      • 2020-12-25
      • 2020-08-16
      相关资源
      最近更新 更多