Pandas dataframe,按最后一个位置的最后一列拆分数据,但保留其他列

2024-10-02 16:25:22 发布

您现在位置:Python中文网/ 问答频道 /正文

对大熊猫来说非常陌生,所以任何解释和解决办法都是值得赞赏的。在

我有一个数据帧,比如

    Company                             Zip State City
1   *CBRE                               San Diego, CA 92101
4   1908 Brands                         Boulder, CO 80301
7   1st Infantry Division Headquarters  Fort Riley, KS
10  21st Century Healthcare, Inc.       Tempe 85282
15  AAA                                 Jefferson City, MO 65101-9564

我想将数据中的Zip State city列拆分为3个不同的列。使用这篇文章的答案Pandas DataFrame, how do i split a column into two如果我没有第一篇专栏文章,我可以完成这项任务。编写一个regex来捕获所有公司只会让我捕获数据中的所有内容。在

我也试过了

^{pr2}$

但这导致我失去了company列,并将多个单词的城市名称拆分为单独的列。在

如何在保留公司列数据的同时拆分上一列?在


Tags: 数据city公司zipcompanycastatesan
1条回答
网友
1楼 · 发布于 2024-10-02 16:25:22

您可以使用extract()方法:

In [110]: df
Out[110]:
                               Company                 Zip State City
1                                *CBRE            San Diego, CA 92101
4                          1908 Brands              Boulder, CO 80301
7   1st Infantry Division Headquarters                 Fort Riley, KS
10       21st Century Healthcare, Inc.                    Tempe 85282
15                                 AAA  Jefferson City, MO 65101-9564

In [112]: df[['City','State','ZIP']] = df['Zip State City'].str.extract(r'([^,\d]+)?[,]*\s*([A-Z]{2})?\s*([\d\-]{4,11})?', expand=True)

In [113]: df
Out[113]:
                               Company                 Zip State City            City State         ZIP
1                                *CBRE            San Diego, CA 92101       San Diego    CA       92101
4                          1908 Brands              Boulder, CO 80301         Boulder    CO       80301
7   1st Infantry Division Headquarters                 Fort Riley, KS      Fort Riley    KS         NaN
10       21st Century Healthcare, Inc.                    Tempe 85282          Tempe    NaN       85282
15                                 AAA  Jefferson City, MO 65101-9564  Jefferson City    MO  65101-9564

docs

^{pr2}$

For each subject string in the Series, extract groups from the first match of regular expression pat.

New in version 0.13.0.

Parameters:

pat : string

Regular expression pattern with capturing groups

flags : int, default 0 (no flags)

re module flags, e.g. re.IGNORECASE .. versionadded:: 0.18.0

expand : bool, default False

If True, return DataFrame.

If False, return Series/Index/DataFrame.

Returns: DataFrame with one row for each subject string, and one column for each group. Any capture group names in regular expression pat will be used for column names; otherwise capture group numbers will be used. The dtype of each result column is always object, even when no match is found. If expand=True and pat has only one capture group, then return a Series (if subject is a Series) or Index (if subject is an Index).

相关问题 更多 >