如何在numpy数组中拆分字符串?

2024-05-29 04:15:21 发布

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

我有下表:

由于列“location”中有重复的状态,因此我正尝试从location中删除该状态,以便它只具有城市名称。

year    location    state   success
2009    New York, NY    NY  1
2009    New York, NY    NY  1
2009    Chicago, IL IL  1
2009    New York, NY    NY  1
2009    Boston, MA  MA  1
2009    Long Beach, CA  CA  1
2009    Atlanta, GA GA  1

我尝试了以下代码:

x = KS_clean.column(1)
np.chararray.split(x, ',')

如何拆分字符串,使结果只包含以下城市名称:

array('New York', 'New York', 'Chicago', ...,) 

好让我把它放回桌子里?

抱歉,这是一个基本问题,但我对python还不熟悉,还在学习。谢谢


Tags: 名称new状态locationbostonyearilca
1条回答
网友
1楼 · 发布于 2024-05-29 04:15:21

我认为您需要首先使用DataFrame(例如通过^{}):

import numpy as np
from pandas.compat import StringIO

temp=u"""year;location;state;success
2009;New York, NY;NY;1
2009;New York, NY;NY;1
2009;Chicago, IL;IL;1
2009;New York, NY;NY;1
2009;Boston, MA;MA;1
2009;Long Beach, CA;CA;1
2009;Atlanta, GA;GA;1"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), sep=";")

print (type(df))
<class 'pandas.core.frame.DataFrame'>

print (df)
   year        location state  success
0  2009    New York, NY    NY        1
1  2009    New York, NY    NY        1
2  2009     Chicago, IL    IL        1
3  2009    New York, NY    NY        1
4  2009      Boston, MA    MA        1
5  2009  Long Beach, CA    CA        1
6  2009     Atlanta, GA    GA        1

然后按^{}分割并按str[0]选择第一个列表:

df['location'] = df['location'].str.split(', ').str[0]
print (df)
   year    location state  success
0  2009    New York    NY        1
1  2009    New York    NY        1
2  2009     Chicago    IL        1
3  2009    New York    NY        1
4  2009      Boston    MA        1
5  2009  Long Beach    CA        1
6  2009     Atlanta    GA        1

最后,如果需要,由^{}转换为numpy数组:

arr = df.values
print (arr)
[[2009 'New York' 'NY' 1]
 [2009 'New York' 'NY' 1]
 [2009 'Chicago' 'IL' 1]
 [2009 'New York' 'NY' 1]
 [2009 'Boston' 'MA' 1]
 [2009 'Long Beach' 'CA' 1]
 [2009 'Atlanta' 'GA' 1]]

相关问题 更多 >

    热门问题