python py__pandas.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python py__pandas.py相关的知识,希望对你有一定的参考价值。


# --------------------------------------
# Handling encoding
# --------------------------------------
import pandas as pd

for file in os.listdir(os.curdir):
	if not file.endswith('csv'):
		continue
	try:
		df = pandas.read_csv(file, low_memory=False)
	except UnicodeDecodeError:
		df = pandas.read_csv(file, encoding='latin1') # latin1 = ISO-8859-1


# ==============================================================================
# Databases 
# ==============================================================================


# --------------------------------------
# Reading from SQL Server
# --------------------------------------
# http://stackoverflow.com/questions/30535476/pandas-reading-from-sql-server-and-outputting-to-csv-encoding-problems#30535477
"""
Need to convert any column with non-ascii chars to udf explicitly
"""
def convert(my_str):
	my_str.decode('Windows-1252').encode('utf-8')
df["Name"] = df["Name"].apply(convert)


class ClassName(object):
	"""docstring for ClassName"""
	def __init__(self, arg):
		super(ClassName, self).__init__()
		self.arg = arg

以上是关于python py__pandas.py的主要内容,如果未能解决你的问题,请参考以下文章

python Pandas.py

python pandas.py

python test_pandas.py

python test_pandas.py

python pandas_snippets.py

python python-pandas-reading-from-files.py