使用read_csv将字符串转换为pandas df [英] Convert string to pandas df using read_csv
本文介绍了使用read_csv将字符串转换为pandas df的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将以下字符串转换为Pandas数据框:
I'm trying to convert the following string to a Pandas dataframe:
'2477\t1974\t89.104.195.179\tDK\t17\t212711\x00\n1974\t2370\t212.10.164.160\tDK\t19\t213017\x00\n1974\t2370\t87.50.40.214\tDK\t17\t56743\x00\n'
我遇到的问题是,大熊猫将每个值转换为它自己的列,而不是所需的6列和3行.
The problem I'm encountering is that pandas converts each value to it's own column instead of 6 columns and 3 rows as desired.
pd.read_csv(StringIO(data), sep='\t', lineterminator='\n', names=['a','b','c','d','e','f'])
我尝试使用其他一些read_csv参数没有成功.我在做什么错了?
I've tried playing around with some of the other read_csv parameters with no success. What am I doing wrong?
推荐答案
通过指定原始 sep
和 lineterminator
,它可以正常工作:
By specifying raw sep
and lineterminator
, it works:
from StringIO import StringIO
import pandas as pd
data = '2477\t1974\t89.104.195.179\tDK\t17\t212711\x00\n1974\t2370\t212.10.164.160\tDK\t19\t213017\x00\n1974\t2370\t87.50.40.214\tDK\t17\t56743\x00\n'
df = pd.read_csv(StringIO(data), sep=r'\t', lineterminator=r'\n', names=['a','b','c','d','e','f'])
这篇关于使用read_csv将字符串转换为pandas df的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文