Python dataframe несколько строк

pandas.Series.str.split#

Splits the string in the Series/Index from the beginning, at the specified delimiter string.

Parameters pat str or compiled regex, optional

String or regular expression to split on. If not specified, split on whitespace.

n int, default -1 (all)

Limit number of splits in output. None , 0 and -1 will be interpreted as return all splits.

expand bool, default False

Expand the split strings into separate columns.

  • If True , return DataFrame/MultiIndex expanding dimensionality.
  • If False , return Series/Index, containing lists of strings.

Determines if the passed-in pattern is a regular expression:

  • If True , assumes the passed-in pattern is a regular expression
  • If False , treats the pattern as a literal string.
  • If None and pat length is 1, treats pat as a literal string.
  • If None and pat length is not 1, treats pat as a regular expression.
  • Cannot be set to False if pat is a compiled regex

Type matches caller unless expand=True (see Notes).

Split strings around given separator/delimiter.

Splits string around given separator/delimiter, starting from the right.

Join lists contained as elements in the Series/Index with passed delimiter.

Standard library version for split.

Standard library version for rsplit.

The handling of the n keyword depends on the number of found splits:

If using expand=True , Series and Index callers return DataFrame and MultiIndex objects, respectively.

Use of regex =False with a pat as a compiled regex will raise an error.

>>> s = pd.Series( . [ . "this is a regular sentence", . "https://docs.python.org/3/tutorial/index.html", . np.nan . ] . ) >>> s 0 this is a regular sentence 1 https://docs.python.org/3/tutorial/index.html 2 NaN dtype: object 

In the default setting, the string is split by whitespace.

>>> s.str.split() 0 [this, is, a, regular, sentence] 1 [https://docs.python.org/3/tutorial/index.html] 2 NaN dtype: object 

Without the n parameter, the outputs of rsplit and split are identical.

>>> s.str.rsplit() 0 [this, is, a, regular, sentence] 1 [https://docs.python.org/3/tutorial/index.html] 2 NaN dtype: object 

The n parameter can be used to limit the number of splits on the delimiter. The outputs of split and rsplit are different.

>>> s.str.split(n=2) 0 [this, is, a regular sentence] 1 [https://docs.python.org/3/tutorial/index.html] 2 NaN dtype: object 
>>> s.str.rsplit(n=2) 0 [this is a, regular, sentence] 1 [https://docs.python.org/3/tutorial/index.html] 2 NaN dtype: object 

The pat parameter can be used to split by other characters.

>>> s.str.split(pat="/") 0 [this is a regular sentence] 1 [https:, , docs.python.org, 3, tutorial, index. 2 NaN dtype: object 

When using expand=True , the split elements will expand out into separate columns. If NaN is present, it is propagated throughout the columns during the split.

>>> s.str.split(expand=True) 0 1 2 3 4 0 this is a regular sentence 1 https://docs.python.org/3/tutorial/index.html None None None None 2 NaN NaN NaN NaN NaN 

For slightly more complex use cases like splitting the html document name from a url, a combination of parameter settings can be used.

>>> s.str.rsplit("/", n=1, expand=True) 0 1 0 this is a regular sentence None 1 https://docs.python.org/3/tutorial index.html 2 NaN NaN 

Remember to escape special characters when explicitly using regular expressions.

>>> s = pd.Series(["foo and bar plus baz"]) >>> s.str.split(r"and|plus", expand=True) 0 1 2 0 foo bar baz 

Regular expressions can be used to handle urls or file names. When pat is a string and regex=None (the default), the given pat is compiled as a regex only if len(pat) != 1 .

>>> s = pd.Series(['foojpgbar.jpg']) >>> s.str.split(r".", expand=True) 0 1 0 foojpgbar jpg 
>>> s.str.split(r"\.jpg", expand=True) 0 1 0 foojpgbar 

When regex=True , pat is interpreted as a regex

>>> s.str.split(r"\.jpg", regex=True, expand=True) 0 1 0 foojpgbar 

A compiled regex can be passed as pat

>>> import re >>> s.str.split(re.compile(r"\.jpg"), expand=True) 0 1 0 foojpgbar 

When regex=False , pat is interpreted as the string itself

>>> s.str.split(r"\.jpg", regex=False, expand=True) 0 0 foojpgbar.jpg 

Источник

Как добавить строки в фрейм данных Pandas (с примерами)

Вы можете использовать функцию df.loc() , чтобы добавить строку в конец кадра данных pandas:

#add row to end of DataFrame df.loc[ len(df.index )] = [value1, value2, value3, . ] 

И вы можете использовать функцию df.append() для добавления нескольких строк существующего DataFrame в конец другого DataFrame:

#append rows of *df2* to end of existing DataFrame df = df.append(df2, ignore_index = True ) 

В следующих примерах показано, как использовать эти функции на практике.

Пример 1: добавить одну строку в Pandas DataFrame

Следующий код показывает, как добавить одну строку в конец кадра данных pandas:

import pandas as pd #create DataFrame df = pd.DataFrame() #view DataFrame df points rebounds assists 0 10 7 11 1 12 7 8 2 12 8 10 3 14 13 6 4 13 7 6 5 18 4 5 #add new row to end of DataFrame df.loc[ len(df.index )] = [20, 7, 5] #view updated DataFrame df points rebounds assists 0 10 7 11 1 12 7 8 2 12 8 10 3 14 13 6 4 13 7 6 5 18 4 5 6 20 7 5 

Пример 2: добавить несколько строк в Pandas DataFrame

Следующий код показывает, как добавить несколько строк существующего DataFrame в конец другого DataFrame:

import pandas as pd #create DataFrame df = pd.DataFrame() #view DataFrame df points rebounds assists 0 10 7 11 1 12 7 8 2 12 8 10 3 14 13 6 4 13 7 6 5 18 4 5 #define second DataFrame df2 = pd.DataFrame() #add new row to end of DataFrame df = df.append(df2, ignore_index = True ) #view updated DataFrame df points rebounds assists 0 10 7 11 1 12 7 8 2 12 8 10 3 14 13 6 4 13 7 6 5 18 4 5 6 21 7 11 7 25 7 3 8 26 13 3 

Обратите внимание, что два кадра данных должны иметь одинаковые имена столбцов, чтобы успешно добавлять строки одного кадра данных в конец другого.

Источник

Читайте также:  Linking Pages in HTML
Оцените статью