我在对Pandas数据框中的某一列使用str.findall()
后,得到一个类似于list
的值,其中使用了方括号。如何去掉这些方括号?
print df
id value
1 [63]
2 [65]
3 [64]
4 [53]
5 [13]
6 [34]
如果 value
列中的值是列表类型,请使用:
df['value'] = df['value'].str[0]
或者:
df['value'] = df['value'].str.get(0)
Docs.
Sample:
df = pd.DataFrame({'value':[[63],[65],[64]]})
print (df)
value
0 [63]
1 [65]
2 [64]
#check type if index 0 exist
print (type(df.loc[0, 'value']))
<class 'list'>
#check type generally, index can be `DatetimeIndex`, `FloatIndex`...
print (type(df.loc[df.index[0], 'value']))
<class 'list'>
df['value'] = df['value'].str.get(0)
print (df)
value
0 63
1 65
2 64
如果 strings
使用 str.strip
并转换为数值类型使用 astype
:
df['value'] = df['value'].str.strip('[]').astype(int)
示例:
df = pd.DataFrame({'value':['[63]','[65]','[64]']})
print (df)
value
0 [63]
1 [65]
2 [64]
#check type if index 0 exist
print (type(df.loc[0, 'value']))
<class 'str'>
#check type generally, index can be `DatetimeIndex`, `FloatIndex`...
print (type(df.loc[df.index[0], 'value']))
<class 'str'>
df['value'] = df['value'].str.strip('[]').astype(int)
print (df)
value
0 63
1 65
2 64
df['value'].str.get(0)
或 df['value'].str[0]
表示给出列表的第一个值。如果需要所有值,则需要 df1 = pd.DataFrame(df['value'].values.tolist())
。 - jezrael[63, 23]
(列表中有2个值)而不是 [63]
时,应该怎么做? - seralouk如果是字符串,我们也可以使用 string.replace 方法。
import pandas as pd
df =pd.DataFrame({'value':['[63]','[65]','[64]']})
print(df)
value
0 [63]
1 [65]
2 [64]
df['value'] = df['value'].apply(lambda x: x.replace('[','').replace(']',''))
#convert the string columns to int
df['value'] = df['value'].astype(int)
#output
print(df)
value
0 63
1 65
2 64
print(df.dtypes)
value int32
dtype: object
[
和 ]
字符的通用解决方案是:df['value'] = df['value'].str.replace(r'[][]', '', regex=True) # one by one
df['value'] = df['value'].str.replace(r'[][]+', '', regex=True) # by chunks of one or more [ or ] chars
[][]
是正则表达式中的字符类,用于匹配 ]
或 [
字符。使用 +
可以让正则引擎按顺序匹配这些字符一次或多次。
请参见正则表达式演示。
然而,在这种情况下,方括号标记了 Series.str.findall
的字符串列表结果。很明显,您想要从列值中提取一个第一个匹配项。
Series.str.extract
Series.str.findall
所以,在这种情况下,为了避免你自己陷入麻烦,你可以使用
df['value'] = df['source_column'].str.extract(r'my regex with one set of (parentheses)')
str.extract
至少需要一个捕获括号才能实际工作并返回值(str.findall
即使没有 捕获组 也可以工作)。findall
获得多个匹配项,并且希望将其作为单个字符串输出,则可以使用 str.join
连接这些匹配项:df['value'] = df['source_column'].str.findall(pattern).str.join(', ')
df['column_name'] = df['column_name'].apply(lambda x: x.replace('[','').replace(']',''))
df['column_name'] = df['column_name'].astype(int)
import pandas as pd
df = pd.DataFrame({'value':[[70,63],[12,65],[64,39]]}).astype(str) #list converted into string, so we can use str.replace
df=df['value'].apply(lambda x: x.replace("[","").replace("]",""))
输出:
0 70, 63
1 12, 65
2 64, 39
Name: value, dtype: object
'[63]'
还是一个列表[63]
? - EdChum