pandas 从字符串转换为熊猫数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53278358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:09:19  来源:igfitidea点击:

convert from string to pandas dataframe

pythonpandasdataframeamazon-s3aws-lambda

提问by prakhar kumar

I'm importing a csv file from AWS S3 in AWS Lambdawith below code:

我正在使用以下代码从 AWS Lambda 中的 AWS S3 导入一个 csv 文件:

file = s3.get_object(Bucket = bucket, Key = key)
rows = file['Body'].read().decode('utf-8').splitlines(False)

I'm getting input in below format :

我收到以下格式的输入:

data = "a,b,c,d,\"x,y\",e,f"

数据 = "a,b,c,d,\"x,y\",e,f"

and I want output in below format:

我想要以下格式的输出:

>>>`>>> df
   0  1  2  3    4  5  6
0  a  b  c  d  x,y  e  f`

i have to split data based on ',' but if some thong is between " " they should remain as it is.

我必须根据 ',' 拆分数据,但是如果在“”之间有一些丁字裤,它们应该保持原样。

Or if you have any other solution for import csv file from s3 to lambda and converting in Data Frame, Please suggest

或者,如果您有将 csv 文件从 s3 导入 lambda 并在数据框中转换的任何其他解决方案,请提出建议

回答by Mohamed Thasin ah

use csvmodule

使用csv模块

try this,

尝试这个,

from csv import reader
import pandas as pd
data=["a,b,c,d,\"x,y\",e,f"]
df=pd.DataFrame( list(reader(data)))
print df

Output:

输出:

   0  1  2  3    4  5  6
0  a  b  c  d  x,y  e  f