简体   繁体   English

pandas数据框基于对其他列的计算来添加新列,并避免链接索引

[英]pandas dataframe add new column based on calulation on other column and avoid chained index

I have a pandas dataframe and I need to add a new column, which would be based on calculation of specific columns,indicated by a column 'site'. 我有一个pandas数据框,我需要添加一个新列,该列将基于特定列的计算,由“站点”列指示。 I have found a way to do this with resort to numpy, but always it gives warning about chained index. 我已经找到了一种使用numpy来做到这一点的方法,但始终会给出有关链索引的警告。 I am sure there should be better solution, please help if you know. 我相信应该有更好的解决方案,如果您知道的话,请提供帮助。

df_num_bin1['Chip_id_3']=np.where(df_num_bin1[key_site_num]==1,df_num_bin1[WB_89_S1]*0x100+df_num_bin1[WB_78_S1],df_num_bin1[WB_89_S2]*0x100+df_num_bin1[WB_78_S2])
df_num_bin1['Chip_id_2']=np.where(df_num_bin1[key_site_num]==1,df_num_bin1[WB_67_S1]*0x100+df_num_bin1[WB_56_S1],df_num_bin1[WB_67_S2]*0x100+df_num_bin1[WB_56_S2])
df_num_bin1['Chip_id_1']=np.where(df_num_bin1[key_site_num]==1,df_num_bin1[WB_45_S1]*0x100+df_num_bin1[WB_34_S1],df_num_bin1[WB_45_S2]*0x100+df_num_bin1[WB_34_S2])
df_num_bin1['Chip_id_0']=np.where(df_num_bin1[key_site_num]==1,df_num_bin1[WB_23_S1]*0x100+df_num_bin1[WB_12_S1],df_num_bin1[WB_23_S2]*0x100+df_num_bin1[WB_12_S2])
df_num_bin1['mac_low']=(df_num_bin1['Chip_id_1'].map(int) % 0x10000) *0x100+df_num_bin1['Chip_id_0'].map(int) // 0x1000000

The code above have 2 issues: 上面的代码有2个问题:

1: Here the value of column [key_site_num] determines which columns I should extract chip id data from. 1:此处[key_site_num]列的值确定了我应该从中提取芯片ID数据的列。 In this example it is only of site 0 or 1, but actually it could be 2 or 3 as well. 在此示例中,它仅是站点0或1,但实际上也可以是2或3。 I would need a general solution. 我需要一个一般的解决方案。

2: it generates chained index warning; 2:生成链接索引警告;

C:\Anaconda2\lib\site-packages\ipykernel\__main__.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

Well, i´m not too sure about your first quest but i think that this will help you. 好吧,我不太确定您的第一个任务,但我认为这会对您有所帮助。

import pandas as pd
reader = pd.read_csv(path,engine='python')
reader['new'] = reader['treasury.maturity.rate']+reader['bond.yield']
reader.to_csv('test.csv',index=False)

As you can see,you don´t need get the values before operate with them only reference the column where they are; 如您所见,在使用它们之前,无需获取值,只需引用它们所在的列即可。 and to do the same for only a specific row you could filter the dataframe before create the new column. 并仅对特定行执行相同操作,则可以在创建新列之前过滤数据框。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM