简体   繁体   English

如何用特定的dtype创建一个新的空熊猫列?

[英]How to create a new empty pandas columns with a specific dtype?

I have a DataFrame df with columns 'a' . 我有一个带有'a'列的DataFrame df How would I create a new column 'b' which has dtype=object ? 如何创建具有dtype=object的新列'b'

I know this may be considered poor form, but at the moment I have a dataframe df where the column 'a' contains arrays (each element is an np.array ). 我知道这可能被认为是较差的形式,但是目前我有一个数据框df ,其中的列'a'包含数组(每个元素都是np.array )。 I want to create a new column 'b' where each element is a new np.array that contains the logs of the corresponding elemnent in 'a' . 我想创建一个新列'b' ,其中每个元素都是一个新的np.array ,其中包含'a'相应元素的日志。

At the moment I tried these two methods, but neither worked: 目前,我尝试了这两种方法,但是都没有用:

    for i in df.index:
        df.set_value(i,'b', log10(df.loc[i,'a']))

and

    for i in df.index:
        df.loc[i,'b'] = log10(df.loc[i,'a']))

Both give me ValueError: Must have equal len keys and value when setting with an iterable . 两者都给我ValueError: Must have equal len keys and value when setting with an iterable

I'm assuming the error comes about because the dtype of the new column is defaulted to float although I may be wrong. 我假设出现错误是因为新列的dtype默认为float尽管我可能是错的。

As each row of your column is an array, it's better to use the standard NumPy mathematical functions for computing their element-wise logarithms to the base 10: 由于列的每一行都是一个数组,因此最好使用标准的NumPy数学函数来计算以10为底的元素对数:

df['log_a'] = df.a.apply(lambda x: np.log10(x))

在此处输入图片说明

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

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