Reputation: 33
data={'GradeCol':['BVCS,105308.248']}
data1=DataFrame(data)
print(data1)
data1[['GradeColText','GradeColNum']] = pd.DataFrame([x for x in
data1.GradeCol])
#data1[['GradeColText','GradeColNum']] =
#pd.DataFrame(data1.GradeCol.values.tolist())
print(data1)
while splitting row into columns getting error ValueError: Columns must be same length as key
Expected output
GradeColText GradeColNum
BVCS 105308.248
Upvotes: 1
Views: 1292
Reputation: 403128
You can use df.str.rsplit(expand=True)
:
In [429]: df[['GradeColText', 'GradeColNum']] = df['GradeCol'].str.rsplit(',', expand=True)
In [432]: df.drop(['GradeCol'], axis=1, inplace=True); df
Out[432]:
GradeColText GradeColNum
0 BVCS 105308.248
Upvotes: 1
Reputation: 323376
Try this ?
data={'GradeCol':['BVCS,105308.248']}
df=pd.DataFrame(data)
df = pd.DataFrame(df.GradeCol.str.split(',', 1).tolist(),
columns=['GradeColText', 'GradeColNum'])
GradeColText GradeColNum
0 BVCS 105308.248
Upvotes: 1