简体   繁体   English

Python:如何有条件地从python中的数据框中的列中删除字母?

[英]Python: How to do the conditional removal of letters from the column in dataframe in python?

data = {'period':['chy1md','chy2md','chy6md',chy6L6L1y,'chy6L6L5y','chy6L6L10y']}
df = pd.DataFrame(data)

Expected output:预期输出:

new_df = {'period':['1md','2md','6md',1y,'5y','10y']}

May I get help to get the above expected output.我可以得到帮助以获得上述预期输出。 I want conditional strip from the column based on length of the column values.我想根据列值的长度从列中进行条件剥离。 I dont want to hard code the letters to be removed.我不想对要删除的字母进行硬编码。 It is so because I have many files and there are different names in the columns.之所以如此,是因为我有很多文件,并且列中有不同的名称。 Conditional stripping help me to format other files.条件剥离帮助我格式化其他文件。

Do:做:

result = df['period'].str.replace('(chy|6L6L)', '')
print(result)

Output输出

0    1md
1    2md
2    6md
3     1y
4     5y
5    10y
Name: period, dtype: object

IIUC, this woudl do: IIUC,这会做:

df.period.str.extract('(\d+\D+)$')

Output:输出:

     0
0  1md
1  2md
2  6md
3   1y
4   5y
5  10y

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

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