找到你要的答案

Q:How to print pandas dataframe values in a sentence

Q:如何在一个句子里打印熊猫帧值

I have created a database using sqlite within python 2.7, and loaded the data into the pandas dataframe as below. What I'm trying to do is I would like to print the result as "The cities that are warmest in July are: Istanbul, Ankara, Izmir, Bursa". The code that I have written in Python is as below:

import sqlite3 as lite
import pandas as pd

con = lite.connect("project_warmest.db")

with con:
    cur = con.cursor()
    cur.execute("DROP TABLE IF EXISTS cities;")
    cur.execute("DROP TABLE IF EXISTS weather;")
    cur.execute("CREATE TABLE cities (name text, region text)")
    cur.execute("CREATE TABLE weather (city text, warm_month text, average_high integer)")

    cur.execute("INSERT INTO cities VALUES('Istanbul', 'Marmara')")
    cur.execute("INSERT INTO cities VALUES('Ankara', 'Ic Anadolu')")
    cur.execute("INSERT INTO cities VALUES('Izmir', 'Ege')")
    cur.execute("INSERT INTO cities VALUES('Antalya', 'Akdeniz')")
    cur.execute("INSERT INTO cities VALUES('Bursa', 'Marmara')")

    cur.execute("INSERT INTO weather VALUES('Istanbul', 'July',24)")
    cur.execute("INSERT INTO weather VALUES('Ankara', 'July',21)")
    cur.execute("INSERT INTO weather VALUES('Izmir', 'July',27)")
    cur.execute("INSERT INTO weather VALUES('Antalya', 'August',30)")
    cur.execute("INSERT INTO weather VALUES('Bursa', 'July',23)")
    cur.execute("SELECT city FROM weather INNER JOIN cities ON name = city WHERE warm_month = 'July'")

    rows = cur.fetchall()
    cols = [desc[0] for desc in cur.description]
    df = pd.DataFrame(rows, columns = cols)

print "The cities that are warmest in July are: %s, " %df.iloc[0]["city"]

我创建了一个数据库采用SQLite在Python 2.7,并加载数据到熊猫帧如下。我想做的是打印结果,“最温暖的城市在七月是:伊斯坦布尔,安卡拉,伊兹密尔,囊”。我写的Python代码如下:

import sqlite3 as lite
import pandas as pd

con = lite.connect("project_warmest.db")

with con:
    cur = con.cursor()
    cur.execute("DROP TABLE IF EXISTS cities;")
    cur.execute("DROP TABLE IF EXISTS weather;")
    cur.execute("CREATE TABLE cities (name text, region text)")
    cur.execute("CREATE TABLE weather (city text, warm_month text, average_high integer)")

    cur.execute("INSERT INTO cities VALUES('Istanbul', 'Marmara')")
    cur.execute("INSERT INTO cities VALUES('Ankara', 'Ic Anadolu')")
    cur.execute("INSERT INTO cities VALUES('Izmir', 'Ege')")
    cur.execute("INSERT INTO cities VALUES('Antalya', 'Akdeniz')")
    cur.execute("INSERT INTO cities VALUES('Bursa', 'Marmara')")

    cur.execute("INSERT INTO weather VALUES('Istanbul', 'July',24)")
    cur.execute("INSERT INTO weather VALUES('Ankara', 'July',21)")
    cur.execute("INSERT INTO weather VALUES('Izmir', 'July',27)")
    cur.execute("INSERT INTO weather VALUES('Antalya', 'August',30)")
    cur.execute("INSERT INTO weather VALUES('Bursa', 'July',23)")
    cur.execute("SELECT city FROM weather INNER JOIN cities ON name = city WHERE warm_month = 'July'")

    rows = cur.fetchall()
    cols = [desc[0] for desc in cur.description]
    df = pd.DataFrame(rows, columns = cols)

print "The cities that are warmest in July are: %s, " %df.iloc[0]["city"]
answer1: 回答1:

You could join array of elements from df["city"] like

In [53]: print "The cities warmest in July are: %s" % ', '.join(df["city"].values)
The cities warmest in July are: Istanbul, Ankara, Izmir, Bursa

', '.join(df["city"].values) -- this will return a comma-separated string.


Also, you could use pd.read_sql() or pd.read_sql_query to directly read the sql results to dataframe.

In [54]: pd.read_sql("SELECT city FROM weather INNER JOIN cities ON name = city"
   ....:             " WHERE warm_month = 'July'", con)
Out[54]:
       city
0  Istanbul
1    Ankara
2     Izmir
3     Bursa

你可以加入阵列的元素[城市]喜欢DF

In [53]: print "The cities warmest in July are: %s" % ', '.join(df["city"].values)
The cities warmest in July are: Istanbul, Ankara, Izmir, Bursa

','加入(DF [城市]。值),这将返回一个以逗号分隔的字符串。


另外,你可以使用PD。read_sql()或pd.read_sql_query直接读取SQL结果帧。

In [54]: pd.read_sql("SELECT city FROM weather INNER JOIN cities ON name = city"
   ....:             " WHERE warm_month = 'July'", con)
Out[54]:
       city
0  Istanbul
1    Ankara
2     Izmir
3     Bursa
python  pandas  sqlite3