out = df['ColA'].str.extract(r'([^\d]*)(\d+)') \
.rename(columns={0: 'Name', 1: 'Number'})
print(out)
# Output:
Name Number
0 CAIN TAN 86092142
1 YEO KIAT JUN 81901613
更新:
Is there a way to remove the Name and Number when it outputs to the csv?
name = []
id_num = []
file = open('file.csv', 'r')
for f in file:
f = f.split(',') # split the data
name.append(str(f[0])) # append name to list
id_num.append(str(f[1])) # append ID to list
您在引用和分隔符方面有问题。要清除数据帧,请使用以下代码:
旧答案 使用
str.extract
:假设此数据帧:
在遇到的第一个数字上拆分:
更新:
当打开纯文本文件(或者在本例中是纯文本csv文件)时,可以使用for循环逐行遍历文件,就像这样(从Python 3开始,当前为):
既然列表中有了数据,您就可以按自己的方式打印/存储它了
相关问题 更多 >
编程相关推荐