![](/img/trans.png)
[英]Python project - Writing contents of .txt file to Pandas dataframe
[英]converting the contents of txt file to columns of pandas dataframe
我有这种类型的 .txt 文件
12
21
23
1
23
42
12
0
其中 <12,21,23> 是特征,<1> 是 label。 <23,42,12> 是特征,<0> 是 label 等等。 我想从上面的文本文件中创建一个 pandas dataframe ,它只包含一列到多列。 dataframe 的格式为{column1,column2,column3,column4}。 并且其中没有列名。 有人可以帮我解决这个问题吗? 谢谢
import pandas as pd
df = dict()
features = list()
label = ''
filename = '.txt'
with open(filename) as fd:
i = 0
for line in fd:
if i != 3:
features.append(line.strip())
i += 1
else:
label = line.strip()
i = 0
df[label] = features
features = list()
df = pd.DataFrame(df)
df
import pandas as pd
with open(<FILEPATH>, "r") as f:
lines = f.readlines()
formatted = [int(line[:-1]) for line in lines] # Remove \n and convert to int
labels = formatted[3::4]
features = list(zip(formatted[::4], formatted[1::4], formatted[2::4])) # You can modify this if there are more than three rows
data = {}
for i, label in enumerate(labels):
data[label] = list(features[i])
df = pd.DataFrame(data)
如果您有任何问题或发现任何错误,请发表评论,我会进行修改。
可以先用numpy,需要保证值的个数是4的倍数
a = np.loadtxt('file.txt').reshape((4,-1), order='F')
df = pd.DataFrame(a[:-1], columns=a[-1])
Output:
1.0 0.0
0 12.0 23.0
1 21.0 42.0
2 23.0 12.0
a = np.loadtxt('file.txt').reshape((-1,4))
df = pd.DataFrame(a)
Output:
0 1 2 3
0 12.0 21.0 23.0 1.0
1 23.0 42.0 12.0 0.0
row = []
i = 0
data = []
with open('a.txt') as f:
for line in f:
data
i+= 1
row.append(int(line.strip()))
if i%4==0 and i!=0:
print(i)
data_rows_count +=1
data.append(row)
row = []
f.close()
df = pd.DataFrame(data)
结果 df 为:
0 1 2 3
0 12 21 23 1
1 23 42 12 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.