如何将 load/export CSV/TSV 文件从 Pig 到 Pandas?
how to load/export CSV/TSV files from Pig to Pandas?
我正在使用 Apache PIG 处理一些数据,并且在我的脚本末尾使用
store data into '/mypath/tempp2' using PigStorage('\t','-schema');
fs -getmerge /mypath/tempp2 /localpath/data.tsv;
这样我就有了一个 tsv
文件,我可以在 Pandas 中用 read_csv(headers=0)
读取它。
问题是 tsv
文件现在在第一行包含 headers(这很好),但在第二行也包含连接到第一个观察结果的模式,例如:
col1 col2 col3
{pigschema}0 1 2
假设第一行是 [0,1,2]
。因此,除非我在 read_csv
中使用 skiprows=1
(丢失该行),否则我会在我的数据中得到这种奇怪的观察结果。
所以我想知道是否有更好的方法来导出我的数据,同时获得 headers。
非常感谢!
首先你想为-getmerge
使用-nl
参数:
store data into '/mypath/tempp2' using PigStorage('\t','-schema');
fs -getmerge -nl /mypath/tempp2 /localpath/data.tsv;
Optionally -nl can be set to enable adding a newline character (LF) at
the end of each file.
然后您的 /localpath/data.tsv
将具有以下结构:
0 - headerline
1 - empty line
2 - PIG schema
3 - empty line
4 - 1-st line of DATA
5 - 2-nd line of DATA
...
所以现在您可以在 pandas:
中轻松阅读它
df = pd.read_csv('/localpath/data.tsv', sep='\t', skiprows=[1,2,3])
我正在使用 Apache PIG 处理一些数据,并且在我的脚本末尾使用
store data into '/mypath/tempp2' using PigStorage('\t','-schema');
fs -getmerge /mypath/tempp2 /localpath/data.tsv;
这样我就有了一个 tsv
文件,我可以在 Pandas 中用 read_csv(headers=0)
读取它。
问题是 tsv
文件现在在第一行包含 headers(这很好),但在第二行也包含连接到第一个观察结果的模式,例如:
col1 col2 col3
{pigschema}0 1 2
假设第一行是 [0,1,2]
。因此,除非我在 read_csv
中使用 skiprows=1
(丢失该行),否则我会在我的数据中得到这种奇怪的观察结果。
所以我想知道是否有更好的方法来导出我的数据,同时获得 headers。
非常感谢!
首先你想为-getmerge
使用-nl
参数:
store data into '/mypath/tempp2' using PigStorage('\t','-schema');
fs -getmerge -nl /mypath/tempp2 /localpath/data.tsv;
Optionally -nl can be set to enable adding a newline character (LF) at the end of each file.
然后您的 /localpath/data.tsv
将具有以下结构:
0 - headerline
1 - empty line
2 - PIG schema
3 - empty line
4 - 1-st line of DATA
5 - 2-nd line of DATA
...
所以现在您可以在 pandas:
中轻松阅读它df = pd.read_csv('/localpath/data.tsv', sep='\t', skiprows=[1,2,3])