简体   繁体   English

将数字和字符串拆分为 pandas 上的不同列

[英]split numbers and string to differents columns on pandas

I like to split the column into str and numbers.我喜欢将列拆分为str和数字。

data={"name&numb":["cat 123","34 dog","bird 93","dolphin dof 8 ","lion cat 76","tiger 22 animal "]}
df=pd.DataFrame.from_dict(data)

I did this so split the numbers我这样做所以分开数字

df["number"]=df["name&numb"].str.extract('(\d+)')

Now I like to make one more column so I get only string, I do not know if it will affect but in the original data, not in the English language现在我想再做一列,所以我只得到字符串,我不知道它是否会影响但在原始数据中,而不是英文

something like:就像是:

 df["strings"]=df["name&numb"].str.extract('str')

I believe you need Series.str.extract with \D for non digit data with Series.str.strip for remove trailing whitespaces:我相信您需要Series.str.extract\D用于非数字数据,而Series.str.strip用于删除尾随空格:

df["number"]=df["name&numb"].str.extract('(\d+)')

df["strings"] = df["name&numb"].str.extract('(\D+)', expand=False).str.strip()

If need all strings one idea is use:如果需要所有字符串,一个想法是使用:

f = lambda x: ' '.join(y for y in x.split() if not y.isdigit())
df["strings1"] = df["name&numb"].apply(f)
print (df)
          name&numb number      strings      strings1
0           cat 123    123          cat           cat
1            34 dog     34          dog           dog
2           bird 93     93         bird          bird
3    dolphin dof 8       8  dolphin dof   dolphin dof
4       lion cat 76     76     lion cat      lion cat
5  tiger 22 animal      22        tiger  tiger animal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM