简体   繁体   English

将大熊猫分为两部分

[英]Split pandas column into two

There are other similar questions, but the difference here is that my dataframe already has a lot of columns, only one of which needs to be split. 还有其他类似的问题,但不同之处在于我的数据框架已经有很多列,其中只有一列需要拆分。

I have a large dataframe(hundreds of columns, millions of rows). 我有一个大型数据帧(数百列,数百万行)。 I would like to split one of these columns when a character ("|") is found in the string. 当在字符串中找到字符(“|”)时,我想拆分其中一列。

All values have only one "|". 所有值只有一个“|”。

For a fixed length I would do this: df['StateInitial'] = df['state'].str[:2] 对于固定长度,我会这样做:df ['StateInitial'] = df ['state']。str [:2]

I wish I could replace the 2 by string.index("|"), but how do I call the string? 我希望我可以用string.index(“|”)替换2,但是如何调用字符串呢?

How about: 怎么样:

df = pd.DataFrame(['a|b', 'c|d'])
s = df[0].apply(lambda x: x.split('|'))
df['left'] = s.apply(lambda x: x[0])
df['right'] = s.apply(lambda x: x[1])

Output: 输出:

     0 left right
0  a|b    a     b
1  c|d    c     d

First, set you new column values equal to the old column values. 首先,将新列值设置为等于旧列值。

Next, create a new column with values initially equal to None. 接下来,创建一个新列,其值最初等于None。

Now, update the new column with valid values of the first. 现在,使用第一个有效值更新新列。

df['new_col1'] = df['old_col']
df['new_col2'] = None
df['new_col2'].update(df.new_col1.apply(lambda x: x.str.split('|')[1] 
                      if len(x.str.split()) == 2 else None))

Here is a one liner that builds on the answer provided by @santon: 这是一个基于@santon提供的答案的单线程:

df['left'],df['right'] = zip(*df[0].apply(lambda x: x.split('|')))

>>> df 
     0 left right
0  a|b    a     b
1  c|d    c     d

If you have a column of strings, with a delimiter '|' 如果你有一列字符串,带有分隔符'|' you can use the following line to split the columns: 您可以使用以下行拆分列:

df['left'], df['right'] = df['combined'].str.split('|', 1).str

LeoRochael has a great in-depth explanation of how this works over on a separate thread: https://stackoverflow.com/a/39358924/11688667 LeoRochael在一个单独的线程上有一个很好的深入解释如何工作: https ://stackoverflow.com/a/39358924/11688667

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM