Skip to content

Commit e42bd37

Browse files
Merge pull request plotly#3347 from plotly/pretty_names
add pretty_names option to datasets
2 parents f724e3b + 76826f0 commit e42bd37

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

packages/python/plotly/plotly/data/__init__.py

+36-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55

6-
def gapminder(datetimes=False, centroids=False, year=None):
6+
def gapminder(datetimes=False, centroids=False, year=None, pretty_names=False):
77
"""
88
Each row represents a country on a given year.
99
@@ -24,10 +24,27 @@ def gapminder(datetimes=False, centroids=False, year=None):
2424
df["year"] = (df["year"].astype(str) + "-01-01").astype("datetime64[ns]")
2525
if not centroids:
2626
df = df.drop(["centroid_lat", "centroid_lon"], axis=1)
27+
if pretty_names:
28+
df.rename(
29+
mapper=dict(
30+
country="Country",
31+
continent="Continent",
32+
year="Year",
33+
lifeExp="Life Expectancy",
34+
gdpPercap="GDP per Capita",
35+
pop="Population",
36+
iso_alpha="ISO Alpha Country Code",
37+
iso_num="ISO Numeric Country Code",
38+
centroid_lat="Centroid Latitude",
39+
centroid_lon="Centroid Longitude",
40+
),
41+
axis="columns",
42+
inplace=True,
43+
)
2744
return df
2845

2946

30-
def tips():
47+
def tips(pretty_names=False):
3148
"""
3249
Each row represents a restaurant bill.
3350
@@ -36,7 +53,23 @@ def tips():
3653
Returns:
3754
A `pandas.DataFrame` with 244 rows and the following columns:
3855
`['total_bill', 'tip', 'sex', 'smoker', 'day', 'time', 'size']`."""
39-
return _get_dataset("tips")
56+
57+
df = _get_dataset("tips")
58+
if pretty_names:
59+
df.rename(
60+
mapper=dict(
61+
total_bill="Total Bill",
62+
tip="Tip",
63+
sex="Payer Gender",
64+
smoker="Smokers at Table",
65+
day="Day of Week",
66+
time="Meal",
67+
size="Party Size",
68+
),
69+
axis="columns",
70+
inplace=True,
71+
)
72+
return df
4073

4174

4275
def iris():

0 commit comments

Comments
 (0)