[英]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
尽管我可能是错的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.