简体   繁体   English

删除具有复杂列名的列时出现pandas错误

[英]pandas error in dropping column with complex column name

For practical reasons I need to define my column name with complex tuples. 出于实际原因,我需要使用复杂元组定义列名。 The problem is that I cannot drop the column as usual as a result. 问题是我不能像往常一样丢弃列。 To replicate my problem, 要复制我的问题,

complex_column_name = (u'CHRIS/CME_SP1', 34, ((u'CHRIS/CME_SP1_Settle_rolling_200_mean_to_current_value', 1),))

pd_tmp = pd.DataFrame(np.random.randn(4,1),columns=[complex_column_name])

pd_tmp.drop(complex_column_name,axis=1)

The error is : 错误是:

ValueError: setting an array element with a sequence

Any help? 有帮助吗? Thank you. 谢谢。

Thats not an efficient way of setting column name so instead use a boolean mask and select the columns rather than dropping it ie 这不是设置列名的有效方法,所以改为使用布尔掩码并选择列而不是删除它,即

mask = pd_tmp.columns != complex_column_name
ndf = pd_tmp.iloc[:,mask]

After replicating your code I get following messages. 复制代码后,我收到以下消息。

...\Anaconda3\lib\site-packages\numpy\core\numeric.py in asarray(a, dtype, order)
    480 
    481     """
--> 482     return array(a, dtype, copy=False, order=order)
    483 
    484 def asanyarray(a, dtype=None, order=None):

ValueError: setting an array element with a sequence

According to numpy.asarray 根据numpy.asarray

Input data, in any form that can be converted to an array. 输入数据,可以转换为数组的任何形式。 This includes lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays. 这包括列表,元组列表,元组,元组元组,列表元组和ndarray。

The complex_column_name here is a complex sequence which is not included above. 这里的complex_column_name是一个复杂的序列,不包含在上面。
My suggestion is to make your complex_column_name simpler. 我的建议是使你的complex_column_name更简单。 Hope this could be helpful. 希望这可能会有所帮助。

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

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