I want to create a dataframe from 1 column that contains 1 of each variable in one column and the count in another.
This is what i have tried, i know zipping a and b is useless because its already a dictionary, but i just don’t know how to make it into a dataframe
def site_occurence(countycolumn):
print('Site Occurrence')
countylist = []
for county in countycolumn:
countylist.append(county)
a = Counter(countylist).keys()
b = Counter(countylist).values()
site_occurence = dict(zip(a, b))
df = pd.DataFrame(site_occurence)
return df
Terminal:
Site Occurrence
site
occurrence
1685
I have also tried:
def site_occurence(countycolumn):
print('Site Occurrence')
countylist = []
for county in countycolumn:
countylist.append(county)
a = Counter(countylist).keys()
b = Counter(countylist).values()
df = pd.DataFrame((a, b), columns = ['site', 'occurrence'])
return df
but only get…
Terminal:
Site Occurrence
0
1
1685
I’m expecting to see…
Angus 11
Angus / East Perthshire 1
Argyll 176
Ayrshire 62
Banffshire 7
Banffshire / Moray ...........
if i return a, b, i get this
dict_keys(['South Aberdeenshire', 'Fifeshire', 'Mid Perthshire', 'Angus', 'West Perthshire', 'East Lothian', 'East Inverness-shire / Moray', 'East Inverness-shire & Nairn', 'Kintyre', 'West Inverness-shire', 'East Ross', 'West Ross', 'Caithness', 'North Aberdeenshire', 'Dumfriesshire', 'West Sutherland', 'Berwickshire', 'Roxburghshire', 'Banffshire', 'South Aberdeenshire / Kincardineshire', 'Kincardineshire', 'Argyll', 'Moray', 'Moray / East Inverness-shire & Nairn', 'Lanarkshire', 'East Sutherland', 'Selkirkshire', 'East Perthshire', 'Clyde Isles', 'Ayrshire', 'Dunbartonshire', 'Banffshire / Moray', 'Renfrewshire', 'Angus / East Perthshire', 'Peebleshire', 'West Ross / West Sutherland', 'Kirkcudbrightshire', 'Shetland', 'Midlothian', 'Stirlingshire', 'Outer Hebrides', 'West Lothian', 'Banffshire / South Aberdeenshire', 'East Inverness-shire & Nairn / Banffshire', 'North Ebudes', 'Peebleshire / Selkirkshire', 'Midlothain / Peebleshire', 'Wigtownshire', 'Mid Ebudes', 'South Ebudes', 'East Ross / East Inverness-shire', 'East Ross / East Sutherland', 'Mid Perthshire / East Perthshire', 'Midlothian / Berwickshire', 'South Aberdeenshire / North Aberdeenshire', 'Lanarkshire / Peebleshire', 'West Inverness (Westerness)', 'West Lothian / Stirlingshire', 'Orkney', 'South Aberdeenshire / East Perthshire'])
dict_values([173, 167, 115, 33, 55, 32, 9, 242, 46, 170, 84, 90, 72, 50, 71, 181, 25, 56, 29, 1, 39, 242, 81, 2, 105, 47, 22, 45, 63, 101, 38, 1, 81, 1, 50, 2, 43, 137, 59, 72, 126, 12, 1, 1, 126, 2, 2, 22, 45, 68, 1, 1, 1, 1, 1, 1, 1, 1, 104, 1])
but its just two lists. Any recommendations?
New contributor
Daisy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.