[英]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
Kind
为AAA
行:
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.