0

I've download a time series from Quandl using the get() function from Quandl python module and called UNES_HARD_COAL_BR and it has shape (24,19), but i want use only the column 1 to 3 and the column 18. I wrote this script to do this job using a list called ll and applying the drop() method in my DataFrame object, the list contain the name of column that i don't use. The challenge here is do it without create a list.

import Quandl
import pandas as pd

UNES_HARD_COAL_BR = Quandl.get("UENG/CL_BRA", authtoken="xX6ntNSFuvq7eCZvDdvL")
UNES_HARD_COAL_BR.columns = UNES_HARD_COAL_BR.columns.str.replace(' ', '_')



ll =['Hard_coal_-_transformation_(Metric_tons,_thousand)', 'Hard_coal_-_transformation_in_coke_ovens_(Metric_tons,_thousand)','Hard_coal_-_transformation_in_electricity,_CHP_and_heat_plants_(Metric_tons,_thousand)','Hard_coal_-_transformation_in_electricity_plants_-_main_activity_producers_(Metric_tons,_thousand)','Hard_coal_-_final_energy_consumption_(Metric_tons,_thousand)','Hard_coal_-_consumption_by_manufacturing,_construction_and_non-fuel_mining_industry_(Metric_tons,_thousand)','Hard_coal_-_consumption_by_other_manuf.,_const._and_non-fuel_ind._(Metric_tons,_thousand)','Hard_coal_-_final_consumption_(Metric_tons,_thousand)','Hard_coal_-_stock_changes_(Metric_tons,_thousand)','Hard_coal_-_transformation_in_electricity_plants_-_autoproducers_(Metric_tons,_thousand)','Hard_coal_-_consumption_by_transport_(Metric_tons,_thousand)','Hard_coal_-_consumption_by_rail_(Metric_tons,_thousand)','Hrad_coal_-_consumption_by_iron_and_steel_industry_(Metric_tons,_thousand)', 'Hard_coal_-_losses_(Metric_tons,_thousand)', 'Hard_coal_-_total_energy_supply_(Metric_tons,_thousand)']

UNES_HARD_COAL_BR.drop(ll, axis=1, inplace=True)

Related

How to select only specific columns from a DataFrame with MultiIndex columns?

pandas: Extracting specific selected columns from a DataFrame to new DataFrame

Community
  • 1
  • 1
Darleison Rodrigues
  • 579
  • 1
  • 7
  • 22

1 Answers1

1

use .iloc[] and note that pandas counts columns starting from 0:

In [14]: UNES_HARD_COAL_BR = UNES_HARD_COAL_BR.iloc[:, [0,1,2,17]]

In [15]: UNES_HARD_COAL_BR.head()
Out[15]:
            Hard_coal_-_production_(Metric_tons,_thousand)  \
Date
1990-12-31                                          4595.0
1991-12-31                                          5188.0
1992-12-31                                          4731.0
1993-12-31                                          4595.0
1994-12-31                                          5134.0

            Hard_coal_-_imports_(Metric_tons,_thousand)  \
Date
1990-12-31                                      10146.0
1991-12-31                                      10758.0
1992-12-31                                      10399.0
1993-12-31                                      10975.0
1994-12-31                                      11319.0

            Hard_coal_-_exports_(Metric_tons,_thousand)  \
Date
1990-12-31                                          NaN
1991-12-31                                          NaN
1992-12-31                                          NaN
1993-12-31                                          NaN
1994-12-31                                          NaN

            Hard_coal_-_consumption_by_chemical_and_petrochemical_industry_(Metric_tons,_thousand)
Date
1990-12-31                                              260.0
1991-12-31                                              375.0
1992-12-31                                              390.0
1993-12-31                                              362.0
1994-12-31                                              354.0
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419