English 中文(简体)
关于在安达斯建造新col的有条件yn子的问题
原标题:Question on conditional syntax for new col in pandas df

我在努力根据使用某种条件的现有一栏,在一栏 d子上制造新的col。 从根本上说,如果客户合同号包含一个强调,我想新一栏的价值在强调之前就具有所有特性,否则,我希望它成为所有已删除的客户合同编号。 I m 能够按以下方式去除 da,但第二行不工作

raw_data_df[ Search Text ] = raw_data_df[ Client Contract Number ].str.replace( - ,  )

raw_data_df[ Search Text ] = raw_data_df[ Client Contract Number ].str.split( _ )[0] if raw_data_df[ Client Contract Number ].str.contains("_") else raw_data_df[ Client Contract Number ].str.replace( - ,  )
问题回答

Lengthy答复:最后答复载于“Answer”分节。

<>载体>

因此,鉴于你列举的前提条件,它希望我们有以下客户合同号码:

Client Contract Number
123_2-31
23-1415
124-5_259
1234

鉴于你的代码和意图,我们希望有以下新的数据框架:

Search Text
123
231415
1245
1234

(由于我们取消了你第一行的法文本中的所有表格,我假定第3次样本中没有任何表格。)

<><>>

2. 将该数据框架与以下代码重新编号:

import pandas as pd

data = {"Client Contract Number": ["123_2-31", "23-1415", "124-5_259", "1234"] }
raw_data_df = pd.DataFrame(data)

在试图操作我们第二行的法典时,我们看到以下错误信息:

raw_data_df[ Search Text ] = raw_data_df[ Client Contract Number ].str.split( _ )[0] if raw_data_df[ Client Contract Number ].str.contains("_") else raw_data_df[ Client Contract Number ].str.replace( - , )

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我认为,这必须涉及以下事实:我们重新尝试适用。 基本上,条件是要试图在整个系列中得出一个单一的真相价值,因此,为什么安达要求我们使用<条码>a.empt、a.bool()、a.()、a.any()或a.all(。

Also using the [0] index in raw_data_df["Client Contract Number"].str.split("_")[0] will get us the list that results in splitting the first row of data, instead of the first index of each split row.

Answer

我认为,逐行操作的一个很好的替代办法是applymeth。 从根本上说,我们创造出一种匿名功能,这种功能完全是同一私人经营人,但个别行人除外。

用你原来的代码作为模板,其行文法如下:

raw_data_df[ Search Text ] = raw_data_df[ Client Contract Number ].str.replace( - ,  )
raw_data_df["Search Text"] = raw_data_df["Search Text"].apply(lambda x: x.split("_")[0] if "_" in x else x)

这应当产生你希望再次寻找的第二个数据框架。 由于你已重新删除了你的第一行,没有必要在你第二行的法典中重申这一点。

希望! 如果我列举的初步假设是准确的,你可能不得不打碎这一法典。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签