简体   繁体   English

pandas 基于其他其他列创建新列

[英]pandas creating a new column based on other other columns

I have a dataframe called bank and ı would like to create a new column explaining percentage change but when I try ı encounter with this error:unsupported operand type(s) for -: 'str' and 'str' How can I solve this problem or is there other ways for me to create this column?我有一个名为 bank 的 dataframe 并且我想创建一个新列来解释百分比变化但是当我尝试时遇到此错误:不支持的操作数类型为 -:'str' 和 'str' 我该如何解决这个问题或者我还有其他方法可以创建这个专栏吗?

bank=bank.iloc[0:15,0:4]
pctchange=(bank['Yüksek']-bank['Düşük'])-bank['Düşük']
def risk(bank):
    if pctchange>0.3:
        val='High Risk'
    elif pctchange>0.2 :
        val='Middle Risk'
    else:
        val='Low Risk'
    return val
bank.apply(risk,axis=1)

You could sanitize your data and then try to change type, like this:您可以清理数据,然后尝试更改类型,如下所示:

# Replace commas with points
bank = bank.apply(lambda x: x.str.replace(',', '.'))

# Convert to float
bank = bank.astype('float64')

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

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