简体   繁体   English

Pandas:替换和删除列中的字符

[英]Pandas: Replace and remove character in columns

I have a dataframe in pandas, in this format: I need to perform formatting on my dataframe that is larger than this, generally speaking only on the 'CTe' column我在 pandas 中有一个 dataframe,格式为:

CTe = ["1221-2","12321-45","123-3"]
UF = ['A','B','C']

df = pd.DataFrame(
    data = zip(CTe,UF),
        columns=["CTe","UF"])

And I would like to know how I can format the entire "CTe" column, where I can remove the '-' and the numbers after the '-'.我想知道如何格式化整个“CTe”列,我可以在其中删除“-”和“-”之后的数字。 The result I expect is the following:我期望的结果如下:

CTe = ["1221","12321","123"]
UF = ['A','B','C']

df = pd.DataFrame(
    data = zip(CTe,UF),
        columns=["CTe","UF"])

I'm asking this because I just need to do a "merge" and my other dataframe only has the number that is before the ' - '.我问这个是因为我只需要进行“合并”,而我的另一个 dataframe 只有“-”之前的数字。

I don't know what I can do我不知道我能做什么

Something like this?是这样的吗?

df['CTe'].str.split('-', 1).str[0]

Alternatively clean the CTe list before creating the dataframe:或者在创建 dataframe 之前清理 CTe 列表:

CTe_cleaned = [''.join(x.split('-')[0]) for x in CTe ]

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

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