Reputation: 3937
Is there a way to drop columns in a Dataframe with column names having a particular letter as I wasn't able to find any information on this? I currently have the following code, which creates a dataframe that look as follows:
dates BETA0 BETA1 BETA2 BETA3 SVEN1F01 \
0 2015-06-17 5.06404203 -4.82869948 -4.40587372 -4.63038436 1.0273
1 2015-06-16 5.21759473 -5.10202006 -4.06913368 -5.23883082 1.1137
2 2015-06-15 5.25192065 -5.08400946 -4.34674471 -5.25279453 1.1193
3 2015-06-12 5.38755314 -5.17612353 -4.53605822 -5.62290340 1.1443
I want to drop all column headers having the letter F
in them. I was planning on doing it using df.drop([df.columns[[column_names]]], axis=1)
, but there are so many that I was wondering if there is an easier way to do this.
from bs4 import BeautifulSoup
import urllib.request
import requests
from itertools import islice
import pandas as pd
r = requests.get(
"http://www.federalreserve.gov/econresdata/researchdata/feds200628_1.html")
soup = BeautifulSoup(r.content)
data = soup.find('table', attrs={'rules': 'all'})
h = data.find_all("th", scope="col") #Issue
final = [[t.th.text] + [ele.text for ele in t.find_all("td")] for t in h[-1].find_all_next("tr")]
headers = [th.text for th in h]
headers.insert(0,"Date")
df = pd.DataFrame(final,columns=headers)
print(df)
Thank You
Upvotes: 2
Views: 132