简体   繁体   中英

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

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.

The complex_column_name here is a complex sequence which is not included above.
My suggestion is to make your complex_column_name simpler. Hope this could be helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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