在 Pandas DataFrame 中转换列值的最有效方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35639588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:46:11  来源:igfitidea点击:

Most efficient way to convert values of column in Pandas DataFrame

pythonpandasintdataframe

提问by O.rka

I have a a pd.DataFrame that looks like:

我有一个 pd.DataFrame 看起来像:

enter image description here

在此处输入图片说明

I want to create a cutoff on the values to push them into binary digits, my cutoff in this case is 0.85. I want the resulting dataframe to look like:

我想在值上创建一个截止值以将它们推入二进制数字,在这种情况下我的截止值是0.85. 我希望生成的数据框看起来像:

enter image description here

在此处输入图片说明

The script I wrote to do this is easy to understand but for large datasets it is inefficient. I'm sure Pandas has some way of taking care of these types of transformations.

我为此编写的脚本很容易理解,但对于大型数据集来说效率很低。我确信 Pandas 有某种方法来处理这些类型的转换。

Does anyone know of an efficient way to convert a column of floats to a column of integers using a threshold?

有谁知道使用阈值将一列浮点数转换为一列整数的有效方法?

My extremely naive way of doing such a thing:

我做这种事情的极其天真的方式:

DF_test = pd.DataFrame(np.array([list("abcde"),list("pqrst"),[0.12,0.23,0.93,0.86,0.33]]).T,columns=["c1","c2","value"])
DF_want = pd.DataFrame(np.array([list("abcde"),list("pqrst"),[0,0,1,1,0]]).T,columns=["c1","c2","value"])


threshold = 0.85

#Empty dataframe to append rows
DF_naive = pd.DataFrame()
for i in range(DF_test.shape[0]):
    #Get first 2 columns
    first2cols = list(DF_test.ix[i][:-1])
    #Check if value is greater than threshold
    binary_value = [int((bool(float(DF_test.ix[i][-1]) > threshold)))]
    #Create series object
    SR_row = pd.Series( first2cols + binary_value,name=i)
    #Add to empty dataframe container
    DF_naive = DF_naive.append(SR_row)
#Relabel columns
DF_naive.columns = DF_test.columns
DF_naive.head()
#the sample DF_want

回答by EdChum

You can use np.whereto set your desired value based on a boolean condition:

您可以使用np.where基于布尔条件设置所需的值:

In [18]:
DF_test['value'] = np.where(DF_test['value'] > threshold, 1,0)
DF_test

Out[18]:
  c1 c2  value
0  a  p      0
1  b  q      0
2  c  r      1
3  d  s      1
4  e  t      0

Note that because your data is a heterogenous np array the 'value' column contains strings rather than floats:

请注意,由于您的数据是异构 np 数组,因此“值”列包含字符串而不是浮点数:

In [58]:
DF_test.iloc[0]['value']

Out[58]:
'0.12'

So you'll need to convert the dtypeto floatfirst: DF_test['value'] = DF_test['value'].astype(float)

所以你需要先转换dtypefloatDF_test['value'] = DF_test['value'].astype(float)

You can compare the timings:

您可以比较时间:

In [16]:
%timeit np.where(DF_test['value'] > threshold, 1,0)
1000 loops, best of 3: 297 μs per loop

In [17]:
%%timeit
DF_naive = pd.DataFrame()
for i in range(DF_test.shape[0]):
    #Get first 2 columns
    first2cols = list(DF_test.ix[i][:-1])
    #Check if value is greater than threshold
    binary_value = [int((bool(float(DF_test.ix[i][-1]) > threshold)))]
    #Create series object
    SR_row = pd.Series( first2cols + binary_value,name=i)
    #Add to empty dataframe container
    DF_naive = DF_naive.append(SR_row)
10 loops, best of 3: 39.3 ms per loop

the np.whereversion is over 100x faster, admittedly your code is doing a lot of unnecessary stuff but you get the point

np.where版本快了 100 倍以上,诚然您的代码做了很多不必要的事情,但您明白了

回答by jpp

Since boolis a subclass of int, i.e. True == 1and False == 0, you can convert a Boolean series to its integer form:

由于boolint,即True == 1的子类False == 0,您可以将布尔系列转换为其整数形式:

DF_test['value'] = (DF_test['value'] > threshold).astype(int)

Generally, including most uses in computation or indexing, the intconversion is not necessary and you may wish to forego it altogether.

通常,包括计算或索引中的大多数用途,int转换不是必需的,您可能希望完全放弃它。