Python 从字符串创建 Pandas DataFrame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22604564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:15:40  来源:igfitidea点击:

Create Pandas DataFrame from a string

pythonstringpandascsvcsv-import

提问by Emil H

In order to test some functionality I would like to create a DataFramefrom a string. Let's say my test data looks like:

为了测试某些功能,我想DataFrame从字符串创建一个。假设我的测试数据如下所示:

TESTDATA="""col1;col2;col3
1;4.4;99
2;4.5;200
3;4.7;65
4;3.2;140
"""

What is the simplest way to read that data into a Pandas DataFrame?

将数据读入 Pandas 的最简单方法是什么DataFrame

采纳答案by Emil H

A simple way to do this is to use StringIO.StringIO(python2)or io.StringIO(python3)and pass that to the pandas.read_csvfunction. E.g:

一个简单的方法是使用StringIO.StringIO(python2)io.StringIO(python3)并将其传递给pandas.read_csv函数。例如:

import sys
if sys.version_info[0] < 3: 
    from StringIO import StringIO
else:
    from io import StringIO

import pandas as pd

TESTDATA = StringIO("""col1;col2;col3
    1;4.4;99
    2;4.5;200
    3;4.7;65
    4;3.2;140
    """)

df = pd.read_csv(TESTDATA, sep=";")

回答by Acumenus

A traditional variable-width CSV is unreadable for storing data as a string variable. Especially for use inside a .pyfile, consider fixed-width pipe-separated data instead. Various IDEs and editors may have a plugin to format pipe-separated text into a neat table.

传统的可变宽度 CSV 无法将数据存储为字符串变量。特别是在.py文件内部使用时,请考虑使用固定宽度的管道分隔数据。各种 IDE 和编辑器可能有一个插件来将管道分隔的文本格式化为一个整洁的表格。

Using read_csv

使用 read_csv

Store the following in a utility module, e.g. util/pandas.py. An example is included in the function's docstring.

将以下内容存储在实用程序模块中,例如util/pandas.py。函数的文档字符串中包含一个示例。

import io
import re

import pandas as pd


def read_psv(str_input: str, **kwargs) -> pd.DataFrame:
    """Read a Pandas object from a pipe-separated table contained within a string.

    Input example:
        | int_score | ext_score | eligible |
        |           | 701       | True     |
        | 221.3     | 0         | False    |
        |           | 576       | True     |
        | 300       | 600       | True     |

    The leading and trailing pipes are optional, but if one is present,
    so must be the other.

    `kwargs` are passed to `read_csv`. They must not include `sep`.

    In PyCharm, the "Pipe Table Formatter" plugin has a "Format" feature that can 
    be used to neatly format a table.

    Ref: https://stackoverflow.com/a/46471952/
    """

    substitutions = [
        ('^ *', ''),  # Remove leading spaces
        (' *$', ''),  # Remove trailing spaces
        (r' *\| *', '|'),  # Remove spaces between columns
    ]
    if all(line.lstrip().startswith('|') and line.rstrip().endswith('|') for line in str_input.strip().split('\n')):
        substitutions.extend([
            (r'^\|', ''),  # Remove redundant leading delimiter
            (r'\|$', ''),  # Remove redundant trailing delimiter
        ])
    for pattern, replacement in substitutions:
        str_input = re.sub(pattern, replacement, str_input, flags=re.MULTILINE)
    return pd.read_csv(io.StringIO(str_input), sep='|', **kwargs)

Non-working alternatives

非工作替代品

The code below doesn't work properly because it adds an empty column on both the left and right sides.

下面的代码无法正常工作,因为它在左侧和右侧添加了一个空列。

df = pd.read_csv(io.StringIO(df_str), sep=r'\s*\|\s*', engine='python')

As for read_fwf, it doesn't actually useso many of the optional kwargs that read_csvaccepts and uses. As such, it shouldn't be used at all for pipe-separated data.

至于read_fwf,它实际上并没有使用这么多read_csv接受和使用的可选 kwargs 。因此,它根本不应该用于管道分隔的数据。

回答by user2314737

A quick and easy solution for interactive work is to copy-and-paste the text by loading the data from the clipboard.

交互式工作的一种快速简便的解决方案是通过从剪贴板加载数据来复制和粘贴文本。

Select the content of the string with your mouse:

用鼠标选择字符串的内容:

Copy data for pasting into a Pandas dataframe

复制数据以粘贴到 Pandas 数据框中

In the Python shell use read_clipboard()

在 Python shell 中使用 read_clipboard()

>>> pd.read_clipboard()
  col1;col2;col3
0       1;4.4;99
1      2;4.5;200
2       3;4.7;65
3      4;3.2;140

Use the appropriate separator:

使用适当的分隔符:

>>> pd.read_clipboard(sep=';')
   col1  col2  col3
0     1   4.4    99
1     2   4.5   200
2     3   4.7    65
3     4   3.2   140

>>> df = pd.read_clipboard(sep=';') # save to dataframe

回答by shaurya uppal

Split Method

拆分方法

data = input_string
df = pd.DataFrame([x.split(';') for x in data.split('\n')])
print(df)

回答by QtRoS

Simplestway is to save it to temp file and then read it:

最简单的方法是将其保存到临时文件,然后读取它:

import pandas as pd

CSV_FILE_NAME = 'temp_file.csv'  # Consider creating temp file, look URL below
with open(CSV_FILE_NAME, 'w') as outfile:
    outfile.write(TESTDATA)
df = pd.read_csv(CSV_FILE_NAME, sep=';')

Right way of creating temp file: How can I create a tmp file in Python?

创建临时文件的正确方法:如何在 Python 中创建 tmp 文件?