Reputation: 91
I have seen similar questions, but it seems all would not work in Python 3.8. I have a dataframe like
index id
0 0001 01 12537.30 0
1 0001 01 1278.50 1
2 0001 03 53.10 0
where id column should be split into 4 columns, but they are in one column now. I need to split it based on space.
I have tried
df2 = df1['id'].apply(lambda x: pd.Series(x.split(' ')))
or
df1[["City", "State", "Country",'indicator']] = df1["id"].str.split(pat=" ", expand=True)
All told me that TypeError: 'function' object is not subscriptable. Does anyone have any idea? Thanks
Upvotes: 0
Views: 1401
Reputation: 2923
Pretty late, but this is what you can do:
df['id'].str.extractall('(\w+)')[0].unstack()
Here is the link to the original answer (Link
Upvotes: 0
Reputation: 2901
Instead of going through the hassle of splitting the column, the easy path for you would be to create a new Dataframe with read_csv
method where the sep
argument as spaces or tab (whatever you have there) and skiprows
argument as 1 and names=[co1, col2, col3,...].
Here is an example to further clarify the above point:
new_df = pd.read_csv(path_to_your_file, sep='\\t', skiprows=1, names=['id', 'col1', 'col2', 'col3']
Upvotes: 1