从 unix box 读取 .zip 文件后如何 return Pandas Dataframe
How to return a Pandas Dataframe after reading .zip file from unix box
我在某个 unix 服务器上有一个文件 abc.zip。
我需要读取 abc.zip 文件的内容并将结果存储在 pandas 数据帧
我尝试使用 paramiko 从 unix 服务器读取文件,但无法将结果存储在 pandas dataframe 中。
import paramiko
ssh_client=paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname='xyx',username='abc',password='qwe')
#using unzip is creating 3 lines which are not needed in dataframe , so
#using tail.
stdin,stdout,stderr=ssh_client.exec_command("unzip -c path/abc.zip | tail -n +3")
#created an empty list.
unix_file =[]
#read the data from stdout and appended in list
for line in stdout.readlines():
unix_file.append(line)
#tried creating Dataframe by iterating over unix_file list
df = pd.DataFrame([sub.split("\t") for sub in unix_file])
我期望 Dataframe 带有 header 和 data ,但我得到的实际 o/p 是 Dataframe 正在将 Columns 视为 data 的一部分,并且默认分配 [0,1,2,]值作为列 .
读取 .zip 文件和处理 tab-separated 列表的任何更好的方法都会有所帮助。
如果 read_csv
适合您,请将其与您的远程数据一起使用:
stdin,stdout,stderr = ssh_client.exec_command("unzip -c path/abc.zip | tail -n +3")
pyitgdf = pd.read_csv(stdout, sep='\t', header=0)
我做了一些解决方法,可能不是最好的方法,但它确实有效。
'
stdin,stdout,stderr=ssh_client.exec_command("unzip -c /ges2/data/TransactionData/ITG/Well_extract_20120406_test.zip | tail -n +3")
unix_file =[]
for line in stdout.readlines():
unix_file.append(line)
output = [line.rstrip() for line in unix_file]
col = output[0].split('\t')
data = output[1:]
pyitgdf = pd.DataFrame([sub.split('\t') for sub in data], columns = col)
print(pyitgdf.head(5))
`
我知道有一些简洁的方法可以实现这一点,但我没有得到它
我在某个 unix 服务器上有一个文件 abc.zip。 我需要读取 abc.zip 文件的内容并将结果存储在 pandas 数据帧
我尝试使用 paramiko 从 unix 服务器读取文件,但无法将结果存储在 pandas dataframe 中。
import paramiko
ssh_client=paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname='xyx',username='abc',password='qwe')
#using unzip is creating 3 lines which are not needed in dataframe , so
#using tail.
stdin,stdout,stderr=ssh_client.exec_command("unzip -c path/abc.zip | tail -n +3")
#created an empty list.
unix_file =[]
#read the data from stdout and appended in list
for line in stdout.readlines():
unix_file.append(line)
#tried creating Dataframe by iterating over unix_file list
df = pd.DataFrame([sub.split("\t") for sub in unix_file])
我期望 Dataframe 带有 header 和 data ,但我得到的实际 o/p 是 Dataframe 正在将 Columns 视为 data 的一部分,并且默认分配 [0,1,2,]值作为列 .
读取 .zip 文件和处理 tab-separated 列表的任何更好的方法都会有所帮助。
如果 read_csv
适合您,请将其与您的远程数据一起使用:
stdin,stdout,stderr = ssh_client.exec_command("unzip -c path/abc.zip | tail -n +3")
pyitgdf = pd.read_csv(stdout, sep='\t', header=0)
我做了一些解决方法,可能不是最好的方法,但它确实有效。
'
stdin,stdout,stderr=ssh_client.exec_command("unzip -c /ges2/data/TransactionData/ITG/Well_extract_20120406_test.zip | tail -n +3")
unix_file =[]
for line in stdout.readlines():
unix_file.append(line)
output = [line.rstrip() for line in unix_file]
col = output[0].split('\t')
data = output[1:]
pyitgdf = pd.DataFrame([sub.split('\t') for sub in data], columns = col)
print(pyitgdf.head(5))
`
我知道有一些简洁的方法可以实现这一点,但我没有得到它