将多个列连接到新列中。csv文件,python,熊猫
我有一个很大的csv索引,有x个列和y个行。我希望我的代码在每个csv(whileindex循环)中运行,并将具有特定标题的列合并到一个新列中,然后将csv保存到一个新路径中。这是到目前为止我的代码,但我遇到了错误:将多个列连接到新列中。csv文件,python,熊猫,python,pandas,csv,Python,Pandas,Csv,我有一个很大的csv索引,有x个列和y个行。我希望我的代码在每个csv(whileindex循环)中运行,并将具有特定标题的列合并到一个新列中,然后将csv保存到一个新路径中。这是到目前为止我的代码,但我遇到了错误: 'utf-8' codec can't decode byte 0xa9 in position 33: invalid start byte 有什么想法吗 import os import pandas as pd #code to add new row to all cs
'utf-8' codec can't decode byte 0xa9 in position 33: invalid start byte
有什么想法吗
import os
import pandas as pd
#code to add new row to all csvs with unique identifier stamp that combines
the following:
#wellkey+drillkey+lat+long+spuddate
files=['Apr 23 2018.csv','Apr 20 2018.csv']
index=0
os.chdir('file path')
#code to loop through all the files listed above
while index < len(files):
os.chdir('file path')
current_file=files[index]
#unique identifier column
df=pd.read_csv(current_file)
df['Unique Identifier']=df['A'] + "-" + df['B'] + "-" + df['C'] + "-" +
df['D'] + "-" + df['E']
df.to_csv(current_file)
#save new csv
os.chdir('New file Path')
index = index + 1
导入操作系统
作为pd进口熊猫
#用于向所有CSV添加新行的代码,该行具有组合的唯一标识符戳
以下是:
#井键+钻键+横向+纵向+钻孔
文件=['Apr 23 2018.csv','Apr 20 2018.csv']
索引=0
chdir('文件路径')
#循环浏览上面列出的所有文件的代码
索引
谢谢您的建议/评论/更正。当我遇到这个问题时,我将尝试的第一件事是将
encoding='ISO-8859-1
添加到我的pd.read\u csv()
语句中
因此,您的声明如下所示:
df=pd.read\u csv(当前文件,编码为='ISO-8859-1')
欢迎使用SO的可能副本。请花时间阅读该页面上的其他链接。