Python Pandas转换列数据类型 [英] Python Pandas convert column data type

查看:664
本文介绍了Python Pandas转换列数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有人问过像这样的问题,但是到目前为止,我还没有找到这个问题的答案.

I know a question like this has been asked zillion types, but so far I have not been able to find an answer to this question.

我已经将两个.csv文件与Pandas一起加入了,现在我想在新的加入的.csv文件中添加更多列,并根据已经可用的数据来计算值.

I have joined two .csv files together with Pandas and now I would like to add some more columns to the new joined .csv file and the values calculate based on the already available data.

但是,我不断收到此错误:

However, I keep getting this error:

"The truth value of a is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."

现在,这显然是我的列的数据类型(全是整数)的问题,但是我还没有找到一种(有效的)方式将该列设置为整数.

Now that obviously seems to be a problem with the data type of my column (which is all integers), but I have not found a (working) way to set that column as integers.

这是我的代码:

import pandas

def nscap(ns):
    if ns <= 13:
        x = ns
    elif ns > 13:
        x = 13
    return x

df_1 = pandas.read_csv("a.csv", sep=';', names=["DWD_ID", "NS"], header=0)
df_2 = pandas.read_csv("b.csv", sep=';', names=["VEG", "DWD_ID"], header=0)
df_joined = pandas.merge(df_1, df_2, on="DWD_ID")
df_joined["NS_Cap"] = nscap(df_joined["NS"])

如果我设置了

df_joined["NS_Cap"] = nscap(20)

代码正常工作

我尝试过.astype(int)或.to_numeric()之类的函数,但除非语法错误,否则它对我不起作用.

I have tried functions like .astype(int) or .to_numeric() but unless I had the syntax wrong, it didn't work for me.

提前谢谢!

推荐答案

与@EdChum的注释一样,您需要使用clip(upper=13)clip_upper(13).从长远来看,这样的实例可以帮助您的另一种选择是将apply与lambda函数一起使用.这是一种非常漂亮的全方位方法.

As with @EdChum's comment, you need to use clip(upper=13) or clip_upper(13). One other option which can help you in the long run with instances like this is to use apply with a lambda function. This is a really nifty all-around method.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(5,18,size=(5, 4)), columns=list('ABCD'))
nscap = lambda x: min(x, 13)

print df.head()
print '-' * 20

df['NSCAP'] = df['D'].apply(nscap)

print df.head()

结果:

记下第二个数据帧的最后两行.

Take note of the last 2 lines of the second dataframe.

希望这会有所帮助.

这篇关于Python Pandas转换列数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆