How to Remove Square Bracket from Pandas Dataframe

How to remove square bracket from pandas dataframe

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

How to remove square brackets from dataframe

Try with apply, explode and groupby:

>>> df.apply(lambda x: x.explode().astype(str).groupby(level=0).agg(", ".join))
column1 column2 column3
0 data1 data1 data1
1 nan data2 data2
2 data2 data3 data3, data3, testing how are you guys hope yo...
3 data3 data3 data4, dummy text to test to test test test
4 nan data4 data5
  1. Use pandas.explode() to transform each list element to its own row, replicating index values.
  2. Then groupby identical index values and aggregate using str.join().
  3. Use apply to apply the same function to all columns of the DataFrame.

Removing Null Square Brackets from Pandas Dataframe

You can check the length of the lists with str.len and slice using a boolean array when it is greater than 0:

data[data['C'].str.len().gt(0)]

or, convert to boolean (which seems to be the fastest approach):

data[data['C'].astype(bool)]

output:

   A                C
0 5 [man, talk]
1 3 [bar]
3 6 [bat, cat, mat]

How to remove the square bracket from the python dataframe

This might help. Use .apply with lambda. I am using isinstance to check if type is list.

Ex:

import pandas as pd
df = pd.DataFrame({"changes": [['Body Weight', 'Color'], ['band Voltage Rating', 'Body Weight'], "aaaaa"]})
print(df["changes"].apply(lambda x: ",".join(x) if isinstance(x, list) else x))

Output:

0                  Body Weight,Color
1 band Voltage Rating,Body Weight
2 aaaaa
Name: changes, dtype: object

How to remove square brackets and commas from dataframe column

Convert to string and use str.repalce with regex:

df = pd.DataFrame({'i1': [1.0]*2, 'i2': [1.0]*2,
'R1': [[0.0]*3]*2, 'R2': [[0.0, 0.0, 0.802],[0.0, 0.0, -0.802]],
'D': [-0.013347]*2})

df['R1'] = df['R1'].astype(str).str.replace(r'\[|\]|,', '')
df['R2'] = df['R2'].astype(str).str.replace(r'\[|\]|,', '')

i1 i2 R1 R2 D
0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.802 -0.013347
1 1.0 1.0 0.0 0.0 0.0 0.0 0.0 -0.802 -0.013347

How to remove square brackets from entire dataframe if not every row and column have square brackets?

You can cast your df to str, replace the brackets and then cast back to float:

df.astype(str).replace({"\[":"", "\]":""}, regex=True).astype(float)

removing brackets from list inside pandas cell

If values are strings:

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

If values are lists:

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


Related Topics



Leave a reply



Submit