Python 使用数据流清理CSV文件中的数据
我正在尝试从GCS读取一个CSV(带标题)文件,该文件大约有150列,然后Python 使用数据流清理CSV文件中的数据,python,google-cloud-platform,google-cloud-storage,google-cloud-dataflow,Python,Google Cloud Platform,Google Cloud Storage,Google Cloud Dataflow,我正在尝试从GCS读取一个CSV(带标题)文件,该文件大约有150列,然后 1.为特定列设置列数据 2.使用空值更新所有列的NaN 3.将csv文件(带标题)写入GCS 这里是棘手的部分:处理是在云数据流上完成的,这意味着我必须使用Apache beam转换来实现这一点。 我尝试了多种方法,比如跳过标题行和使用模式 我的管道代码是: def parse_method(self, line): reader = csv.reader(line.split('\n')) f
1.为特定列设置列数据
2.使用空值更新所有列的NaN
3.将csv文件(带标题)写入GCS 这里是棘手的部分:处理是在云数据流上完成的,这意味着我必须使用Apache beam转换来实现这一点。
我尝试了多种方法,比如跳过标题行和使用模式 我的管道代码是:
def parse_method(self, line):
reader = csv.reader(line.split('\n'))
for csv_row in reader:
values = [x.decode('utf8') for x in csv_row]
row = []
for value in csv_row:
if value == 'NaN':
value = 'Null'
row.append(value)
return row
(p
| 'Read_from_source' >> beam.io.ReadFromText('gs://{0}/test.csv'.format(BUCKET))
| 'Split' >> beam.Map(lambda s: data_ingestion.parse_method(s))
| 'Write_to_dest' >> beam.io.WriteToText(output_prefix,file_name_suffix='.csv', num_shards=1))
例如:
如果我的csv输入包含
名称custom1 custom2arun未定义Nan
丹妮·洛桑 预期csv
名称custom1 custom2
阿伦·洛桑零度
dany losangels使用以下命令生成您要查找的输出:
lines = p | ReadFromText(file_pattern="gs://<my-bucket>/input_file.csv")
def parse_method(line):
import csv
reader = csv.reader(line.split('\n'))
for csv_row in reader:
values = [x.decode('utf8') for x in csv_row]
row = []
for value in csv_row:
if value == 'NaN':
value = 'Null'
row.append(value)
return ",".join(row)
lines = lines | 'Split' >> beam.Map(parse_method)
line = lines | 'Output to file' >> WriteToText(file_pattern="gs://<my-bucket>/output_file.csv")
lines=p | ReadFromText(file_pattern=“gs:///input_file.csv”)
def parse_方法(第行):
导入csv
reader=csv.reader(line.split('\n'))
对于读卡器中的csv_行:
值=[x.decode('utf8'),用于csv_行中的x]
行=[]
对于csv_行中的值:
如果值=='NaN':
值='Null'
行追加(值)
返回“,”。连接(行)
lines=lines |“Split”>>beam.Map(解析法)
line=lines |“输出到文件”>>WriteToText(file_pattern=“gs:///Output_file.csv”)
现在,对于基于标题编辑列,我不确定是否有更直接的方法,但我将使用以下方法:
lines = p | "ReadFromText" >> ReadFromText(file_pattern="gs://<my-bucket>/input_file.csv")
def parse_method(line):
import pandas as pd
line = line.split(',')
df = pd.DataFrame(data=[line],columns=['name','custom1','custom2'])
df['custom2'] = df.custom2.apply(lambda x: 'None' if x == 'Nan' else x)
values = list(df.loc[0].values)
return ",".join(values)
lines = lines | "Split" >> beam.Map(parse_method)
line = lines | "Output to file" >> WriteToText(file_path_prefix="gs://<my-bucket>/output_file.csv")
lines=p |“ReadFromText”>>ReadFromText(file\u pattern=“gs:///input\u file.csv”)
def parse_方法(第行):
作为pd进口熊猫
line=line.split(',')
df=pd.DataFrame(数据=[line],列=['name','custom1','custom2'])
df['custom2']=df.custom2.apply(如果x='Nan'else x,则lambda x:'None')
值=列表(df.loc[0]。值)
返回“,”.join(值)
lines=lines |“Split”>>beam.Map(parse_方法)
line=lines |“输出到文件”>>WriteToText(文件路径\u前缀=“gs:///Output\u file.csv”)
您能详细说明您遇到了什么样的错误吗?您是否遇到了问题,因为标题没有被丢弃?或者你的文件怎么了?嘿,巴勃罗,这是两个问题,1。文件以列表形式打印,2。根据从CSV读取的列标题更改值。谢谢,我能够按预期写入CSV。但问题是根据列编辑文件。标题是从源中读取的