简体   繁体   English

将基于索引的列添加到Pandas中的数据框

[英]Adding a column based on index to a data frame in Pandas

I have a data frame where I want to add a new column with values based on the index. 我有一个数据框,我想在其中添加一个基于索引的值的新列。

This is my fake df: 这是我的假df:

{'fruit': [
'Apple', 'Kiwi', 'Clementine', 'Kiwi', 'Banana', 'Clementine', 'Apple', 'Kiwi'],
'bites': [1, 2, 3, 1, 2, 3, 1, 2]})

I have found a similar question and tried the solution there but I get error messages. 我发现了一个类似的问题,并在那里尝试了解决方案,但收到错误消息。 This is what I tried: 这是我尝试的:

conds = [(my.index >= 0) & (my.index <= row_2),
         (my.index > row_2) & (my.index<=row_5),
         (my.index > row_5) & (my.index<=row_6),
         (my.index > row_6)]


names = ['Donna', 'Kelly', 'Andrea','Brenda']


my['names'] = np.select(conds, names)

For me it working nice (variables changed to numeric), also added alternative solutions with cut with include_lowest=True parameter for match 0 value and selecting by DataFrame.loc : 对我来说,它工作得很好(变量更改为数字),还添加了带有cut替代解决方案,其中include_lowest=True参数用于匹配0值,并通过DataFrame.loc选择:

conds = [(my.index >= 0) & (my.index <= 2),
         (my.index > 2) & (my.index<=5),
         (my.index > 5) & (my.index<=6),
         (my.index > 6)]


names = ['Donna', 'Kelly', 'Andrea','Brenda']


my['names'] = np.select(conds, names)
my['names1'] = pd.cut(my.index, [0,2,5,6,np.inf], labels=names, include_lowest=True)

my.loc[:2, 'names2'] = 'Donna'
my.loc[3:5, 'names2'] = 'Kelly'
my.loc[6:7, 'names2'] = 'Andrea'
my.loc[7:, 'names2'] = 'Brenda'

print (my)
        fruit  bites   names  names1  names2
0       Apple      1   Donna   Donna   Donna
1        Kiwi      2   Donna   Donna   Donna
2  Clementine      3   Donna   Donna   Donna
3        Kiwi      1   Kelly   Kelly   Kelly
4      Banana      2   Kelly   Kelly   Kelly
5  Clementine      3   Kelly   Kelly   Kelly
6       Apple      1  Andrea  Andrea  Andrea
7        Kiwi      2  Brenda  Brenda  Brenda

You can try pd.cut : 您可以尝试pd.cut

df['names'] = (pd.cut(df.index, 
                      [0, 2, 5, 6, np.inf], 
                      labels=names)
                 .fillna(names[0])
              )

暂无
暂无

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

相关问题 根据另一列的值向python pandas数据框添加一列 - Adding a column to a python pandas data frame based on the value of another column 基于多级索引在熊猫数据框中添加行 - Adding Rows in pandas data frame based on multilevel index Pandas 数据框根据其他 2 列的数据添加一列 - Pandas data frame adding a column based on data from 2 other columns 根据列名过滤数据框,而不使用pandas中的索引 - Filter the data frame based on the column names without using index in pandas Python Pandas-根据索引替换部分数据框列的值 - Python Pandas - Replacing values of a part of data frame column based on index 根据 Pandas 数据框中的一个月添加带有布尔值的列 - Adding column with boolean values, based on a month in pandas data frame 大熊猫:根据索引和列将一个数据框的值替换为另一数据框的值 - Pandas: replace values of one data frame with values of another data frame based on index and column 将pandas数据框的特定列的所有值相加,然后用总和除以索引(pandas,python)-在数据帧中添加另一个列 - Adding all values for a particular column of a pandas data frame and dividing by sum by index (pandas, python) - ADDING AS ANOTHER COLUMN IN DATAFRAME 根据 pandas 中的索引列表添加新列 - Adding new column based on index list in pandas “是否存在一个熊猫函数,用于基于数据帧的另一列的某些值添加新列?” - “Is there an pandas function for adding a new column based on certain values of another column of the data frame?”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM