Python 如何将没有分隔符的固定宽度数字表加载到熊猫中?
这是表文件的外观(但要大得多): 每个数字都是一个值。例如,第一行具有以下值:Python 如何将没有分隔符的固定宽度数字表加载到熊猫中?,python,pandas,Python,Pandas,这是表文件的外观(但要大得多): 每个数字都是一个值。例如,第一行具有以下值: [0, 5, 8, 3, 7, 5] 我无法提前知道每个文件中的列数和行数。以下是一种“pythonic”方法: import pandas as pd with open("data") as file: df = pd.DataFrame([ [ char for char in line.rstrip() ] for line in file ]) print(df) import pandas
[0, 5, 8, 3, 7, 5]
我无法提前知道每个文件中的列数和行数。以下是一种“pythonic”方法:
import pandas as pd
with open("data") as file:
df = pd.DataFrame([ [ char for char in line.rstrip() ] for line in file ])
print(df)
import pandas as pd
df = pd.read_csv("data", header = None, dtype = "str")
df = df.apply(lambda series : [ char for char in series[0] ], axis = 1, result_type = "expand")
print(df)
输出:
0 1 2 3 4 5
0 0 5 8 3 7 5
1 7 5 8 3 6 4
2 0 0 0 1 0 0
0 1 2 3 4 5
0 0 5 8 3 7 5
1 7 5 8 3 6 4
2 0 0 0 1 0 0
以下是一种“潘多尼克”的方法:
import pandas as pd
with open("data") as file:
df = pd.DataFrame([ [ char for char in line.rstrip() ] for line in file ])
print(df)
import pandas as pd
df = pd.read_csv("data", header = None, dtype = "str")
df = df.apply(lambda series : [ char for char in series[0] ], axis = 1, result_type = "expand")
print(df)
输出:
0 1 2 3 4 5
0 0 5 8 3 7 5
1 7 5 8 3 6 4
2 0 0 0 1 0 0
0 1 2 3 4 5
0 0 5 8 3 7 5
1 7 5 8 3 6 4
2 0 0 0 1 0 0
展示您的努力并发布预期的数据帧