我不断得到:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 265-266: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
当我尝试:
df.to_html("mypage.html")
Run Code Online (Sandbox Code Playgroud)
以下是如何重现该问题的示例:
df = pd.DataFrame({"a": [u'Rue du Gu\xc3\xa9, 78120 Sonchamp'], "b": [u"some other thing"]})
df.to_html("mypage.html")
Run Code Online (Sandbox Code Playgroud)
中的元素列表"a"类型为"unicode"。
当我想将其导出到csv时,它可以工作,因为您可以执行以下操作:
df.to_csv("myfile.csv", encoding="utf-8")
Run Code Online (Sandbox Code Playgroud) 目前我在python中编写查询,将数据从oracle dbo导出到.csv文件.我不知道如何在文件中编写标题.
try:
connection = cx_Oracle.connect('user','pass','tns_name')
cursor = connection.cursor()
print "connected"
try:
query = """select * from """ .format(line_name)
tmp = cursor.execute(query)
results = tmp.fetchall()
except:
pass
except:
print IOError
filename='{0}.csv'.format(line_name)
csv_file = open(filename,'wb')
if results:
myFile = csv.writer(csv_file)
myFile.writerows(results)
else:
print "null"
csv_file.close()
Run Code Online (Sandbox Code Playgroud) import pandas as pd
Run Code Online (Sandbox Code Playgroud)
假设我dataframe喜欢这样:
df = pd.DataFrame({"a":range(4),"b":range(1,5)})
Run Code Online (Sandbox Code Playgroud)
它看起来像这样:
a b
0 0 1
1 1 2
2 2 3
3 3 4
Run Code Online (Sandbox Code Playgroud)
以及将X乘以Y的函数:
def XtimesY(x,y):
return x*y
Run Code Online (Sandbox Code Playgroud)
如果我想添加一个新的熊猫系列,我可以这样做:
df["c"] =df.apply( lambda x:XtimesY(x["a"],2), axis =1)
Run Code Online (Sandbox Code Playgroud)
有用 !
现在我要添加多个系列:
我有这个功能:
def divideAndMultiply(x,y):
return x/y, x*y
Run Code Online (Sandbox Code Playgroud)
像这样的东西?:
df["e"], df["f"] = df.apply( lambda x: divideAndMultiply(x["a"],2) , axis =1)
Run Code Online (Sandbox Code Playgroud)
它不起作用!
我希望'e'列接收分区并将'f'乘法列列出来!
注意:这不是我正在使用的代码,但我期待相同的行为.