在 python 中使用 .assign() 方法和 lambda
use .assign() method with lambda in python
我运行这段代码在Python:
#Declaring these now for later use in the plots
TOP_CAP_TITLE = 'Top 10 market capitalization'
TOP_CAP_YLABEL = '% of total cap'
# Selecting the first 10 rows and setting the index
cap10 = cap.loc[:10, :].set_index('id')
# Calculating market_cap_perc
cap10 = cap10.assign(market_cap_perc =
lambda x: (x.market_cap_usd / cap.market_cap_usd.sum()) * 100)
# Plotting the barplot with the title defined above
ax = cap10.plot.bar(x= id, y= market_cap_perc)
ax.set_title(TOP_CAP_TITLE)
# Annotating the y axis with the label defined above
ax.set_ylabel(TOP_CAP_YLABEL)
出现错误:
NameError Traceback (most recent call last) in ()
10 lambda x: (x.market_cap_usd / cap.market_cap_usd.sum()) * 100)
11 # Plotting the barplot with the title defined above --->
12 ax = cap10.plot.bar(x= id, y= market_cap_perc)
13 ax.set_title(TOP_CAP_TITLE)
14 # Annotating the y axis with the label defined above
NameError: name 'market_cap_perc' is not defined
这是来自 DataCamp 项目探索比特币加密货币市场的 Task4 的代码。 cap
是列为id
的DataFrame(如'bitcoin'、'ripple')。另一列 market_cap_usd
(此列包含以美元为单位的加密货币市场成本。例如,'159640995719' - 它是比特币的 market_cap_usd
)。
有完成此任务的说明:
1.Select前10个硬币,设置索引为id
,并将结果DataFrame赋值给cap10
。
2.Calculate每个硬币的市值百分比使用assign()
并再次分配给cap10
。
3.Plot 条形图中标题为 "Top 10 market capitalization" 的前 10 个代币 market_cap_perc
并将其分配给 ax
.
4.Using ax
object,用“占总市值的百分比”注释 y 轴。
我尝试在 lambda 之前定义 market_cap_perc:
market_cap_perc = 0
并得到一个错误:
KeyError Traceback (most recent call last)
2133 try:
-> 2134 return self._engine.get_loc(key)
2135 except KeyError:
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4443)()
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4289)()
KeyError: 139887424317984
我得到了答案:
import pandas as pd
# Reading datasets/coinmarketcap_06122017.csv into pandas
dec6 = pd.read_csv('datasets/coinmarketcap_06122017.csv')
# Selecting the 'id' and the 'market_cap_usd' columns
market_cap_raw = dec6[['id','market_cap_usd']]
cap = market_cap_raw.query('market_cap_usd > 0')
#Declaring these now for later use in the plots
TOP_CAP_TITLE = 'Top 10 market capitalization'
TOP_CAP_YLABEL = '% of total cap'
# Selecting the first 10 rows and setting the index
cap10 = cap.head(10).set_index('id')
# Calculating market_cap_perc
cap10 = cap10.assign(market_cap_perc = lambda x: (x.market_cap_usd/cap.market_cap_usd.sum())*100)
# Plotting the barplot with the title defined above
ax = cap10.market_cap_perc.head(10).plot.bar(title=TOP_CAP_TITLE)
# Annotating the y axis with the label defined above
ax.set_ylabel(TOP_CAP_YLABEL)
我运行这段代码在Python:
#Declaring these now for later use in the plots
TOP_CAP_TITLE = 'Top 10 market capitalization'
TOP_CAP_YLABEL = '% of total cap'
# Selecting the first 10 rows and setting the index
cap10 = cap.loc[:10, :].set_index('id')
# Calculating market_cap_perc
cap10 = cap10.assign(market_cap_perc =
lambda x: (x.market_cap_usd / cap.market_cap_usd.sum()) * 100)
# Plotting the barplot with the title defined above
ax = cap10.plot.bar(x= id, y= market_cap_perc)
ax.set_title(TOP_CAP_TITLE)
# Annotating the y axis with the label defined above
ax.set_ylabel(TOP_CAP_YLABEL)
出现错误:
NameError Traceback (most recent call last) in ()
10 lambda x: (x.market_cap_usd / cap.market_cap_usd.sum()) * 100)
11 # Plotting the barplot with the title defined above --->
12 ax = cap10.plot.bar(x= id, y= market_cap_perc)
13 ax.set_title(TOP_CAP_TITLE)
14 # Annotating the y axis with the label defined above
NameError: name 'market_cap_perc' is not defined
这是来自 DataCamp 项目探索比特币加密货币市场的 Task4 的代码。 cap
是列为id
的DataFrame(如'bitcoin'、'ripple')。另一列 market_cap_usd
(此列包含以美元为单位的加密货币市场成本。例如,'159640995719' - 它是比特币的 market_cap_usd
)。
有完成此任务的说明:
1.Select前10个硬币,设置索引为id
,并将结果DataFrame赋值给cap10
。
2.Calculate每个硬币的市值百分比使用assign()
并再次分配给cap10
。
3.Plot 条形图中标题为 "Top 10 market capitalization" 的前 10 个代币 market_cap_perc
并将其分配给 ax
.
4.Using ax
object,用“占总市值的百分比”注释 y 轴。
我尝试在 lambda 之前定义 market_cap_perc:
market_cap_perc = 0
并得到一个错误:
KeyError Traceback (most recent call last)
2133 try:
-> 2134 return self._engine.get_loc(key)
2135 except KeyError:
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4443)()
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4289)()
KeyError: 139887424317984
我得到了答案:
import pandas as pd
# Reading datasets/coinmarketcap_06122017.csv into pandas
dec6 = pd.read_csv('datasets/coinmarketcap_06122017.csv')
# Selecting the 'id' and the 'market_cap_usd' columns
market_cap_raw = dec6[['id','market_cap_usd']]
cap = market_cap_raw.query('market_cap_usd > 0')
#Declaring these now for later use in the plots
TOP_CAP_TITLE = 'Top 10 market capitalization'
TOP_CAP_YLABEL = '% of total cap'
# Selecting the first 10 rows and setting the index
cap10 = cap.head(10).set_index('id')
# Calculating market_cap_perc
cap10 = cap10.assign(market_cap_perc = lambda x: (x.market_cap_usd/cap.market_cap_usd.sum())*100)
# Plotting the barplot with the title defined above
ax = cap10.market_cap_perc.head(10).plot.bar(title=TOP_CAP_TITLE)
# Annotating the y axis with the label defined above
ax.set_ylabel(TOP_CAP_YLABEL)