python pandas对列的操作 [英] python pandas operations on columns
问题描述
我想知道使用 Pandas 对 Python 中的列进行操作的最佳方法.
我有一个经典数据库,我已将其作为数据框加载,并且我经常需要对每一行进行操作,如果标记为A"的列中的值大于 x,则将该值替换为C"列减去D"列
现在我做类似的事情
for i in len(df.index):如果 df.ix[i,'A'] >X :df.ix[i,'A'] = df.ix[i,'C'] - df.ix[i,'D']
我想知道是否有一种更简单的方法来执行此类操作,更重要的是,由于我拥有大型数据库,因此是最有效的方法
我曾尝试不使用 for i 循环,就像在 R 或 Stata 中一样,有人建议我使用a.any"或a.all",但我在此处或 Pandas 文档中都没有找到任何内容.>
提前致谢.
在我看来最简单.
from random import randint、randrange、uniform将熊猫导入为 pd将 numpy 导入为 npdf = pd.DataFrame({'a':randrange(0,10),'b':randrange(10,20),'c':np.random.randn(10)})#如果 colC >0,5,然后 ColC = ColB - 可乐df['c'][df['c'] >0.5] = df['b'] - df['a']
经过测试,有效.
a b c2 11 -0.5763092 11 -0.5784492 11 -1.0858222 11 9.0000002 11 9.0000002 11 -1.081405
Hi I would like to know the best way to do operations on columns in python using pandas.
I have a classical database which I have loaded as a dataframe, and I often have to do operations such as for each row, if value in column labeled 'A' is greater than x then replace this value by column'C' minus column 'D'
for now I do something like
for i in len(df.index):
if df.ix[i,'A'] > x :
df.ix[i,'A'] = df.ix[i,'C'] - df.ix[i, 'D']
I would like to know if there is a simpler way of doing these kind of operations and more importantly the most effective one as I have large databases
I had tried without the for i loop, like in R or Stata, I was advised to use "a.any" or "a.all" but I did non find anything either here or in the pandas docs.
Thanks by advance.
simplest according to me.
from random import randint, randrange, uniform
import pandas as pd
import numpy as np
df = pd.DataFrame({'a':randrange(0,10),'b':randrange(10,20),'c':np.random.randn(10)})
#If colC > 0,5, then ColC = ColB - Cola
df['c'][df['c'] > 0.5] = df['b'] - df['a']
Tested, it works.
a b c
2 11 -0.576309
2 11 -0.578449
2 11 -1.085822
2 11 9.000000
2 11 9.000000
2 11 -1.081405
这篇关于python pandas对列的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!