Python 如何从熊猫数据框中删除方括号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38147447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:25:25  来源:igfitidea点击:

How to remove square bracket from pandas dataframe

pythonstringpandasdataframe

提问by DougKruger

I came up with values in square bracket(more like a list) after applying str.findall()to column of a pandas dataframe. How can I remove the square bracket ?

list在应用于str.findall()熊猫数据框的列后,我想出了方括号中的值(更像是 a )。如何删除方括号?

print df

id     value                 
1      [63]        
2      [65]       
3      [64]        
4      [53]       
5      [13]      
6      [34]  

回答by jezrael

If values in column valuehave type list, use:

如果列中的值value具有 type list,请使用:

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

Or:

或者:

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

If stringsuse str.stripand then convert to numeric by astype:

如果strings使用str.strip然后通过astype以下方式转换为数字:

df['value'] = df['value'].str.strip('[]').astype(int)

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 '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

回答by qaiser

if string we can also use string.replace method

如果字符串我们也可以使用 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