Home > database >  Pandas Column Split (Array)
Pandas Column Split (Array)

Time:11-19

Below is my data in SQL Server

enter image description here

After reading data in python it become like this

enter image description here

I use the below code to split the value to multiple columns

# 1. To split single array column to multiple column based on '\t'
df[['v1','v2','v3','v4','v5','v6','v7','v8','v9','v10','v11','v12','v13','v14','v15',\
       'v16','v17','v18','v19','v20','v21','v22','v23','v24','v25','v26','v27','v28',\
'v29','v30','v31','v32','v33','v34','v35','v36','v37','v38','v39','v40','v41']] = df['_VALUE'].str.split(pat="\t", expand=True)

# 2. To remove the '\r\n' from the last column
df['v41'] = df['v41'].replace(r'\s |\\n', ' ', regex=True)

But in some data sets the array value is more Eg. 100 columns, the above code is so big. I have to write from V1 to V100. Is there any simple way to do this.

CodePudding user response:

You can replace the hardcoded array from your code with one that generates the array for you using a method like this:

df[[f'v{x}' for x in range(100)]] = df['_VALUE'].str.split(pat="\t", expand=True)
  • Related