'How to split a very long string column in Pandas?
I have a (very) long single dataframe column and want to split it into multiple columns to make a regular DataFrame out of it. I've tried
.tolist()
zip()
str.extract()
.str.split()
expand=True
which always results in some errors.
Dataframe infos:
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3150708 entries, 0 to 3150707
Data columns (total 1 columns):
# Column Dtype
--- ------ -----
0 CAL_DT,MODEL_NAME,BRAND_FULL_NAME,BRAND_NAME,VENDOR_NAME,OS_NAME,DEVICE_TYPE,_2G_FLG,_3G_FLG,_4G_FLG,WIFI_FLG,BLUETOOTH_FLG,TOUCH_SCREEN_FLG,DUAL_SIM_FLG,GENDER_TYPE_CD,AGE_B,NATIONALITY_CD,NATIONALITY_NAME,SAUDI_NON_SAUDI,DEVICE_COUNT object
dtypes: object(1)
memory usage: 24.0+ MB
Solution 1:[1]
If only splitting values of first column use:
df1 = df.iloc[:, 0].str.split(',', expand=True)
If possible assign back splitted first column, here not, because different length of len(df1.columns) and len(df.columns[0].split(',')):
df1.columns = df.columns[0].split(',')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
