简体   繁体   English

如何将 Pandas 系列中的值添加到 Dataframe 列而不重复

[英]How to add values from a Pandas Series to a Dataframe column without duplicates

I have a dataframe called 'df':我有一个名为“df”的 dataframe:

  Value   Num
0 alpha     5
1 bravo     6
2 charlie   7

And a Series called 'series_to_add':还有一个名为“series_to_add”的系列:

  New Value
0 alpha     
1 bravo     
2 delta

How can I combine the unique values of the series into the existing dataframe to get something like this:如何将系列的唯一值组合到现有的 dataframe 中以获得如下内容:

  Value   Num
0 alpha     5
1 bravo     6
2 charlie   7
3 delta     nan

We can to_frame我们可以to_frame

s=s.to_frame('Value')
s
   Value
0  alpha
1  bravo
2  delta

Then do groupby get the first然后做groupby得到第first

pd.concat([df,s]).groupby('Value',as_index=False).first()
     Value  Num
0    alpha  5.0
1    bravo  6.0
2  charlie  7.0
3    delta  NaN

Or drop_duplicatesdrop_duplicates

pd.concat([df,s]).drop_duplicates('Value')
     Value  Num
0    alpha  5.0
1    bravo  6.0
2  charlie  7.0
2    delta  NaN

Or mergemerge

df.merge(s,how='outer')
     Value  Num
0    alpha  5.0
1    bravo  6.0
2  charlie  7.0
3    delta  NaN

暂无
暂无

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

相关问题 如何创建从列中获取的唯一值的熊猫数据框,没有重复项 - How to create a pandas dataframe of unique values fetched from column with no duplicates Pandas - 根据与数据框中某个值匹配的系列索引,将系列中的值添加到数据框列 - Pandas - Add values from series to dataframe column based on index of series matching some value in dataframe 将 pandas 系列值添加到 pandas Z6A8064B5DF479455507DZ553 - add pandas series values to new dataframe column at end of pandas dataframe 如何将Series添加为DataFrame(Pandas)中的列? - How to add Series as a column in a DataFrame (Pandas)? 熊猫向数据框列添加系列 - Pandas add a series to dataframe column 如何使用一系列列名从 pandas dataframe 获取系列? - How to get a series from a pandas dataframe using a series of column names? 这是如何在不调用.Series() 的情况下从 Dataframe 隐式创建 Pandas 系列的? - How is this implicitly creating a Pandas Series from a Dataframe without calling .Series()? 如何在熊猫中添加来自其他数据框的值的列 - How to add a column in pandas with values taken from another dataframe 从系列或字典向数据帧添加一个新列,将我的系列索引和数据帧列映射到键熊猫 python - Add a new column to a dataframe from a Series or dictionary mapping my series index and a dataframe column to key pandas python 如何根据 Pandas 中第一个数据框中的某些列值从第二个数据框中添加列值 - How to add Column Values from 2nd DataFrame Based on Some Column Values in First dataframe in Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM