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]
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With