简体   繁体   English

Python Pandas dataframe - 根据索引值添加新列

[英]Python Pandas dataframe - add a new column based on index value

I have a Python pandas dataframe that looks like this:我有一个 Python pandas dataframe 看起来像这样:

      year_2000  year_1999  year_1998  year_1997  year_1996  year_1995 (MANH, stock name)
MANH 454.47     -71.90     nan        nan        nan        nan         TEST             
LH   385.52     180.95     -24.14     -41.67     -68.92     -26.47      TEST             
DGX  373.33     68.04      4.01       nan        nan        nan         TEST             
SKX  306.56     nan        nan        nan        nan        nan         TEST 

where the stock tickers are the index.其中股票代码是指数。 I want to add the name of each stock as a new column我想将每只股票的名称添加为新列

I tried adding the stock name column via yearly_best['MANH','stock name']='TEST' but it adds the same name in all rows.我尝试通过yearly_best['MANH','stock name']='TEST'添加股票名称列,但它在所有行中添加了相同的名称。

I have a dictionary called ticker_name which contains the tickers and the names我有一本名为ticker_name 的字典,其中包含代码和名称

Out[65]: 
{'TWOU': '2U',
 'MMM': '3M',
 'ABT': 'Abbott Laboratories',
 'ABBV': 'AbbVie Inc.',
 'ABMD': 'Abiomed',
 'ACHC': 'Acadia Healthcare',
 'ACN': 'Accenture',
 'ATVI': 'Activision Blizzard',
 'AYI': 'Acuity Brands',
 'ADNT': 'Adient',

thus I would like to get the names from the dict and put then in a column in the dataframe.因此,我想从字典中获取名称,然后将其放入 dataframe 的列中。 How can I do that?我怎样才能做到这一点?

As the key of your dictionnary are index of your dataFrame you can try:由于您的字典的键是您的 dataFrame 的索引,您可以尝试:

d = {'TWOU': '2U',
'MMM': '3M',
'ABT': 'Abbott Laboratories',
'ABBV': 'AbbVie Inc.',
'ABMD': 'Abiomed',
'ACHC': 'Acadia Healthcare',
'ACN': 'Accenture',
'ATVI': 'Activision Blizzard',
'AYI': 'Acuity Brands',
'ADNT': 'Adient',}

df['stock name'] = pd.Series(d)

You can try:你可以试试:

# Create a new column "stock_name" with the index values
yearly_best['stock_name'] = yearly_best.index
# Replace the "stock_name" values based on the dictionary
yearly_best['stock_name'].map(ticker_name, inplace=True)

Note that in this case, the dataframe's indices will remain as they were (stock tickers).请注意,在这种情况下,数据框的索引将保持原样(股票代码)。 If you would like to replace the indices with row numbers, consider using reset_index如果您想用行号替换索引,请考虑使用reset_index

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

相关问题 pandas:根据相同 dataframe 的日期时间索引查找添加新列 - pandas: add new column based on datetime index lookup of same dataframe 熊猫根据索引向“数据框”列添加值,如果值存在,则追加 - Pandas Add value to Dataframe column based on index and if value exists then append 基于列值的新二级索引(熊猫数据框) - New secondary index based on column value (pandas dataframe) 根据groupby值将新列添加到pandas数据框中 - add a new column to a pandas dataframe based on groupby value 基于变量值的新 python pandas 数据框列,使用函数 - new python pandas dataframe column based on value of variable, using function 从系列或字典向数据帧添加一个新列,将我的系列索引和数据帧列映射到键熊猫 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:使用基于索引的函数添加新列 - Pandas : Add new column with function based on index Pandas (python):如何将列添加到数据帧以进行索引? - Pandas (python): How to add column to dataframe for index? pandas数据框基于对其他列的计算来添加新列,并避免链接索引 - pandas dataframe add new column based on calulation on other column and avoid chained index Python Pandas:根据DataFrame中的值查找索引 - Python Pandas: Find index based on value in DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM