import pandas as pd
import numpy as np
# assuming you have defined other columns in df here
# Create empty columns for the new int columns
df['First'] = np.NaN
df['Second'] = np.NaN
# For each element in Pairing
for i, pairing in enumerate(df['Pairing']):
# split pairing into list based on underscores, get last two ints only
ints = [int(x) for x in pairing.split('_')[-2:]]
df['First'][i] = ints[0]
df['Second'][i] = ints[1]
print(df)
import pandas as pd
df = pd.DataFrame({
'Pairing': ['1001_1234_1235', '1001_1233_1236'],
'Result': [1, 0],
})
# split at '_', each result will become a new column
df2 = df['Pairing'].str.split('_', expand=True)
# convert to numbers
df2 = df2.astype(int)
#rename columns back to something useful
df2.columns = ['Pairing{}'.format(col) for col in df2.columns ]
# add the columns back to the old DataFrame
df = df.join(df2)
新的
^{pr2}$df
应该如下所示:使用pandas
str
操作可以很容易地做到这一点:这将导致:
^{pr2}$有关更多示例,请参见Pandas–使用文本数据:
http://pandas.pydata.org/pandas-docs/stable/text.html
如果您有
pairing = '1001_1234_1235'
,那么相关问题 更多 >
编程相关推荐