简体   繁体   English

导入csv后移动的列

[英]Columns moved after importing csv

I am new to Python/Pandas. 我是Python / Pandas的新手。 I am wondering if there's a code that can help me fix how the columns move to the right inside the .csv we pull out of our systems - one column is filled with user input (containing messy characters ",) so usually after loading the user input column spreads out on several columns instead of one, wrongly moving out to the right the other columns as well. 我想知道是否有一个代码可以帮助我修复我们从系统中拉出的.csv内列如何向右移动-一列充满了用户输入(包含混乱字符“,”),因此通常在加载用户后输入列分散在几列而不是一列上,也错误地将其他列移到右侧。

I fix this manually in excel, manually filtering, deleting, moving the columns to their correct place - it takes 20 mins / day. 我会在excel中手动修复此问题,手动进行过滤,删除并将列移动到正确的位置-每天需要20分钟。 在此处输入图片说明

I would like to ask advice if there is code which I could try to clean and arrange correctly the columns or if it is easier the manual fix in excel as I do it now. 我想问一下建议,是否有代码可以尝试清理和正确排列列,或者像现在这样在excel中手动修复更容易。 Thank you! 谢谢!

pandas is altering the columns because it sees 'separators' in the import file. 熊猫正在更改列,因为它在导入文件中看到“分隔符”。

In Excel, for each newline, count how many times a comma appears. 在Excel中,对于每个换行符,计算逗号出现的次数。 Using your example above there should be 3 per line. 使用上面的示例,每行应有3个。

My quick and dirty solution would be replace the last three commas in your file with a character that is almost impossible for a user to type, I typically go for a pipe '|' 我快速而又肮脏的解决方案是将文件中的后三个逗号替换为用户几乎无法键入的字符,我通常选择竖线“ |” character. 字符。

Try importing that into pandas, specifying a new delimier/separator example below: 尝试将其导入熊猫,在下面指定一个新的delimier / separator示例:

import pandas as pd
df = pd.read_csv(filepath, sep="|")
df.head()

You cannot play with the layout with CSV that is a pure data transport format. 您不能使用纯数据传输格式的CSV布局。 Hopefully, there are 3rd party libs that can play with .xlsx files here and here . 希望这里这里都有可以与.xlsx文件一起播放的3rd party库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM