简体   繁体   English

在熊猫中,如何根据条件从另一个部分中创建一个新列?

[英]In pandas how to create a new column from part of another, obeying a condition?

In python 3 and pandas I have the dataframe: 在python 3和pandas中,我有数据框:

lista_projetos.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 59 entries, 0 to 58
Data columns (total 14 columns):
n_projeto                             59 non-null object
autor                                 59 non-null object
ementa                                59 non-null object
resumo                                59 non-null object
votacao_nominal                       59 non-null object
votacao_nominal_alternativa_emenda    59 non-null object
link_votacao                          0 non-null float64
observacao                            0 non-null float64
link_emenda                           0 non-null float64
indicado_por                          59 non-null object
entidade_que_avalia                   59 non-null object
favoravel_desfavoravel_indiferente    59 non-null object
explicacao                            59 non-null object
link_projeto                          59 non-null object
dtypes: float64(3), object(11)
memory usage: 6.5+ KB

The column "link_projeto" has urls, always in this format: “ link_projeto”列中的网址始终采用以下格式:

" http://www.camara.gov.br/proposicoesWeb/fichadetramitacao?idProposicao=2171854 " http://www.camara.gov.br/proposicoesWeb/fichadetramitacao?idProposicao=2171854

" http://www.camara.gov.br/proposicoesWeb/fichadetramitacao?idProposicao=2147513 " http://www.camara.gov.br/proposicoesWeb/fichadetramitacao?idProposicao=2147513

" http://www.camara.gov.br/proposicoesWeb/fichadetramitacao?idProposicao=2168253 " http://www.camara.gov.br/proposicoesWeb/fichadetramitacao?idProposicao=2168253

I want to create a new column from the "link_projeto" column. 我想从“ link_projeto”列中创建一个新列。 So: always pick up the final number after the "=" sign 因此:请务必在“ =”符号后选择最终数字

Like this: 像这样:

new_column
2171854
2147513
2168253

Please, is there a way to create a new column from part of another? 请问,有没有一种方法可以从另一部分中创建一个新列?

First, how would you do this on a single value? 首先,您将如何对单个值执行此操作?

>>> link = "http://www.camara.gov.br/proposicoesWeb/fichadetramitacao?idProposicao=2171854"
>>> link.split("=", 1)[1]
'2171854'

But split is a method on str objects; 但是split是对str对象的一种方法。 how do you apply it to a column full of strings? 如何将其应用于充满字符串的列? Simple: columns (Series and Index) have a str attribute for exactly this purpose: 简单:列(“系列”和“索引”)具有str属性 ,正是出于这个目的:

df.link_projecto.str.split("=", 1)

But split doesn't just return a string, it returns a list of strings. 但是split不仅返回字符串,还返回字符串列表。 How do we get the last one? 我们如何获得最后一个?

As explained in Splitting and Replacing Strings , you just access str again and index it: 拆分和替换字符串中所述 ,您只需再次访问str并对其进行索引:

df.link_projecto.str.split("=", 1).str[1]

So: 所以:

df["new_column"] = df.link_projecto.str.split("=", 1).str[1]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Pandas 中创建新列,条件是重复另一列的值? - How to create new column in Pandas with condition to repeat by a value of another column? 使用来自另一个数据帧的 if 条件在 Pandas 数据帧中创建一个新列 - create a new column in pandas dataframe using if condition from another dataframe Pandas 根据来自另一个 dataframe 的计数和条件创建新列 - Pandas Create new column based on a count and a condition from another dataframe Pandas 数据框根据另一列的条件创建新行 - Pandas dataframe create new rows based on condition from another column 根据一列的条件和熊猫中另一列的值创建新列 - Create new column based on condition from one column and the value from another column in pandas 如何根据条件从 Python、Pandas 的另一列中删除一列的部分值? - How to drop part of the values from one column by condition from another column in Python, Pandas? 如何根据条件在 Pandas 中创建新列 - How Create new column in Pandas based on condition 如何根据条件在 pandas 中创建另一列? - How to create another column in pandas based on a condition? 如何从 dataframe 中的另一列按条件创建新组? - How to create new group by condition from another column in dataframe? Pandas/Python:如何根据其他列的值创建新列并将额外条件应用于此新列 - Pandas/Python: How to create new column based on values from other columns and apply extra condition to this new column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM