pandas split by space
# importing pandas module
import pandas as pd
# new data frame with split value columns
data["Team"]= data["Team"].str.split(" ", n = 1, expand = True)
# df display
data
pandas split by space
# importing pandas module
import pandas as pd
# new data frame with split value columns
data["Team"]= data["Team"].str.split(" ", n = 1, expand = True)
# df display
data
How to split a text column into two separate columns?
import pandas as pd
df = pd.DataFrame(["STD, City State",
"33, Kolkata West Bengal",
"44, Chennai Tamil Nadu",
"40, Hyderabad Telengana",
"80, Bangalore Karnataka"], columns=['row'])
out = pd.DataFrame(df.row.str.split(' ',2).tolist(),columns=['STD','City','State'])
out.drop(index=0,inplace=True)
how to use split in pandas
import pandas as pd
# new data frame with split value columns
data["Team"]= data["Team"].str.split(" ", n = 1, expand = True)
# df display
data
dataframe split column
df[['First','Last']] = df.Name.str.split(" ", expand=True)
split a pd dataframe
In [255]: df = pd.DataFrame(np.random.rand(5, 6), columns=list('abcdef'))
In [256]: df
Out[256]:
a b c d e f
0 0.823638 0.767999 0.460358 0.034578 0.592420 0.776803
1 0.344320 0.754412 0.274944 0.545039 0.031752 0.784564
2 0.238826 0.610893 0.861127 0.189441 0.294646 0.557034
3 0.478562 0.571750 0.116209 0.534039 0.869545 0.855520
4 0.130601 0.678583 0.157052 0.899672 0.093976 0.268974
In [257]: dfs = np.split(df, [4], axis=1)
In [258]: dfs[0]
Out[258]:
a b c d
0 0.823638 0.767999 0.460358 0.034578
1 0.344320 0.754412 0.274944 0.545039
2 0.238826 0.610893 0.861127 0.189441
3 0.478562 0.571750 0.116209 0.534039
4 0.130601 0.678583 0.157052 0.899672
In [259]: dfs[1]
Out[259]:
e f
0 0.592420 0.776803
1 0.031752 0.784564
2 0.294646 0.557034
3 0.869545 0.855520
4 0.093976 0.268974
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us