简体   繁体   English

如何创建一个新的数据框列,并从另一个列中移出值?

[英]How to create a new dataframe column with shifted values from another column?

I am returning data from a database query and want to create a new column in the resulting dataframe. 我正在从数据库查询返回数据,并想在结果数据框中创建一个新列。 I need to shift the results of one column forward one month to create a new column. 我需要将一个专栏的结果向前移动一个月才能创建一个新专栏。

I have a dataframe that is populated from a sql query and has the format: 我有一个从SQL查询填充的数据框,其格式为:

df.dtypes
ACTIVITY_MONTH     datetime64[ns]
PRODUCT_KEY                object
COUNT                 float64

When I run: 当我跑步时:

df['NEW_COUNT'] = df.groupby('PRODUCT_KEY')['COUNT'].shift(+1)

I get this error: 我收到此错误:

ValueError: cannot reindex from a duplicate axis

This error doesn't make sense to me and I am not sure what to do to fix it. 这个错误对我来说没有任何意义,我不确定该如何解决。 Any help is appreciated. 任何帮助表示赞赏。

The error ValueError: cannot reindex from a duplicate axis indicates in this case that you have duplicate entries in your index (and for this reason, it cannot assign to a new column, as pandas cannot know where to place the values for the duplicate entries). 错误ValueError: cannot reindex from a duplicate axis重新编制索引表明在这种情况下,索引中有重复的条目(由于这个原因,它无法分配给新的列,因为熊猫无法知道将重复的条目的值放在何处) 。

To check for duplicate values in the index, you can do: 要检查索引中的重复值,可以执行以下操作:

df.index.get_duplicates()

And then to get rid of the duplicate values (if you don't need to keep the original index), you can eg do df.reset_index(drop=True) , or you can use ignore_index=True in append or concat . 然后要摆脱重复的值(如果不需要保留原始索引),可以例如执行df.reset_index(drop=True) ,也可以在appendconcat使用ignore_index=True

暂无
暂无

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

相关问题 从移位的现有列创建新的 dataframe 列 - Create new dataframe column from a shifted existing column 如何从另一列的所有值创建新的列名并按 pandas dataframe 中的另一列创建新列名? - how to create new column names from another column all values and agg by another column in pandas dataframe? 如果来自另一个 dataframe 的列和来自原始 dataframe 的列具有匹配值,则在原始 dataframe 中创建一个新列 - Create a new column in the original dataframe if the column from another dataframe and a column from original dataframe have matching values 如何基于另一列的值在pandas dataframe列中创建新值 - How to create new values in a pandas dataframe column based on values from another column 如何使用另一个 dataframe 中的值在 dataframe 中创建新列? - How do I create a new column in a dataframe using values from another dataframe? 通过解析列值为数据框创建新列,并使用来自另一列python的值填充新列 - Create new columns for a dataframe by parsing column values and populate new columns with values from another column python 根据另一列中的“NaN”值在 Pandas Dataframe 中创建一个新列 - Create a new column in Pandas Dataframe based on the 'NaN' values in another column 根据数据框中另一列的值创建一个新列 - Create a new column based on the values of another column in a dataframe 从列中的最大值创建新的 dataframe - Create new dataframe from the highest values in a column 如何从 dataframe 中的另一列按条件创建新组? - How to create new group by condition from another column in dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM