简体   繁体   English

DataFrame:根据第 3 列中的值确定的动态列更新一列值

[英]DataFrame: update one column value based on dynamic column determined by value in 3rd column

Consider the following dataframe created from a dictionary考虑以下从字典创建的数据框

d = { 'p_symbol': ['A','B','C','D','E']
     , 'p_volume': [0,0,0,0,0]
     , 'p_exchange': ['IEXG', 'ASE', 'PSE', 'NAS', 'NYS']
     , 'p_volume_rh': [1000,1000,1000,1000,1000]
     , 'p_volume.1': [2000,2000,2000,2000,2000]
     , 'p_volume.2': [3000,3000,3000,3000,3000]
     , 'p_volume.3': [4000,4000,4000,4000,4000]
     , 'p_volume.4': [5000,5000,5000,5000,5000]
     }

snapshot = pd.DataFrame(d)

I need to set the value in p_volume to be the value in one of the last 5 p_volume* columns based on the value in p_exchange.我需要根据 p_exchange 中的值将 p_volume 中的值设置为最后 5 个 p_volume* 列之一中的值。 I need to do it this way due to the way data is being returned from a third party vendor API over which I have no control.由于我无法控制的第三方供应商 API 返回数据的方式,我需要这样做。

I have tried setting up a dictionary that given the value in p_exchange gives me the column name with the resulting code tried我试过设置一个字典,给出 p_exchange 中的值给我列名,结果代码尝试

us_primary_exchange_map = {
    "NYS": "xp_volume_rh"
    , "NAS": "xp_volume.1"
    , "PSE": "xp_volume.2"
    , "ASE": "xp_volume.3"
    , "IEXG": "xp_volume.4"
    }

snapshot["p_volume"] = snapshot[us_primary_exchange_map[snapshot["p_exchange"]]])

But this does not work...但这不起作用......

Can someone help me out here?有人可以帮我吗? Is there a straightforward way to do this without having to iterate over the rows?有没有一种直接的方法可以做到这一点而不必遍历行?

I hope I've understood your question right (and xp_volume_* is a typo, should be p_volume_* without x ?):我希望我已经正确理解了您的问题(并且xp_volume_*是一个错字,应该是没有x p_volume_* ?):

snapshot['p_volume'] = snapshot.lookup(snapshot.index, snapshot['p_exchange'].map(us_primary_exchange_map))
print(snapshot)

Prints:印刷:

  p_symbol  p_volume p_exchange  ...  p_volume.2  p_volume.3  p_volume.4
0        A      5000        NYS  ...        3000        4000        5000
1        B      4000        ASE  ...        3000        4000        5000
2        C      3000        PSE  ...        3000        4000        5000
3        D      2000        NAS  ...        3000        4000        5000
4        E      1000        NYS  ...        3000        4000        5000

[5 rows x 8 columns]

You can use pandas.dataframe.apply with argument axis=1 to apply a function on dataframe rows:您可以使用带有参数axis=1 pandas.dataframe.apply在数据帧行上应用函数:

snapshot['p_volume'] = snapshot.apply(lambda row: snapshot.loc[row.name,
us_primary_exchange_map[row['p_exchange']]], axis=1)

And your dataframe will look like:您的数据框将如下所示:

  p_symbol  p_volume p_exchange  p_volume_rh  p_volume.1  p_volume.2  \
0        A      5000       IEXG         1000        2000        3000   
1        B      4000        ASE         1000        2000        3000   
2        C      3000        PSE         1000        2000        3000   
3        D      2000        NAS         1000        2000        3000   
4        E      1000        NYS         1000        2000        3000   

   p_volume.3  p_volume.4  
0        4000        5000  
1        4000        5000  
2        4000        5000  
3        4000        5000  
4        4000        5000  

I'm not sure it's more efficient than iterate the rows, but I think it's prettier.我不确定它是否比迭代行更有效,但我认为它更漂亮。

暂无
暂无

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

相关问题 根据条件使用另一个数据帧列值更新一个数据帧值 - Update one dataframe value with another dataframe column value based on the condition 比较来自相同 pandas dataframe 的 2 列的值和基于比较的第 3 列的返回值 - comparing values of 2 columns from same pandas dataframe & returning value of 3rd column based on comparison 将一个 Dataframe 的列乘以另一个 Dataframe 的值,由键确定 - Multiply Column of One Dataframe by a Value from Another Dataframe, Determined by a Key 基于第三个值的 New_target 列 - New_target column based on 3rd value 根据 Python 中的第三列值设置 Plotly 条形颜色 - Set Plotly Bar Color Based on 3rd Column Value in Python 从另一列计算数据帧列中的值,但前提是满足第三列中的条件 - computing a value in a dataframe colum from another column, but only if a condition in a 3rd column is met 是否有一种SQL语法会根据同一表中第3列的相等值搜索2列来创建新列? - Is there a SQL syntax that will create new column by searching in 2 columns based on equal value of 3rd column in same table? Pandas:如果来自第三列的字符串值,则根据另一列的值创建列 - Pandas : Create columns based on values of another column if string value from 3rd column 尝试基于多个/两个数据框列值更新一个数据框列值时如何解决 ZeroDivisionError? - How to resolve ZeroDivisionError while trying to update one dataframe column value based on multiple / two dataframe column values? 根据其他列中的值更新 dataframe 中的元素 - Update element in dataframe based on value in orther column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM