使用Pandas将列复制到文件时遇到问题

时间:2019-03-02 18:53:59

标签: python pandas writefile

代码的目标只是将OHE分为两列,并将其余的列写入原始文件中。但是,如图中所示,Dur列在写入第二个文件时传递的内容超出了应有的方式,因此以某种方式“调试”。我不想限制该字段,因为原始文件太大,并且可能具有包含较长和较短字段的行,并且以后可能会使分析复杂化。

Image of the problem

import pandas as pd
from sklearn.preprocessing import LabelEncoder,OneHotEncoder

def opendataset():
    file = pd.read_csv('originalfiletest.binetflow')
    return file

def writefile():
    df.to_csv('newfiletest.binetflow', columns=['Dur','Proto','State','TotBytes','average_packet_size','average_bits_psecond'], index=False)

def writebackupproto():
    df.to_csv('fieldprotobackup.binetflow', columns=['Proto2','Proto'], index=False)

def writebackupstate():
    df.to_csv('fieldstatebackup.binetflow', columns=['State2','State'], index=False)

df = opendataset()

df['State2'] = df['State']
df['Proto2'] = df['Proto']

le = LabelEncoder()
dfle = df
dfle.State = le.fit_transform(dfle.State)
X = dfle[['State']].values
Y = dfle[['Proto']].values
ohe = OneHotEncoder()
OnehotX = ohe.fit_transform(X).toarray()
OnehotY = ohe.fit_transform(Y).toarray()

dx = pd.DataFrame(data=OnehotX)
dy = pd.DataFrame(data=OnehotY)

dfle['State'] = (dx[dx.columns[0:]].apply(lambda x:''.join(x.dropna().astype(int).astype(str)), axis=1))
dfle['Proto'] = (dy[dy.columns[0:]].apply(lambda y:''.join(y.dropna().astype(int).astype(str)), axis=1))

writefile()
writebackupproto()
writebackupstate()

1 个答案:

答案 0 :(得分:0)

看起来唯一的错误是您的值没有被截断。您只需将pandas.Series.apply方法与“截断lambda”一起使用,即可获得预期的结果。

df.Dur = df.Dur.apply(lambda n: '%.6f' % n) 

一个可行的示例可能会截断pi

from math import pi
non_truncated = pd.Series(10*[pi])
non_truncated.apply(lambda n: '%.2f' % n)

您会看到一个被截断的系列

0    3.14
1    3.14
2    3.14
3    3.14
4    3.14
5    3.14
6    3.14
7    3.14
8    3.14
9    3.14
dtype: object