Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.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_Dataframe - Fatal编程技术网

Python 使用列名获取列值

Python 使用列名获取列值,python,pandas,dataframe,Python,Pandas,Dataframe,我有以下数据: production_type type_a type_b type_c type_d 0 type_a 1.173783 0.714846 0.583621 1 1 type_b 1.418876 0.864110 0.705485 1 2 type_c 1.560452

我有以下数据:

    production_type       type_a       type_b     type_c     type_d     
0             type_a     1.173783    0.714846    0.583621        1
1             type_b     1.418876    0.864110    0.705485        1
2             type_c     1.560452    0.950331    0.775878        1
3             type_d     1.750531    1.066091    0.870388        1
4             type_a     1.797883    1.094929    0.893932        1
5             type_a     1.461784    0.890241    0.726819        1
6             type_b     0.941938    0.573650    0.468344        1
7             type_a     0.507370    0.308994    0.252271        1
8             type_c     0.443565    0.270136    0.220547        1
9             type_d     0.426232    0.259579    0.211928        1
10            type_d     0.425379    0.259060    0.211504        1
我想创建一个新的列、列表或序列来返回该列的值

输出

    production_type       type_a       type_b     type_c     type_d     Results 
0             type_a     1.173783    0.714846    0.583621        1     1.173783    
1             type_b     1.418876    0.864110    0.705485        1     0.864110    
2             type_c     1.560452    0.950331    0.775878        1     0.775878        
3             type_d     1.750531    1.066091    0.870388        1     1
4             type_a     1.797883    1.094929    0.893932        1     1.797883
5             type_a     1.461784    0.890241    0.726819        1     1.461784
6             type_b     0.941938    0.573650    0.468344        1     0.573650
基本上,如果在[production\u type]列中写入类型_a,我希望在[results]列中返回类型_a的结果

我尝试了以下方法:

for i in df:
    if i == 'type_a':
        print ('type_a')
    elif i == 'type_b':
        print ('type_b')
    elif i == 'type_c':
        print ('type_c')
    elif i == 'type_d':
        print ('type_d')
    else:
        print('')  
    print('')   
使用result.append

要生成数据帧,请使用以下命令:

list_cols = ['type_a','type_b','type_c']
df = pd.DataFrame(np.random.rand(10, 3), columns = list_cols )
df['production_type']= ['type_a','type_b','type_c','type_d','type_a','type_a','type_b'
                       ,'type_b','type_c','type_d']
 df['type_d'] = 1
 df['results'] = ''

关于在哪里搜索的任何提示?

您可以使用
pd.DataFrame。为此应用

df['Results'] = df.apply(lambda row: row.get(row['production_type']), axis=1)
解释

  • pd.DataFrame.apply
    with
    axis=1
    将函数应用于每行,并通过隐式循环提取每行的组件
  • 该方法允许匿名
    lambda
    函数作为参数
  • 我们可以定义
    lambda
    函数,从
    production\u type
    列中提取所需的值
你可以试试

result = list()
index =0
for i in df['production_type']:
    value = df[i][index]
    index = index+1
    result.append(value)

df['Results'] = pd.DataFrame(result)

通过传递
lambda
函数,可以使用
map
方法

df['Results'] = df.index.map(lambda index : df[df['production_type'][index]][index])

您能否使用问题中的df.values将此数据帧添加为numpy数组。使用它在我的机器上创建数据帧比只键入数据帧更容易。1。读取csv文件,2。计算结果,3。编写一个新的csv文件。我没有你的csvfile@jat按住Ctrl+C组合键单击数据框,然后使用
df=pd.read_clipboard()
@jpp感谢您的反馈。使用lambda真的很有帮助。