Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.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 按列表在pandas.DataFrame中建立索引_Python_Python 3.x_Pandas_Slice - Fatal编程技术网

Python 按列表在pandas.DataFrame中建立索引

Python 按列表在pandas.DataFrame中建立索引,python,python-3.x,pandas,slice,Python,Python 3.x,Pandas,Slice,我想根据另一列(“列搜索”)中的值,在pd.DataFrame中更改一列(“列更改”)的值(转换为“新值”)。对于单个更改,我有一个解决方案,但我正在搜索多个搜索值的解决方案 预期的单个值示例: import numpy as np import pandas as pd my_array = np.array([[1,2,3,4,5,6,7,8,9,10],[11,22,33,44,55,66,77,88,99,100]]) my_df = pd.DataFrame(my_array, col

我想根据另一列(“列搜索”)中的值,在pd.DataFrame中更改一列(“列更改”)的值(转换为“新值”)。对于单个更改,我有一个解决方案,但我正在搜索多个搜索值的解决方案

预期的单个值示例:

import numpy as np
import pandas as pd
my_array = np.array([[1,2,3,4,5,6,7,8,9,10],[11,22,33,44,55,66,77,88,99,100]])
my_df = pd.DataFrame(my_array, columns = ['col_change', 'col_search'])
my_df.col_change[my_df.col_search == 22] = 'new value'
print(my_df)
多值不按预期工作的示例:“in”运算符在此处不工作

import numpy as np
import pandas as pd
my_array = np.array([[1,2,3,4,5,6,7,8,9,10],[11,22,33,44,55,66,77,88,99,100]])
my_df = pd.DataFrame(my_array, columns = ['col_change', 'col_search'])
list_of_search = [33, 44, 55]
my_df.col_change[my_df.col_search in list_of_search] = 'new value'
print(my_df)

使用
df.columns.isin

In [1083]: my_df.loc[my_df.col_search.isin([33, 44, 55]), 'col_change'] = 'new value'

In [1084]: my_df
Out[1084]: 
  col_change  col_search
0          1          11
1          2          22
2  new value          33
3  new value          44
4  new value          55
5          6          66
6          7          77
7          8          88
8          9          99
9         10         100

使用
df.columns.isin

In [1083]: my_df.loc[my_df.col_search.isin([33, 44, 55]), 'col_change'] = 'new value'

In [1084]: my_df
Out[1084]: 
  col_change  col_search
0          1          11
1          2          22
2  new value          33
3  new value          44
4  new value          55
5          6          66
6          7          77
7          8          88
8          9          99
9         10         100

在python中应该是
isin
不在,更改为
my_-df.col\u-change[my_-df.col\u-search.isin(搜索列表)]='new value'
只需将行更改为my_-df.col\u-change[my_-df.col\u-search.isin(搜索列表)]='python中的'new value'应该是
isin
my-df.col-search.isin(搜索列表)]='new value'只需将in行更改为my_df.col\u change[my_df.col\u search.isin(搜索列表)]='new value'不幸的是,这两个选项在这里都不起作用。我已经测试了选项2。不幸的是,这两个选项在这里都不起作用。我已经测试了选项2。