Python Dataframe将行拆分为新列—X、Y坐标和;文本
我的文本已经在数据框中,通常如下所示:Python Dataframe将行拆分为新列—X、Y坐标和;文本,python,pandas,dataframe,text,Python,Pandas,Dataframe,Text,我的文本已经在数据框中,通常如下所示: Column 100, 1594, text & or numbers $124,345.56 108, 1805, _ _ _ 254, 2000, (13,452,863) Y X Text 100 1594 text & or numbers $124,345.56 108 1805 _ _ _ 254 2000 (13,452,863) 第一个数字系列和第二个数
Column
100, 1594, text & or numbers $124,345.56
108, 1805, _ _ _
254, 2000, (13,452,863)
Y X Text
100 1594 text & or numbers $124,345.56
108 1805 _ _ _
254 2000 (13,452,863)
第一个数字系列和第二个数字系列之间有4个空格,分别是X,Y坐标和文本坐标。我如何将其放入这样的新数据框中:
Column
100, 1594, text & or numbers $124,345.56
108, 1805, _ _ _
254, 2000, (13,452,863)
Y X Text
100 1594 text & or numbers $124,345.56
108 1805 _ _ _
254 2000 (13,452,863)
鉴于您提供的输入数据,这是一种解决方案
import pandas as pd
df = pd.DataFrame({'Column': ['100, 1594, text & or numbers $124,345.56',
'108, 1805, _ _ _',
'254, 2000, (13,452,863)']})
df_out = pd.DataFrame(df['Column'].str.split(', ').values.tolist(),
columns=['Y', 'X', 'Text'])
df_out[['Y', 'X']] = df_out[['Y', 'X']].apply(pd.to_numeric, downcast='integer')
# Y X Text
# 0 100 1594 text & or numbers $124,345.56
# 1 108 1805 _ _ _
# 2 254 2000 (13,452,863)