Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove square bracket from pandas dataframe

Tags:

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 ?

print df  id     value                  1      [63]         2      [65]        3      [64]         4      [53]        5      [13]       6      [34]   
like image 673
DougKruger Avatar asked Jul 01 '16 14:07

DougKruger


People also ask

How do I get rid of square brackets in Python?

The Python strip() function removes specified characters from the beginning and end of a string. To remove square brackets from the beginning and end of a string using Python, we pass “[]” to the strip() function as shown below. If you have curly brackets as well, we pass “[]{}” to strip() to remove the brackets.

How do you change pandas brackets?

The pandas. str. replace() function is used with Series objects to replace an occurrence of a substring that matches a given regular expression (regex) pattern. We can use this function to remove parentheses from rows of a Series object at once.

How do I remove items from a pandas DataFrame?

Overview: From a pandas Series a set of elements can be removed using the index, index labels through the methods drop() and truncate(). The drop() method removes a set of elements at specific index locations. The locations are specified by index or index labels.


Video Answer


2 Answers

If values in column value have type list, use:

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 strings use str.strip and then convert to numeric by 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 
like image 153
jezrael Avatar answered Oct 28 '22 06:10

jezrael


if string we can also use string.replace method

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 
like image 22
qaiser Avatar answered Oct 28 '22 07:10

qaiser