Python Pandas:在其他列的基础上添加列 [英] Python Pandas: Add column based on other column

查看:239
本文介绍了Python Pandas:在其他列的基础上添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是熊猫的新手,对此很困惑,尤其是与列表和列表理解相比.

I'm new to pandas and pretty confused about it especially compared to lists and using list comprehensions.

我有一个包含4列的数据框.我想基于第四列"m"创建第五列"c".通过对列"m"中的每一行应用函数,可以获得"c"的值.

I have a dataframe with 4 columns. I want to create a 5th column "c" based on 4th column "m". I can get the value for "c" by applying my function for each row in column "m".

如果"m"是列表,并且使用列表理解,则为

If "m" was a list and using list comprehension it would be

c = [myfunction(x) for x in m]

如何将这种逻辑"应用于数据框?

How do I do apply this "logic" to a dataframe?

推荐答案

您可以 assign -来自doc的示例:

import pandas as pd
import numpy as np

df = pd.DataFrame({'A': range(1, 11), 'B': np.random.randn(10)})
print df
    A         B
0   1  0.769028
1   2 -0.392471
2   3  0.153051
3   4 -0.379848
4   5 -0.665426
5   6  0.880684
6   7  1.126381
7   8 -0.559828
8   9  0.862935
9  10 -0.909402

df = df.assign(ln_A = lambda x: np.log(x.A))
print df
    A         B      ln_A
0   1  0.769028  0.000000
1   2 -0.392471  0.693147
2   3  0.153051  1.098612
3   4 -0.379848  1.386294
4   5 -0.665426  1.609438
5   6  0.880684  1.791759
6   7  1.126381  1.945910
7   8 -0.559828  2.079442
8   9  0.862935  2.197225
9  10 -0.909402  2.302585

apply 作为<一个href ="https://stackoverflow.com/questions/35424567/python-pandas-add-column-based-on-other-column/35426050#comment58548804_35424567">路奇进行了评论.

有时lambda功能很有帮助:

import pandas as pd
import numpy as np

df = pd.DataFrame({'A': range(1, 11), 'B': np.random.randn(10)})

df['ln_A'] = df['A'].apply(np.log)
df['round'] = df['B'].apply(lambda x: np.round(x, 2))
print df

    A         B      ln_A  round
0   1 -0.982828  0.000000  -0.98
1   2  2.306111  0.693147   2.31
2   3  0.967858  1.098612   0.97
3   4 -0.286280  1.386294  -0.29
4   5 -2.026937  1.609438  -2.03
5   6  0.061735  1.791759   0.06
6   7 -0.506620  1.945910  -0.51
7   8 -0.309438  2.079442  -0.31
8   9 -1.261842  2.197225  -1.26
9  10  1.079921  2.302585   1.08

这篇关于Python Pandas:在其他列的基础上添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆