从Dataframe中删除方括号

时间:2018-04-12 06:08:02

标签: python pandas replace

我在数据帧格式中跟随adtaset,我需要从数据中删除方括号。我们怎样才能继续任何人的帮助

   From             TO
   [wrestle]        engage in a wrestling match
   [write]          communicate or express by writing
   [write]          publish
   [spell]          write
   [compose]        write music

预期输出为:

   From             TO
   wrestle      engage in a wrestling match
   write       communicate or express by writing
   write       publish
   spell       write

2 个答案:

答案 0 :(得分:2)

如果string s:

,请使用str.strip
print (type(df.loc[0, 'From']))
<class 'str'>

df['From'] = df['From'].str.strip('[]')

...如果liststr.join转换它们:

print (type(df.loc[0, 'From']))
<class 'list'>

df['From'] = df['From'].str.join(', ')

感谢@ juanpa.arrivillaga建议如果有一项list s:

df['From'] = df['From'].str[0]

可以检查的是:

print (type(df.loc[0, 'From']))
<class 'list'>

print (df['From'].str.len().eq(1).all())
True
print (df)
      From                                 TO
0  wrestle        engage in a wrestling match
1    write  communicate or express by writing
2    write                            publish
3    spell                              write
4  compose                        write music

答案 1 :(得分:0)

假设您具有以下数据框:

df = pd.DataFrame({'Region':['New York','Los Angeles','Chicago'], 'State': ['NY [new york]', '[California]', 'IL']})

会是这样的:

        Region          State
0     New York  NY [new york]
1  Los Angeles   [California]
2      Chicago             IL

要删除方括号,您需要以下几行:

df['State'] = df['State'].str.replace(r"\[","")
df['State'] = df['State'].str.replace(r"\]","")

结果:

        Region        State
0     New York  NY new york
1  Los Angeles   California
2      Chicago           IL

如果要删除方括号之间的所有内容:

df['State'] = df['State'].str.replace(r"\[.*\]","")
df['State'] = df['State'].str.replace(r" \[.*\]","")

第一行只删除方括号之间的字符,第二行考虑字符前的空格,因此为确保安全起见,最好同时运行这两行。

通过将其应用于原始df上的行:

        Region State
0     New York    NY
1  Los Angeles      
2      Chicago    IL