Series对象没有split属性 - 从文本文件中读取数据

时间:2017-08-10 19:28:46

标签: python pandas

我正在读取来自.txt文件的数据,如下所示,我正在使用底部的代码提取每个括号中的第三项。

使用行data = data.iloc[0, ::4],我每4个数据点中就删除了3个(我认为)。

但是,现在我试图保留每个数据点。所以当我删除线; data = data.iloc[0, ::4],我收到以下错误:

"'Series' object has no attribute 'split'", 'occurred at index 0'

我不明白为什么删除该行会给我收到的错误。简而言之,我想从每个括号中提取每三个项目,并将其存储在pandas DataFrame中。但是当我尝试删除行data = data.iloc[0, ::4]时,我会抛出错误(如上所示)。

感谢您的支持!

enter image description here

data = pd.read_csv("C:\\Users\\Desktop\\data_test9.txt", sep="\[|\]\[|\]",engine = 'python', header = None)
data = data.iloc[0, ::4]
data = data.astype(str).apply(lambda x: x.split(',')[-1]).astype(float)
data.pop(0)
xar = range(len(data)
yar = pd.DataFrame(data)

数据:

[0.0, -0.01953125, -0.0009765625][0.0, -0.01953125, -0.0009765625][0.0, -0.01953125, -0.0009765625][0.0, -0.01953125, -0.0009765625][0.0078125, -0.912109375, -0.00390625][0.0078125, -0.912109375, -0.00390625][0.0078125, -0.912109375, -0.00390625][0.0078125, -0.912109375, -0.00390625][0.0078125, -0.912109375, -0.00390625][0.0078125, -0.912109375, -0.00390625][0.0078125, -0.912109375, -0.00390625][0.0078125, -0.912109375, -0.00390625][0.0087890625, -0.978515625, -0.00390625][0.0087890625, -0.978515625, -0.00390625][0.0087890625, -0.978515625, -0.00390625][0.0087890625, -0.978515625, -0.00390625][0.0087890625, -0.978515625, -0.00390625][0.0078125, -0.9521484375, -0.0029296875][0.0078125, -0.9521484375, -0.0029296875][0.0078125, -0.9521484375, -0.0029296875][0.0078125, -0.9521484375, -0.0029296875][0.0078125, -0.9521484375, -0.0029296875][0.0078125, -0.9521484375, -0.0029296875][0.0078125, -0.9521484375, -0.0029296875][0.0078125, -0.9521484375, -0.0029296875][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.955078125, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.0029296875][0.0078125, -0.9560546875, -0.0029296875][0.0078125, -0.9560546875, -0.0029296875][0.0078125, -0.9560546875, -0.0029296875][0.0078125, -0.9560546875, -0.0029296875][0.0078125, -0.9560546875, -0.0029296875][0.0078125, -0.9560546875, -0.0029296875][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0078125, -0.9560546875, -0.0048828125][0.0087890625, -0.9560546875, -0.0029296875][0.0087890625, -0.9560546875, -0.0029296875][0.0087890625, -0.9560546875, -0.0029296875][0.0087890625, -0.9560546875, -0.0029296875][0.0087890625, -0.9560546875, -0.0029296875][0.0087890625, -0.9560546875, -0.0029296875][0.0087890625, -0.9560546875, -0.0029296875][0.0087890625, -0.9560546875, -0.0029296875][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.0029296875][0.0078125, -0.9560546875, -0.0029296875][0.0078125, -0.9560546875, -0.0029296875][0.0078125, -0.9560546875, -0.0029296875][0.0078125, -0.9560546875, -0.0029296875][0.0078125, -0.9560546875, -0.0029296875][0.0078125, -0.9560546875, -0.0029296875][0.0068359375, -0.9560546875, -0.00390625][0.0068359375, -0.9560546875, -0.00390625][0.0068359375, -0.9560546875, -0.00390625][0.0068359375, -0.9560546875, -0.00390625][0.0068359375, -0.9560546875, -0.00390625][0.0068359375, -0.9560546875, -0.00390625][0.0068359375, -0.9560546875, -0.00390625][0.0068359375, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625][0.0078125, -0.9560546875, -0.00390625]

编辑1: 我的数据保存如下:

while True:
    delay(DELAY_INTERVAL)

    file = open("C:\\Users\\Desktop\\demo3.txt", "a")
    file.writelines(str(convert(readField())))
    file.flush()
    file.close()

1 个答案:

答案 0 :(得分:1)

因为datapd.DataFrame,所以当你.apply函数时,它会将pd.Series作为参数传递给函数。 pd.Series没有.split方法。当你这样做时:

data = data.iloc[0, ::4]

您正在选择第一行和每四列,从第一行开始。这*返回pd.Series。所以考虑一下:

In [16]: df
Out[16]:
     a   b   c   d   e
0  -10  17  17  14 -10
1    2  10   0   4  -5
2   16  10   1  -9  -6
3   12  -6  10   5   3
4    9  10  -4  10   3
5   -8  16   4  17   0
6    9  12   4  17  -5
7   19  17   9   2  19
8    1   4  10  10  12
9   12  -1   9  -5  10
10  -1  -3   4  11   5
11  19   4   6  16   3
12   5  19  -1   1  15
13  18   2  15   0   7
14   3  10  15   6  15
15   8  12   3  -6   8
16  14   0  -3   6   2
17  -1  -5  -5  -3  16
18  -2  12  17  17  13
19  18   4  19  14   4

In [17]: df.iloc[0, ::4]
Out[17]:
a   -10
e   -10
Name: 0, dtype: int64

In [18]: type(df.iloc[0, ::4])
Out[18]: pandas.core.series.Series

现在,当你.apply 系列的函数时,它将系列中的每个传递给函数,这就是你的{{1}因为每个值都是lambda,而字符串确实有 str方法。

所以,也许你想要:

.split

选择每四行,并返回一个数据框。