Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 提取和转换特定列中的值_Python_Pandas_Numpy - Fatal编程技术网

Python 提取和转换特定列中的值

Python 提取和转换特定列中的值,python,pandas,numpy,Python,Pandas,Numpy,我有一个数据帧df,我想在这里找到一个特定的模式,然后用一个新的模式替换它。例如,任何包含:2X6.4T的值都应替换为12T,但如果包含“word”WOW,则不应替换 数据: 期望的: 做: 但是,这会消除:AY12-AYY-AY-R800-900G-2X6.4T18T-R6-v.A的全部值,并且不会搜索和替换 感谢您的任何建议,我仍在对此进行研究。请使用np.wherecondition、string if condition true、string if condition false。对

我有一个数据帧df,我想在这里找到一个特定的模式,然后用一个新的模式替换它。例如,任何包含:2X6.4T的值都应替换为12T,但如果包含“word”WOW,则不应替换

数据:

期望的:

做:

但是,这会消除:AY12-AYY-AY-R800-900G-2X6.4T18T-R6-v.A的全部值,并且不会搜索和替换

感谢您的任何建议,我仍在对此进行研究。

请使用np.wherecondition、string if condition true、string if condition false。对于选择,请使用.str,contains

正如@TrentonMcKinney所建议的,您还可以使用df.where

df['Type']=df.where(df['Type'].str.contains('WOW'),df['Type'].str.replace('2X6.4T','12T'), axis=0)
                                  Type
0      AY12-AYY-AY-R800-900G-12T_18T-R6-v.A
1      AY12-AYY-AY-R800-900G-12T_18T-R6-v.A
2      AY12-AYY-AY-R800-900G-12T_18T-R6-v.A
3  AY12-AYY-AY-R800XD-900G-6.4T-R11-WOW-v.A
4  AY12-AYY-AY-R800XD-900G-6.4T-R11-WOW-v.A
请使用np.wherecondition,如果条件为真,则使用字符串,如果条件为假,则使用字符串。对于选择,请使用.str,包含

正如@TrentonMcKinney所建议的,您还可以使用df.where

df['Type']=df.where(df['Type'].str.contains('WOW'),df['Type'].str.replace('2X6.4T','12T'), axis=0)
                                  Type
0      AY12-AYY-AY-R800-900G-12T_18T-R6-v.A
1      AY12-AYY-AY-R800-900G-12T_18T-R6-v.A
2      AY12-AYY-AY-R800-900G-12T_18T-R6-v.A
3  AY12-AYY-AY-R800XD-900G-6.4T-R11-WOW-v.A
4  AY12-AYY-AY-R800XD-900G-6.4T-R11-WOW-v.A
其中一个选项是.mask,如果条件在某个列本身上,我通常使用它对该列进行条件更改。这是因为默认选项是现有列值

df['Type'] =  df['Type'].mask(~df['Type'].str.contains('WOW'),
                              df['Type'].str.replace('2X6.4T','12T'))
df
Out[1]: 
                                       Type
0      AY12-AYY-AY-R800-900G-12T_18T-R6-v.A
1      AY12-AYY-AY-R800-900G-12T_18T-R6-v.A
2      AY12-AYY-AY-R800-900G-12T_18T-R6-v.A
3  AY12-AYY-AY-R800XD-900G-6.4T-R11-WOW-v.A
4  AY12-AYY-AY-R800XD-900G-6.4T-R11-WOW-v.A
回答时使用。其中类似于面具的反面:

 df['Type'] =  df['Type'].where(df['Type'].str.contains('WOW'),
                                df['Type'].str.replace('2X6.4T','12T'))
其中一个选项是.mask,如果条件在某个列本身上,我通常使用它对该列进行条件更改。这是因为默认选项是现有列值

df['Type'] =  df['Type'].mask(~df['Type'].str.contains('WOW'),
                              df['Type'].str.replace('2X6.4T','12T'))
df
Out[1]: 
                                       Type
0      AY12-AYY-AY-R800-900G-12T_18T-R6-v.A
1      AY12-AYY-AY-R800-900G-12T_18T-R6-v.A
2      AY12-AYY-AY-R800-900G-12T_18T-R6-v.A
3  AY12-AYY-AY-R800XD-900G-6.4T-R11-WOW-v.A
4  AY12-AYY-AY-R800XD-900G-6.4T-R11-WOW-v.A
回答时使用。其中类似于面具的反面:

 df['Type'] =  df['Type'].where(df['Type'].str.contains('WOW'),
                                df['Type'].str.replace('2X6.4T','12T'))

还有一个,它取消了单独导入numpy的要求。@TrentonMcKinney或.maskThanks@Trenton McKinney,很好的提醒。将添加选项nice one@David EricksonThere is,这将取消单独导入numpy的要求。@Trenton McKinney或.maskThanks@Trenton McKinney,很好的提醒。将添加选项nice one@David EricksonI有一个脑屁。现在输出应该是正确的:我有个屁。现在输出应该是正确的:
 df['Type'] =  df['Type'].where(df['Type'].str.contains('WOW'),
                                df['Type'].str.replace('2X6.4T','12T'))