Python 熊猫按拆分分组,获取用户输入,将列添加到子集,合并回df
我有一个df,它需要基于用户输入的值。这将是每次运行不同数据的程序的一部分。我遇到问题的代码是Python 熊猫按拆分分组,获取用户输入,将列添加到子集,合并回df,python,pandas,merge,Python,Pandas,Merge,我有一个df,它需要基于用户输入的值。这将是每次运行不同数据的程序的一部分。我遇到问题的代码是 for A, new_df in data.groupby(level=0): print A tariff=get_tariff() this=data.iloc[data.index.get_level_values('A') == A] #this adds a new column this['Tariff Added']= this['Price']
for A, new_df in data.groupby(level=0):
print A
tariff=get_tariff()
this=data.iloc[data.index.get_level_values('A') == A]
#this adds a new column
this['Tariff Added']= this['Price'] * tariff
pd.merge(data, this)
我有两个级别的索引。我在适当的级别上拆分数据,然后获得用户输入。这是我想不出来的。我需要每个级别=0来创建一个新列。在这段代码中,这变成了一个df-were-level=0只是适当的值。然后我想把它合并回原来的df(我意识到合并可能不对,我一次只做一步)
当我在终端运行这个时,我得到了这个
data_load.py:35: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
this['Tariff Added']= this['Price'] * tariff
然后是失败。我不知道为什么它说我正在制作一个副本,我正在使用.iloc声明一个新的df,并将其合并到原始文件中
我的一个想法是在开始之前创建“关税添加”列,但我想在这里查看一下为什么该代码不起作用
与我合作的df是
Quantity Price
A B
NB Well 123n5/15 100 1
123n5/15 200 2
123n5/15 300 3
456n5/15 100 1
456n5/15 200 2
456n5/15 300 3
excito 123n5/15 100 1
123n5/15 200 2
123n5/15 300 3
456n5/15 100 1
456n5/15 200 2
456n5/15 300 3
推荐的输出是什么?@jezrael我需要一个新列,该列的值根据a索引的不同而不同