text_html_output_1.png
import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import plotly.express as px

import plotly.graph_objects as go

ci = pd.read_csv('../input/farmers-markets-in-the-united-states/wiki_county_info.csv')
fm = pd.read_csv('../input/farmers-markets-in-the-united-states/farmers_markets_from_usda.csv')

import plotly.express as px

# create a DataFrame with the count of farmers markets by state
state_farms = pd.DataFrame(fm['State'].value_counts())

# rename the column 'State' to 'Number of farmers markets'
state_farms.rename(columns={'State':'Number of farmers markets'}, inplace = True)

# create a bar chart 
fig = px.bar(
    state_farms,
    x=state_farms.index,
    y='Number of farmers markets',
    color='Number of farmers markets',  # Assign colors based on the number of farmers markets
    color_continuous_scale='Blues',  # Set the color scale
    title='Number of Farmers Markets by State'
)

# change visual style
fig.update_layout(
    xaxis_title='State',
    yaxis_title='Number of Farmers Markets',
    plot_bgcolor='white',  # Set the background color of the plot
    showlegend=False  # Hide the legend
)

# show the bar chart
fig.show()

import plotly.graph_objects as go
fig = go.Figure(data=go.Scattergeo(lon=fm['x'], lat=fm['y'], mode='markers', marker=dict(size=1, opacity=1, reversescale=True, autocolorscale=False, symbol=0, line=dict(width=1, color='rgba(102, 102, 102)'), colorscale='icefire', cmin=0)))
fig.update_layout(title='US farmers markets', geo_scope='usa')

# create a collection of strings
categories = ['Coffee', 'Beans', 'Fruits', 'Grains', 'Juices', 'Mushrooms', 'PetFood', 'Tofu', 'WildHarvested']

# count the occurrences of "Y"(yes) for each category to see how many markets have those categories
category_counts = fm[categories].eq('Y').sum()

# convert the category totals to a DataFrame for plotting
df_category_totals = pd.DataFrame({'Category': category_counts.index, 'Count': category_counts.values})

# sort the DataFrame by count in descending order
df_category_totals = df_category_totals.sort_values('Count', ascending=False)

# plot the bar chart
fig = px.bar(df_category_totals, x='Category', y='Count', color='Category',
             title='Frequency of Categories in Markets',
             labels={'Category': 'Category', 'Count': 'Count'},
             color_discrete_sequence=px.colors.qualitative.Safe)

fig.show()

def convert_dollar_number(data):
    if type(data) == float:
        return data
    elif type(data) == str and '$' in data:
        return int(''.join(data.split('$')[1].split(',')))
    elif type(data) == str and '$' not in data:
        return int(''.join(data.split(',')))
for i in range(3, 8):
    ci[ci.columns[i]] = ci[ci.columns[i]].apply(convert_dollar_number)

state_level = ci[['State', 'median household income', 'population', 'number of households']].groupby('State').agg({'median household income': 'mean', 'population': 'mean', 'number of households': 'mean'})

markets = pd.concat([state_level, no_markets_per_state], axis=1)

markets.head()