翻译来自多个数据框列的数据以创建一个句子

时间:2019-07-02 14:55:19

标签: python-3.x pandas dataframe

我正在尝试使用世界银行文件创建概括多个国家/地区统计信息的句子。我想使用数据框中的数据来创建句子。

df = pd.DataFrame({'country':['United States','Canada','Mexico'],'GDP':[2.9, 1.9, 2.0],'CPI':[2.4, 2.3, 4.9]}

def risky(val):
    if val < 2.2:
        return "at a high risk"
    else:
        return "not at a high risk"

for i in df.columns.values().tolist():
    df[i] = df[i].apply(risky)

def text_app(country, ind):
    line1 = df["country"].map(str) + " is " df["GDP"].map(str) + " of 
    defaulting."`

我一直收到以下错误:

"TypeError: can only concatenate str (not "method") to str"

df [i] .apply(risky)的输出符合预期:

country|    GDP|  CPI
United States|  not at a high risk| not at a high risk
Canada| at a high risk| not at a high risk
Mexico| at a high risk| not at a high risk

预期输出为:

"Canada is at a high risk of defaulting."
"United States is not at a high risk of defaulting."
"Mexico is at a high risk of defaulting."

0 个答案:

没有答案