简体   繁体   English

如何根据其他列中的数据替换python大熊猫中的某些值?

[英]How do you replace certain values from row to row in python pandas based on data in other columns?

Right now I am working with python pandas and cannot find the solution to this issue: I have a dataframe df and I want to copy certain data from one row to another row (replacing) based on on a tag in another colomn of the same dataset. 现在,我正在使用python pandas,但找不到此问题的解决方案:我有一个数据框df,我想基于同一数据集另一列中的标签将某些数据从一行复制到另一行(替换) 。 An example is the best way to show it I think: 我认为,举个例子是最好的方式:

 Kind  Type       V1      V2    V3 

 AAA      AAA     7       8     4 

 BBB      AAA    4       8     1 

 CCC      AAA    3       1     7 

 AAA      BBB    2       5     3

 BBB      BBB    11      9     8 

 CCC      BBB    7       7     10

 AAA      CCC    5       8     2 

 BBB      CCC    2       3     7 

 CCC      CCC    4       10    6

What I want is replacing kind AAA with kind BBB for each Type but only for row Value3(V3). 我想要的是为每个类型用类型BBB替换类型AAA,但仅用于行Value3(V3)。 So that the result will be: 这样的结果将是:

 Kind     Type   V1     V2    V3 

 AAA      AAA    7       8     1 

 BBB      AAA    4       8     1 

 CCC      AAA    3       1     7 

 AAA      BBB    2       5     8

 BBB      BBB    11      9     8 

 CCC      BBB    7       7     10

 AAA      CCC    5       8     7 

 BBB      CCC    2       3     7 

 CCC      CCC    4       10    6

As you can see, these 3 number were replaced now, with values from kind BBB. 如您所见,这3个数字现已替换为BBB类型的值。

With

df.loc[df['Kind']=='AAA','V3'] = x  # x is numeric

I can only replace each V3 number in combination with kind AAA with number x, but this is not what I want. 我只能将每个V3号替换为带有数字x的AAA类,但这不是我想要的。 As the numbers are all different. 由于数字都是不同的。 Can someone help me here? 有人可以帮我吗? Thanks! 谢谢!

Filter DataFrame by boolean indexing and create Series for map only rows where Kind is AAA : 通过boolean indexing过滤DataFrame并创建Series以仅map KindAAA行:

s = df.loc[df['Kind'] == 'BBB', ['Type', 'V3']].set_index('Type')['V3']
print (s)
Type
AAA    1
BBB    8
CCC    7
Name: V3, dtype: int64

df.loc[df['Kind']=='AAA','V3'] = df['Type'].map(s)
print (df)
  Kind Type  V1  V2  V3
0  AAA  AAA   7   8   1
1  BBB  AAA   4   8   1
2  CCC  AAA   3   1   7
3  AAA  BBB   2   5   8
4  BBB  BBB  11   9   8
5  CCC  BBB   7   7  10
6  AAA  CCC   5   8   7
7  BBB  CCC   2   3   7
8  CCC  CCC   4  10   6

Here's an example using shift 这是使用shift的示例

df.loc[df['Kind'] == 'AAA', 'V3'] = df['V3'].shift(-1).fillna(0).astype(int)

    Kind    Type    V1  V2  V3
0    AAA     AAA    7   8   1
1    BBB     AAA    4   8   1
2    CCC     AAA    3   1   7
3    AAA     BBB    2   5   8
4    BBB     BBB    11  9   8
5    CCC     BBB    7   7   10
6    AAA     CCC    5   8   7
7    BBB     CCC    2   3   7
8    CCC     CCC    4   10  6

Note that this relies on your dataframe being sorted by kind and type, and that non-matches are given a default value of 0. 请注意,这取决于您的数据框按种类和类型排序,并且不匹配项的默认值为0。

暂无
暂无

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

相关问题 Pandas - 如何根据其他列减去行值? - Pandas - How to subtract row values based on other columns? 如何替换熊猫数据框中单个行和特定列的值 - how to replace values of an individual row and specific columns in pandas data frame pandas:根据其他列将多行中一个单元格的值替换为一个特定行 - pandas: replace one cell's value from mutiple row by one particular row based on other columns 熊猫根据同一行中其他列的值创建新列 - pandas creates new columns based on the values of the other columns in the same row 根据其他列的值创建新列/在 Pandas 中按行应用多列的 function - Create new column based on values from other columns / apply a function of multiple columns, row-wise in Pandas pandas dataframe根据相应行的其他列更新列值 - pandas dataframe update column values based on other columns of the corresponding row 大熊猫-如果同一行的其他值出现在第二个数据框中,则替换列的值 - Pandas - replace values of a column if other values from same row appear in second data frame 根据Pandas中的特定条件将值从一列复制到同一行中的其他列 - Copy values from one column to other columns in same row based on specific criteria in Pandas Pandas:根据其他列的值创建一个新列(按行) - Pandas: Create a new column based on values from other columns (Row wise) 将 pandas dataframe 中的某些值替换为该行的模式 - Replace certain values in pandas dataframe with mode of that row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM