我有许多文本文件,比如50,我需要读入一个庞大的数据帧.目前,我正在使用以下步骤.
这适用于100 KB大小的文件 - 几分钟,但在50 MB时,它只需要几个小时,并且不实用.
如何优化我的代码?特别是 -
这是一个示例代码.我自己的代码有点复杂,因为文本文件更复杂,因此我必须使用大约10个正则表达式和多个while循环来读取数据并将其分配到正确数组中的正确位置.为了保持MWE的简单,我没有在MWE的输入文件中使用重复标签,所以我希望我无缘无故地读取文件两次.我希望这是有道理的!
import re
import pandas as pd
df = pd.DataFrame()
paths = ["../gitignore/test1.txt", "../gitignore/test2.txt"]
reg_ex = re.compile('^(.+) (.+)\n')
# read all files to determine what indices are available
for path in paths:
file_obj = open(path, 'r')
print file_obj.readlines()
['a 1\n', 'b 2\n', 'end']
['c 3\n', 'd 4\n', 'end']
indices = []
for path in paths:
index = []
with open(path, 'r') …Run Code Online (Sandbox Code Playgroud) 我有三个我试图连接的DataFrame.
concat_df = pd.concat([df1, df2, df3])
Run Code Online (Sandbox Code Playgroud)
这会导致MemoryError.我该如何解决这个问题?
请注意,大多数现有的类似问题都是在读取大文件时发生的MemoryErrors上.我没有那个问题.我已将我的文件读入DataFrames.我只是不能连接那些数据.
我正在寻找一种将复杂文本文件解析为pandas DataFrame的简单方法.下面是一个示例文件,我希望解析后的结果和我当前的方法.
有没有办法让它更简洁/更快/更pythonic /更可读?
我也把这个问题放在Code Review上.
我最终写了一篇博客文章,向初学者解释这一点.
这是一个示例文件:
Sample text
A selection of students from Riverdale High and Hogwarts took part in a quiz. This is a record of their scores.
School = Riverdale High
Grade = 1
Student number, Name
0, Phoebe
1, Rachel
Student number, Score
0, 3
1, 7
Grade = 2
Student number, Name
0, Angela
1, Tristan
2, Aurora
Student number, Score
0, 6
1, 3
2, 9
School = Hogwarts …Run Code Online (Sandbox Code Playgroud) 我正在尝试做doctest.'预期'和'得到'结果是相同的,但我的doctest仍然失败.它失败了,因为x-axis y-axis在打印输出之后有一些尾随空格我没有包含在我的文档字符串中.我怎么包括它呢?当我手动插入空格并进行测试时,只要我将光标保持在那里,它就会成功运行.
x轴y轴______________________ [光标在这里]
但是,如果我使用我的光标在其他地方运行测试,则会删除尾随空格并且测试失败.
我知道这听起来很奇怪,但它就是它!
这是代码:
import pandas as pd
import doctest
class NewDataStructure(pd.DataFrame):
"""
>>> arrays = [[1, 1, 2, 2], [10, 20, 10, 20]]
>>> index = pd.MultiIndex.from_arrays(arrays, names=('x-axis', 'y-axis'))
>>> data_input = {"Pressure (Pa)": [1+1j, 2+2j, 3+3j, 4+4j],
... "Temperature": [1, 2, 3, 4]}
>>> new_data_variable = NewDataStructure(data=data_input, index=index, title="Pressures and temperatures")
>>> print new_data_variable
New Data Structure Pressures and temperatures:
Pressure (Pa) Temperature
x-axis y-axis
1 10 (1+1j) 1
20 (2+2j) 2
2 …Run Code Online (Sandbox Code Playgroud) 在我的Sphinx文档中,当我在重组文本中引用它们时,我想显示键盘键的图片.
例如,如果我说:点击Enter键.我想在线显示Enter键的图片,而不是单词Enter.
我在许多教程中都看到过这种图形,用于引用键盘按键,菜单选项等.他们是如何做到这一点的?我可以在Sphinx中这样做吗?
我希望能够在以下代码中为图例添加标题.但是,看一下这些文档,我认为没有办法解决这个问题.
import plotly.plotly as py
import plotly.graph_objs as go
trace0 = go.Scatter(
x=[1, 2, 3, 4, 5],
y=[1, 2, 3, 4, 5],
)
trace1 = go.Scatter(
x=[1, 2, 3, 4, 5],
y=[5, 4, 3, 2, 1],
)
data = [trace0, trace1]
fig = go.Figure(data=data)
py.iplot(fig, filename='default-legend')
Run Code Online (Sandbox Code Playgroud) 这个问题与Python连接文本文件有关
我有一个清单file_names,比如['file1.txt', 'file2.txt', ...].
我想将所有文件打开成一个文件对象,我可以逐行阅读,但我不想在这个过程中创建一个新文件.那可能吗?
with open(file_names, 'r') as file_obj:
line = file_obj.readline()
while line:
...
Run Code Online (Sandbox Code Playgroud) 我有一个jsconfig.json,我用一个替换了tsconfig.json。替换后,VSCode 不会获取路径别名并报告导入错误:
例子:
Cannot find module '@Components/Title' or its corresponding type declarations.ts(2307)
Run Code Online (Sandbox Code Playgroud)
jsconfig.json
// https://code.visualstudio.com/docs/languages/jsconfig
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@Components/*": ["./src/components/*"],
"@Modules/*": ["./src/modules/*"],
"@Styles/*": ["./src/styles/*"],
"@Content/*": ["./src/content/*"],
"@Apps/*": ["./src/apps/*"]
}
},
//Add any build/compiled folders here to stop vscode searching those
"exclude": ["node_modules", "build"]
}
Run Code Online (Sandbox Code Playgroud)
tsconfig.json
// https://code.visualstudio.com/docs/languages/jsconfig
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@Components/*": ["./src/components/*"],
"@Modules/*": ["./src/modules/*"],
"@Styles/*": ["./src/styles/*"],
"@Content/*": ["./src/content/*"],
"@Apps/*": ["./src/apps/*"]
},
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": …Run Code Online (Sandbox Code Playgroud) 是否可以在Jupyter笔记本中使用破折号应用程序,而不是在浏览器中提供和查看?
我的目的是,使得鼠标悬停在一个图表生成用于另一曲线图所需要的输入Jupter笔记本内链接的曲线图.
python ×9
pandas ×3
plotly ×3
parsing ×2
regex ×2
doctest ×1
file ×1
memory ×1
performance ×1
plot ×1
plotly-dash ×1
tsconfig ×1
typescript ×1