question_id
int64
59.8M
70.5M
question_title
stringlengths
17
135
question_body
stringlengths
274
3.35k
accepted_answer_id
int64
59.8M
70.5M
question_creation_date
unknown
question_answer_count
int64
1
4
question_favorite_count
float64
0
3
question_score
int64
-2
5
question_view_count
int64
20
1.17k
tags
stringclasses
2 values
answer_body
stringlengths
65
4.03k
answer_creation_date
unknown
answer_score
int64
0
5
link
stringlengths
49
87
context
stringlengths
1.11k
251k
answer_start
int64
0
1.25k
answer_end
int64
187
3.53k
question
stringlengths
263
3.22k
predicted_answer
stringclasses
24 values
parsed_answer
stringlengths
41
3.53k
63,701,878
Convert series from pandas DataFrame to string
<p>For my dataframe</p> <pre><code>df = pd.DataFrame({ cat: ['a','a','a','b','b','b'], step: [1,3,2, 2,1,3], Id: [101,103,102, 902,901,903] }) </code></pre> <p>I need to get ID values as string on output using STEP values as ordering clause:</p> <pre><code>cat_a: '101,102,103' cat_b: '901,902,903' </code></pre> <p>I try this with heavy construction. Is there any elegant solution instead?</p> <pre><code>dfa = df.loc[df['cat'] == 'a', ['step', 'id']] dfa.set_index('step') a1=dfa[dfa.index == 1].iloc[0][0] a2=dfa[dfa.index == 2].iloc[0][0] a3=dfa[dfa.index == 3].iloc[0][0] cat_a = '{}, {}, {}'.format(a1,a2,a3) … cat_b = '{}, {}, {}'.format(b1,b2,b3) </code></pre>
63,701,919
"2020-09-02T08:45:06.357000"
1
null
1
37
python|pandas
<p>Use <a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.sort_values.html" rel="nofollow noreferrer"><code>DataFrame.sort_values</code></a> by both columns first for expected order and then aggregate <code>join</code> with lambda method with convert to <code>string</code>s:</p> <pre><code>d = (df.sort_values(['cat','step']) .groupby('cat')['Id'] .agg(lambda x: ','.join(x.astype(str))) .to_dict()) print (d) {'a': '101,102,103', 'b': '901,902,903'} </code></pre>
"2020-09-02T08:47:22.353000"
0
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_string.html
pandas.DataFrame.to_string# pandas.DataFrame.to_string# DataFrame.to_string(buf=None, columns=None, col_space=None, header=True, index=True, na_rep='NaN', formatters=None, float_format=None, sparsify=None, index_names=True, justify=None, max_rows=None, max_cols=None, show_dimensions=False, decimal='.', line_width=None, min_rows=None, max_colwidth=None, encoding=None)[source]# Use DataFrame.sort_values by both columns first for expected order and then aggregate join with lambda method with convert to strings: d = (df.sort_values(['cat','step']) .groupby('cat')['Id'] .agg(lambda x: ','.join(x.astype(str))) .to_dict()) print (d) {'a': '101,102,103', 'b': '901,902,903'} Render a DataFrame to a console-friendly tabular output. Parameters bufstr, Path or StringIO-like, optional, default NoneBuffer to write to. If None, the output is returned as a string. columnssequence, optional, default NoneThe subset of columns to write. Writes all columns by default. col_spaceint, list or dict of int, optionalThe minimum width of each column. If a list of ints is given every integers corresponds with one column. If a dict is given, the key references the column, while the value defines the space to use.. headerbool or sequence of str, optionalWrite out the column names. If a list of strings is given, it is assumed to be aliases for the column names. indexbool, optional, default TrueWhether to print index (row) labels. na_repstr, optional, default ‘NaN’String representation of NaN to use. formatterslist, tuple or dict of one-param. functions, optionalFormatter functions to apply to columns’ elements by position or name. The result of each function must be a unicode string. List/tuple must be of length equal to the number of columns. float_formatone-parameter function, optional, default NoneFormatter function to apply to columns’ elements if they are floats. This function must return a unicode string and will be applied only to the non-NaN elements, with NaN being handled by na_rep. Changed in version 1.2.0. sparsifybool, optional, default TrueSet to False for a DataFrame with a hierarchical index to print every multiindex key at each row. index_namesbool, optional, default TruePrints the names of the indexes. justifystr, default NoneHow to justify the column labels. If None uses the option from the print configuration (controlled by set_option), ‘right’ out of the box. Valid values are left right center justify justify-all start end inherit match-parent initial unset. max_rowsint, optionalMaximum number of rows to display in the console. max_colsint, optionalMaximum number of columns to display in the console. show_dimensionsbool, default FalseDisplay DataFrame dimensions (number of rows by number of columns). decimalstr, default ‘.’Character recognized as decimal separator, e.g. ‘,’ in Europe. line_widthint, optionalWidth to wrap a line in characters. min_rowsint, optionalThe number of rows to display in the console in a truncated repr (when number of rows is above max_rows). max_colwidthint, optionalMax width to truncate each column in characters. By default, no limit. New in version 1.0.0. encodingstr, default “utf-8”Set character encoding. New in version 1.0. Returns str or NoneIf buf is None, returns the result as a string. Otherwise returns None. See also to_htmlConvert DataFrame to HTML. Examples >>> d = {'col1': [1, 2, 3], 'col2': [4, 5, 6]} >>> df = pd.DataFrame(d) >>> print(df.to_string()) col1 col2 0 1 4 1 2 5 2 3 6
383
697
Convert series from pandas DataFrame to string For my dataframe df = pd.DataFrame({ cat: ['a','a','a','b','b','b'], step: [1,3,2, 2,1,3], Id: [101,103,102, 902,901,903] }) I need to get ID values as string on output using STEP values as ordering clause: cat_a: '101,102,103' cat_b: '901,902,903' I try this with heavy construction. Is there any elegant solution instead? dfa = df.loc[df['cat'] == 'a', ['step', 'id']] dfa.set_index('step') a1=dfa[dfa.index == 1].iloc[0][0] a2=dfa[dfa.index == 2].iloc[0][0] a3=dfa[dfa.index == 3].iloc[0][0] cat_a = '{}, {}, {}'.format(a1,a2,a3) … cat_b = '{}, {}, {}'.format(b1,b2,b3)
/
Use DataFrame.sort_values by both columns first for expected order and then aggregate join with lambda method with convert to strings: d = (df.sort_values(['cat','step']) .groupby('cat')['Id'] .agg(lambda x: ','.join(x.astype(str))) .to_dict()) print (d) {'a': '101,102,103', 'b': '901,902,903'}
67,914,151
Filtering only 1 column in a df without returning the entire DF in 1 line
<p>I'm hoping that there is a way i can return a series from df while im filtering it in 1 line. Is there a way I could return a column from my df after I filter it? Currently my process is something like this</p> <pre><code>df = df[df['a'] &gt; 0 ] list = df['a'] </code></pre>
67,915,627
"2021-06-10T03:10:52.427000"
1
null
1
41
python|pandas
<p>The <code>df.loc</code> syntax is the preferred way to do this, as @JohnM wrote in his comment, though I find the syntax from @Don'tAccept more readable and scaleable however since it can handle cases like column names with spaces in them. These combine like:</p> <pre><code>df.loc[df['a'] &gt; 0, 'a'] </code></pre> <p>Note this is expandable to provide multiple columns, for example if you wanted columns 'a' and 'b' you would do:</p> <pre><code>df.loc[df['a'] &gt; 0, ['a', 'b']] </code></pre> <p>Lastly, you can verify that <code>df.a</code> and <code>df['a']</code> are the same by checking</p> <pre><code>in: df.a is df['a'] out: True </code></pre> <p>The <code>is</code> here (as opposed to <code>==</code>) means <code>df.a</code> and <code>df['a']</code> point to the same object in memory, so they are interchangeable.</p>
"2021-06-10T06:08:47.610000"
0
https://pandas.pydata.org/docs/user_guide/groupby.html
Group by: split-apply-combine# The df.loc syntax is the preferred way to do this, as @JohnM wrote in his comment, though I find the syntax from @Don'tAccept more readable and scaleable however since it can handle cases like column names with spaces in them. These combine like: df.loc[df['a'] > 0, 'a'] Note this is expandable to provide multiple columns, for example if you wanted columns 'a' and 'b' you would do: df.loc[df['a'] > 0, ['a', 'b']] Lastly, you can verify that df.a and df['a'] are the same by checking in: df.a is df['a'] out: True The is here (as opposed to ==) means df.a and df['a'] point to the same object in memory, so they are interchangeable. Group by: split-apply-combine# By “group by” we are referring to a process involving one or more of the following steps: Splitting the data into groups based on some criteria. Applying a function to each group independently. Combining the results into a data structure. Out of these, the split step is the most straightforward. In fact, in many situations we may wish to split the data set into groups and do something with those groups. In the apply step, we might wish to do one of the following: Aggregation: compute a summary statistic (or statistics) for each group. Some examples: Compute group sums or means. Compute group sizes / counts. Transformation: perform some group-specific computations and return a like-indexed object. Some examples: Standardize data (zscore) within a group. Filling NAs within groups with a value derived from each group. Filtration: discard some groups, according to a group-wise computation that evaluates True or False. Some examples: Discard data that belongs to groups with only a few members. Filter out data based on the group sum or mean. Some combination of the above: GroupBy will examine the results of the apply step and try to return a sensibly combined result if it doesn’t fit into either of the above two categories. Since the set of object instance methods on pandas data structures are generally rich and expressive, we often simply want to invoke, say, a DataFrame function on each group. The name GroupBy should be quite familiar to those who have used a SQL-based tool (or itertools), in which you can write code like: SELECT Column1, Column2, mean(Column3), sum(Column4) FROM SomeTable GROUP BY Column1, Column2 We aim to make operations like this natural and easy to express using pandas. We’ll address each area of GroupBy functionality then provide some non-trivial examples / use cases. See the cookbook for some advanced strategies. Splitting an object into groups# pandas objects can be split on any of their axes. The abstract definition of grouping is to provide a mapping of labels to group names. To create a GroupBy object (more on what the GroupBy object is later), you may do the following: In [1]: df = pd.DataFrame( ...: [ ...: ("bird", "Falconiformes", 389.0), ...: ("bird", "Psittaciformes", 24.0), ...: ("mammal", "Carnivora", 80.2), ...: ("mammal", "Primates", np.nan), ...: ("mammal", "Carnivora", 58), ...: ], ...: index=["falcon", "parrot", "lion", "monkey", "leopard"], ...: columns=("class", "order", "max_speed"), ...: ) ...: In [2]: df Out[2]: class order max_speed falcon bird Falconiformes 389.0 parrot bird Psittaciformes 24.0 lion mammal Carnivora 80.2 monkey mammal Primates NaN leopard mammal Carnivora 58.0 # default is axis=0 In [3]: grouped = df.groupby("class") In [4]: grouped = df.groupby("order", axis="columns") In [5]: grouped = df.groupby(["class", "order"]) The mapping can be specified many different ways: A Python function, to be called on each of the axis labels. A list or NumPy array of the same length as the selected axis. A dict or Series, providing a label -> group name mapping. For DataFrame objects, a string indicating either a column name or an index level name to be used to group. df.groupby('A') is just syntactic sugar for df.groupby(df['A']). A list of any of the above things. Collectively we refer to the grouping objects as the keys. For example, consider the following DataFrame: Note A string passed to groupby may refer to either a column or an index level. If a string matches both a column name and an index level name, a ValueError will be raised. In [6]: df = pd.DataFrame( ...: { ...: "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"], ...: "B": ["one", "one", "two", "three", "two", "two", "one", "three"], ...: "C": np.random.randn(8), ...: "D": np.random.randn(8), ...: } ...: ) ...: In [7]: df Out[7]: A B C D 0 foo one 0.469112 -0.861849 1 bar one -0.282863 -2.104569 2 foo two -1.509059 -0.494929 3 bar three -1.135632 1.071804 4 foo two 1.212112 0.721555 5 bar two -0.173215 -0.706771 6 foo one 0.119209 -1.039575 7 foo three -1.044236 0.271860 On a DataFrame, we obtain a GroupBy object by calling groupby(). We could naturally group by either the A or B columns, or both: In [8]: grouped = df.groupby("A") In [9]: grouped = df.groupby(["A", "B"]) If we also have a MultiIndex on columns A and B, we can group by all but the specified columns In [10]: df2 = df.set_index(["A", "B"]) In [11]: grouped = df2.groupby(level=df2.index.names.difference(["B"])) In [12]: grouped.sum() Out[12]: C D A bar -1.591710 -1.739537 foo -0.752861 -1.402938 These will split the DataFrame on its index (rows). We could also split by the columns: In [13]: def get_letter_type(letter): ....: if letter.lower() in 'aeiou': ....: return 'vowel' ....: else: ....: return 'consonant' ....: In [14]: grouped = df.groupby(get_letter_type, axis=1) pandas Index objects support duplicate values. If a non-unique index is used as the group key in a groupby operation, all values for the same index value will be considered to be in one group and thus the output of aggregation functions will only contain unique index values: In [15]: lst = [1, 2, 3, 1, 2, 3] In [16]: s = pd.Series([1, 2, 3, 10, 20, 30], lst) In [17]: grouped = s.groupby(level=0) In [18]: grouped.first() Out[18]: 1 1 2 2 3 3 dtype: int64 In [19]: grouped.last() Out[19]: 1 10 2 20 3 30 dtype: int64 In [20]: grouped.sum() Out[20]: 1 11 2 22 3 33 dtype: int64 Note that no splitting occurs until it’s needed. Creating the GroupBy object only verifies that you’ve passed a valid mapping. Note Many kinds of complicated data manipulations can be expressed in terms of GroupBy operations (though can’t be guaranteed to be the most efficient). You can get quite creative with the label mapping functions. GroupBy sorting# By default the group keys are sorted during the groupby operation. You may however pass sort=False for potential speedups: In [21]: df2 = pd.DataFrame({"X": ["B", "B", "A", "A"], "Y": [1, 2, 3, 4]}) In [22]: df2.groupby(["X"]).sum() Out[22]: Y X A 7 B 3 In [23]: df2.groupby(["X"], sort=False).sum() Out[23]: Y X B 3 A 7 Note that groupby will preserve the order in which observations are sorted within each group. For example, the groups created by groupby() below are in the order they appeared in the original DataFrame: In [24]: df3 = pd.DataFrame({"X": ["A", "B", "A", "B"], "Y": [1, 4, 3, 2]}) In [25]: df3.groupby(["X"]).get_group("A") Out[25]: X Y 0 A 1 2 A 3 In [26]: df3.groupby(["X"]).get_group("B") Out[26]: X Y 1 B 4 3 B 2 New in version 1.1.0. GroupBy dropna# By default NA values are excluded from group keys during the groupby operation. However, in case you want to include NA values in group keys, you could pass dropna=False to achieve it. In [27]: df_list = [[1, 2, 3], [1, None, 4], [2, 1, 3], [1, 2, 2]] In [28]: df_dropna = pd.DataFrame(df_list, columns=["a", "b", "c"]) In [29]: df_dropna Out[29]: a b c 0 1 2.0 3 1 1 NaN 4 2 2 1.0 3 3 1 2.0 2 # Default ``dropna`` is set to True, which will exclude NaNs in keys In [30]: df_dropna.groupby(by=["b"], dropna=True).sum() Out[30]: a c b 1.0 2 3 2.0 2 5 # In order to allow NaN in keys, set ``dropna`` to False In [31]: df_dropna.groupby(by=["b"], dropna=False).sum() Out[31]: a c b 1.0 2 3 2.0 2 5 NaN 1 4 The default setting of dropna argument is True which means NA are not included in group keys. GroupBy object attributes# The groups attribute is a dict whose keys are the computed unique groups and corresponding values being the axis labels belonging to each group. In the above example we have: In [32]: df.groupby("A").groups Out[32]: {'bar': [1, 3, 5], 'foo': [0, 2, 4, 6, 7]} In [33]: df.groupby(get_letter_type, axis=1).groups Out[33]: {'consonant': ['B', 'C', 'D'], 'vowel': ['A']} Calling the standard Python len function on the GroupBy object just returns the length of the groups dict, so it is largely just a convenience: In [34]: grouped = df.groupby(["A", "B"]) In [35]: grouped.groups Out[35]: {('bar', 'one'): [1], ('bar', 'three'): [3], ('bar', 'two'): [5], ('foo', 'one'): [0, 6], ('foo', 'three'): [7], ('foo', 'two'): [2, 4]} In [36]: len(grouped) Out[36]: 6 GroupBy will tab complete column names (and other attributes): In [37]: df Out[37]: height weight gender 2000-01-01 42.849980 157.500553 male 2000-01-02 49.607315 177.340407 male 2000-01-03 56.293531 171.524640 male 2000-01-04 48.421077 144.251986 female 2000-01-05 46.556882 152.526206 male 2000-01-06 68.448851 168.272968 female 2000-01-07 70.757698 136.431469 male 2000-01-08 58.909500 176.499753 female 2000-01-09 76.435631 174.094104 female 2000-01-10 45.306120 177.540920 male In [38]: gb = df.groupby("gender") In [39]: gb.<TAB> # noqa: E225, E999 gb.agg gb.boxplot gb.cummin gb.describe gb.filter gb.get_group gb.height gb.last gb.median gb.ngroups gb.plot gb.rank gb.std gb.transform gb.aggregate gb.count gb.cumprod gb.dtype gb.first gb.groups gb.hist gb.max gb.min gb.nth gb.prod gb.resample gb.sum gb.var gb.apply gb.cummax gb.cumsum gb.fillna gb.gender gb.head gb.indices gb.mean gb.name gb.ohlc gb.quantile gb.size gb.tail gb.weight GroupBy with MultiIndex# With hierarchically-indexed data, it’s quite natural to group by one of the levels of the hierarchy. Let’s create a Series with a two-level MultiIndex. In [40]: arrays = [ ....: ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"], ....: ["one", "two", "one", "two", "one", "two", "one", "two"], ....: ] ....: In [41]: index = pd.MultiIndex.from_arrays(arrays, names=["first", "second"]) In [42]: s = pd.Series(np.random.randn(8), index=index) In [43]: s Out[43]: first second bar one -0.919854 two -0.042379 baz one 1.247642 two -0.009920 foo one 0.290213 two 0.495767 qux one 0.362949 two 1.548106 dtype: float64 We can then group by one of the levels in s. In [44]: grouped = s.groupby(level=0) In [45]: grouped.sum() Out[45]: first bar -0.962232 baz 1.237723 foo 0.785980 qux 1.911055 dtype: float64 If the MultiIndex has names specified, these can be passed instead of the level number: In [46]: s.groupby(level="second").sum() Out[46]: second one 0.980950 two 1.991575 dtype: float64 Grouping with multiple levels is supported. In [47]: s Out[47]: first second third bar doo one -1.131345 two -0.089329 baz bee one 0.337863 two -0.945867 foo bop one -0.932132 two 1.956030 qux bop one 0.017587 two -0.016692 dtype: float64 In [48]: s.groupby(level=["first", "second"]).sum() Out[48]: first second bar doo -1.220674 baz bee -0.608004 foo bop 1.023898 qux bop 0.000895 dtype: float64 Index level names may be supplied as keys. In [49]: s.groupby(["first", "second"]).sum() Out[49]: first second bar doo -1.220674 baz bee -0.608004 foo bop 1.023898 qux bop 0.000895 dtype: float64 More on the sum function and aggregation later. Grouping DataFrame with Index levels and columns# A DataFrame may be grouped by a combination of columns and index levels by specifying the column names as strings and the index levels as pd.Grouper objects. In [50]: arrays = [ ....: ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"], ....: ["one", "two", "one", "two", "one", "two", "one", "two"], ....: ] ....: In [51]: index = pd.MultiIndex.from_arrays(arrays, names=["first", "second"]) In [52]: df = pd.DataFrame({"A": [1, 1, 1, 1, 2, 2, 3, 3], "B": np.arange(8)}, index=index) In [53]: df Out[53]: A B first second bar one 1 0 two 1 1 baz one 1 2 two 1 3 foo one 2 4 two 2 5 qux one 3 6 two 3 7 The following example groups df by the second index level and the A column. In [54]: df.groupby([pd.Grouper(level=1), "A"]).sum() Out[54]: B second A one 1 2 2 4 3 6 two 1 4 2 5 3 7 Index levels may also be specified by name. In [55]: df.groupby([pd.Grouper(level="second"), "A"]).sum() Out[55]: B second A one 1 2 2 4 3 6 two 1 4 2 5 3 7 Index level names may be specified as keys directly to groupby. In [56]: df.groupby(["second", "A"]).sum() Out[56]: B second A one 1 2 2 4 3 6 two 1 4 2 5 3 7 DataFrame column selection in GroupBy# Once you have created the GroupBy object from a DataFrame, you might want to do something different for each of the columns. Thus, using [] similar to getting a column from a DataFrame, you can do: In [57]: df = pd.DataFrame( ....: { ....: "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"], ....: "B": ["one", "one", "two", "three", "two", "two", "one", "three"], ....: "C": np.random.randn(8), ....: "D": np.random.randn(8), ....: } ....: ) ....: In [58]: df Out[58]: A B C D 0 foo one -0.575247 1.346061 1 bar one 0.254161 1.511763 2 foo two -1.143704 1.627081 3 bar three 0.215897 -0.990582 4 foo two 1.193555 -0.441652 5 bar two -0.077118 1.211526 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 In [59]: grouped = df.groupby(["A"]) In [60]: grouped_C = grouped["C"] In [61]: grouped_D = grouped["D"] This is mainly syntactic sugar for the alternative and much more verbose: In [62]: df["C"].groupby(df["A"]) Out[62]: <pandas.core.groupby.generic.SeriesGroupBy object at 0x7f1ea100a490> Additionally this method avoids recomputing the internal grouping information derived from the passed key. Iterating through groups# With the GroupBy object in hand, iterating through the grouped data is very natural and functions similarly to itertools.groupby(): In [63]: grouped = df.groupby('A') In [64]: for name, group in grouped: ....: print(name) ....: print(group) ....: bar A B C D 1 bar one 0.254161 1.511763 3 bar three 0.215897 -0.990582 5 bar two -0.077118 1.211526 foo A B C D 0 foo one -0.575247 1.346061 2 foo two -1.143704 1.627081 4 foo two 1.193555 -0.441652 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 In the case of grouping by multiple keys, the group name will be a tuple: In [65]: for name, group in df.groupby(['A', 'B']): ....: print(name) ....: print(group) ....: ('bar', 'one') A B C D 1 bar one 0.254161 1.511763 ('bar', 'three') A B C D 3 bar three 0.215897 -0.990582 ('bar', 'two') A B C D 5 bar two -0.077118 1.211526 ('foo', 'one') A B C D 0 foo one -0.575247 1.346061 6 foo one -0.408530 0.268520 ('foo', 'three') A B C D 7 foo three -0.862495 0.02458 ('foo', 'two') A B C D 2 foo two -1.143704 1.627081 4 foo two 1.193555 -0.441652 See Iterating through groups. Selecting a group# A single group can be selected using get_group(): In [66]: grouped.get_group("bar") Out[66]: A B C D 1 bar one 0.254161 1.511763 3 bar three 0.215897 -0.990582 5 bar two -0.077118 1.211526 Or for an object grouped on multiple columns: In [67]: df.groupby(["A", "B"]).get_group(("bar", "one")) Out[67]: A B C D 1 bar one 0.254161 1.511763 Aggregation# Once the GroupBy object has been created, several methods are available to perform a computation on the grouped data. These operations are similar to the aggregating API, window API, and resample API. An obvious one is aggregation via the aggregate() or equivalently agg() method: In [68]: grouped = df.groupby("A") In [69]: grouped[["C", "D"]].aggregate(np.sum) Out[69]: C D A bar 0.392940 1.732707 foo -1.796421 2.824590 In [70]: grouped = df.groupby(["A", "B"]) In [71]: grouped.aggregate(np.sum) Out[71]: C D A B bar one 0.254161 1.511763 three 0.215897 -0.990582 two -0.077118 1.211526 foo one -0.983776 1.614581 three -0.862495 0.024580 two 0.049851 1.185429 As you can see, the result of the aggregation will have the group names as the new index along the grouped axis. In the case of multiple keys, the result is a MultiIndex by default, though this can be changed by using the as_index option: In [72]: grouped = df.groupby(["A", "B"], as_index=False) In [73]: grouped.aggregate(np.sum) Out[73]: A B C D 0 bar one 0.254161 1.511763 1 bar three 0.215897 -0.990582 2 bar two -0.077118 1.211526 3 foo one -0.983776 1.614581 4 foo three -0.862495 0.024580 5 foo two 0.049851 1.185429 In [74]: df.groupby("A", as_index=False)[["C", "D"]].sum() Out[74]: A C D 0 bar 0.392940 1.732707 1 foo -1.796421 2.824590 Note that you could use the reset_index DataFrame function to achieve the same result as the column names are stored in the resulting MultiIndex: In [75]: df.groupby(["A", "B"]).sum().reset_index() Out[75]: A B C D 0 bar one 0.254161 1.511763 1 bar three 0.215897 -0.990582 2 bar two -0.077118 1.211526 3 foo one -0.983776 1.614581 4 foo three -0.862495 0.024580 5 foo two 0.049851 1.185429 Another simple aggregation example is to compute the size of each group. This is included in GroupBy as the size method. It returns a Series whose index are the group names and whose values are the sizes of each group. In [76]: grouped.size() Out[76]: A B size 0 bar one 1 1 bar three 1 2 bar two 1 3 foo one 2 4 foo three 1 5 foo two 2 In [77]: grouped.describe() Out[77]: C ... D count mean std min ... 25% 50% 75% max 0 1.0 0.254161 NaN 0.254161 ... 1.511763 1.511763 1.511763 1.511763 1 1.0 0.215897 NaN 0.215897 ... -0.990582 -0.990582 -0.990582 -0.990582 2 1.0 -0.077118 NaN -0.077118 ... 1.211526 1.211526 1.211526 1.211526 3 2.0 -0.491888 0.117887 -0.575247 ... 0.537905 0.807291 1.076676 1.346061 4 1.0 -0.862495 NaN -0.862495 ... 0.024580 0.024580 0.024580 0.024580 5 2.0 0.024925 1.652692 -1.143704 ... 0.075531 0.592714 1.109898 1.627081 [6 rows x 16 columns] Another aggregation example is to compute the number of unique values of each group. This is similar to the value_counts function, except that it only counts unique values. In [78]: ll = [['foo', 1], ['foo', 2], ['foo', 2], ['bar', 1], ['bar', 1]] In [79]: df4 = pd.DataFrame(ll, columns=["A", "B"]) In [80]: df4 Out[80]: A B 0 foo 1 1 foo 2 2 foo 2 3 bar 1 4 bar 1 In [81]: df4.groupby("A")["B"].nunique() Out[81]: A bar 1 foo 2 Name: B, dtype: int64 Note Aggregation functions will not return the groups that you are aggregating over if they are named columns, when as_index=True, the default. The grouped columns will be the indices of the returned object. Passing as_index=False will return the groups that you are aggregating over, if they are named columns. Aggregating functions are the ones that reduce the dimension of the returned objects. Some common aggregating functions are tabulated below: Function Description mean() Compute mean of groups sum() Compute sum of group values size() Compute group sizes count() Compute count of group std() Standard deviation of groups var() Compute variance of groups sem() Standard error of the mean of groups describe() Generates descriptive statistics first() Compute first of group values last() Compute last of group values nth() Take nth value, or a subset if n is a list min() Compute min of group values max() Compute max of group values The aggregating functions above will exclude NA values. Any function which reduces a Series to a scalar value is an aggregation function and will work, a trivial example is df.groupby('A').agg(lambda ser: 1). Note that nth() can act as a reducer or a filter, see here. Applying multiple functions at once# With grouped Series you can also pass a list or dict of functions to do aggregation with, outputting a DataFrame: In [82]: grouped = df.groupby("A") In [83]: grouped["C"].agg([np.sum, np.mean, np.std]) Out[83]: sum mean std A bar 0.392940 0.130980 0.181231 foo -1.796421 -0.359284 0.912265 On a grouped DataFrame, you can pass a list of functions to apply to each column, which produces an aggregated result with a hierarchical index: In [84]: grouped[["C", "D"]].agg([np.sum, np.mean, np.std]) Out[84]: C D sum mean std sum mean std A bar 0.392940 0.130980 0.181231 1.732707 0.577569 1.366330 foo -1.796421 -0.359284 0.912265 2.824590 0.564918 0.884785 The resulting aggregations are named for the functions themselves. If you need to rename, then you can add in a chained operation for a Series like this: In [85]: ( ....: grouped["C"] ....: .agg([np.sum, np.mean, np.std]) ....: .rename(columns={"sum": "foo", "mean": "bar", "std": "baz"}) ....: ) ....: Out[85]: foo bar baz A bar 0.392940 0.130980 0.181231 foo -1.796421 -0.359284 0.912265 For a grouped DataFrame, you can rename in a similar manner: In [86]: ( ....: grouped[["C", "D"]].agg([np.sum, np.mean, np.std]).rename( ....: columns={"sum": "foo", "mean": "bar", "std": "baz"} ....: ) ....: ) ....: Out[86]: C D foo bar baz foo bar baz A bar 0.392940 0.130980 0.181231 1.732707 0.577569 1.366330 foo -1.796421 -0.359284 0.912265 2.824590 0.564918 0.884785 Note In general, the output column names should be unique. You can’t apply the same function (or two functions with the same name) to the same column. In [87]: grouped["C"].agg(["sum", "sum"]) Out[87]: sum sum A bar 0.392940 0.392940 foo -1.796421 -1.796421 pandas does allow you to provide multiple lambdas. In this case, pandas will mangle the name of the (nameless) lambda functions, appending _<i> to each subsequent lambda. In [88]: grouped["C"].agg([lambda x: x.max() - x.min(), lambda x: x.median() - x.mean()]) Out[88]: <lambda_0> <lambda_1> A bar 0.331279 0.084917 foo 2.337259 -0.215962 Named aggregation# New in version 0.25.0. To support column-specific aggregation with control over the output column names, pandas accepts the special syntax in GroupBy.agg(), known as “named aggregation”, where The keywords are the output column names The values are tuples whose first element is the column to select and the second element is the aggregation to apply to that column. pandas provides the pandas.NamedAgg namedtuple with the fields ['column', 'aggfunc'] to make it clearer what the arguments are. As usual, the aggregation can be a callable or a string alias. In [89]: animals = pd.DataFrame( ....: { ....: "kind": ["cat", "dog", "cat", "dog"], ....: "height": [9.1, 6.0, 9.5, 34.0], ....: "weight": [7.9, 7.5, 9.9, 198.0], ....: } ....: ) ....: In [90]: animals Out[90]: kind height weight 0 cat 9.1 7.9 1 dog 6.0 7.5 2 cat 9.5 9.9 3 dog 34.0 198.0 In [91]: animals.groupby("kind").agg( ....: min_height=pd.NamedAgg(column="height", aggfunc="min"), ....: max_height=pd.NamedAgg(column="height", aggfunc="max"), ....: average_weight=pd.NamedAgg(column="weight", aggfunc=np.mean), ....: ) ....: Out[91]: min_height max_height average_weight kind cat 9.1 9.5 8.90 dog 6.0 34.0 102.75 pandas.NamedAgg is just a namedtuple. Plain tuples are allowed as well. In [92]: animals.groupby("kind").agg( ....: min_height=("height", "min"), ....: max_height=("height", "max"), ....: average_weight=("weight", np.mean), ....: ) ....: Out[92]: min_height max_height average_weight kind cat 9.1 9.5 8.90 dog 6.0 34.0 102.75 If your desired output column names are not valid Python keywords, construct a dictionary and unpack the keyword arguments In [93]: animals.groupby("kind").agg( ....: **{ ....: "total weight": pd.NamedAgg(column="weight", aggfunc=sum) ....: } ....: ) ....: Out[93]: total weight kind cat 17.8 dog 205.5 Additional keyword arguments are not passed through to the aggregation functions. Only pairs of (column, aggfunc) should be passed as **kwargs. If your aggregation functions requires additional arguments, partially apply them with functools.partial(). Note For Python 3.5 and earlier, the order of **kwargs in a functions was not preserved. This means that the output column ordering would not be consistent. To ensure consistent ordering, the keys (and so output columns) will always be sorted for Python 3.5. Named aggregation is also valid for Series groupby aggregations. In this case there’s no column selection, so the values are just the functions. In [94]: animals.groupby("kind").height.agg( ....: min_height="min", ....: max_height="max", ....: ) ....: Out[94]: min_height max_height kind cat 9.1 9.5 dog 6.0 34.0 Applying different functions to DataFrame columns# By passing a dict to aggregate you can apply a different aggregation to the columns of a DataFrame: In [95]: grouped.agg({"C": np.sum, "D": lambda x: np.std(x, ddof=1)}) Out[95]: C D A bar 0.392940 1.366330 foo -1.796421 0.884785 The function names can also be strings. In order for a string to be valid it must be either implemented on GroupBy or available via dispatching: In [96]: grouped.agg({"C": "sum", "D": "std"}) Out[96]: C D A bar 0.392940 1.366330 foo -1.796421 0.884785 Cython-optimized aggregation functions# Some common aggregations, currently only sum, mean, std, and sem, have optimized Cython implementations: In [97]: df.groupby("A")[["C", "D"]].sum() Out[97]: C D A bar 0.392940 1.732707 foo -1.796421 2.824590 In [98]: df.groupby(["A", "B"]).mean() Out[98]: C D A B bar one 0.254161 1.511763 three 0.215897 -0.990582 two -0.077118 1.211526 foo one -0.491888 0.807291 three -0.862495 0.024580 two 0.024925 0.592714 Of course sum and mean are implemented on pandas objects, so the above code would work even without the special versions via dispatching (see below). Aggregations with User-Defined Functions# Users can also provide their own functions for custom aggregations. When aggregating with a User-Defined Function (UDF), the UDF should not mutate the provided Series, see Mutating with User Defined Function (UDF) methods for more information. In [99]: animals.groupby("kind")[["height"]].agg(lambda x: set(x)) Out[99]: height kind cat {9.1, 9.5} dog {34.0, 6.0} The resulting dtype will reflect that of the aggregating function. If the results from different groups have different dtypes, then a common dtype will be determined in the same way as DataFrame construction. In [100]: animals.groupby("kind")[["height"]].agg(lambda x: x.astype(int).sum()) Out[100]: height kind cat 18 dog 40 Transformation# The transform method returns an object that is indexed the same as the one being grouped. The transform function must: Return a result that is either the same size as the group chunk or broadcastable to the size of the group chunk (e.g., a scalar, grouped.transform(lambda x: x.iloc[-1])). Operate column-by-column on the group chunk. The transform is applied to the first group chunk using chunk.apply. Not perform in-place operations on the group chunk. Group chunks should be treated as immutable, and changes to a group chunk may produce unexpected results. For example, when using fillna, inplace must be False (grouped.transform(lambda x: x.fillna(inplace=False))). (Optionally) operates on the entire group chunk. If this is supported, a fast path is used starting from the second chunk. Deprecated since version 1.5.0: When using .transform on a grouped DataFrame and the transformation function returns a DataFrame, currently pandas does not align the result’s index with the input’s index. This behavior is deprecated and alignment will be performed in a future version of pandas. You can apply .to_numpy() to the result of the transformation function to avoid alignment. Similar to Aggregations with User-Defined Functions, the resulting dtype will reflect that of the transformation function. If the results from different groups have different dtypes, then a common dtype will be determined in the same way as DataFrame construction. Suppose we wished to standardize the data within each group: In [101]: index = pd.date_range("10/1/1999", periods=1100) In [102]: ts = pd.Series(np.random.normal(0.5, 2, 1100), index) In [103]: ts = ts.rolling(window=100, min_periods=100).mean().dropna() In [104]: ts.head() Out[104]: 2000-01-08 0.779333 2000-01-09 0.778852 2000-01-10 0.786476 2000-01-11 0.782797 2000-01-12 0.798110 Freq: D, dtype: float64 In [105]: ts.tail() Out[105]: 2002-09-30 0.660294 2002-10-01 0.631095 2002-10-02 0.673601 2002-10-03 0.709213 2002-10-04 0.719369 Freq: D, dtype: float64 In [106]: transformed = ts.groupby(lambda x: x.year).transform( .....: lambda x: (x - x.mean()) / x.std() .....: ) .....: We would expect the result to now have mean 0 and standard deviation 1 within each group, which we can easily check: # Original Data In [107]: grouped = ts.groupby(lambda x: x.year) In [108]: grouped.mean() Out[108]: 2000 0.442441 2001 0.526246 2002 0.459365 dtype: float64 In [109]: grouped.std() Out[109]: 2000 0.131752 2001 0.210945 2002 0.128753 dtype: float64 # Transformed Data In [110]: grouped_trans = transformed.groupby(lambda x: x.year) In [111]: grouped_trans.mean() Out[111]: 2000 -4.870756e-16 2001 -1.545187e-16 2002 4.136282e-16 dtype: float64 In [112]: grouped_trans.std() Out[112]: 2000 1.0 2001 1.0 2002 1.0 dtype: float64 We can also visually compare the original and transformed data sets. In [113]: compare = pd.DataFrame({"Original": ts, "Transformed": transformed}) In [114]: compare.plot() Out[114]: <AxesSubplot: > Transformation functions that have lower dimension outputs are broadcast to match the shape of the input array. In [115]: ts.groupby(lambda x: x.year).transform(lambda x: x.max() - x.min()) Out[115]: 2000-01-08 0.623893 2000-01-09 0.623893 2000-01-10 0.623893 2000-01-11 0.623893 2000-01-12 0.623893 ... 2002-09-30 0.558275 2002-10-01 0.558275 2002-10-02 0.558275 2002-10-03 0.558275 2002-10-04 0.558275 Freq: D, Length: 1001, dtype: float64 Alternatively, the built-in methods could be used to produce the same outputs. In [116]: max_ts = ts.groupby(lambda x: x.year).transform("max") In [117]: min_ts = ts.groupby(lambda x: x.year).transform("min") In [118]: max_ts - min_ts Out[118]: 2000-01-08 0.623893 2000-01-09 0.623893 2000-01-10 0.623893 2000-01-11 0.623893 2000-01-12 0.623893 ... 2002-09-30 0.558275 2002-10-01 0.558275 2002-10-02 0.558275 2002-10-03 0.558275 2002-10-04 0.558275 Freq: D, Length: 1001, dtype: float64 Another common data transform is to replace missing data with the group mean. In [119]: data_df Out[119]: A B C 0 1.539708 -1.166480 0.533026 1 1.302092 -0.505754 NaN 2 -0.371983 1.104803 -0.651520 3 -1.309622 1.118697 -1.161657 4 -1.924296 0.396437 0.812436 .. ... ... ... 995 -0.093110 0.683847 -0.774753 996 -0.185043 1.438572 NaN 997 -0.394469 -0.642343 0.011374 998 -1.174126 1.857148 NaN 999 0.234564 0.517098 0.393534 [1000 rows x 3 columns] In [120]: countries = np.array(["US", "UK", "GR", "JP"]) In [121]: key = countries[np.random.randint(0, 4, 1000)] In [122]: grouped = data_df.groupby(key) # Non-NA count in each group In [123]: grouped.count() Out[123]: A B C GR 209 217 189 JP 240 255 217 UK 216 231 193 US 239 250 217 In [124]: transformed = grouped.transform(lambda x: x.fillna(x.mean())) We can verify that the group means have not changed in the transformed data and that the transformed data contains no NAs. In [125]: grouped_trans = transformed.groupby(key) In [126]: grouped.mean() # original group means Out[126]: A B C GR -0.098371 -0.015420 0.068053 JP 0.069025 0.023100 -0.077324 UK 0.034069 -0.052580 -0.116525 US 0.058664 -0.020399 0.028603 In [127]: grouped_trans.mean() # transformation did not change group means Out[127]: A B C GR -0.098371 -0.015420 0.068053 JP 0.069025 0.023100 -0.077324 UK 0.034069 -0.052580 -0.116525 US 0.058664 -0.020399 0.028603 In [128]: grouped.count() # original has some missing data points Out[128]: A B C GR 209 217 189 JP 240 255 217 UK 216 231 193 US 239 250 217 In [129]: grouped_trans.count() # counts after transformation Out[129]: A B C GR 228 228 228 JP 267 267 267 UK 247 247 247 US 258 258 258 In [130]: grouped_trans.size() # Verify non-NA count equals group size Out[130]: GR 228 JP 267 UK 247 US 258 dtype: int64 Note Some functions will automatically transform the input when applied to a GroupBy object, but returning an object of the same shape as the original. Passing as_index=False will not affect these transformation methods. For example: fillna, ffill, bfill, shift.. In [131]: grouped.ffill() Out[131]: A B C 0 1.539708 -1.166480 0.533026 1 1.302092 -0.505754 0.533026 2 -0.371983 1.104803 -0.651520 3 -1.309622 1.118697 -1.161657 4 -1.924296 0.396437 0.812436 .. ... ... ... 995 -0.093110 0.683847 -0.774753 996 -0.185043 1.438572 -0.774753 997 -0.394469 -0.642343 0.011374 998 -1.174126 1.857148 -0.774753 999 0.234564 0.517098 0.393534 [1000 rows x 3 columns] Window and resample operations# It is possible to use resample(), expanding() and rolling() as methods on groupbys. The example below will apply the rolling() method on the samples of the column B based on the groups of column A. In [132]: df_re = pd.DataFrame({"A": [1] * 10 + [5] * 10, "B": np.arange(20)}) In [133]: df_re Out[133]: A B 0 1 0 1 1 1 2 1 2 3 1 3 4 1 4 .. .. .. 15 5 15 16 5 16 17 5 17 18 5 18 19 5 19 [20 rows x 2 columns] In [134]: df_re.groupby("A").rolling(4).B.mean() Out[134]: A 1 0 NaN 1 NaN 2 NaN 3 1.5 4 2.5 ... 5 15 13.5 16 14.5 17 15.5 18 16.5 19 17.5 Name: B, Length: 20, dtype: float64 The expanding() method will accumulate a given operation (sum() in the example) for all the members of each particular group. In [135]: df_re.groupby("A").expanding().sum() Out[135]: B A 1 0 0.0 1 1.0 2 3.0 3 6.0 4 10.0 ... ... 5 15 75.0 16 91.0 17 108.0 18 126.0 19 145.0 [20 rows x 1 columns] Suppose you want to use the resample() method to get a daily frequency in each group of your dataframe and wish to complete the missing values with the ffill() method. In [136]: df_re = pd.DataFrame( .....: { .....: "date": pd.date_range(start="2016-01-01", periods=4, freq="W"), .....: "group": [1, 1, 2, 2], .....: "val": [5, 6, 7, 8], .....: } .....: ).set_index("date") .....: In [137]: df_re Out[137]: group val date 2016-01-03 1 5 2016-01-10 1 6 2016-01-17 2 7 2016-01-24 2 8 In [138]: df_re.groupby("group").resample("1D").ffill() Out[138]: group val group date 1 2016-01-03 1 5 2016-01-04 1 5 2016-01-05 1 5 2016-01-06 1 5 2016-01-07 1 5 ... ... ... 2 2016-01-20 2 7 2016-01-21 2 7 2016-01-22 2 7 2016-01-23 2 7 2016-01-24 2 8 [16 rows x 2 columns] Filtration# The filter method returns a subset of the original object. Suppose we want to take only elements that belong to groups with a group sum greater than 2. In [139]: sf = pd.Series([1, 1, 2, 3, 3, 3]) In [140]: sf.groupby(sf).filter(lambda x: x.sum() > 2) Out[140]: 3 3 4 3 5 3 dtype: int64 The argument of filter must be a function that, applied to the group as a whole, returns True or False. Another useful operation is filtering out elements that belong to groups with only a couple members. In [141]: dff = pd.DataFrame({"A": np.arange(8), "B": list("aabbbbcc")}) In [142]: dff.groupby("B").filter(lambda x: len(x) > 2) Out[142]: A B 2 2 b 3 3 b 4 4 b 5 5 b Alternatively, instead of dropping the offending groups, we can return a like-indexed objects where the groups that do not pass the filter are filled with NaNs. In [143]: dff.groupby("B").filter(lambda x: len(x) > 2, dropna=False) Out[143]: A B 0 NaN NaN 1 NaN NaN 2 2.0 b 3 3.0 b 4 4.0 b 5 5.0 b 6 NaN NaN 7 NaN NaN For DataFrames with multiple columns, filters should explicitly specify a column as the filter criterion. In [144]: dff["C"] = np.arange(8) In [145]: dff.groupby("B").filter(lambda x: len(x["C"]) > 2) Out[145]: A B C 2 2 b 2 3 3 b 3 4 4 b 4 5 5 b 5 Note Some functions when applied to a groupby object will act as a filter on the input, returning a reduced shape of the original (and potentially eliminating groups), but with the index unchanged. Passing as_index=False will not affect these transformation methods. For example: head, tail. In [146]: dff.groupby("B").head(2) Out[146]: A B C 0 0 a 0 1 1 a 1 2 2 b 2 3 3 b 3 6 6 c 6 7 7 c 7 Dispatching to instance methods# When doing an aggregation or transformation, you might just want to call an instance method on each data group. This is pretty easy to do by passing lambda functions: In [147]: grouped = df.groupby("A") In [148]: grouped.agg(lambda x: x.std()) Out[148]: C D A bar 0.181231 1.366330 foo 0.912265 0.884785 But, it’s rather verbose and can be untidy if you need to pass additional arguments. Using a bit of metaprogramming cleverness, GroupBy now has the ability to “dispatch” method calls to the groups: In [149]: grouped.std() Out[149]: C D A bar 0.181231 1.366330 foo 0.912265 0.884785 What is actually happening here is that a function wrapper is being generated. When invoked, it takes any passed arguments and invokes the function with any arguments on each group (in the above example, the std function). The results are then combined together much in the style of agg and transform (it actually uses apply to infer the gluing, documented next). This enables some operations to be carried out rather succinctly: In [150]: tsdf = pd.DataFrame( .....: np.random.randn(1000, 3), .....: index=pd.date_range("1/1/2000", periods=1000), .....: columns=["A", "B", "C"], .....: ) .....: In [151]: tsdf.iloc[::2] = np.nan In [152]: grouped = tsdf.groupby(lambda x: x.year) In [153]: grouped.fillna(method="pad") Out[153]: A B C 2000-01-01 NaN NaN NaN 2000-01-02 -0.353501 -0.080957 -0.876864 2000-01-03 -0.353501 -0.080957 -0.876864 2000-01-04 0.050976 0.044273 -0.559849 2000-01-05 0.050976 0.044273 -0.559849 ... ... ... ... 2002-09-22 0.005011 0.053897 -1.026922 2002-09-23 0.005011 0.053897 -1.026922 2002-09-24 -0.456542 -1.849051 1.559856 2002-09-25 -0.456542 -1.849051 1.559856 2002-09-26 1.123162 0.354660 1.128135 [1000 rows x 3 columns] In this example, we chopped the collection of time series into yearly chunks then independently called fillna on the groups. The nlargest and nsmallest methods work on Series style groupbys: In [154]: s = pd.Series([9, 8, 7, 5, 19, 1, 4.2, 3.3]) In [155]: g = pd.Series(list("abababab")) In [156]: gb = s.groupby(g) In [157]: gb.nlargest(3) Out[157]: a 4 19.0 0 9.0 2 7.0 b 1 8.0 3 5.0 7 3.3 dtype: float64 In [158]: gb.nsmallest(3) Out[158]: a 6 4.2 2 7.0 0 9.0 b 5 1.0 7 3.3 3 5.0 dtype: float64 Flexible apply# Some operations on the grouped data might not fit into either the aggregate or transform categories. Or, you may simply want GroupBy to infer how to combine the results. For these, use the apply function, which can be substituted for both aggregate and transform in many standard use cases. However, apply can handle some exceptional use cases. Note apply can act as a reducer, transformer, or filter function, depending on exactly what is passed to it. It can depend on the passed function and exactly what you are grouping. Thus the grouped column(s) may be included in the output as well as set the indices. In [159]: df Out[159]: A B C D 0 foo one -0.575247 1.346061 1 bar one 0.254161 1.511763 2 foo two -1.143704 1.627081 3 bar three 0.215897 -0.990582 4 foo two 1.193555 -0.441652 5 bar two -0.077118 1.211526 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 In [160]: grouped = df.groupby("A") # could also just call .describe() In [161]: grouped["C"].apply(lambda x: x.describe()) Out[161]: A bar count 3.000000 mean 0.130980 std 0.181231 min -0.077118 25% 0.069390 ... foo min -1.143704 25% -0.862495 50% -0.575247 75% -0.408530 max 1.193555 Name: C, Length: 16, dtype: float64 The dimension of the returned result can also change: In [162]: grouped = df.groupby('A')['C'] In [163]: def f(group): .....: return pd.DataFrame({'original': group, .....: 'demeaned': group - group.mean()}) .....: apply on a Series can operate on a returned value from the applied function, that is itself a series, and possibly upcast the result to a DataFrame: In [164]: def f(x): .....: return pd.Series([x, x ** 2], index=["x", "x^2"]) .....: In [165]: s = pd.Series(np.random.rand(5)) In [166]: s Out[166]: 0 0.321438 1 0.493496 2 0.139505 3 0.910103 4 0.194158 dtype: float64 In [167]: s.apply(f) Out[167]: x x^2 0 0.321438 0.103323 1 0.493496 0.243538 2 0.139505 0.019462 3 0.910103 0.828287 4 0.194158 0.037697 Control grouped column(s) placement with group_keys# Note If group_keys=True is specified when calling groupby(), functions passed to apply that return like-indexed outputs will have the group keys added to the result index. Previous versions of pandas would add the group keys only when the result from the applied function had a different index than the input. If group_keys is not specified, the group keys will not be added for like-indexed outputs. In the future this behavior will change to always respect group_keys, which defaults to True. Changed in version 1.5.0. To control whether the grouped column(s) are included in the indices, you can use the argument group_keys. Compare In [168]: df.groupby("A", group_keys=True).apply(lambda x: x) Out[168]: A B C D A bar 1 bar one 0.254161 1.511763 3 bar three 0.215897 -0.990582 5 bar two -0.077118 1.211526 foo 0 foo one -0.575247 1.346061 2 foo two -1.143704 1.627081 4 foo two 1.193555 -0.441652 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 with In [169]: df.groupby("A", group_keys=False).apply(lambda x: x) Out[169]: A B C D 0 foo one -0.575247 1.346061 1 bar one 0.254161 1.511763 2 foo two -1.143704 1.627081 3 bar three 0.215897 -0.990582 4 foo two 1.193555 -0.441652 5 bar two -0.077118 1.211526 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 Similar to Aggregations with User-Defined Functions, the resulting dtype will reflect that of the apply function. If the results from different groups have different dtypes, then a common dtype will be determined in the same way as DataFrame construction. Numba Accelerated Routines# New in version 1.1. If Numba is installed as an optional dependency, the transform and aggregate methods support engine='numba' and engine_kwargs arguments. See enhancing performance with Numba for general usage of the arguments and performance considerations. The function signature must start with values, index exactly as the data belonging to each group will be passed into values, and the group index will be passed into index. Warning When using engine='numba', there will be no “fall back” behavior internally. The group data and group index will be passed as NumPy arrays to the JITed user defined function, and no alternative execution attempts will be tried. Other useful features# Automatic exclusion of “nuisance” columns# Again consider the example DataFrame we’ve been looking at: In [170]: df Out[170]: A B C D 0 foo one -0.575247 1.346061 1 bar one 0.254161 1.511763 2 foo two -1.143704 1.627081 3 bar three 0.215897 -0.990582 4 foo two 1.193555 -0.441652 5 bar two -0.077118 1.211526 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 Suppose we wish to compute the standard deviation grouped by the A column. There is a slight problem, namely that we don’t care about the data in column B. We refer to this as a “nuisance” column. You can avoid nuisance columns by specifying numeric_only=True: In [171]: df.groupby("A").std(numeric_only=True) Out[171]: C D A bar 0.181231 1.366330 foo 0.912265 0.884785 Note that df.groupby('A').colname.std(). is more efficient than df.groupby('A').std().colname, so if the result of an aggregation function is only interesting over one column (here colname), it may be filtered before applying the aggregation function. Note Any object column, also if it contains numerical values such as Decimal objects, is considered as a “nuisance” columns. They are excluded from aggregate functions automatically in groupby. If you do wish to include decimal or object columns in an aggregation with other non-nuisance data types, you must do so explicitly. Warning The automatic dropping of nuisance columns has been deprecated and will be removed in a future version of pandas. If columns are included that cannot be operated on, pandas will instead raise an error. In order to avoid this, either select the columns you wish to operate on or specify numeric_only=True. In [172]: from decimal import Decimal In [173]: df_dec = pd.DataFrame( .....: { .....: "id": [1, 2, 1, 2], .....: "int_column": [1, 2, 3, 4], .....: "dec_column": [ .....: Decimal("0.50"), .....: Decimal("0.15"), .....: Decimal("0.25"), .....: Decimal("0.40"), .....: ], .....: } .....: ) .....: # Decimal columns can be sum'd explicitly by themselves... In [174]: df_dec.groupby(["id"])[["dec_column"]].sum() Out[174]: dec_column id 1 0.75 2 0.55 # ...but cannot be combined with standard data types or they will be excluded In [175]: df_dec.groupby(["id"])[["int_column", "dec_column"]].sum() Out[175]: int_column id 1 4 2 6 # Use .agg function to aggregate over standard and "nuisance" data types # at the same time In [176]: df_dec.groupby(["id"]).agg({"int_column": "sum", "dec_column": "sum"}) Out[176]: int_column dec_column id 1 4 0.75 2 6 0.55 Handling of (un)observed Categorical values# When using a Categorical grouper (as a single grouper, or as part of multiple groupers), the observed keyword controls whether to return a cartesian product of all possible groupers values (observed=False) or only those that are observed groupers (observed=True). Show all values: In [177]: pd.Series([1, 1, 1]).groupby( .....: pd.Categorical(["a", "a", "a"], categories=["a", "b"]), observed=False .....: ).count() .....: Out[177]: a 3 b 0 dtype: int64 Show only the observed values: In [178]: pd.Series([1, 1, 1]).groupby( .....: pd.Categorical(["a", "a", "a"], categories=["a", "b"]), observed=True .....: ).count() .....: Out[178]: a 3 dtype: int64 The returned dtype of the grouped will always include all of the categories that were grouped. In [179]: s = ( .....: pd.Series([1, 1, 1]) .....: .groupby(pd.Categorical(["a", "a", "a"], categories=["a", "b"]), observed=False) .....: .count() .....: ) .....: In [180]: s.index.dtype Out[180]: CategoricalDtype(categories=['a', 'b'], ordered=False) NA and NaT group handling# If there are any NaN or NaT values in the grouping key, these will be automatically excluded. In other words, there will never be an “NA group” or “NaT group”. This was not the case in older versions of pandas, but users were generally discarding the NA group anyway (and supporting it was an implementation headache). Grouping with ordered factors# Categorical variables represented as instance of pandas’s Categorical class can be used as group keys. If so, the order of the levels will be preserved: In [181]: data = pd.Series(np.random.randn(100)) In [182]: factor = pd.qcut(data, [0, 0.25, 0.5, 0.75, 1.0]) In [183]: data.groupby(factor).mean() Out[183]: (-2.645, -0.523] -1.362896 (-0.523, 0.0296] -0.260266 (0.0296, 0.654] 0.361802 (0.654, 2.21] 1.073801 dtype: float64 Grouping with a grouper specification# You may need to specify a bit more data to properly group. You can use the pd.Grouper to provide this local control. In [184]: import datetime In [185]: df = pd.DataFrame( .....: { .....: "Branch": "A A A A A A A B".split(), .....: "Buyer": "Carl Mark Carl Carl Joe Joe Joe Carl".split(), .....: "Quantity": [1, 3, 5, 1, 8, 1, 9, 3], .....: "Date": [ .....: datetime.datetime(2013, 1, 1, 13, 0), .....: datetime.datetime(2013, 1, 1, 13, 5), .....: datetime.datetime(2013, 10, 1, 20, 0), .....: datetime.datetime(2013, 10, 2, 10, 0), .....: datetime.datetime(2013, 10, 1, 20, 0), .....: datetime.datetime(2013, 10, 2, 10, 0), .....: datetime.datetime(2013, 12, 2, 12, 0), .....: datetime.datetime(2013, 12, 2, 14, 0), .....: ], .....: } .....: ) .....: In [186]: df Out[186]: Branch Buyer Quantity Date 0 A Carl 1 2013-01-01 13:00:00 1 A Mark 3 2013-01-01 13:05:00 2 A Carl 5 2013-10-01 20:00:00 3 A Carl 1 2013-10-02 10:00:00 4 A Joe 8 2013-10-01 20:00:00 5 A Joe 1 2013-10-02 10:00:00 6 A Joe 9 2013-12-02 12:00:00 7 B Carl 3 2013-12-02 14:00:00 Groupby a specific column with the desired frequency. This is like resampling. In [187]: df.groupby([pd.Grouper(freq="1M", key="Date"), "Buyer"])[["Quantity"]].sum() Out[187]: Quantity Date Buyer 2013-01-31 Carl 1 Mark 3 2013-10-31 Carl 6 Joe 9 2013-12-31 Carl 3 Joe 9 You have an ambiguous specification in that you have a named index and a column that could be potential groupers. In [188]: df = df.set_index("Date") In [189]: df["Date"] = df.index + pd.offsets.MonthEnd(2) In [190]: df.groupby([pd.Grouper(freq="6M", key="Date"), "Buyer"])[["Quantity"]].sum() Out[190]: Quantity Date Buyer 2013-02-28 Carl 1 Mark 3 2014-02-28 Carl 9 Joe 18 In [191]: df.groupby([pd.Grouper(freq="6M", level="Date"), "Buyer"])[["Quantity"]].sum() Out[191]: Quantity Date Buyer 2013-01-31 Carl 1 Mark 3 2014-01-31 Carl 9 Joe 18 Taking the first rows of each group# Just like for a DataFrame or Series you can call head and tail on a groupby: In [192]: df = pd.DataFrame([[1, 2], [1, 4], [5, 6]], columns=["A", "B"]) In [193]: df Out[193]: A B 0 1 2 1 1 4 2 5 6 In [194]: g = df.groupby("A") In [195]: g.head(1) Out[195]: A B 0 1 2 2 5 6 In [196]: g.tail(1) Out[196]: A B 1 1 4 2 5 6 This shows the first or last n rows from each group. Taking the nth row of each group# To select from a DataFrame or Series the nth item, use nth(). This is a reduction method, and will return a single row (or no row) per group if you pass an int for n: In [197]: df = pd.DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=["A", "B"]) In [198]: g = df.groupby("A") In [199]: g.nth(0) Out[199]: B A 1 NaN 5 6.0 In [200]: g.nth(-1) Out[200]: B A 1 4.0 5 6.0 In [201]: g.nth(1) Out[201]: B A 1 4.0 If you want to select the nth not-null item, use the dropna kwarg. For a DataFrame this should be either 'any' or 'all' just like you would pass to dropna: # nth(0) is the same as g.first() In [202]: g.nth(0, dropna="any") Out[202]: B A 1 4.0 5 6.0 In [203]: g.first() Out[203]: B A 1 4.0 5 6.0 # nth(-1) is the same as g.last() In [204]: g.nth(-1, dropna="any") # NaNs denote group exhausted when using dropna Out[204]: B A 1 4.0 5 6.0 In [205]: g.last() Out[205]: B A 1 4.0 5 6.0 In [206]: g.B.nth(0, dropna="all") Out[206]: A 1 4.0 5 6.0 Name: B, dtype: float64 As with other methods, passing as_index=False, will achieve a filtration, which returns the grouped row. In [207]: df = pd.DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=["A", "B"]) In [208]: g = df.groupby("A", as_index=False) In [209]: g.nth(0) Out[209]: A B 0 1 NaN 2 5 6.0 In [210]: g.nth(-1) Out[210]: A B 1 1 4.0 2 5 6.0 You can also select multiple rows from each group by specifying multiple nth values as a list of ints. In [211]: business_dates = pd.date_range(start="4/1/2014", end="6/30/2014", freq="B") In [212]: df = pd.DataFrame(1, index=business_dates, columns=["a", "b"]) # get the first, 4th, and last date index for each month In [213]: df.groupby([df.index.year, df.index.month]).nth([0, 3, -1]) Out[213]: a b 2014 4 1 1 4 1 1 4 1 1 5 1 1 5 1 1 5 1 1 6 1 1 6 1 1 6 1 1 Enumerate group items# To see the order in which each row appears within its group, use the cumcount method: In [214]: dfg = pd.DataFrame(list("aaabba"), columns=["A"]) In [215]: dfg Out[215]: A 0 a 1 a 2 a 3 b 4 b 5 a In [216]: dfg.groupby("A").cumcount() Out[216]: 0 0 1 1 2 2 3 0 4 1 5 3 dtype: int64 In [217]: dfg.groupby("A").cumcount(ascending=False) Out[217]: 0 3 1 2 2 1 3 1 4 0 5 0 dtype: int64 Enumerate groups# To see the ordering of the groups (as opposed to the order of rows within a group given by cumcount) you can use ngroup(). Note that the numbers given to the groups match the order in which the groups would be seen when iterating over the groupby object, not the order they are first observed. In [218]: dfg = pd.DataFrame(list("aaabba"), columns=["A"]) In [219]: dfg Out[219]: A 0 a 1 a 2 a 3 b 4 b 5 a In [220]: dfg.groupby("A").ngroup() Out[220]: 0 0 1 0 2 0 3 1 4 1 5 0 dtype: int64 In [221]: dfg.groupby("A").ngroup(ascending=False) Out[221]: 0 1 1 1 2 1 3 0 4 0 5 1 dtype: int64 Plotting# Groupby also works with some plotting methods. For example, suppose we suspect that some features in a DataFrame may differ by group, in this case, the values in column 1 where the group is “B” are 3 higher on average. In [222]: np.random.seed(1234) In [223]: df = pd.DataFrame(np.random.randn(50, 2)) In [224]: df["g"] = np.random.choice(["A", "B"], size=50) In [225]: df.loc[df["g"] == "B", 1] += 3 We can easily visualize this with a boxplot: In [226]: df.groupby("g").boxplot() Out[226]: A AxesSubplot(0.1,0.15;0.363636x0.75) B AxesSubplot(0.536364,0.15;0.363636x0.75) dtype: object The result of calling boxplot is a dictionary whose keys are the values of our grouping column g (“A” and “B”). The values of the resulting dictionary can be controlled by the return_type keyword of boxplot. See the visualization documentation for more. Warning For historical reasons, df.groupby("g").boxplot() is not equivalent to df.boxplot(by="g"). See here for an explanation. Piping function calls# Similar to the functionality provided by DataFrame and Series, functions that take GroupBy objects can be chained together using a pipe method to allow for a cleaner, more readable syntax. To read about .pipe in general terms, see here. Combining .groupby and .pipe is often useful when you need to reuse GroupBy objects. As an example, imagine having a DataFrame with columns for stores, products, revenue and quantity sold. We’d like to do a groupwise calculation of prices (i.e. revenue/quantity) per store and per product. We could do this in a multi-step operation, but expressing it in terms of piping can make the code more readable. First we set the data: In [227]: n = 1000 In [228]: df = pd.DataFrame( .....: { .....: "Store": np.random.choice(["Store_1", "Store_2"], n), .....: "Product": np.random.choice(["Product_1", "Product_2"], n), .....: "Revenue": (np.random.random(n) * 50 + 10).round(2), .....: "Quantity": np.random.randint(1, 10, size=n), .....: } .....: ) .....: In [229]: df.head(2) Out[229]: Store Product Revenue Quantity 0 Store_2 Product_1 26.12 1 1 Store_2 Product_1 28.86 1 Now, to find prices per store/product, we can simply do: In [230]: ( .....: df.groupby(["Store", "Product"]) .....: .pipe(lambda grp: grp.Revenue.sum() / grp.Quantity.sum()) .....: .unstack() .....: .round(2) .....: ) .....: Out[230]: Product Product_1 Product_2 Store Store_1 6.82 7.05 Store_2 6.30 6.64 Piping can also be expressive when you want to deliver a grouped object to some arbitrary function, for example: In [231]: def mean(groupby): .....: return groupby.mean() .....: In [232]: df.groupby(["Store", "Product"]).pipe(mean) Out[232]: Revenue Quantity Store Product Store_1 Product_1 34.622727 5.075758 Product_2 35.482815 5.029630 Store_2 Product_1 32.972837 5.237589 Product_2 34.684360 5.224000 where mean takes a GroupBy object and finds the mean of the Revenue and Quantity columns respectively for each Store-Product combination. The mean function can be any function that takes in a GroupBy object; the .pipe will pass the GroupBy object as a parameter into the function you specify. Examples# Regrouping by factor# Regroup columns of a DataFrame according to their sum, and sum the aggregated ones. In [233]: df = pd.DataFrame({"a": [1, 0, 0], "b": [0, 1, 0], "c": [1, 0, 0], "d": [2, 3, 4]}) In [234]: df Out[234]: a b c d 0 1 0 1 2 1 0 1 0 3 2 0 0 0 4 In [235]: df.groupby(df.sum(), axis=1).sum() Out[235]: 1 9 0 2 2 1 1 3 2 0 4 Multi-column factorization# By using ngroup(), we can extract information about the groups in a way similar to factorize() (as described further in the reshaping API) but which applies naturally to multiple columns of mixed type and different sources. This can be useful as an intermediate categorical-like step in processing, when the relationships between the group rows are more important than their content, or as input to an algorithm which only accepts the integer encoding. (For more information about support in pandas for full categorical data, see the Categorical introduction and the API documentation.) In [236]: dfg = pd.DataFrame({"A": [1, 1, 2, 3, 2], "B": list("aaaba")}) In [237]: dfg Out[237]: A B 0 1 a 1 1 a 2 2 a 3 3 b 4 2 a In [238]: dfg.groupby(["A", "B"]).ngroup() Out[238]: 0 0 1 0 2 1 3 2 4 1 dtype: int64 In [239]: dfg.groupby(["A", [0, 0, 0, 1, 1]]).ngroup() Out[239]: 0 0 1 0 2 1 3 3 4 2 dtype: int64 Groupby by indexer to ‘resample’ data# Resampling produces new hypothetical samples (resamples) from already existing observed data or from a model that generates data. These new samples are similar to the pre-existing samples. In order to resample to work on indices that are non-datetimelike, the following procedure can be utilized. In the following examples, df.index // 5 returns a binary array which is used to determine what gets selected for the groupby operation. Note The below example shows how we can downsample by consolidation of samples into fewer samples. Here by using df.index // 5, we are aggregating the samples in bins. By applying std() function, we aggregate the information contained in many samples into a small subset of values which is their standard deviation thereby reducing the number of samples. In [240]: df = pd.DataFrame(np.random.randn(10, 2)) In [241]: df Out[241]: 0 1 0 -0.793893 0.321153 1 0.342250 1.618906 2 -0.975807 1.918201 3 -0.810847 -1.405919 4 -1.977759 0.461659 5 0.730057 -1.316938 6 -0.751328 0.528290 7 -0.257759 -1.081009 8 0.505895 -1.701948 9 -1.006349 0.020208 In [242]: df.index // 5 Out[242]: Int64Index([0, 0, 0, 0, 0, 1, 1, 1, 1, 1], dtype='int64') In [243]: df.groupby(df.index // 5).std() Out[243]: 0 1 0 0.823647 1.312912 1 0.760109 0.942941 Returning a Series to propagate names# Group DataFrame columns, compute a set of metrics and return a named Series. The Series name is used as the name for the column index. This is especially useful in conjunction with reshaping operations such as stacking in which the column index name will be used as the name of the inserted column: In [244]: df = pd.DataFrame( .....: { .....: "a": [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], .....: "b": [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1], .....: "c": [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], .....: "d": [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1], .....: } .....: ) .....: In [245]: def compute_metrics(x): .....: result = {"b_sum": x["b"].sum(), "c_mean": x["c"].mean()} .....: return pd.Series(result, name="metrics") .....: In [246]: result = df.groupby("a").apply(compute_metrics) In [247]: result Out[247]: metrics b_sum c_mean a 0 2.0 0.5 1 2.0 0.5 2 2.0 0.5 In [248]: result.stack() Out[248]: a metrics 0 b_sum 2.0 c_mean 0.5 1 b_sum 2.0 c_mean 0.5 2 b_sum 2.0 c_mean 0.5 dtype: float64
32
671
Filtering only 1 column in a df without returning the entire DF in 1 line I'm hoping that there is a way i can return a series from df while im filtering it in 1 line. Is there a way I could return a column from my df after I filter it? Currently my process is something like this df = df[df['a'] > 0 ] list = df['a']
The df.loc syntax is the preferred way to do this, as @JohnM wrote in his comment, though I find the syntax from @Don'tAccept more readable and scaleable however since it can handle cases like column names with spaces in them. These combine like: df.loc[df['a'] > 0, 'a'] Note this is expandable to provide multiple columns, for example if you wanted columns 'a' and 'b' you would do: df.loc[df['a'] > 0, ['a', 'b']] Lastly, you can verify that df.a and df['a'] are the same by checking in: df.a is df['a'] out: True The is here (as opposed to ==) means df.a and df['a'] point to the same object in memory, so they are interchangeable.
64,239,252
Time series data merge nearest right dataset has multiple same values
<p>I have two dataframes. The first is like a log while the second is like inputs. I want to combine this log and inputs based on their time columns.</p> <p>I tried using <code>merge_asof</code> but it only takes one input into the input dateframe.</p> <p>Here is an example. Dataframe Log Times, <code>log</code>:</p> <pre><code> STARTTIME_Log 2020-05-28 21:57:27.000000 2020-05-28 06:35:20.000000 2020-05-28 19:51:39.000000 2020-05-28 20:43:23.000000 </code></pre> <p>DataFrame İnput Times and Values, <code>input</code>:</p> <pre><code> IO_Time IOName value 2020-05-28 21:57:35.037 A 79.65 2020-05-28 21:57:35.037 B 33.33 2020-05-28 06:35:22.037 A 27.53 2020-05-28 06:35:22.037 B 6.23 2020-05-28 09:30:20.037 A 43.50 2020-05-28 09:30:20.037 B 15.23 2020-05-28 19:51:40.037 A 100.00 2020-05-28 19:51:40.037 B 12.52 2020-05-28 20:43:25.037 A 56.43 2020-05-28 20:43:25.037 B 2.67 2020-05-28 22:32:56.037 A 23.45 2020-05-28 22:32:56.037 B 3.55 </code></pre> <p>Expected Output:</p> <pre><code> STARTTIME_Log IOName value 2020-05-28 21:57:27.000000 A 79.65 2020-05-28 21:57:27.000000 B 33.33 2020-05-28 06:35:20.000000 A 27.53 2020-05-28 06:35:20.000000 B 6.23 2020-05-28 19:51:39.000000 A 100.00 2020-05-28 19:51:39.000000 B 12.52 2020-05-28 20:43:23.000000 A 56.43 2020-05-28 20:43:23.000000 B 2.67 </code></pre> <p>The output merges the <code>log</code> and <code>input</code> dataframes in the nearest time. The merge is done on <code>STARTTIME_Log</code> for the <code>log</code> dataframe and <code>IO_Time</code> on <code>input</code>. If there is too large a difference then the rows are dropped.</p> <p>How can I do that?</p>
64,239,730
"2020-10-07T07:28:00.140000"
1
null
0
42
python|pandas
<p>First, make sure that the <code>IO_Time</code> and <code>STARTTIME_Log</code> columns are of datetime type and are sorted (required to use <code>merge_asof</code>:</p> <pre><code>log['STARTTIME_Log'] = pd.to_datetime(log['STARTTIME_Log']) input['IO_Time'] = pd.to_datetime(input['IO_Time']) log = log.sort_values('STARTTIME_Log') input = input.sort_values('IO_Time') </code></pre> <p>Now, use <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.merge_asof.html" rel="nofollow noreferrer"><code>merge_asof</code></a> with <code>input</code> as the left dataframe and <code>log</code> as the right. Note that you need to specify an acceptable tolerance value (I set it to 10 seconds here):</p> <pre><code>tol = pd.Timedelta('10s') df = pd.merge_asof(input, log, left_on='IO_Time', right_on='STARTTIME_Log', tolerance=tol, direction='nearest') df = df.dropna(subset=['STARTTIME_Log']).drop(columns='IO_Time') </code></pre> <p>Afterwards, the rows that don't have a match in <code>log</code> are dropped and the <code>IO_Time</code> column is removed.</p> <p>Result:</p> <pre><code> IOName value STARTTIME_Log 0 A 27.53 2020-05-28 06:35:20 1 B 6.23 2020-05-28 06:35:20 4 A 100.00 2020-05-28 19:51:39 5 B 12.52 2020-05-28 19:51:39 6 A 56.43 2020-05-28 20:43:23 7 B 2.67 2020-05-28 20:43:23 8 A 79.65 2020-05-28 21:57:27 9 B 33.33 2020-05-28 21:57:27 </code></pre>
"2020-10-07T08:00:18.793000"
0
https://pandas.pydata.org/docs/dev/user_guide/merging.html
Merge, join, concatenate and compare# First, make sure that the IO_Time and STARTTIME_Log columns are of datetime type and are sorted (required to use merge_asof: log['STARTTIME_Log'] = pd.to_datetime(log['STARTTIME_Log']) input['IO_Time'] = pd.to_datetime(input['IO_Time']) log = log.sort_values('STARTTIME_Log') input = input.sort_values('IO_Time') Now, use merge_asof with input as the left dataframe and log as the right. Note that you need to specify an acceptable tolerance value (I set it to 10 seconds here): tol = pd.Timedelta('10s') df = pd.merge_asof(input, log, left_on='IO_Time', right_on='STARTTIME_Log', tolerance=tol, direction='nearest') df = df.dropna(subset=['STARTTIME_Log']).drop(columns='IO_Time') Afterwards, the rows that don't have a match in log are dropped and the IO_Time column is removed. Result: IOName value STARTTIME_Log 0 A 27.53 2020-05-28 06:35:20 1 B 6.23 2020-05-28 06:35:20 4 A 100.00 2020-05-28 19:51:39 5 B 12.52 2020-05-28 19:51:39 6 A 56.43 2020-05-28 20:43:23 7 B 2.67 2020-05-28 20:43:23 8 A 79.65 2020-05-28 21:57:27 9 B 33.33 2020-05-28 21:57:27 Merge, join, concatenate and compare# pandas provides various facilities for easily combining together Series or DataFrame with various kinds of set logic for the indexes and relational algebra functionality in the case of join / merge-type operations. In addition, pandas also provides utilities to compare two Series or DataFrame and summarize their differences. Concatenating objects# The concat() function (in the main pandas namespace) does all of the heavy lifting of performing concatenation operations along an axis while performing optional set logic (union or intersection) of the indexes (if any) on the other axes. Note that I say “if any” because there is only a single possible axis of concatenation for Series. Before diving into all of the details of concat and what it can do, here is a simple example: In [1]: df1 = pd.DataFrame( ...: { ...: "A": ["A0", "A1", "A2", "A3"], ...: "B": ["B0", "B1", "B2", "B3"], ...: "C": ["C0", "C1", "C2", "C3"], ...: "D": ["D0", "D1", "D2", "D3"], ...: }, ...: index=[0, 1, 2, 3], ...: ) ...: In [2]: df2 = pd.DataFrame( ...: { ...: "A": ["A4", "A5", "A6", "A7"], ...: "B": ["B4", "B5", "B6", "B7"], ...: "C": ["C4", "C5", "C6", "C7"], ...: "D": ["D4", "D5", "D6", "D7"], ...: }, ...: index=[4, 5, 6, 7], ...: ) ...: In [3]: df3 = pd.DataFrame( ...: { ...: "A": ["A8", "A9", "A10", "A11"], ...: "B": ["B8", "B9", "B10", "B11"], ...: "C": ["C8", "C9", "C10", "C11"], ...: "D": ["D8", "D9", "D10", "D11"], ...: }, ...: index=[8, 9, 10, 11], ...: ) ...: In [4]: frames = [df1, df2, df3] In [5]: result = pd.concat(frames) Like its sibling function on ndarrays, numpy.concatenate, pandas.concat takes a list or dict of homogeneously-typed objects and concatenates them with some configurable handling of “what to do with the other axes”: pd.concat( objs, axis=0, join="outer", ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, copy=True, ) objs : a sequence or mapping of Series or DataFrame objects. If a dict is passed, the sorted keys will be used as the keys argument, unless it is passed, in which case the values will be selected (see below). Any None objects will be dropped silently unless they are all None in which case a ValueError will be raised. axis : {0, 1, …}, default 0. The axis to concatenate along. join : {‘inner’, ‘outer’}, default ‘outer’. How to handle indexes on other axis(es). Outer for union and inner for intersection. ignore_index : boolean, default False. If True, do not use the index values on the concatenation axis. The resulting axis will be labeled 0, …, n - 1. This is useful if you are concatenating objects where the concatenation axis does not have meaningful indexing information. Note the index values on the other axes are still respected in the join. keys : sequence, default None. Construct hierarchical index using the passed keys as the outermost level. If multiple levels passed, should contain tuples. levels : list of sequences, default None. Specific levels (unique values) to use for constructing a MultiIndex. Otherwise they will be inferred from the keys. names : list, default None. Names for the levels in the resulting hierarchical index. verify_integrity : boolean, default False. Check whether the new concatenated axis contains duplicates. This can be very expensive relative to the actual data concatenation. copy : boolean, default True. If False, do not copy data unnecessarily. Without a little bit of context many of these arguments don’t make much sense. Let’s revisit the above example. Suppose we wanted to associate specific keys with each of the pieces of the chopped up DataFrame. We can do this using the keys argument: In [6]: result = pd.concat(frames, keys=["x", "y", "z"]) As you can see (if you’ve read the rest of the documentation), the resulting object’s index has a hierarchical index. This means that we can now select out each chunk by key: In [7]: result.loc["y"] Out[7]: A B C D 4 A4 B4 C4 D4 5 A5 B5 C5 D5 6 A6 B6 C6 D6 7 A7 B7 C7 D7 It’s not a stretch to see how this can be very useful. More detail on this functionality below. Note It is worth noting that concat() makes a full copy of the data, and that constantly reusing this function can create a significant performance hit. If you need to use the operation over several datasets, use a list comprehension. frames = [ process_your_file(f) for f in files ] result = pd.concat(frames) Note When concatenating DataFrames with named axes, pandas will attempt to preserve these index/column names whenever possible. In the case where all inputs share a common name, this name will be assigned to the result. When the input names do not all agree, the result will be unnamed. The same is true for MultiIndex, but the logic is applied separately on a level-by-level basis. Set logic on the other axes# When gluing together multiple DataFrames, you have a choice of how to handle the other axes (other than the one being concatenated). This can be done in the following two ways: Take the union of them all, join='outer'. This is the default option as it results in zero information loss. Take the intersection, join='inner'. Here is an example of each of these methods. First, the default join='outer' behavior: In [8]: df4 = pd.DataFrame( ...: { ...: "B": ["B2", "B3", "B6", "B7"], ...: "D": ["D2", "D3", "D6", "D7"], ...: "F": ["F2", "F3", "F6", "F7"], ...: }, ...: index=[2, 3, 6, 7], ...: ) ...: In [9]: result = pd.concat([df1, df4], axis=1) Here is the same thing with join='inner': In [10]: result = pd.concat([df1, df4], axis=1, join="inner") Lastly, suppose we just wanted to reuse the exact index from the original DataFrame: In [11]: result = pd.concat([df1, df4], axis=1).reindex(df1.index) Similarly, we could index before the concatenation: In [12]: pd.concat([df1, df4.reindex(df1.index)], axis=1) Out[12]: A B C D B D F 0 A0 B0 C0 D0 NaN NaN NaN 1 A1 B1 C1 D1 NaN NaN NaN 2 A2 B2 C2 D2 B2 D2 F2 3 A3 B3 C3 D3 B3 D3 F3 Ignoring indexes on the concatenation axis# For DataFrame objects which don’t have a meaningful index, you may wish to append them and ignore the fact that they may have overlapping indexes. To do this, use the ignore_index argument: In [13]: result = pd.concat([df1, df4], ignore_index=True, sort=False) Concatenating with mixed ndims# You can concatenate a mix of Series and DataFrame objects. The Series will be transformed to DataFrame with the column name as the name of the Series. In [14]: s1 = pd.Series(["X0", "X1", "X2", "X3"], name="X") In [15]: result = pd.concat([df1, s1], axis=1) Note Since we’re concatenating a Series to a DataFrame, we could have achieved the same result with DataFrame.assign(). To concatenate an arbitrary number of pandas objects (DataFrame or Series), use concat. If unnamed Series are passed they will be numbered consecutively. In [16]: s2 = pd.Series(["_0", "_1", "_2", "_3"]) In [17]: result = pd.concat([df1, s2, s2, s2], axis=1) Passing ignore_index=True will drop all name references. In [18]: result = pd.concat([df1, s1], axis=1, ignore_index=True) More concatenating with group keys# A fairly common use of the keys argument is to override the column names when creating a new DataFrame based on existing Series. Notice how the default behaviour consists on letting the resulting DataFrame inherit the parent Series’ name, when these existed. In [19]: s3 = pd.Series([0, 1, 2, 3], name="foo") In [20]: s4 = pd.Series([0, 1, 2, 3]) In [21]: s5 = pd.Series([0, 1, 4, 5]) In [22]: pd.concat([s3, s4, s5], axis=1) Out[22]: foo 0 1 0 0 0 0 1 1 1 1 2 2 2 4 3 3 3 5 Through the keys argument we can override the existing column names. In [23]: pd.concat([s3, s4, s5], axis=1, keys=["red", "blue", "yellow"]) Out[23]: red blue yellow 0 0 0 0 1 1 1 1 2 2 2 4 3 3 3 5 Let’s consider a variation of the very first example presented: In [24]: result = pd.concat(frames, keys=["x", "y", "z"]) You can also pass a dict to concat in which case the dict keys will be used for the keys argument (unless other keys are specified): In [25]: pieces = {"x": df1, "y": df2, "z": df3} In [26]: result = pd.concat(pieces) In [27]: result = pd.concat(pieces, keys=["z", "y"]) The MultiIndex created has levels that are constructed from the passed keys and the index of the DataFrame pieces: In [28]: result.index.levels Out[28]: FrozenList([['z', 'y'], [4, 5, 6, 7, 8, 9, 10, 11]]) If you wish to specify other levels (as will occasionally be the case), you can do so using the levels argument: In [29]: result = pd.concat( ....: pieces, keys=["x", "y", "z"], levels=[["z", "y", "x", "w"]], names=["group_key"] ....: ) ....: In [30]: result.index.levels Out[30]: FrozenList([['z', 'y', 'x', 'w'], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]) This is fairly esoteric, but it is actually necessary for implementing things like GroupBy where the order of a categorical variable is meaningful. Appending rows to a DataFrame# If you have a series that you want to append as a single row to a DataFrame, you can convert the row into a DataFrame and use concat In [31]: s2 = pd.Series(["X0", "X1", "X2", "X3"], index=["A", "B", "C", "D"]) In [32]: result = pd.concat([df1, s2.to_frame().T], ignore_index=True) You should use ignore_index with this method to instruct DataFrame to discard its index. If you wish to preserve the index, you should construct an appropriately-indexed DataFrame and append or concatenate those objects. Database-style DataFrame or named Series joining/merging# pandas has full-featured, high performance in-memory join operations idiomatically very similar to relational databases like SQL. These methods perform significantly better (in some cases well over an order of magnitude better) than other open source implementations (like base::merge.data.frame in R). The reason for this is careful algorithmic design and the internal layout of the data in DataFrame. See the cookbook for some advanced strategies. Users who are familiar with SQL but new to pandas might be interested in a comparison with SQL. pandas provides a single function, merge(), as the entry point for all standard database join operations between DataFrame or named Series objects: pd.merge( left, right, how="inner", on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=True, suffixes=("_x", "_y"), copy=True, indicator=False, validate=None, ) left: A DataFrame or named Series object. right: Another DataFrame or named Series object. on: Column or index level names to join on. Must be found in both the left and right DataFrame and/or Series objects. If not passed and left_index and right_index are False, the intersection of the columns in the DataFrames and/or Series will be inferred to be the join keys. left_on: Columns or index levels from the left DataFrame or Series to use as keys. Can either be column names, index level names, or arrays with length equal to the length of the DataFrame or Series. right_on: Columns or index levels from the right DataFrame or Series to use as keys. Can either be column names, index level names, or arrays with length equal to the length of the DataFrame or Series. left_index: If True, use the index (row labels) from the left DataFrame or Series as its join key(s). In the case of a DataFrame or Series with a MultiIndex (hierarchical), the number of levels must match the number of join keys from the right DataFrame or Series. right_index: Same usage as left_index for the right DataFrame or Series how: One of 'left', 'right', 'outer', 'inner', 'cross'. Defaults to inner. See below for more detailed description of each method. sort: Sort the result DataFrame by the join keys in lexicographical order. Defaults to True, setting to False will improve performance substantially in many cases. suffixes: A tuple of string suffixes to apply to overlapping columns. Defaults to ('_x', '_y'). copy: Always copy data (default True) from the passed DataFrame or named Series objects, even when reindexing is not necessary. Cannot be avoided in many cases but may improve performance / memory usage. The cases where copying can be avoided are somewhat pathological but this option is provided nonetheless. indicator: Add a column to the output DataFrame called _merge with information on the source of each row. _merge is Categorical-type and takes on a value of left_only for observations whose merge key only appears in 'left' DataFrame or Series, right_only for observations whose merge key only appears in 'right' DataFrame or Series, and both if the observation’s merge key is found in both. validate : string, default None. If specified, checks if merge is of specified type. “one_to_one” or “1:1”: checks if merge keys are unique in both left and right datasets. “one_to_many” or “1:m”: checks if merge keys are unique in left dataset. “many_to_one” or “m:1”: checks if merge keys are unique in right dataset. “many_to_many” or “m:m”: allowed, but does not result in checks. Note Support for specifying index levels as the on, left_on, and right_on parameters was added in version 0.23.0. Support for merging named Series objects was added in version 0.24.0. The return type will be the same as left. If left is a DataFrame or named Series and right is a subclass of DataFrame, the return type will still be DataFrame. merge is a function in the pandas namespace, and it is also available as a DataFrame instance method merge(), with the calling DataFrame being implicitly considered the left object in the join. The related join() method, uses merge internally for the index-on-index (by default) and column(s)-on-index join. If you are joining on index only, you may wish to use DataFrame.join to save yourself some typing. Brief primer on merge methods (relational algebra)# Experienced users of relational databases like SQL will be familiar with the terminology used to describe join operations between two SQL-table like structures (DataFrame objects). There are several cases to consider which are very important to understand: one-to-one joins: for example when joining two DataFrame objects on their indexes (which must contain unique values). many-to-one joins: for example when joining an index (unique) to one or more columns in a different DataFrame. many-to-many joins: joining columns on columns. Note When joining columns on columns (potentially a many-to-many join), any indexes on the passed DataFrame objects will be discarded. It is worth spending some time understanding the result of the many-to-many join case. In SQL / standard relational algebra, if a key combination appears more than once in both tables, the resulting table will have the Cartesian product of the associated data. Here is a very basic example with one unique key combination: In [33]: left = pd.DataFrame( ....: { ....: "key": ["K0", "K1", "K2", "K3"], ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: } ....: ) ....: In [34]: right = pd.DataFrame( ....: { ....: "key": ["K0", "K1", "K2", "K3"], ....: "C": ["C0", "C1", "C2", "C3"], ....: "D": ["D0", "D1", "D2", "D3"], ....: } ....: ) ....: In [35]: result = pd.merge(left, right, on="key") Here is a more complicated example with multiple join keys. Only the keys appearing in left and right are present (the intersection), since how='inner' by default. In [36]: left = pd.DataFrame( ....: { ....: "key1": ["K0", "K0", "K1", "K2"], ....: "key2": ["K0", "K1", "K0", "K1"], ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: } ....: ) ....: In [37]: right = pd.DataFrame( ....: { ....: "key1": ["K0", "K1", "K1", "K2"], ....: "key2": ["K0", "K0", "K0", "K0"], ....: "C": ["C0", "C1", "C2", "C3"], ....: "D": ["D0", "D1", "D2", "D3"], ....: } ....: ) ....: In [38]: result = pd.merge(left, right, on=["key1", "key2"]) The how argument to merge specifies how to determine which keys are to be included in the resulting table. If a key combination does not appear in either the left or right tables, the values in the joined table will be NA. Here is a summary of the how options and their SQL equivalent names: Merge method SQL Join Name Description left LEFT OUTER JOIN Use keys from left frame only right RIGHT OUTER JOIN Use keys from right frame only outer FULL OUTER JOIN Use union of keys from both frames inner INNER JOIN Use intersection of keys from both frames cross CROSS JOIN Create the cartesian product of rows of both frames In [39]: result = pd.merge(left, right, how="left", on=["key1", "key2"]) In [40]: result = pd.merge(left, right, how="right", on=["key1", "key2"]) In [41]: result = pd.merge(left, right, how="outer", on=["key1", "key2"]) In [42]: result = pd.merge(left, right, how="inner", on=["key1", "key2"]) In [43]: result = pd.merge(left, right, how="cross") You can merge a mult-indexed Series and a DataFrame, if the names of the MultiIndex correspond to the columns from the DataFrame. Transform the Series to a DataFrame using Series.reset_index() before merging, as shown in the following example. In [44]: df = pd.DataFrame({"Let": ["A", "B", "C"], "Num": [1, 2, 3]}) In [45]: df Out[45]: Let Num 0 A 1 1 B 2 2 C 3 In [46]: ser = pd.Series( ....: ["a", "b", "c", "d", "e", "f"], ....: index=pd.MultiIndex.from_arrays( ....: [["A", "B", "C"] * 2, [1, 2, 3, 4, 5, 6]], names=["Let", "Num"] ....: ), ....: ) ....: In [47]: ser Out[47]: Let Num A 1 a B 2 b C 3 c A 4 d B 5 e C 6 f dtype: object In [48]: pd.merge(df, ser.reset_index(), on=["Let", "Num"]) Out[48]: Let Num 0 0 A 1 a 1 B 2 b 2 C 3 c Here is another example with duplicate join keys in DataFrames: In [49]: left = pd.DataFrame({"A": [1, 2], "B": [2, 2]}) In [50]: right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]}) In [51]: result = pd.merge(left, right, on="B", how="outer") Warning Joining / merging on duplicate keys can cause a returned frame that is the multiplication of the row dimensions, which may result in memory overflow. It is the user’ s responsibility to manage duplicate values in keys before joining large DataFrames. Checking for duplicate keys# Users can use the validate argument to automatically check whether there are unexpected duplicates in their merge keys. Key uniqueness is checked before merge operations and so should protect against memory overflows. Checking key uniqueness is also a good way to ensure user data structures are as expected. In the following example, there are duplicate values of B in the right DataFrame. As this is not a one-to-one merge – as specified in the validate argument – an exception will be raised. In [52]: left = pd.DataFrame({"A": [1, 2], "B": [1, 2]}) In [53]: right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]}) In [53]: result = pd.merge(left, right, on="B", how="outer", validate="one_to_one") ... MergeError: Merge keys are not unique in right dataset; not a one-to-one merge If the user is aware of the duplicates in the right DataFrame but wants to ensure there are no duplicates in the left DataFrame, one can use the validate='one_to_many' argument instead, which will not raise an exception. In [54]: pd.merge(left, right, on="B", how="outer", validate="one_to_many") Out[54]: A_x B A_y 0 1 1 NaN 1 2 2 4.0 2 2 2 5.0 3 2 2 6.0 The merge indicator# merge() accepts the argument indicator. If True, a Categorical-type column called _merge will be added to the output object that takes on values: Observation Origin _merge value Merge key only in 'left' frame left_only Merge key only in 'right' frame right_only Merge key in both frames both In [55]: df1 = pd.DataFrame({"col1": [0, 1], "col_left": ["a", "b"]}) In [56]: df2 = pd.DataFrame({"col1": [1, 2, 2], "col_right": [2, 2, 2]}) In [57]: pd.merge(df1, df2, on="col1", how="outer", indicator=True) Out[57]: col1 col_left col_right _merge 0 0 a NaN left_only 1 1 b 2.0 both 2 2 NaN 2.0 right_only 3 2 NaN 2.0 right_only The indicator argument will also accept string arguments, in which case the indicator function will use the value of the passed string as the name for the indicator column. In [58]: pd.merge(df1, df2, on="col1", how="outer", indicator="indicator_column") Out[58]: col1 col_left col_right indicator_column 0 0 a NaN left_only 1 1 b 2.0 both 2 2 NaN 2.0 right_only 3 2 NaN 2.0 right_only Merge dtypes# Merging will preserve the dtype of the join keys. In [59]: left = pd.DataFrame({"key": [1], "v1": [10]}) In [60]: left Out[60]: key v1 0 1 10 In [61]: right = pd.DataFrame({"key": [1, 2], "v1": [20, 30]}) In [62]: right Out[62]: key v1 0 1 20 1 2 30 We are able to preserve the join keys: In [63]: pd.merge(left, right, how="outer") Out[63]: key v1 0 1 10 1 1 20 2 2 30 In [64]: pd.merge(left, right, how="outer").dtypes Out[64]: key int64 v1 int64 dtype: object Of course if you have missing values that are introduced, then the resulting dtype will be upcast. In [65]: pd.merge(left, right, how="outer", on="key") Out[65]: key v1_x v1_y 0 1 10.0 20 1 2 NaN 30 In [66]: pd.merge(left, right, how="outer", on="key").dtypes Out[66]: key int64 v1_x float64 v1_y int64 dtype: object Merging will preserve category dtypes of the mergands. See also the section on categoricals. The left frame. In [67]: from pandas.api.types import CategoricalDtype In [68]: X = pd.Series(np.random.choice(["foo", "bar"], size=(10,))) In [69]: X = X.astype(CategoricalDtype(categories=["foo", "bar"])) In [70]: left = pd.DataFrame( ....: {"X": X, "Y": np.random.choice(["one", "two", "three"], size=(10,))} ....: ) ....: In [71]: left Out[71]: X Y 0 bar one 1 foo one 2 foo three 3 bar three 4 foo one 5 bar one 6 bar three 7 bar three 8 bar three 9 foo three In [72]: left.dtypes Out[72]: X category Y object dtype: object The right frame. In [73]: right = pd.DataFrame( ....: { ....: "X": pd.Series(["foo", "bar"], dtype=CategoricalDtype(["foo", "bar"])), ....: "Z": [1, 2], ....: } ....: ) ....: In [74]: right Out[74]: X Z 0 foo 1 1 bar 2 In [75]: right.dtypes Out[75]: X category Z int64 dtype: object The merged result: In [76]: result = pd.merge(left, right, how="outer") In [77]: result Out[77]: X Y Z 0 bar one 2 1 bar three 2 2 bar one 2 3 bar three 2 4 bar three 2 5 bar three 2 6 foo one 1 7 foo three 1 8 foo one 1 9 foo three 1 In [78]: result.dtypes Out[78]: X category Y object Z int64 dtype: object Note The category dtypes must be exactly the same, meaning the same categories and the ordered attribute. Otherwise the result will coerce to the categories’ dtype. Note Merging on category dtypes that are the same can be quite performant compared to object dtype merging. Joining on index# DataFrame.join() is a convenient method for combining the columns of two potentially differently-indexed DataFrames into a single result DataFrame. Here is a very basic example: In [79]: left = pd.DataFrame( ....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=["K0", "K1", "K2"] ....: ) ....: In [80]: right = pd.DataFrame( ....: {"C": ["C0", "C2", "C3"], "D": ["D0", "D2", "D3"]}, index=["K0", "K2", "K3"] ....: ) ....: In [81]: result = left.join(right) In [82]: result = left.join(right, how="outer") The same as above, but with how='inner'. In [83]: result = left.join(right, how="inner") The data alignment here is on the indexes (row labels). This same behavior can be achieved using merge plus additional arguments instructing it to use the indexes: In [84]: result = pd.merge(left, right, left_index=True, right_index=True, how="outer") In [85]: result = pd.merge(left, right, left_index=True, right_index=True, how="inner") Joining key columns on an index# join() takes an optional on argument which may be a column or multiple column names, which specifies that the passed DataFrame is to be aligned on that column in the DataFrame. These two function calls are completely equivalent: left.join(right, on=key_or_keys) pd.merge( left, right, left_on=key_or_keys, right_index=True, how="left", sort=False ) Obviously you can choose whichever form you find more convenient. For many-to-one joins (where one of the DataFrame’s is already indexed by the join key), using join may be more convenient. Here is a simple example: In [86]: left = pd.DataFrame( ....: { ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: "key": ["K0", "K1", "K0", "K1"], ....: } ....: ) ....: In [87]: right = pd.DataFrame({"C": ["C0", "C1"], "D": ["D0", "D1"]}, index=["K0", "K1"]) In [88]: result = left.join(right, on="key") In [89]: result = pd.merge( ....: left, right, left_on="key", right_index=True, how="left", sort=False ....: ) ....: To join on multiple keys, the passed DataFrame must have a MultiIndex: In [90]: left = pd.DataFrame( ....: { ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: "key1": ["K0", "K0", "K1", "K2"], ....: "key2": ["K0", "K1", "K0", "K1"], ....: } ....: ) ....: In [91]: index = pd.MultiIndex.from_tuples( ....: [("K0", "K0"), ("K1", "K0"), ("K2", "K0"), ("K2", "K1")] ....: ) ....: In [92]: right = pd.DataFrame( ....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=index ....: ) ....: Now this can be joined by passing the two key column names: In [93]: result = left.join(right, on=["key1", "key2"]) The default for DataFrame.join is to perform a left join (essentially a “VLOOKUP” operation, for Excel users), which uses only the keys found in the calling DataFrame. Other join types, for example inner join, can be just as easily performed: In [94]: result = left.join(right, on=["key1", "key2"], how="inner") As you can see, this drops any rows where there was no match. Joining a single Index to a MultiIndex# You can join a singly-indexed DataFrame with a level of a MultiIndexed DataFrame. The level will match on the name of the index of the singly-indexed frame against a level name of the MultiIndexed frame. In [95]: left = pd.DataFrame( ....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, ....: index=pd.Index(["K0", "K1", "K2"], name="key"), ....: ) ....: In [96]: index = pd.MultiIndex.from_tuples( ....: [("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")], ....: names=["key", "Y"], ....: ) ....: In [97]: right = pd.DataFrame( ....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, ....: index=index, ....: ) ....: In [98]: result = left.join(right, how="inner") This is equivalent but less verbose and more memory efficient / faster than this. In [99]: result = pd.merge( ....: left.reset_index(), right.reset_index(), on=["key"], how="inner" ....: ).set_index(["key","Y"]) ....: Joining with two MultiIndexes# This is supported in a limited way, provided that the index for the right argument is completely used in the join, and is a subset of the indices in the left argument, as in this example: In [100]: leftindex = pd.MultiIndex.from_product( .....: [list("abc"), list("xy"), [1, 2]], names=["abc", "xy", "num"] .....: ) .....: In [101]: left = pd.DataFrame({"v1": range(12)}, index=leftindex) In [102]: left Out[102]: v1 abc xy num a x 1 0 2 1 y 1 2 2 3 b x 1 4 2 5 y 1 6 2 7 c x 1 8 2 9 y 1 10 2 11 In [103]: rightindex = pd.MultiIndex.from_product( .....: [list("abc"), list("xy")], names=["abc", "xy"] .....: ) .....: In [104]: right = pd.DataFrame({"v2": [100 * i for i in range(1, 7)]}, index=rightindex) In [105]: right Out[105]: v2 abc xy a x 100 y 200 b x 300 y 400 c x 500 y 600 In [106]: left.join(right, on=["abc", "xy"], how="inner") Out[106]: v1 v2 abc xy num a x 1 0 100 2 1 100 y 1 2 200 2 3 200 b x 1 4 300 2 5 300 y 1 6 400 2 7 400 c x 1 8 500 2 9 500 y 1 10 600 2 11 600 If that condition is not satisfied, a join with two multi-indexes can be done using the following code. In [107]: leftindex = pd.MultiIndex.from_tuples( .....: [("K0", "X0"), ("K0", "X1"), ("K1", "X2")], names=["key", "X"] .....: ) .....: In [108]: left = pd.DataFrame( .....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=leftindex .....: ) .....: In [109]: rightindex = pd.MultiIndex.from_tuples( .....: [("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")], names=["key", "Y"] .....: ) .....: In [110]: right = pd.DataFrame( .....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=rightindex .....: ) .....: In [111]: result = pd.merge( .....: left.reset_index(), right.reset_index(), on=["key"], how="inner" .....: ).set_index(["key", "X", "Y"]) .....: Merging on a combination of columns and index levels# Strings passed as the on, left_on, and right_on parameters may refer to either column names or index level names. This enables merging DataFrame instances on a combination of index levels and columns without resetting indexes. In [112]: left_index = pd.Index(["K0", "K0", "K1", "K2"], name="key1") In [113]: left = pd.DataFrame( .....: { .....: "A": ["A0", "A1", "A2", "A3"], .....: "B": ["B0", "B1", "B2", "B3"], .....: "key2": ["K0", "K1", "K0", "K1"], .....: }, .....: index=left_index, .....: ) .....: In [114]: right_index = pd.Index(["K0", "K1", "K2", "K2"], name="key1") In [115]: right = pd.DataFrame( .....: { .....: "C": ["C0", "C1", "C2", "C3"], .....: "D": ["D0", "D1", "D2", "D3"], .....: "key2": ["K0", "K0", "K0", "K1"], .....: }, .....: index=right_index, .....: ) .....: In [116]: result = left.merge(right, on=["key1", "key2"]) Note When DataFrames are merged on a string that matches an index level in both frames, the index level is preserved as an index level in the resulting DataFrame. Note When DataFrames are merged using only some of the levels of a MultiIndex, the extra levels will be dropped from the resulting merge. In order to preserve those levels, use reset_index on those level names to move those levels to columns prior to doing the merge. Note If a string matches both a column name and an index level name, then a warning is issued and the column takes precedence. This will result in an ambiguity error in a future version. Overlapping value columns# The merge suffixes argument takes a tuple of list of strings to append to overlapping column names in the input DataFrames to disambiguate the result columns: In [117]: left = pd.DataFrame({"k": ["K0", "K1", "K2"], "v": [1, 2, 3]}) In [118]: right = pd.DataFrame({"k": ["K0", "K0", "K3"], "v": [4, 5, 6]}) In [119]: result = pd.merge(left, right, on="k") In [120]: result = pd.merge(left, right, on="k", suffixes=("_l", "_r")) DataFrame.join() has lsuffix and rsuffix arguments which behave similarly. In [121]: left = left.set_index("k") In [122]: right = right.set_index("k") In [123]: result = left.join(right, lsuffix="_l", rsuffix="_r") Joining multiple DataFrames# A list or tuple of DataFrames can also be passed to join() to join them together on their indexes. In [124]: right2 = pd.DataFrame({"v": [7, 8, 9]}, index=["K1", "K1", "K2"]) In [125]: result = left.join([right, right2]) Merging together values within Series or DataFrame columns# Another fairly common situation is to have two like-indexed (or similarly indexed) Series or DataFrame objects and wanting to “patch” values in one object from values for matching indices in the other. Here is an example: In [126]: df1 = pd.DataFrame( .....: [[np.nan, 3.0, 5.0], [-4.6, np.nan, np.nan], [np.nan, 7.0, np.nan]] .....: ) .....: In [127]: df2 = pd.DataFrame([[-42.6, np.nan, -8.2], [-5.0, 1.6, 4]], index=[1, 2]) For this, use the combine_first() method: In [128]: result = df1.combine_first(df2) Note that this method only takes values from the right DataFrame if they are missing in the left DataFrame. A related method, update(), alters non-NA values in place: In [129]: df1.update(df2) Timeseries friendly merging# Merging ordered data# A merge_ordered() function allows combining time series and other ordered data. In particular it has an optional fill_method keyword to fill/interpolate missing data: In [130]: left = pd.DataFrame( .....: {"k": ["K0", "K1", "K1", "K2"], "lv": [1, 2, 3, 4], "s": ["a", "b", "c", "d"]} .....: ) .....: In [131]: right = pd.DataFrame({"k": ["K1", "K2", "K4"], "rv": [1, 2, 3]}) In [132]: pd.merge_ordered(left, right, fill_method="ffill", left_by="s") Out[132]: k lv s rv 0 K0 1.0 a NaN 1 K1 1.0 a 1.0 2 K2 1.0 a 2.0 3 K4 1.0 a 3.0 4 K1 2.0 b 1.0 5 K2 2.0 b 2.0 6 K4 2.0 b 3.0 7 K1 3.0 c 1.0 8 K2 3.0 c 2.0 9 K4 3.0 c 3.0 10 K1 NaN d 1.0 11 K2 4.0 d 2.0 12 K4 4.0 d 3.0 Merging asof# A merge_asof() is similar to an ordered left-join except that we match on nearest key rather than equal keys. For each row in the left DataFrame, we select the last row in the right DataFrame whose on key is less than the left’s key. Both DataFrames must be sorted by the key. Optionally an asof merge can perform a group-wise merge. This matches the by key equally, in addition to the nearest match on the on key. For example; we might have trades and quotes and we want to asof merge them. In [133]: trades = pd.DataFrame( .....: { .....: "time": pd.to_datetime( .....: [ .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.038", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.048", .....: ] .....: ), .....: "ticker": ["MSFT", "MSFT", "GOOG", "GOOG", "AAPL"], .....: "price": [51.95, 51.95, 720.77, 720.92, 98.00], .....: "quantity": [75, 155, 100, 100, 100], .....: }, .....: columns=["time", "ticker", "price", "quantity"], .....: ) .....: In [134]: quotes = pd.DataFrame( .....: { .....: "time": pd.to_datetime( .....: [ .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.030", .....: "20160525 13:30:00.041", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.049", .....: "20160525 13:30:00.072", .....: "20160525 13:30:00.075", .....: ] .....: ), .....: "ticker": ["GOOG", "MSFT", "MSFT", "MSFT", "GOOG", "AAPL", "GOOG", "MSFT"], .....: "bid": [720.50, 51.95, 51.97, 51.99, 720.50, 97.99, 720.50, 52.01], .....: "ask": [720.93, 51.96, 51.98, 52.00, 720.93, 98.01, 720.88, 52.03], .....: }, .....: columns=["time", "ticker", "bid", "ask"], .....: ) .....: In [135]: trades Out[135]: time ticker price quantity 0 2016-05-25 13:30:00.023 MSFT 51.95 75 1 2016-05-25 13:30:00.038 MSFT 51.95 155 2 2016-05-25 13:30:00.048 GOOG 720.77 100 3 2016-05-25 13:30:00.048 GOOG 720.92 100 4 2016-05-25 13:30:00.048 AAPL 98.00 100 In [136]: quotes Out[136]: time ticker bid ask 0 2016-05-25 13:30:00.023 GOOG 720.50 720.93 1 2016-05-25 13:30:00.023 MSFT 51.95 51.96 2 2016-05-25 13:30:00.030 MSFT 51.97 51.98 3 2016-05-25 13:30:00.041 MSFT 51.99 52.00 4 2016-05-25 13:30:00.048 GOOG 720.50 720.93 5 2016-05-25 13:30:00.049 AAPL 97.99 98.01 6 2016-05-25 13:30:00.072 GOOG 720.50 720.88 7 2016-05-25 13:30:00.075 MSFT 52.01 52.03 By default we are taking the asof of the quotes. In [137]: pd.merge_asof(trades, quotes, on="time", by="ticker") Out[137]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 51.95 51.96 1 2016-05-25 13:30:00.038 MSFT 51.95 155 51.97 51.98 2 2016-05-25 13:30:00.048 GOOG 720.77 100 720.50 720.93 3 2016-05-25 13:30:00.048 GOOG 720.92 100 720.50 720.93 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN We only asof within 2ms between the quote time and the trade time. In [138]: pd.merge_asof(trades, quotes, on="time", by="ticker", tolerance=pd.Timedelta("2ms")) Out[138]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 51.95 51.96 1 2016-05-25 13:30:00.038 MSFT 51.95 155 NaN NaN 2 2016-05-25 13:30:00.048 GOOG 720.77 100 720.50 720.93 3 2016-05-25 13:30:00.048 GOOG 720.92 100 720.50 720.93 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN We only asof within 10ms between the quote time and the trade time and we exclude exact matches on time. Note that though we exclude the exact matches (of the quotes), prior quotes do propagate to that point in time. In [139]: pd.merge_asof( .....: trades, .....: quotes, .....: on="time", .....: by="ticker", .....: tolerance=pd.Timedelta("10ms"), .....: allow_exact_matches=False, .....: ) .....: Out[139]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 NaN NaN 1 2016-05-25 13:30:00.038 MSFT 51.95 155 51.97 51.98 2 2016-05-25 13:30:00.048 GOOG 720.77 100 NaN NaN 3 2016-05-25 13:30:00.048 GOOG 720.92 100 NaN NaN 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN Comparing objects# The compare() and compare() methods allow you to compare two DataFrame or Series, respectively, and summarize their differences. This feature was added in V1.1.0. For example, you might want to compare two DataFrame and stack their differences side by side. In [140]: df = pd.DataFrame( .....: { .....: "col1": ["a", "a", "b", "b", "a"], .....: "col2": [1.0, 2.0, 3.0, np.nan, 5.0], .....: "col3": [1.0, 2.0, 3.0, 4.0, 5.0], .....: }, .....: columns=["col1", "col2", "col3"], .....: ) .....: In [141]: df Out[141]: col1 col2 col3 0 a 1.0 1.0 1 a 2.0 2.0 2 b 3.0 3.0 3 b NaN 4.0 4 a 5.0 5.0 In [142]: df2 = df.copy() In [143]: df2.loc[0, "col1"] = "c" In [144]: df2.loc[2, "col3"] = 4.0 In [145]: df2 Out[145]: col1 col2 col3 0 c 1.0 1.0 1 a 2.0 2.0 2 b 3.0 4.0 3 b NaN 4.0 4 a 5.0 5.0 In [146]: df.compare(df2) Out[146]: col1 col3 self other self other 0 a c NaN NaN 2 NaN NaN 3.0 4.0 By default, if two corresponding values are equal, they will be shown as NaN. Furthermore, if all values in an entire row / column, the row / column will be omitted from the result. The remaining differences will be aligned on columns. If you wish, you may choose to stack the differences on rows. In [147]: df.compare(df2, align_axis=0) Out[147]: col1 col3 0 self a NaN other c NaN 2 self NaN 3.0 other NaN 4.0 If you wish to keep all original rows and columns, set keep_shape argument to True. In [148]: df.compare(df2, keep_shape=True) Out[148]: col1 col2 col3 self other self other self other 0 a c NaN NaN NaN NaN 1 NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN 3.0 4.0 3 NaN NaN NaN NaN NaN NaN 4 NaN NaN NaN NaN NaN NaN You may also keep all the original values even if they are equal. In [149]: df.compare(df2, keep_shape=True, keep_equal=True) Out[149]: col1 col2 col3 self other self other self other 0 a c 1.0 1.0 1.0 1.0 1 a a 2.0 2.0 2.0 2.0 2 b b 3.0 3.0 3.0 4.0 3 b b NaN NaN 4.0 4.0 4 a a 5.0 5.0 5.0 5.0
40
1,193
Time series data merge nearest right dataset has multiple same values I have two dataframes. The first is like a log while the second is like inputs. I want to combine this log and inputs based on their time columns. I tried using merge_asof but it only takes one input into the input dateframe. Here is an example. Dataframe Log Times, log: STARTTIME_Log 2020-05-28 21:57:27.000000 2020-05-28 06:35:20.000000 2020-05-28 19:51:39.000000 2020-05-28 20:43:23.000000 DataFrame İnput Times and Values, input: IO_Time IOName value 2020-05-28 21:57:35.037 A 79.65 2020-05-28 21:57:35.037 B 33.33 2020-05-28 06:35:22.037 A 27.53 2020-05-28 06:35:22.037 B 6.23 2020-05-28 09:30:20.037 A 43.50 2020-05-28 09:30:20.037 B 15.23 2020-05-28 19:51:40.037 A 100.00 2020-05-28 19:51:40.037 B 12.52 2020-05-28 20:43:25.037 A 56.43 2020-05-28 20:43:25.037 B 2.67 2020-05-28 22:32:56.037 A 23.45 2020-05-28 22:32:56.037 B 3.55 Expected Output: STARTTIME_Log IOName value 2020-05-28 21:57:27.000000 A 79.65 2020-05-28 21:57:27.000000 B 33.33 2020-05-28 06:35:20.000000 A 27.53 2020-05-28 06:35:20.000000 B 6.23 2020-05-28 19:51:39.000000 A 100.00 2020-05-28 19:51:39.000000 B 12.52 2020-05-28 20:43:23.000000 A 56.43 2020-05-28 20:43:23.000000 B 2.67 The output merges the log and input dataframes in the nearest time. The merge is done on STARTTIME_Log for the log dataframe and IO_Time on input. If there is too large a difference then the rows are dropped. How can I do that?
First, make sure that the IO_Time and STARTTIME_Log columns are of datetime type and are sorted (required to use merge_asof: log['STARTTIME_Log'] = pd.to_datetime(log['STARTTIME_Log']) input['IO_Time'] = pd.to_datetime(input['IO_Time']) log = log.sort_values('STARTTIME_Log') input = input.sort_values('IO_Time') Now, use merge_asof with input as the left dataframe and log as the right. Note that you need to specify an acceptable tolerance value (I set it to 10 seconds here): tol = pd.Timedelta('10s') df = pd.merge_asof(input, log, left_on='IO_Time', right_on='STARTTIME_Log', tolerance=tol, direction='nearest') df = df.dropna(subset=['STARTTIME_Log']).drop(columns='IO_Time') Afterwards, the rows that don't have a match in log are dropped and the IO_Time column is removed. Result: IOName value STARTTIME_Log 0 A 27.53 2020-05-28 06:35:20 1 B 6.23 2020-05-28 06:35:20 4 A 100.00 2020-05-28 19:51:39 5 B 12.52 2020-05-28 19:51:39 6 A 56.43 2020-05-28 20:43:23 7 B 2.67 2020-05-28 20:43:23 8 A 79.65 2020-05-28 21:57:27 9 B 33.33 2020-05-28 21:57:27
66,867,941
Getting an error when checking if values in a list match a column PANDAS
<p>I'm just wondering how one might overcome the below error.</p> <p><strong>AttributeError: 'list' object has no attribute 'str'</strong></p> <p>What I am trying to do is create a new column &quot;PrivilegedAccess&quot; and in this column I want to write &quot;True&quot; if any of the names in the first_names column match the ones outlined in the &quot;Search_for_These_values&quot; list and &quot;False&quot; if they don't</p> <p>Code</p> <pre><code>## Create list of Privileged accounts Search_for_These_values = ['Privileged','Diagnostics','SYS','service account'] #creating list pattern = '|'.join(Search_for_These_values) # joining list for comparision PrivilegedAccounts_DF['PrivilegedAccess'] = PrivilegedAccounts_DF.columns=[['first_name']].str.contains(pattern) PrivilegedAccounts_DF['PrivilegedAccess'] = PrivilegedAccounts_DF['PrivilegedAccess'].map({True: 'True', False: 'False'}) </code></pre> <p>SAMPLE DATA:</p> <pre><code> uid last_name first_name language role email_address department 0 121 Chad Diagnostics English Team Lead Michael.chad@gmail.com Data Scientist 1 253 Montegu Paulo Spanish CIO Paulo.Montegu@gmail.com Marketing 2 545 Mitchel Susan English Team Lead Susan.Mitchel@gmail.com Data Scientist 3 555 Vuvko Matia Polish Marketing Lead Matia.Vuvko@gmail.com Marketing 4 568 Sisk Ivan English Supply Chain Lead Ivan.Sisk@gmail.com Supply Chain 5 475 Andrea Patrice Spanish Sales Graduate Patrice.Andrea@gmail.com Sales 6 365 Akkinapalli Cherifa French Supply Chain Assistance Cherifa.Akkinapalli@gmail.com Supply Chain </code></pre> <p>Note that the dtype of the first_name column is &quot;object&quot; and the dataframe is multi index (not sure how to change from multi index)</p> <p>Many thanks</p>
66,867,973
"2021-03-30T09:09:32.117000"
2
null
1
44
python|pandas
<p>It seems you need select one column for <code>str.contains</code> and then use map or convert boolean to strings:</p> <pre><code>Search_for_These_values = ['Privileged','Diagnostics','SYS','service account'] #creating list pattern = '|'.join(Search_for_These_values) PrivilegedAccounts_DF = pd.DataFrame({'first_name':['Privileged 111', 'aaa SYS', 'sss']}) print (PrivilegedAccounts_DF.columns) Index(['first_name'], dtype='object') print (PrivilegedAccounts_DF.loc[0, 'first_name']) Privileged 111 print (type(PrivilegedAccounts_DF.loc[0, 'first_name'])) &lt;class 'str'&gt; </code></pre> <hr /> <pre><code>PrivilegedAccounts_DF['PrivilegedAccess'] = PrivilegedAccounts_DF['first_name'].str.contains(pattern).astype(str) print (PrivilegedAccounts_DF) first_name PrivilegedAccess 0 Privileged 111 True 1 aaa SYS True 2 sss False </code></pre> <p>EDIT:</p> <p>There is problem one level MultiIndex, need:</p> <pre><code>PrivilegedAccounts_DF = pd.DataFrame({'first_name':['Privileged 111', 'aaa SYS', 'sss']}) #simulate problem PrivilegedAccounts_DF.columns = [PrivilegedAccounts_DF.columns.tolist()] print (PrivilegedAccounts_DF) first_name 0 Privileged 111 1 aaa SYS 2 sss #check columns print (PrivilegedAccounts_DF.columns) MultiIndex([('first_name',)], ) </code></pre> <p>Solution is join values, e.g. by empty string:</p> <pre><code>PrivilegedAccounts_DF.columns = PrivilegedAccounts_DF.columns.map(''.join) </code></pre> <p>So now columns names are correct:</p> <pre><code>print (PrivilegedAccounts_DF.columns) Index(['first_name'], dtype='object') PrivilegedAccounts_DF['PrivilegedAccess'] = PrivilegedAccounts_DF['first_name'].str.contains(pattern).astype(str) print (PrivilegedAccounts_DF) </code></pre>
"2021-03-30T09:11:26.167000"
0
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.isin.html
It seems you need select one column for str.contains and then use map or convert boolean to strings: Search_for_These_values = ['Privileged','Diagnostics','SYS','service account'] #creating list pattern = '|'.join(Search_for_These_values) PrivilegedAccounts_DF = pd.DataFrame({'first_name':['Privileged 111', 'aaa SYS', 'sss']}) print (PrivilegedAccounts_DF.columns) Index(['first_name'], dtype='object') print (PrivilegedAccounts_DF.loc[0, 'first_name']) Privileged 111 print (type(PrivilegedAccounts_DF.loc[0, 'first_name'])) <class 'str'> PrivilegedAccounts_DF['PrivilegedAccess'] = PrivilegedAccounts_DF['first_name'].str.contains(pattern).astype(str) print (PrivilegedAccounts_DF) first_name PrivilegedAccess 0 Privileged 111 True 1 aaa SYS True 2 sss False EDIT: There is problem one level MultiIndex, need: PrivilegedAccounts_DF = pd.DataFrame({'first_name':['Privileged 111', 'aaa SYS', 'sss']}) #simulate problem PrivilegedAccounts_DF.columns = [PrivilegedAccounts_DF.columns.tolist()] print (PrivilegedAccounts_DF) first_name 0 Privileged 111 1 aaa SYS 2 sss #check columns print (PrivilegedAccounts_DF.columns) MultiIndex([('first_name',)], ) Solution is join values, e.g. by empty string: PrivilegedAccounts_DF.columns = PrivilegedAccounts_DF.columns.map(''.join) So now columns names are correct: print (PrivilegedAccounts_DF.columns) Index(['first_name'], dtype='object') PrivilegedAccounts_DF['PrivilegedAccess'] = PrivilegedAccounts_DF['first_name'].str.contains(pattern).astype(str) print (PrivilegedAccounts_DF)
0
1,851
Getting an error when checking if values in a list match a column PANDAS I'm just wondering how one might overcome the below error. AttributeError: 'list' object has no attribute 'str' What I am trying to do is create a new column "PrivilegedAccess" and in this column I want to write "True" if any of the names in the first_names column match the ones outlined in the "Search_for_These_values" list and "False" if they don't Code ## Create list of Privileged accounts Search_for_These_values = ['Privileged','Diagnostics','SYS','service account'] #creating list pattern = '|'.join(Search_for_These_values) # joining list for comparision PrivilegedAccounts_DF['PrivilegedAccess'] = PrivilegedAccounts_DF.columns=[['first_name']].str.contains(pattern) PrivilegedAccounts_DF['PrivilegedAccess'] = PrivilegedAccounts_DF['PrivilegedAccess'].map({True: 'True', False: 'False'}) SAMPLE DATA: uid last_name first_name language role email_address department 0 121 Chad Diagnostics English Team Lead Michael.chad@gmail.com Data Scientist 1 253 Montegu Paulo Spanish CIO Paulo.Montegu@gmail.com Marketing 2 545 Mitchel Susan English Team Lead Susan.Mitchel@gmail.com Data Scientist 3 555 Vuvko Matia Polish Marketing Lead Matia.Vuvko@gmail.com Marketing 4 568 Sisk Ivan English Supply Chain Lead Ivan.Sisk@gmail.com Supply Chain 5 475 Andrea Patrice Spanish Sales Graduate Patrice.Andrea@gmail.com Sales 6 365 Akkinapalli Cherifa French Supply Chain Assistance Cherifa.Akkinapalli@gmail.com Supply Chain Note that the dtype of the first_name column is "object" and the dataframe is multi index (not sure how to change from multi index) Many thanks
It seems you need select one column for str.contains and then use map or convert boolean to strings: Search_for_These_values = ['Privileged','Diagnostics','SYS','service account'] #creating list pattern = '|'.join(Search_for_These_values) PrivilegedAccounts_DF = pd.DataFrame({'first_name':['Privileged 111', 'aaa SYS', 'sss']}) print (PrivilegedAccounts_DF.columns) Index(['first_name'], dtype='object') print (PrivilegedAccounts_DF.loc[0, 'first_name']) Privileged 111 print (type(PrivilegedAccounts_DF.loc[0, 'first_name'])) <class 'str'> PrivilegedAccounts_DF['PrivilegedAccess'] = PrivilegedAccounts_DF['first_name'].str.contains(pattern).astype(str) print (PrivilegedAccounts_DF) first_name PrivilegedAccess 0 Privileged 111 True 1 aaa SYS True 2 sss False EDIT: There is problem one level MultiIndex, need: PrivilegedAccounts_DF = pd.DataFrame({'first_name':['Privileged 111', 'aaa SYS', 'sss']}) #simulate problem PrivilegedAccounts_DF.columns = [PrivilegedAccounts_DF.columns.tolist()] print (PrivilegedAccounts_DF) first_name 0 Privileged 111 1 aaa SYS 2 sss #check columns print (PrivilegedAccounts_DF.columns) MultiIndex([('first_name',)], ) Solution is join values, e.g. by empty string: PrivilegedAccounts_DF.columns = PrivilegedAccounts_DF.columns.map(''.join) So now columns names are correct: print (PrivilegedAccounts_DF.columns) Index(['first_name'], dtype='object') PrivilegedAccounts_DF['PrivilegedAccess'] = PrivilegedAccounts_DF['first_name'].str.contains(pattern).astype(str) print (PrivilegedAccounts_DF)
59,822,568
How i can change invalid string pattern with default string in dataframe?
<p>i have a dataframe like below.</p> <pre><code>name birthdate ----------------- john 21011990 steve 14021986 bob alice 13020198 </code></pre> <p>i want to detect invalid value in birthdate column then change value.</p> <p>the birthdate column use date format is "DDMMYYYY" . but in dataframe have a invalid format also as "13020198","". i want to change invalid data to 31125000 . </p> <p>i want result like below</p> <pre><code>name birthdate ----------------- john 21011990 steve 14021986 bob 31125000 alice 31125000 </code></pre> <p>thank you </p>
59,822,864
"2020-01-20T11:43:04.543000"
3
null
1
57
python|pandas
<p>You can first create non-valid date mask and then update their values:</p> <pre><code>mask = df.birthdate.apply(lambda x: pd.to_datetime(x, format='%d%m%Y', errors='coerce')).isna() df.loc[mask, 'birthdate'] = 31125000 name birthdate 0 john 21011990 1 steve 14021986 2 bob 31125000 3 alice 31125000 </code></pre>
"2020-01-20T12:01:26.027000"
0
https://pandas.pydata.org/docs/reference/api/pandas.to_datetime.html
pandas.to_datetime# pandas.to_datetime# pandas.to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False, utc=None, format=None, exact=True, unit=None, infer_datetime_format=False, origin='unix', cache=True)[source]# You can first create non-valid date mask and then update their values: mask = df.birthdate.apply(lambda x: pd.to_datetime(x, format='%d%m%Y', errors='coerce')).isna() df.loc[mask, 'birthdate'] = 31125000 name birthdate 0 john 21011990 1 steve 14021986 2 bob 31125000 3 alice 31125000 Convert argument to datetime. This function converts a scalar, array-like, Series or DataFrame/dict-like to a pandas datetime object. Parameters argint, float, str, datetime, list, tuple, 1-d array, Series, DataFrame/dict-likeThe object to convert to a datetime. If a DataFrame is provided, the method expects minimally the following columns: "year", "month", "day". errors{‘ignore’, ‘raise’, ‘coerce’}, default ‘raise’ If 'raise', then invalid parsing will raise an exception. If 'coerce', then invalid parsing will be set as NaT. If 'ignore', then invalid parsing will return the input. dayfirstbool, default FalseSpecify a date parse order if arg is str or is list-like. If True, parses dates with the day first, e.g. "10/11/12" is parsed as 2012-11-10. Warning dayfirst=True is not strict, but will prefer to parse with day first. If a delimited date string cannot be parsed in accordance with the given dayfirst option, e.g. to_datetime(['31-12-2021']), then a warning will be shown. yearfirstbool, default FalseSpecify a date parse order if arg is str or is list-like. If True parses dates with the year first, e.g. "10/11/12" is parsed as 2010-11-12. If both dayfirst and yearfirst are True, yearfirst is preceded (same as dateutil). Warning yearfirst=True is not strict, but will prefer to parse with year first. utcbool, default NoneControl timezone-related parsing, localization and conversion. If True, the function always returns a timezone-aware UTC-localized Timestamp, Series or DatetimeIndex. To do this, timezone-naive inputs are localized as UTC, while timezone-aware inputs are converted to UTC. If False (default), inputs will not be coerced to UTC. Timezone-naive inputs will remain naive, while timezone-aware ones will keep their time offsets. Limitations exist for mixed offsets (typically, daylight savings), see Examples section for details. See also: pandas general documentation about timezone conversion and localization. formatstr, default NoneThe strftime to parse time, e.g. "%d/%m/%Y". Note that "%f" will parse all the way up to nanoseconds. See strftime documentation for more information on choices. exactbool, default TrueControl how format is used: If True, require an exact format match. If False, allow the format to match anywhere in the target string. unitstr, default ‘ns’The unit of the arg (D,s,ms,us,ns) denote the unit, which is an integer or float number. This will be based off the origin. Example, with unit='ms' and origin='unix', this would calculate the number of milliseconds to the unix epoch start. infer_datetime_formatbool, default FalseIf True and no format is given, attempt to infer the format of the datetime strings based on the first non-NaN element, and if it can be inferred, switch to a faster method of parsing them. In some cases this can increase the parsing speed by ~5-10x. originscalar, default ‘unix’Define the reference date. The numeric values would be parsed as number of units (defined by unit) since this reference date. If 'unix' (or POSIX) time; origin is set to 1970-01-01. If 'julian', unit must be 'D', and origin is set to beginning of Julian Calendar. Julian day number 0 is assigned to the day starting at noon on January 1, 4713 BC. If Timestamp convertible, origin is set to Timestamp identified by origin. cachebool, default TrueIf True, use a cache of unique, converted dates to apply the datetime conversion. May produce significant speed-up when parsing duplicate date strings, especially ones with timezone offsets. The cache is only used when there are at least 50 values. The presence of out-of-bounds values will render the cache unusable and may slow down parsing. Changed in version 0.25.0: changed default value from False to True. Returns datetimeIf parsing succeeded. Return type depends on input (types in parenthesis correspond to fallback in case of unsuccessful timezone or out-of-range timestamp parsing): scalar: Timestamp (or datetime.datetime) array-like: DatetimeIndex (or Series with object dtype containing datetime.datetime) Series: Series of datetime64 dtype (or Series of object dtype containing datetime.datetime) DataFrame: Series of datetime64 dtype (or Series of object dtype containing datetime.datetime) Raises ParserErrorWhen parsing a date from string fails. ValueErrorWhen another datetime conversion error happens. For example when one of ‘year’, ‘month’, day’ columns is missing in a DataFrame, or when a Timezone-aware datetime.datetime is found in an array-like of mixed time offsets, and utc=False. See also DataFrame.astypeCast argument to a specified dtype. to_timedeltaConvert argument to timedelta. convert_dtypesConvert dtypes. Notes Many input types are supported, and lead to different output types: scalars can be int, float, str, datetime object (from stdlib datetime module or numpy). They are converted to Timestamp when possible, otherwise they are converted to datetime.datetime. None/NaN/null scalars are converted to NaT. array-like can contain int, float, str, datetime objects. They are converted to DatetimeIndex when possible, otherwise they are converted to Index with object dtype, containing datetime.datetime. None/NaN/null entries are converted to NaT in both cases. Series are converted to Series with datetime64 dtype when possible, otherwise they are converted to Series with object dtype, containing datetime.datetime. None/NaN/null entries are converted to NaT in both cases. DataFrame/dict-like are converted to Series with datetime64 dtype. For each row a datetime is created from assembling the various dataframe columns. Column keys can be common abbreviations like [‘year’, ‘month’, ‘day’, ‘minute’, ‘second’, ‘ms’, ‘us’, ‘ns’]) or plurals of the same. The following causes are responsible for datetime.datetime objects being returned (possibly inside an Index or a Series with object dtype) instead of a proper pandas designated type (Timestamp, DatetimeIndex or Series with datetime64 dtype): when any input element is before Timestamp.min or after Timestamp.max, see timestamp limitations. when utc=False (default) and the input is an array-like or Series containing mixed naive/aware datetime, or aware with mixed time offsets. Note that this happens in the (quite frequent) situation when the timezone has a daylight savings policy. In that case you may wish to use utc=True. Examples Handling various input formats Assembling a datetime from multiple columns of a DataFrame. The keys can be common abbreviations like [‘year’, ‘month’, ‘day’, ‘minute’, ‘second’, ‘ms’, ‘us’, ‘ns’]) or plurals of the same >>> df = pd.DataFrame({'year': [2015, 2016], ... 'month': [2, 3], ... 'day': [4, 5]}) >>> pd.to_datetime(df) 0 2015-02-04 1 2016-03-05 dtype: datetime64[ns] Passing infer_datetime_format=True can often-times speedup a parsing if its not an ISO8601 format exactly, but in a regular format. >>> s = pd.Series(['3/11/2000', '3/12/2000', '3/13/2000'] * 1000) >>> s.head() 0 3/11/2000 1 3/12/2000 2 3/13/2000 3 3/11/2000 4 3/12/2000 dtype: object >>> %timeit pd.to_datetime(s, infer_datetime_format=True) 100 loops, best of 3: 10.4 ms per loop >>> %timeit pd.to_datetime(s, infer_datetime_format=False) 1 loop, best of 3: 471 ms per loop Using a unix epoch time >>> pd.to_datetime(1490195805, unit='s') Timestamp('2017-03-22 15:16:45') >>> pd.to_datetime(1490195805433502912, unit='ns') Timestamp('2017-03-22 15:16:45.433502912') Warning For float arg, precision rounding might happen. To prevent unexpected behavior use a fixed-width exact type. Using a non-unix epoch origin >>> pd.to_datetime([1, 2, 3], unit='D', ... origin=pd.Timestamp('1960-01-01')) DatetimeIndex(['1960-01-02', '1960-01-03', '1960-01-04'], dtype='datetime64[ns]', freq=None) Non-convertible date/times If a date does not meet the timestamp limitations, passing errors='ignore' will return the original input instead of raising any exception. Passing errors='coerce' will force an out-of-bounds date to NaT, in addition to forcing non-dates (or non-parseable dates) to NaT. >>> pd.to_datetime('13000101', format='%Y%m%d', errors='ignore') datetime.datetime(1300, 1, 1, 0, 0) >>> pd.to_datetime('13000101', format='%Y%m%d', errors='coerce') NaT Timezones and time offsets The default behaviour (utc=False) is as follows: Timezone-naive inputs are converted to timezone-naive DatetimeIndex: >>> pd.to_datetime(['2018-10-26 12:00', '2018-10-26 13:00:15']) DatetimeIndex(['2018-10-26 12:00:00', '2018-10-26 13:00:15'], dtype='datetime64[ns]', freq=None) Timezone-aware inputs with constant time offset are converted to timezone-aware DatetimeIndex: >>> pd.to_datetime(['2018-10-26 12:00 -0500', '2018-10-26 13:00 -0500']) DatetimeIndex(['2018-10-26 12:00:00-05:00', '2018-10-26 13:00:00-05:00'], dtype='datetime64[ns, pytz.FixedOffset(-300)]', freq=None) However, timezone-aware inputs with mixed time offsets (for example issued from a timezone with daylight savings, such as Europe/Paris) are not successfully converted to a DatetimeIndex. Instead a simple Index containing datetime.datetime objects is returned: >>> pd.to_datetime(['2020-10-25 02:00 +0200', '2020-10-25 04:00 +0100']) Index([2020-10-25 02:00:00+02:00, 2020-10-25 04:00:00+01:00], dtype='object') A mix of timezone-aware and timezone-naive inputs is converted to a timezone-aware DatetimeIndex if the offsets of the timezone-aware are constant: >>> from datetime import datetime >>> pd.to_datetime(["2020-01-01 01:00 -01:00", datetime(2020, 1, 1, 3, 0)]) DatetimeIndex(['2020-01-01 01:00:00-01:00', '2020-01-01 02:00:00-01:00'], dtype='datetime64[ns, pytz.FixedOffset(-60)]', freq=None) Setting utc=True solves most of the above issues: Timezone-naive inputs are localized as UTC >>> pd.to_datetime(['2018-10-26 12:00', '2018-10-26 13:00'], utc=True) DatetimeIndex(['2018-10-26 12:00:00+00:00', '2018-10-26 13:00:00+00:00'], dtype='datetime64[ns, UTC]', freq=None) Timezone-aware inputs are converted to UTC (the output represents the exact same datetime, but viewed from the UTC time offset +00:00). >>> pd.to_datetime(['2018-10-26 12:00 -0530', '2018-10-26 12:00 -0500'], ... utc=True) DatetimeIndex(['2018-10-26 17:30:00+00:00', '2018-10-26 17:00:00+00:00'], dtype='datetime64[ns, UTC]', freq=None) Inputs can contain both naive and aware, string or datetime, the above rules still apply >>> from datetime import timezone, timedelta >>> pd.to_datetime(['2018-10-26 12:00', '2018-10-26 12:00 -0530', ... datetime(2020, 1, 1, 18), ... datetime(2020, 1, 1, 18, ... tzinfo=timezone(-timedelta(hours=1)))], ... utc=True) DatetimeIndex(['2018-10-26 12:00:00+00:00', '2018-10-26 17:30:00+00:00', '2020-01-01 18:00:00+00:00', '2020-01-01 19:00:00+00:00'], dtype='datetime64[ns, UTC]', freq=None)
228
540
How i can change invalid string pattern with default string in dataframe? i have a dataframe like below. name birthdate ----------------- john 21011990 steve 14021986 bob alice 13020198 i want to detect invalid value in birthdate column then change value. the birthdate column use date format is "DDMMYYYY" . but in dataframe have a invalid format also as "13020198","". i want to change invalid data to 31125000 . i want result like below name birthdate ----------------- john 21011990 steve 14021986 bob 31125000 alice 31125000 thank you
You can first create non-valid date mask and then update their values: mask = df.birthdate.apply(lambda x: pd.to_datetime(x, format='%d%m%Y', errors='coerce')).isna() df.loc[mask, 'birthdate'] = 31125000 name birthdate 0 john 21011990 1 steve 14021986 2 bob 31125000 3 alice 31125000
62,441,689
Pandas Groupby Ranges when ranges are not continuous
<p>I have a dataframe that looks like this:</p> <pre><code>id | A | B | C ------------------------------ 1 | 0.1 | 1.2 | 100 2 | 0.2 | 1.4 | 200 3 | 0.3 | 1.6 | 300 4 | 0.4 | 1.8 | 400 5 | 0.5 | 2.0 | 500 6 | 0.6 | 2.2 | 600 7 | 0.7 | 2.4 | 700 8 | 0.8 | 2.6 | 800 9 | 0.9 | 2.8 | 900 10 | 1.0 | 3.0 | 1000 11 | 1.1 | 3.2 | 1100 </code></pre> <p>I want to use groupby to this dataframe to group it by a range of increments for the column 'A' or 'B'. But the ranges are not consecutive nor exclusive, they are like this:</p> <pre><code>(0,1.1.1] (0.2,1.1] (0.4,1.1] (0.6,1.1] (0.8,1.1] (1.0,1.1] </code></pre> <p>Then apply it some functions (mean and sum), so my end result will be kind of like this:</p> <pre><code> | A_mean | B_mean | C_sum A_bins | | | ------------------------------------- (0,1.1.1] | 0.6 | 2.2 | 6600 (0.2,1.1] | 0.7 | 2.4 | 6300 (0.4,1.1] | 0.8 | 2.6 | 5600 (0.6,1.1] | 0.9 | 2.8 | 4500 (0.8,1.1] | 1.0 | 3.0 | 3000 (1.0,1.1] | 1.1 | 3.2 | 1100 </code></pre> <p>I was thinking of trying <code>groupby</code> with <code>pd.cut()</code> but I think <code>pd.cut()</code> won't be able to work with those intervals.</p> <p>So, is there any way that I can achieve that with those kinds of ranges? Or any kind of ranges that are not in the form of something like: <code>np.arange(0, 1.1+0.05, 0.2)</code></p> <p>Thank you all</p>
62,442,506
"2020-06-18T03:07:44.227000"
2
null
1
61
python|pandas
<p>How about just using the apply function to generate the metrics you need.</p> <pre><code>df2 = pd.DataFrame({'A_bins': [(0.1,1.1), (0.2,1.1), (0.4,1.1), (0.6,1.1), (0.8,1.1), (1.0,1.1)]}) def get_sum(row): # this is where the logic for your metrics goes return df.loc[(row['A_bins'][0]&lt;df['A']) &amp; (row['A_bins'][1]&gt;=df['A']),'C'].sum() df2['C_sum'] = df2.apply(get_sum, axis = 1) print (df2) </code></pre> <p>Output:</p> <pre><code> A_bins C_sum 0 (0.1, 1.1) 6500.0 1 (0.2, 1.1) 6300.0 2 (0.4, 1.1) 5600.0 3 (0.6, 1.1) 4500.0 4 (0.8, 1.1) 3000.0 5 (1.0, 1.1) 1100.0 </code></pre>
"2020-06-18T04:39:35.983000"
0
https://pandas.pydata.org/docs/reference/api/pandas.cut.html
pandas.cut# pandas.cut# pandas.cut(x, bins, right=True, labels=None, retbins=False, precision=3, include_lowest=False, duplicates='raise', ordered=True)[source]# Bin values into discrete intervals. Use cut when you need to segment and sort data values into bins. This function is also useful for going from a continuous variable to a How about just using the apply function to generate the metrics you need. df2 = pd.DataFrame({'A_bins': [(0.1,1.1), (0.2,1.1), (0.4,1.1), (0.6,1.1), (0.8,1.1), (1.0,1.1)]}) def get_sum(row): # this is where the logic for your metrics goes return df.loc[(row['A_bins'][0]<df['A']) & (row['A_bins'][1]>=df['A']),'C'].sum() df2['C_sum'] = df2.apply(get_sum, axis = 1) print (df2) Output: A_bins C_sum 0 (0.1, 1.1) 6500.0 1 (0.2, 1.1) 6300.0 2 (0.4, 1.1) 5600.0 3 (0.6, 1.1) 4500.0 4 (0.8, 1.1) 3000.0 5 (1.0, 1.1) 1100.0 categorical variable. For example, cut could convert ages to groups of age ranges. Supports binning into an equal number of bins, or a pre-specified array of bins. Parameters xarray-likeThe input array to be binned. Must be 1-dimensional. binsint, sequence of scalars, or IntervalIndexThe criteria to bin by. int : Defines the number of equal-width bins in the range of x. The range of x is extended by .1% on each side to include the minimum and maximum values of x. sequence of scalars : Defines the bin edges allowing for non-uniform width. No extension of the range of x is done. IntervalIndex : Defines the exact bins to be used. Note that IntervalIndex for bins must be non-overlapping. rightbool, default TrueIndicates whether bins includes the rightmost edge or not. If right == True (the default), then the bins [1, 2, 3, 4] indicate (1,2], (2,3], (3,4]. This argument is ignored when bins is an IntervalIndex. labelsarray or False, default NoneSpecifies the labels for the returned bins. Must be the same length as the resulting bins. If False, returns only integer indicators of the bins. This affects the type of the output container (see below). This argument is ignored when bins is an IntervalIndex. If True, raises an error. When ordered=False, labels must be provided. retbinsbool, default FalseWhether to return the bins or not. Useful when bins is provided as a scalar. precisionint, default 3The precision at which to store and display the bins labels. include_lowestbool, default FalseWhether the first interval should be left-inclusive or not. duplicates{default ‘raise’, ‘drop’}, optionalIf bin edges are not unique, raise ValueError or drop non-uniques. orderedbool, default TrueWhether the labels are ordered or not. Applies to returned types Categorical and Series (with Categorical dtype). If True, the resulting categorical will be ordered. If False, the resulting categorical will be unordered (labels must be provided). New in version 1.1.0. Returns outCategorical, Series, or ndarrayAn array-like object representing the respective bin for each value of x. The type depends on the value of labels. None (default) : returns a Series for Series x or a Categorical for all other inputs. The values stored within are Interval dtype. sequence of scalars : returns a Series for Series x or a Categorical for all other inputs. The values stored within are whatever the type in the sequence is. False : returns an ndarray of integers. binsnumpy.ndarray or IntervalIndex.The computed or specified bins. Only returned when retbins=True. For scalar or sequence bins, this is an ndarray with the computed bins. If set duplicates=drop, bins will drop non-unique bin. For an IntervalIndex bins, this is equal to bins. See also qcutDiscretize variable into equal-sized buckets based on rank or based on sample quantiles. CategoricalArray type for storing data that come from a fixed set of values. SeriesOne-dimensional array with axis labels (including time series). IntervalIndexImmutable Index implementing an ordered, sliceable set. Notes Any NA values will be NA in the result. Out of bounds values will be NA in the resulting Series or Categorical object. Reference the user guide for more examples. Examples Discretize into three equal-sized bins. >>> pd.cut(np.array([1, 7, 5, 4, 6, 3]), 3) ... [(0.994, 3.0], (5.0, 7.0], (3.0, 5.0], (3.0, 5.0], (5.0, 7.0], ... Categories (3, interval[float64, right]): [(0.994, 3.0] < (3.0, 5.0] ... >>> pd.cut(np.array([1, 7, 5, 4, 6, 3]), 3, retbins=True) ... ([(0.994, 3.0], (5.0, 7.0], (3.0, 5.0], (3.0, 5.0], (5.0, 7.0], ... Categories (3, interval[float64, right]): [(0.994, 3.0] < (3.0, 5.0] ... array([0.994, 3. , 5. , 7. ])) Discovers the same bins, but assign them specific labels. Notice that the returned Categorical’s categories are labels and is ordered. >>> pd.cut(np.array([1, 7, 5, 4, 6, 3]), ... 3, labels=["bad", "medium", "good"]) ['bad', 'good', 'medium', 'medium', 'good', 'bad'] Categories (3, object): ['bad' < 'medium' < 'good'] ordered=False will result in unordered categories when labels are passed. This parameter can be used to allow non-unique labels: >>> pd.cut(np.array([1, 7, 5, 4, 6, 3]), 3, ... labels=["B", "A", "B"], ordered=False) ['B', 'B', 'A', 'A', 'B', 'B'] Categories (2, object): ['A', 'B'] labels=False implies you just want the bins back. >>> pd.cut([0, 1, 1, 2], bins=4, labels=False) array([0, 1, 1, 3]) Passing a Series as an input returns a Series with categorical dtype: >>> s = pd.Series(np.array([2, 4, 6, 8, 10]), ... index=['a', 'b', 'c', 'd', 'e']) >>> pd.cut(s, 3) ... a (1.992, 4.667] b (1.992, 4.667] c (4.667, 7.333] d (7.333, 10.0] e (7.333, 10.0] dtype: category Categories (3, interval[float64, right]): [(1.992, 4.667] < (4.667, ... Passing a Series as an input returns a Series with mapping value. It is used to map numerically to intervals based on bins. >>> s = pd.Series(np.array([2, 4, 6, 8, 10]), ... index=['a', 'b', 'c', 'd', 'e']) >>> pd.cut(s, [0, 2, 4, 6, 8, 10], labels=False, retbins=True, right=False) ... (a 1.0 b 2.0 c 3.0 d 4.0 e NaN dtype: float64, array([ 0, 2, 4, 6, 8, 10])) Use drop optional when bins is not unique >>> pd.cut(s, [0, 2, 4, 6, 10, 10], labels=False, retbins=True, ... right=False, duplicates='drop') ... (a 1.0 b 2.0 c 3.0 d 3.0 e NaN dtype: float64, array([ 0, 2, 4, 6, 10])) Passing an IntervalIndex for bins results in those categories exactly. Notice that values not covered by the IntervalIndex are set to NaN. 0 is to the left of the first bin (which is closed on the right), and 1.5 falls between two bins. >>> bins = pd.IntervalIndex.from_tuples([(0, 1), (2, 3), (4, 5)]) >>> pd.cut([0, 0.5, 1.5, 2.5, 4.5], bins) [NaN, (0.0, 1.0], NaN, (2.0, 3.0], (4.0, 5.0]] Categories (3, interval[int64, right]): [(0, 1] < (2, 3] < (4, 5]]
338
883
Pandas Groupby Ranges when ranges are not continuous I have a dataframe that looks like this: id | A | B | C ------------------------------ 1 | 0.1 | 1.2 | 100 2 | 0.2 | 1.4 | 200 3 | 0.3 | 1.6 | 300 4 | 0.4 | 1.8 | 400 5 | 0.5 | 2.0 | 500 6 | 0.6 | 2.2 | 600 7 | 0.7 | 2.4 | 700 8 | 0.8 | 2.6 | 800 9 | 0.9 | 2.8 | 900 10 | 1.0 | 3.0 | 1000 11 | 1.1 | 3.2 | 1100 I want to use groupby to this dataframe to group it by a range of increments for the column 'A' or 'B'. But the ranges are not consecutive nor exclusive, they are like this: (0,1.1.1] (0.2,1.1] (0.4,1.1] (0.6,1.1] (0.8,1.1] (1.0,1.1] Then apply it some functions (mean and sum), so my end result will be kind of like this: | A_mean | B_mean | C_sum A_bins | | | ------------------------------------- (0,1.1.1] | 0.6 | 2.2 | 6600 (0.2,1.1] | 0.7 | 2.4 | 6300 (0.4,1.1] | 0.8 | 2.6 | 5600 (0.6,1.1] | 0.9 | 2.8 | 4500 (0.8,1.1] | 1.0 | 3.0 | 3000 (1.0,1.1] | 1.1 | 3.2 | 1100 I was thinking of trying groupby with pd.cut() but I think pd.cut() won't be able to work with those intervals. So, is there any way that I can achieve that with those kinds of ranges? Or any kind of ranges that are not in the form of something like: np.arange(0, 1.1+0.05, 0.2) Thank you all
How about just using the apply function to generate the metrics you need. df2 = pd.DataFrame({'A_bins': [(0.1,1.1), (0.2,1.1), (0.4,1.1), (0.6,1.1), (0.8,1.1), (1.0,1.1)]}) def get_sum(row): # this is where the logic for your metrics goes return df.loc[(row['A_bins'][0]<df['A']) & (row['A_bins'][1]>=df['A']),'C'].sum() df2['C_sum'] = df2.apply(get_sum, axis = 1) print (df2) Output: A_bins C_sum 0 (0.1, 1.1) 6500.0 1 (0.2, 1.1) 6300.0 2 (0.4, 1.1) 5600.0 3 (0.6, 1.1) 4500.0 4 (0.8, 1.1) 3000.0 5 (1.0, 1.1) 1100.0
69,511,132
How to add positive and negative increments to every row based on a specific date?
<p>I have a pandas df which has 2 columns such as <code>Date, First_Date (constant)</code>.</p> <p>I am trying to add a new column in which the value will be 0 where First_Date=Date. Then, all rows below that instance should increment in a negative way such as -1, -2, -3 etc.. and same should be true for rows above should increment in a positive way such as 1,2,3,4 etc. Please see attachment for concept visualization.</p> <p>I am not sure if there is a pandas function to do this or if a function is better in this case. Any guidance would be great.</p> <p><a href="https://i.stack.imgur.com/bea18.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bea18.png" alt="enter image description here" /></a></p>
69,511,213
"2021-10-09T23:05:50.443000"
2
null
0
101
python|pandas
<pre><code>&gt;&gt;&gt; df = pd.DataFrame({'Date':pd.date_range('2020-01-01', '2020-01-18')}) &gt;&gt;&gt; df Date 0 2020-01-01 1 2020-01-02 2 2020-01-03 3 2020-01-04 4 2020-01-05 5 2020-01-06 6 2020-01-07 7 2020-01-08 8 2020-01-09 9 2020-01-10 10 2020-01-11 11 2020-01-12 12 2020-01-13 13 2020-01-14 14 2020-01-15 15 2020-01-16 16 2020-01-17 17 2020-01-18 </code></pre> <p>Checkout pandas <a href="https://pandas.pydata.org/docs/reference/api/pandas.Timestamp.html" rel="nofollow noreferrer">Timestamps</a> strings to DateTime etc. No reason to work with strings and ints.</p> <pre><code>&gt;&gt;&gt; df['index'] = df - pd.Timestamp('2020-01-11') &gt;&gt;&gt; df Date index 0 2020-01-01 -10 days 1 2020-01-02 -9 days 2 2020-01-03 -8 days 3 2020-01-04 -7 days 4 2020-01-05 -6 days 5 2020-01-06 -5 days 6 2020-01-07 -4 days 7 2020-01-08 -3 days 8 2020-01-09 -2 days 9 2020-01-10 -1 days 10 2020-01-11 0 days 11 2020-01-12 1 days 12 2020-01-13 2 days 13 2020-01-14 3 days 14 2020-01-15 4 days 15 2020-01-16 5 days 16 2020-01-17 6 days 17 2020-01-18 7 days </code></pre> <p>You can get your desired ints afterwards with:</p> <pre><code>&gt;&gt;&gt; df['index'].transform(lambda x: x.days) 0 -10 1 -9 2 -8 3 -7 4 -6 5 -5 6 -4 7 -3 8 -2 9 -1 10 0 11 1 12 2 13 3 14 4 15 5 16 6 17 7 </code></pre> <p>EDIT</p> <p>To answer more specifically since you have string dates you have to do the following first</p> <pre><code>df[['Date', 'First_Date']] = df[['Date', 'First_Date']].astype('datetime64[ns]') </code></pre> <p>then you can subtract the columns and get your result:</p> <pre><code>df['index'] = df['Date'] - df['First_Date'] </code></pre>
"2021-10-09T23:27:09.653000"
0
https://pandas.pydata.org/docs/reference/api/pandas.Index.shift.html
>>> df = pd.DataFrame({'Date':pd.date_range('2020-01-01', '2020-01-18')}) >>> df Date 0 2020-01-01 1 2020-01-02 2 2020-01-03 3 2020-01-04 4 2020-01-05 5 2020-01-06 6 2020-01-07 7 2020-01-08 8 2020-01-09 9 2020-01-10 10 2020-01-11 11 2020-01-12 12 2020-01-13 13 2020-01-14 14 2020-01-15 15 2020-01-16 16 2020-01-17 17 2020-01-18 Checkout pandas Timestamps strings to DateTime etc. No reason to work with strings and ints. >>> df['index'] = df - pd.Timestamp('2020-01-11') >>> df Date index 0 2020-01-01 -10 days 1 2020-01-02 -9 days 2 2020-01-03 -8 days 3 2020-01-04 -7 days 4 2020-01-05 -6 days 5 2020-01-06 -5 days 6 2020-01-07 -4 days 7 2020-01-08 -3 days 8 2020-01-09 -2 days 9 2020-01-10 -1 days 10 2020-01-11 0 days 11 2020-01-12 1 days 12 2020-01-13 2 days 13 2020-01-14 3 days 14 2020-01-15 4 days 15 2020-01-16 5 days 16 2020-01-17 6 days 17 2020-01-18 7 days You can get your desired ints afterwards with: >>> df['index'].transform(lambda x: x.days) 0 -10 1 -9 2 -8 3 -7 4 -6 5 -5 6 -4 7 -3 8 -2 9 -1 10 0 11 1 12 2 13 3 14 4 15 5 16 6 17 7 EDIT To answer more specifically since you have string dates you have to do the following first df[['Date', 'First_Date']] = df[['Date', 'First_Date']].astype('datetime64[ns]') then you can subtract the columns and get your result: df['index'] = df['Date'] - df['First_Date']
0
1,466
How to add positive and negative increments to every row based on a specific date? I have a pandas df which has 2 columns such as Date, First_Date (constant). I am trying to add a new column in which the value will be 0 where First_Date=Date. Then, all rows below that instance should increment in a negative way such as -1, -2, -3 etc.. and same should be true for rows above should increment in a positive way such as 1,2,3,4 etc. Please see attachment for concept visualization. I am not sure if there is a pandas function to do this or if a function is better in this case. Any guidance would be great.
/
>>> df = pd.DataFrame({'Date':pd.date_range('2020-01-01', '2020-01-18')}) >>> df Date 0 2020-01-01 1 2020-01-02 2 2020-01-03 3 2020-01-04 4 2020-01-05 5 2020-01-06 6 2020-01-07 7 2020-01-08 8 2020-01-09 9 2020-01-10 10 2020-01-11 11 2020-01-12 12 2020-01-13 13 2020-01-14 14 2020-01-15 15 2020-01-16 16 2020-01-17 17 2020-01-18 Checkout pandas Timestamps strings to DateTime etc. No reason to work with strings and ints. >>> df['index'] = df - pd.Timestamp('2020-01-11') >>> df Date index 0 2020-01-01 -10 days 1 2020-01-02 -9 days 2 2020-01-03 -8 days 3 2020-01-04 -7 days 4 2020-01-05 -6 days 5 2020-01-06 -5 days 6 2020-01-07 -4 days 7 2020-01-08 -3 days 8 2020-01-09 -2 days 9 2020-01-10 -1 days 10 2020-01-11 0 days 11 2020-01-12 1 days 12 2020-01-13 2 days 13 2020-01-14 3 days 14 2020-01-15 4 days 15 2020-01-16 5 days 16 2020-01-17 6 days 17 2020-01-18 7 days You can get your desired ints afterwards with: >>> df['index'].transform(lambda x: x.days) 0 -10 1 -9 2 -8 3 -7 4 -6 5 -5 6 -4 7 -3 8 -2 9 -1 10 0 11 1 12 2 13 3 14 4 15 5 16 6 17 7 EDIT To answer more specifically since you have string dates you have to do the following first df[['Date', 'First_Date']] = df[['Date', 'First_Date']].astype('datetime64[ns]') then you can subtract the columns and get your result: df['index'] = df['Date'] - df['First_Date']
69,712,773
Remove duplicates that are in included in two columns in pandas
<p>I have a dataframe that has two columns. I want to delete rows such that, for each row, it includes only one instance in the first column, but all unique values in column two are included.</p> <p>Here is an example:</p> <pre><code>data = [[1,100], [1,101], [1,102], [1,103], [2,102], [2,104], [2,105], [3,102], [3,107]] df = pd.DataFrame(data,columns = ['x', 'y']) </code></pre> <p>The data frame looks like this:</p> <pre><code> x y 0 1 100 1 1 101 2 1 102 3 1 103 4 2 102 5 2 104 6 2 105 7 3 102 8 3 107 </code></pre> <p>The output dataframe would look like this:</p> <pre><code> x y inc 0 1 100 1 1 1 101 0 2 1 102 0 3 1 103 0 4 2 102 1 5 2 104 0 6 2 105 0 7 3 102 0 8 3 107 1 </code></pre> <p>so row 0 would be included (inc), as 1 had not been duplicated yet in column x. Rows 1-3 would be excluded, as 1 in column x had already been accounted for. Row 4 would be included, as 2 in column x had not been included yet and column y (102) had not been included (it was excluded as a duplicate). At row 7, the first instance of 3 in column x would be excluded because 102 (in column y) had already been account for in row 4. Therefore, we would skip to row 8 and include it.</p> <p>I have tried a variety of <code>.duplicated</code> approaches, but none of them have worked so far. If you only take the first instance of a value in column x, you would exclude rows that should be included (for example row 7).</p> <p>Any help would be appreciated.</p>
69,713,006
"2021-10-25T18:02:25.890000"
2
1
2
105
python|pandas
<p>One way is to use a <code>set</code> and create custom function:</p> <pre><code>seen = set() def func(d): res = d[~d.isin(seen)] if len(res): cur = res.iat[0] seen.add(cur) return cur print (df.groupby(&quot;x&quot;)[&quot;y&quot;].apply(func)) x 1 100 2 102 3 107 Name: y, dtype: int64 </code></pre>
"2021-10-25T18:24:46.800000"
0
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.drop_duplicates.html
pandas.DataFrame.drop_duplicates# One way is to use a set and create custom function: seen = set() def func(d): res = d[~d.isin(seen)] if len(res): cur = res.iat[0] seen.add(cur) return cur print (df.groupby("x")["y"].apply(func)) x 1 100 2 102 3 107 Name: y, dtype: int64 pandas.DataFrame.drop_duplicates# DataFrame.drop_duplicates(subset=None, *, keep='first', inplace=False, ignore_index=False)[source]# Return DataFrame with duplicate rows removed. Considering certain columns is optional. Indexes, including time indexes are ignored. Parameters subsetcolumn label or sequence of labels, optionalOnly consider certain columns for identifying duplicates, by default use all of the columns. keep{‘first’, ‘last’, False}, default ‘first’Determines which duplicates (if any) to keep. - first : Drop duplicates except for the first occurrence. - last : Drop duplicates except for the last occurrence. - False : Drop all duplicates. inplacebool, default FalseWhether to modify the DataFrame rather than creating a new one. ignore_indexbool, default FalseIf True, the resulting axis will be labeled 0, 1, …, n - 1. New in version 1.0.0. Returns DataFrame or NoneDataFrame with duplicates removed or None if inplace=True. See also DataFrame.value_countsCount unique combinations of columns. Examples Consider dataset containing ramen rating. >>> df = pd.DataFrame({ ... 'brand': ['Yum Yum', 'Yum Yum', 'Indomie', 'Indomie', 'Indomie'], ... 'style': ['cup', 'cup', 'cup', 'pack', 'pack'], ... 'rating': [4, 4, 3.5, 15, 5] ... }) >>> df brand style rating 0 Yum Yum cup 4.0 1 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.0 By default, it removes duplicate rows based on all columns. >>> df.drop_duplicates() brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.0 To remove duplicates on specific column(s), use subset. >>> df.drop_duplicates(subset=['brand']) brand style rating 0 Yum Yum cup 4.0 2 Indomie cup 3.5 To remove duplicates and keep last occurrences, use keep. >>> df.drop_duplicates(subset=['brand', 'style'], keep='last') brand style rating 1 Yum Yum cup 4.0 2 Indomie cup 3.5 4 Indomie pack 5.0
35
318
Remove duplicates that are in included in two columns in pandas I have a dataframe that has two columns. I want to delete rows such that, for each row, it includes only one instance in the first column, but all unique values in column two are included. Here is an example: data = [[1,100], [1,101], [1,102], [1,103], [2,102], [2,104], [2,105], [3,102], [3,107]] df = pd.DataFrame(data,columns = ['x', 'y']) The data frame looks like this: x y 0 1 100 1 1 101 2 1 102 3 1 103 4 2 102 5 2 104 6 2 105 7 3 102 8 3 107 The output dataframe would look like this: x y inc 0 1 100 1 1 1 101 0 2 1 102 0 3 1 103 0 4 2 102 1 5 2 104 0 6 2 105 0 7 3 102 0 8 3 107 1 so row 0 would be included (inc), as 1 had not been duplicated yet in column x. Rows 1-3 would be excluded, as 1 in column x had already been accounted for. Row 4 would be included, as 2 in column x had not been included yet and column y (102) had not been included (it was excluded as a duplicate). At row 7, the first instance of 3 in column x would be excluded because 102 (in column y) had already been account for in row 4. Therefore, we would skip to row 8 and include it. I have tried a variety of .duplicated approaches, but none of them have worked so far. If you only take the first instance of a value in column x, you would exclude rows that should be included (for example row 7). Any help would be appreciated.
One way is to use a set and create custom function: seen = set() def func(d): res = d[~d.isin(seen)] if len(res): cur = res.iat[0] seen.add(cur) return cur print (df.groupby("x")["y"].apply(func)) x 1 100 2 102 3 107 Name: y, dtype: int64
68,385,969
Calculate Time Between Orders By Customer ID
<p>I have following Porblem:</p> <p>I want to calculate the time between orders for every Customer in Days. My Dataframe looks like below.</p> <pre><code> CustID OrderDate Sales 5 16838 2015-05-13 197.00 6 17986 2015-12-18 224.90 7 18191 2015-11-10 325.80 8 18191 2015-02-09 43.80 9 18191 2015-03-10 375.60 </code></pre> <p>I found following piece of Code but I cant get it to work.</p> <pre><code>(data.groupby('CustID') .OrderDate .apply(lambda x: (x-x.min).days()) .reset_index()) </code></pre>
68,387,807
"2021-07-14T22:59:18.797000"
1
null
0
117
python|pandas
<p>You need to convert the date column to a datetime first and also put it in chronological order. This code should dot he trick:</p> <pre><code>data.OrderDate = pd.to_datetime(data.OrderDate) data = data.sort_values(by=['OrderDate']) data['days'] = data.groupby('CustID').OrderDate.apply(lambda x: x.diff()) </code></pre> <p>Notice that this gives the days since the last order made by the customer. If the customer has not made a previous order then it will be returned as NaT.</p>
"2021-07-15T04:12:05.180000"
0
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.diff.html
pandas.DataFrame.diff# pandas.DataFrame.diff# DataFrame.diff(periods=1, axis=0)[source]# First discrete difference of element. Calculates the difference of a DataFrame element compared with another element in the DataFrame (default is element in previous row). You need to convert the date column to a datetime first and also put it in chronological order. This code should dot he trick: data.OrderDate = pd.to_datetime(data.OrderDate) data = data.sort_values(by=['OrderDate']) data['days'] = data.groupby('CustID').OrderDate.apply(lambda x: x.diff()) Notice that this gives the days since the last order made by the customer. If the customer has not made a previous order then it will be returned as NaT. Parameters periodsint, default 1Periods to shift for calculating difference, accepts negative values. axis{0 or ‘index’, 1 or ‘columns’}, default 0Take difference over rows (0) or columns (1). Returns DataFrameFirst differences of the Series. See also DataFrame.pct_changePercent change over given number of periods. DataFrame.shiftShift index by desired number of periods with an optional time freq. Series.diffFirst discrete difference of object. Notes For boolean dtypes, this uses operator.xor() rather than operator.sub(). The result is calculated according to current dtype in DataFrame, however dtype of the result is always float64. Examples Difference with previous row >>> df = pd.DataFrame({'a': [1, 2, 3, 4, 5, 6], ... 'b': [1, 1, 2, 3, 5, 8], ... 'c': [1, 4, 9, 16, 25, 36]}) >>> df a b c 0 1 1 1 1 2 1 4 2 3 2 9 3 4 3 16 4 5 5 25 5 6 8 36 >>> df.diff() a b c 0 NaN NaN NaN 1 1.0 0.0 3.0 2 1.0 1.0 5.0 3 1.0 1.0 7.0 4 1.0 2.0 9.0 5 1.0 3.0 11.0 Difference with previous column >>> df.diff(axis=1) a b c 0 NaN 0 0 1 NaN -1 3 2 NaN -1 7 3 NaN -1 13 4 NaN 0 20 5 NaN 2 28 Difference with 3rd previous row >>> df.diff(periods=3) a b c 0 NaN NaN NaN 1 NaN NaN NaN 2 NaN NaN NaN 3 3.0 2.0 15.0 4 3.0 4.0 21.0 5 3.0 6.0 27.0 Difference with following row >>> df.diff(periods=-1) a b c 0 -1.0 0.0 -3.0 1 -1.0 -1.0 -5.0 2 -1.0 -1.0 -7.0 3 -1.0 -2.0 -9.0 4 -1.0 -3.0 -11.0 5 NaN NaN NaN Overflow in input dtype >>> df = pd.DataFrame({'a': [1, 0]}, dtype=np.uint8) >>> df.diff() a 0 NaN 1 255.0
265
710
Calculate Time Between Orders By Customer ID I have following Porblem: I want to calculate the time between orders for every Customer in Days. My Dataframe looks like below. CustID OrderDate Sales 5 16838 2015-05-13 197.00 6 17986 2015-12-18 224.90 7 18191 2015-11-10 325.80 8 18191 2015-02-09 43.80 9 18191 2015-03-10 375.60 I found following piece of Code but I cant get it to work. (data.groupby('CustID') .OrderDate .apply(lambda x: (x-x.min).days()) .reset_index())
You need to convert the date column to a datetime first and also put it in chronological order. This code should dot he trick: data.OrderDate = pd.to_datetime(data.OrderDate) data = data.sort_values(by=['OrderDate']) data['days'] = data.groupby('CustID').OrderDate.apply(lambda x: x.diff()) Notice that this gives the days since the last order made by the customer. If the customer has not made a previous order then it will be returned as NaT.
65,677,018
How to generate a list of names associated with specific letter grade using regex in python pandas
<p>I'm starting with this code but it generates a list of only last names and letter grades in the format ['First Last: A']. What expression can I use to create a list of names associated with a letter grade A in the format ['First', 'Last'] with names extracted from only A letter grades? More specifically, I'd like to remove everything after the ': Grade' so that I only see the name. The data has other letter grades included. I think using \s and (?= ) could be helpful but I'm not sure where to place it.</p> <pre><code> pattern = &quot;(\w.+:\s[A])&quot; matches = re.findall(pattern,file) </code></pre> <p>The file is a simple text file in this format:</p> <p>First Last: Grade</p> <p>I'd like the output to extract only names with a grade A in this format:</p> <p>First Last</p>
65,677,441
"2021-01-12T01:55:02.370000"
2
null
0
409
python|pandas
<p>Ther are so much ways, depending on what you input data:</p> <pre><code>re.split(':', 'First Last: Grade') # ['First Last', ' Grade'] re.findall('^(.*?):', 'First Last: Grade') # ['First Last'] re.findall('^(\w+\s?\w*):', 'First Last: Grade') # ['First Last'] </code></pre>
"2021-01-12T02:59:16.437000"
0
https://pandas.pydata.org/docs/reference/api/pandas.Series.str.count.html
pandas.Series.str.count# pandas.Series.str.count# Series.str.count(pat, flags=0)[source]# Count occurrences of pattern in each string of the Series/Index. This function is used to count the number of times a particular regex Ther are so much ways, depending on what you input data: re.split(':', 'First Last: Grade') # ['First Last', ' Grade'] re.findall('^(.*?):', 'First Last: Grade') # ['First Last'] re.findall('^(\w+\s?\w*):', 'First Last: Grade') # ['First Last'] pattern is repeated in each of the string elements of the Series. Parameters patstrValid regular expression. flagsint, default 0, meaning no flagsFlags for the re module. For a complete list, see here. **kwargsFor compatibility with other string methods. Not used. Returns Series or IndexSame type as the calling object containing the integer counts. See also reStandard library module for regular expressions. str.countStandard library version, without regular expression support. Notes Some characters need to be escaped when passing in pat. eg. '$' has a special meaning in regex and must be escaped when finding this literal character. Examples >>> s = pd.Series(['A', 'B', 'Aaba', 'Baca', np.nan, 'CABA', 'cat']) >>> s.str.count('a') 0 0.0 1 0.0 2 2.0 3 2.0 4 NaN 5 0.0 6 1.0 dtype: float64 Escape '$' to find the literal dollar sign. >>> s = pd.Series(['$', 'B', 'Aab$', '$$ca', 'C$B$', 'cat']) >>> s.str.count('\\$') 0 1 1 0 2 1 3 2 4 2 5 0 dtype: int64 This is also available on Index >>> pd.Index(['A', 'A', 'Aaba', 'cat']).str.count('a') Int64Index([0, 0, 2, 1], dtype='int64')
229
477
How to generate a list of names associated with specific letter grade using regex in python pandas I'm starting with this code but it generates a list of only last names and letter grades in the format ['First Last: A']. What expression can I use to create a list of names associated with a letter grade A in the format ['First', 'Last'] with names extracted from only A letter grades? More specifically, I'd like to remove everything after the ': Grade' so that I only see the name. The data has other letter grades included. I think using \s and (?= ) could be helpful but I'm not sure where to place it. pattern = "(\w.+:\s[A])" matches = re.findall(pattern,file) The file is a simple text file in this format: First Last: Grade I'd like the output to extract only names with a grade A in this format: First Last
Index ( [ ' A ', ' A ', ' Aaba ', ' cat ' ] ). str. count /
Ther are so much ways, depending on what you input data: re.split(':', 'First Last: Grade') # ['First Last', ' Grade'] re.findall('^(.*?):', 'First Last: Grade') # ['First Last'] re.findall('^(\w+\s?\w*):', 'First Last: Grade') # ['First Last']
63,025,014
Pandas - groupby one column and then sort by all remaining columns
<p>I need to group the dataframe bellow by 'player_slug' and <em>then</em> sort all columns for each (numeric) 'mean' column.</p> <p>Please note that column values are already mean values.</p> <p>This is <code>df.head(5)</code>:</p> <pre><code> player_slug player_id player_nickname player_team player_position ... DD_mean DP_mean status price_diff last_points 0 paulo-andre 37604 Paulo André 293 zag ... 0.000000 0.000000 Provável 0.11 1.7 1 evandro 37614 Evandro 277 mei ... 0.000000 0.000000 Dúvida -1.78 2.8 2 betao 37646 Betão 314 zag ... 0.000000 0.000000 Provável -0.14 0.1 3 rafael-moura 37655 Rafael Moura 290 ata ... 0.000000 0.000000 Provável 2.89 22.2 4 fabio 37656 Fábio 283 gol ... 1.257143 0.057143 Provável 0.42 2.0 </code></pre> <hr /> <p>I have tried to create a function and pass all features, like so:</p> <pre><code> columns = ['score_mean','score_no_cleansheets_mean','diff_home_away_s', 'n_games','score_mean_home','score_mean_away','shots_x_mean','fouls_mean','RB_mean', 'PE_mean','A_mean','I_mean','FS_mean','FF_mean','G_mean','DD_mean','DP_mean', 'price_diff','last_points'] def sorted_medias(df, feature=None): df_agg = df.groupby(['player_slug', 'player_team']).agg({feature:'sum'}).sort_values(feature, ascending=False) print (df_agg) </code></pre> <p>and finally:</p> <pre><code>for feature in columns: sorted_medias(df_medias, feature) </code></pre> <p>But I'm unsure of using 'sum', or 'mean', in agg, since values are already means.</p> <p>What is the way to go here?</p>
63,026,208
"2020-07-22T00:35:21.387000"
1
null
0
169
python|pandas
<p>Looks like this is what the OP was asking for. Grouping by player and pick any value inside the group as the values are already aggregated.</p> <p><code>df.groupby(['player_slug'])['goals'].min().sort_values(ascending=False)</code></p>
"2020-07-22T03:08:44.057000"
0
https://pandas.pydata.org/docs/reference/api/pandas.core.groupby.GroupBy.mean.html
pandas.core.groupby.GroupBy.mean# pandas.core.groupby.GroupBy.mean# final GroupBy.mean(numeric_only=_NoDefault.no_default, engine='cython', engine_kwargs=None)[source]# Looks like this is what the OP was asking for. Grouping by player and pick any value inside the group as the values are already aggregated. df.groupby(['player_slug'])['goals'].min().sort_values(ascending=False) Compute mean of groups, excluding missing values. Parameters numeric_onlybool, default TrueInclude only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. enginestr, default None 'cython' : Runs the operation through C-extensions from cython. 'numba' : Runs the operation through JIT compiled code from numba. None : Defaults to 'cython' or globally setting compute.use_numba New in version 1.4.0. engine_kwargsdict, default None For 'cython' engine, there are no accepted engine_kwargs For 'numba' engine, the engine can accept nopython, nogil and parallel dictionary keys. The values must either be True or False. The default engine_kwargs for the 'numba' engine is {{'nopython': True, 'nogil': False, 'parallel': False}} New in version 1.4.0. Returns pandas.Series or pandas.DataFrame See also Series.groupbyApply a function groupby to a Series. DataFrame.groupbyApply a function groupby to each row or column of a DataFrame. Examples >>> df = pd.DataFrame({'A': [1, 1, 2, 1, 2], ... 'B': [np.nan, 2, 3, 4, 5], ... 'C': [1, 2, 1, 1, 2]}, columns=['A', 'B', 'C']) Groupby one column and return the mean of the remaining columns in each group. >>> df.groupby('A').mean() B C A 1 3.0 1.333333 2 4.0 1.500000 Groupby two columns and return the mean of the remaining column. >>> df.groupby(['A', 'B']).mean() C A B 1 2.0 2.0 4.0 1.0 2 3.0 1.0 5.0 2.0 Groupby one column and return the mean of only particular column in the group. >>> df.groupby('A')['B'].mean() A 1 3.0 2 4.0 Name: B, dtype: float64
173
384
Pandas - groupby one column and then sort by all remaining columns I need to group the dataframe bellow by 'player_slug' and then sort all columns for each (numeric) 'mean' column. Please note that column values are already mean values. This is df.head(5): player_slug player_id player_nickname player_team player_position ... DD_mean DP_mean status price_diff last_points 0 paulo-andre 37604 Paulo André 293 zag ... 0.000000 0.000000 Provável 0.11 1.7 1 evandro 37614 Evandro 277 mei ... 0.000000 0.000000 Dúvida -1.78 2.8 2 betao 37646 Betão 314 zag ... 0.000000 0.000000 Provável -0.14 0.1 3 rafael-moura 37655 Rafael Moura 290 ata ... 0.000000 0.000000 Provável 2.89 22.2 4 fabio 37656 Fábio 283 gol ... 1.257143 0.057143 Provável 0.42 2.0 I have tried to create a function and pass all features, like so: columns = ['score_mean','score_no_cleansheets_mean','diff_home_away_s', 'n_games','score_mean_home','score_mean_away','shots_x_mean','fouls_mean','RB_mean', 'PE_mean','A_mean','I_mean','FS_mean','FF_mean','G_mean','DD_mean','DP_mean', 'price_diff','last_points'] def sorted_medias(df, feature=None): df_agg = df.groupby(['player_slug', 'player_team']).agg({feature:'sum'}).sort_values(feature, ascending=False) print (df_agg) and finally: for feature in columns: sorted_medias(df_medias, feature) But I'm unsure of using 'sum', or 'mean', in agg, since values are already means. What is the way to go here?
/
Looks like this is what the OP was asking for. Grouping by player and pick any value inside the group as the values are already aggregated. df.groupby(['player_slug'])['goals'].min().sort_values(ascending=False)
69,024,982
Fastest way to find a Pandas index-column value pair
<p>I have a largish DataFrame with a date index ['Date'] and several columns. One column is a string identifier ['Type'], with related data in the remaining columns. I need to add newData to the DataFrame, but only if the date-type pair (i.e. index-ColumnValue pair) is not already present in the DataFrame. Checking for the existing pairing takes up ~95% of my code computing time, so I really need to find a quicker way to do it.</p> <p>Options already considered, with timings in increasing order of speed::</p> <pre><code>existing_pair = len(compiledData[(compiledData['Type'] == newData['Type']) &amp; (compiledData.index == newData['Date'])]) &gt; 0 # average = 114 ms </code></pre> <pre><code>existing_pair = newData['Date'] in compiledData[compiledData['Type'] == newData['Type']].index # average = 68 ms </code></pre> <pre><code>existing_pair = compiledData[compiledData.index == newData['Type']]['Type']. isin([newData['Date']]).any() # average = 44 ms </code></pre> <p>I am relatively new to Python so I am sure there are better (= faster) ways of checking for an index-colVal pair. Or, it may be that my entire data structure is wrong. Would appreciate any pointers anyone can offer.</p> <p>Edit: Sample of the compiledData dataframe:</p> <pre><code> Type ColA ColB ColC ColD ColE ColF 2021-01-19 B 83.0 -122.15 0.0 11.0 11.000 11.0 2021-01-19 D 83.0 -1495.48 0.0 11.0 11.000 11.0 2021-03-25 D 83.0 432.00 0.0 11.0 11.000 11.0 2021-04-14 D 83.0 646.00 0.0 11.0 11.000 11.0 2021-04-16 A 20.0 11.00 0.0 30.0 11.000 11.0 2021-04-25 D 83.0 -26.82 0.0 11.0 11.000 11.0 2021-04-28 B 83.0 -651.00 0.0 11.0 11.000 11.0 </code></pre>
69,025,583
"2021-09-02T06:12:21.863000"
2
null
4
274
python|pandas
<p>Index value lookup is faster than column value lookup. I don't know the implementation details (it looks like lookup depends on number of rows). Here is a performance comparison:</p> <pre><code>def test_value_matches(df, v1, v2): # return True if v1, v2 found in df columns, else return False if any(df[(df.c1 == v1) &amp; (df.c2 == v2)]): return True return False def test_index_matches(df, v1, v2): # returns True if (v1, v2) found in (multi) index, else returns False if (v1, v2) in df.index: return True return False # test dependence of funcs above on num rows in df: for n in [int(j) for j in [1e4, 1e5, 1e6, 1e7]]: df = pd.DataFrame(np.random.random(size=(n, 2)), columns=[&quot;c1&quot;, &quot;c2&quot;]) v1, v2 = df.sample(n=1).iloc[0] %timeit test_value_matches(df, v1, v2) # create an index based on column values: df2 = df.set_index([&quot;c1&quot;, &quot;c2&quot;]) %timeit test_index_matches(df2, v1, v2) </code></pre> <p>output</p> <pre><code>421 µs ± 22.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) 10.5 µs ± 175 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) 557 µs ± 5.35 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) 10.3 µs ± 143 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) 3.77 ms ± 166 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) 16.5 µs ± 185 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) 22.4 ms ± 2.06 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 28.1 µs ± 10.2 µs per loop (mean ± std. dev. of 7 runs, 1 loop each) </code></pre> <p>Note that this ignores indexing time itself, which may be significant; this approach probably works best with repeated lookups on the same df. For <code>n=1e7</code>, the performance is something like your problem on my machine; the indexed version is ~1000x faster (although apparently growing with <code>n</code>).</p>
"2021-09-02T07:04:29.173000"
1
https://pandas.pydata.org/docs/reference/api/pandas.Series.iteritems.html
Index value lookup is faster than column value lookup. I don't know the implementation details (it looks like lookup depends on number of rows). Here is a performance comparison: def test_value_matches(df, v1, v2): # return True if v1, v2 found in df columns, else return False if any(df[(df.c1 == v1) & (df.c2 == v2)]): return True return False def test_index_matches(df, v1, v2): # returns True if (v1, v2) found in (multi) index, else returns False if (v1, v2) in df.index: return True return False # test dependence of funcs above on num rows in df: for n in [int(j) for j in [1e4, 1e5, 1e6, 1e7]]: df = pd.DataFrame(np.random.random(size=(n, 2)), columns=["c1", "c2"]) v1, v2 = df.sample(n=1).iloc[0] %timeit test_value_matches(df, v1, v2) # create an index based on column values: df2 = df.set_index(["c1", "c2"]) %timeit test_index_matches(df2, v1, v2) output 421 µs ± 22.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) 10.5 µs ± 175 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) 557 µs ± 5.35 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) 10.3 µs ± 143 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) 3.77 ms ± 166 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) 16.5 µs ± 185 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) 22.4 ms ± 2.06 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 28.1 µs ± 10.2 µs per loop (mean ± std. dev. of 7 runs, 1 loop each) Note that this ignores indexing time itself, which may be significant; this approach probably works best with repeated lookups on the same df. For n=1e7, the performance is something like your problem on my machine; the indexed version is ~1000x faster (although apparently growing with n).
0
1,812
Fastest way to find a Pandas index-column value pair I have a largish DataFrame with a date index ['Date'] and several columns. One column is a string identifier ['Type'], with related data in the remaining columns. I need to add newData to the DataFrame, but only if the date-type pair (i.e. index-ColumnValue pair) is not already present in the DataFrame. Checking for the existing pairing takes up ~95% of my code computing time, so I really need to find a quicker way to do it. Options already considered, with timings in increasing order of speed:: existing_pair = len(compiledData[(compiledData['Type'] == newData['Type']) & (compiledData.index == newData['Date'])]) > 0 # average = 114 ms existing_pair = newData['Date'] in compiledData[compiledData['Type'] == newData['Type']].index # average = 68 ms existing_pair = compiledData[compiledData.index == newData['Type']]['Type']. isin([newData['Date']]).any() # average = 44 ms I am relatively new to Python so I am sure there are better (= faster) ways of checking for an index-colVal pair. Or, it may be that my entire data structure is wrong. Would appreciate any pointers anyone can offer. Edit: Sample of the compiledData dataframe: Type ColA ColB ColC ColD ColE ColF 2021-01-19 B 83.0 -122.15 0.0 11.0 11.000 11.0 2021-01-19 D 83.0 -1495.48 0.0 11.0 11.000 11.0 2021-03-25 D 83.0 432.00 0.0 11.0 11.000 11.0 2021-04-14 D 83.0 646.00 0.0 11.0 11.000 11.0 2021-04-16 A 20.0 11.00 0.0 30.0 11.000 11.0 2021-04-25 D 83.0 -26.82 0.0 11.0 11.000 11.0 2021-04-28 B 83.0 -651.00 0.0 11.0 11.000 11.0
Index value lookup is faster than column value lookup. I don't know the implementation details (it looks like lookup depends on number of rows). Here is a performance comparison: def test_value_matches(df, v1, v2): # return True if v1, v2 found in df columns, else return False if any(df[(df.c1 == v1) & (df.c2 == v2)]): return True return False def test_index_matches(df, v1, v2): # returns True if (v1, v2) found in (multi) index, else returns False if (v1, v2) in df.index: return True return False # test dependence of funcs above on num rows in df: for n in [int(j) for j in [1e4, 1e5, 1e6, 1e7]]: df = pd.DataFrame(np.random.random(size=(n, 2)), columns=["c1", "c2"]) v1, v2 = df.sample(n=1).iloc[0] %timeit test_value_matches(df, v1, v2) # create an index based on column values: df2 = df.set_index(["c1", "c2"]) %timeit test_index_matches(df2, v1, v2) output 421 µs ± 22.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) 10.5 µs ± 175 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) 557 µs ± 5.35 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) 10.3 µs ± 143 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) 3.77 ms ± 166 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) 16.5 µs ± 185 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each) 22.4 ms ± 2.06 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 28.1 µs ± 10.2 µs per loop (mean ± std. dev. of 7 runs, 1 loop each) Note that this ignores indexing time itself, which may be significant; this approach probably works best with repeated lookups on the same df. For n=1e7, the performance is something like your problem on my machine; the indexed version is ~1000x faster (although apparently growing with n).
65,074,569
Pandas Dataframe: List type column created from comparisons between two dataframes
<p>Considering the two dataframes below, where in <code>df</code> the column <code>USER</code> represent a user and <code>ANTENNA_ID</code> represent antennas that users used to make phone calls and in <code>df2</code> the column <code>USER</code> also represent a user and <code>PRESUMED_RESIDENCE</code> indicates the antenna closest to the user's home:</p> <pre><code>import pandas as pd import numpy as np df = pd.DataFrame({'USER':[1,2,3,1,1,2], 'ANTENNA_ID': ['SJDR1', 'LD', 'LD', 'LD', 'TR', 'SVM']}) df2 = pd.DataFrame({'USER': [1,2,3], 'PRESUMED_RESIDENCE': ['SJDR1', 'LD', 'LD']}) </code></pre> <p>I need to create another dataframe containing the <code>USER</code> column, the <code>PRESUMED_RESIDENCE</code> of <code>df2</code> column and other column containing the locations where users made calls that are different from their <code>PRESUMED_RESIDENCE</code>.</p> <pre><code>USER | PRESUMED RESIDENCE | CALL_LOC 1 | SJDR1 | LD, TR 2 | LD | SVM 3 | LD | Nan </code></pre> <p>what I've managed to imagine so far is far from the expected result, as can be seen below:</p> <pre><code>df2['CALL_LOC'] = np.where(df.USER.values==df2.USER.values, df.ANTENNA_ID.values, np.nan) df2 </code></pre> <p>Can anyone help?</p>
65,074,898
"2020-11-30T13:33:28.377000"
1
null
1
20
python|pandas
<p>If you don't care about removing the <code>PRESUMED_RESIDENCE</code> from the list of <code>CALL_LOC</code>, it's fairly straightforward in pandas.</p> <pre><code>merged = pd.merge(df, df2, left_on=&quot;USER&quot;, right_on=&quot;USER&quot;) print(merged.to_string()) &gt; USER ANTENNA_ID PRESUMED_RESIDENCE 0 1 SJDR1 SJDR1 1 1 LD SJDR1 2 1 TR SJDR1 3 2 LD LD 4 2 SVM LD 5 3 LD LD grouped = merged.groupby(&quot;USER&quot;).agg( {&quot;PRESUMED_RESIDENCE&quot;: min, &quot;ANTENNA_ID&quot;: list} ) print(grouped.to_string()) &gt; PRESUMED_RESIDENCE ANTENNA_ID USER 1 SJDR1 [SJDR1, LD, TR] 2 LD [LD, SVM] 3 LD [LD] </code></pre> <p>You could also use the <code>USER</code> as the index.</p> <p>If you want to remove the <code>PRESUMED_RESIDENCE</code> from the <code>ANTENNA_ID</code>, you can do it as a second step by iterating through the rows.</p> <p>If you want to do that during the construction of the pd.DataFrame, you can modify each group of the <code>groupby</code> object.</p>
"2020-11-30T13:55:41.023000"
1
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.compare.html
pandas.DataFrame.compare# pandas.DataFrame.compare# DataFrame.compare(other, align_axis=1, keep_shape=False, keep_equal=False, result_names=('self', 'other'))[source]# If you don't care about removing the PRESUMED_RESIDENCE from the list of CALL_LOC, it's fairly straightforward in pandas. merged = pd.merge(df, df2, left_on="USER", right_on="USER") print(merged.to_string()) > USER ANTENNA_ID PRESUMED_RESIDENCE 0 1 SJDR1 SJDR1 1 1 LD SJDR1 2 1 TR SJDR1 3 2 LD LD 4 2 SVM LD 5 3 LD LD grouped = merged.groupby("USER").agg( {"PRESUMED_RESIDENCE": min, "ANTENNA_ID": list} ) print(grouped.to_string()) > PRESUMED_RESIDENCE ANTENNA_ID USER 1 SJDR1 [SJDR1, LD, TR] 2 LD [LD, SVM] 3 LD [LD] You could also use the USER as the index. If you want to remove the PRESUMED_RESIDENCE from the ANTENNA_ID, you can do it as a second step by iterating through the rows. If you want to do that during the construction of the pd.DataFrame, you can modify each group of the groupby object. Compare to another DataFrame and show the differences. New in version 1.1.0. Parameters otherDataFrameObject to compare with. align_axis{0 or ‘index’, 1 or ‘columns’}, default 1Determine which axis to align the comparison on. 0, or ‘index’Resulting differences are stacked verticallywith rows drawn alternately from self and other. 1, or ‘columns’Resulting differences are aligned horizontallywith columns drawn alternately from self and other. keep_shapebool, default FalseIf true, all rows and columns are kept. Otherwise, only the ones with different values are kept. keep_equalbool, default FalseIf true, the result keeps values that are equal. Otherwise, equal values are shown as NaNs. result_namestuple, default (‘self’, ‘other’)Set the dataframes names in the comparison. New in version 1.5.0. Returns DataFrameDataFrame that shows the differences stacked side by side. The resulting index will be a MultiIndex with ‘self’ and ‘other’ stacked alternately at the inner level. Raises ValueErrorWhen the two DataFrames don’t have identical labels or shape. See also Series.compareCompare with another Series and show differences. DataFrame.equalsTest whether two objects contain the same elements. Notes Matching NaNs will not appear as a difference. Can only compare identically-labeled (i.e. same shape, identical row and column labels) DataFrames Examples >>> df = pd.DataFrame( ... { ... "col1": ["a", "a", "b", "b", "a"], ... "col2": [1.0, 2.0, 3.0, np.nan, 5.0], ... "col3": [1.0, 2.0, 3.0, 4.0, 5.0] ... }, ... columns=["col1", "col2", "col3"], ... ) >>> df col1 col2 col3 0 a 1.0 1.0 1 a 2.0 2.0 2 b 3.0 3.0 3 b NaN 4.0 4 a 5.0 5.0 >>> df2 = df.copy() >>> df2.loc[0, 'col1'] = 'c' >>> df2.loc[2, 'col3'] = 4.0 >>> df2 col1 col2 col3 0 c 1.0 1.0 1 a 2.0 2.0 2 b 3.0 4.0 3 b NaN 4.0 4 a 5.0 5.0 Align the differences on columns >>> df.compare(df2) col1 col3 self other self other 0 a c NaN NaN 2 NaN NaN 3.0 4.0 Assign result_names >>> df.compare(df2, result_names=("left", "right")) col1 col3 left right left right 0 a c NaN NaN 2 NaN NaN 3.0 4.0 Stack the differences on rows >>> df.compare(df2, align_axis=0) col1 col3 0 self a NaN other c NaN 2 self NaN 3.0 other NaN 4.0 Keep the equal values >>> df.compare(df2, keep_equal=True) col1 col3 self other self other 0 a c 1.0 1.0 2 b b 3.0 4.0 Keep all original rows and columns >>> df.compare(df2, keep_shape=True) col1 col2 col3 self other self other self other 0 a c NaN NaN NaN NaN 1 NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN 3.0 4.0 3 NaN NaN NaN NaN NaN NaN 4 NaN NaN NaN NaN NaN NaN Keep all original rows and columns and also all original values >>> df.compare(df2, keep_shape=True, keep_equal=True) col1 col2 col3 self other self other self other 0 a c 1.0 1.0 1.0 1.0 1 a a 2.0 2.0 2.0 2.0 2 b b 3.0 3.0 3.0 4.0 3 b b NaN NaN 4.0 4.0 4 a a 5.0 5.0 5.0 5.0
172
1,265
Pandas Dataframe: List type column created from comparisons between two dataframes Considering the two dataframes below, where in df the column USER represent a user and ANTENNA_ID represent antennas that users used to make phone calls and in df2 the column USER also represent a user and PRESUMED_RESIDENCE indicates the antenna closest to the user's home: import pandas as pd import numpy as np df = pd.DataFrame({'USER':[1,2,3,1,1,2], 'ANTENNA_ID': ['SJDR1', 'LD', 'LD', 'LD', 'TR', 'SVM']}) df2 = pd.DataFrame({'USER': [1,2,3], 'PRESUMED_RESIDENCE': ['SJDR1', 'LD', 'LD']}) I need to create another dataframe containing the USER column, the PRESUMED_RESIDENCE of df2 column and other column containing the locations where users made calls that are different from their PRESUMED_RESIDENCE. USER | PRESUMED RESIDENCE | CALL_LOC 1 | SJDR1 | LD, TR 2 | LD | SVM 3 | LD | Nan what I've managed to imagine so far is far from the expected result, as can be seen below: df2['CALL_LOC'] = np.where(df.USER.values==df2.USER.values, df.ANTENNA_ID.values, np.nan) df2 Can anyone help?
5. 0 /
If you don't care about removing the PRESUMED_RESIDENCE from the list of CALL_LOC, it's fairly straightforward in pandas. merged = pd.merge(df, df2, left_on="USER", right_on="USER") print(merged.to_string()) > USER ANTENNA_ID PRESUMED_RESIDENCE 0 1 SJDR1 SJDR1 1 1 LD SJDR1 2 1 TR SJDR1 3 2 LD LD 4 2 SVM LD 5 3 LD LD grouped = merged.groupby("USER").agg( {"PRESUMED_RESIDENCE": min, "ANTENNA_ID": list} ) print(grouped.to_string()) > PRESUMED_RESIDENCE ANTENNA_ID USER 1 SJDR1 [SJDR1, LD, TR] 2 LD [LD, SVM] 3 LD [LD] You could also use the USER as the index. If you want to remove the PRESUMED_RESIDENCE from the ANTENNA_ID, you can do it as a second step by iterating through the rows. If you want to do that during the construction of the pd.DataFrame, you can modify each group of the groupby object.
60,792,899
Copying Column Values using as referencing a third column
<p>I have the following pandas DataFrame:</p> <pre><code>import pandas as pd df = pd.DataFrame([['client_1', 'a', 5], ['client_1', 'b', 3], ['client_2', 'a', 4], ['client_2', 'b', 8], ['client_3', 'a', 1], ['client_3', 'b', 2]], columns=['Col1', 'Col2', 'NumCol']) ╔══════════╦══════╦════════╗ ║ Col1 ║ Col2 ║ NumCol ║ ╠══════════╬══════╬════════╣ ║ client_1 ║ a ║ 5 ║ ║ client_1 ║ b ║ 3 ║ ║ client_2 ║ a ║ 4 ║ ║ client_2 ║ b ║ 8 ║ ║ client_3 ║ a ║ 1 ║ ║ client_3 ║ b ║ 2 ║ ╚══════════╩══════╩════════╝ </code></pre> <p>I want to create a column called 'Col3' which for every client should have Col2 value corresponding to NumCol values, e.g.</p> <pre><code>╔══════════╦══════╦════════╦═══════╗ ║ Col1 ║ Col2 ║ NumCol ║ Col 3 ║ ╠══════════╬══════╬════════╬═══════╣ ║ client_1 ║ a ║ 5 ║ a ║ ║ client_1 ║ b ║ 3 ║ a ║ ║ client_2 ║ a ║ 4 ║ b ║ ║ client_2 ║ b ║ 8 ║ b ║ ║ client_3 ║ a ║ 1 ║ b ║ ║ client_3 ║ b ║ 2 ║ b ║ ╚══════════╩══════╩════════╩═══════╝ </code></pre> <p>Is there a pandas-pythonic way to acchieve this?</p>
60,792,921
"2020-03-21T20:26:06.197000"
1
null
0
28
python|pandas
<p>IIUC <code>idxmax</code> with <code>transform</code> </p> <pre><code>df['New']=df.Col2.reindex(df.groupby('Col1').NumCol.transform('idxmax')).values df Col1 Col2 NumCol New 0 client_1 a 5 a 1 client_1 b 3 a 2 client_2 a 4 b 3 client_2 b 8 b 4 client_3 a 1 b 5 client_3 b 2 b </code></pre>
"2020-03-21T20:28:42.287000"
1
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.copy.html
pandas.DataFrame.copy# pandas.DataFrame.copy# DataFrame.copy(deep=True)[source]# Make a copy of this object’s indices and data. When deep=True (default), a new object will be created with a copy of the calling object’s data and indices. Modifications to the data or indices of the copy will not be reflected in the original object (see notes below). IIUC idxmax with transform df['New']=df.Col2.reindex(df.groupby('Col1').NumCol.transform('idxmax')).values df Col1 Col2 NumCol New 0 client_1 a 5 a 1 client_1 b 3 a 2 client_2 a 4 b 3 client_2 b 8 b 4 client_3 a 1 b 5 client_3 b 2 b When deep=False, a new object will be created without copying the calling object’s data or index (only references to the data and index are copied). Any changes to the data of the original will be reflected in the shallow copy (and vice versa). Parameters deepbool, default TrueMake a deep copy, including a copy of the data and the indices. With deep=False neither the indices nor the data are copied. Returns copySeries or DataFrameObject type matches caller. Notes When deep=True, data is copied but actual Python objects will not be copied recursively, only the reference to the object. This is in contrast to copy.deepcopy in the Standard Library, which recursively copies object data (see examples below). While Index objects are copied when deep=True, the underlying numpy array is not copied for performance reasons. Since Index is immutable, the underlying data can be safely shared and a copy is not needed. Since pandas is not thread safe, see the gotchas when copying in a threading environment. Examples >>> s = pd.Series([1, 2], index=["a", "b"]) >>> s a 1 b 2 dtype: int64 >>> s_copy = s.copy() >>> s_copy a 1 b 2 dtype: int64 Shallow copy versus default (deep) copy: >>> s = pd.Series([1, 2], index=["a", "b"]) >>> deep = s.copy() >>> shallow = s.copy(deep=False) Shallow copy shares data and index with original. >>> s is shallow False >>> s.values is shallow.values and s.index is shallow.index True Deep copy has own copy of data and index. >>> s is deep False >>> s.values is deep.values or s.index is deep.index False Updates to the data shared by shallow copy and original is reflected in both; deep copy remains unchanged. >>> s[0] = 3 >>> shallow[1] = 4 >>> s a 3 b 4 dtype: int64 >>> shallow a 3 b 4 dtype: int64 >>> deep a 1 b 2 dtype: int64 Note that when copying an object containing Python objects, a deep copy will copy the data, but will not do so recursively. Updating a nested data object will be reflected in the deep copy. >>> s = pd.Series([[1, 2], [3, 4]]) >>> deep = s.copy() >>> s[0][0] = 10 >>> s 0 [10, 2] 1 [3, 4] dtype: object >>> deep 0 [10, 2] 1 [3, 4] dtype: object
354
668
Copying Column Values using as referencing a third column I have the following pandas DataFrame: import pandas as pd df = pd.DataFrame([['client_1', 'a', 5], ['client_1', 'b', 3], ['client_2', 'a', 4], ['client_2', 'b', 8], ['client_3', 'a', 1], ['client_3', 'b', 2]], columns=['Col1', 'Col2', 'NumCol']) ╔══════════╦══════╦════════╗ ║ Col1 ║ Col2 ║ NumCol ║ ╠══════════╬══════╬════════╣ ║ client_1 ║ a ║ 5 ║ ║ client_1 ║ b ║ 3 ║ ║ client_2 ║ a ║ 4 ║ ║ client_2 ║ b ║ 8 ║ ║ client_3 ║ a ║ 1 ║ ║ client_3 ║ b ║ 2 ║ ╚══════════╩══════╩════════╝ I want to create a column called 'Col3' which for every client should have Col2 value corresponding to NumCol values, e.g. ╔══════════╦══════╦════════╦═══════╗ ║ Col1 ║ Col2 ║ NumCol ║ Col 3 ║ ╠══════════╬══════╬════════╬═══════╣ ║ client_1 ║ a ║ 5 ║ a ║ ║ client_1 ║ b ║ 3 ║ a ║ ║ client_2 ║ a ║ 4 ║ b ║ ║ client_2 ║ b ║ 8 ║ b ║ ║ client_3 ║ a ║ 1 ║ b ║ ║ client_3 ║ b ║ 2 ║ b ║ ╚══════════╩══════╩════════╩═══════╝ Is there a pandas-pythonic way to acchieve this?
IIUC idxmax with transform df['New']=df.Col2.reindex(df.groupby('Col1').NumCol.transform('idxmax')).values df Col1 Col2 NumCol New 0 client_1 a 5 a 1 client_1 b 3 a 2 client_2 a 4 b 3 client_2 b 8 b 4 client_3 a 1 b 5 client_3 b 2 b
61,805,973
How to choose items out of many options in pandas
<p>This question <a href="https://stackoverflow.com/q/61803635/13543235">Calculate points with pandas</a> did not capture well what I wanted to ask so I ask it here. I have this data here</p> <pre><code>df = pd.DataFrame({'ENG':[10,3,5,6,3],'KIS':[9,5,7,9,10],'BIO':[10,'',4,'',4],'PHY':[9,5,'',10,12],'HIS':['','',9,7,8],'GEO':['',7,'',11,''],'CRE':[8,3,6,'','']}) </code></pre> <p>My program is to calculate the points for every individual in the data choosing only 4 subjects out of the 5 done. The first two subjects are compulsories. In order to get the other two subjects to add to get the 4 required, you weigh between the remaining subjects, in which you pick the best performed.</p> <p>My expected outcome looks like this.</p> <pre><code> ENG KIS BIO PHY HIS GEO CRE POINTS 10 9 10 9 8 38 3 5 5 7 3 20 5 7 4 9 6 27 6 9 10 7 11 36 3 10 4 12 8 25 </code></pre> <p>This is what I tried</p> <pre><code>a = df['ENG'] + df['KIS'] + df[['BIO', 'PHY']].fillna(0).max(axis=1) + df[['HIS', 'GEO', 'CRE']].fillna(0).max(axis=1) df['POINTS'] = a print(df) </code></pre>
61,806,287
"2020-05-14T19:35:28.610000"
1
null
1
33
pandas
<p>Sort each row by value and then pick the best two and sum them: </p> <pre><code>df = pd.DataFrame({'ENG':[10,3,5,6,3],'KIS':[9,5,7,9,10],'BIO':[10,'',4,'',4],'PHY':[9,5,'',10,12],'HIS':['','',9,7,8],'GEO':['',7,'',11,''],'CRE':[8,3,6,'','']}) df = df.replace('',0) df[['BIO','PHY','HIS','GEO','CRE']].apply(lambda row: row.sort_values(ascending=False).head(2).sum() ,axis=1) + df['ENG'] + df['KIS'] 0 38 1 20 2 27 3 36 4 33 </code></pre>
"2020-05-14T19:55:11.267000"
1
https://pandas.pydata.org/docs/user_guide/options.html
Options and settings# Options and settings# Overview# pandas has an options API configure and customize global behavior related to DataFrame display, data behavior and more. Options have a full “dotted-style”, case-insensitive name (e.g. display.max_rows). You can get/set options directly as attributes of the top-level options attribute: In [1]: import pandas as pd In [2]: pd.options.display.max_rows Out[2]: 15 In [3]: pd.options.display.max_rows = 999 Sort each row by value and then pick the best two and sum them: df = pd.DataFrame({'ENG':[10,3,5,6,3],'KIS':[9,5,7,9,10],'BIO':[10,'',4,'',4],'PHY':[9,5,'',10,12],'HIS':['','',9,7,8],'GEO':['',7,'',11,''],'CRE':[8,3,6,'','']}) df = df.replace('',0) df[['BIO','PHY','HIS','GEO','CRE']].apply(lambda row: row.sort_values(ascending=False).head(2).sum() ,axis=1) + df['ENG'] + df['KIS'] 0 38 1 20 2 27 3 36 4 33 In [4]: pd.options.display.max_rows Out[4]: 999 The API is composed of 5 relevant functions, available directly from the pandas namespace: get_option() / set_option() - get/set the value of a single option. reset_option() - reset one or more options to their default value. describe_option() - print the descriptions of one or more options. option_context() - execute a codeblock with a set of options that revert to prior settings after execution. Note Developers can check out pandas/core/config_init.py for more information. All of the functions above accept a regexp pattern (re.search style) as an argument, to match an unambiguous substring: In [5]: pd.get_option("display.chop_threshold") In [6]: pd.set_option("display.chop_threshold", 2) In [7]: pd.get_option("display.chop_threshold") Out[7]: 2 In [8]: pd.set_option("chop", 4) In [9]: pd.get_option("display.chop_threshold") Out[9]: 4 The following will not work because it matches multiple option names, e.g. display.max_colwidth, display.max_rows, display.max_columns: In [10]: pd.get_option("max") --------------------------------------------------------------------------- OptionError Traceback (most recent call last) Cell In[10], line 1 ----> 1 pd.get_option("max") File ~/work/pandas/pandas/pandas/_config/config.py:263, in CallableDynamicDoc.__call__(self, *args, **kwds) 262 def __call__(self, *args, **kwds) -> T: --> 263 return self.__func__(*args, **kwds) File ~/work/pandas/pandas/pandas/_config/config.py:135, in _get_option(pat, silent) 134 def _get_option(pat: str, silent: bool = False) -> Any: --> 135 key = _get_single_key(pat, silent) 137 # walk the nested dict 138 root, k = _get_root(key) File ~/work/pandas/pandas/pandas/_config/config.py:123, in _get_single_key(pat, silent) 121 raise OptionError(f"No such keys(s): {repr(pat)}") 122 if len(keys) > 1: --> 123 raise OptionError("Pattern matched multiple keys") 124 key = keys[0] 126 if not silent: OptionError: 'Pattern matched multiple keys' Warning Using this form of shorthand may cause your code to break if new options with similar names are added in future versions. Available options# You can get a list of available options and their descriptions with describe_option(). When called with no argument describe_option() will print out the descriptions for all available options. In [11]: pd.describe_option() compute.use_bottleneck : bool Use the bottleneck library to accelerate if it is installed, the default is True Valid values: False,True [default: True] [currently: True] compute.use_numba : bool Use the numba engine option for select operations if it is installed, the default is False Valid values: False,True [default: False] [currently: False] compute.use_numexpr : bool Use the numexpr library to accelerate computation if it is installed, the default is True Valid values: False,True [default: True] [currently: True] display.chop_threshold : float or None if set to a float value, all float values smaller than the given threshold will be displayed as exactly 0 by repr and friends. [default: None] [currently: None] display.colheader_justify : 'left'/'right' Controls the justification of column headers. used by DataFrameFormatter. [default: right] [currently: right] display.column_space No description available. [default: 12] [currently: 12] display.date_dayfirst : boolean When True, prints and parses dates with the day first, eg 20/01/2005 [default: False] [currently: False] display.date_yearfirst : boolean When True, prints and parses dates with the year first, eg 2005/01/20 [default: False] [currently: False] display.encoding : str/unicode Defaults to the detected encoding of the console. Specifies the encoding to be used for strings returned by to_string, these are generally strings meant to be displayed on the console. [default: utf-8] [currently: utf8] display.expand_frame_repr : boolean Whether to print out the full DataFrame repr for wide DataFrames across multiple lines, `max_columns` is still respected, but the output will wrap-around across multiple "pages" if its width exceeds `display.width`. [default: True] [currently: True] display.float_format : callable The callable should accept a floating point number and return a string with the desired format of the number. This is used in some places like SeriesFormatter. See formats.format.EngFormatter for an example. [default: None] [currently: None] display.html.border : int A ``border=value`` attribute is inserted in the ``<table>`` tag for the DataFrame HTML repr. [default: 1] [currently: 1] display.html.table_schema : boolean Whether to publish a Table Schema representation for frontends that support it. (default: False) [default: False] [currently: False] display.html.use_mathjax : boolean When True, Jupyter notebook will process table contents using MathJax, rendering mathematical expressions enclosed by the dollar symbol. (default: True) [default: True] [currently: True] display.large_repr : 'truncate'/'info' For DataFrames exceeding max_rows/max_cols, the repr (and HTML repr) can show a truncated table (the default from 0.13), or switch to the view from df.info() (the behaviour in earlier versions of pandas). [default: truncate] [currently: truncate] display.latex.escape : bool This specifies if the to_latex method of a Dataframe uses escapes special characters. Valid values: False,True [default: True] [currently: True] display.latex.longtable :bool This specifies if the to_latex method of a Dataframe uses the longtable format. Valid values: False,True [default: False] [currently: False] display.latex.multicolumn : bool This specifies if the to_latex method of a Dataframe uses multicolumns to pretty-print MultiIndex columns. Valid values: False,True [default: True] [currently: True] display.latex.multicolumn_format : bool This specifies if the to_latex method of a Dataframe uses multicolumns to pretty-print MultiIndex columns. Valid values: False,True [default: l] [currently: l] display.latex.multirow : bool This specifies if the to_latex method of a Dataframe uses multirows to pretty-print MultiIndex rows. Valid values: False,True [default: False] [currently: False] display.latex.repr : boolean Whether to produce a latex DataFrame representation for jupyter environments that support it. (default: False) [default: False] [currently: False] display.max_categories : int This sets the maximum number of categories pandas should output when printing out a `Categorical` or a Series of dtype "category". [default: 8] [currently: 8] display.max_columns : int If max_cols is exceeded, switch to truncate view. Depending on `large_repr`, objects are either centrally truncated or printed as a summary view. 'None' value means unlimited. In case python/IPython is running in a terminal and `large_repr` equals 'truncate' this can be set to 0 and pandas will auto-detect the width of the terminal and print a truncated object which fits the screen width. The IPython notebook, IPython qtconsole, or IDLE do not run in a terminal and hence it is not possible to do correct auto-detection. [default: 0] [currently: 0] display.max_colwidth : int or None The maximum width in characters of a column in the repr of a pandas data structure. When the column overflows, a "..." placeholder is embedded in the output. A 'None' value means unlimited. [default: 50] [currently: 50] display.max_dir_items : int The number of items that will be added to `dir(...)`. 'None' value means unlimited. Because dir is cached, changing this option will not immediately affect already existing dataframes until a column is deleted or added. This is for instance used to suggest columns from a dataframe to tab completion. [default: 100] [currently: 100] display.max_info_columns : int max_info_columns is used in DataFrame.info method to decide if per column information will be printed. [default: 100] [currently: 100] display.max_info_rows : int or None df.info() will usually show null-counts for each column. For large frames this can be quite slow. max_info_rows and max_info_cols limit this null check only to frames with smaller dimensions than specified. [default: 1690785] [currently: 1690785] display.max_rows : int If max_rows is exceeded, switch to truncate view. Depending on `large_repr`, objects are either centrally truncated or printed as a summary view. 'None' value means unlimited. In case python/IPython is running in a terminal and `large_repr` equals 'truncate' this can be set to 0 and pandas will auto-detect the height of the terminal and print a truncated object which fits the screen height. The IPython notebook, IPython qtconsole, or IDLE do not run in a terminal and hence it is not possible to do correct auto-detection. [default: 60] [currently: 60] display.max_seq_items : int or None When pretty-printing a long sequence, no more then `max_seq_items` will be printed. If items are omitted, they will be denoted by the addition of "..." to the resulting string. If set to None, the number of items to be printed is unlimited. [default: 100] [currently: 100] display.memory_usage : bool, string or None This specifies if the memory usage of a DataFrame should be displayed when df.info() is called. Valid values True,False,'deep' [default: True] [currently: True] display.min_rows : int The numbers of rows to show in a truncated view (when `max_rows` is exceeded). Ignored when `max_rows` is set to None or 0. When set to None, follows the value of `max_rows`. [default: 10] [currently: 10] display.multi_sparse : boolean "sparsify" MultiIndex display (don't display repeated elements in outer levels within groups) [default: True] [currently: True] display.notebook_repr_html : boolean When True, IPython notebook will use html representation for pandas objects (if it is available). [default: True] [currently: True] display.pprint_nest_depth : int Controls the number of nested levels to process when pretty-printing [default: 3] [currently: 3] display.precision : int Floating point output precision in terms of number of places after the decimal, for regular formatting as well as scientific notation. Similar to ``precision`` in :meth:`numpy.set_printoptions`. [default: 6] [currently: 6] display.show_dimensions : boolean or 'truncate' Whether to print out dimensions at the end of DataFrame repr. If 'truncate' is specified, only print out the dimensions if the frame is truncated (e.g. not display all rows and/or columns) [default: truncate] [currently: truncate] display.unicode.ambiguous_as_wide : boolean Whether to use the Unicode East Asian Width to calculate the display text width. Enabling this may affect to the performance (default: False) [default: False] [currently: False] display.unicode.east_asian_width : boolean Whether to use the Unicode East Asian Width to calculate the display text width. Enabling this may affect to the performance (default: False) [default: False] [currently: False] display.width : int Width of the display in characters. In case python/IPython is running in a terminal this can be set to None and pandas will correctly auto-detect the width. Note that the IPython notebook, IPython qtconsole, or IDLE do not run in a terminal and hence it is not possible to correctly detect the width. [default: 80] [currently: 80] io.excel.ods.reader : string The default Excel reader engine for 'ods' files. Available options: auto, odf. [default: auto] [currently: auto] io.excel.ods.writer : string The default Excel writer engine for 'ods' files. Available options: auto, odf. [default: auto] [currently: auto] io.excel.xls.reader : string The default Excel reader engine for 'xls' files. Available options: auto, xlrd. [default: auto] [currently: auto] io.excel.xls.writer : string The default Excel writer engine for 'xls' files. Available options: auto, xlwt. [default: auto] [currently: auto] (Deprecated, use `` instead.) io.excel.xlsb.reader : string The default Excel reader engine for 'xlsb' files. Available options: auto, pyxlsb. [default: auto] [currently: auto] io.excel.xlsm.reader : string The default Excel reader engine for 'xlsm' files. Available options: auto, xlrd, openpyxl. [default: auto] [currently: auto] io.excel.xlsm.writer : string The default Excel writer engine for 'xlsm' files. Available options: auto, openpyxl. [default: auto] [currently: auto] io.excel.xlsx.reader : string The default Excel reader engine for 'xlsx' files. Available options: auto, xlrd, openpyxl. [default: auto] [currently: auto] io.excel.xlsx.writer : string The default Excel writer engine for 'xlsx' files. Available options: auto, openpyxl, xlsxwriter. [default: auto] [currently: auto] io.hdf.default_format : format default format writing format, if None, then put will default to 'fixed' and append will default to 'table' [default: None] [currently: None] io.hdf.dropna_table : boolean drop ALL nan rows when appending to a table [default: False] [currently: False] io.parquet.engine : string The default parquet reader/writer engine. Available options: 'auto', 'pyarrow', 'fastparquet', the default is 'auto' [default: auto] [currently: auto] io.sql.engine : string The default sql reader/writer engine. Available options: 'auto', 'sqlalchemy', the default is 'auto' [default: auto] [currently: auto] mode.chained_assignment : string Raise an exception, warn, or no action if trying to use chained assignment, The default is warn [default: warn] [currently: warn] mode.copy_on_write : bool Use new copy-view behaviour using Copy-on-Write. Defaults to False, unless overridden by the 'PANDAS_COPY_ON_WRITE' environment variable (if set to "1" for True, needs to be set before pandas is imported). [default: False] [currently: False] mode.data_manager : string Internal data manager type; can be "block" or "array". Defaults to "block", unless overridden by the 'PANDAS_DATA_MANAGER' environment variable (needs to be set before pandas is imported). [default: block] [currently: block] mode.sim_interactive : boolean Whether to simulate interactive mode for purposes of testing [default: False] [currently: False] mode.string_storage : string The default storage for StringDtype. [default: python] [currently: python] mode.use_inf_as_na : boolean True means treat None, NaN, INF, -INF as NA (old way), False means None and NaN are null, but INF, -INF are not NA (new way). [default: False] [currently: False] mode.use_inf_as_null : boolean use_inf_as_null had been deprecated and will be removed in a future version. Use `use_inf_as_na` instead. [default: False] [currently: False] (Deprecated, use `mode.use_inf_as_na` instead.) plotting.backend : str The plotting backend to use. The default value is "matplotlib", the backend provided with pandas. Other backends can be specified by providing the name of the module that implements the backend. [default: matplotlib] [currently: matplotlib] plotting.matplotlib.register_converters : bool or 'auto'. Whether to register converters with matplotlib's units registry for dates, times, datetimes, and Periods. Toggling to False will remove the converters, restoring any converters that pandas overwrote. [default: auto] [currently: auto] styler.format.decimal : str The character representation for the decimal separator for floats and complex. [default: .] [currently: .] styler.format.escape : str, optional Whether to escape certain characters according to the given context; html or latex. [default: None] [currently: None] styler.format.formatter : str, callable, dict, optional A formatter object to be used as default within ``Styler.format``. [default: None] [currently: None] styler.format.na_rep : str, optional The string representation for values identified as missing. [default: None] [currently: None] styler.format.precision : int The precision for floats and complex numbers. [default: 6] [currently: 6] styler.format.thousands : str, optional The character representation for thousands separator for floats, int and complex. [default: None] [currently: None] styler.html.mathjax : bool If False will render special CSS classes to table attributes that indicate Mathjax will not be used in Jupyter Notebook. [default: True] [currently: True] styler.latex.environment : str The environment to replace ``\begin{table}``. If "longtable" is used results in a specific longtable environment format. [default: None] [currently: None] styler.latex.hrules : bool Whether to add horizontal rules on top and bottom and below the headers. [default: False] [currently: False] styler.latex.multicol_align : {"r", "c", "l", "naive-l", "naive-r"} The specifier for horizontal alignment of sparsified LaTeX multicolumns. Pipe decorators can also be added to non-naive values to draw vertical rules, e.g. "\|r" will draw a rule on the left side of right aligned merged cells. [default: r] [currently: r] styler.latex.multirow_align : {"c", "t", "b"} The specifier for vertical alignment of sparsified LaTeX multirows. [default: c] [currently: c] styler.render.encoding : str The encoding used for output HTML and LaTeX files. [default: utf-8] [currently: utf-8] styler.render.max_columns : int, optional The maximum number of columns that will be rendered. May still be reduced to satsify ``max_elements``, which takes precedence. [default: None] [currently: None] styler.render.max_elements : int The maximum number of data-cell (<td>) elements that will be rendered before trimming will occur over columns, rows or both if needed. [default: 262144] [currently: 262144] styler.render.max_rows : int, optional The maximum number of rows that will be rendered. May still be reduced to satsify ``max_elements``, which takes precedence. [default: None] [currently: None] styler.render.repr : str Determine which output to use in Jupyter Notebook in {"html", "latex"}. [default: html] [currently: html] styler.sparse.columns : bool Whether to sparsify the display of hierarchical columns. Setting to False will display each explicit level element in a hierarchical key for each column. [default: True] [currently: True] styler.sparse.index : bool Whether to sparsify the display of a hierarchical index. Setting to False will display each explicit level element in a hierarchical key for each row. [default: True] [currently: True] Getting and setting options# As described above, get_option() and set_option() are available from the pandas namespace. To change an option, call set_option('option regex', new_value). In [12]: pd.get_option("mode.sim_interactive") Out[12]: False In [13]: pd.set_option("mode.sim_interactive", True) In [14]: pd.get_option("mode.sim_interactive") Out[14]: True Note The option 'mode.sim_interactive' is mostly used for debugging purposes. You can use reset_option() to revert to a setting’s default value In [15]: pd.get_option("display.max_rows") Out[15]: 60 In [16]: pd.set_option("display.max_rows", 999) In [17]: pd.get_option("display.max_rows") Out[17]: 999 In [18]: pd.reset_option("display.max_rows") In [19]: pd.get_option("display.max_rows") Out[19]: 60 It’s also possible to reset multiple options at once (using a regex): In [20]: pd.reset_option("^display") option_context() context manager has been exposed through the top-level API, allowing you to execute code with given option values. Option values are restored automatically when you exit the with block: In [21]: with pd.option_context("display.max_rows", 10, "display.max_columns", 5): ....: print(pd.get_option("display.max_rows")) ....: print(pd.get_option("display.max_columns")) ....: 10 5 In [22]: print(pd.get_option("display.max_rows")) 60 In [23]: print(pd.get_option("display.max_columns")) 0 Setting startup options in Python/IPython environment# Using startup scripts for the Python/IPython environment to import pandas and set options makes working with pandas more efficient. To do this, create a .py or .ipy script in the startup directory of the desired profile. An example where the startup folder is in a default IPython profile can be found at: $IPYTHONDIR/profile_default/startup More information can be found in the IPython documentation. An example startup script for pandas is displayed below: import pandas as pd pd.set_option("display.max_rows", 999) pd.set_option("display.precision", 5) Frequently used options# The following is a demonstrates the more frequently used display options. display.max_rows and display.max_columns sets the maximum number of rows and columns displayed when a frame is pretty-printed. Truncated lines are replaced by an ellipsis. In [24]: df = pd.DataFrame(np.random.randn(7, 2)) In [25]: pd.set_option("display.max_rows", 7) In [26]: df Out[26]: 0 1 0 0.469112 -0.282863 1 -1.509059 -1.135632 2 1.212112 -0.173215 3 0.119209 -1.044236 4 -0.861849 -2.104569 5 -0.494929 1.071804 6 0.721555 -0.706771 In [27]: pd.set_option("display.max_rows", 5) In [28]: df Out[28]: 0 1 0 0.469112 -0.282863 1 -1.509059 -1.135632 .. ... ... 5 -0.494929 1.071804 6 0.721555 -0.706771 [7 rows x 2 columns] In [29]: pd.reset_option("display.max_rows") Once the display.max_rows is exceeded, the display.min_rows options determines how many rows are shown in the truncated repr. In [30]: pd.set_option("display.max_rows", 8) In [31]: pd.set_option("display.min_rows", 4) # below max_rows -> all rows shown In [32]: df = pd.DataFrame(np.random.randn(7, 2)) In [33]: df Out[33]: 0 1 0 -1.039575 0.271860 1 -0.424972 0.567020 2 0.276232 -1.087401 3 -0.673690 0.113648 4 -1.478427 0.524988 5 0.404705 0.577046 6 -1.715002 -1.039268 # above max_rows -> only min_rows (4) rows shown In [34]: df = pd.DataFrame(np.random.randn(9, 2)) In [35]: df Out[35]: 0 1 0 -0.370647 -1.157892 1 -1.344312 0.844885 .. ... ... 7 0.276662 -0.472035 8 -0.013960 -0.362543 [9 rows x 2 columns] In [36]: pd.reset_option("display.max_rows") In [37]: pd.reset_option("display.min_rows") display.expand_frame_repr allows for the representation of a DataFrame to stretch across pages, wrapped over the all the columns. In [38]: df = pd.DataFrame(np.random.randn(5, 10)) In [39]: pd.set_option("expand_frame_repr", True) In [40]: df Out[40]: 0 1 2 ... 7 8 9 0 -0.006154 -0.923061 0.895717 ... 1.340309 -1.170299 -0.226169 1 0.410835 0.813850 0.132003 ... -1.436737 -1.413681 1.607920 2 1.024180 0.569605 0.875906 ... -0.078638 0.545952 -1.219217 3 -1.226825 0.769804 -1.281247 ... 0.341734 0.959726 -1.110336 4 -0.619976 0.149748 -0.732339 ... 0.301624 -2.179861 -1.369849 [5 rows x 10 columns] In [41]: pd.set_option("expand_frame_repr", False) In [42]: df Out[42]: 0 1 2 3 4 5 6 7 8 9 0 -0.006154 -0.923061 0.895717 0.805244 -1.206412 2.565646 1.431256 1.340309 -1.170299 -0.226169 1 0.410835 0.813850 0.132003 -0.827317 -0.076467 -1.187678 1.130127 -1.436737 -1.413681 1.607920 2 1.024180 0.569605 0.875906 -2.211372 0.974466 -2.006747 -0.410001 -0.078638 0.545952 -1.219217 3 -1.226825 0.769804 -1.281247 -0.727707 -0.121306 -0.097883 0.695775 0.341734 0.959726 -1.110336 4 -0.619976 0.149748 -0.732339 0.687738 0.176444 0.403310 -0.154951 0.301624 -2.179861 -1.369849 In [43]: pd.reset_option("expand_frame_repr") display.large_repr displays a DataFrame that exceed max_columns or max_rows as a truncated frame or summary. In [44]: df = pd.DataFrame(np.random.randn(10, 10)) In [45]: pd.set_option("display.max_rows", 5) In [46]: pd.set_option("large_repr", "truncate") In [47]: df Out[47]: 0 1 2 ... 7 8 9 0 -0.954208 1.462696 -1.743161 ... 0.995761 2.396780 0.014871 1 3.357427 -0.317441 -1.236269 ... 0.380396 0.084844 0.432390 .. ... ... ... ... ... ... ... 8 -0.303421 -0.858447 0.306996 ... 0.476720 0.473424 -0.242861 9 -0.014805 -0.284319 0.650776 ... 1.613616 0.464000 0.227371 [10 rows x 10 columns] In [48]: pd.set_option("large_repr", "info") In [49]: df Out[49]: <class 'pandas.core.frame.DataFrame'> RangeIndex: 10 entries, 0 to 9 Data columns (total 10 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 0 10 non-null float64 1 1 10 non-null float64 2 2 10 non-null float64 3 3 10 non-null float64 4 4 10 non-null float64 5 5 10 non-null float64 6 6 10 non-null float64 7 7 10 non-null float64 8 8 10 non-null float64 9 9 10 non-null float64 dtypes: float64(10) memory usage: 928.0 bytes In [50]: pd.reset_option("large_repr") In [51]: pd.reset_option("display.max_rows") display.max_colwidth sets the maximum width of columns. Cells of this length or longer will be truncated with an ellipsis. In [52]: df = pd.DataFrame( ....: np.array( ....: [ ....: ["foo", "bar", "bim", "uncomfortably long string"], ....: ["horse", "cow", "banana", "apple"], ....: ] ....: ) ....: ) ....: In [53]: pd.set_option("max_colwidth", 40) In [54]: df Out[54]: 0 1 2 3 0 foo bar bim uncomfortably long string 1 horse cow banana apple In [55]: pd.set_option("max_colwidth", 6) In [56]: df Out[56]: 0 1 2 3 0 foo bar bim un... 1 horse cow ba... apple In [57]: pd.reset_option("max_colwidth") display.max_info_columns sets a threshold for the number of columns displayed when calling info(). In [58]: df = pd.DataFrame(np.random.randn(10, 10)) In [59]: pd.set_option("max_info_columns", 11) In [60]: df.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 10 entries, 0 to 9 Data columns (total 10 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 0 10 non-null float64 1 1 10 non-null float64 2 2 10 non-null float64 3 3 10 non-null float64 4 4 10 non-null float64 5 5 10 non-null float64 6 6 10 non-null float64 7 7 10 non-null float64 8 8 10 non-null float64 9 9 10 non-null float64 dtypes: float64(10) memory usage: 928.0 bytes In [61]: pd.set_option("max_info_columns", 5) In [62]: df.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 10 entries, 0 to 9 Columns: 10 entries, 0 to 9 dtypes: float64(10) memory usage: 928.0 bytes In [63]: pd.reset_option("max_info_columns") display.max_info_rows: info() will usually show null-counts for each column. For a large DataFrame, this can be quite slow. max_info_rows and max_info_cols limit this null check to the specified rows and columns respectively. The info() keyword argument null_counts=True will override this. In [64]: df = pd.DataFrame(np.random.choice([0, 1, np.nan], size=(10, 10))) In [65]: df Out[65]: 0 1 2 3 4 5 6 7 8 9 0 0.0 NaN 1.0 NaN NaN 0.0 NaN 0.0 NaN 1.0 1 1.0 NaN 1.0 1.0 1.0 1.0 NaN 0.0 0.0 NaN 2 0.0 NaN 1.0 0.0 0.0 NaN NaN NaN NaN 0.0 3 NaN NaN NaN 0.0 1.0 1.0 NaN 1.0 NaN 1.0 4 0.0 NaN NaN NaN 0.0 NaN NaN NaN 1.0 0.0 5 0.0 1.0 1.0 1.0 1.0 0.0 NaN NaN 1.0 0.0 6 1.0 1.0 1.0 NaN 1.0 NaN 1.0 0.0 NaN NaN 7 0.0 0.0 1.0 0.0 1.0 0.0 1.0 1.0 0.0 NaN 8 NaN NaN NaN 0.0 NaN NaN NaN NaN 1.0 NaN 9 0.0 NaN 0.0 NaN NaN 0.0 NaN 1.0 1.0 0.0 In [66]: pd.set_option("max_info_rows", 11) In [67]: df.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 10 entries, 0 to 9 Data columns (total 10 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 0 8 non-null float64 1 1 3 non-null float64 2 2 7 non-null float64 3 3 6 non-null float64 4 4 7 non-null float64 5 5 6 non-null float64 6 6 2 non-null float64 7 7 6 non-null float64 8 8 6 non-null float64 9 9 6 non-null float64 dtypes: float64(10) memory usage: 928.0 bytes In [68]: pd.set_option("max_info_rows", 5) In [69]: df.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 10 entries, 0 to 9 Data columns (total 10 columns): # Column Dtype --- ------ ----- 0 0 float64 1 1 float64 2 2 float64 3 3 float64 4 4 float64 5 5 float64 6 6 float64 7 7 float64 8 8 float64 9 9 float64 dtypes: float64(10) memory usage: 928.0 bytes In [70]: pd.reset_option("max_info_rows") display.precision sets the output display precision in terms of decimal places. In [71]: df = pd.DataFrame(np.random.randn(5, 5)) In [72]: pd.set_option("display.precision", 7) In [73]: df Out[73]: 0 1 2 3 4 0 -1.1506406 -0.7983341 -0.5576966 0.3813531 1.3371217 1 -1.5310949 1.3314582 -0.5713290 -0.0266708 -1.0856630 2 -1.1147378 -0.0582158 -0.4867681 1.6851483 0.1125723 3 -1.4953086 0.8984347 -0.1482168 -1.5960698 0.1596530 4 0.2621358 0.0362196 0.1847350 -0.2550694 -0.2710197 In [74]: pd.set_option("display.precision", 4) In [75]: df Out[75]: 0 1 2 3 4 0 -1.1506 -0.7983 -0.5577 0.3814 1.3371 1 -1.5311 1.3315 -0.5713 -0.0267 -1.0857 2 -1.1147 -0.0582 -0.4868 1.6851 0.1126 3 -1.4953 0.8984 -0.1482 -1.5961 0.1597 4 0.2621 0.0362 0.1847 -0.2551 -0.2710 display.chop_threshold sets the rounding threshold to zero when displaying a Series or DataFrame. This setting does not change the precision at which the number is stored. In [76]: df = pd.DataFrame(np.random.randn(6, 6)) In [77]: pd.set_option("chop_threshold", 0) In [78]: df Out[78]: 0 1 2 3 4 5 0 1.2884 0.2946 -1.1658 0.8470 -0.6856 0.6091 1 -0.3040 0.6256 -0.0593 0.2497 1.1039 -1.0875 2 1.9980 -0.2445 0.1362 0.8863 -1.3507 -0.8863 3 -1.0133 1.9209 -0.3882 -2.3144 0.6655 0.4026 4 0.3996 -1.7660 0.8504 0.3881 0.9923 0.7441 5 -0.7398 -1.0549 -0.1796 0.6396 1.5850 1.9067 In [79]: pd.set_option("chop_threshold", 0.5) In [80]: df Out[80]: 0 1 2 3 4 5 0 1.2884 0.0000 -1.1658 0.8470 -0.6856 0.6091 1 0.0000 0.6256 0.0000 0.0000 1.1039 -1.0875 2 1.9980 0.0000 0.0000 0.8863 -1.3507 -0.8863 3 -1.0133 1.9209 0.0000 -2.3144 0.6655 0.0000 4 0.0000 -1.7660 0.8504 0.0000 0.9923 0.7441 5 -0.7398 -1.0549 0.0000 0.6396 1.5850 1.9067 In [81]: pd.reset_option("chop_threshold") display.colheader_justify controls the justification of the headers. The options are 'right', and 'left'. In [82]: df = pd.DataFrame( ....: np.array([np.random.randn(6), np.random.randint(1, 9, 6) * 0.1, np.zeros(6)]).T, ....: columns=["A", "B", "C"], ....: dtype="float", ....: ) ....: In [83]: pd.set_option("colheader_justify", "right") In [84]: df Out[84]: A B C 0 0.1040 0.1 0.0 1 0.1741 0.5 0.0 2 -0.4395 0.4 0.0 3 -0.7413 0.8 0.0 4 -0.0797 0.4 0.0 5 -0.9229 0.3 0.0 In [85]: pd.set_option("colheader_justify", "left") In [86]: df Out[86]: A B C 0 0.1040 0.1 0.0 1 0.1741 0.5 0.0 2 -0.4395 0.4 0.0 3 -0.7413 0.8 0.0 4 -0.0797 0.4 0.0 5 -0.9229 0.3 0.0 In [87]: pd.reset_option("colheader_justify") Number formatting# pandas also allows you to set how numbers are displayed in the console. This option is not set through the set_options API. Use the set_eng_float_format function to alter the floating-point formatting of pandas objects to produce a particular format. In [88]: import numpy as np In [89]: pd.set_eng_float_format(accuracy=3, use_eng_prefix=True) In [90]: s = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"]) In [91]: s / 1.0e3 Out[91]: a 303.638u b -721.084u c -622.696u d 648.250u e -1.945m dtype: float64 In [92]: s / 1.0e6 Out[92]: a 303.638n b -721.084n c -622.696n d 648.250n e -1.945u dtype: float64 Use round() to specifically control rounding of an individual DataFrame Unicode formatting# Warning Enabling this option will affect the performance for printing of DataFrame and Series (about 2 times slower). Use only when it is actually required. Some East Asian countries use Unicode characters whose width corresponds to two Latin characters. If a DataFrame or Series contains these characters, the default output mode may not align them properly. In [93]: df = pd.DataFrame({"国籍": ["UK", "日本"], "名前": ["Alice", "しのぶ"]}) In [94]: df Out[94]: 国籍 名前 0 UK Alice 1 日本 しのぶ Enabling display.unicode.east_asian_width allows pandas to check each character’s “East Asian Width” property. These characters can be aligned properly by setting this option to True. However, this will result in longer render times than the standard len function. In [95]: pd.set_option("display.unicode.east_asian_width", True) In [96]: df Out[96]: 国籍 名前 0 UK Alice 1 日本 しのぶ In addition, Unicode characters whose width is “ambiguous” can either be 1 or 2 characters wide depending on the terminal setting or encoding. The option display.unicode.ambiguous_as_wide can be used to handle the ambiguity. By default, an “ambiguous” character’s width, such as “¡” (inverted exclamation) in the example below, is taken to be 1. In [97]: df = pd.DataFrame({"a": ["xxx", "¡¡"], "b": ["yyy", "¡¡"]}) In [98]: df Out[98]: a b 0 xxx yyy 1 ¡¡ ¡¡ Enabling display.unicode.ambiguous_as_wide makes pandas interpret these characters’ widths to be 2. (Note that this option will only be effective when display.unicode.east_asian_width is enabled.) However, setting this option incorrectly for your terminal will cause these characters to be aligned incorrectly: In [99]: pd.set_option("display.unicode.ambiguous_as_wide", True) In [100]: df Out[100]: a b 0 xxx yyy 1 ¡¡ ¡¡ Table schema display# DataFrame and Series will publish a Table Schema representation by default. This can be enabled globally with the display.html.table_schema option: In [101]: pd.set_option("display.html.table_schema", True) Only 'display.max_rows' are serialized and published.
462
890
How to choose items out of many options in pandas This question Calculate points with pandas did not capture well what I wanted to ask so I ask it here. I have this data here df = pd.DataFrame({'ENG':[10,3,5,6,3],'KIS':[9,5,7,9,10],'BIO':[10,'',4,'',4],'PHY':[9,5,'',10,12],'HIS':['','',9,7,8],'GEO':['',7,'',11,''],'CRE':[8,3,6,'','']}) My program is to calculate the points for every individual in the data choosing only 4 subjects out of the 5 done. The first two subjects are compulsories. In order to get the other two subjects to add to get the 4 required, you weigh between the remaining subjects, in which you pick the best performed. My expected outcome looks like this. ENG KIS BIO PHY HIS GEO CRE POINTS 10 9 10 9 8 38 3 5 5 7 3 20 5 7 4 9 6 27 6 9 10 7 11 36 3 10 4 12 8 25 This is what I tried a = df['ENG'] + df['KIS'] + df[['BIO', 'PHY']].fillna(0).max(axis=1) + df[['HIS', 'GEO', 'CRE']].fillna(0).max(axis=1) df['POINTS'] = a print(df)
Sort each row by value and then pick the best two and sum them: df = pd.DataFrame({'ENG':[10,3,5,6,3],'KIS':[9,5,7,9,10],'BIO':[10,'',4,'',4],'PHY':[9,5,'',10,12],'HIS':['','',9,7,8],'GEO':['',7,'',11,''],'CRE':[8,3,6,'','']}) df = df.replace('',0) df[['BIO','PHY','HIS','GEO','CRE']].apply(lambda row: row.sort_values(ascending=False).head(2).sum() ,axis=1) + df['ENG'] + df['KIS'] 0 38 1 20 2 27 3 36 4 33
66,204,246
Finding the missing values
<p>I have a huge dataset of students, each student has its own csv file, Dataset B has 297,444 csv files, I want to know which student csv file is missing in that dataset.</p> <p>Like you see in this picture, there's no u2.csv file present in that dataset so how can I check which all of the csv files that are missing using pandas?</p> <p>Here's the code I tried so far</p> <pre><code>import pandas as pd import numpy as np import glob path = r'C:/Users/user1/Desktop/EDNET DATA/EdNet-KT4/KT4' # use your path all_files = glob.glob(path + &quot;/*.csv&quot;) li = [] for i,filename in enumerate (all_files): df = pd.read_csv(filename, ',' ,index_col=None, header=0).assign(user_iD=filename.split(&quot;\\&quot;)[-1].split(&quot;.&quot;)[0]) li.append(df) data = pd.concat(li, axis=0, ignore_index=True) df = data.copy() df.isnull().sum() df.to_feather('KT4.ftr') data1= pd.read_feather('KT4.ftr') data1.head() </code></pre> <p><a href="https://i.stack.imgur.com/fEPqa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fEPqa.png" alt="enter image description here" /></a></p> <p><a href="https://i.stack.imgur.com/LqLam.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LqLam.png" alt="enter image description here" /></a></p>
66,208,711
"2021-02-15T07:41:35.380000"
1
null
0
289
python|pandas
<h1>Solution</h1> <blockquote> <p> <strong>Note</strong>: You only need the list of files names. But what you are doing in the code you posted, is reading the contents of the files (<em>which is not what you want</em>)!</p> </blockquote> <p>You could choose to use any of the following two methods. For the sake of reproducibility, I have created some dummy data and I tested the solution on Google Colab. I found that using pandas (<strong>Method-2</strong>) was somehow faster.</p> <p><a href="https://colab.research.google.com/github/sugatoray/stackoverflow/blob/master/src/answers/Q_66204246/Q_66204246.ipynb" rel="nofollow noreferrer"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab" /></a></p> <h2>Common Code</h2> <pre class="lang-py prettyprint-override"><code>import glob # import pandas as pd all_files = glob.glob(path + &quot;/*.csv&quot;) # I am deliberately using this for # a small number of students to # test the code. num_students = 20 # 297444 </code></pre> <h2>Method-1: Simple Python Loop</h2> <ul> <li>For <code>100,000</code> files, it took around <strong>1min 29s</strong> on Google Colab.</li> <li>Run the following in a jupyter-notebook cell.</li> </ul> <pre class="lang-py prettyprint-override"><code>%%time missing_files = [] for i in range(15): student_file = f'u{i}.csv' if f'{path}/{student_file}' not in all_files: missing_files.append(student_file) #print(f&quot;Total missing: {len(missing_files)}&quot;) #print(missing_files) ## Runtime # CPU times: user 1min 29s, sys: 0 ns, total: 1min 29s # Wall time: 1min 29s </code></pre> <h2>Method-2: Process using Pandas library (<em>faster</em>) </h2> <ul> <li>For <code>100,000</code> files, it took around <strong>358 ms</strong> on Google Colab.</li> <li>Almost <strong><code>250 times</code> FASTER</strong> than method-1.</li> <li>Run the following in a jupyter-notebook cell.</li> </ul> <pre class="lang-py prettyprint-override"><code>%%time # import pandas as pd existing_student_ids = ( pd.DataFrame({'Filename': all_files}) .Filename.str.extract(f'{path}/u(?P&lt;StudentID&gt;\d+)\.csv') .astype(int) .sort_values('StudentID') .StudentID.to_list() ) missing_student_ids = list(set(range(num_students)) - set(existing_student_ids)) # print(f&quot;Total missing students: {len(missing_student_ids)}&quot;) # print(f'missing_student_ids: {missing_student_ids}') ## Runtime # CPU times: user 323 ms, sys: 31.1 ms, total: 354 ms # Wall time: 358 ms </code></pre> <h2>Dummy Data</h2> <p>Here I will define some dummy data for the purpose of making the solution reproducible and easily testable.</p> <p>I will skip the following student-ids (<code>skip_student_ids</code>) and NOT create any <code>.csv</code> file for them.</p> <pre class="lang-py prettyprint-override"><code>import os NUM_STUDENTS = 20 ## CREATE FILE NAMES num_students = NUM_STUDENTS skip_student_ids = [3, 8, 10, 13] ## --&gt; we will skip these student-ids skip_files = [f'u{i}.csv' for i in skip_student_ids] all_files = [f'u{i}.csv' for i in range(num_students) if i not in skip_student_ids] if num_students &lt;= 20: print(f'skip_files: {skip_files}') print(f'all_files: {all_files}') ## CREATE FILES path = 'test' if not os.path.exists(path): os.makedirs(path) for filename in all_files: with open(path + '/' + filename, 'w') as f: student_id = str(filename).split(&quot;.&quot;)[0].replace('u', '') content = f&quot;&quot;&quot; Filename,StudentID {filename},{student_id} &quot;&quot;&quot; f.write(content) </code></pre> <h2>References</h2> <ol> <li><p><a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.extract.html" rel="nofollow noreferrer"><code>pandas.Series.str.extract</code> - Docs</a></p> </li> <li><p><a href="https://stackoverflow.com/questions/37506645/can-i-add-message-to-the-tqdm-progressbar">Can I add message to the tqdm progressbar?</a></p> </li> </ol>
"2021-02-15T13:12:00.083000"
1
https://pandas.pydata.org/docs/user_guide/missing_data.html
Solution Note: You only need the list of files names. But what you are doing in the code you posted, is reading the contents of the files (which is not what you want)! You could choose to use any of the following two methods. For the sake of reproducibility, I have created some dummy data and I tested the solution on Google Colab. I found that using pandas (Method-2) was somehow faster. Common Code import glob # import pandas as pd all_files = glob.glob(path + "/*.csv") # I am deliberately using this for # a small number of students to # test the code. num_students = 20 # 297444 Method-1: Simple Python Loop For 100,000 files, it took around 1min 29s on Google Colab. Run the following in a jupyter-notebook cell. %%time missing_files = [] for i in range(15): student_file = f'u{i}.csv' if f'{path}/{student_file}' not in all_files: missing_files.append(student_file) #print(f"Total missing: {len(missing_files)}") #print(missing_files) ## Runtime # CPU times: user 1min 29s, sys: 0 ns, total: 1min 29s # Wall time: 1min 29s Method-2: Process using Pandas library (faster) For 100,000 files, it took around 358 ms on Google Colab. Almost 250 times FASTER than method-1. Run the following in a jupyter-notebook cell. %%time # import pandas as pd existing_student_ids = ( pd.DataFrame({'Filename': all_files}) .Filename.str.extract(f'{path}/u(?P<StudentID>\d+)\.csv') .astype(int) .sort_values('StudentID') .StudentID.to_list() ) missing_student_ids = list(set(range(num_students)) - set(existing_student_ids)) # print(f"Total missing students: {len(missing_student_ids)}") # print(f'missing_student_ids: {missing_student_ids}') ## Runtime # CPU times: user 323 ms, sys: 31.1 ms, total: 354 ms # Wall time: 358 ms Dummy Data Here I will define some dummy data for the purpose of making the solution reproducible and easily testable. I will skip the following student-ids (skip_student_ids) and NOT create any .csv file for them. import os NUM_STUDENTS = 20 ## CREATE FILE NAMES num_students = NUM_STUDENTS skip_student_ids = [3, 8, 10, 13] ## --> we will skip these student-ids skip_files = [f'u{i}.csv' for i in skip_student_ids] all_files = [f'u{i}.csv' for i in range(num_students) if i not in skip_student_ids] if num_students <= 20: print(f'skip_files: {skip_files}') print(f'all_files: {all_files}') ## CREATE FILES path = 'test' if not os.path.exists(path): os.makedirs(path) for filename in all_files: with open(path + '/' + filename, 'w') as f: student_id = str(filename).split(".")[0].replace('u', '') content = f""" Filename,StudentID {filename},{student_id} """ f.write(content) References pandas.Series.str.extract - Docs Can I add message to the tqdm progressbar?
0
2,825
Finding the missing values I have a huge dataset of students, each student has its own csv file, Dataset B has 297,444 csv files, I want to know which student csv file is missing in that dataset. Like you see in this picture, there's no u2.csv file present in that dataset so how can I check which all of the csv files that are missing using pandas? Here's the code I tried so far import pandas as pd import numpy as np import glob path = r'C:/Users/user1/Desktop/EDNET DATA/EdNet-KT4/KT4' # use your path all_files = glob.glob(path + "/*.csv") li = [] for i,filename in enumerate (all_files): df = pd.read_csv(filename, ',' ,index_col=None, header=0).assign(user_iD=filename.split("\\")[-1].split(".")[0]) li.append(df) data = pd.concat(li, axis=0, ignore_index=True) df = data.copy() df.isnull().sum() df.to_feather('KT4.ftr') data1= pd.read_feather('KT4.ftr') data1.head()
Solution Note: You only need the list of files names. But what you are doing in the code you posted, is reading the contents of the files (which is not what you want)! You could choose to use any of the following two methods. For the sake of reproducibility, I have created some dummy data and I tested the solution on Google Colab. I found that using pandas (Method-2) was somehow faster. Common Code import glob # import pandas as pd all_files = glob.glob(path + "/*.csv") # I am deliberately using this for # a small number of students to # test the code. num_students = 20 # 297444 Method-1: Simple Python Loop For 100,000 files, it took around 1min 29s on Google Colab. Run the following in a jupyter-notebook cell. %%time missing_files = [] for i in range(15): student_file = f'u{i}.csv' if f'{path}/{student_file}' not in all_files: missing_files.append(student_file) #print(f"Total missing: {len(missing_files)}") #print(missing_files) ## Runtime # CPU times: user 1min 29s, sys: 0 ns, total: 1min 29s # Wall time: 1min 29s Method-2: Process using Pandas library (faster) For 100,000 files, it took around 358 ms on Google Colab. Almost 250 times FASTER than method-1. Run the following in a jupyter-notebook cell. %%time # import pandas as pd existing_student_ids = ( pd.DataFrame({'Filename': all_files}) .Filename.str.extract(f'{path}/u(?P<StudentID>\d+)\.csv') .astype(int) .sort_values('StudentID') .StudentID.to_list() ) missing_student_ids = list(set(range(num_students)) - set(existing_student_ids)) # print(f"Total missing students: {len(missing_student_ids)}") # print(f'missing_student_ids: {missing_student_ids}') ## Runtime # CPU times: user 323 ms, sys: 31.1 ms, total: 354 ms # Wall time: 358 ms Dummy Data Here I will define some dummy data for the purpose of making the solution reproducible and easily testable. I will skip the following student-ids (skip_student_ids) and NOT create any .csv file for them. import os NUM_STUDENTS = 20 ## CREATE FILE NAMES num_students = NUM_STUDENTS skip_student_ids = [3, 8, 10, 13] ## --> we will skip these student-ids skip_files = [f'u{i}.csv' for i in skip_student_ids] all_files = [f'u{i}.csv' for i in range(num_students) if i not in skip_student_ids] if num_students <= 20: print(f'skip_files: {skip_files}') print(f'all_files: {all_files}') ## CREATE FILES path = 'test' if not os.path.exists(path): os.makedirs(path) for filename in all_files: with open(path + '/' + filename, 'w') as f: student_id = str(filename).split(".")[0].replace('u', '') content = f""" Filename,StudentID {filename},{student_id} """ f.write(content) References pandas.Series.str.extract - Docs Can I add message to the tqdm progressbar?
65,250,675
return matching word in python
<p>I wrote a script that is checking if values in 'Product content' sheet (column 'TITLE') match with values from 'Keyword list' sheet, column 'KEYWORD' (the same workbook) . Compare_title function returns true or false which is ok but I also need to know which keywords are matching so not only true/false output but also the word that is considered as 'True match'.</p> <p>The Python script is below.</p> <pre><code>import pandas as pd import re file_path ='C:/Users/User/Desktop/data.xlsx' def get_keyword(file_path): &quot;&quot;&quot; Get keywords that are active (based on value in column 'ACTIVE?') from 'KEYWORD' column from 'Hidden search' terms sheet and convert it into the list &quot;&quot;&quot; df = pd.read_excel(file_path, sheet_name='Keyword list') keywords = df['KEYWORD'].to_list() return keywords keyword_list = get_keyword(file_path) def words(phrase: str) -&gt; [str]: &quot;&quot;&quot; Splits string to words by all characters that are not letters or digits (spaces, commas etc.) &quot;&quot;&quot; return list(map(lambda x: x.lower(), filter(len, re.split(r'\W', phrase)))) def compare_title(file_path): &quot;&quot;&quot; Get title from 'Product content' sheet and compare the values with keyword_list values &quot;&quot;&quot; df = pd.read_excel(file_path, sheet_name='Product content') df = df.fillna('-') title = df['TITLE'].apply(lambda find_kw: any([keyword in words(find_kw) for keyword in keyword_list])) return title </code></pre> <p>Thanks in advance for your help.</p>
65,250,764
"2020-12-11T11:38:28.843000"
1
null
0
34
python|pandas
<p>I think this is what you're looking for:</p> <pre><code>title = df['TITLE'].apply(lambda find_kw: [keyword for keyword in keyword_list if keyword in words(find_kw)])) </code></pre> <p>This means <code>compare_title</code> will return <code>list[str]</code> instead of <code>bool</code>. If you do <code>if compare_title(...)</code> it still works as before because an empty list is falsy and a non-empty list is truthy.</p>
"2020-12-11T11:45:12.373000"
1
https://pandas.pydata.org/docs/reference/api/pandas.Series.str.extract.html
pandas.Series.str.extract# pandas.Series.str.extract# Series.str.extract(pat, flags=0, expand=True)[source]# I think this is what you're looking for: title = df['TITLE'].apply(lambda find_kw: [keyword for keyword in keyword_list if keyword in words(find_kw)])) This means compare_title will return list[str] instead of bool. If you do if compare_title(...) it still works as before because an empty list is falsy and a non-empty list is truthy. Extract capture groups in the regex pat as columns in a DataFrame. For each subject string in the Series, extract groups from the first match of regular expression pat. Parameters patstrRegular expression pattern with capturing groups. flagsint, default 0 (no flags)Flags from the re module, e.g. re.IGNORECASE, that modify regular expression matching for things like case, spaces, etc. For more details, see re. expandbool, default TrueIf True, return DataFrame with one column per capture group. If False, return a Series/Index if there is one capture group or DataFrame if there are multiple capture groups. Returns DataFrame or Series or IndexA DataFrame with one row for each subject string, and one column for each group. Any capture group names in regular expression pat will be used for column names; otherwise capture group numbers will be used. The dtype of each result column is always object, even when no match is found. If expand=False and pat has only one capture group, then return a Series (if subject is a Series) or Index (if subject is an Index). See also extractallReturns all matches (not just the first match). Examples A pattern with two groups will return a DataFrame with two columns. Non-matches will be NaN. >>> s = pd.Series(['a1', 'b2', 'c3']) >>> s.str.extract(r'([ab])(\d)') 0 1 0 a 1 1 b 2 2 NaN NaN A pattern may contain optional groups. >>> s.str.extract(r'([ab])?(\d)') 0 1 0 a 1 1 b 2 2 NaN 3 Named groups will become column names in the result. >>> s.str.extract(r'(?P<letter>[ab])(?P<digit>\d)') letter digit 0 a 1 1 b 2 2 NaN NaN A pattern with one group will return a DataFrame with one column if expand=True. >>> s.str.extract(r'[ab](\d)', expand=True) 0 0 1 1 2 2 NaN A pattern with one group will return a Series if expand=False. >>> s.str.extract(r'[ab](\d)', expand=False) 0 1 1 2 2 NaN dtype: object
113
449
return matching word in python I wrote a script that is checking if values in 'Product content' sheet (column 'TITLE') match with values from 'Keyword list' sheet, column 'KEYWORD' (the same workbook) . Compare_title function returns true or false which is ok but I also need to know which keywords are matching so not only true/false output but also the word that is considered as 'True match'. The Python script is below. import pandas as pd import re file_path ='C:/Users/User/Desktop/data.xlsx' def get_keyword(file_path): """ Get keywords that are active (based on value in column 'ACTIVE?') from 'KEYWORD' column from 'Hidden search' terms sheet and convert it into the list """ df = pd.read_excel(file_path, sheet_name='Keyword list') keywords = df['KEYWORD'].to_list() return keywords keyword_list = get_keyword(file_path) def words(phrase: str) -> [str]: """ Splits string to words by all characters that are not letters or digits (spaces, commas etc.) """ return list(map(lambda x: x.lower(), filter(len, re.split(r'\W', phrase)))) def compare_title(file_path): """ Get title from 'Product content' sheet and compare the values with keyword_list values """ df = pd.read_excel(file_path, sheet_name='Product content') df = df.fillna('-') title = df['TITLE'].apply(lambda find_kw: any([keyword in words(find_kw) for keyword in keyword_list])) return title Thanks in advance for your help.
I think this is what you're looking for: title = df['TITLE'].apply(lambda find_kw: [keyword for keyword in keyword_list if keyword in words(find_kw)])) This means compare_title will return list[str] instead of bool. If you do if compare_title(...) it still works as before because an empty list is falsy and a non-empty list is truthy.
61,998,891
How to evaluate if a group of rows contains a specific value?
<p>I want to evaluate if a <em>id</em> contains a specific <em>value</em> ('b'). The problem is, that the <em>id</em> can occur multiple times (rows). But if id '1' in row 1 contains 'a' and the id '1' in row '2' contains 'b' all the rows with id '1' should have the value true within a new column.</p> <p><strong>What I got:</strong></p> <pre><code>+---------+-----------+ | id | value | +---------------------+ | 1 | A | | 1 | A | | 1 | B | | 2 | C | | 2 | A | | 3 | B | | 4 | C | | 5 | A | +---------+-----------+ </code></pre> <p><strong>What I want:</strong></p> <pre><code>+---------+-----------+ | id |Contains_B | +---------------------+ | 1 | True | | 1 | True | | 1 | True | | 2 | False | | 2 | False | | 3 | True | | 4 | False | | 5 | False | +---------+-----------+ </code></pre> <p><strong>Logic:</strong> If at least one row with the same <em>id</em> contains a B, set the column <em>Contains_B</em> for all there <em>id's</em> to True, else to false.</p> <p><strong>What I tried:</strong></p> <pre><code>df[“Contains_B”] = df[“id”].apply(lambda x: idContainsB(df, x)) def idContainsB(df, id): df_for_id = df[df[“id”] == id] #select data for specific id is_in = “B” in df_for_id[“value”].values #evaluate if column contains b return is_in </code></pre>
61,998,944
"2020-05-25T08:49:57.883000"
2
null
1
547
python|pandas
<p>Compare value <code>B</code> by <a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.eq.html" rel="nofollow noreferrer"><code>Series.eq</code></a> first and then test if at least one <code>True</code> per groups by <a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.core.groupby.GroupBy.transform.html" rel="nofollow noreferrer"><code>GroupBy.transform</code></a> and <a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.any.html" rel="nofollow noreferrer"><code>Series.any</code></a>:</p> <pre><code>df['Contains_B'] = df['value'].eq('B').groupby(df['id']).transform('any') </code></pre> <p>Or get all <code>id</code> with <code>B</code> and then for new column use <a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.isin.html" rel="nofollow noreferrer"><code>Series.isin</code></a>:</p> <pre><code>df['Contains_B'] = df['id'].isin(df.loc[df['value'].eq('B'), 'id']) print (df) id value Contains_B 0 1 A True 1 1 A True 2 1 B True 3 2 C False 4 2 A False 5 3 B True 6 4 C False 7 5 A False </code></pre>
"2020-05-25T08:52:19.590000"
1
https://pandas.pydata.org/docs/user_guide/groupby.html
Group by: split-apply-combine# Group by: split-apply-combine# By “group by” we are referring to a process involving one or more of the following steps: Splitting the data into groups based on some criteria. Compare value B by Series.eq first and then test if at least one True per groups by GroupBy.transform and Series.any: df['Contains_B'] = df['value'].eq('B').groupby(df['id']).transform('any') Or get all id with B and then for new column use Series.isin: df['Contains_B'] = df['id'].isin(df.loc[df['value'].eq('B'), 'id']) print (df) id value Contains_B 0 1 A True 1 1 A True 2 1 B True 3 2 C False 4 2 A False 5 3 B True 6 4 C False 7 5 A False Applying a function to each group independently. Combining the results into a data structure. Out of these, the split step is the most straightforward. In fact, in many situations we may wish to split the data set into groups and do something with those groups. In the apply step, we might wish to do one of the following: Aggregation: compute a summary statistic (or statistics) for each group. Some examples: Compute group sums or means. Compute group sizes / counts. Transformation: perform some group-specific computations and return a like-indexed object. Some examples: Standardize data (zscore) within a group. Filling NAs within groups with a value derived from each group. Filtration: discard some groups, according to a group-wise computation that evaluates True or False. Some examples: Discard data that belongs to groups with only a few members. Filter out data based on the group sum or mean. Some combination of the above: GroupBy will examine the results of the apply step and try to return a sensibly combined result if it doesn’t fit into either of the above two categories. Since the set of object instance methods on pandas data structures are generally rich and expressive, we often simply want to invoke, say, a DataFrame function on each group. The name GroupBy should be quite familiar to those who have used a SQL-based tool (or itertools), in which you can write code like: SELECT Column1, Column2, mean(Column3), sum(Column4) FROM SomeTable GROUP BY Column1, Column2 We aim to make operations like this natural and easy to express using pandas. We’ll address each area of GroupBy functionality then provide some non-trivial examples / use cases. See the cookbook for some advanced strategies. Splitting an object into groups# pandas objects can be split on any of their axes. The abstract definition of grouping is to provide a mapping of labels to group names. To create a GroupBy object (more on what the GroupBy object is later), you may do the following: In [1]: df = pd.DataFrame( ...: [ ...: ("bird", "Falconiformes", 389.0), ...: ("bird", "Psittaciformes", 24.0), ...: ("mammal", "Carnivora", 80.2), ...: ("mammal", "Primates", np.nan), ...: ("mammal", "Carnivora", 58), ...: ], ...: index=["falcon", "parrot", "lion", "monkey", "leopard"], ...: columns=("class", "order", "max_speed"), ...: ) ...: In [2]: df Out[2]: class order max_speed falcon bird Falconiformes 389.0 parrot bird Psittaciformes 24.0 lion mammal Carnivora 80.2 monkey mammal Primates NaN leopard mammal Carnivora 58.0 # default is axis=0 In [3]: grouped = df.groupby("class") In [4]: grouped = df.groupby("order", axis="columns") In [5]: grouped = df.groupby(["class", "order"]) The mapping can be specified many different ways: A Python function, to be called on each of the axis labels. A list or NumPy array of the same length as the selected axis. A dict or Series, providing a label -> group name mapping. For DataFrame objects, a string indicating either a column name or an index level name to be used to group. df.groupby('A') is just syntactic sugar for df.groupby(df['A']). A list of any of the above things. Collectively we refer to the grouping objects as the keys. For example, consider the following DataFrame: Note A string passed to groupby may refer to either a column or an index level. If a string matches both a column name and an index level name, a ValueError will be raised. In [6]: df = pd.DataFrame( ...: { ...: "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"], ...: "B": ["one", "one", "two", "three", "two", "two", "one", "three"], ...: "C": np.random.randn(8), ...: "D": np.random.randn(8), ...: } ...: ) ...: In [7]: df Out[7]: A B C D 0 foo one 0.469112 -0.861849 1 bar one -0.282863 -2.104569 2 foo two -1.509059 -0.494929 3 bar three -1.135632 1.071804 4 foo two 1.212112 0.721555 5 bar two -0.173215 -0.706771 6 foo one 0.119209 -1.039575 7 foo three -1.044236 0.271860 On a DataFrame, we obtain a GroupBy object by calling groupby(). We could naturally group by either the A or B columns, or both: In [8]: grouped = df.groupby("A") In [9]: grouped = df.groupby(["A", "B"]) If we also have a MultiIndex on columns A and B, we can group by all but the specified columns In [10]: df2 = df.set_index(["A", "B"]) In [11]: grouped = df2.groupby(level=df2.index.names.difference(["B"])) In [12]: grouped.sum() Out[12]: C D A bar -1.591710 -1.739537 foo -0.752861 -1.402938 These will split the DataFrame on its index (rows). We could also split by the columns: In [13]: def get_letter_type(letter): ....: if letter.lower() in 'aeiou': ....: return 'vowel' ....: else: ....: return 'consonant' ....: In [14]: grouped = df.groupby(get_letter_type, axis=1) pandas Index objects support duplicate values. If a non-unique index is used as the group key in a groupby operation, all values for the same index value will be considered to be in one group and thus the output of aggregation functions will only contain unique index values: In [15]: lst = [1, 2, 3, 1, 2, 3] In [16]: s = pd.Series([1, 2, 3, 10, 20, 30], lst) In [17]: grouped = s.groupby(level=0) In [18]: grouped.first() Out[18]: 1 1 2 2 3 3 dtype: int64 In [19]: grouped.last() Out[19]: 1 10 2 20 3 30 dtype: int64 In [20]: grouped.sum() Out[20]: 1 11 2 22 3 33 dtype: int64 Note that no splitting occurs until it’s needed. Creating the GroupBy object only verifies that you’ve passed a valid mapping. Note Many kinds of complicated data manipulations can be expressed in terms of GroupBy operations (though can’t be guaranteed to be the most efficient). You can get quite creative with the label mapping functions. GroupBy sorting# By default the group keys are sorted during the groupby operation. You may however pass sort=False for potential speedups: In [21]: df2 = pd.DataFrame({"X": ["B", "B", "A", "A"], "Y": [1, 2, 3, 4]}) In [22]: df2.groupby(["X"]).sum() Out[22]: Y X A 7 B 3 In [23]: df2.groupby(["X"], sort=False).sum() Out[23]: Y X B 3 A 7 Note that groupby will preserve the order in which observations are sorted within each group. For example, the groups created by groupby() below are in the order they appeared in the original DataFrame: In [24]: df3 = pd.DataFrame({"X": ["A", "B", "A", "B"], "Y": [1, 4, 3, 2]}) In [25]: df3.groupby(["X"]).get_group("A") Out[25]: X Y 0 A 1 2 A 3 In [26]: df3.groupby(["X"]).get_group("B") Out[26]: X Y 1 B 4 3 B 2 New in version 1.1.0. GroupBy dropna# By default NA values are excluded from group keys during the groupby operation. However, in case you want to include NA values in group keys, you could pass dropna=False to achieve it. In [27]: df_list = [[1, 2, 3], [1, None, 4], [2, 1, 3], [1, 2, 2]] In [28]: df_dropna = pd.DataFrame(df_list, columns=["a", "b", "c"]) In [29]: df_dropna Out[29]: a b c 0 1 2.0 3 1 1 NaN 4 2 2 1.0 3 3 1 2.0 2 # Default ``dropna`` is set to True, which will exclude NaNs in keys In [30]: df_dropna.groupby(by=["b"], dropna=True).sum() Out[30]: a c b 1.0 2 3 2.0 2 5 # In order to allow NaN in keys, set ``dropna`` to False In [31]: df_dropna.groupby(by=["b"], dropna=False).sum() Out[31]: a c b 1.0 2 3 2.0 2 5 NaN 1 4 The default setting of dropna argument is True which means NA are not included in group keys. GroupBy object attributes# The groups attribute is a dict whose keys are the computed unique groups and corresponding values being the axis labels belonging to each group. In the above example we have: In [32]: df.groupby("A").groups Out[32]: {'bar': [1, 3, 5], 'foo': [0, 2, 4, 6, 7]} In [33]: df.groupby(get_letter_type, axis=1).groups Out[33]: {'consonant': ['B', 'C', 'D'], 'vowel': ['A']} Calling the standard Python len function on the GroupBy object just returns the length of the groups dict, so it is largely just a convenience: In [34]: grouped = df.groupby(["A", "B"]) In [35]: grouped.groups Out[35]: {('bar', 'one'): [1], ('bar', 'three'): [3], ('bar', 'two'): [5], ('foo', 'one'): [0, 6], ('foo', 'three'): [7], ('foo', 'two'): [2, 4]} In [36]: len(grouped) Out[36]: 6 GroupBy will tab complete column names (and other attributes): In [37]: df Out[37]: height weight gender 2000-01-01 42.849980 157.500553 male 2000-01-02 49.607315 177.340407 male 2000-01-03 56.293531 171.524640 male 2000-01-04 48.421077 144.251986 female 2000-01-05 46.556882 152.526206 male 2000-01-06 68.448851 168.272968 female 2000-01-07 70.757698 136.431469 male 2000-01-08 58.909500 176.499753 female 2000-01-09 76.435631 174.094104 female 2000-01-10 45.306120 177.540920 male In [38]: gb = df.groupby("gender") In [39]: gb.<TAB> # noqa: E225, E999 gb.agg gb.boxplot gb.cummin gb.describe gb.filter gb.get_group gb.height gb.last gb.median gb.ngroups gb.plot gb.rank gb.std gb.transform gb.aggregate gb.count gb.cumprod gb.dtype gb.first gb.groups gb.hist gb.max gb.min gb.nth gb.prod gb.resample gb.sum gb.var gb.apply gb.cummax gb.cumsum gb.fillna gb.gender gb.head gb.indices gb.mean gb.name gb.ohlc gb.quantile gb.size gb.tail gb.weight GroupBy with MultiIndex# With hierarchically-indexed data, it’s quite natural to group by one of the levels of the hierarchy. Let’s create a Series with a two-level MultiIndex. In [40]: arrays = [ ....: ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"], ....: ["one", "two", "one", "two", "one", "two", "one", "two"], ....: ] ....: In [41]: index = pd.MultiIndex.from_arrays(arrays, names=["first", "second"]) In [42]: s = pd.Series(np.random.randn(8), index=index) In [43]: s Out[43]: first second bar one -0.919854 two -0.042379 baz one 1.247642 two -0.009920 foo one 0.290213 two 0.495767 qux one 0.362949 two 1.548106 dtype: float64 We can then group by one of the levels in s. In [44]: grouped = s.groupby(level=0) In [45]: grouped.sum() Out[45]: first bar -0.962232 baz 1.237723 foo 0.785980 qux 1.911055 dtype: float64 If the MultiIndex has names specified, these can be passed instead of the level number: In [46]: s.groupby(level="second").sum() Out[46]: second one 0.980950 two 1.991575 dtype: float64 Grouping with multiple levels is supported. In [47]: s Out[47]: first second third bar doo one -1.131345 two -0.089329 baz bee one 0.337863 two -0.945867 foo bop one -0.932132 two 1.956030 qux bop one 0.017587 two -0.016692 dtype: float64 In [48]: s.groupby(level=["first", "second"]).sum() Out[48]: first second bar doo -1.220674 baz bee -0.608004 foo bop 1.023898 qux bop 0.000895 dtype: float64 Index level names may be supplied as keys. In [49]: s.groupby(["first", "second"]).sum() Out[49]: first second bar doo -1.220674 baz bee -0.608004 foo bop 1.023898 qux bop 0.000895 dtype: float64 More on the sum function and aggregation later. Grouping DataFrame with Index levels and columns# A DataFrame may be grouped by a combination of columns and index levels by specifying the column names as strings and the index levels as pd.Grouper objects. In [50]: arrays = [ ....: ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"], ....: ["one", "two", "one", "two", "one", "two", "one", "two"], ....: ] ....: In [51]: index = pd.MultiIndex.from_arrays(arrays, names=["first", "second"]) In [52]: df = pd.DataFrame({"A": [1, 1, 1, 1, 2, 2, 3, 3], "B": np.arange(8)}, index=index) In [53]: df Out[53]: A B first second bar one 1 0 two 1 1 baz one 1 2 two 1 3 foo one 2 4 two 2 5 qux one 3 6 two 3 7 The following example groups df by the second index level and the A column. In [54]: df.groupby([pd.Grouper(level=1), "A"]).sum() Out[54]: B second A one 1 2 2 4 3 6 two 1 4 2 5 3 7 Index levels may also be specified by name. In [55]: df.groupby([pd.Grouper(level="second"), "A"]).sum() Out[55]: B second A one 1 2 2 4 3 6 two 1 4 2 5 3 7 Index level names may be specified as keys directly to groupby. In [56]: df.groupby(["second", "A"]).sum() Out[56]: B second A one 1 2 2 4 3 6 two 1 4 2 5 3 7 DataFrame column selection in GroupBy# Once you have created the GroupBy object from a DataFrame, you might want to do something different for each of the columns. Thus, using [] similar to getting a column from a DataFrame, you can do: In [57]: df = pd.DataFrame( ....: { ....: "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"], ....: "B": ["one", "one", "two", "three", "two", "two", "one", "three"], ....: "C": np.random.randn(8), ....: "D": np.random.randn(8), ....: } ....: ) ....: In [58]: df Out[58]: A B C D 0 foo one -0.575247 1.346061 1 bar one 0.254161 1.511763 2 foo two -1.143704 1.627081 3 bar three 0.215897 -0.990582 4 foo two 1.193555 -0.441652 5 bar two -0.077118 1.211526 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 In [59]: grouped = df.groupby(["A"]) In [60]: grouped_C = grouped["C"] In [61]: grouped_D = grouped["D"] This is mainly syntactic sugar for the alternative and much more verbose: In [62]: df["C"].groupby(df["A"]) Out[62]: <pandas.core.groupby.generic.SeriesGroupBy object at 0x7f1ea100a490> Additionally this method avoids recomputing the internal grouping information derived from the passed key. Iterating through groups# With the GroupBy object in hand, iterating through the grouped data is very natural and functions similarly to itertools.groupby(): In [63]: grouped = df.groupby('A') In [64]: for name, group in grouped: ....: print(name) ....: print(group) ....: bar A B C D 1 bar one 0.254161 1.511763 3 bar three 0.215897 -0.990582 5 bar two -0.077118 1.211526 foo A B C D 0 foo one -0.575247 1.346061 2 foo two -1.143704 1.627081 4 foo two 1.193555 -0.441652 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 In the case of grouping by multiple keys, the group name will be a tuple: In [65]: for name, group in df.groupby(['A', 'B']): ....: print(name) ....: print(group) ....: ('bar', 'one') A B C D 1 bar one 0.254161 1.511763 ('bar', 'three') A B C D 3 bar three 0.215897 -0.990582 ('bar', 'two') A B C D 5 bar two -0.077118 1.211526 ('foo', 'one') A B C D 0 foo one -0.575247 1.346061 6 foo one -0.408530 0.268520 ('foo', 'three') A B C D 7 foo three -0.862495 0.02458 ('foo', 'two') A B C D 2 foo two -1.143704 1.627081 4 foo two 1.193555 -0.441652 See Iterating through groups. Selecting a group# A single group can be selected using get_group(): In [66]: grouped.get_group("bar") Out[66]: A B C D 1 bar one 0.254161 1.511763 3 bar three 0.215897 -0.990582 5 bar two -0.077118 1.211526 Or for an object grouped on multiple columns: In [67]: df.groupby(["A", "B"]).get_group(("bar", "one")) Out[67]: A B C D 1 bar one 0.254161 1.511763 Aggregation# Once the GroupBy object has been created, several methods are available to perform a computation on the grouped data. These operations are similar to the aggregating API, window API, and resample API. An obvious one is aggregation via the aggregate() or equivalently agg() method: In [68]: grouped = df.groupby("A") In [69]: grouped[["C", "D"]].aggregate(np.sum) Out[69]: C D A bar 0.392940 1.732707 foo -1.796421 2.824590 In [70]: grouped = df.groupby(["A", "B"]) In [71]: grouped.aggregate(np.sum) Out[71]: C D A B bar one 0.254161 1.511763 three 0.215897 -0.990582 two -0.077118 1.211526 foo one -0.983776 1.614581 three -0.862495 0.024580 two 0.049851 1.185429 As you can see, the result of the aggregation will have the group names as the new index along the grouped axis. In the case of multiple keys, the result is a MultiIndex by default, though this can be changed by using the as_index option: In [72]: grouped = df.groupby(["A", "B"], as_index=False) In [73]: grouped.aggregate(np.sum) Out[73]: A B C D 0 bar one 0.254161 1.511763 1 bar three 0.215897 -0.990582 2 bar two -0.077118 1.211526 3 foo one -0.983776 1.614581 4 foo three -0.862495 0.024580 5 foo two 0.049851 1.185429 In [74]: df.groupby("A", as_index=False)[["C", "D"]].sum() Out[74]: A C D 0 bar 0.392940 1.732707 1 foo -1.796421 2.824590 Note that you could use the reset_index DataFrame function to achieve the same result as the column names are stored in the resulting MultiIndex: In [75]: df.groupby(["A", "B"]).sum().reset_index() Out[75]: A B C D 0 bar one 0.254161 1.511763 1 bar three 0.215897 -0.990582 2 bar two -0.077118 1.211526 3 foo one -0.983776 1.614581 4 foo three -0.862495 0.024580 5 foo two 0.049851 1.185429 Another simple aggregation example is to compute the size of each group. This is included in GroupBy as the size method. It returns a Series whose index are the group names and whose values are the sizes of each group. In [76]: grouped.size() Out[76]: A B size 0 bar one 1 1 bar three 1 2 bar two 1 3 foo one 2 4 foo three 1 5 foo two 2 In [77]: grouped.describe() Out[77]: C ... D count mean std min ... 25% 50% 75% max 0 1.0 0.254161 NaN 0.254161 ... 1.511763 1.511763 1.511763 1.511763 1 1.0 0.215897 NaN 0.215897 ... -0.990582 -0.990582 -0.990582 -0.990582 2 1.0 -0.077118 NaN -0.077118 ... 1.211526 1.211526 1.211526 1.211526 3 2.0 -0.491888 0.117887 -0.575247 ... 0.537905 0.807291 1.076676 1.346061 4 1.0 -0.862495 NaN -0.862495 ... 0.024580 0.024580 0.024580 0.024580 5 2.0 0.024925 1.652692 -1.143704 ... 0.075531 0.592714 1.109898 1.627081 [6 rows x 16 columns] Another aggregation example is to compute the number of unique values of each group. This is similar to the value_counts function, except that it only counts unique values. In [78]: ll = [['foo', 1], ['foo', 2], ['foo', 2], ['bar', 1], ['bar', 1]] In [79]: df4 = pd.DataFrame(ll, columns=["A", "B"]) In [80]: df4 Out[80]: A B 0 foo 1 1 foo 2 2 foo 2 3 bar 1 4 bar 1 In [81]: df4.groupby("A")["B"].nunique() Out[81]: A bar 1 foo 2 Name: B, dtype: int64 Note Aggregation functions will not return the groups that you are aggregating over if they are named columns, when as_index=True, the default. The grouped columns will be the indices of the returned object. Passing as_index=False will return the groups that you are aggregating over, if they are named columns. Aggregating functions are the ones that reduce the dimension of the returned objects. Some common aggregating functions are tabulated below: Function Description mean() Compute mean of groups sum() Compute sum of group values size() Compute group sizes count() Compute count of group std() Standard deviation of groups var() Compute variance of groups sem() Standard error of the mean of groups describe() Generates descriptive statistics first() Compute first of group values last() Compute last of group values nth() Take nth value, or a subset if n is a list min() Compute min of group values max() Compute max of group values The aggregating functions above will exclude NA values. Any function which reduces a Series to a scalar value is an aggregation function and will work, a trivial example is df.groupby('A').agg(lambda ser: 1). Note that nth() can act as a reducer or a filter, see here. Applying multiple functions at once# With grouped Series you can also pass a list or dict of functions to do aggregation with, outputting a DataFrame: In [82]: grouped = df.groupby("A") In [83]: grouped["C"].agg([np.sum, np.mean, np.std]) Out[83]: sum mean std A bar 0.392940 0.130980 0.181231 foo -1.796421 -0.359284 0.912265 On a grouped DataFrame, you can pass a list of functions to apply to each column, which produces an aggregated result with a hierarchical index: In [84]: grouped[["C", "D"]].agg([np.sum, np.mean, np.std]) Out[84]: C D sum mean std sum mean std A bar 0.392940 0.130980 0.181231 1.732707 0.577569 1.366330 foo -1.796421 -0.359284 0.912265 2.824590 0.564918 0.884785 The resulting aggregations are named for the functions themselves. If you need to rename, then you can add in a chained operation for a Series like this: In [85]: ( ....: grouped["C"] ....: .agg([np.sum, np.mean, np.std]) ....: .rename(columns={"sum": "foo", "mean": "bar", "std": "baz"}) ....: ) ....: Out[85]: foo bar baz A bar 0.392940 0.130980 0.181231 foo -1.796421 -0.359284 0.912265 For a grouped DataFrame, you can rename in a similar manner: In [86]: ( ....: grouped[["C", "D"]].agg([np.sum, np.mean, np.std]).rename( ....: columns={"sum": "foo", "mean": "bar", "std": "baz"} ....: ) ....: ) ....: Out[86]: C D foo bar baz foo bar baz A bar 0.392940 0.130980 0.181231 1.732707 0.577569 1.366330 foo -1.796421 -0.359284 0.912265 2.824590 0.564918 0.884785 Note In general, the output column names should be unique. You can’t apply the same function (or two functions with the same name) to the same column. In [87]: grouped["C"].agg(["sum", "sum"]) Out[87]: sum sum A bar 0.392940 0.392940 foo -1.796421 -1.796421 pandas does allow you to provide multiple lambdas. In this case, pandas will mangle the name of the (nameless) lambda functions, appending _<i> to each subsequent lambda. In [88]: grouped["C"].agg([lambda x: x.max() - x.min(), lambda x: x.median() - x.mean()]) Out[88]: <lambda_0> <lambda_1> A bar 0.331279 0.084917 foo 2.337259 -0.215962 Named aggregation# New in version 0.25.0. To support column-specific aggregation with control over the output column names, pandas accepts the special syntax in GroupBy.agg(), known as “named aggregation”, where The keywords are the output column names The values are tuples whose first element is the column to select and the second element is the aggregation to apply to that column. pandas provides the pandas.NamedAgg namedtuple with the fields ['column', 'aggfunc'] to make it clearer what the arguments are. As usual, the aggregation can be a callable or a string alias. In [89]: animals = pd.DataFrame( ....: { ....: "kind": ["cat", "dog", "cat", "dog"], ....: "height": [9.1, 6.0, 9.5, 34.0], ....: "weight": [7.9, 7.5, 9.9, 198.0], ....: } ....: ) ....: In [90]: animals Out[90]: kind height weight 0 cat 9.1 7.9 1 dog 6.0 7.5 2 cat 9.5 9.9 3 dog 34.0 198.0 In [91]: animals.groupby("kind").agg( ....: min_height=pd.NamedAgg(column="height", aggfunc="min"), ....: max_height=pd.NamedAgg(column="height", aggfunc="max"), ....: average_weight=pd.NamedAgg(column="weight", aggfunc=np.mean), ....: ) ....: Out[91]: min_height max_height average_weight kind cat 9.1 9.5 8.90 dog 6.0 34.0 102.75 pandas.NamedAgg is just a namedtuple. Plain tuples are allowed as well. In [92]: animals.groupby("kind").agg( ....: min_height=("height", "min"), ....: max_height=("height", "max"), ....: average_weight=("weight", np.mean), ....: ) ....: Out[92]: min_height max_height average_weight kind cat 9.1 9.5 8.90 dog 6.0 34.0 102.75 If your desired output column names are not valid Python keywords, construct a dictionary and unpack the keyword arguments In [93]: animals.groupby("kind").agg( ....: **{ ....: "total weight": pd.NamedAgg(column="weight", aggfunc=sum) ....: } ....: ) ....: Out[93]: total weight kind cat 17.8 dog 205.5 Additional keyword arguments are not passed through to the aggregation functions. Only pairs of (column, aggfunc) should be passed as **kwargs. If your aggregation functions requires additional arguments, partially apply them with functools.partial(). Note For Python 3.5 and earlier, the order of **kwargs in a functions was not preserved. This means that the output column ordering would not be consistent. To ensure consistent ordering, the keys (and so output columns) will always be sorted for Python 3.5. Named aggregation is also valid for Series groupby aggregations. In this case there’s no column selection, so the values are just the functions. In [94]: animals.groupby("kind").height.agg( ....: min_height="min", ....: max_height="max", ....: ) ....: Out[94]: min_height max_height kind cat 9.1 9.5 dog 6.0 34.0 Applying different functions to DataFrame columns# By passing a dict to aggregate you can apply a different aggregation to the columns of a DataFrame: In [95]: grouped.agg({"C": np.sum, "D": lambda x: np.std(x, ddof=1)}) Out[95]: C D A bar 0.392940 1.366330 foo -1.796421 0.884785 The function names can also be strings. In order for a string to be valid it must be either implemented on GroupBy or available via dispatching: In [96]: grouped.agg({"C": "sum", "D": "std"}) Out[96]: C D A bar 0.392940 1.366330 foo -1.796421 0.884785 Cython-optimized aggregation functions# Some common aggregations, currently only sum, mean, std, and sem, have optimized Cython implementations: In [97]: df.groupby("A")[["C", "D"]].sum() Out[97]: C D A bar 0.392940 1.732707 foo -1.796421 2.824590 In [98]: df.groupby(["A", "B"]).mean() Out[98]: C D A B bar one 0.254161 1.511763 three 0.215897 -0.990582 two -0.077118 1.211526 foo one -0.491888 0.807291 three -0.862495 0.024580 two 0.024925 0.592714 Of course sum and mean are implemented on pandas objects, so the above code would work even without the special versions via dispatching (see below). Aggregations with User-Defined Functions# Users can also provide their own functions for custom aggregations. When aggregating with a User-Defined Function (UDF), the UDF should not mutate the provided Series, see Mutating with User Defined Function (UDF) methods for more information. In [99]: animals.groupby("kind")[["height"]].agg(lambda x: set(x)) Out[99]: height kind cat {9.1, 9.5} dog {34.0, 6.0} The resulting dtype will reflect that of the aggregating function. If the results from different groups have different dtypes, then a common dtype will be determined in the same way as DataFrame construction. In [100]: animals.groupby("kind")[["height"]].agg(lambda x: x.astype(int).sum()) Out[100]: height kind cat 18 dog 40 Transformation# The transform method returns an object that is indexed the same as the one being grouped. The transform function must: Return a result that is either the same size as the group chunk or broadcastable to the size of the group chunk (e.g., a scalar, grouped.transform(lambda x: x.iloc[-1])). Operate column-by-column on the group chunk. The transform is applied to the first group chunk using chunk.apply. Not perform in-place operations on the group chunk. Group chunks should be treated as immutable, and changes to a group chunk may produce unexpected results. For example, when using fillna, inplace must be False (grouped.transform(lambda x: x.fillna(inplace=False))). (Optionally) operates on the entire group chunk. If this is supported, a fast path is used starting from the second chunk. Deprecated since version 1.5.0: When using .transform on a grouped DataFrame and the transformation function returns a DataFrame, currently pandas does not align the result’s index with the input’s index. This behavior is deprecated and alignment will be performed in a future version of pandas. You can apply .to_numpy() to the result of the transformation function to avoid alignment. Similar to Aggregations with User-Defined Functions, the resulting dtype will reflect that of the transformation function. If the results from different groups have different dtypes, then a common dtype will be determined in the same way as DataFrame construction. Suppose we wished to standardize the data within each group: In [101]: index = pd.date_range("10/1/1999", periods=1100) In [102]: ts = pd.Series(np.random.normal(0.5, 2, 1100), index) In [103]: ts = ts.rolling(window=100, min_periods=100).mean().dropna() In [104]: ts.head() Out[104]: 2000-01-08 0.779333 2000-01-09 0.778852 2000-01-10 0.786476 2000-01-11 0.782797 2000-01-12 0.798110 Freq: D, dtype: float64 In [105]: ts.tail() Out[105]: 2002-09-30 0.660294 2002-10-01 0.631095 2002-10-02 0.673601 2002-10-03 0.709213 2002-10-04 0.719369 Freq: D, dtype: float64 In [106]: transformed = ts.groupby(lambda x: x.year).transform( .....: lambda x: (x - x.mean()) / x.std() .....: ) .....: We would expect the result to now have mean 0 and standard deviation 1 within each group, which we can easily check: # Original Data In [107]: grouped = ts.groupby(lambda x: x.year) In [108]: grouped.mean() Out[108]: 2000 0.442441 2001 0.526246 2002 0.459365 dtype: float64 In [109]: grouped.std() Out[109]: 2000 0.131752 2001 0.210945 2002 0.128753 dtype: float64 # Transformed Data In [110]: grouped_trans = transformed.groupby(lambda x: x.year) In [111]: grouped_trans.mean() Out[111]: 2000 -4.870756e-16 2001 -1.545187e-16 2002 4.136282e-16 dtype: float64 In [112]: grouped_trans.std() Out[112]: 2000 1.0 2001 1.0 2002 1.0 dtype: float64 We can also visually compare the original and transformed data sets. In [113]: compare = pd.DataFrame({"Original": ts, "Transformed": transformed}) In [114]: compare.plot() Out[114]: <AxesSubplot: > Transformation functions that have lower dimension outputs are broadcast to match the shape of the input array. In [115]: ts.groupby(lambda x: x.year).transform(lambda x: x.max() - x.min()) Out[115]: 2000-01-08 0.623893 2000-01-09 0.623893 2000-01-10 0.623893 2000-01-11 0.623893 2000-01-12 0.623893 ... 2002-09-30 0.558275 2002-10-01 0.558275 2002-10-02 0.558275 2002-10-03 0.558275 2002-10-04 0.558275 Freq: D, Length: 1001, dtype: float64 Alternatively, the built-in methods could be used to produce the same outputs. In [116]: max_ts = ts.groupby(lambda x: x.year).transform("max") In [117]: min_ts = ts.groupby(lambda x: x.year).transform("min") In [118]: max_ts - min_ts Out[118]: 2000-01-08 0.623893 2000-01-09 0.623893 2000-01-10 0.623893 2000-01-11 0.623893 2000-01-12 0.623893 ... 2002-09-30 0.558275 2002-10-01 0.558275 2002-10-02 0.558275 2002-10-03 0.558275 2002-10-04 0.558275 Freq: D, Length: 1001, dtype: float64 Another common data transform is to replace missing data with the group mean. In [119]: data_df Out[119]: A B C 0 1.539708 -1.166480 0.533026 1 1.302092 -0.505754 NaN 2 -0.371983 1.104803 -0.651520 3 -1.309622 1.118697 -1.161657 4 -1.924296 0.396437 0.812436 .. ... ... ... 995 -0.093110 0.683847 -0.774753 996 -0.185043 1.438572 NaN 997 -0.394469 -0.642343 0.011374 998 -1.174126 1.857148 NaN 999 0.234564 0.517098 0.393534 [1000 rows x 3 columns] In [120]: countries = np.array(["US", "UK", "GR", "JP"]) In [121]: key = countries[np.random.randint(0, 4, 1000)] In [122]: grouped = data_df.groupby(key) # Non-NA count in each group In [123]: grouped.count() Out[123]: A B C GR 209 217 189 JP 240 255 217 UK 216 231 193 US 239 250 217 In [124]: transformed = grouped.transform(lambda x: x.fillna(x.mean())) We can verify that the group means have not changed in the transformed data and that the transformed data contains no NAs. In [125]: grouped_trans = transformed.groupby(key) In [126]: grouped.mean() # original group means Out[126]: A B C GR -0.098371 -0.015420 0.068053 JP 0.069025 0.023100 -0.077324 UK 0.034069 -0.052580 -0.116525 US 0.058664 -0.020399 0.028603 In [127]: grouped_trans.mean() # transformation did not change group means Out[127]: A B C GR -0.098371 -0.015420 0.068053 JP 0.069025 0.023100 -0.077324 UK 0.034069 -0.052580 -0.116525 US 0.058664 -0.020399 0.028603 In [128]: grouped.count() # original has some missing data points Out[128]: A B C GR 209 217 189 JP 240 255 217 UK 216 231 193 US 239 250 217 In [129]: grouped_trans.count() # counts after transformation Out[129]: A B C GR 228 228 228 JP 267 267 267 UK 247 247 247 US 258 258 258 In [130]: grouped_trans.size() # Verify non-NA count equals group size Out[130]: GR 228 JP 267 UK 247 US 258 dtype: int64 Note Some functions will automatically transform the input when applied to a GroupBy object, but returning an object of the same shape as the original. Passing as_index=False will not affect these transformation methods. For example: fillna, ffill, bfill, shift.. In [131]: grouped.ffill() Out[131]: A B C 0 1.539708 -1.166480 0.533026 1 1.302092 -0.505754 0.533026 2 -0.371983 1.104803 -0.651520 3 -1.309622 1.118697 -1.161657 4 -1.924296 0.396437 0.812436 .. ... ... ... 995 -0.093110 0.683847 -0.774753 996 -0.185043 1.438572 -0.774753 997 -0.394469 -0.642343 0.011374 998 -1.174126 1.857148 -0.774753 999 0.234564 0.517098 0.393534 [1000 rows x 3 columns] Window and resample operations# It is possible to use resample(), expanding() and rolling() as methods on groupbys. The example below will apply the rolling() method on the samples of the column B based on the groups of column A. In [132]: df_re = pd.DataFrame({"A": [1] * 10 + [5] * 10, "B": np.arange(20)}) In [133]: df_re Out[133]: A B 0 1 0 1 1 1 2 1 2 3 1 3 4 1 4 .. .. .. 15 5 15 16 5 16 17 5 17 18 5 18 19 5 19 [20 rows x 2 columns] In [134]: df_re.groupby("A").rolling(4).B.mean() Out[134]: A 1 0 NaN 1 NaN 2 NaN 3 1.5 4 2.5 ... 5 15 13.5 16 14.5 17 15.5 18 16.5 19 17.5 Name: B, Length: 20, dtype: float64 The expanding() method will accumulate a given operation (sum() in the example) for all the members of each particular group. In [135]: df_re.groupby("A").expanding().sum() Out[135]: B A 1 0 0.0 1 1.0 2 3.0 3 6.0 4 10.0 ... ... 5 15 75.0 16 91.0 17 108.0 18 126.0 19 145.0 [20 rows x 1 columns] Suppose you want to use the resample() method to get a daily frequency in each group of your dataframe and wish to complete the missing values with the ffill() method. In [136]: df_re = pd.DataFrame( .....: { .....: "date": pd.date_range(start="2016-01-01", periods=4, freq="W"), .....: "group": [1, 1, 2, 2], .....: "val": [5, 6, 7, 8], .....: } .....: ).set_index("date") .....: In [137]: df_re Out[137]: group val date 2016-01-03 1 5 2016-01-10 1 6 2016-01-17 2 7 2016-01-24 2 8 In [138]: df_re.groupby("group").resample("1D").ffill() Out[138]: group val group date 1 2016-01-03 1 5 2016-01-04 1 5 2016-01-05 1 5 2016-01-06 1 5 2016-01-07 1 5 ... ... ... 2 2016-01-20 2 7 2016-01-21 2 7 2016-01-22 2 7 2016-01-23 2 7 2016-01-24 2 8 [16 rows x 2 columns] Filtration# The filter method returns a subset of the original object. Suppose we want to take only elements that belong to groups with a group sum greater than 2. In [139]: sf = pd.Series([1, 1, 2, 3, 3, 3]) In [140]: sf.groupby(sf).filter(lambda x: x.sum() > 2) Out[140]: 3 3 4 3 5 3 dtype: int64 The argument of filter must be a function that, applied to the group as a whole, returns True or False. Another useful operation is filtering out elements that belong to groups with only a couple members. In [141]: dff = pd.DataFrame({"A": np.arange(8), "B": list("aabbbbcc")}) In [142]: dff.groupby("B").filter(lambda x: len(x) > 2) Out[142]: A B 2 2 b 3 3 b 4 4 b 5 5 b Alternatively, instead of dropping the offending groups, we can return a like-indexed objects where the groups that do not pass the filter are filled with NaNs. In [143]: dff.groupby("B").filter(lambda x: len(x) > 2, dropna=False) Out[143]: A B 0 NaN NaN 1 NaN NaN 2 2.0 b 3 3.0 b 4 4.0 b 5 5.0 b 6 NaN NaN 7 NaN NaN For DataFrames with multiple columns, filters should explicitly specify a column as the filter criterion. In [144]: dff["C"] = np.arange(8) In [145]: dff.groupby("B").filter(lambda x: len(x["C"]) > 2) Out[145]: A B C 2 2 b 2 3 3 b 3 4 4 b 4 5 5 b 5 Note Some functions when applied to a groupby object will act as a filter on the input, returning a reduced shape of the original (and potentially eliminating groups), but with the index unchanged. Passing as_index=False will not affect these transformation methods. For example: head, tail. In [146]: dff.groupby("B").head(2) Out[146]: A B C 0 0 a 0 1 1 a 1 2 2 b 2 3 3 b 3 6 6 c 6 7 7 c 7 Dispatching to instance methods# When doing an aggregation or transformation, you might just want to call an instance method on each data group. This is pretty easy to do by passing lambda functions: In [147]: grouped = df.groupby("A") In [148]: grouped.agg(lambda x: x.std()) Out[148]: C D A bar 0.181231 1.366330 foo 0.912265 0.884785 But, it’s rather verbose and can be untidy if you need to pass additional arguments. Using a bit of metaprogramming cleverness, GroupBy now has the ability to “dispatch” method calls to the groups: In [149]: grouped.std() Out[149]: C D A bar 0.181231 1.366330 foo 0.912265 0.884785 What is actually happening here is that a function wrapper is being generated. When invoked, it takes any passed arguments and invokes the function with any arguments on each group (in the above example, the std function). The results are then combined together much in the style of agg and transform (it actually uses apply to infer the gluing, documented next). This enables some operations to be carried out rather succinctly: In [150]: tsdf = pd.DataFrame( .....: np.random.randn(1000, 3), .....: index=pd.date_range("1/1/2000", periods=1000), .....: columns=["A", "B", "C"], .....: ) .....: In [151]: tsdf.iloc[::2] = np.nan In [152]: grouped = tsdf.groupby(lambda x: x.year) In [153]: grouped.fillna(method="pad") Out[153]: A B C 2000-01-01 NaN NaN NaN 2000-01-02 -0.353501 -0.080957 -0.876864 2000-01-03 -0.353501 -0.080957 -0.876864 2000-01-04 0.050976 0.044273 -0.559849 2000-01-05 0.050976 0.044273 -0.559849 ... ... ... ... 2002-09-22 0.005011 0.053897 -1.026922 2002-09-23 0.005011 0.053897 -1.026922 2002-09-24 -0.456542 -1.849051 1.559856 2002-09-25 -0.456542 -1.849051 1.559856 2002-09-26 1.123162 0.354660 1.128135 [1000 rows x 3 columns] In this example, we chopped the collection of time series into yearly chunks then independently called fillna on the groups. The nlargest and nsmallest methods work on Series style groupbys: In [154]: s = pd.Series([9, 8, 7, 5, 19, 1, 4.2, 3.3]) In [155]: g = pd.Series(list("abababab")) In [156]: gb = s.groupby(g) In [157]: gb.nlargest(3) Out[157]: a 4 19.0 0 9.0 2 7.0 b 1 8.0 3 5.0 7 3.3 dtype: float64 In [158]: gb.nsmallest(3) Out[158]: a 6 4.2 2 7.0 0 9.0 b 5 1.0 7 3.3 3 5.0 dtype: float64 Flexible apply# Some operations on the grouped data might not fit into either the aggregate or transform categories. Or, you may simply want GroupBy to infer how to combine the results. For these, use the apply function, which can be substituted for both aggregate and transform in many standard use cases. However, apply can handle some exceptional use cases. Note apply can act as a reducer, transformer, or filter function, depending on exactly what is passed to it. It can depend on the passed function and exactly what you are grouping. Thus the grouped column(s) may be included in the output as well as set the indices. In [159]: df Out[159]: A B C D 0 foo one -0.575247 1.346061 1 bar one 0.254161 1.511763 2 foo two -1.143704 1.627081 3 bar three 0.215897 -0.990582 4 foo two 1.193555 -0.441652 5 bar two -0.077118 1.211526 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 In [160]: grouped = df.groupby("A") # could also just call .describe() In [161]: grouped["C"].apply(lambda x: x.describe()) Out[161]: A bar count 3.000000 mean 0.130980 std 0.181231 min -0.077118 25% 0.069390 ... foo min -1.143704 25% -0.862495 50% -0.575247 75% -0.408530 max 1.193555 Name: C, Length: 16, dtype: float64 The dimension of the returned result can also change: In [162]: grouped = df.groupby('A')['C'] In [163]: def f(group): .....: return pd.DataFrame({'original': group, .....: 'demeaned': group - group.mean()}) .....: apply on a Series can operate on a returned value from the applied function, that is itself a series, and possibly upcast the result to a DataFrame: In [164]: def f(x): .....: return pd.Series([x, x ** 2], index=["x", "x^2"]) .....: In [165]: s = pd.Series(np.random.rand(5)) In [166]: s Out[166]: 0 0.321438 1 0.493496 2 0.139505 3 0.910103 4 0.194158 dtype: float64 In [167]: s.apply(f) Out[167]: x x^2 0 0.321438 0.103323 1 0.493496 0.243538 2 0.139505 0.019462 3 0.910103 0.828287 4 0.194158 0.037697 Control grouped column(s) placement with group_keys# Note If group_keys=True is specified when calling groupby(), functions passed to apply that return like-indexed outputs will have the group keys added to the result index. Previous versions of pandas would add the group keys only when the result from the applied function had a different index than the input. If group_keys is not specified, the group keys will not be added for like-indexed outputs. In the future this behavior will change to always respect group_keys, which defaults to True. Changed in version 1.5.0. To control whether the grouped column(s) are included in the indices, you can use the argument group_keys. Compare In [168]: df.groupby("A", group_keys=True).apply(lambda x: x) Out[168]: A B C D A bar 1 bar one 0.254161 1.511763 3 bar three 0.215897 -0.990582 5 bar two -0.077118 1.211526 foo 0 foo one -0.575247 1.346061 2 foo two -1.143704 1.627081 4 foo two 1.193555 -0.441652 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 with In [169]: df.groupby("A", group_keys=False).apply(lambda x: x) Out[169]: A B C D 0 foo one -0.575247 1.346061 1 bar one 0.254161 1.511763 2 foo two -1.143704 1.627081 3 bar three 0.215897 -0.990582 4 foo two 1.193555 -0.441652 5 bar two -0.077118 1.211526 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 Similar to Aggregations with User-Defined Functions, the resulting dtype will reflect that of the apply function. If the results from different groups have different dtypes, then a common dtype will be determined in the same way as DataFrame construction. Numba Accelerated Routines# New in version 1.1. If Numba is installed as an optional dependency, the transform and aggregate methods support engine='numba' and engine_kwargs arguments. See enhancing performance with Numba for general usage of the arguments and performance considerations. The function signature must start with values, index exactly as the data belonging to each group will be passed into values, and the group index will be passed into index. Warning When using engine='numba', there will be no “fall back” behavior internally. The group data and group index will be passed as NumPy arrays to the JITed user defined function, and no alternative execution attempts will be tried. Other useful features# Automatic exclusion of “nuisance” columns# Again consider the example DataFrame we’ve been looking at: In [170]: df Out[170]: A B C D 0 foo one -0.575247 1.346061 1 bar one 0.254161 1.511763 2 foo two -1.143704 1.627081 3 bar three 0.215897 -0.990582 4 foo two 1.193555 -0.441652 5 bar two -0.077118 1.211526 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 Suppose we wish to compute the standard deviation grouped by the A column. There is a slight problem, namely that we don’t care about the data in column B. We refer to this as a “nuisance” column. You can avoid nuisance columns by specifying numeric_only=True: In [171]: df.groupby("A").std(numeric_only=True) Out[171]: C D A bar 0.181231 1.366330 foo 0.912265 0.884785 Note that df.groupby('A').colname.std(). is more efficient than df.groupby('A').std().colname, so if the result of an aggregation function is only interesting over one column (here colname), it may be filtered before applying the aggregation function. Note Any object column, also if it contains numerical values such as Decimal objects, is considered as a “nuisance” columns. They are excluded from aggregate functions automatically in groupby. If you do wish to include decimal or object columns in an aggregation with other non-nuisance data types, you must do so explicitly. Warning The automatic dropping of nuisance columns has been deprecated and will be removed in a future version of pandas. If columns are included that cannot be operated on, pandas will instead raise an error. In order to avoid this, either select the columns you wish to operate on or specify numeric_only=True. In [172]: from decimal import Decimal In [173]: df_dec = pd.DataFrame( .....: { .....: "id": [1, 2, 1, 2], .....: "int_column": [1, 2, 3, 4], .....: "dec_column": [ .....: Decimal("0.50"), .....: Decimal("0.15"), .....: Decimal("0.25"), .....: Decimal("0.40"), .....: ], .....: } .....: ) .....: # Decimal columns can be sum'd explicitly by themselves... In [174]: df_dec.groupby(["id"])[["dec_column"]].sum() Out[174]: dec_column id 1 0.75 2 0.55 # ...but cannot be combined with standard data types or they will be excluded In [175]: df_dec.groupby(["id"])[["int_column", "dec_column"]].sum() Out[175]: int_column id 1 4 2 6 # Use .agg function to aggregate over standard and "nuisance" data types # at the same time In [176]: df_dec.groupby(["id"]).agg({"int_column": "sum", "dec_column": "sum"}) Out[176]: int_column dec_column id 1 4 0.75 2 6 0.55 Handling of (un)observed Categorical values# When using a Categorical grouper (as a single grouper, or as part of multiple groupers), the observed keyword controls whether to return a cartesian product of all possible groupers values (observed=False) or only those that are observed groupers (observed=True). Show all values: In [177]: pd.Series([1, 1, 1]).groupby( .....: pd.Categorical(["a", "a", "a"], categories=["a", "b"]), observed=False .....: ).count() .....: Out[177]: a 3 b 0 dtype: int64 Show only the observed values: In [178]: pd.Series([1, 1, 1]).groupby( .....: pd.Categorical(["a", "a", "a"], categories=["a", "b"]), observed=True .....: ).count() .....: Out[178]: a 3 dtype: int64 The returned dtype of the grouped will always include all of the categories that were grouped. In [179]: s = ( .....: pd.Series([1, 1, 1]) .....: .groupby(pd.Categorical(["a", "a", "a"], categories=["a", "b"]), observed=False) .....: .count() .....: ) .....: In [180]: s.index.dtype Out[180]: CategoricalDtype(categories=['a', 'b'], ordered=False) NA and NaT group handling# If there are any NaN or NaT values in the grouping key, these will be automatically excluded. In other words, there will never be an “NA group” or “NaT group”. This was not the case in older versions of pandas, but users were generally discarding the NA group anyway (and supporting it was an implementation headache). Grouping with ordered factors# Categorical variables represented as instance of pandas’s Categorical class can be used as group keys. If so, the order of the levels will be preserved: In [181]: data = pd.Series(np.random.randn(100)) In [182]: factor = pd.qcut(data, [0, 0.25, 0.5, 0.75, 1.0]) In [183]: data.groupby(factor).mean() Out[183]: (-2.645, -0.523] -1.362896 (-0.523, 0.0296] -0.260266 (0.0296, 0.654] 0.361802 (0.654, 2.21] 1.073801 dtype: float64 Grouping with a grouper specification# You may need to specify a bit more data to properly group. You can use the pd.Grouper to provide this local control. In [184]: import datetime In [185]: df = pd.DataFrame( .....: { .....: "Branch": "A A A A A A A B".split(), .....: "Buyer": "Carl Mark Carl Carl Joe Joe Joe Carl".split(), .....: "Quantity": [1, 3, 5, 1, 8, 1, 9, 3], .....: "Date": [ .....: datetime.datetime(2013, 1, 1, 13, 0), .....: datetime.datetime(2013, 1, 1, 13, 5), .....: datetime.datetime(2013, 10, 1, 20, 0), .....: datetime.datetime(2013, 10, 2, 10, 0), .....: datetime.datetime(2013, 10, 1, 20, 0), .....: datetime.datetime(2013, 10, 2, 10, 0), .....: datetime.datetime(2013, 12, 2, 12, 0), .....: datetime.datetime(2013, 12, 2, 14, 0), .....: ], .....: } .....: ) .....: In [186]: df Out[186]: Branch Buyer Quantity Date 0 A Carl 1 2013-01-01 13:00:00 1 A Mark 3 2013-01-01 13:05:00 2 A Carl 5 2013-10-01 20:00:00 3 A Carl 1 2013-10-02 10:00:00 4 A Joe 8 2013-10-01 20:00:00 5 A Joe 1 2013-10-02 10:00:00 6 A Joe 9 2013-12-02 12:00:00 7 B Carl 3 2013-12-02 14:00:00 Groupby a specific column with the desired frequency. This is like resampling. In [187]: df.groupby([pd.Grouper(freq="1M", key="Date"), "Buyer"])[["Quantity"]].sum() Out[187]: Quantity Date Buyer 2013-01-31 Carl 1 Mark 3 2013-10-31 Carl 6 Joe 9 2013-12-31 Carl 3 Joe 9 You have an ambiguous specification in that you have a named index and a column that could be potential groupers. In [188]: df = df.set_index("Date") In [189]: df["Date"] = df.index + pd.offsets.MonthEnd(2) In [190]: df.groupby([pd.Grouper(freq="6M", key="Date"), "Buyer"])[["Quantity"]].sum() Out[190]: Quantity Date Buyer 2013-02-28 Carl 1 Mark 3 2014-02-28 Carl 9 Joe 18 In [191]: df.groupby([pd.Grouper(freq="6M", level="Date"), "Buyer"])[["Quantity"]].sum() Out[191]: Quantity Date Buyer 2013-01-31 Carl 1 Mark 3 2014-01-31 Carl 9 Joe 18 Taking the first rows of each group# Just like for a DataFrame or Series you can call head and tail on a groupby: In [192]: df = pd.DataFrame([[1, 2], [1, 4], [5, 6]], columns=["A", "B"]) In [193]: df Out[193]: A B 0 1 2 1 1 4 2 5 6 In [194]: g = df.groupby("A") In [195]: g.head(1) Out[195]: A B 0 1 2 2 5 6 In [196]: g.tail(1) Out[196]: A B 1 1 4 2 5 6 This shows the first or last n rows from each group. Taking the nth row of each group# To select from a DataFrame or Series the nth item, use nth(). This is a reduction method, and will return a single row (or no row) per group if you pass an int for n: In [197]: df = pd.DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=["A", "B"]) In [198]: g = df.groupby("A") In [199]: g.nth(0) Out[199]: B A 1 NaN 5 6.0 In [200]: g.nth(-1) Out[200]: B A 1 4.0 5 6.0 In [201]: g.nth(1) Out[201]: B A 1 4.0 If you want to select the nth not-null item, use the dropna kwarg. For a DataFrame this should be either 'any' or 'all' just like you would pass to dropna: # nth(0) is the same as g.first() In [202]: g.nth(0, dropna="any") Out[202]: B A 1 4.0 5 6.0 In [203]: g.first() Out[203]: B A 1 4.0 5 6.0 # nth(-1) is the same as g.last() In [204]: g.nth(-1, dropna="any") # NaNs denote group exhausted when using dropna Out[204]: B A 1 4.0 5 6.0 In [205]: g.last() Out[205]: B A 1 4.0 5 6.0 In [206]: g.B.nth(0, dropna="all") Out[206]: A 1 4.0 5 6.0 Name: B, dtype: float64 As with other methods, passing as_index=False, will achieve a filtration, which returns the grouped row. In [207]: df = pd.DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=["A", "B"]) In [208]: g = df.groupby("A", as_index=False) In [209]: g.nth(0) Out[209]: A B 0 1 NaN 2 5 6.0 In [210]: g.nth(-1) Out[210]: A B 1 1 4.0 2 5 6.0 You can also select multiple rows from each group by specifying multiple nth values as a list of ints. In [211]: business_dates = pd.date_range(start="4/1/2014", end="6/30/2014", freq="B") In [212]: df = pd.DataFrame(1, index=business_dates, columns=["a", "b"]) # get the first, 4th, and last date index for each month In [213]: df.groupby([df.index.year, df.index.month]).nth([0, 3, -1]) Out[213]: a b 2014 4 1 1 4 1 1 4 1 1 5 1 1 5 1 1 5 1 1 6 1 1 6 1 1 6 1 1 Enumerate group items# To see the order in which each row appears within its group, use the cumcount method: In [214]: dfg = pd.DataFrame(list("aaabba"), columns=["A"]) In [215]: dfg Out[215]: A 0 a 1 a 2 a 3 b 4 b 5 a In [216]: dfg.groupby("A").cumcount() Out[216]: 0 0 1 1 2 2 3 0 4 1 5 3 dtype: int64 In [217]: dfg.groupby("A").cumcount(ascending=False) Out[217]: 0 3 1 2 2 1 3 1 4 0 5 0 dtype: int64 Enumerate groups# To see the ordering of the groups (as opposed to the order of rows within a group given by cumcount) you can use ngroup(). Note that the numbers given to the groups match the order in which the groups would be seen when iterating over the groupby object, not the order they are first observed. In [218]: dfg = pd.DataFrame(list("aaabba"), columns=["A"]) In [219]: dfg Out[219]: A 0 a 1 a 2 a 3 b 4 b 5 a In [220]: dfg.groupby("A").ngroup() Out[220]: 0 0 1 0 2 0 3 1 4 1 5 0 dtype: int64 In [221]: dfg.groupby("A").ngroup(ascending=False) Out[221]: 0 1 1 1 2 1 3 0 4 0 5 1 dtype: int64 Plotting# Groupby also works with some plotting methods. For example, suppose we suspect that some features in a DataFrame may differ by group, in this case, the values in column 1 where the group is “B” are 3 higher on average. In [222]: np.random.seed(1234) In [223]: df = pd.DataFrame(np.random.randn(50, 2)) In [224]: df["g"] = np.random.choice(["A", "B"], size=50) In [225]: df.loc[df["g"] == "B", 1] += 3 We can easily visualize this with a boxplot: In [226]: df.groupby("g").boxplot() Out[226]: A AxesSubplot(0.1,0.15;0.363636x0.75) B AxesSubplot(0.536364,0.15;0.363636x0.75) dtype: object The result of calling boxplot is a dictionary whose keys are the values of our grouping column g (“A” and “B”). The values of the resulting dictionary can be controlled by the return_type keyword of boxplot. See the visualization documentation for more. Warning For historical reasons, df.groupby("g").boxplot() is not equivalent to df.boxplot(by="g"). See here for an explanation. Piping function calls# Similar to the functionality provided by DataFrame and Series, functions that take GroupBy objects can be chained together using a pipe method to allow for a cleaner, more readable syntax. To read about .pipe in general terms, see here. Combining .groupby and .pipe is often useful when you need to reuse GroupBy objects. As an example, imagine having a DataFrame with columns for stores, products, revenue and quantity sold. We’d like to do a groupwise calculation of prices (i.e. revenue/quantity) per store and per product. We could do this in a multi-step operation, but expressing it in terms of piping can make the code more readable. First we set the data: In [227]: n = 1000 In [228]: df = pd.DataFrame( .....: { .....: "Store": np.random.choice(["Store_1", "Store_2"], n), .....: "Product": np.random.choice(["Product_1", "Product_2"], n), .....: "Revenue": (np.random.random(n) * 50 + 10).round(2), .....: "Quantity": np.random.randint(1, 10, size=n), .....: } .....: ) .....: In [229]: df.head(2) Out[229]: Store Product Revenue Quantity 0 Store_2 Product_1 26.12 1 1 Store_2 Product_1 28.86 1 Now, to find prices per store/product, we can simply do: In [230]: ( .....: df.groupby(["Store", "Product"]) .....: .pipe(lambda grp: grp.Revenue.sum() / grp.Quantity.sum()) .....: .unstack() .....: .round(2) .....: ) .....: Out[230]: Product Product_1 Product_2 Store Store_1 6.82 7.05 Store_2 6.30 6.64 Piping can also be expressive when you want to deliver a grouped object to some arbitrary function, for example: In [231]: def mean(groupby): .....: return groupby.mean() .....: In [232]: df.groupby(["Store", "Product"]).pipe(mean) Out[232]: Revenue Quantity Store Product Store_1 Product_1 34.622727 5.075758 Product_2 35.482815 5.029630 Store_2 Product_1 32.972837 5.237589 Product_2 34.684360 5.224000 where mean takes a GroupBy object and finds the mean of the Revenue and Quantity columns respectively for each Store-Product combination. The mean function can be any function that takes in a GroupBy object; the .pipe will pass the GroupBy object as a parameter into the function you specify. Examples# Regrouping by factor# Regroup columns of a DataFrame according to their sum, and sum the aggregated ones. In [233]: df = pd.DataFrame({"a": [1, 0, 0], "b": [0, 1, 0], "c": [1, 0, 0], "d": [2, 3, 4]}) In [234]: df Out[234]: a b c d 0 1 0 1 2 1 0 1 0 3 2 0 0 0 4 In [235]: df.groupby(df.sum(), axis=1).sum() Out[235]: 1 9 0 2 2 1 1 3 2 0 4 Multi-column factorization# By using ngroup(), we can extract information about the groups in a way similar to factorize() (as described further in the reshaping API) but which applies naturally to multiple columns of mixed type and different sources. This can be useful as an intermediate categorical-like step in processing, when the relationships between the group rows are more important than their content, or as input to an algorithm which only accepts the integer encoding. (For more information about support in pandas for full categorical data, see the Categorical introduction and the API documentation.) In [236]: dfg = pd.DataFrame({"A": [1, 1, 2, 3, 2], "B": list("aaaba")}) In [237]: dfg Out[237]: A B 0 1 a 1 1 a 2 2 a 3 3 b 4 2 a In [238]: dfg.groupby(["A", "B"]).ngroup() Out[238]: 0 0 1 0 2 1 3 2 4 1 dtype: int64 In [239]: dfg.groupby(["A", [0, 0, 0, 1, 1]]).ngroup() Out[239]: 0 0 1 0 2 1 3 3 4 2 dtype: int64 Groupby by indexer to ‘resample’ data# Resampling produces new hypothetical samples (resamples) from already existing observed data or from a model that generates data. These new samples are similar to the pre-existing samples. In order to resample to work on indices that are non-datetimelike, the following procedure can be utilized. In the following examples, df.index // 5 returns a binary array which is used to determine what gets selected for the groupby operation. Note The below example shows how we can downsample by consolidation of samples into fewer samples. Here by using df.index // 5, we are aggregating the samples in bins. By applying std() function, we aggregate the information contained in many samples into a small subset of values which is their standard deviation thereby reducing the number of samples. In [240]: df = pd.DataFrame(np.random.randn(10, 2)) In [241]: df Out[241]: 0 1 0 -0.793893 0.321153 1 0.342250 1.618906 2 -0.975807 1.918201 3 -0.810847 -1.405919 4 -1.977759 0.461659 5 0.730057 -1.316938 6 -0.751328 0.528290 7 -0.257759 -1.081009 8 0.505895 -1.701948 9 -1.006349 0.020208 In [242]: df.index // 5 Out[242]: Int64Index([0, 0, 0, 0, 0, 1, 1, 1, 1, 1], dtype='int64') In [243]: df.groupby(df.index // 5).std() Out[243]: 0 1 0 0.823647 1.312912 1 0.760109 0.942941 Returning a Series to propagate names# Group DataFrame columns, compute a set of metrics and return a named Series. The Series name is used as the name for the column index. This is especially useful in conjunction with reshaping operations such as stacking in which the column index name will be used as the name of the inserted column: In [244]: df = pd.DataFrame( .....: { .....: "a": [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], .....: "b": [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1], .....: "c": [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], .....: "d": [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1], .....: } .....: ) .....: In [245]: def compute_metrics(x): .....: result = {"b_sum": x["b"].sum(), "c_mean": x["c"].mean()} .....: return pd.Series(result, name="metrics") .....: In [246]: result = df.groupby("a").apply(compute_metrics) In [247]: result Out[247]: metrics b_sum c_mean a 0 2.0 0.5 1 2.0 0.5 2 2.0 0.5 In [248]: result.stack() Out[248]: a metrics 0 b_sum 2.0 c_mean 0.5 1 b_sum 2.0 c_mean 0.5 2 b_sum 2.0 c_mean 0.5 dtype: float64
210
760
How to evaluate if a group of rows contains a specific value? I want to evaluate if a id contains a specific value ('b'). The problem is, that the id can occur multiple times (rows). But if id '1' in row 1 contains 'a' and the id '1' in row '2' contains 'b' all the rows with id '1' should have the value true within a new column. What I got: +---------+-----------+ | id | value | +---------------------+ | 1 | A | | 1 | A | | 1 | B | | 2 | C | | 2 | A | | 3 | B | | 4 | C | | 5 | A | +---------+-----------+ What I want: +---------+-----------+ | id |Contains_B | +---------------------+ | 1 | True | | 1 | True | | 1 | True | | 2 | False | | 2 | False | | 3 | True | | 4 | False | | 5 | False | +---------+-----------+ Logic: If at least one row with the same id contains a B, set the column Contains_B for all there id's to True, else to false. What I tried: df[“Contains_B”] = df[“id”].apply(lambda x: idContainsB(df, x)) def idContainsB(df, id): df_for_id = df[df[“id”] == id] #select data for specific id is_in = “B” in df_for_id[“value”].values #evaluate if column contains b return is_in
Compare value B by Series.eq first and then test if at least one True per groups by GroupBy.transform and Series.any: df['Contains_B'] = df['value'].eq('B').groupby(df['id']).transform('any') Or get all id with B and then for new column use Series.isin: df['Contains_B'] = df['id'].isin(df.loc[df['value'].eq('B'), 'id']) print (df) id value Contains_B 0 1 A True 1 1 A True 2 1 B True 3 2 C False 4 2 A False 5 3 B True 6 4 C False 7 5 A False
60,978,873
Losing a column when merging data frames pandas
<p>I have a multi-level issue. There are two tables:</p> <p>Table 1 Sample Info:</p> <pre><code>Sample Compound Label Abundance 1 ABC 0 10 1 ABC 1 50 2 ABC 0 100 2 ABC 0 5 3 ABC 0 100 4 ABC 0 5 1 DEF 0 10 1 DEF 1 50 1 DEF 2 100 2 DEF 0 5 3 DEF 0 100 3 DEF 1 5 </code></pre> <p>Table 2 Cohort Info:</p> <pre><code>Sample Cohort 1 control 2 control 3 disease 4 disease </code></pre> <p>I have three tasks: a) sum total abundances for each Sample in Table 1 to yield something like this</p> <pre><code>Sample Compound Sum_Abundance 1 ABC 60 2 ABC 105 3 ABC 100 4 ABC 5 </code></pre> <p>b) Merge these with Table 2 to have a column with cohort info: </p> <pre><code>Sample Compound Sum_Abundance Cohort Info 1 ABC 60 control 2 ABC 105 control 3 ABC 100 disease 4 ABC 5 disease </code></pre> <p>c) Average Sum_Abundance for each Compound within a Cohort</p> <pre><code>Compound Avg_Abundance Cohort Info ABC 82.5 control ABC 57.5 disease </code></pre> <p>I have tried these steps:</p> <pre><code>pivot_table=pd.pivot_table(table1, values=['Abundance'], index=['Sample', 'Name'], aggfunc = np.sum) print(table1.head(2)) sum_table = pd.DataFrame(pivot_table) cohort_df = pd.DataFrame(table2) print(cohort_df.head()) merged_df = pd.merge(sum_table, cohort_df, on = "Sample") </code></pre> <p>This is where it merges both frames but removes the compound column and no matter what I try, I cannot move passed that. If I put 'Name' into column, it creates a nicely looking output but I have no idea how to average the fields.</p>
60,979,129
"2020-04-01T19:29:38.573000"
1
null
1
38
python|pandas
<p>Here's what I would do:</p> <pre><code>step1 = (df1.groupby(['Sample','Compound']) ['Abundance'].sum() .reset_index(name='Sum_Abundance') ) step2 = step1.merge(df2, on='Sample') step3 = (step2.groupby(['Compound','Cohort']) ['Sum_Abundance'].mean() .reset_index(name='Avg_Abundance') ) </code></pre> <p>Output:</p> <pre><code> Compound Cohort Avg_Abundance 0 ABC control 82.5 1 ABC disease 52.5 2 DEF control 82.5 3 DEF disease 105.0 </code></pre> <p>If the intermediate dataframes (<code>step1</code>, <code>step2</code>) are not needed, you can chain all of them:</p> <pre><code>final_df = (df1.groupby(['Sample','Compound']) ['Abundance'].sum() .reset_index(name='Sum_Abundance') .merge(df2, on='Sample') .step2.groupby(['Compound','Cohort']) ['Sum_Abundance'].mean() .reset_index(name='Avg_Abundance') ) </code></pre>
"2020-04-01T19:46:11.250000"
1
https://pandas.pydata.org/docs/dev/user_guide/merging.html
Merge, join, concatenate and compare# Merge, join, concatenate and compare# pandas provides various facilities for easily combining together Series or DataFrame with various kinds of set logic for the indexes and relational algebra functionality in the case of join / merge-type Here's what I would do: step1 = (df1.groupby(['Sample','Compound']) ['Abundance'].sum() .reset_index(name='Sum_Abundance') ) step2 = step1.merge(df2, on='Sample') step3 = (step2.groupby(['Compound','Cohort']) ['Sum_Abundance'].mean() .reset_index(name='Avg_Abundance') ) Output: Compound Cohort Avg_Abundance 0 ABC control 82.5 1 ABC disease 52.5 2 DEF control 82.5 3 DEF disease 105.0 If the intermediate dataframes (step1, step2) are not needed, you can chain all of them: final_df = (df1.groupby(['Sample','Compound']) ['Abundance'].sum() .reset_index(name='Sum_Abundance') .merge(df2, on='Sample') .step2.groupby(['Compound','Cohort']) ['Sum_Abundance'].mean() .reset_index(name='Avg_Abundance') ) operations. In addition, pandas also provides utilities to compare two Series or DataFrame and summarize their differences. Concatenating objects# The concat() function (in the main pandas namespace) does all of the heavy lifting of performing concatenation operations along an axis while performing optional set logic (union or intersection) of the indexes (if any) on the other axes. Note that I say “if any” because there is only a single possible axis of concatenation for Series. Before diving into all of the details of concat and what it can do, here is a simple example: In [1]: df1 = pd.DataFrame( ...: { ...: "A": ["A0", "A1", "A2", "A3"], ...: "B": ["B0", "B1", "B2", "B3"], ...: "C": ["C0", "C1", "C2", "C3"], ...: "D": ["D0", "D1", "D2", "D3"], ...: }, ...: index=[0, 1, 2, 3], ...: ) ...: In [2]: df2 = pd.DataFrame( ...: { ...: "A": ["A4", "A5", "A6", "A7"], ...: "B": ["B4", "B5", "B6", "B7"], ...: "C": ["C4", "C5", "C6", "C7"], ...: "D": ["D4", "D5", "D6", "D7"], ...: }, ...: index=[4, 5, 6, 7], ...: ) ...: In [3]: df3 = pd.DataFrame( ...: { ...: "A": ["A8", "A9", "A10", "A11"], ...: "B": ["B8", "B9", "B10", "B11"], ...: "C": ["C8", "C9", "C10", "C11"], ...: "D": ["D8", "D9", "D10", "D11"], ...: }, ...: index=[8, 9, 10, 11], ...: ) ...: In [4]: frames = [df1, df2, df3] In [5]: result = pd.concat(frames) Like its sibling function on ndarrays, numpy.concatenate, pandas.concat takes a list or dict of homogeneously-typed objects and concatenates them with some configurable handling of “what to do with the other axes”: pd.concat( objs, axis=0, join="outer", ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, copy=True, ) objs : a sequence or mapping of Series or DataFrame objects. If a dict is passed, the sorted keys will be used as the keys argument, unless it is passed, in which case the values will be selected (see below). Any None objects will be dropped silently unless they are all None in which case a ValueError will be raised. axis : {0, 1, …}, default 0. The axis to concatenate along. join : {‘inner’, ‘outer’}, default ‘outer’. How to handle indexes on other axis(es). Outer for union and inner for intersection. ignore_index : boolean, default False. If True, do not use the index values on the concatenation axis. The resulting axis will be labeled 0, …, n - 1. This is useful if you are concatenating objects where the concatenation axis does not have meaningful indexing information. Note the index values on the other axes are still respected in the join. keys : sequence, default None. Construct hierarchical index using the passed keys as the outermost level. If multiple levels passed, should contain tuples. levels : list of sequences, default None. Specific levels (unique values) to use for constructing a MultiIndex. Otherwise they will be inferred from the keys. names : list, default None. Names for the levels in the resulting hierarchical index. verify_integrity : boolean, default False. Check whether the new concatenated axis contains duplicates. This can be very expensive relative to the actual data concatenation. copy : boolean, default True. If False, do not copy data unnecessarily. Without a little bit of context many of these arguments don’t make much sense. Let’s revisit the above example. Suppose we wanted to associate specific keys with each of the pieces of the chopped up DataFrame. We can do this using the keys argument: In [6]: result = pd.concat(frames, keys=["x", "y", "z"]) As you can see (if you’ve read the rest of the documentation), the resulting object’s index has a hierarchical index. This means that we can now select out each chunk by key: In [7]: result.loc["y"] Out[7]: A B C D 4 A4 B4 C4 D4 5 A5 B5 C5 D5 6 A6 B6 C6 D6 7 A7 B7 C7 D7 It’s not a stretch to see how this can be very useful. More detail on this functionality below. Note It is worth noting that concat() makes a full copy of the data, and that constantly reusing this function can create a significant performance hit. If you need to use the operation over several datasets, use a list comprehension. frames = [ process_your_file(f) for f in files ] result = pd.concat(frames) Note When concatenating DataFrames with named axes, pandas will attempt to preserve these index/column names whenever possible. In the case where all inputs share a common name, this name will be assigned to the result. When the input names do not all agree, the result will be unnamed. The same is true for MultiIndex, but the logic is applied separately on a level-by-level basis. Set logic on the other axes# When gluing together multiple DataFrames, you have a choice of how to handle the other axes (other than the one being concatenated). This can be done in the following two ways: Take the union of them all, join='outer'. This is the default option as it results in zero information loss. Take the intersection, join='inner'. Here is an example of each of these methods. First, the default join='outer' behavior: In [8]: df4 = pd.DataFrame( ...: { ...: "B": ["B2", "B3", "B6", "B7"], ...: "D": ["D2", "D3", "D6", "D7"], ...: "F": ["F2", "F3", "F6", "F7"], ...: }, ...: index=[2, 3, 6, 7], ...: ) ...: In [9]: result = pd.concat([df1, df4], axis=1) Here is the same thing with join='inner': In [10]: result = pd.concat([df1, df4], axis=1, join="inner") Lastly, suppose we just wanted to reuse the exact index from the original DataFrame: In [11]: result = pd.concat([df1, df4], axis=1).reindex(df1.index) Similarly, we could index before the concatenation: In [12]: pd.concat([df1, df4.reindex(df1.index)], axis=1) Out[12]: A B C D B D F 0 A0 B0 C0 D0 NaN NaN NaN 1 A1 B1 C1 D1 NaN NaN NaN 2 A2 B2 C2 D2 B2 D2 F2 3 A3 B3 C3 D3 B3 D3 F3 Ignoring indexes on the concatenation axis# For DataFrame objects which don’t have a meaningful index, you may wish to append them and ignore the fact that they may have overlapping indexes. To do this, use the ignore_index argument: In [13]: result = pd.concat([df1, df4], ignore_index=True, sort=False) Concatenating with mixed ndims# You can concatenate a mix of Series and DataFrame objects. The Series will be transformed to DataFrame with the column name as the name of the Series. In [14]: s1 = pd.Series(["X0", "X1", "X2", "X3"], name="X") In [15]: result = pd.concat([df1, s1], axis=1) Note Since we’re concatenating a Series to a DataFrame, we could have achieved the same result with DataFrame.assign(). To concatenate an arbitrary number of pandas objects (DataFrame or Series), use concat. If unnamed Series are passed they will be numbered consecutively. In [16]: s2 = pd.Series(["_0", "_1", "_2", "_3"]) In [17]: result = pd.concat([df1, s2, s2, s2], axis=1) Passing ignore_index=True will drop all name references. In [18]: result = pd.concat([df1, s1], axis=1, ignore_index=True) More concatenating with group keys# A fairly common use of the keys argument is to override the column names when creating a new DataFrame based on existing Series. Notice how the default behaviour consists on letting the resulting DataFrame inherit the parent Series’ name, when these existed. In [19]: s3 = pd.Series([0, 1, 2, 3], name="foo") In [20]: s4 = pd.Series([0, 1, 2, 3]) In [21]: s5 = pd.Series([0, 1, 4, 5]) In [22]: pd.concat([s3, s4, s5], axis=1) Out[22]: foo 0 1 0 0 0 0 1 1 1 1 2 2 2 4 3 3 3 5 Through the keys argument we can override the existing column names. In [23]: pd.concat([s3, s4, s5], axis=1, keys=["red", "blue", "yellow"]) Out[23]: red blue yellow 0 0 0 0 1 1 1 1 2 2 2 4 3 3 3 5 Let’s consider a variation of the very first example presented: In [24]: result = pd.concat(frames, keys=["x", "y", "z"]) You can also pass a dict to concat in which case the dict keys will be used for the keys argument (unless other keys are specified): In [25]: pieces = {"x": df1, "y": df2, "z": df3} In [26]: result = pd.concat(pieces) In [27]: result = pd.concat(pieces, keys=["z", "y"]) The MultiIndex created has levels that are constructed from the passed keys and the index of the DataFrame pieces: In [28]: result.index.levels Out[28]: FrozenList([['z', 'y'], [4, 5, 6, 7, 8, 9, 10, 11]]) If you wish to specify other levels (as will occasionally be the case), you can do so using the levels argument: In [29]: result = pd.concat( ....: pieces, keys=["x", "y", "z"], levels=[["z", "y", "x", "w"]], names=["group_key"] ....: ) ....: In [30]: result.index.levels Out[30]: FrozenList([['z', 'y', 'x', 'w'], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]) This is fairly esoteric, but it is actually necessary for implementing things like GroupBy where the order of a categorical variable is meaningful. Appending rows to a DataFrame# If you have a series that you want to append as a single row to a DataFrame, you can convert the row into a DataFrame and use concat In [31]: s2 = pd.Series(["X0", "X1", "X2", "X3"], index=["A", "B", "C", "D"]) In [32]: result = pd.concat([df1, s2.to_frame().T], ignore_index=True) You should use ignore_index with this method to instruct DataFrame to discard its index. If you wish to preserve the index, you should construct an appropriately-indexed DataFrame and append or concatenate those objects. Database-style DataFrame or named Series joining/merging# pandas has full-featured, high performance in-memory join operations idiomatically very similar to relational databases like SQL. These methods perform significantly better (in some cases well over an order of magnitude better) than other open source implementations (like base::merge.data.frame in R). The reason for this is careful algorithmic design and the internal layout of the data in DataFrame. See the cookbook for some advanced strategies. Users who are familiar with SQL but new to pandas might be interested in a comparison with SQL. pandas provides a single function, merge(), as the entry point for all standard database join operations between DataFrame or named Series objects: pd.merge( left, right, how="inner", on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=True, suffixes=("_x", "_y"), copy=True, indicator=False, validate=None, ) left: A DataFrame or named Series object. right: Another DataFrame or named Series object. on: Column or index level names to join on. Must be found in both the left and right DataFrame and/or Series objects. If not passed and left_index and right_index are False, the intersection of the columns in the DataFrames and/or Series will be inferred to be the join keys. left_on: Columns or index levels from the left DataFrame or Series to use as keys. Can either be column names, index level names, or arrays with length equal to the length of the DataFrame or Series. right_on: Columns or index levels from the right DataFrame or Series to use as keys. Can either be column names, index level names, or arrays with length equal to the length of the DataFrame or Series. left_index: If True, use the index (row labels) from the left DataFrame or Series as its join key(s). In the case of a DataFrame or Series with a MultiIndex (hierarchical), the number of levels must match the number of join keys from the right DataFrame or Series. right_index: Same usage as left_index for the right DataFrame or Series how: One of 'left', 'right', 'outer', 'inner', 'cross'. Defaults to inner. See below for more detailed description of each method. sort: Sort the result DataFrame by the join keys in lexicographical order. Defaults to True, setting to False will improve performance substantially in many cases. suffixes: A tuple of string suffixes to apply to overlapping columns. Defaults to ('_x', '_y'). copy: Always copy data (default True) from the passed DataFrame or named Series objects, even when reindexing is not necessary. Cannot be avoided in many cases but may improve performance / memory usage. The cases where copying can be avoided are somewhat pathological but this option is provided nonetheless. indicator: Add a column to the output DataFrame called _merge with information on the source of each row. _merge is Categorical-type and takes on a value of left_only for observations whose merge key only appears in 'left' DataFrame or Series, right_only for observations whose merge key only appears in 'right' DataFrame or Series, and both if the observation’s merge key is found in both. validate : string, default None. If specified, checks if merge is of specified type. “one_to_one” or “1:1”: checks if merge keys are unique in both left and right datasets. “one_to_many” or “1:m”: checks if merge keys are unique in left dataset. “many_to_one” or “m:1”: checks if merge keys are unique in right dataset. “many_to_many” or “m:m”: allowed, but does not result in checks. Note Support for specifying index levels as the on, left_on, and right_on parameters was added in version 0.23.0. Support for merging named Series objects was added in version 0.24.0. The return type will be the same as left. If left is a DataFrame or named Series and right is a subclass of DataFrame, the return type will still be DataFrame. merge is a function in the pandas namespace, and it is also available as a DataFrame instance method merge(), with the calling DataFrame being implicitly considered the left object in the join. The related join() method, uses merge internally for the index-on-index (by default) and column(s)-on-index join. If you are joining on index only, you may wish to use DataFrame.join to save yourself some typing. Brief primer on merge methods (relational algebra)# Experienced users of relational databases like SQL will be familiar with the terminology used to describe join operations between two SQL-table like structures (DataFrame objects). There are several cases to consider which are very important to understand: one-to-one joins: for example when joining two DataFrame objects on their indexes (which must contain unique values). many-to-one joins: for example when joining an index (unique) to one or more columns in a different DataFrame. many-to-many joins: joining columns on columns. Note When joining columns on columns (potentially a many-to-many join), any indexes on the passed DataFrame objects will be discarded. It is worth spending some time understanding the result of the many-to-many join case. In SQL / standard relational algebra, if a key combination appears more than once in both tables, the resulting table will have the Cartesian product of the associated data. Here is a very basic example with one unique key combination: In [33]: left = pd.DataFrame( ....: { ....: "key": ["K0", "K1", "K2", "K3"], ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: } ....: ) ....: In [34]: right = pd.DataFrame( ....: { ....: "key": ["K0", "K1", "K2", "K3"], ....: "C": ["C0", "C1", "C2", "C3"], ....: "D": ["D0", "D1", "D2", "D3"], ....: } ....: ) ....: In [35]: result = pd.merge(left, right, on="key") Here is a more complicated example with multiple join keys. Only the keys appearing in left and right are present (the intersection), since how='inner' by default. In [36]: left = pd.DataFrame( ....: { ....: "key1": ["K0", "K0", "K1", "K2"], ....: "key2": ["K0", "K1", "K0", "K1"], ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: } ....: ) ....: In [37]: right = pd.DataFrame( ....: { ....: "key1": ["K0", "K1", "K1", "K2"], ....: "key2": ["K0", "K0", "K0", "K0"], ....: "C": ["C0", "C1", "C2", "C3"], ....: "D": ["D0", "D1", "D2", "D3"], ....: } ....: ) ....: In [38]: result = pd.merge(left, right, on=["key1", "key2"]) The how argument to merge specifies how to determine which keys are to be included in the resulting table. If a key combination does not appear in either the left or right tables, the values in the joined table will be NA. Here is a summary of the how options and their SQL equivalent names: Merge method SQL Join Name Description left LEFT OUTER JOIN Use keys from left frame only right RIGHT OUTER JOIN Use keys from right frame only outer FULL OUTER JOIN Use union of keys from both frames inner INNER JOIN Use intersection of keys from both frames cross CROSS JOIN Create the cartesian product of rows of both frames In [39]: result = pd.merge(left, right, how="left", on=["key1", "key2"]) In [40]: result = pd.merge(left, right, how="right", on=["key1", "key2"]) In [41]: result = pd.merge(left, right, how="outer", on=["key1", "key2"]) In [42]: result = pd.merge(left, right, how="inner", on=["key1", "key2"]) In [43]: result = pd.merge(left, right, how="cross") You can merge a mult-indexed Series and a DataFrame, if the names of the MultiIndex correspond to the columns from the DataFrame. Transform the Series to a DataFrame using Series.reset_index() before merging, as shown in the following example. In [44]: df = pd.DataFrame({"Let": ["A", "B", "C"], "Num": [1, 2, 3]}) In [45]: df Out[45]: Let Num 0 A 1 1 B 2 2 C 3 In [46]: ser = pd.Series( ....: ["a", "b", "c", "d", "e", "f"], ....: index=pd.MultiIndex.from_arrays( ....: [["A", "B", "C"] * 2, [1, 2, 3, 4, 5, 6]], names=["Let", "Num"] ....: ), ....: ) ....: In [47]: ser Out[47]: Let Num A 1 a B 2 b C 3 c A 4 d B 5 e C 6 f dtype: object In [48]: pd.merge(df, ser.reset_index(), on=["Let", "Num"]) Out[48]: Let Num 0 0 A 1 a 1 B 2 b 2 C 3 c Here is another example with duplicate join keys in DataFrames: In [49]: left = pd.DataFrame({"A": [1, 2], "B": [2, 2]}) In [50]: right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]}) In [51]: result = pd.merge(left, right, on="B", how="outer") Warning Joining / merging on duplicate keys can cause a returned frame that is the multiplication of the row dimensions, which may result in memory overflow. It is the user’ s responsibility to manage duplicate values in keys before joining large DataFrames. Checking for duplicate keys# Users can use the validate argument to automatically check whether there are unexpected duplicates in their merge keys. Key uniqueness is checked before merge operations and so should protect against memory overflows. Checking key uniqueness is also a good way to ensure user data structures are as expected. In the following example, there are duplicate values of B in the right DataFrame. As this is not a one-to-one merge – as specified in the validate argument – an exception will be raised. In [52]: left = pd.DataFrame({"A": [1, 2], "B": [1, 2]}) In [53]: right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]}) In [53]: result = pd.merge(left, right, on="B", how="outer", validate="one_to_one") ... MergeError: Merge keys are not unique in right dataset; not a one-to-one merge If the user is aware of the duplicates in the right DataFrame but wants to ensure there are no duplicates in the left DataFrame, one can use the validate='one_to_many' argument instead, which will not raise an exception. In [54]: pd.merge(left, right, on="B", how="outer", validate="one_to_many") Out[54]: A_x B A_y 0 1 1 NaN 1 2 2 4.0 2 2 2 5.0 3 2 2 6.0 The merge indicator# merge() accepts the argument indicator. If True, a Categorical-type column called _merge will be added to the output object that takes on values: Observation Origin _merge value Merge key only in 'left' frame left_only Merge key only in 'right' frame right_only Merge key in both frames both In [55]: df1 = pd.DataFrame({"col1": [0, 1], "col_left": ["a", "b"]}) In [56]: df2 = pd.DataFrame({"col1": [1, 2, 2], "col_right": [2, 2, 2]}) In [57]: pd.merge(df1, df2, on="col1", how="outer", indicator=True) Out[57]: col1 col_left col_right _merge 0 0 a NaN left_only 1 1 b 2.0 both 2 2 NaN 2.0 right_only 3 2 NaN 2.0 right_only The indicator argument will also accept string arguments, in which case the indicator function will use the value of the passed string as the name for the indicator column. In [58]: pd.merge(df1, df2, on="col1", how="outer", indicator="indicator_column") Out[58]: col1 col_left col_right indicator_column 0 0 a NaN left_only 1 1 b 2.0 both 2 2 NaN 2.0 right_only 3 2 NaN 2.0 right_only Merge dtypes# Merging will preserve the dtype of the join keys. In [59]: left = pd.DataFrame({"key": [1], "v1": [10]}) In [60]: left Out[60]: key v1 0 1 10 In [61]: right = pd.DataFrame({"key": [1, 2], "v1": [20, 30]}) In [62]: right Out[62]: key v1 0 1 20 1 2 30 We are able to preserve the join keys: In [63]: pd.merge(left, right, how="outer") Out[63]: key v1 0 1 10 1 1 20 2 2 30 In [64]: pd.merge(left, right, how="outer").dtypes Out[64]: key int64 v1 int64 dtype: object Of course if you have missing values that are introduced, then the resulting dtype will be upcast. In [65]: pd.merge(left, right, how="outer", on="key") Out[65]: key v1_x v1_y 0 1 10.0 20 1 2 NaN 30 In [66]: pd.merge(left, right, how="outer", on="key").dtypes Out[66]: key int64 v1_x float64 v1_y int64 dtype: object Merging will preserve category dtypes of the mergands. See also the section on categoricals. The left frame. In [67]: from pandas.api.types import CategoricalDtype In [68]: X = pd.Series(np.random.choice(["foo", "bar"], size=(10,))) In [69]: X = X.astype(CategoricalDtype(categories=["foo", "bar"])) In [70]: left = pd.DataFrame( ....: {"X": X, "Y": np.random.choice(["one", "two", "three"], size=(10,))} ....: ) ....: In [71]: left Out[71]: X Y 0 bar one 1 foo one 2 foo three 3 bar three 4 foo one 5 bar one 6 bar three 7 bar three 8 bar three 9 foo three In [72]: left.dtypes Out[72]: X category Y object dtype: object The right frame. In [73]: right = pd.DataFrame( ....: { ....: "X": pd.Series(["foo", "bar"], dtype=CategoricalDtype(["foo", "bar"])), ....: "Z": [1, 2], ....: } ....: ) ....: In [74]: right Out[74]: X Z 0 foo 1 1 bar 2 In [75]: right.dtypes Out[75]: X category Z int64 dtype: object The merged result: In [76]: result = pd.merge(left, right, how="outer") In [77]: result Out[77]: X Y Z 0 bar one 2 1 bar three 2 2 bar one 2 3 bar three 2 4 bar three 2 5 bar three 2 6 foo one 1 7 foo three 1 8 foo one 1 9 foo three 1 In [78]: result.dtypes Out[78]: X category Y object Z int64 dtype: object Note The category dtypes must be exactly the same, meaning the same categories and the ordered attribute. Otherwise the result will coerce to the categories’ dtype. Note Merging on category dtypes that are the same can be quite performant compared to object dtype merging. Joining on index# DataFrame.join() is a convenient method for combining the columns of two potentially differently-indexed DataFrames into a single result DataFrame. Here is a very basic example: In [79]: left = pd.DataFrame( ....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=["K0", "K1", "K2"] ....: ) ....: In [80]: right = pd.DataFrame( ....: {"C": ["C0", "C2", "C3"], "D": ["D0", "D2", "D3"]}, index=["K0", "K2", "K3"] ....: ) ....: In [81]: result = left.join(right) In [82]: result = left.join(right, how="outer") The same as above, but with how='inner'. In [83]: result = left.join(right, how="inner") The data alignment here is on the indexes (row labels). This same behavior can be achieved using merge plus additional arguments instructing it to use the indexes: In [84]: result = pd.merge(left, right, left_index=True, right_index=True, how="outer") In [85]: result = pd.merge(left, right, left_index=True, right_index=True, how="inner") Joining key columns on an index# join() takes an optional on argument which may be a column or multiple column names, which specifies that the passed DataFrame is to be aligned on that column in the DataFrame. These two function calls are completely equivalent: left.join(right, on=key_or_keys) pd.merge( left, right, left_on=key_or_keys, right_index=True, how="left", sort=False ) Obviously you can choose whichever form you find more convenient. For many-to-one joins (where one of the DataFrame’s is already indexed by the join key), using join may be more convenient. Here is a simple example: In [86]: left = pd.DataFrame( ....: { ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: "key": ["K0", "K1", "K0", "K1"], ....: } ....: ) ....: In [87]: right = pd.DataFrame({"C": ["C0", "C1"], "D": ["D0", "D1"]}, index=["K0", "K1"]) In [88]: result = left.join(right, on="key") In [89]: result = pd.merge( ....: left, right, left_on="key", right_index=True, how="left", sort=False ....: ) ....: To join on multiple keys, the passed DataFrame must have a MultiIndex: In [90]: left = pd.DataFrame( ....: { ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: "key1": ["K0", "K0", "K1", "K2"], ....: "key2": ["K0", "K1", "K0", "K1"], ....: } ....: ) ....: In [91]: index = pd.MultiIndex.from_tuples( ....: [("K0", "K0"), ("K1", "K0"), ("K2", "K0"), ("K2", "K1")] ....: ) ....: In [92]: right = pd.DataFrame( ....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=index ....: ) ....: Now this can be joined by passing the two key column names: In [93]: result = left.join(right, on=["key1", "key2"]) The default for DataFrame.join is to perform a left join (essentially a “VLOOKUP” operation, for Excel users), which uses only the keys found in the calling DataFrame. Other join types, for example inner join, can be just as easily performed: In [94]: result = left.join(right, on=["key1", "key2"], how="inner") As you can see, this drops any rows where there was no match. Joining a single Index to a MultiIndex# You can join a singly-indexed DataFrame with a level of a MultiIndexed DataFrame. The level will match on the name of the index of the singly-indexed frame against a level name of the MultiIndexed frame. In [95]: left = pd.DataFrame( ....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, ....: index=pd.Index(["K0", "K1", "K2"], name="key"), ....: ) ....: In [96]: index = pd.MultiIndex.from_tuples( ....: [("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")], ....: names=["key", "Y"], ....: ) ....: In [97]: right = pd.DataFrame( ....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, ....: index=index, ....: ) ....: In [98]: result = left.join(right, how="inner") This is equivalent but less verbose and more memory efficient / faster than this. In [99]: result = pd.merge( ....: left.reset_index(), right.reset_index(), on=["key"], how="inner" ....: ).set_index(["key","Y"]) ....: Joining with two MultiIndexes# This is supported in a limited way, provided that the index for the right argument is completely used in the join, and is a subset of the indices in the left argument, as in this example: In [100]: leftindex = pd.MultiIndex.from_product( .....: [list("abc"), list("xy"), [1, 2]], names=["abc", "xy", "num"] .....: ) .....: In [101]: left = pd.DataFrame({"v1": range(12)}, index=leftindex) In [102]: left Out[102]: v1 abc xy num a x 1 0 2 1 y 1 2 2 3 b x 1 4 2 5 y 1 6 2 7 c x 1 8 2 9 y 1 10 2 11 In [103]: rightindex = pd.MultiIndex.from_product( .....: [list("abc"), list("xy")], names=["abc", "xy"] .....: ) .....: In [104]: right = pd.DataFrame({"v2": [100 * i for i in range(1, 7)]}, index=rightindex) In [105]: right Out[105]: v2 abc xy a x 100 y 200 b x 300 y 400 c x 500 y 600 In [106]: left.join(right, on=["abc", "xy"], how="inner") Out[106]: v1 v2 abc xy num a x 1 0 100 2 1 100 y 1 2 200 2 3 200 b x 1 4 300 2 5 300 y 1 6 400 2 7 400 c x 1 8 500 2 9 500 y 1 10 600 2 11 600 If that condition is not satisfied, a join with two multi-indexes can be done using the following code. In [107]: leftindex = pd.MultiIndex.from_tuples( .....: [("K0", "X0"), ("K0", "X1"), ("K1", "X2")], names=["key", "X"] .....: ) .....: In [108]: left = pd.DataFrame( .....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=leftindex .....: ) .....: In [109]: rightindex = pd.MultiIndex.from_tuples( .....: [("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")], names=["key", "Y"] .....: ) .....: In [110]: right = pd.DataFrame( .....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=rightindex .....: ) .....: In [111]: result = pd.merge( .....: left.reset_index(), right.reset_index(), on=["key"], how="inner" .....: ).set_index(["key", "X", "Y"]) .....: Merging on a combination of columns and index levels# Strings passed as the on, left_on, and right_on parameters may refer to either column names or index level names. This enables merging DataFrame instances on a combination of index levels and columns without resetting indexes. In [112]: left_index = pd.Index(["K0", "K0", "K1", "K2"], name="key1") In [113]: left = pd.DataFrame( .....: { .....: "A": ["A0", "A1", "A2", "A3"], .....: "B": ["B0", "B1", "B2", "B3"], .....: "key2": ["K0", "K1", "K0", "K1"], .....: }, .....: index=left_index, .....: ) .....: In [114]: right_index = pd.Index(["K0", "K1", "K2", "K2"], name="key1") In [115]: right = pd.DataFrame( .....: { .....: "C": ["C0", "C1", "C2", "C3"], .....: "D": ["D0", "D1", "D2", "D3"], .....: "key2": ["K0", "K0", "K0", "K1"], .....: }, .....: index=right_index, .....: ) .....: In [116]: result = left.merge(right, on=["key1", "key2"]) Note When DataFrames are merged on a string that matches an index level in both frames, the index level is preserved as an index level in the resulting DataFrame. Note When DataFrames are merged using only some of the levels of a MultiIndex, the extra levels will be dropped from the resulting merge. In order to preserve those levels, use reset_index on those level names to move those levels to columns prior to doing the merge. Note If a string matches both a column name and an index level name, then a warning is issued and the column takes precedence. This will result in an ambiguity error in a future version. Overlapping value columns# The merge suffixes argument takes a tuple of list of strings to append to overlapping column names in the input DataFrames to disambiguate the result columns: In [117]: left = pd.DataFrame({"k": ["K0", "K1", "K2"], "v": [1, 2, 3]}) In [118]: right = pd.DataFrame({"k": ["K0", "K0", "K3"], "v": [4, 5, 6]}) In [119]: result = pd.merge(left, right, on="k") In [120]: result = pd.merge(left, right, on="k", suffixes=("_l", "_r")) DataFrame.join() has lsuffix and rsuffix arguments which behave similarly. In [121]: left = left.set_index("k") In [122]: right = right.set_index("k") In [123]: result = left.join(right, lsuffix="_l", rsuffix="_r") Joining multiple DataFrames# A list or tuple of DataFrames can also be passed to join() to join them together on their indexes. In [124]: right2 = pd.DataFrame({"v": [7, 8, 9]}, index=["K1", "K1", "K2"]) In [125]: result = left.join([right, right2]) Merging together values within Series or DataFrame columns# Another fairly common situation is to have two like-indexed (or similarly indexed) Series or DataFrame objects and wanting to “patch” values in one object from values for matching indices in the other. Here is an example: In [126]: df1 = pd.DataFrame( .....: [[np.nan, 3.0, 5.0], [-4.6, np.nan, np.nan], [np.nan, 7.0, np.nan]] .....: ) .....: In [127]: df2 = pd.DataFrame([[-42.6, np.nan, -8.2], [-5.0, 1.6, 4]], index=[1, 2]) For this, use the combine_first() method: In [128]: result = df1.combine_first(df2) Note that this method only takes values from the right DataFrame if they are missing in the left DataFrame. A related method, update(), alters non-NA values in place: In [129]: df1.update(df2) Timeseries friendly merging# Merging ordered data# A merge_ordered() function allows combining time series and other ordered data. In particular it has an optional fill_method keyword to fill/interpolate missing data: In [130]: left = pd.DataFrame( .....: {"k": ["K0", "K1", "K1", "K2"], "lv": [1, 2, 3, 4], "s": ["a", "b", "c", "d"]} .....: ) .....: In [131]: right = pd.DataFrame({"k": ["K1", "K2", "K4"], "rv": [1, 2, 3]}) In [132]: pd.merge_ordered(left, right, fill_method="ffill", left_by="s") Out[132]: k lv s rv 0 K0 1.0 a NaN 1 K1 1.0 a 1.0 2 K2 1.0 a 2.0 3 K4 1.0 a 3.0 4 K1 2.0 b 1.0 5 K2 2.0 b 2.0 6 K4 2.0 b 3.0 7 K1 3.0 c 1.0 8 K2 3.0 c 2.0 9 K4 3.0 c 3.0 10 K1 NaN d 1.0 11 K2 4.0 d 2.0 12 K4 4.0 d 3.0 Merging asof# A merge_asof() is similar to an ordered left-join except that we match on nearest key rather than equal keys. For each row in the left DataFrame, we select the last row in the right DataFrame whose on key is less than the left’s key. Both DataFrames must be sorted by the key. Optionally an asof merge can perform a group-wise merge. This matches the by key equally, in addition to the nearest match on the on key. For example; we might have trades and quotes and we want to asof merge them. In [133]: trades = pd.DataFrame( .....: { .....: "time": pd.to_datetime( .....: [ .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.038", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.048", .....: ] .....: ), .....: "ticker": ["MSFT", "MSFT", "GOOG", "GOOG", "AAPL"], .....: "price": [51.95, 51.95, 720.77, 720.92, 98.00], .....: "quantity": [75, 155, 100, 100, 100], .....: }, .....: columns=["time", "ticker", "price", "quantity"], .....: ) .....: In [134]: quotes = pd.DataFrame( .....: { .....: "time": pd.to_datetime( .....: [ .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.030", .....: "20160525 13:30:00.041", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.049", .....: "20160525 13:30:00.072", .....: "20160525 13:30:00.075", .....: ] .....: ), .....: "ticker": ["GOOG", "MSFT", "MSFT", "MSFT", "GOOG", "AAPL", "GOOG", "MSFT"], .....: "bid": [720.50, 51.95, 51.97, 51.99, 720.50, 97.99, 720.50, 52.01], .....: "ask": [720.93, 51.96, 51.98, 52.00, 720.93, 98.01, 720.88, 52.03], .....: }, .....: columns=["time", "ticker", "bid", "ask"], .....: ) .....: In [135]: trades Out[135]: time ticker price quantity 0 2016-05-25 13:30:00.023 MSFT 51.95 75 1 2016-05-25 13:30:00.038 MSFT 51.95 155 2 2016-05-25 13:30:00.048 GOOG 720.77 100 3 2016-05-25 13:30:00.048 GOOG 720.92 100 4 2016-05-25 13:30:00.048 AAPL 98.00 100 In [136]: quotes Out[136]: time ticker bid ask 0 2016-05-25 13:30:00.023 GOOG 720.50 720.93 1 2016-05-25 13:30:00.023 MSFT 51.95 51.96 2 2016-05-25 13:30:00.030 MSFT 51.97 51.98 3 2016-05-25 13:30:00.041 MSFT 51.99 52.00 4 2016-05-25 13:30:00.048 GOOG 720.50 720.93 5 2016-05-25 13:30:00.049 AAPL 97.99 98.01 6 2016-05-25 13:30:00.072 GOOG 720.50 720.88 7 2016-05-25 13:30:00.075 MSFT 52.01 52.03 By default we are taking the asof of the quotes. In [137]: pd.merge_asof(trades, quotes, on="time", by="ticker") Out[137]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 51.95 51.96 1 2016-05-25 13:30:00.038 MSFT 51.95 155 51.97 51.98 2 2016-05-25 13:30:00.048 GOOG 720.77 100 720.50 720.93 3 2016-05-25 13:30:00.048 GOOG 720.92 100 720.50 720.93 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN We only asof within 2ms between the quote time and the trade time. In [138]: pd.merge_asof(trades, quotes, on="time", by="ticker", tolerance=pd.Timedelta("2ms")) Out[138]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 51.95 51.96 1 2016-05-25 13:30:00.038 MSFT 51.95 155 NaN NaN 2 2016-05-25 13:30:00.048 GOOG 720.77 100 720.50 720.93 3 2016-05-25 13:30:00.048 GOOG 720.92 100 720.50 720.93 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN We only asof within 10ms between the quote time and the trade time and we exclude exact matches on time. Note that though we exclude the exact matches (of the quotes), prior quotes do propagate to that point in time. In [139]: pd.merge_asof( .....: trades, .....: quotes, .....: on="time", .....: by="ticker", .....: tolerance=pd.Timedelta("10ms"), .....: allow_exact_matches=False, .....: ) .....: Out[139]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 NaN NaN 1 2016-05-25 13:30:00.038 MSFT 51.95 155 51.97 51.98 2 2016-05-25 13:30:00.048 GOOG 720.77 100 NaN NaN 3 2016-05-25 13:30:00.048 GOOG 720.92 100 NaN NaN 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN Comparing objects# The compare() and compare() methods allow you to compare two DataFrame or Series, respectively, and summarize their differences. This feature was added in V1.1.0. For example, you might want to compare two DataFrame and stack their differences side by side. In [140]: df = pd.DataFrame( .....: { .....: "col1": ["a", "a", "b", "b", "a"], .....: "col2": [1.0, 2.0, 3.0, np.nan, 5.0], .....: "col3": [1.0, 2.0, 3.0, 4.0, 5.0], .....: }, .....: columns=["col1", "col2", "col3"], .....: ) .....: In [141]: df Out[141]: col1 col2 col3 0 a 1.0 1.0 1 a 2.0 2.0 2 b 3.0 3.0 3 b NaN 4.0 4 a 5.0 5.0 In [142]: df2 = df.copy() In [143]: df2.loc[0, "col1"] = "c" In [144]: df2.loc[2, "col3"] = 4.0 In [145]: df2 Out[145]: col1 col2 col3 0 c 1.0 1.0 1 a 2.0 2.0 2 b 3.0 4.0 3 b NaN 4.0 4 a 5.0 5.0 In [146]: df.compare(df2) Out[146]: col1 col3 self other self other 0 a c NaN NaN 2 NaN NaN 3.0 4.0 By default, if two corresponding values are equal, they will be shown as NaN. Furthermore, if all values in an entire row / column, the row / column will be omitted from the result. The remaining differences will be aligned on columns. If you wish, you may choose to stack the differences on rows. In [147]: df.compare(df2, align_axis=0) Out[147]: col1 col3 0 self a NaN other c NaN 2 self NaN 3.0 other NaN 4.0 If you wish to keep all original rows and columns, set keep_shape argument to True. In [148]: df.compare(df2, keep_shape=True) Out[148]: col1 col2 col3 self other self other self other 0 a c NaN NaN NaN NaN 1 NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN 3.0 4.0 3 NaN NaN NaN NaN NaN NaN 4 NaN NaN NaN NaN NaN NaN You may also keep all the original values even if they are equal. In [149]: df.compare(df2, keep_shape=True, keep_equal=True) Out[149]: col1 col2 col3 self other self other self other 0 a c 1.0 1.0 1.0 1.0 1 a a 2.0 2.0 2.0 2.0 2 b b 3.0 3.0 3.0 4.0 3 b b NaN NaN 4.0 4.0 4 a a 5.0 5.0 5.0 5.0
281
1,204
Losing a column when merging data frames pandas I have a multi-level issue. There are two tables: Table 1 Sample Info: Sample Compound Label Abundance 1 ABC 0 10 1 ABC 1 50 2 ABC 0 100 2 ABC 0 5 3 ABC 0 100 4 ABC 0 5 1 DEF 0 10 1 DEF 1 50 1 DEF 2 100 2 DEF 0 5 3 DEF 0 100 3 DEF 1 5 Table 2 Cohort Info: Sample Cohort 1 control 2 control 3 disease 4 disease I have three tasks: a) sum total abundances for each Sample in Table 1 to yield something like this Sample Compound Sum_Abundance 1 ABC 60 2 ABC 105 3 ABC 100 4 ABC 5 b) Merge these with Table 2 to have a column with cohort info: Sample Compound Sum_Abundance Cohort Info 1 ABC 60 control 2 ABC 105 control 3 ABC 100 disease 4 ABC 5 disease c) Average Sum_Abundance for each Compound within a Cohort Compound Avg_Abundance Cohort Info ABC 82.5 control ABC 57.5 disease I have tried these steps: pivot_table=pd.pivot_table(table1, values=['Abundance'], index=['Sample', 'Name'], aggfunc = np.sum) print(table1.head(2)) sum_table = pd.DataFrame(pivot_table) cohort_df = pd.DataFrame(table2) print(cohort_df.head()) merged_df = pd.merge(sum_table, cohort_df, on = "Sample") This is where it merges both frames but removes the compound column and no matter what I try, I cannot move passed that. If I put 'Name' into column, it creates a nicely looking output but I have no idea how to average the fields.
Here's what I would do: step1 = (df1.groupby(['Sample','Compound']) ['Abundance'].sum() .reset_index(name='Sum_Abundance') ) step2 = step1.merge(df2, on='Sample') step3 = (step2.groupby(['Compound','Cohort']) ['Sum_Abundance'].mean() .reset_index(name='Avg_Abundance') ) Output: Compound Cohort Avg_Abundance 0 ABC control 82.5 1 ABC disease 52.5 2 DEF control 82.5 3 DEF disease 105.0 If the intermediate dataframes (step1, step2) are not needed, you can chain all of them: final_df = (df1.groupby(['Sample','Compound']) ['Abundance'].sum() .reset_index(name='Sum_Abundance') .merge(df2, on='Sample') .step2.groupby(['Compound','Cohort']) ['Sum_Abundance'].mean() .reset_index(name='Avg_Abundance') )
68,679,478
How can I "join" rows with the same ID in pandas and add data as new columns
<p>I have a dataframe which contains multiple rows for individual clients. There is a separate row for each product with all fields identical other than the item and amount column for a given reference.</p> <p><strong>Example</strong></p> <pre><code> name reference item amount 0 john 1234 chair 40 1 john 1234 table 10 2 john 1234 table 20 2 john 1234 pole 10 3 jane 9876 chair 15 4 jane 9876 pole 30 </code></pre> <p><strong>My Problem</strong></p> <p>Each customer has an unknown amount of entries in the database (around 10 entries would be the maximum). I want to edit this dataframe to have entries with the same reference on the same row. The row need to also have extra columns added for 'item 2' and 'amount 2'.</p> <p><strong>Example Output</strong></p> <pre><code> name reference item 1 amount1 item2 amount2 item3 amount3 item4 amount4 0 john 1234 chair 40 table 10 table 20 pole 10 1 jane 9876 chair 15 pole 30 NaN NaN NaN NaN </code></pre> <p>After reading the documentation and having a look for other similar questions, I can only find how to merge / aggregate the data which will not work in my scenario.</p> <p>Is this possible in Pandas?</p> <p>Thanks!</p>
68,679,589
"2021-08-06T09:42:13.137000"
1
1
-1
557
python|pandas
<p>Let's <code>unstack()</code> by tracking position using <code>groupby()</code>+<code>cumcount()</code>:</p> <pre><code>df['s']=df.groupby(['name','reference']).cumcount()+1 df=df.set_index(['s','name','reference']).unstack(0) df.columns=[f&quot;{x}{y}&quot; for x,y in df.columns] df=df.reset_index() </code></pre> <p>output of <code>df</code>:</p> <pre><code> name reference item1 item2 item3 item4 amount1 amount2 amount3 amount4 0 jane 9876 chair pole NaN NaN 15.0 30.0 NaN NaN 1 john 1234 chair table table pole 40.0 10.0 20.0 10.0 </code></pre>
"2021-08-06T09:50:13.713000"
1
https://pandas.pydata.org/docs/dev/user_guide/merging.html
Merge, join, concatenate and compare# Merge, join, concatenate and compare# pandas provides various facilities for easily combining together Series or DataFrame with various kinds of set logic for the indexes and relational algebra functionality in the case of join / merge-type operations. In addition, pandas also provides utilities to compare two Series or DataFrame and summarize their differences. Concatenating objects# The concat() function (in the main pandas namespace) does all of the heavy lifting of performing concatenation operations along an axis while performing optional set logic (union or intersection) of the indexes (if any) on Let's unstack() by tracking position using groupby()+cumcount(): df['s']=df.groupby(['name','reference']).cumcount()+1 df=df.set_index(['s','name','reference']).unstack(0) df.columns=[f"{x}{y}" for x,y in df.columns] df=df.reset_index() output of df: name reference item1 item2 item3 item4 amount1 amount2 amount3 amount4 0 jane 9876 chair pole NaN NaN 15.0 30.0 NaN NaN 1 john 1234 chair table table pole 40.0 10.0 20.0 10.0 the other axes. Note that I say “if any” because there is only a single possible axis of concatenation for Series. Before diving into all of the details of concat and what it can do, here is a simple example: In [1]: df1 = pd.DataFrame( ...: { ...: "A": ["A0", "A1", "A2", "A3"], ...: "B": ["B0", "B1", "B2", "B3"], ...: "C": ["C0", "C1", "C2", "C3"], ...: "D": ["D0", "D1", "D2", "D3"], ...: }, ...: index=[0, 1, 2, 3], ...: ) ...: In [2]: df2 = pd.DataFrame( ...: { ...: "A": ["A4", "A5", "A6", "A7"], ...: "B": ["B4", "B5", "B6", "B7"], ...: "C": ["C4", "C5", "C6", "C7"], ...: "D": ["D4", "D5", "D6", "D7"], ...: }, ...: index=[4, 5, 6, 7], ...: ) ...: In [3]: df3 = pd.DataFrame( ...: { ...: "A": ["A8", "A9", "A10", "A11"], ...: "B": ["B8", "B9", "B10", "B11"], ...: "C": ["C8", "C9", "C10", "C11"], ...: "D": ["D8", "D9", "D10", "D11"], ...: }, ...: index=[8, 9, 10, 11], ...: ) ...: In [4]: frames = [df1, df2, df3] In [5]: result = pd.concat(frames) Like its sibling function on ndarrays, numpy.concatenate, pandas.concat takes a list or dict of homogeneously-typed objects and concatenates them with some configurable handling of “what to do with the other axes”: pd.concat( objs, axis=0, join="outer", ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, copy=True, ) objs : a sequence or mapping of Series or DataFrame objects. If a dict is passed, the sorted keys will be used as the keys argument, unless it is passed, in which case the values will be selected (see below). Any None objects will be dropped silently unless they are all None in which case a ValueError will be raised. axis : {0, 1, …}, default 0. The axis to concatenate along. join : {‘inner’, ‘outer’}, default ‘outer’. How to handle indexes on other axis(es). Outer for union and inner for intersection. ignore_index : boolean, default False. If True, do not use the index values on the concatenation axis. The resulting axis will be labeled 0, …, n - 1. This is useful if you are concatenating objects where the concatenation axis does not have meaningful indexing information. Note the index values on the other axes are still respected in the join. keys : sequence, default None. Construct hierarchical index using the passed keys as the outermost level. If multiple levels passed, should contain tuples. levels : list of sequences, default None. Specific levels (unique values) to use for constructing a MultiIndex. Otherwise they will be inferred from the keys. names : list, default None. Names for the levels in the resulting hierarchical index. verify_integrity : boolean, default False. Check whether the new concatenated axis contains duplicates. This can be very expensive relative to the actual data concatenation. copy : boolean, default True. If False, do not copy data unnecessarily. Without a little bit of context many of these arguments don’t make much sense. Let’s revisit the above example. Suppose we wanted to associate specific keys with each of the pieces of the chopped up DataFrame. We can do this using the keys argument: In [6]: result = pd.concat(frames, keys=["x", "y", "z"]) As you can see (if you’ve read the rest of the documentation), the resulting object’s index has a hierarchical index. This means that we can now select out each chunk by key: In [7]: result.loc["y"] Out[7]: A B C D 4 A4 B4 C4 D4 5 A5 B5 C5 D5 6 A6 B6 C6 D6 7 A7 B7 C7 D7 It’s not a stretch to see how this can be very useful. More detail on this functionality below. Note It is worth noting that concat() makes a full copy of the data, and that constantly reusing this function can create a significant performance hit. If you need to use the operation over several datasets, use a list comprehension. frames = [ process_your_file(f) for f in files ] result = pd.concat(frames) Note When concatenating DataFrames with named axes, pandas will attempt to preserve these index/column names whenever possible. In the case where all inputs share a common name, this name will be assigned to the result. When the input names do not all agree, the result will be unnamed. The same is true for MultiIndex, but the logic is applied separately on a level-by-level basis. Set logic on the other axes# When gluing together multiple DataFrames, you have a choice of how to handle the other axes (other than the one being concatenated). This can be done in the following two ways: Take the union of them all, join='outer'. This is the default option as it results in zero information loss. Take the intersection, join='inner'. Here is an example of each of these methods. First, the default join='outer' behavior: In [8]: df4 = pd.DataFrame( ...: { ...: "B": ["B2", "B3", "B6", "B7"], ...: "D": ["D2", "D3", "D6", "D7"], ...: "F": ["F2", "F3", "F6", "F7"], ...: }, ...: index=[2, 3, 6, 7], ...: ) ...: In [9]: result = pd.concat([df1, df4], axis=1) Here is the same thing with join='inner': In [10]: result = pd.concat([df1, df4], axis=1, join="inner") Lastly, suppose we just wanted to reuse the exact index from the original DataFrame: In [11]: result = pd.concat([df1, df4], axis=1).reindex(df1.index) Similarly, we could index before the concatenation: In [12]: pd.concat([df1, df4.reindex(df1.index)], axis=1) Out[12]: A B C D B D F 0 A0 B0 C0 D0 NaN NaN NaN 1 A1 B1 C1 D1 NaN NaN NaN 2 A2 B2 C2 D2 B2 D2 F2 3 A3 B3 C3 D3 B3 D3 F3 Ignoring indexes on the concatenation axis# For DataFrame objects which don’t have a meaningful index, you may wish to append them and ignore the fact that they may have overlapping indexes. To do this, use the ignore_index argument: In [13]: result = pd.concat([df1, df4], ignore_index=True, sort=False) Concatenating with mixed ndims# You can concatenate a mix of Series and DataFrame objects. The Series will be transformed to DataFrame with the column name as the name of the Series. In [14]: s1 = pd.Series(["X0", "X1", "X2", "X3"], name="X") In [15]: result = pd.concat([df1, s1], axis=1) Note Since we’re concatenating a Series to a DataFrame, we could have achieved the same result with DataFrame.assign(). To concatenate an arbitrary number of pandas objects (DataFrame or Series), use concat. If unnamed Series are passed they will be numbered consecutively. In [16]: s2 = pd.Series(["_0", "_1", "_2", "_3"]) In [17]: result = pd.concat([df1, s2, s2, s2], axis=1) Passing ignore_index=True will drop all name references. In [18]: result = pd.concat([df1, s1], axis=1, ignore_index=True) More concatenating with group keys# A fairly common use of the keys argument is to override the column names when creating a new DataFrame based on existing Series. Notice how the default behaviour consists on letting the resulting DataFrame inherit the parent Series’ name, when these existed. In [19]: s3 = pd.Series([0, 1, 2, 3], name="foo") In [20]: s4 = pd.Series([0, 1, 2, 3]) In [21]: s5 = pd.Series([0, 1, 4, 5]) In [22]: pd.concat([s3, s4, s5], axis=1) Out[22]: foo 0 1 0 0 0 0 1 1 1 1 2 2 2 4 3 3 3 5 Through the keys argument we can override the existing column names. In [23]: pd.concat([s3, s4, s5], axis=1, keys=["red", "blue", "yellow"]) Out[23]: red blue yellow 0 0 0 0 1 1 1 1 2 2 2 4 3 3 3 5 Let’s consider a variation of the very first example presented: In [24]: result = pd.concat(frames, keys=["x", "y", "z"]) You can also pass a dict to concat in which case the dict keys will be used for the keys argument (unless other keys are specified): In [25]: pieces = {"x": df1, "y": df2, "z": df3} In [26]: result = pd.concat(pieces) In [27]: result = pd.concat(pieces, keys=["z", "y"]) The MultiIndex created has levels that are constructed from the passed keys and the index of the DataFrame pieces: In [28]: result.index.levels Out[28]: FrozenList([['z', 'y'], [4, 5, 6, 7, 8, 9, 10, 11]]) If you wish to specify other levels (as will occasionally be the case), you can do so using the levels argument: In [29]: result = pd.concat( ....: pieces, keys=["x", "y", "z"], levels=[["z", "y", "x", "w"]], names=["group_key"] ....: ) ....: In [30]: result.index.levels Out[30]: FrozenList([['z', 'y', 'x', 'w'], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]) This is fairly esoteric, but it is actually necessary for implementing things like GroupBy where the order of a categorical variable is meaningful. Appending rows to a DataFrame# If you have a series that you want to append as a single row to a DataFrame, you can convert the row into a DataFrame and use concat In [31]: s2 = pd.Series(["X0", "X1", "X2", "X3"], index=["A", "B", "C", "D"]) In [32]: result = pd.concat([df1, s2.to_frame().T], ignore_index=True) You should use ignore_index with this method to instruct DataFrame to discard its index. If you wish to preserve the index, you should construct an appropriately-indexed DataFrame and append or concatenate those objects. Database-style DataFrame or named Series joining/merging# pandas has full-featured, high performance in-memory join operations idiomatically very similar to relational databases like SQL. These methods perform significantly better (in some cases well over an order of magnitude better) than other open source implementations (like base::merge.data.frame in R). The reason for this is careful algorithmic design and the internal layout of the data in DataFrame. See the cookbook for some advanced strategies. Users who are familiar with SQL but new to pandas might be interested in a comparison with SQL. pandas provides a single function, merge(), as the entry point for all standard database join operations between DataFrame or named Series objects: pd.merge( left, right, how="inner", on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=True, suffixes=("_x", "_y"), copy=True, indicator=False, validate=None, ) left: A DataFrame or named Series object. right: Another DataFrame or named Series object. on: Column or index level names to join on. Must be found in both the left and right DataFrame and/or Series objects. If not passed and left_index and right_index are False, the intersection of the columns in the DataFrames and/or Series will be inferred to be the join keys. left_on: Columns or index levels from the left DataFrame or Series to use as keys. Can either be column names, index level names, or arrays with length equal to the length of the DataFrame or Series. right_on: Columns or index levels from the right DataFrame or Series to use as keys. Can either be column names, index level names, or arrays with length equal to the length of the DataFrame or Series. left_index: If True, use the index (row labels) from the left DataFrame or Series as its join key(s). In the case of a DataFrame or Series with a MultiIndex (hierarchical), the number of levels must match the number of join keys from the right DataFrame or Series. right_index: Same usage as left_index for the right DataFrame or Series how: One of 'left', 'right', 'outer', 'inner', 'cross'. Defaults to inner. See below for more detailed description of each method. sort: Sort the result DataFrame by the join keys in lexicographical order. Defaults to True, setting to False will improve performance substantially in many cases. suffixes: A tuple of string suffixes to apply to overlapping columns. Defaults to ('_x', '_y'). copy: Always copy data (default True) from the passed DataFrame or named Series objects, even when reindexing is not necessary. Cannot be avoided in many cases but may improve performance / memory usage. The cases where copying can be avoided are somewhat pathological but this option is provided nonetheless. indicator: Add a column to the output DataFrame called _merge with information on the source of each row. _merge is Categorical-type and takes on a value of left_only for observations whose merge key only appears in 'left' DataFrame or Series, right_only for observations whose merge key only appears in 'right' DataFrame or Series, and both if the observation’s merge key is found in both. validate : string, default None. If specified, checks if merge is of specified type. “one_to_one” or “1:1”: checks if merge keys are unique in both left and right datasets. “one_to_many” or “1:m”: checks if merge keys are unique in left dataset. “many_to_one” or “m:1”: checks if merge keys are unique in right dataset. “many_to_many” or “m:m”: allowed, but does not result in checks. Note Support for specifying index levels as the on, left_on, and right_on parameters was added in version 0.23.0. Support for merging named Series objects was added in version 0.24.0. The return type will be the same as left. If left is a DataFrame or named Series and right is a subclass of DataFrame, the return type will still be DataFrame. merge is a function in the pandas namespace, and it is also available as a DataFrame instance method merge(), with the calling DataFrame being implicitly considered the left object in the join. The related join() method, uses merge internally for the index-on-index (by default) and column(s)-on-index join. If you are joining on index only, you may wish to use DataFrame.join to save yourself some typing. Brief primer on merge methods (relational algebra)# Experienced users of relational databases like SQL will be familiar with the terminology used to describe join operations between two SQL-table like structures (DataFrame objects). There are several cases to consider which are very important to understand: one-to-one joins: for example when joining two DataFrame objects on their indexes (which must contain unique values). many-to-one joins: for example when joining an index (unique) to one or more columns in a different DataFrame. many-to-many joins: joining columns on columns. Note When joining columns on columns (potentially a many-to-many join), any indexes on the passed DataFrame objects will be discarded. It is worth spending some time understanding the result of the many-to-many join case. In SQL / standard relational algebra, if a key combination appears more than once in both tables, the resulting table will have the Cartesian product of the associated data. Here is a very basic example with one unique key combination: In [33]: left = pd.DataFrame( ....: { ....: "key": ["K0", "K1", "K2", "K3"], ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: } ....: ) ....: In [34]: right = pd.DataFrame( ....: { ....: "key": ["K0", "K1", "K2", "K3"], ....: "C": ["C0", "C1", "C2", "C3"], ....: "D": ["D0", "D1", "D2", "D3"], ....: } ....: ) ....: In [35]: result = pd.merge(left, right, on="key") Here is a more complicated example with multiple join keys. Only the keys appearing in left and right are present (the intersection), since how='inner' by default. In [36]: left = pd.DataFrame( ....: { ....: "key1": ["K0", "K0", "K1", "K2"], ....: "key2": ["K0", "K1", "K0", "K1"], ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: } ....: ) ....: In [37]: right = pd.DataFrame( ....: { ....: "key1": ["K0", "K1", "K1", "K2"], ....: "key2": ["K0", "K0", "K0", "K0"], ....: "C": ["C0", "C1", "C2", "C3"], ....: "D": ["D0", "D1", "D2", "D3"], ....: } ....: ) ....: In [38]: result = pd.merge(left, right, on=["key1", "key2"]) The how argument to merge specifies how to determine which keys are to be included in the resulting table. If a key combination does not appear in either the left or right tables, the values in the joined table will be NA. Here is a summary of the how options and their SQL equivalent names: Merge method SQL Join Name Description left LEFT OUTER JOIN Use keys from left frame only right RIGHT OUTER JOIN Use keys from right frame only outer FULL OUTER JOIN Use union of keys from both frames inner INNER JOIN Use intersection of keys from both frames cross CROSS JOIN Create the cartesian product of rows of both frames In [39]: result = pd.merge(left, right, how="left", on=["key1", "key2"]) In [40]: result = pd.merge(left, right, how="right", on=["key1", "key2"]) In [41]: result = pd.merge(left, right, how="outer", on=["key1", "key2"]) In [42]: result = pd.merge(left, right, how="inner", on=["key1", "key2"]) In [43]: result = pd.merge(left, right, how="cross") You can merge a mult-indexed Series and a DataFrame, if the names of the MultiIndex correspond to the columns from the DataFrame. Transform the Series to a DataFrame using Series.reset_index() before merging, as shown in the following example. In [44]: df = pd.DataFrame({"Let": ["A", "B", "C"], "Num": [1, 2, 3]}) In [45]: df Out[45]: Let Num 0 A 1 1 B 2 2 C 3 In [46]: ser = pd.Series( ....: ["a", "b", "c", "d", "e", "f"], ....: index=pd.MultiIndex.from_arrays( ....: [["A", "B", "C"] * 2, [1, 2, 3, 4, 5, 6]], names=["Let", "Num"] ....: ), ....: ) ....: In [47]: ser Out[47]: Let Num A 1 a B 2 b C 3 c A 4 d B 5 e C 6 f dtype: object In [48]: pd.merge(df, ser.reset_index(), on=["Let", "Num"]) Out[48]: Let Num 0 0 A 1 a 1 B 2 b 2 C 3 c Here is another example with duplicate join keys in DataFrames: In [49]: left = pd.DataFrame({"A": [1, 2], "B": [2, 2]}) In [50]: right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]}) In [51]: result = pd.merge(left, right, on="B", how="outer") Warning Joining / merging on duplicate keys can cause a returned frame that is the multiplication of the row dimensions, which may result in memory overflow. It is the user’ s responsibility to manage duplicate values in keys before joining large DataFrames. Checking for duplicate keys# Users can use the validate argument to automatically check whether there are unexpected duplicates in their merge keys. Key uniqueness is checked before merge operations and so should protect against memory overflows. Checking key uniqueness is also a good way to ensure user data structures are as expected. In the following example, there are duplicate values of B in the right DataFrame. As this is not a one-to-one merge – as specified in the validate argument – an exception will be raised. In [52]: left = pd.DataFrame({"A": [1, 2], "B": [1, 2]}) In [53]: right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]}) In [53]: result = pd.merge(left, right, on="B", how="outer", validate="one_to_one") ... MergeError: Merge keys are not unique in right dataset; not a one-to-one merge If the user is aware of the duplicates in the right DataFrame but wants to ensure there are no duplicates in the left DataFrame, one can use the validate='one_to_many' argument instead, which will not raise an exception. In [54]: pd.merge(left, right, on="B", how="outer", validate="one_to_many") Out[54]: A_x B A_y 0 1 1 NaN 1 2 2 4.0 2 2 2 5.0 3 2 2 6.0 The merge indicator# merge() accepts the argument indicator. If True, a Categorical-type column called _merge will be added to the output object that takes on values: Observation Origin _merge value Merge key only in 'left' frame left_only Merge key only in 'right' frame right_only Merge key in both frames both In [55]: df1 = pd.DataFrame({"col1": [0, 1], "col_left": ["a", "b"]}) In [56]: df2 = pd.DataFrame({"col1": [1, 2, 2], "col_right": [2, 2, 2]}) In [57]: pd.merge(df1, df2, on="col1", how="outer", indicator=True) Out[57]: col1 col_left col_right _merge 0 0 a NaN left_only 1 1 b 2.0 both 2 2 NaN 2.0 right_only 3 2 NaN 2.0 right_only The indicator argument will also accept string arguments, in which case the indicator function will use the value of the passed string as the name for the indicator column. In [58]: pd.merge(df1, df2, on="col1", how="outer", indicator="indicator_column") Out[58]: col1 col_left col_right indicator_column 0 0 a NaN left_only 1 1 b 2.0 both 2 2 NaN 2.0 right_only 3 2 NaN 2.0 right_only Merge dtypes# Merging will preserve the dtype of the join keys. In [59]: left = pd.DataFrame({"key": [1], "v1": [10]}) In [60]: left Out[60]: key v1 0 1 10 In [61]: right = pd.DataFrame({"key": [1, 2], "v1": [20, 30]}) In [62]: right Out[62]: key v1 0 1 20 1 2 30 We are able to preserve the join keys: In [63]: pd.merge(left, right, how="outer") Out[63]: key v1 0 1 10 1 1 20 2 2 30 In [64]: pd.merge(left, right, how="outer").dtypes Out[64]: key int64 v1 int64 dtype: object Of course if you have missing values that are introduced, then the resulting dtype will be upcast. In [65]: pd.merge(left, right, how="outer", on="key") Out[65]: key v1_x v1_y 0 1 10.0 20 1 2 NaN 30 In [66]: pd.merge(left, right, how="outer", on="key").dtypes Out[66]: key int64 v1_x float64 v1_y int64 dtype: object Merging will preserve category dtypes of the mergands. See also the section on categoricals. The left frame. In [67]: from pandas.api.types import CategoricalDtype In [68]: X = pd.Series(np.random.choice(["foo", "bar"], size=(10,))) In [69]: X = X.astype(CategoricalDtype(categories=["foo", "bar"])) In [70]: left = pd.DataFrame( ....: {"X": X, "Y": np.random.choice(["one", "two", "three"], size=(10,))} ....: ) ....: In [71]: left Out[71]: X Y 0 bar one 1 foo one 2 foo three 3 bar three 4 foo one 5 bar one 6 bar three 7 bar three 8 bar three 9 foo three In [72]: left.dtypes Out[72]: X category Y object dtype: object The right frame. In [73]: right = pd.DataFrame( ....: { ....: "X": pd.Series(["foo", "bar"], dtype=CategoricalDtype(["foo", "bar"])), ....: "Z": [1, 2], ....: } ....: ) ....: In [74]: right Out[74]: X Z 0 foo 1 1 bar 2 In [75]: right.dtypes Out[75]: X category Z int64 dtype: object The merged result: In [76]: result = pd.merge(left, right, how="outer") In [77]: result Out[77]: X Y Z 0 bar one 2 1 bar three 2 2 bar one 2 3 bar three 2 4 bar three 2 5 bar three 2 6 foo one 1 7 foo three 1 8 foo one 1 9 foo three 1 In [78]: result.dtypes Out[78]: X category Y object Z int64 dtype: object Note The category dtypes must be exactly the same, meaning the same categories and the ordered attribute. Otherwise the result will coerce to the categories’ dtype. Note Merging on category dtypes that are the same can be quite performant compared to object dtype merging. Joining on index# DataFrame.join() is a convenient method for combining the columns of two potentially differently-indexed DataFrames into a single result DataFrame. Here is a very basic example: In [79]: left = pd.DataFrame( ....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=["K0", "K1", "K2"] ....: ) ....: In [80]: right = pd.DataFrame( ....: {"C": ["C0", "C2", "C3"], "D": ["D0", "D2", "D3"]}, index=["K0", "K2", "K3"] ....: ) ....: In [81]: result = left.join(right) In [82]: result = left.join(right, how="outer") The same as above, but with how='inner'. In [83]: result = left.join(right, how="inner") The data alignment here is on the indexes (row labels). This same behavior can be achieved using merge plus additional arguments instructing it to use the indexes: In [84]: result = pd.merge(left, right, left_index=True, right_index=True, how="outer") In [85]: result = pd.merge(left, right, left_index=True, right_index=True, how="inner") Joining key columns on an index# join() takes an optional on argument which may be a column or multiple column names, which specifies that the passed DataFrame is to be aligned on that column in the DataFrame. These two function calls are completely equivalent: left.join(right, on=key_or_keys) pd.merge( left, right, left_on=key_or_keys, right_index=True, how="left", sort=False ) Obviously you can choose whichever form you find more convenient. For many-to-one joins (where one of the DataFrame’s is already indexed by the join key), using join may be more convenient. Here is a simple example: In [86]: left = pd.DataFrame( ....: { ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: "key": ["K0", "K1", "K0", "K1"], ....: } ....: ) ....: In [87]: right = pd.DataFrame({"C": ["C0", "C1"], "D": ["D0", "D1"]}, index=["K0", "K1"]) In [88]: result = left.join(right, on="key") In [89]: result = pd.merge( ....: left, right, left_on="key", right_index=True, how="left", sort=False ....: ) ....: To join on multiple keys, the passed DataFrame must have a MultiIndex: In [90]: left = pd.DataFrame( ....: { ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: "key1": ["K0", "K0", "K1", "K2"], ....: "key2": ["K0", "K1", "K0", "K1"], ....: } ....: ) ....: In [91]: index = pd.MultiIndex.from_tuples( ....: [("K0", "K0"), ("K1", "K0"), ("K2", "K0"), ("K2", "K1")] ....: ) ....: In [92]: right = pd.DataFrame( ....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=index ....: ) ....: Now this can be joined by passing the two key column names: In [93]: result = left.join(right, on=["key1", "key2"]) The default for DataFrame.join is to perform a left join (essentially a “VLOOKUP” operation, for Excel users), which uses only the keys found in the calling DataFrame. Other join types, for example inner join, can be just as easily performed: In [94]: result = left.join(right, on=["key1", "key2"], how="inner") As you can see, this drops any rows where there was no match. Joining a single Index to a MultiIndex# You can join a singly-indexed DataFrame with a level of a MultiIndexed DataFrame. The level will match on the name of the index of the singly-indexed frame against a level name of the MultiIndexed frame. In [95]: left = pd.DataFrame( ....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, ....: index=pd.Index(["K0", "K1", "K2"], name="key"), ....: ) ....: In [96]: index = pd.MultiIndex.from_tuples( ....: [("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")], ....: names=["key", "Y"], ....: ) ....: In [97]: right = pd.DataFrame( ....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, ....: index=index, ....: ) ....: In [98]: result = left.join(right, how="inner") This is equivalent but less verbose and more memory efficient / faster than this. In [99]: result = pd.merge( ....: left.reset_index(), right.reset_index(), on=["key"], how="inner" ....: ).set_index(["key","Y"]) ....: Joining with two MultiIndexes# This is supported in a limited way, provided that the index for the right argument is completely used in the join, and is a subset of the indices in the left argument, as in this example: In [100]: leftindex = pd.MultiIndex.from_product( .....: [list("abc"), list("xy"), [1, 2]], names=["abc", "xy", "num"] .....: ) .....: In [101]: left = pd.DataFrame({"v1": range(12)}, index=leftindex) In [102]: left Out[102]: v1 abc xy num a x 1 0 2 1 y 1 2 2 3 b x 1 4 2 5 y 1 6 2 7 c x 1 8 2 9 y 1 10 2 11 In [103]: rightindex = pd.MultiIndex.from_product( .....: [list("abc"), list("xy")], names=["abc", "xy"] .....: ) .....: In [104]: right = pd.DataFrame({"v2": [100 * i for i in range(1, 7)]}, index=rightindex) In [105]: right Out[105]: v2 abc xy a x 100 y 200 b x 300 y 400 c x 500 y 600 In [106]: left.join(right, on=["abc", "xy"], how="inner") Out[106]: v1 v2 abc xy num a x 1 0 100 2 1 100 y 1 2 200 2 3 200 b x 1 4 300 2 5 300 y 1 6 400 2 7 400 c x 1 8 500 2 9 500 y 1 10 600 2 11 600 If that condition is not satisfied, a join with two multi-indexes can be done using the following code. In [107]: leftindex = pd.MultiIndex.from_tuples( .....: [("K0", "X0"), ("K0", "X1"), ("K1", "X2")], names=["key", "X"] .....: ) .....: In [108]: left = pd.DataFrame( .....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=leftindex .....: ) .....: In [109]: rightindex = pd.MultiIndex.from_tuples( .....: [("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")], names=["key", "Y"] .....: ) .....: In [110]: right = pd.DataFrame( .....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=rightindex .....: ) .....: In [111]: result = pd.merge( .....: left.reset_index(), right.reset_index(), on=["key"], how="inner" .....: ).set_index(["key", "X", "Y"]) .....: Merging on a combination of columns and index levels# Strings passed as the on, left_on, and right_on parameters may refer to either column names or index level names. This enables merging DataFrame instances on a combination of index levels and columns without resetting indexes. In [112]: left_index = pd.Index(["K0", "K0", "K1", "K2"], name="key1") In [113]: left = pd.DataFrame( .....: { .....: "A": ["A0", "A1", "A2", "A3"], .....: "B": ["B0", "B1", "B2", "B3"], .....: "key2": ["K0", "K1", "K0", "K1"], .....: }, .....: index=left_index, .....: ) .....: In [114]: right_index = pd.Index(["K0", "K1", "K2", "K2"], name="key1") In [115]: right = pd.DataFrame( .....: { .....: "C": ["C0", "C1", "C2", "C3"], .....: "D": ["D0", "D1", "D2", "D3"], .....: "key2": ["K0", "K0", "K0", "K1"], .....: }, .....: index=right_index, .....: ) .....: In [116]: result = left.merge(right, on=["key1", "key2"]) Note When DataFrames are merged on a string that matches an index level in both frames, the index level is preserved as an index level in the resulting DataFrame. Note When DataFrames are merged using only some of the levels of a MultiIndex, the extra levels will be dropped from the resulting merge. In order to preserve those levels, use reset_index on those level names to move those levels to columns prior to doing the merge. Note If a string matches both a column name and an index level name, then a warning is issued and the column takes precedence. This will result in an ambiguity error in a future version. Overlapping value columns# The merge suffixes argument takes a tuple of list of strings to append to overlapping column names in the input DataFrames to disambiguate the result columns: In [117]: left = pd.DataFrame({"k": ["K0", "K1", "K2"], "v": [1, 2, 3]}) In [118]: right = pd.DataFrame({"k": ["K0", "K0", "K3"], "v": [4, 5, 6]}) In [119]: result = pd.merge(left, right, on="k") In [120]: result = pd.merge(left, right, on="k", suffixes=("_l", "_r")) DataFrame.join() has lsuffix and rsuffix arguments which behave similarly. In [121]: left = left.set_index("k") In [122]: right = right.set_index("k") In [123]: result = left.join(right, lsuffix="_l", rsuffix="_r") Joining multiple DataFrames# A list or tuple of DataFrames can also be passed to join() to join them together on their indexes. In [124]: right2 = pd.DataFrame({"v": [7, 8, 9]}, index=["K1", "K1", "K2"]) In [125]: result = left.join([right, right2]) Merging together values within Series or DataFrame columns# Another fairly common situation is to have two like-indexed (or similarly indexed) Series or DataFrame objects and wanting to “patch” values in one object from values for matching indices in the other. Here is an example: In [126]: df1 = pd.DataFrame( .....: [[np.nan, 3.0, 5.0], [-4.6, np.nan, np.nan], [np.nan, 7.0, np.nan]] .....: ) .....: In [127]: df2 = pd.DataFrame([[-42.6, np.nan, -8.2], [-5.0, 1.6, 4]], index=[1, 2]) For this, use the combine_first() method: In [128]: result = df1.combine_first(df2) Note that this method only takes values from the right DataFrame if they are missing in the left DataFrame. A related method, update(), alters non-NA values in place: In [129]: df1.update(df2) Timeseries friendly merging# Merging ordered data# A merge_ordered() function allows combining time series and other ordered data. In particular it has an optional fill_method keyword to fill/interpolate missing data: In [130]: left = pd.DataFrame( .....: {"k": ["K0", "K1", "K1", "K2"], "lv": [1, 2, 3, 4], "s": ["a", "b", "c", "d"]} .....: ) .....: In [131]: right = pd.DataFrame({"k": ["K1", "K2", "K4"], "rv": [1, 2, 3]}) In [132]: pd.merge_ordered(left, right, fill_method="ffill", left_by="s") Out[132]: k lv s rv 0 K0 1.0 a NaN 1 K1 1.0 a 1.0 2 K2 1.0 a 2.0 3 K4 1.0 a 3.0 4 K1 2.0 b 1.0 5 K2 2.0 b 2.0 6 K4 2.0 b 3.0 7 K1 3.0 c 1.0 8 K2 3.0 c 2.0 9 K4 3.0 c 3.0 10 K1 NaN d 1.0 11 K2 4.0 d 2.0 12 K4 4.0 d 3.0 Merging asof# A merge_asof() is similar to an ordered left-join except that we match on nearest key rather than equal keys. For each row in the left DataFrame, we select the last row in the right DataFrame whose on key is less than the left’s key. Both DataFrames must be sorted by the key. Optionally an asof merge can perform a group-wise merge. This matches the by key equally, in addition to the nearest match on the on key. For example; we might have trades and quotes and we want to asof merge them. In [133]: trades = pd.DataFrame( .....: { .....: "time": pd.to_datetime( .....: [ .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.038", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.048", .....: ] .....: ), .....: "ticker": ["MSFT", "MSFT", "GOOG", "GOOG", "AAPL"], .....: "price": [51.95, 51.95, 720.77, 720.92, 98.00], .....: "quantity": [75, 155, 100, 100, 100], .....: }, .....: columns=["time", "ticker", "price", "quantity"], .....: ) .....: In [134]: quotes = pd.DataFrame( .....: { .....: "time": pd.to_datetime( .....: [ .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.030", .....: "20160525 13:30:00.041", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.049", .....: "20160525 13:30:00.072", .....: "20160525 13:30:00.075", .....: ] .....: ), .....: "ticker": ["GOOG", "MSFT", "MSFT", "MSFT", "GOOG", "AAPL", "GOOG", "MSFT"], .....: "bid": [720.50, 51.95, 51.97, 51.99, 720.50, 97.99, 720.50, 52.01], .....: "ask": [720.93, 51.96, 51.98, 52.00, 720.93, 98.01, 720.88, 52.03], .....: }, .....: columns=["time", "ticker", "bid", "ask"], .....: ) .....: In [135]: trades Out[135]: time ticker price quantity 0 2016-05-25 13:30:00.023 MSFT 51.95 75 1 2016-05-25 13:30:00.038 MSFT 51.95 155 2 2016-05-25 13:30:00.048 GOOG 720.77 100 3 2016-05-25 13:30:00.048 GOOG 720.92 100 4 2016-05-25 13:30:00.048 AAPL 98.00 100 In [136]: quotes Out[136]: time ticker bid ask 0 2016-05-25 13:30:00.023 GOOG 720.50 720.93 1 2016-05-25 13:30:00.023 MSFT 51.95 51.96 2 2016-05-25 13:30:00.030 MSFT 51.97 51.98 3 2016-05-25 13:30:00.041 MSFT 51.99 52.00 4 2016-05-25 13:30:00.048 GOOG 720.50 720.93 5 2016-05-25 13:30:00.049 AAPL 97.99 98.01 6 2016-05-25 13:30:00.072 GOOG 720.50 720.88 7 2016-05-25 13:30:00.075 MSFT 52.01 52.03 By default we are taking the asof of the quotes. In [137]: pd.merge_asof(trades, quotes, on="time", by="ticker") Out[137]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 51.95 51.96 1 2016-05-25 13:30:00.038 MSFT 51.95 155 51.97 51.98 2 2016-05-25 13:30:00.048 GOOG 720.77 100 720.50 720.93 3 2016-05-25 13:30:00.048 GOOG 720.92 100 720.50 720.93 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN We only asof within 2ms between the quote time and the trade time. In [138]: pd.merge_asof(trades, quotes, on="time", by="ticker", tolerance=pd.Timedelta("2ms")) Out[138]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 51.95 51.96 1 2016-05-25 13:30:00.038 MSFT 51.95 155 NaN NaN 2 2016-05-25 13:30:00.048 GOOG 720.77 100 720.50 720.93 3 2016-05-25 13:30:00.048 GOOG 720.92 100 720.50 720.93 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN We only asof within 10ms between the quote time and the trade time and we exclude exact matches on time. Note that though we exclude the exact matches (of the quotes), prior quotes do propagate to that point in time. In [139]: pd.merge_asof( .....: trades, .....: quotes, .....: on="time", .....: by="ticker", .....: tolerance=pd.Timedelta("10ms"), .....: allow_exact_matches=False, .....: ) .....: Out[139]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 NaN NaN 1 2016-05-25 13:30:00.038 MSFT 51.95 155 51.97 51.98 2 2016-05-25 13:30:00.048 GOOG 720.77 100 NaN NaN 3 2016-05-25 13:30:00.048 GOOG 720.92 100 NaN NaN 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN Comparing objects# The compare() and compare() methods allow you to compare two DataFrame or Series, respectively, and summarize their differences. This feature was added in V1.1.0. For example, you might want to compare two DataFrame and stack their differences side by side. In [140]: df = pd.DataFrame( .....: { .....: "col1": ["a", "a", "b", "b", "a"], .....: "col2": [1.0, 2.0, 3.0, np.nan, 5.0], .....: "col3": [1.0, 2.0, 3.0, 4.0, 5.0], .....: }, .....: columns=["col1", "col2", "col3"], .....: ) .....: In [141]: df Out[141]: col1 col2 col3 0 a 1.0 1.0 1 a 2.0 2.0 2 b 3.0 3.0 3 b NaN 4.0 4 a 5.0 5.0 In [142]: df2 = df.copy() In [143]: df2.loc[0, "col1"] = "c" In [144]: df2.loc[2, "col3"] = 4.0 In [145]: df2 Out[145]: col1 col2 col3 0 c 1.0 1.0 1 a 2.0 2.0 2 b 3.0 4.0 3 b NaN 4.0 4 a 5.0 5.0 In [146]: df.compare(df2) Out[146]: col1 col3 self other self other 0 a c NaN NaN 2 NaN NaN 3.0 4.0 By default, if two corresponding values are equal, they will be shown as NaN. Furthermore, if all values in an entire row / column, the row / column will be omitted from the result. The remaining differences will be aligned on columns. If you wish, you may choose to stack the differences on rows. In [147]: df.compare(df2, align_axis=0) Out[147]: col1 col3 0 self a NaN other c NaN 2 self NaN 3.0 other NaN 4.0 If you wish to keep all original rows and columns, set keep_shape argument to True. In [148]: df.compare(df2, keep_shape=True) Out[148]: col1 col2 col3 self other self other self other 0 a c NaN NaN NaN NaN 1 NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN 3.0 4.0 3 NaN NaN NaN NaN NaN NaN 4 NaN NaN NaN NaN NaN NaN You may also keep all the original values even if they are equal. In [149]: df.compare(df2, keep_shape=True, keep_equal=True) Out[149]: col1 col2 col3 self other self other self other 0 a c 1.0 1.0 1.0 1.0 1 a a 2.0 2.0 2.0 2.0 2 b b 3.0 3.0 3.0 4.0 3 b b NaN NaN 4.0 4.0 4 a a 5.0 5.0 5.0 5.0
652
1,185
How can I "join" rows with the same ID in pandas and add data as new columns I have a dataframe which contains multiple rows for individual clients. There is a separate row for each product with all fields identical other than the item and amount column for a given reference. Example name reference item amount 0 john 1234 chair 40 1 john 1234 table 10 2 john 1234 table 20 2 john 1234 pole 10 3 jane 9876 chair 15 4 jane 9876 pole 30 My Problem Each customer has an unknown amount of entries in the database (around 10 entries would be the maximum). I want to edit this dataframe to have entries with the same reference on the same row. The row need to also have extra columns added for 'item 2' and 'amount 2'. Example Output name reference item 1 amount1 item2 amount2 item3 amount3 item4 amount4 0 john 1234 chair 40 table 10 table 20 pole 10 1 jane 9876 chair 15 pole 30 NaN NaN NaN NaN After reading the documentation and having a look for other similar questions, I can only find how to merge / aggregate the data which will not work in my scenario. Is this possible in Pandas? Thanks!
Let's unstack() by tracking position using groupby()+cumcount(): df['s']=df.groupby(['name','reference']).cumcount()+1 df=df.set_index(['s','name','reference']).unstack(0) df.columns=[f"{x}{y}" for x,y in df.columns] df=df.reset_index() output of df: name reference item1 item2 item3 item4 amount1 amount2 amount3 amount4 0 jane 9876 chair pole NaN NaN 15.0 30.0 NaN NaN 1 john 1234 chair table table pole 40.0 10.0 20.0 10.0
70,210,875
How to do a nested renamer in pandas in groupby function
<p>I am trying to do like this</p> <pre><code>df_temp = df.groupby(['_SubjectId', 'QuestionTypeId'], as_index=True)['QuestionTypeId'].count() </code></pre> <p>Here Subject have 10 different values and Question type has 5 and i want to know how many Questions are there of each QuestionType and Subject, i am able to do it with it but i also want to give a name to the count returned</p> <p>I am doing it like this</p> <pre><code>df_temp = df.groupby(['_SubjectId', 'QuestionTypeId'], as_index=True).agg({'QuestionTypeId': {'count': 'count'}}) </code></pre> <p>But it is giving me nested renaming error , so please suggest an alternative way of achieving the result i needed, i have gone through other answers but it is not helping me</p>
70,210,890
"2021-12-03T07:33:21.980000"
1
null
1
55
python|pandas
<p>Use names aggreagtion - it means for column <code>QuestionTypeId</code> use aggragation <code>count</code> and assign to column <code>new</code>:</p> <pre><code>df_temp = (df.groupby(['_SubjectId', 'QuestionTypeId'], as_index=True) .agg(new=('QuestionTypeId': 'count'))) </code></pre>
"2021-12-03T07:34:42.367000"
1
https://pandas.pydata.org/docs/dev/reference/api/pandas.errors.SpecificationError.html
pandas.errors.SpecificationError# pandas.errors.SpecificationError# Use names aggreagtion - it means for column QuestionTypeId use aggragation count and assign to column new: df_temp = (df.groupby(['_SubjectId', 'QuestionTypeId'], as_index=True) .agg(new=('QuestionTypeId': 'count'))) exception pandas.errors.SpecificationError[source]# Exception raised by agg when the functions are ill-specified. The exception raised in two scenarios. The first way is calling agg on a Dataframe or Series using a nested renamer (dict-of-dict). The second way is calling agg on a Dataframe with duplicated functions names without assigning column name. Examples >>> df = pd.DataFrame({'A': [1, 1, 1, 2, 2], ... 'B': range(5), ... 'C': range(5)}) >>> df.groupby('A').B.agg({'foo': 'count'}) ... # SpecificationError: nested renamer is not supported >>> df.groupby('A').agg({'B': {'foo': ['sum', 'max']}}) ... # SpecificationError: nested renamer is not supported >>> df.groupby('A').agg(['min', 'min']) ... # SpecificationError: nested renamer is not supported
70
300
How to do a nested renamer in pandas in groupby function I am trying to do like this df_temp = df.groupby(['_SubjectId', 'QuestionTypeId'], as_index=True)['QuestionTypeId'].count() Here Subject have 10 different values and Question type has 5 and i want to know how many Questions are there of each QuestionType and Subject, i am able to do it with it but i also want to give a name to the count returned I am doing it like this df_temp = df.groupby(['_SubjectId', 'QuestionTypeId'], as_index=True).agg({'QuestionTypeId': {'count': 'count'}}) But it is giving me nested renaming error , so please suggest an alternative way of achieving the result i needed, i have gone through other answers but it is not helping me
How to do a nested renamer in pandas in groupby function
Use names aggreagtion - it means for column QuestionTypeId use aggragation count and assign to column new: df_temp = (df.groupby(['_SubjectId', 'QuestionTypeId'], as_index=True) .agg(new=('QuestionTypeId': 'count')))
61,871,338
Pandas: Creating column by applying custom funtions on row level
<p>I have a dataframe <code>df</code> as follows</p> <pre><code>ZMONTH GROUP ORDER_QTY 201907 A 25 201908 A 23 201909 A 24 201907 B 15 201908 B 0 201909 B 0 </code></pre> <p>I want to add another column <code>ACTIVE</code> based on the following condition:</p> <pre><code>if ORDER_QTY of last two rows for each group is 0, then ACTIVE==0 else 1 </code></pre> <p>So the resultant <code>df_f</code> looks like</p> <pre><code>ZMONTH GROUP ORDER_QTY ACTIVE 201907 A 25 1 201908 A 23 1 201909 A 24 1 201907 B 15 0 201908 B 0 0 201909 B 0 0 </code></pre> <p>With the above logic, I have tried the following</p> <pre><code>def active_field(row): if row.loc[row['ZMONTH']=='201909','ORDER_QTY']==0: val=0 elif row.loc[row['ZMONTH']=='201908','ORDER_QTY']==0: val=0 else: val=1 return val df['ACTIVE'] = df.apply(active_field,axis=1) </code></pre> <p>The above code is giving <code>IndexingError: Too many indexers</code> Error. </p> <p>Where am I missing? </p>
61,871,403
"2020-05-18T14:01:41.500000"
3
null
0
55
python|pandas
<p>You can <code>groupby</code> and check for equality on the last two rows per group, and take <code>all</code> on the boolean result. Then just view the resulting boolean arrays as <code>int8</code> and assign them to <code>ACTIVE</code>:</p> <pre><code>df['ACTIVE'] = (df.groupby('GROUP').ORDER_QTY .transform(lambda x: x.tail(2).ne(0).any()) .view('i1')) </code></pre> <hr> <pre><code>print(df) ZMONTH GROUP ORDER_QTY ACTIVE 0 201907 A 25 1 1 201908 A 23 1 2 201909 A 24 1 3 201907 B 15 0 4 201908 B 0 0 5 201909 B 0 0 </code></pre>
"2020-05-18T14:04:21.913000"
1
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.set_index.html
pandas.DataFrame.set_index# pandas.DataFrame.set_index# DataFrame.set_index(keys, *, drop=True, append=False, inplace=False, verify_integrity=False)[source]# Set the DataFrame index using existing columns. Set the DataFrame index (row labels) using one or more existing columns or arrays (of the correct length). The index can replace the existing index or expand on it. Parameters keyslabel or array-like or list of labels/arraysThis parameter can be either a single column key, a single array of the same length as the calling DataFrame, or a list containing an arbitrary combination of column keys and arrays. Here, “array” You can groupby and check for equality on the last two rows per group, and take all on the boolean result. Then just view the resulting boolean arrays as int8 and assign them to ACTIVE: df['ACTIVE'] = (df.groupby('GROUP').ORDER_QTY .transform(lambda x: x.tail(2).ne(0).any()) .view('i1')) print(df) ZMONTH GROUP ORDER_QTY ACTIVE 0 201907 A 25 1 1 201908 A 23 1 2 201909 A 24 1 3 201907 B 15 0 4 201908 B 0 0 5 201909 B 0 0 encompasses Series, Index, np.ndarray, and instances of Iterator. dropbool, default TrueDelete columns to be used as the new index. appendbool, default FalseWhether to append columns to existing index. inplacebool, default FalseWhether to modify the DataFrame rather than creating a new one. verify_integritybool, default FalseCheck the new index for duplicates. Otherwise defer the check until necessary. Setting to False will improve the performance of this method. Returns DataFrame or NoneChanged row labels or None if inplace=True. See also DataFrame.reset_indexOpposite of set_index. DataFrame.reindexChange to new indices or expand indices. DataFrame.reindex_likeChange to same indices as other DataFrame. Examples >>> df = pd.DataFrame({'month': [1, 4, 7, 10], ... 'year': [2012, 2014, 2013, 2014], ... 'sale': [55, 40, 84, 31]}) >>> df month year sale 0 1 2012 55 1 4 2014 40 2 7 2013 84 3 10 2014 31 Set the index to become the ‘month’ column: >>> df.set_index('month') year sale month 1 2012 55 4 2014 40 7 2013 84 10 2014 31 Create a MultiIndex using columns ‘year’ and ‘month’: >>> df.set_index(['year', 'month']) sale year month 2012 1 55 2014 4 40 2013 7 84 2014 10 31 Create a MultiIndex using an Index and a column: >>> df.set_index([pd.Index([1, 2, 3, 4]), 'year']) month sale year 1 2012 1 55 2 2014 4 40 3 2013 7 84 4 2014 10 31 Create a MultiIndex using two Series: >>> s = pd.Series([1, 2, 3, 4]) >>> df.set_index([s, s**2]) month year sale 1 1 1 2012 55 2 4 4 2014 40 3 9 7 2013 84 4 16 10 2014 31
633
1,215
Pandas: Creating column by applying custom funtions on row level I have a dataframe df as follows ZMONTH GROUP ORDER_QTY 201907 A 25 201908 A 23 201909 A 24 201907 B 15 201908 B 0 201909 B 0 I want to add another column ACTIVE based on the following condition: if ORDER_QTY of last two rows for each group is 0, then ACTIVE==0 else 1 So the resultant df_f looks like ZMONTH GROUP ORDER_QTY ACTIVE 201907 A 25 1 201908 A 23 1 201909 A 24 1 201907 B 15 0 201908 B 0 0 201909 B 0 0 With the above logic, I have tried the following def active_field(row): if row.loc[row['ZMONTH']=='201909','ORDER_QTY']==0: val=0 elif row.loc[row['ZMONTH']=='201908','ORDER_QTY']==0: val=0 else: val=1 return val df['ACTIVE'] = df.apply(active_field,axis=1) The above code is giving IndexingError: Too many indexers Error. Where am I missing?
You can groupby and check for equality on the last two rows per group, and take all on the boolean result. Then just view the resulting boolean arrays as int8 and assign them to ACTIVE: df['ACTIVE'] = (df.groupby('GROUP').ORDER_QTY .transform(lambda x: x.tail(2).ne(0).any()) .view('i1')) print(df) ZMONTH GROUP ORDER_QTY ACTIVE 0 201907 A 25 1 1 201908 A 23 1 2 201909 A 24 1 3 201907 B 15 0 4 201908 B 0 0 5 201909 B 0 0
67,224,766
How to iterate over every data entry and subtract each one from another? [Python]
<p>So i have the following (i'm working with pandas btw.) when i print the head. As you can see i already get back the first entry of every day.</p> <pre><code> first Timestamp 2021-03-11 596319.8125 2021-03-12 596349.5625 2021-03-13 602205.1875 2021-03-14 609445.5625 2021-03-15 609479.6250 </code></pre> <p>And now i want to get the yield from every day, which means i have to subtract the value (in this case Energy = first) of day 1 from day 2, and than the value of day 2 from day 3 and so on and so forth.</p> <p>Here's my code so far, i know this only can be a noob problem but i'm very new to programming so i'm thankful for every answer.</p> <pre><code> import os import glob import pandas as pd # set working directory os.chdir(&quot;Path to CSVs&quot;) # find all csv files in the folder # use glob pattern matching -&gt; extension = 'csv' # save result in list -&gt; all_filenames extension = 'csv' all_filenames = [i for i in glob.glob('*.{}'.format(extension))] # print(all_filenames) # combine all files in the list combined_csv = pd.concat([pd.read_csv(f, sep=';') for f in all_filenames]) # Format CSV # Transform Timestamp column into datetime combined_csv['Timestamp'] = pd.to_datetime(combined_csv.Timestamp) # Read out first entry of every day of every month combined_csv = combined_csv.resample('D', on='Timestamp')['HtmDht_Energy'].agg(['first']) # To get the yield of day i have to subtract day 2 HtmDht_Energy - day 1 HtmDht_Energy # # # # print(combined_csv.head()) # export as excel file # combined_csv.to_excel('energy_data.xlsx', index=False) </code></pre> <p>After i tried the solution from below with the following code:</p> <pre><code> import os import glob import pandas as pd # set working directory os.chdir(&quot;C:/Users/winklerm/OneDrive - SOLID Solar Energy Systems GmbH/Desktop/CSVs&quot;) # find all csv files in the folder # use glob pattern matching -&gt; extension = 'csv' # save result in list -&gt; all_filenames extension = 'csv' all_filenames = [i for i in glob.glob('*.{}'.format(extension))] # print(all_filenames) # combine all files in the list combined_csv = pd.concat([pd.read_csv(f, sep=';') for f in all_filenames]) # Format CSV # Transform Timestamp column into datetime combined_csv['Timestamp'] = pd.to_datetime(combined_csv.Timestamp) # Read out first entry of every day of every month combined_csv = combined_csv.resample('D', on='Timestamp')['HtmDht_Energy'].agg(['first']) # To get the yield of day i have to subtract day 2 HtmDht_Energy - day 1 HtmDht_Energy combined_csv.assign(tagesertrag=combined_csv[&quot;first&quot;]-combined_csv[&quot;first&quot;].shift()) # # # print(combined_csv.head()) # export as excel file # combined_csv.to_excel('energy_data.xlsx', index=False) </code></pre> <p>But my print still looks like this:</p> <pre><code> first Timestamp 2021-03-11 596319.8125 2021-03-12 596349.5625 2021-03-13 602205.1875 2021-03-14 609445.5625 2021-03-15 609479.6250 Process finished with exit code 0 </code></pre>
67,225,551
"2021-04-23T06:09:04.007000"
1
null
0
56
python|pandas
<p>This is a simple case of subtracting <code>shift()</code> value</p> <pre><code>df = pd.read_csv(io.StringIO(&quot;&quot;&quot; Timestamp first 2021-03-11 596319.8125 2021-03-12 596349.5625 2021-03-13 602205.1875 2021-03-14 609445.5625 2021-03-15 609479.6250&quot;&quot;&quot;), sep=&quot;\s+&quot;) df.assign(yieldc=df[&quot;first&quot;]-df[&quot;first&quot;].shift()) </code></pre> <div class="s-table-container"> <table class="s-table"> <thead> <tr> <th style="text-align: right;"></th> <th style="text-align: left;">Timestamp</th> <th style="text-align: right;">first</th> <th style="text-align: right;">yieldc</th> </tr> </thead> <tbody> <tr> <td style="text-align: right;">0</td> <td style="text-align: left;">2021-03-11</td> <td style="text-align: right;">596320</td> <td style="text-align: right;">nan</td> </tr> <tr> <td style="text-align: right;">1</td> <td style="text-align: left;">2021-03-12</td> <td style="text-align: right;">596350</td> <td style="text-align: right;">29.75</td> </tr> <tr> <td style="text-align: right;">2</td> <td style="text-align: left;">2021-03-13</td> <td style="text-align: right;">602205</td> <td style="text-align: right;">5855.62</td> </tr> <tr> <td style="text-align: right;">3</td> <td style="text-align: left;">2021-03-14</td> <td style="text-align: right;">609446</td> <td style="text-align: right;">7240.38</td> </tr> <tr> <td style="text-align: right;">4</td> <td style="text-align: left;">2021-03-15</td> <td style="text-align: right;">609480</td> <td style="text-align: right;">34.0625</td> </tr> </tbody> </table> </div>
"2021-04-23T07:15:01.487000"
1
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.subtract.html
pandas.DataFrame.subtract# pandas.DataFrame.subtract# DataFrame.subtract(other, axis='columns', level=None, fill_value=None)[source]# Get Subtraction of dataframe and other, element-wise (binary operator sub). Equivalent to dataframe - other, but with support to substitute a fill_value for missing data in one of the inputs. With reverse version, rsub. Among flexible wrappers (add, sub, mul, div, mod, pow) to arithmetic operators: +, -, *, /, //, %, **. Parameters otherscalar, sequence, Series, dict or DataFrameAny single or multiple element data structure, or list-like object. axis{0 or ‘index’, 1 or ‘columns’}Whether to compare by the index (0 or ‘index’) or columns. (1 or ‘columns’). For Series input, axis to match Series index on. levelint or labelBroadcast across a level, matching Index values on the This is a simple case of subtracting shift() value df = pd.read_csv(io.StringIO(""" Timestamp first 2021-03-11 596319.8125 2021-03-12 596349.5625 2021-03-13 602205.1875 2021-03-14 609445.5625 2021-03-15 609479.6250"""), sep="\s+") df.assign(yieldc=df["first"]-df["first"].shift()) Timestamp first yieldc 0 2021-03-11 596320 nan 1 2021-03-12 596350 29.75 2 2021-03-13 602205 5855.62 3 2021-03-14 609446 7240.38 4 2021-03-15 609480 34.0625 passed MultiIndex level. fill_valuefloat or None, default NoneFill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing. Returns DataFrameResult of the arithmetic operation. See also DataFrame.addAdd DataFrames. DataFrame.subSubtract DataFrames. DataFrame.mulMultiply DataFrames. DataFrame.divDivide DataFrames (float division). DataFrame.truedivDivide DataFrames (float division). DataFrame.floordivDivide DataFrames (integer division). DataFrame.modCalculate modulo (remainder after division). DataFrame.powCalculate exponential power. Notes Mismatched indices will be unioned together. Examples >>> df = pd.DataFrame({'angles': [0, 3, 4], ... 'degrees': [360, 180, 360]}, ... index=['circle', 'triangle', 'rectangle']) >>> df angles degrees circle 0 360 triangle 3 180 rectangle 4 360 Add a scalar with operator version which return the same results. >>> df + 1 angles degrees circle 1 361 triangle 4 181 rectangle 5 361 >>> df.add(1) angles degrees circle 1 361 triangle 4 181 rectangle 5 361 Divide by constant with reverse version. >>> df.div(10) angles degrees circle 0.0 36.0 triangle 0.3 18.0 rectangle 0.4 36.0 >>> df.rdiv(10) angles degrees circle inf 0.027778 triangle 3.333333 0.055556 rectangle 2.500000 0.027778 Subtract a list and Series by axis with operator version. >>> df - [1, 2] angles degrees circle -1 358 triangle 2 178 rectangle 3 358 >>> df.sub([1, 2], axis='columns') angles degrees circle -1 358 triangle 2 178 rectangle 3 358 >>> df.sub(pd.Series([1, 1, 1], index=['circle', 'triangle', 'rectangle']), ... axis='index') angles degrees circle -1 359 triangle 2 179 rectangle 3 359 Multiply a dictionary by axis. >>> df.mul({'angles': 0, 'degrees': 2}) angles degrees circle 0 720 triangle 0 360 rectangle 0 720 >>> df.mul({'circle': 0, 'triangle': 2, 'rectangle': 3}, axis='index') angles degrees circle 0 0 triangle 6 360 rectangle 12 1080 Multiply a DataFrame of different shape with operator version. >>> other = pd.DataFrame({'angles': [0, 3, 4]}, ... index=['circle', 'triangle', 'rectangle']) >>> other angles circle 0 triangle 3 rectangle 4 >>> df * other angles degrees circle 0 NaN triangle 9 NaN rectangle 16 NaN >>> df.mul(other, fill_value=0) angles degrees circle 0 0.0 triangle 9 0.0 rectangle 16 0.0 Divide by a MultiIndex by level. >>> df_multindex = pd.DataFrame({'angles': [0, 3, 4, 4, 5, 6], ... 'degrees': [360, 180, 360, 360, 540, 720]}, ... index=[['A', 'A', 'A', 'B', 'B', 'B'], ... ['circle', 'triangle', 'rectangle', ... 'square', 'pentagon', 'hexagon']]) >>> df_multindex angles degrees A circle 0 360 triangle 3 180 rectangle 4 360 B square 4 360 pentagon 5 540 hexagon 6 720 >>> df.div(df_multindex, level=1, fill_value=0) angles degrees A circle NaN 1.0 triangle 1.0 1.0 rectangle 1.0 1.0 B square 0.0 0.0 pentagon 0.0 0.0 hexagon 0.0 0.0
824
1,308
How to iterate over every data entry and subtract each one from another? [Python] So i have the following (i'm working with pandas btw.) when i print the head. As you can see i already get back the first entry of every day. first Timestamp 2021-03-11 596319.8125 2021-03-12 596349.5625 2021-03-13 602205.1875 2021-03-14 609445.5625 2021-03-15 609479.6250 And now i want to get the yield from every day, which means i have to subtract the value (in this case Energy = first) of day 1 from day 2, and than the value of day 2 from day 3 and so on and so forth. Here's my code so far, i know this only can be a noob problem but i'm very new to programming so i'm thankful for every answer. import os import glob import pandas as pd # set working directory os.chdir("Path to CSVs") # find all csv files in the folder # use glob pattern matching -> extension = 'csv' # save result in list -> all_filenames extension = 'csv' all_filenames = [i for i in glob.glob('*.{}'.format(extension))] # print(all_filenames) # combine all files in the list combined_csv = pd.concat([pd.read_csv(f, sep=';') for f in all_filenames]) # Format CSV # Transform Timestamp column into datetime combined_csv['Timestamp'] = pd.to_datetime(combined_csv.Timestamp) # Read out first entry of every day of every month combined_csv = combined_csv.resample('D', on='Timestamp')['HtmDht_Energy'].agg(['first']) # To get the yield of day i have to subtract day 2 HtmDht_Energy - day 1 HtmDht_Energy # # # # print(combined_csv.head()) # export as excel file # combined_csv.to_excel('energy_data.xlsx', index=False) After i tried the solution from below with the following code: import os import glob import pandas as pd # set working directory os.chdir("C:/Users/winklerm/OneDrive - SOLID Solar Energy Systems GmbH/Desktop/CSVs") # find all csv files in the folder # use glob pattern matching -> extension = 'csv' # save result in list -> all_filenames extension = 'csv' all_filenames = [i for i in glob.glob('*.{}'.format(extension))] # print(all_filenames) # combine all files in the list combined_csv = pd.concat([pd.read_csv(f, sep=';') for f in all_filenames]) # Format CSV # Transform Timestamp column into datetime combined_csv['Timestamp'] = pd.to_datetime(combined_csv.Timestamp) # Read out first entry of every day of every month combined_csv = combined_csv.resample('D', on='Timestamp')['HtmDht_Energy'].agg(['first']) # To get the yield of day i have to subtract day 2 HtmDht_Energy - day 1 HtmDht_Energy combined_csv.assign(tagesertrag=combined_csv["first"]-combined_csv["first"].shift()) # # # print(combined_csv.head()) # export as excel file # combined_csv.to_excel('energy_data.xlsx', index=False) But my print still looks like this: first Timestamp 2021-03-11 596319.8125 2021-03-12 596349.5625 2021-03-13 602205.1875 2021-03-14 609445.5625 2021-03-15 609479.6250 Process finished with exit code 0
This is a simple case of subtracting shift() value df = pd.read_csv(io.StringIO(""" Timestamp first 2021-03-11 596319.8125 2021-03-12 596349.5625 2021-03-13 602205.1875 2021-03-14 609445.5625 2021-03-15 609479.6250"""), sep="\s+") df.assign(yieldc=df["first"]-df["first"].shift()) Timestamp first yieldc 0 2021-03-11 596320 nan 1 2021-03-12 596350 29.75 2 2021-03-13 602205 5855.62 3 2021-03-14 609446 7240.38 4 2021-03-15 609480 34.0625
68,347,614
Extract specific symbols from pandas cells, then replace them with values from a dict where they are keys
<p>My data looks like this, well these are two of the columns:</p> <pre><code>Index MSC Primary MSC Secondary 0 05C25 (05C20 20F05) 1 20-04 (20F55 62Qxx) 2 13F20 (17B20 22E40 22F30 65Fxx) 3 05Exx (20-04 20H20) 4 20G40 (05C25) </code></pre> <p>These are MSC codes, corresponding to different areas of science. I need to replace each code with the corresponding subject from this dict here: <a href="https://mathscinet.ams.org/msnhtml/msc2020.pdf" rel="nofollow noreferrer">https://mathscinet.ams.org/msnhtml/msc2020.pdf</a> , some of them are: &quot;&quot;&quot; 00 General and overarching topics; collections 01 History and biography 03 Mathematical logic and foundations 05 Combinatorics &quot;&quot;&quot;</p> <p>First I need to isolate the first two digits from each code, for instance <code>05C25</code> to transform to <code>05</code> or from the second column <code>(05E15 14H50)</code> to transform to <code>05, 14</code>.</p> <p>Then I need each number replaced by the corresponding science for example <code>05, 14</code> to become <code>Combinatorics, Algebraic geometry</code>. This is all tricky form be because I am new to Python and the second column has different number of MSC codes in each cell so I cannot use indexing there.</p> <p>I know for the first column I can use indexing:</p> <pre><code>df['MSC Primary'] = [x[:2] for x in df['MSC Primary']] </code></pre> <p>But this is not working for the other column, because there are several secondary MSC codes, different for each cell.</p> <p>Thank you for your help, much appreciated.</p>
68,347,856
"2021-07-12T12:45:09.190000"
2
null
1
63
python|pandas
<p>Here are a couple of options:</p> <p>Using a regex to get the first two chars of each &quot;word&quot;:</p> <pre class="lang-py prettyprint-override"><code>df[&quot;MSC Secondary&quot;] = ( df[&quot;MSC Secondary&quot;] .str.extractall(r&quot;[ \(](\w{2})&quot;)[0] .map(d) .groupby(level=0).agg(list) ) </code></pre> <p>Using:</p> <ul> <li><a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.extractall.html" rel="nofollow noreferrer"><code>str.extractall</code></a> apply the regex <a href="https://regex101.com/r/WDqTbw/2/" rel="nofollow noreferrer"><code>[ \(](\w{2})</code></a> to get the first two characters from all words in each row</li> <li><a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.map.html" rel="nofollow noreferrer"><code>map</code></a> map the dict, <code>d</code> over the <code>[0]</code> (zero-eth) match group</li> <li><a href="https://pandas.pydata.org/docs/reference/api/pandas.Series.groupby.html" rel="nofollow noreferrer"><code>groupby(level=0).agg(list)</code></a> to group the Series by index (<code>level=0</code>) and put them back into lists (<code>.agg(list)</code>)</li> </ul> <hr /> <p>Through a few chained pandas str methods:</p> <pre class="lang-py prettyprint-override"><code>d = dict(...) df[&quot;MSC Secondary&quot;] = ( df[&quot;MSC Secondary&quot;] .str.strip(&quot;()&quot;) .str.split() .explode() .str[:2] .map(d) .groupby(level=0) .agg(list) ) # MSC Primary MSC Secondary # 0 05C25 [Combinatorics, Group theory and generalizations] # 1 20-04 [Group theory and generalizations, Statistics] # 2 13F20 [Nonassociative rings and algebras, Topologica... # 3 05Exx [Group theory and generalizations, Group theor... # 4 20G40 [Combinatorics] </code></pre> <p>Here we use:</p> <ul> <li><a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.strip.html" rel="nofollow noreferrer"><code>pandas.Series.str.strip</code></a> to remove the parentheses</li> <li><a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.split.html" rel="nofollow noreferrer"><code>pandas.Series.str.split</code></a> to split the substrings into lists</li> <li><a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.explode.html" rel="nofollow noreferrer"><code>pandas.Series.explode</code></a> to turn every element in each list into it's own row</li> <li><a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.slice.html" rel="nofollow noreferrer"><code>str[:2]</code></a> to slice of the first two characters</li> <li><a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.map.html" rel="nofollow noreferrer"><code>map</code></a> to map your linked dict</li> <li><a href="https://pandas.pydata.org/docs/reference/api/pandas.Series.groupby.html" rel="nofollow noreferrer"><code>grouby(level=0).agg(list)</code></a> to group the Series by index (<code>level=0</code>) and put them back into lists (<code>.agg(list)</code>)</li> </ul>
"2021-07-12T13:03:35.243000"
1
https://pandas.pydata.org/docs/reference/api/pandas.Series.replace.html
Here are a couple of options: Using a regex to get the first two chars of each "word": df["MSC Secondary"] = ( df["MSC Secondary"] .str.extractall(r"[ \(](\w{2})")[0] .map(d) .groupby(level=0).agg(list) ) Using: str.extractall apply the regex [ \(](\w{2}) to get the first two characters from all words in each row map map the dict, d over the [0] (zero-eth) match group groupby(level=0).agg(list) to group the Series by index (level=0) and put them back into lists (.agg(list)) Through a few chained pandas str methods: d = dict(...) df["MSC Secondary"] = ( df["MSC Secondary"] .str.strip("()") .str.split() .explode() .str[:2] .map(d) .groupby(level=0) .agg(list) ) # MSC Primary MSC Secondary # 0 05C25 [Combinatorics, Group theory and generalizations] # 1 20-04 [Group theory and generalizations, Statistics] # 2 13F20 [Nonassociative rings and algebras, Topologica... # 3 05Exx [Group theory and generalizations, Group theor... # 4 20G40 [Combinatorics] Here we use: pandas.Series.str.strip to remove the parentheses pandas.Series.str.split to split the substrings into lists pandas.Series.explode to turn every element in each list into it's own row str[:2] to slice of the first two characters map to map your linked dict grouby(level=0).agg(list) to group the Series by index (level=0) and put them back into lists (.agg(list))
0
1,501
Extract specific symbols from pandas cells, then replace them with values from a dict where they are keys My data looks like this, well these are two of the columns: Index MSC Primary MSC Secondary 0 05C25 (05C20 20F05) 1 20-04 (20F55 62Qxx) 2 13F20 (17B20 22E40 22F30 65Fxx) 3 05Exx (20-04 20H20) 4 20G40 (05C25) These are MSC codes, corresponding to different areas of science. I need to replace each code with the corresponding subject from this dict here: https://mathscinet.ams.org/msnhtml/msc2020.pdf , some of them are: """ 00 General and overarching topics; collections 01 History and biography 03 Mathematical logic and foundations 05 Combinatorics """ First I need to isolate the first two digits from each code, for instance 05C25 to transform to 05 or from the second column (05E15 14H50) to transform to 05, 14. Then I need each number replaced by the corresponding science for example 05, 14 to become Combinatorics, Algebraic geometry. This is all tricky form be because I am new to Python and the second column has different number of MSC codes in each cell so I cannot use indexing there. I know for the first column I can use indexing: df['MSC Primary'] = [x[:2] for x in df['MSC Primary']] But this is not working for the other column, because there are several secondary MSC codes, different for each cell. Thank you for your help, much appreciated.
Extract specific symbols from pandas cells, then replace them with values from a dict where they are keys
Here are a couple of options: Using a regex to get the first two chars of each "word": df["MSC Secondary"] = ( df["MSC Secondary"] .str.extractall(r"[ \(](\w{2})")[0] .map(d) .groupby(level=0).agg(list) ) Using: str.extractall apply the regex [ \(](\w{2}) to get the first two characters from all words in each row map map the dict, d over the [0] (zero-eth) match group groupby(level=0).agg(list) to group the Series by index (level=0) and put them back into lists (.agg(list)) Through a few chained pandas str methods: d = dict(...) df["MSC Secondary"] = ( df["MSC Secondary"] .str.strip("()") .str.split() .explode() .str[:2] .map(d) .groupby(level=0) .agg(list) ) # MSC Primary MSC Secondary # 0 05C25 [Combinatorics, Group theory and generalizations] # 1 20-04 [Group theory and generalizations, Statistics] # 2 13F20 [Nonassociative rings and algebras, Topologica... # 3 05Exx [Group theory and generalizations, Group theor... # 4 20G40 [Combinatorics] Here we use: pandas.Series.str.strip to remove the parentheses pandas.Series.str.split to split the substrings into lists pandas.Series.explode to turn every element in each list into it's own row str[:2] to slice of the first two characters map to map your linked dict grouby(level=0).agg(list) to group the Series by index (level=0) and put them back into lists (.agg(list))
69,419,086
DataFrame dtypes to list of dicts
<p>I have a very wide dataframe, and I need the dtypes of all the columns as a list of dicts:</p> <pre><code>[{'name': 'col1', 'type': 'STRING'},...] </code></pre> <p>I need to do that to supply this as the schema for a BigQuery table.</p> <p>How could that be achieved?</p>
69,419,149
"2021-10-02T18:18:43.793000"
1
null
0
76
python|pandas
<p>Use a comprehension:</p> <pre><code>out = [{'name': col, 'type': dtype.name} for col, dtype in df.dtypes.items()] print(out) # Output: [{'name': 'col1', 'type': 'float64'}, {'name': 'col2', 'type': 'float64'}] </code></pre>
"2021-10-02T18:28:36.517000"
1
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_dict.html
pandas.DataFrame.to_dict# pandas.DataFrame.to_dict# DataFrame.to_dict(orient='dict', into=<class 'dict'>)[source]# Convert the DataFrame to a dictionary. The type of the key-value pairs can be customized with the parameters (see below). Parameters orientstr {‘dict’, ‘list’, ‘series’, ‘split’, ‘tight’, ‘records’, ‘index’}Determines the type of the values of the dictionary. ‘dict’ (default) : dict like {column -> {index -> value}} ‘list’ : dict like {column -> [values]} ‘series’ : dict like {column -> Series(values)} ‘split’ : dict like {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values]} ‘tight’ : dict like {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values], Use a comprehension: out = [{'name': col, 'type': dtype.name} for col, dtype in df.dtypes.items()] print(out) # Output: [{'name': 'col1', 'type': 'float64'}, {'name': 'col2', 'type': 'float64'}] ‘index_names’ -> [index.names], ‘column_names’ -> [column.names]} ‘records’ : list like [{column -> value}, … , {column -> value}] ‘index’ : dict like {index -> {column -> value}} Abbreviations are allowed. s indicates series and sp indicates split. New in version 1.4.0: ‘tight’ as an allowed value for the orient argument intoclass, default dictThe collections.abc.Mapping subclass used for all Mappings in the return value. Can be the actual class or an empty instance of the mapping type you want. If you want a collections.defaultdict, you must pass it initialized. Returns dict, list or collections.abc.MappingReturn a collections.abc.Mapping object representing the DataFrame. The resulting transformation depends on the orient parameter. See also DataFrame.from_dictCreate a DataFrame from a dictionary. DataFrame.to_jsonConvert a DataFrame to JSON format. Examples >>> df = pd.DataFrame({'col1': [1, 2], ... 'col2': [0.5, 0.75]}, ... index=['row1', 'row2']) >>> df col1 col2 row1 1 0.50 row2 2 0.75 >>> df.to_dict() {'col1': {'row1': 1, 'row2': 2}, 'col2': {'row1': 0.5, 'row2': 0.75}} You can specify the return orientation. >>> df.to_dict('series') {'col1': row1 1 row2 2 Name: col1, dtype: int64, 'col2': row1 0.50 row2 0.75 Name: col2, dtype: float64} >>> df.to_dict('split') {'index': ['row1', 'row2'], 'columns': ['col1', 'col2'], 'data': [[1, 0.5], [2, 0.75]]} >>> df.to_dict('records') [{'col1': 1, 'col2': 0.5}, {'col1': 2, 'col2': 0.75}] >>> df.to_dict('index') {'row1': {'col1': 1, 'col2': 0.5}, 'row2': {'col1': 2, 'col2': 0.75}} >>> df.to_dict('tight') {'index': ['row1', 'row2'], 'columns': ['col1', 'col2'], 'data': [[1, 0.5], [2, 0.75]], 'index_names': [None], 'column_names': [None]} You can also specify the mapping type. >>> from collections import OrderedDict, defaultdict >>> df.to_dict(into=OrderedDict) OrderedDict([('col1', OrderedDict([('row1', 1), ('row2', 2)])), ('col2', OrderedDict([('row1', 0.5), ('row2', 0.75)]))]) If you want a defaultdict, you need to initialize it: >>> dd = defaultdict(list) >>> df.to_dict('records', into=dd) [defaultdict(<class 'list'>, {'col1': 1, 'col2': 0.5}), defaultdict(<class 'list'>, {'col1': 2, 'col2': 0.75})]
698
894
DataFrame dtypes to list of dicts I have a very wide dataframe, and I need the dtypes of all the columns as a list of dicts: [{'name': 'col1', 'type': 'STRING'},...] I need to do that to supply this as the schema for a BigQuery table. How could that be achieved?
Use a comprehension: out = [{'name': col, 'type': dtype.name} for col, dtype in df.dtypes.items()] print(out) # Output: [{'name': 'col1', 'type': 'float64'}, {'name': 'col2', 'type': 'float64'}]
64,611,332
Is there an efficient way to retrieve values from dictionary
<p>I have a dictionary of values. This is for a company name.</p> <p>It has 3 keys:</p> <blockquote> <pre><code>{'html_attributions': [], 'result' : {'Address': '123 Street', 'website' :'123street.com' 'status': 'Ok' } </code></pre> </blockquote> <p>I have a dataframe of many dictionaries. I want to loop through each row's dictionary and get the necessary information I want.</p> <p>Currently I am writing for loops to retrieve these information. Is there a more efficient way to retrieve these information?</p> <pre><code>addresses = [] for i in range(len(testing)): try: addresses.append(testing['Results_dict'][i]['result']['Address']) except: addresses.append('No info') </code></pre> <p>What I have works perfectly fine. However I would like something that would be more efficient. Perhaps using the get() method? but I don't know how I can call to get the inside of 'result'.</p>
64,611,789
"2020-10-30T15:31:09.523000"
3
null
1
86
python|pandas
<p>Try this:</p> <pre><code>def get_address(r): try: return r['result']['Address'] except Exception: return 'No info' addresses = df['Results_dict'].map(get_address) </code></pre> <p>This guards against cases where <code>Result_dict</code> is None, not a dict, or any key along the path way does not exist.</p>
"2020-10-30T15:59:56.333000"
1
https://pandas.pydata.org/docs/user_guide/io.html
IO tools (text, CSV, HDF5, …)# IO tools (text, CSV, HDF5, …)# The pandas I/O API is a set of top level reader functions accessed like pandas.read_csv() that generally return a pandas object. The corresponding writer functions are object methods that are accessed like DataFrame.to_csv(). Below is a table containing available readers and writers. Format Type Data Description Reader Writer text CSV read_csv to_csv text Fixed-Width Text File read_fwf text JSON read_json to_json text HTML read_html to_html text LaTeX Styler.to_latex text XML read_xml to_xml text Local clipboard read_clipboard to_clipboard binary MS Excel read_excel to_excel binary OpenDocument read_excel binary HDF5 Format read_hdf to_hdf binary Feather Format read_feather to_feather binary Parquet Format Try this: def get_address(r): try: return r['result']['Address'] except Exception: return 'No info' addresses = df['Results_dict'].map(get_address) This guards against cases where Result_dict is None, not a dict, or any key along the path way does not exist. read_parquet to_parquet binary ORC Format read_orc to_orc binary Stata read_stata to_stata binary SAS read_sas binary SPSS read_spss binary Python Pickle Format read_pickle to_pickle SQL SQL read_sql to_sql SQL Google BigQuery read_gbq to_gbq Here is an informal performance comparison for some of these IO methods. Note For examples that use the StringIO class, make sure you import it with from io import StringIO for Python 3. CSV & text files# The workhorse function for reading text files (a.k.a. flat files) is read_csv(). See the cookbook for some advanced strategies. Parsing options# read_csv() accepts the following common arguments: Basic# filepath_or_buffervariousEither a path to a file (a str, pathlib.Path, or py:py._path.local.LocalPath), URL (including http, ftp, and S3 locations), or any object with a read() method (such as an open file or StringIO). sepstr, defaults to ',' for read_csv(), \t for read_table()Delimiter to use. If sep is None, the C engine cannot automatically detect the separator, but the Python parsing engine can, meaning the latter will be used and automatically detect the separator by Python’s builtin sniffer tool, csv.Sniffer. In addition, separators longer than 1 character and different from '\s+' will be interpreted as regular expressions and will also force the use of the Python parsing engine. Note that regex delimiters are prone to ignoring quoted data. Regex example: '\\r\\t'. delimiterstr, default NoneAlternative argument name for sep. delim_whitespaceboolean, default FalseSpecifies whether or not whitespace (e.g. ' ' or '\t') will be used as the delimiter. Equivalent to setting sep='\s+'. If this option is set to True, nothing should be passed in for the delimiter parameter. Column and index locations and names# headerint or list of ints, default 'infer'Row number(s) to use as the column names, and the start of the data. Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first line of the file, if column names are passed explicitly then the behavior is identical to header=None. Explicitly pass header=0 to be able to replace existing names. The header can be a list of ints that specify row locations for a MultiIndex on the columns e.g. [0,1,3]. Intervening rows that are not specified will be skipped (e.g. 2 in this example is skipped). Note that this parameter ignores commented lines and empty lines if skip_blank_lines=True, so header=0 denotes the first line of data rather than the first line of the file. namesarray-like, default NoneList of column names to use. If file contains no header row, then you should explicitly pass header=None. Duplicates in this list are not allowed. index_colint, str, sequence of int / str, or False, optional, default NoneColumn(s) to use as the row labels of the DataFrame, either given as string name or column index. If a sequence of int / str is given, a MultiIndex is used. Note index_col=False can be used to force pandas to not use the first column as the index, e.g. when you have a malformed file with delimiters at the end of each line. The default value of None instructs pandas to guess. If the number of fields in the column header row is equal to the number of fields in the body of the data file, then a default index is used. If it is larger, then the first columns are used as index so that the remaining number of fields in the body are equal to the number of fields in the header. The first row after the header is used to determine the number of columns, which will go into the index. If the subsequent rows contain less columns than the first row, they are filled with NaN. This can be avoided through usecols. This ensures that the columns are taken as is and the trailing data are ignored. usecolslist-like or callable, default NoneReturn a subset of the columns. If list-like, all elements must either be positional (i.e. integer indices into the document columns) or strings that correspond to column names provided either by the user in names or inferred from the document header row(s). If names are given, the document header row(s) are not taken into account. For example, a valid list-like usecols parameter would be [0, 1, 2] or ['foo', 'bar', 'baz']. Element order is ignored, so usecols=[0, 1] is the same as [1, 0]. To instantiate a DataFrame from data with element order preserved use pd.read_csv(data, usecols=['foo', 'bar'])[['foo', 'bar']] for columns in ['foo', 'bar'] order or pd.read_csv(data, usecols=['foo', 'bar'])[['bar', 'foo']] for ['bar', 'foo'] order. If callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True: In [1]: import pandas as pd In [2]: from io import StringIO In [3]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [4]: pd.read_csv(StringIO(data)) Out[4]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [5]: pd.read_csv(StringIO(data), usecols=lambda x: x.upper() in ["COL1", "COL3"]) Out[5]: col1 col3 0 a 1 1 a 2 2 c 3 Using this parameter results in much faster parsing time and lower memory usage when using the c engine. The Python engine loads the data first before deciding which columns to drop. squeezeboolean, default FalseIf the parsed data only contains one column then return a Series. Deprecated since version 1.4.0: Append .squeeze("columns") to the call to {func_name} to squeeze the data. prefixstr, default NonePrefix to add to column numbers when no header, e.g. ‘X’ for X0, X1, … Deprecated since version 1.4.0: Use a list comprehension on the DataFrame’s columns after calling read_csv. In [6]: data = "col1,col2,col3\na,b,1" In [7]: df = pd.read_csv(StringIO(data)) In [8]: df.columns = [f"pre_{col}" for col in df.columns] In [9]: df Out[9]: pre_col1 pre_col2 pre_col3 0 a b 1 mangle_dupe_colsboolean, default TrueDuplicate columns will be specified as ‘X’, ‘X.1’…’X.N’, rather than ‘X’…’X’. Passing in False will cause data to be overwritten if there are duplicate names in the columns. Deprecated since version 1.5.0: The argument was never implemented, and a new argument where the renaming pattern can be specified will be added instead. General parsing configuration# dtypeType name or dict of column -> type, default NoneData type for data or columns. E.g. {'a': np.float64, 'b': np.int32, 'c': 'Int64'} Use str or object together with suitable na_values settings to preserve and not interpret dtype. If converters are specified, they will be applied INSTEAD of dtype conversion. New in version 1.5.0: Support for defaultdict was added. Specify a defaultdict as input where the default determines the dtype of the columns which are not explicitly listed. engine{'c', 'python', 'pyarrow'}Parser engine to use. The C and pyarrow engines are faster, while the python engine is currently more feature-complete. Multithreading is currently only supported by the pyarrow engine. New in version 1.4.0: The “pyarrow” engine was added as an experimental engine, and some features are unsupported, or may not work correctly, with this engine. convertersdict, default NoneDict of functions for converting values in certain columns. Keys can either be integers or column labels. true_valueslist, default NoneValues to consider as True. false_valueslist, default NoneValues to consider as False. skipinitialspaceboolean, default FalseSkip spaces after delimiter. skiprowslist-like or integer, default NoneLine numbers to skip (0-indexed) or number of lines to skip (int) at the start of the file. If callable, the callable function will be evaluated against the row indices, returning True if the row should be skipped and False otherwise: In [10]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [11]: pd.read_csv(StringIO(data)) Out[11]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [12]: pd.read_csv(StringIO(data), skiprows=lambda x: x % 2 != 0) Out[12]: col1 col2 col3 0 a b 2 skipfooterint, default 0Number of lines at bottom of file to skip (unsupported with engine=’c’). nrowsint, default NoneNumber of rows of file to read. Useful for reading pieces of large files. low_memoryboolean, default TrueInternally process the file in chunks, resulting in lower memory use while parsing, but possibly mixed type inference. To ensure no mixed types either set False, or specify the type with the dtype parameter. Note that the entire file is read into a single DataFrame regardless, use the chunksize or iterator parameter to return the data in chunks. (Only valid with C parser) memory_mapboolean, default FalseIf a filepath is provided for filepath_or_buffer, map the file object directly onto memory and access the data directly from there. Using this option can improve performance because there is no longer any I/O overhead. NA and missing data handling# na_valuesscalar, str, list-like, or dict, default NoneAdditional strings to recognize as NA/NaN. If dict passed, specific per-column NA values. See na values const below for a list of the values interpreted as NaN by default. keep_default_naboolean, default TrueWhether or not to include the default NaN values when parsing the data. Depending on whether na_values is passed in, the behavior is as follows: If keep_default_na is True, and na_values are specified, na_values is appended to the default NaN values used for parsing. If keep_default_na is True, and na_values are not specified, only the default NaN values are used for parsing. If keep_default_na is False, and na_values are specified, only the NaN values specified na_values are used for parsing. If keep_default_na is False, and na_values are not specified, no strings will be parsed as NaN. Note that if na_filter is passed in as False, the keep_default_na and na_values parameters will be ignored. na_filterboolean, default TrueDetect missing value markers (empty strings and the value of na_values). In data without any NAs, passing na_filter=False can improve the performance of reading a large file. verboseboolean, default FalseIndicate number of NA values placed in non-numeric columns. skip_blank_linesboolean, default TrueIf True, skip over blank lines rather than interpreting as NaN values. Datetime handling# parse_datesboolean or list of ints or names or list of lists or dict, default False. If True -> try parsing the index. If [1, 2, 3] -> try parsing columns 1, 2, 3 each as a separate date column. If [[1, 3]] -> combine columns 1 and 3 and parse as a single date column. If {'foo': [1, 3]} -> parse columns 1, 3 as date and call result ‘foo’. Note A fast-path exists for iso8601-formatted dates. infer_datetime_formatboolean, default FalseIf True and parse_dates is enabled for a column, attempt to infer the datetime format to speed up the processing. keep_date_colboolean, default FalseIf True and parse_dates specifies combining multiple columns then keep the original columns. date_parserfunction, default NoneFunction to use for converting a sequence of string columns to an array of datetime instances. The default uses dateutil.parser.parser to do the conversion. pandas will try to call date_parser in three different ways, advancing to the next if an exception occurs: 1) Pass one or more arrays (as defined by parse_dates) as arguments; 2) concatenate (row-wise) the string values from the columns defined by parse_dates into a single array and pass that; and 3) call date_parser once for each row using one or more strings (corresponding to the columns defined by parse_dates) as arguments. dayfirstboolean, default FalseDD/MM format dates, international and European format. cache_datesboolean, default TrueIf True, use a cache of unique, converted dates to apply the datetime conversion. May produce significant speed-up when parsing duplicate date strings, especially ones with timezone offsets. New in version 0.25.0. Iteration# iteratorboolean, default FalseReturn TextFileReader object for iteration or getting chunks with get_chunk(). chunksizeint, default NoneReturn TextFileReader object for iteration. See iterating and chunking below. Quoting, compression, and file format# compression{'infer', 'gzip', 'bz2', 'zip', 'xz', 'zstd', None, dict}, default 'infer'For on-the-fly decompression of on-disk data. If ‘infer’, then use gzip, bz2, zip, xz, or zstandard if filepath_or_buffer is path-like ending in ‘.gz’, ‘.bz2’, ‘.zip’, ‘.xz’, ‘.zst’, respectively, and no decompression otherwise. If using ‘zip’, the ZIP file must contain only one data file to be read in. Set to None for no decompression. Can also be a dict with key 'method' set to one of {'zip', 'gzip', 'bz2', 'zstd'} and other key-value pairs are forwarded to zipfile.ZipFile, gzip.GzipFile, bz2.BZ2File, or zstandard.ZstdDecompressor. As an example, the following could be passed for faster compression and to create a reproducible gzip archive: compression={'method': 'gzip', 'compresslevel': 1, 'mtime': 1}. Changed in version 1.1.0: dict option extended to support gzip and bz2. Changed in version 1.2.0: Previous versions forwarded dict entries for ‘gzip’ to gzip.open. thousandsstr, default NoneThousands separator. decimalstr, default '.'Character to recognize as decimal point. E.g. use ',' for European data. float_precisionstring, default NoneSpecifies which converter the C engine should use for floating-point values. The options are None for the ordinary converter, high for the high-precision converter, and round_trip for the round-trip converter. lineterminatorstr (length 1), default NoneCharacter to break file into lines. Only valid with C parser. quotecharstr (length 1)The character used to denote the start and end of a quoted item. Quoted items can include the delimiter and it will be ignored. quotingint or csv.QUOTE_* instance, default 0Control field quoting behavior per csv.QUOTE_* constants. Use one of QUOTE_MINIMAL (0), QUOTE_ALL (1), QUOTE_NONNUMERIC (2) or QUOTE_NONE (3). doublequoteboolean, default TrueWhen quotechar is specified and quoting is not QUOTE_NONE, indicate whether or not to interpret two consecutive quotechar elements inside a field as a single quotechar element. escapecharstr (length 1), default NoneOne-character string used to escape delimiter when quoting is QUOTE_NONE. commentstr, default NoneIndicates remainder of line should not be parsed. If found at the beginning of a line, the line will be ignored altogether. This parameter must be a single character. Like empty lines (as long as skip_blank_lines=True), fully commented lines are ignored by the parameter header but not by skiprows. For example, if comment='#', parsing ‘#empty\na,b,c\n1,2,3’ with header=0 will result in ‘a,b,c’ being treated as the header. encodingstr, default NoneEncoding to use for UTF when reading/writing (e.g. 'utf-8'). List of Python standard encodings. dialectstr or csv.Dialect instance, default NoneIf provided, this parameter will override values (default or not) for the following parameters: delimiter, doublequote, escapechar, skipinitialspace, quotechar, and quoting. If it is necessary to override values, a ParserWarning will be issued. See csv.Dialect documentation for more details. Error handling# error_bad_linesboolean, optional, default NoneLines with too many fields (e.g. a csv line with too many commas) will by default cause an exception to be raised, and no DataFrame will be returned. If False, then these “bad lines” will dropped from the DataFrame that is returned. See bad lines below. Deprecated since version 1.3.0: The on_bad_lines parameter should be used instead to specify behavior upon encountering a bad line instead. warn_bad_linesboolean, optional, default NoneIf error_bad_lines is False, and warn_bad_lines is True, a warning for each “bad line” will be output. Deprecated since version 1.3.0: The on_bad_lines parameter should be used instead to specify behavior upon encountering a bad line instead. on_bad_lines(‘error’, ‘warn’, ‘skip’), default ‘error’Specifies what to do upon encountering a bad line (a line with too many fields). Allowed values are : ‘error’, raise an ParserError when a bad line is encountered. ‘warn’, print a warning when a bad line is encountered and skip that line. ‘skip’, skip bad lines without raising or warning when they are encountered. New in version 1.3.0. Specifying column data types# You can indicate the data type for the whole DataFrame or individual columns: In [13]: import numpy as np In [14]: data = "a,b,c,d\n1,2,3,4\n5,6,7,8\n9,10,11" In [15]: print(data) a,b,c,d 1,2,3,4 5,6,7,8 9,10,11 In [16]: df = pd.read_csv(StringIO(data), dtype=object) In [17]: df Out[17]: a b c d 0 1 2 3 4 1 5 6 7 8 2 9 10 11 NaN In [18]: df["a"][0] Out[18]: '1' In [19]: df = pd.read_csv(StringIO(data), dtype={"b": object, "c": np.float64, "d": "Int64"}) In [20]: df.dtypes Out[20]: a int64 b object c float64 d Int64 dtype: object Fortunately, pandas offers more than one way to ensure that your column(s) contain only one dtype. If you’re unfamiliar with these concepts, you can see here to learn more about dtypes, and here to learn more about object conversion in pandas. For instance, you can use the converters argument of read_csv(): In [21]: data = "col_1\n1\n2\n'A'\n4.22" In [22]: df = pd.read_csv(StringIO(data), converters={"col_1": str}) In [23]: df Out[23]: col_1 0 1 1 2 2 'A' 3 4.22 In [24]: df["col_1"].apply(type).value_counts() Out[24]: <class 'str'> 4 Name: col_1, dtype: int64 Or you can use the to_numeric() function to coerce the dtypes after reading in the data, In [25]: df2 = pd.read_csv(StringIO(data)) In [26]: df2["col_1"] = pd.to_numeric(df2["col_1"], errors="coerce") In [27]: df2 Out[27]: col_1 0 1.00 1 2.00 2 NaN 3 4.22 In [28]: df2["col_1"].apply(type).value_counts() Out[28]: <class 'float'> 4 Name: col_1, dtype: int64 which will convert all valid parsing to floats, leaving the invalid parsing as NaN. Ultimately, how you deal with reading in columns containing mixed dtypes depends on your specific needs. In the case above, if you wanted to NaN out the data anomalies, then to_numeric() is probably your best option. However, if you wanted for all the data to be coerced, no matter the type, then using the converters argument of read_csv() would certainly be worth trying. Note In some cases, reading in abnormal data with columns containing mixed dtypes will result in an inconsistent dataset. If you rely on pandas to infer the dtypes of your columns, the parsing engine will go and infer the dtypes for different chunks of the data, rather than the whole dataset at once. Consequently, you can end up with column(s) with mixed dtypes. For example, In [29]: col_1 = list(range(500000)) + ["a", "b"] + list(range(500000)) In [30]: df = pd.DataFrame({"col_1": col_1}) In [31]: df.to_csv("foo.csv") In [32]: mixed_df = pd.read_csv("foo.csv") In [33]: mixed_df["col_1"].apply(type).value_counts() Out[33]: <class 'int'> 737858 <class 'str'> 262144 Name: col_1, dtype: int64 In [34]: mixed_df["col_1"].dtype Out[34]: dtype('O') will result with mixed_df containing an int dtype for certain chunks of the column, and str for others due to the mixed dtypes from the data that was read in. It is important to note that the overall column will be marked with a dtype of object, which is used for columns with mixed dtypes. Specifying categorical dtype# Categorical columns can be parsed directly by specifying dtype='category' or dtype=CategoricalDtype(categories, ordered). In [35]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [36]: pd.read_csv(StringIO(data)) Out[36]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [37]: pd.read_csv(StringIO(data)).dtypes Out[37]: col1 object col2 object col3 int64 dtype: object In [38]: pd.read_csv(StringIO(data), dtype="category").dtypes Out[38]: col1 category col2 category col3 category dtype: object Individual columns can be parsed as a Categorical using a dict specification: In [39]: pd.read_csv(StringIO(data), dtype={"col1": "category"}).dtypes Out[39]: col1 category col2 object col3 int64 dtype: object Specifying dtype='category' will result in an unordered Categorical whose categories are the unique values observed in the data. For more control on the categories and order, create a CategoricalDtype ahead of time, and pass that for that column’s dtype. In [40]: from pandas.api.types import CategoricalDtype In [41]: dtype = CategoricalDtype(["d", "c", "b", "a"], ordered=True) In [42]: pd.read_csv(StringIO(data), dtype={"col1": dtype}).dtypes Out[42]: col1 category col2 object col3 int64 dtype: object When using dtype=CategoricalDtype, “unexpected” values outside of dtype.categories are treated as missing values. In [43]: dtype = CategoricalDtype(["a", "b", "d"]) # No 'c' In [44]: pd.read_csv(StringIO(data), dtype={"col1": dtype}).col1 Out[44]: 0 a 1 a 2 NaN Name: col1, dtype: category Categories (3, object): ['a', 'b', 'd'] This matches the behavior of Categorical.set_categories(). Note With dtype='category', the resulting categories will always be parsed as strings (object dtype). If the categories are numeric they can be converted using the to_numeric() function, or as appropriate, another converter such as to_datetime(). When dtype is a CategoricalDtype with homogeneous categories ( all numeric, all datetimes, etc.), the conversion is done automatically. In [45]: df = pd.read_csv(StringIO(data), dtype="category") In [46]: df.dtypes Out[46]: col1 category col2 category col3 category dtype: object In [47]: df["col3"] Out[47]: 0 1 1 2 2 3 Name: col3, dtype: category Categories (3, object): ['1', '2', '3'] In [48]: new_categories = pd.to_numeric(df["col3"].cat.categories) In [49]: df["col3"] = df["col3"].cat.rename_categories(new_categories) In [50]: df["col3"] Out[50]: 0 1 1 2 2 3 Name: col3, dtype: category Categories (3, int64): [1, 2, 3] Naming and using columns# Handling column names# A file may or may not have a header row. pandas assumes the first row should be used as the column names: In [51]: data = "a,b,c\n1,2,3\n4,5,6\n7,8,9" In [52]: print(data) a,b,c 1,2,3 4,5,6 7,8,9 In [53]: pd.read_csv(StringIO(data)) Out[53]: a b c 0 1 2 3 1 4 5 6 2 7 8 9 By specifying the names argument in conjunction with header you can indicate other names to use and whether or not to throw away the header row (if any): In [54]: print(data) a,b,c 1,2,3 4,5,6 7,8,9 In [55]: pd.read_csv(StringIO(data), names=["foo", "bar", "baz"], header=0) Out[55]: foo bar baz 0 1 2 3 1 4 5 6 2 7 8 9 In [56]: pd.read_csv(StringIO(data), names=["foo", "bar", "baz"], header=None) Out[56]: foo bar baz 0 a b c 1 1 2 3 2 4 5 6 3 7 8 9 If the header is in a row other than the first, pass the row number to header. This will skip the preceding rows: In [57]: data = "skip this skip it\na,b,c\n1,2,3\n4,5,6\n7,8,9" In [58]: pd.read_csv(StringIO(data), header=1) Out[58]: a b c 0 1 2 3 1 4 5 6 2 7 8 9 Note Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first non-blank line of the file, if column names are passed explicitly then the behavior is identical to header=None. Duplicate names parsing# Deprecated since version 1.5.0: mangle_dupe_cols was never implemented, and a new argument where the renaming pattern can be specified will be added instead. If the file or header contains duplicate names, pandas will by default distinguish between them so as to prevent overwriting data: In [59]: data = "a,b,a\n0,1,2\n3,4,5" In [60]: pd.read_csv(StringIO(data)) Out[60]: a b a.1 0 0 1 2 1 3 4 5 There is no more duplicate data because mangle_dupe_cols=True by default, which modifies a series of duplicate columns ‘X’, …, ‘X’ to become ‘X’, ‘X.1’, …, ‘X.N’. Filtering columns (usecols)# The usecols argument allows you to select any subset of the columns in a file, either using the column names, position numbers or a callable: In [61]: data = "a,b,c,d\n1,2,3,foo\n4,5,6,bar\n7,8,9,baz" In [62]: pd.read_csv(StringIO(data)) Out[62]: a b c d 0 1 2 3 foo 1 4 5 6 bar 2 7 8 9 baz In [63]: pd.read_csv(StringIO(data), usecols=["b", "d"]) Out[63]: b d 0 2 foo 1 5 bar 2 8 baz In [64]: pd.read_csv(StringIO(data), usecols=[0, 2, 3]) Out[64]: a c d 0 1 3 foo 1 4 6 bar 2 7 9 baz In [65]: pd.read_csv(StringIO(data), usecols=lambda x: x.upper() in ["A", "C"]) Out[65]: a c 0 1 3 1 4 6 2 7 9 The usecols argument can also be used to specify which columns not to use in the final result: In [66]: pd.read_csv(StringIO(data), usecols=lambda x: x not in ["a", "c"]) Out[66]: b d 0 2 foo 1 5 bar 2 8 baz In this case, the callable is specifying that we exclude the “a” and “c” columns from the output. Comments and empty lines# Ignoring line comments and empty lines# If the comment parameter is specified, then completely commented lines will be ignored. By default, completely blank lines will be ignored as well. In [67]: data = "\na,b,c\n \n# commented line\n1,2,3\n\n4,5,6" In [68]: print(data) a,b,c # commented line 1,2,3 4,5,6 In [69]: pd.read_csv(StringIO(data), comment="#") Out[69]: a b c 0 1 2 3 1 4 5 6 If skip_blank_lines=False, then read_csv will not ignore blank lines: In [70]: data = "a,b,c\n\n1,2,3\n\n\n4,5,6" In [71]: pd.read_csv(StringIO(data), skip_blank_lines=False) Out[71]: a b c 0 NaN NaN NaN 1 1.0 2.0 3.0 2 NaN NaN NaN 3 NaN NaN NaN 4 4.0 5.0 6.0 Warning The presence of ignored lines might create ambiguities involving line numbers; the parameter header uses row numbers (ignoring commented/empty lines), while skiprows uses line numbers (including commented/empty lines): In [72]: data = "#comment\na,b,c\nA,B,C\n1,2,3" In [73]: pd.read_csv(StringIO(data), comment="#", header=1) Out[73]: A B C 0 1 2 3 In [74]: data = "A,B,C\n#comment\na,b,c\n1,2,3" In [75]: pd.read_csv(StringIO(data), comment="#", skiprows=2) Out[75]: a b c 0 1 2 3 If both header and skiprows are specified, header will be relative to the end of skiprows. For example: In [76]: data = ( ....: "# empty\n" ....: "# second empty line\n" ....: "# third emptyline\n" ....: "X,Y,Z\n" ....: "1,2,3\n" ....: "A,B,C\n" ....: "1,2.,4.\n" ....: "5.,NaN,10.0\n" ....: ) ....: In [77]: print(data) # empty # second empty line # third emptyline X,Y,Z 1,2,3 A,B,C 1,2.,4. 5.,NaN,10.0 In [78]: pd.read_csv(StringIO(data), comment="#", skiprows=4, header=1) Out[78]: A B C 0 1.0 2.0 4.0 1 5.0 NaN 10.0 Comments# Sometimes comments or meta data may be included in a file: In [79]: print(open("tmp.csv").read()) ID,level,category Patient1,123000,x # really unpleasant Patient2,23000,y # wouldn't take his medicine Patient3,1234018,z # awesome By default, the parser includes the comments in the output: In [80]: df = pd.read_csv("tmp.csv") In [81]: df Out[81]: ID level category 0 Patient1 123000 x # really unpleasant 1 Patient2 23000 y # wouldn't take his medicine 2 Patient3 1234018 z # awesome We can suppress the comments using the comment keyword: In [82]: df = pd.read_csv("tmp.csv", comment="#") In [83]: df Out[83]: ID level category 0 Patient1 123000 x 1 Patient2 23000 y 2 Patient3 1234018 z Dealing with Unicode data# The encoding argument should be used for encoded unicode data, which will result in byte strings being decoded to unicode in the result: In [84]: from io import BytesIO In [85]: data = b"word,length\n" b"Tr\xc3\xa4umen,7\n" b"Gr\xc3\xbc\xc3\x9fe,5" In [86]: data = data.decode("utf8").encode("latin-1") In [87]: df = pd.read_csv(BytesIO(data), encoding="latin-1") In [88]: df Out[88]: word length 0 Träumen 7 1 Grüße 5 In [89]: df["word"][1] Out[89]: 'Grüße' Some formats which encode all characters as multiple bytes, like UTF-16, won’t parse correctly at all without specifying the encoding. Full list of Python standard encodings. Index columns and trailing delimiters# If a file has one more column of data than the number of column names, the first column will be used as the DataFrame’s row names: In [90]: data = "a,b,c\n4,apple,bat,5.7\n8,orange,cow,10" In [91]: pd.read_csv(StringIO(data)) Out[91]: a b c 4 apple bat 5.7 8 orange cow 10.0 In [92]: data = "index,a,b,c\n4,apple,bat,5.7\n8,orange,cow,10" In [93]: pd.read_csv(StringIO(data), index_col=0) Out[93]: a b c index 4 apple bat 5.7 8 orange cow 10.0 Ordinarily, you can achieve this behavior using the index_col option. There are some exception cases when a file has been prepared with delimiters at the end of each data line, confusing the parser. To explicitly disable the index column inference and discard the last column, pass index_col=False: In [94]: data = "a,b,c\n4,apple,bat,\n8,orange,cow," In [95]: print(data) a,b,c 4,apple,bat, 8,orange,cow, In [96]: pd.read_csv(StringIO(data)) Out[96]: a b c 4 apple bat NaN 8 orange cow NaN In [97]: pd.read_csv(StringIO(data), index_col=False) Out[97]: a b c 0 4 apple bat 1 8 orange cow If a subset of data is being parsed using the usecols option, the index_col specification is based on that subset, not the original data. In [98]: data = "a,b,c\n4,apple,bat,\n8,orange,cow," In [99]: print(data) a,b,c 4,apple,bat, 8,orange,cow, In [100]: pd.read_csv(StringIO(data), usecols=["b", "c"]) Out[100]: b c 4 bat NaN 8 cow NaN In [101]: pd.read_csv(StringIO(data), usecols=["b", "c"], index_col=0) Out[101]: b c 4 bat NaN 8 cow NaN Date Handling# Specifying date columns# To better facilitate working with datetime data, read_csv() uses the keyword arguments parse_dates and date_parser to allow users to specify a variety of columns and date/time formats to turn the input text data into datetime objects. The simplest case is to just pass in parse_dates=True: In [102]: with open("foo.csv", mode="w") as f: .....: f.write("date,A,B,C\n20090101,a,1,2\n20090102,b,3,4\n20090103,c,4,5") .....: # Use a column as an index, and parse it as dates. In [103]: df = pd.read_csv("foo.csv", index_col=0, parse_dates=True) In [104]: df Out[104]: A B C date 2009-01-01 a 1 2 2009-01-02 b 3 4 2009-01-03 c 4 5 # These are Python datetime objects In [105]: df.index Out[105]: DatetimeIndex(['2009-01-01', '2009-01-02', '2009-01-03'], dtype='datetime64[ns]', name='date', freq=None) It is often the case that we may want to store date and time data separately, or store various date fields separately. the parse_dates keyword can be used to specify a combination of columns to parse the dates and/or times from. You can specify a list of column lists to parse_dates, the resulting date columns will be prepended to the output (so as to not affect the existing column order) and the new column names will be the concatenation of the component column names: In [106]: data = ( .....: "KORD,19990127, 19:00:00, 18:56:00, 0.8100\n" .....: "KORD,19990127, 20:00:00, 19:56:00, 0.0100\n" .....: "KORD,19990127, 21:00:00, 20:56:00, -0.5900\n" .....: "KORD,19990127, 21:00:00, 21:18:00, -0.9900\n" .....: "KORD,19990127, 22:00:00, 21:56:00, -0.5900\n" .....: "KORD,19990127, 23:00:00, 22:56:00, -0.5900" .....: ) .....: In [107]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [108]: df = pd.read_csv("tmp.csv", header=None, parse_dates=[[1, 2], [1, 3]]) In [109]: df Out[109]: 1_2 1_3 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 By default the parser removes the component date columns, but you can choose to retain them via the keep_date_col keyword: In [110]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=[[1, 2], [1, 3]], keep_date_col=True .....: ) .....: In [111]: df Out[111]: 1_2 1_3 0 ... 2 3 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD ... 19:00:00 18:56:00 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD ... 20:00:00 19:56:00 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD ... 21:00:00 20:56:00 -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD ... 21:00:00 21:18:00 -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD ... 22:00:00 21:56:00 -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD ... 23:00:00 22:56:00 -0.59 [6 rows x 7 columns] Note that if you wish to combine multiple columns into a single date column, a nested list must be used. In other words, parse_dates=[1, 2] indicates that the second and third columns should each be parsed as separate date columns while parse_dates=[[1, 2]] means the two columns should be parsed into a single column. You can also use a dict to specify custom name columns: In [112]: date_spec = {"nominal": [1, 2], "actual": [1, 3]} In [113]: df = pd.read_csv("tmp.csv", header=None, parse_dates=date_spec) In [114]: df Out[114]: nominal actual 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 It is important to remember that if multiple text columns are to be parsed into a single date column, then a new column is prepended to the data. The index_col specification is based off of this new set of columns rather than the original data columns: In [115]: date_spec = {"nominal": [1, 2], "actual": [1, 3]} In [116]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=date_spec, index_col=0 .....: ) # index is the nominal column .....: In [117]: df Out[117]: actual 0 4 nominal 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 Note If a column or index contains an unparsable date, the entire column or index will be returned unaltered as an object data type. For non-standard datetime parsing, use to_datetime() after pd.read_csv. Note read_csv has a fast_path for parsing datetime strings in iso8601 format, e.g “2000-01-01T00:01:02+00:00” and similar variations. If you can arrange for your data to store datetimes in this format, load times will be significantly faster, ~20x has been observed. Date parsing functions# Finally, the parser allows you to specify a custom date_parser function to take full advantage of the flexibility of the date parsing API: In [118]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=date_spec, date_parser=pd.to_datetime .....: ) .....: In [119]: df Out[119]: nominal actual 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 pandas will try to call the date_parser function in three different ways. If an exception is raised, the next one is tried: date_parser is first called with one or more arrays as arguments, as defined using parse_dates (e.g., date_parser(['2013', '2013'], ['1', '2'])). If #1 fails, date_parser is called with all the columns concatenated row-wise into a single array (e.g., date_parser(['2013 1', '2013 2'])). Note that performance-wise, you should try these methods of parsing dates in order: Try to infer the format using infer_datetime_format=True (see section below). If you know the format, use pd.to_datetime(): date_parser=lambda x: pd.to_datetime(x, format=...). If you have a really non-standard format, use a custom date_parser function. For optimal performance, this should be vectorized, i.e., it should accept arrays as arguments. Parsing a CSV with mixed timezones# pandas cannot natively represent a column or index with mixed timezones. If your CSV file contains columns with a mixture of timezones, the default result will be an object-dtype column with strings, even with parse_dates. In [120]: content = """\ .....: a .....: 2000-01-01T00:00:00+05:00 .....: 2000-01-01T00:00:00+06:00""" .....: In [121]: df = pd.read_csv(StringIO(content), parse_dates=["a"]) In [122]: df["a"] Out[122]: 0 2000-01-01 00:00:00+05:00 1 2000-01-01 00:00:00+06:00 Name: a, dtype: object To parse the mixed-timezone values as a datetime column, pass a partially-applied to_datetime() with utc=True as the date_parser. In [123]: df = pd.read_csv( .....: StringIO(content), .....: parse_dates=["a"], .....: date_parser=lambda col: pd.to_datetime(col, utc=True), .....: ) .....: In [124]: df["a"] Out[124]: 0 1999-12-31 19:00:00+00:00 1 1999-12-31 18:00:00+00:00 Name: a, dtype: datetime64[ns, UTC] Inferring datetime format# If you have parse_dates enabled for some or all of your columns, and your datetime strings are all formatted the same way, you may get a large speed up by setting infer_datetime_format=True. If set, pandas will attempt to guess the format of your datetime strings, and then use a faster means of parsing the strings. 5-10x parsing speeds have been observed. pandas will fallback to the usual parsing if either the format cannot be guessed or the format that was guessed cannot properly parse the entire column of strings. So in general, infer_datetime_format should not have any negative consequences if enabled. Here are some examples of datetime strings that can be guessed (All representing December 30th, 2011 at 00:00:00): “20111230” “2011/12/30” “20111230 00:00:00” “12/30/2011 00:00:00” “30/Dec/2011 00:00:00” “30/December/2011 00:00:00” Note that infer_datetime_format is sensitive to dayfirst. With dayfirst=True, it will guess “01/12/2011” to be December 1st. With dayfirst=False (default) it will guess “01/12/2011” to be January 12th. # Try to infer the format for the index column In [125]: df = pd.read_csv( .....: "foo.csv", .....: index_col=0, .....: parse_dates=True, .....: infer_datetime_format=True, .....: ) .....: In [126]: df Out[126]: A B C date 2009-01-01 a 1 2 2009-01-02 b 3 4 2009-01-03 c 4 5 International date formats# While US date formats tend to be MM/DD/YYYY, many international formats use DD/MM/YYYY instead. For convenience, a dayfirst keyword is provided: In [127]: data = "date,value,cat\n1/6/2000,5,a\n2/6/2000,10,b\n3/6/2000,15,c" In [128]: print(data) date,value,cat 1/6/2000,5,a 2/6/2000,10,b 3/6/2000,15,c In [129]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [130]: pd.read_csv("tmp.csv", parse_dates=[0]) Out[130]: date value cat 0 2000-01-06 5 a 1 2000-02-06 10 b 2 2000-03-06 15 c In [131]: pd.read_csv("tmp.csv", dayfirst=True, parse_dates=[0]) Out[131]: date value cat 0 2000-06-01 5 a 1 2000-06-02 10 b 2 2000-06-03 15 c Writing CSVs to binary file objects# New in version 1.2.0. df.to_csv(..., mode="wb") allows writing a CSV to a file object opened binary mode. In most cases, it is not necessary to specify mode as Pandas will auto-detect whether the file object is opened in text or binary mode. In [132]: import io In [133]: data = pd.DataFrame([0, 1, 2]) In [134]: buffer = io.BytesIO() In [135]: data.to_csv(buffer, encoding="utf-8", compression="gzip") Specifying method for floating-point conversion# The parameter float_precision can be specified in order to use a specific floating-point converter during parsing with the C engine. The options are the ordinary converter, the high-precision converter, and the round-trip converter (which is guaranteed to round-trip values after writing to a file). For example: In [136]: val = "0.3066101993807095471566981359501369297504425048828125" In [137]: data = "a,b,c\n1,2,{0}".format(val) In [138]: abs( .....: pd.read_csv( .....: StringIO(data), .....: engine="c", .....: float_precision=None, .....: )["c"][0] - float(val) .....: ) .....: Out[138]: 5.551115123125783e-17 In [139]: abs( .....: pd.read_csv( .....: StringIO(data), .....: engine="c", .....: float_precision="high", .....: )["c"][0] - float(val) .....: ) .....: Out[139]: 5.551115123125783e-17 In [140]: abs( .....: pd.read_csv(StringIO(data), engine="c", float_precision="round_trip")["c"][0] .....: - float(val) .....: ) .....: Out[140]: 0.0 Thousand separators# For large numbers that have been written with a thousands separator, you can set the thousands keyword to a string of length 1 so that integers will be parsed correctly: By default, numbers with a thousands separator will be parsed as strings: In [141]: data = ( .....: "ID|level|category\n" .....: "Patient1|123,000|x\n" .....: "Patient2|23,000|y\n" .....: "Patient3|1,234,018|z" .....: ) .....: In [142]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [143]: df = pd.read_csv("tmp.csv", sep="|") In [144]: df Out[144]: ID level category 0 Patient1 123,000 x 1 Patient2 23,000 y 2 Patient3 1,234,018 z In [145]: df.level.dtype Out[145]: dtype('O') The thousands keyword allows integers to be parsed correctly: In [146]: df = pd.read_csv("tmp.csv", sep="|", thousands=",") In [147]: df Out[147]: ID level category 0 Patient1 123000 x 1 Patient2 23000 y 2 Patient3 1234018 z In [148]: df.level.dtype Out[148]: dtype('int64') NA values# To control which values are parsed as missing values (which are signified by NaN), specify a string in na_values. If you specify a list of strings, then all values in it are considered to be missing values. If you specify a number (a float, like 5.0 or an integer like 5), the corresponding equivalent values will also imply a missing value (in this case effectively [5.0, 5] are recognized as NaN). To completely override the default values that are recognized as missing, specify keep_default_na=False. The default NaN recognized values are ['-1.#IND', '1.#QNAN', '1.#IND', '-1.#QNAN', '#N/A N/A', '#N/A', 'N/A', 'n/a', 'NA', '<NA>', '#NA', 'NULL', 'null', 'NaN', '-NaN', 'nan', '-nan', '']. Let us consider some examples: pd.read_csv("path_to_file.csv", na_values=[5]) In the example above 5 and 5.0 will be recognized as NaN, in addition to the defaults. A string will first be interpreted as a numerical 5, then as a NaN. pd.read_csv("path_to_file.csv", keep_default_na=False, na_values=[""]) Above, only an empty field will be recognized as NaN. pd.read_csv("path_to_file.csv", keep_default_na=False, na_values=["NA", "0"]) Above, both NA and 0 as strings are NaN. pd.read_csv("path_to_file.csv", na_values=["Nope"]) The default values, in addition to the string "Nope" are recognized as NaN. Infinity# inf like values will be parsed as np.inf (positive infinity), and -inf as -np.inf (negative infinity). These will ignore the case of the value, meaning Inf, will also be parsed as np.inf. Returning Series# Using the squeeze keyword, the parser will return output with a single column as a Series: Deprecated since version 1.4.0: Users should append .squeeze("columns") to the DataFrame returned by read_csv instead. In [149]: data = "level\nPatient1,123000\nPatient2,23000\nPatient3,1234018" In [150]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [151]: print(open("tmp.csv").read()) level Patient1,123000 Patient2,23000 Patient3,1234018 In [152]: output = pd.read_csv("tmp.csv", squeeze=True) In [153]: output Out[153]: Patient1 123000 Patient2 23000 Patient3 1234018 Name: level, dtype: int64 In [154]: type(output) Out[154]: pandas.core.series.Series Boolean values# The common values True, False, TRUE, and FALSE are all recognized as boolean. Occasionally you might want to recognize other values as being boolean. To do this, use the true_values and false_values options as follows: In [155]: data = "a,b,c\n1,Yes,2\n3,No,4" In [156]: print(data) a,b,c 1,Yes,2 3,No,4 In [157]: pd.read_csv(StringIO(data)) Out[157]: a b c 0 1 Yes 2 1 3 No 4 In [158]: pd.read_csv(StringIO(data), true_values=["Yes"], false_values=["No"]) Out[158]: a b c 0 1 True 2 1 3 False 4 Handling “bad” lines# Some files may have malformed lines with too few fields or too many. Lines with too few fields will have NA values filled in the trailing fields. Lines with too many fields will raise an error by default: In [159]: data = "a,b,c\n1,2,3\n4,5,6,7\n8,9,10" In [160]: pd.read_csv(StringIO(data)) --------------------------------------------------------------------------- ParserError Traceback (most recent call last) Cell In[160], line 1 ----> 1 pd.read_csv(StringIO(data)) File ~/work/pandas/pandas/pandas/util/_decorators.py:211, in deprecate_kwarg.<locals>._deprecate_kwarg.<locals>.wrapper(*args, **kwargs) 209 else: 210 kwargs[new_arg_name] = new_arg_value --> 211 return func(*args, **kwargs) File ~/work/pandas/pandas/pandas/util/_decorators.py:331, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs) 325 if len(args) > num_allow_args: 326 warnings.warn( 327 msg.format(arguments=_format_argument_list(allow_args)), 328 FutureWarning, 329 stacklevel=find_stack_level(), 330 ) --> 331 return func(*args, **kwargs) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:950, in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, error_bad_lines, warn_bad_lines, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options) 935 kwds_defaults = _refine_defaults_read( 936 dialect, 937 delimiter, (...) 946 defaults={"delimiter": ","}, 947 ) 948 kwds.update(kwds_defaults) --> 950 return _read(filepath_or_buffer, kwds) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:611, in _read(filepath_or_buffer, kwds) 608 return parser 610 with parser: --> 611 return parser.read(nrows) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:1778, in TextFileReader.read(self, nrows) 1771 nrows = validate_integer("nrows", nrows) 1772 try: 1773 # error: "ParserBase" has no attribute "read" 1774 ( 1775 index, 1776 columns, 1777 col_dict, -> 1778 ) = self._engine.read( # type: ignore[attr-defined] 1779 nrows 1780 ) 1781 except Exception: 1782 self.close() File ~/work/pandas/pandas/pandas/io/parsers/c_parser_wrapper.py:230, in CParserWrapper.read(self, nrows) 228 try: 229 if self.low_memory: --> 230 chunks = self._reader.read_low_memory(nrows) 231 # destructive to chunks 232 data = _concatenate_chunks(chunks) File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:808, in pandas._libs.parsers.TextReader.read_low_memory() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:866, in pandas._libs.parsers.TextReader._read_rows() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:852, in pandas._libs.parsers.TextReader._tokenize_rows() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:1973, in pandas._libs.parsers.raise_parser_error() ParserError: Error tokenizing data. C error: Expected 3 fields in line 3, saw 4 You can elect to skip bad lines: In [29]: pd.read_csv(StringIO(data), on_bad_lines="warn") Skipping line 3: expected 3 fields, saw 4 Out[29]: a b c 0 1 2 3 1 8 9 10 Or pass a callable function to handle the bad line if engine="python". The bad line will be a list of strings that was split by the sep: In [29]: external_list = [] In [30]: def bad_lines_func(line): ...: external_list.append(line) ...: return line[-3:] In [31]: pd.read_csv(StringIO(data), on_bad_lines=bad_lines_func, engine="python") Out[31]: a b c 0 1 2 3 1 5 6 7 2 8 9 10 In [32]: external_list Out[32]: [4, 5, 6, 7] .. versionadded:: 1.4.0 You can also use the usecols parameter to eliminate extraneous column data that appear in some lines but not others: In [33]: pd.read_csv(StringIO(data), usecols=[0, 1, 2]) Out[33]: a b c 0 1 2 3 1 4 5 6 2 8 9 10 In case you want to keep all data including the lines with too many fields, you can specify a sufficient number of names. This ensures that lines with not enough fields are filled with NaN. In [34]: pd.read_csv(StringIO(data), names=['a', 'b', 'c', 'd']) Out[34]: a b c d 0 1 2 3 NaN 1 4 5 6 7 2 8 9 10 NaN Dialect# The dialect keyword gives greater flexibility in specifying the file format. By default it uses the Excel dialect but you can specify either the dialect name or a csv.Dialect instance. Suppose you had data with unenclosed quotes: In [161]: data = "label1,label2,label3\n" 'index1,"a,c,e\n' "index2,b,d,f" In [162]: print(data) label1,label2,label3 index1,"a,c,e index2,b,d,f By default, read_csv uses the Excel dialect and treats the double quote as the quote character, which causes it to fail when it finds a newline before it finds the closing double quote. We can get around this using dialect: In [163]: import csv In [164]: dia = csv.excel() In [165]: dia.quoting = csv.QUOTE_NONE In [166]: pd.read_csv(StringIO(data), dialect=dia) Out[166]: label1 label2 label3 index1 "a c e index2 b d f All of the dialect options can be specified separately by keyword arguments: In [167]: data = "a,b,c~1,2,3~4,5,6" In [168]: pd.read_csv(StringIO(data), lineterminator="~") Out[168]: a b c 0 1 2 3 1 4 5 6 Another common dialect option is skipinitialspace, to skip any whitespace after a delimiter: In [169]: data = "a, b, c\n1, 2, 3\n4, 5, 6" In [170]: print(data) a, b, c 1, 2, 3 4, 5, 6 In [171]: pd.read_csv(StringIO(data), skipinitialspace=True) Out[171]: a b c 0 1 2 3 1 4 5 6 The parsers make every attempt to “do the right thing” and not be fragile. Type inference is a pretty big deal. If a column can be coerced to integer dtype without altering the contents, the parser will do so. Any non-numeric columns will come through as object dtype as with the rest of pandas objects. Quoting and Escape Characters# Quotes (and other escape characters) in embedded fields can be handled in any number of ways. One way is to use backslashes; to properly parse this data, you should pass the escapechar option: In [172]: data = 'a,b\n"hello, \\"Bob\\", nice to see you",5' In [173]: print(data) a,b "hello, \"Bob\", nice to see you",5 In [174]: pd.read_csv(StringIO(data), escapechar="\\") Out[174]: a b 0 hello, "Bob", nice to see you 5 Files with fixed width columns# While read_csv() reads delimited data, the read_fwf() function works with data files that have known and fixed column widths. The function parameters to read_fwf are largely the same as read_csv with two extra parameters, and a different usage of the delimiter parameter: colspecs: A list of pairs (tuples) giving the extents of the fixed-width fields of each line as half-open intervals (i.e., [from, to[ ). String value ‘infer’ can be used to instruct the parser to try detecting the column specifications from the first 100 rows of the data. Default behavior, if not specified, is to infer. widths: A list of field widths which can be used instead of ‘colspecs’ if the intervals are contiguous. delimiter: Characters to consider as filler characters in the fixed-width file. Can be used to specify the filler character of the fields if it is not spaces (e.g., ‘~’). Consider a typical fixed-width data file: In [175]: data1 = ( .....: "id8141 360.242940 149.910199 11950.7\n" .....: "id1594 444.953632 166.985655 11788.4\n" .....: "id1849 364.136849 183.628767 11806.2\n" .....: "id1230 413.836124 184.375703 11916.8\n" .....: "id1948 502.953953 173.237159 12468.3" .....: ) .....: In [176]: with open("bar.csv", "w") as f: .....: f.write(data1) .....: In order to parse this file into a DataFrame, we simply need to supply the column specifications to the read_fwf function along with the file name: # Column specifications are a list of half-intervals In [177]: colspecs = [(0, 6), (8, 20), (21, 33), (34, 43)] In [178]: df = pd.read_fwf("bar.csv", colspecs=colspecs, header=None, index_col=0) In [179]: df Out[179]: 1 2 3 0 id8141 360.242940 149.910199 11950.7 id1594 444.953632 166.985655 11788.4 id1849 364.136849 183.628767 11806.2 id1230 413.836124 184.375703 11916.8 id1948 502.953953 173.237159 12468.3 Note how the parser automatically picks column names X.<column number> when header=None argument is specified. Alternatively, you can supply just the column widths for contiguous columns: # Widths are a list of integers In [180]: widths = [6, 14, 13, 10] In [181]: df = pd.read_fwf("bar.csv", widths=widths, header=None) In [182]: df Out[182]: 0 1 2 3 0 id8141 360.242940 149.910199 11950.7 1 id1594 444.953632 166.985655 11788.4 2 id1849 364.136849 183.628767 11806.2 3 id1230 413.836124 184.375703 11916.8 4 id1948 502.953953 173.237159 12468.3 The parser will take care of extra white spaces around the columns so it’s ok to have extra separation between the columns in the file. By default, read_fwf will try to infer the file’s colspecs by using the first 100 rows of the file. It can do it only in cases when the columns are aligned and correctly separated by the provided delimiter (default delimiter is whitespace). In [183]: df = pd.read_fwf("bar.csv", header=None, index_col=0) In [184]: df Out[184]: 1 2 3 0 id8141 360.242940 149.910199 11950.7 id1594 444.953632 166.985655 11788.4 id1849 364.136849 183.628767 11806.2 id1230 413.836124 184.375703 11916.8 id1948 502.953953 173.237159 12468.3 read_fwf supports the dtype parameter for specifying the types of parsed columns to be different from the inferred type. In [185]: pd.read_fwf("bar.csv", header=None, index_col=0).dtypes Out[185]: 1 float64 2 float64 3 float64 dtype: object In [186]: pd.read_fwf("bar.csv", header=None, dtype={2: "object"}).dtypes Out[186]: 0 object 1 float64 2 object 3 float64 dtype: object Indexes# Files with an “implicit” index column# Consider a file with one less entry in the header than the number of data column: In [187]: data = "A,B,C\n20090101,a,1,2\n20090102,b,3,4\n20090103,c,4,5" In [188]: print(data) A,B,C 20090101,a,1,2 20090102,b,3,4 20090103,c,4,5 In [189]: with open("foo.csv", "w") as f: .....: f.write(data) .....: In this special case, read_csv assumes that the first column is to be used as the index of the DataFrame: In [190]: pd.read_csv("foo.csv") Out[190]: A B C 20090101 a 1 2 20090102 b 3 4 20090103 c 4 5 Note that the dates weren’t automatically parsed. In that case you would need to do as before: In [191]: df = pd.read_csv("foo.csv", parse_dates=True) In [192]: df.index Out[192]: DatetimeIndex(['2009-01-01', '2009-01-02', '2009-01-03'], dtype='datetime64[ns]', freq=None) Reading an index with a MultiIndex# Suppose you have data indexed by two columns: In [193]: data = 'year,indiv,zit,xit\n1977,"A",1.2,.6\n1977,"B",1.5,.5' In [194]: print(data) year,indiv,zit,xit 1977,"A",1.2,.6 1977,"B",1.5,.5 In [195]: with open("mindex_ex.csv", mode="w") as f: .....: f.write(data) .....: The index_col argument to read_csv can take a list of column numbers to turn multiple columns into a MultiIndex for the index of the returned object: In [196]: df = pd.read_csv("mindex_ex.csv", index_col=[0, 1]) In [197]: df Out[197]: zit xit year indiv 1977 A 1.2 0.6 B 1.5 0.5 In [198]: df.loc[1977] Out[198]: zit xit indiv A 1.2 0.6 B 1.5 0.5 Reading columns with a MultiIndex# By specifying list of row locations for the header argument, you can read in a MultiIndex for the columns. Specifying non-consecutive rows will skip the intervening rows. In [199]: from pandas._testing import makeCustomDataframe as mkdf In [200]: df = mkdf(5, 3, r_idx_nlevels=2, c_idx_nlevels=4) In [201]: df.to_csv("mi.csv") In [202]: print(open("mi.csv").read()) C0,,C_l0_g0,C_l0_g1,C_l0_g2 C1,,C_l1_g0,C_l1_g1,C_l1_g2 C2,,C_l2_g0,C_l2_g1,C_l2_g2 C3,,C_l3_g0,C_l3_g1,C_l3_g2 R0,R1,,, R_l0_g0,R_l1_g0,R0C0,R0C1,R0C2 R_l0_g1,R_l1_g1,R1C0,R1C1,R1C2 R_l0_g2,R_l1_g2,R2C0,R2C1,R2C2 R_l0_g3,R_l1_g3,R3C0,R3C1,R3C2 R_l0_g4,R_l1_g4,R4C0,R4C1,R4C2 In [203]: pd.read_csv("mi.csv", header=[0, 1, 2, 3], index_col=[0, 1]) Out[203]: C0 C_l0_g0 C_l0_g1 C_l0_g2 C1 C_l1_g0 C_l1_g1 C_l1_g2 C2 C_l2_g0 C_l2_g1 C_l2_g2 C3 C_l3_g0 C_l3_g1 C_l3_g2 R0 R1 R_l0_g0 R_l1_g0 R0C0 R0C1 R0C2 R_l0_g1 R_l1_g1 R1C0 R1C1 R1C2 R_l0_g2 R_l1_g2 R2C0 R2C1 R2C2 R_l0_g3 R_l1_g3 R3C0 R3C1 R3C2 R_l0_g4 R_l1_g4 R4C0 R4C1 R4C2 read_csv is also able to interpret a more common format of multi-columns indices. In [204]: data = ",a,a,a,b,c,c\n,q,r,s,t,u,v\none,1,2,3,4,5,6\ntwo,7,8,9,10,11,12" In [205]: print(data) ,a,a,a,b,c,c ,q,r,s,t,u,v one,1,2,3,4,5,6 two,7,8,9,10,11,12 In [206]: with open("mi2.csv", "w") as fh: .....: fh.write(data) .....: In [207]: pd.read_csv("mi2.csv", header=[0, 1], index_col=0) Out[207]: a b c q r s t u v one 1 2 3 4 5 6 two 7 8 9 10 11 12 Note If an index_col is not specified (e.g. you don’t have an index, or wrote it with df.to_csv(..., index=False), then any names on the columns index will be lost. Automatically “sniffing” the delimiter# read_csv is capable of inferring delimited (not necessarily comma-separated) files, as pandas uses the csv.Sniffer class of the csv module. For this, you have to specify sep=None. In [208]: df = pd.DataFrame(np.random.randn(10, 4)) In [209]: df.to_csv("tmp.csv", sep="|") In [210]: df.to_csv("tmp2.csv", sep=":") In [211]: pd.read_csv("tmp2.csv", sep=None, engine="python") Out[211]: Unnamed: 0 0 1 2 3 0 0 0.469112 -0.282863 -1.509059 -1.135632 1 1 1.212112 -0.173215 0.119209 -1.044236 2 2 -0.861849 -2.104569 -0.494929 1.071804 3 3 0.721555 -0.706771 -1.039575 0.271860 4 4 -0.424972 0.567020 0.276232 -1.087401 5 5 -0.673690 0.113648 -1.478427 0.524988 6 6 0.404705 0.577046 -1.715002 -1.039268 7 7 -0.370647 -1.157892 -1.344312 0.844885 8 8 1.075770 -0.109050 1.643563 -1.469388 9 9 0.357021 -0.674600 -1.776904 -0.968914 Reading multiple files to create a single DataFrame# It’s best to use concat() to combine multiple files. See the cookbook for an example. Iterating through files chunk by chunk# Suppose you wish to iterate through a (potentially very large) file lazily rather than reading the entire file into memory, such as the following: In [212]: df = pd.DataFrame(np.random.randn(10, 4)) In [213]: df.to_csv("tmp.csv", sep="|") In [214]: table = pd.read_csv("tmp.csv", sep="|") In [215]: table Out[215]: Unnamed: 0 0 1 2 3 0 0 -1.294524 0.413738 0.276662 -0.472035 1 1 -0.013960 -0.362543 -0.006154 -0.923061 2 2 0.895717 0.805244 -1.206412 2.565646 3 3 1.431256 1.340309 -1.170299 -0.226169 4 4 0.410835 0.813850 0.132003 -0.827317 5 5 -0.076467 -1.187678 1.130127 -1.436737 6 6 -1.413681 1.607920 1.024180 0.569605 7 7 0.875906 -2.211372 0.974466 -2.006747 8 8 -0.410001 -0.078638 0.545952 -1.219217 9 9 -1.226825 0.769804 -1.281247 -0.727707 By specifying a chunksize to read_csv, the return value will be an iterable object of type TextFileReader: In [216]: with pd.read_csv("tmp.csv", sep="|", chunksize=4) as reader: .....: reader .....: for chunk in reader: .....: print(chunk) .....: Unnamed: 0 0 1 2 3 0 0 -1.294524 0.413738 0.276662 -0.472035 1 1 -0.013960 -0.362543 -0.006154 -0.923061 2 2 0.895717 0.805244 -1.206412 2.565646 3 3 1.431256 1.340309 -1.170299 -0.226169 Unnamed: 0 0 1 2 3 4 4 0.410835 0.813850 0.132003 -0.827317 5 5 -0.076467 -1.187678 1.130127 -1.436737 6 6 -1.413681 1.607920 1.024180 0.569605 7 7 0.875906 -2.211372 0.974466 -2.006747 Unnamed: 0 0 1 2 3 8 8 -0.410001 -0.078638 0.545952 -1.219217 9 9 -1.226825 0.769804 -1.281247 -0.727707 Changed in version 1.2: read_csv/json/sas return a context-manager when iterating through a file. Specifying iterator=True will also return the TextFileReader object: In [217]: with pd.read_csv("tmp.csv", sep="|", iterator=True) as reader: .....: reader.get_chunk(5) .....: Specifying the parser engine# Pandas currently supports three engines, the C engine, the python engine, and an experimental pyarrow engine (requires the pyarrow package). In general, the pyarrow engine is fastest on larger workloads and is equivalent in speed to the C engine on most other workloads. The python engine tends to be slower than the pyarrow and C engines on most workloads. However, the pyarrow engine is much less robust than the C engine, which lacks a few features compared to the Python engine. Where possible, pandas uses the C parser (specified as engine='c'), but it may fall back to Python if C-unsupported options are specified. Currently, options unsupported by the C and pyarrow engines include: sep other than a single character (e.g. regex separators) skipfooter sep=None with delim_whitespace=False Specifying any of the above options will produce a ParserWarning unless the python engine is selected explicitly using engine='python'. Options that are unsupported by the pyarrow engine which are not covered by the list above include: float_precision chunksize comment nrows thousands memory_map dialect warn_bad_lines error_bad_lines on_bad_lines delim_whitespace quoting lineterminator converters decimal iterator dayfirst infer_datetime_format verbose skipinitialspace low_memory Specifying these options with engine='pyarrow' will raise a ValueError. Reading/writing remote files# You can pass in a URL to read or write remote files to many of pandas’ IO functions - the following example shows reading a CSV file: df = pd.read_csv("https://download.bls.gov/pub/time.series/cu/cu.item", sep="\t") New in version 1.3.0. A custom header can be sent alongside HTTP(s) requests by passing a dictionary of header key value mappings to the storage_options keyword argument as shown below: headers = {"User-Agent": "pandas"} df = pd.read_csv( "https://download.bls.gov/pub/time.series/cu/cu.item", sep="\t", storage_options=headers ) All URLs which are not local files or HTTP(s) are handled by fsspec, if installed, and its various filesystem implementations (including Amazon S3, Google Cloud, SSH, FTP, webHDFS…). Some of these implementations will require additional packages to be installed, for example S3 URLs require the s3fs library: df = pd.read_json("s3://pandas-test/adatafile.json") When dealing with remote storage systems, you might need extra configuration with environment variables or config files in special locations. For example, to access data in your S3 bucket, you will need to define credentials in one of the several ways listed in the S3Fs documentation. The same is true for several of the storage backends, and you should follow the links at fsimpl1 for implementations built into fsspec and fsimpl2 for those not included in the main fsspec distribution. You can also pass parameters directly to the backend driver. For example, if you do not have S3 credentials, you can still access public data by specifying an anonymous connection, such as New in version 1.2.0. pd.read_csv( "s3://ncei-wcsd-archive/data/processed/SH1305/18kHz/SaKe2013" "-D20130523-T080854_to_SaKe2013-D20130523-T085643.csv", storage_options={"anon": True}, ) fsspec also allows complex URLs, for accessing data in compressed archives, local caching of files, and more. To locally cache the above example, you would modify the call to pd.read_csv( "simplecache::s3://ncei-wcsd-archive/data/processed/SH1305/18kHz/" "SaKe2013-D20130523-T080854_to_SaKe2013-D20130523-T085643.csv", storage_options={"s3": {"anon": True}}, ) where we specify that the “anon” parameter is meant for the “s3” part of the implementation, not to the caching implementation. Note that this caches to a temporary directory for the duration of the session only, but you can also specify a permanent store. Writing out data# Writing to CSV format# The Series and DataFrame objects have an instance method to_csv which allows storing the contents of the object as a comma-separated-values file. The function takes a number of arguments. Only the first is required. path_or_buf: A string path to the file to write or a file object. If a file object it must be opened with newline='' sep : Field delimiter for the output file (default “,”) na_rep: A string representation of a missing value (default ‘’) float_format: Format string for floating point numbers columns: Columns to write (default None) header: Whether to write out the column names (default True) index: whether to write row (index) names (default True) index_label: Column label(s) for index column(s) if desired. If None (default), and header and index are True, then the index names are used. (A sequence should be given if the DataFrame uses MultiIndex). mode : Python write mode, default ‘w’ encoding: a string representing the encoding to use if the contents are non-ASCII, for Python versions prior to 3 lineterminator: Character sequence denoting line end (default os.linesep) quoting: Set quoting rules as in csv module (default csv.QUOTE_MINIMAL). Note that if you have set a float_format then floats are converted to strings and csv.QUOTE_NONNUMERIC will treat them as non-numeric quotechar: Character used to quote fields (default ‘”’) doublequote: Control quoting of quotechar in fields (default True) escapechar: Character used to escape sep and quotechar when appropriate (default None) chunksize: Number of rows to write at a time date_format: Format string for datetime objects Writing a formatted string# The DataFrame object has an instance method to_string which allows control over the string representation of the object. All arguments are optional: buf default None, for example a StringIO object columns default None, which columns to write col_space default None, minimum width of each column. na_rep default NaN, representation of NA value formatters default None, a dictionary (by column) of functions each of which takes a single argument and returns a formatted string float_format default None, a function which takes a single (float) argument and returns a formatted string; to be applied to floats in the DataFrame. sparsify default True, set to False for a DataFrame with a hierarchical index to print every MultiIndex key at each row. index_names default True, will print the names of the indices index default True, will print the index (ie, row labels) header default True, will print the column labels justify default left, will print column headers left- or right-justified The Series object also has a to_string method, but with only the buf, na_rep, float_format arguments. There is also a length argument which, if set to True, will additionally output the length of the Series. JSON# Read and write JSON format files and strings. Writing JSON# A Series or DataFrame can be converted to a valid JSON string. Use to_json with optional parameters: path_or_buf : the pathname or buffer to write the output This can be None in which case a JSON string is returned orient : Series: default is index allowed values are {split, records, index} DataFrame: default is columns allowed values are {split, records, index, columns, values, table} The format of the JSON string split dict like {index -> [index], columns -> [columns], data -> [values]} records list like [{column -> value}, … , {column -> value}] index dict like {index -> {column -> value}} columns dict like {column -> {index -> value}} values just the values array table adhering to the JSON Table Schema date_format : string, type of date conversion, ‘epoch’ for timestamp, ‘iso’ for ISO8601. double_precision : The number of decimal places to use when encoding floating point values, default 10. force_ascii : force encoded string to be ASCII, default True. date_unit : The time unit to encode to, governs timestamp and ISO8601 precision. One of ‘s’, ‘ms’, ‘us’ or ‘ns’ for seconds, milliseconds, microseconds and nanoseconds respectively. Default ‘ms’. default_handler : The handler to call if an object cannot otherwise be converted to a suitable format for JSON. Takes a single argument, which is the object to convert, and returns a serializable object. lines : If records orient, then will write each record per line as json. Note NaN’s, NaT’s and None will be converted to null and datetime objects will be converted based on the date_format and date_unit parameters. In [218]: dfj = pd.DataFrame(np.random.randn(5, 2), columns=list("AB")) In [219]: json = dfj.to_json() In [220]: json Out[220]: '{"A":{"0":-0.1213062281,"1":0.6957746499,"2":0.9597255933,"3":-0.6199759194,"4":-0.7323393705},"B":{"0":-0.0978826728,"1":0.3417343559,"2":-1.1103361029,"3":0.1497483186,"4":0.6877383895}}' Orient options# There are a number of different options for the format of the resulting JSON file / string. Consider the following DataFrame and Series: In [221]: dfjo = pd.DataFrame( .....: dict(A=range(1, 4), B=range(4, 7), C=range(7, 10)), .....: columns=list("ABC"), .....: index=list("xyz"), .....: ) .....: In [222]: dfjo Out[222]: A B C x 1 4 7 y 2 5 8 z 3 6 9 In [223]: sjo = pd.Series(dict(x=15, y=16, z=17), name="D") In [224]: sjo Out[224]: x 15 y 16 z 17 Name: D, dtype: int64 Column oriented (the default for DataFrame) serializes the data as nested JSON objects with column labels acting as the primary index: In [225]: dfjo.to_json(orient="columns") Out[225]: '{"A":{"x":1,"y":2,"z":3},"B":{"x":4,"y":5,"z":6},"C":{"x":7,"y":8,"z":9}}' # Not available for Series Index oriented (the default for Series) similar to column oriented but the index labels are now primary: In [226]: dfjo.to_json(orient="index") Out[226]: '{"x":{"A":1,"B":4,"C":7},"y":{"A":2,"B":5,"C":8},"z":{"A":3,"B":6,"C":9}}' In [227]: sjo.to_json(orient="index") Out[227]: '{"x":15,"y":16,"z":17}' Record oriented serializes the data to a JSON array of column -> value records, index labels are not included. This is useful for passing DataFrame data to plotting libraries, for example the JavaScript library d3.js: In [228]: dfjo.to_json(orient="records") Out[228]: '[{"A":1,"B":4,"C":7},{"A":2,"B":5,"C":8},{"A":3,"B":6,"C":9}]' In [229]: sjo.to_json(orient="records") Out[229]: '[15,16,17]' Value oriented is a bare-bones option which serializes to nested JSON arrays of values only, column and index labels are not included: In [230]: dfjo.to_json(orient="values") Out[230]: '[[1,4,7],[2,5,8],[3,6,9]]' # Not available for Series Split oriented serializes to a JSON object containing separate entries for values, index and columns. Name is also included for Series: In [231]: dfjo.to_json(orient="split") Out[231]: '{"columns":["A","B","C"],"index":["x","y","z"],"data":[[1,4,7],[2,5,8],[3,6,9]]}' In [232]: sjo.to_json(orient="split") Out[232]: '{"name":"D","index":["x","y","z"],"data":[15,16,17]}' Table oriented serializes to the JSON Table Schema, allowing for the preservation of metadata including but not limited to dtypes and index names. Note Any orient option that encodes to a JSON object will not preserve the ordering of index and column labels during round-trip serialization. If you wish to preserve label ordering use the split option as it uses ordered containers. Date handling# Writing in ISO date format: In [233]: dfd = pd.DataFrame(np.random.randn(5, 2), columns=list("AB")) In [234]: dfd["date"] = pd.Timestamp("20130101") In [235]: dfd = dfd.sort_index(axis=1, ascending=False) In [236]: json = dfd.to_json(date_format="iso") In [237]: json Out[237]: '{"date":{"0":"2013-01-01T00:00:00.000","1":"2013-01-01T00:00:00.000","2":"2013-01-01T00:00:00.000","3":"2013-01-01T00:00:00.000","4":"2013-01-01T00:00:00.000"},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Writing in ISO date format, with microseconds: In [238]: json = dfd.to_json(date_format="iso", date_unit="us") In [239]: json Out[239]: '{"date":{"0":"2013-01-01T00:00:00.000000","1":"2013-01-01T00:00:00.000000","2":"2013-01-01T00:00:00.000000","3":"2013-01-01T00:00:00.000000","4":"2013-01-01T00:00:00.000000"},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Epoch timestamps, in seconds: In [240]: json = dfd.to_json(date_format="epoch", date_unit="s") In [241]: json Out[241]: '{"date":{"0":1356998400,"1":1356998400,"2":1356998400,"3":1356998400,"4":1356998400},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Writing to a file, with a date index and a date column: In [242]: dfj2 = dfj.copy() In [243]: dfj2["date"] = pd.Timestamp("20130101") In [244]: dfj2["ints"] = list(range(5)) In [245]: dfj2["bools"] = True In [246]: dfj2.index = pd.date_range("20130101", periods=5) In [247]: dfj2.to_json("test.json") In [248]: with open("test.json") as fh: .....: print(fh.read()) .....: {"A":{"1356998400000":-0.1213062281,"1357084800000":0.6957746499,"1357171200000":0.9597255933,"1357257600000":-0.6199759194,"1357344000000":-0.7323393705},"B":{"1356998400000":-0.0978826728,"1357084800000":0.3417343559,"1357171200000":-1.1103361029,"1357257600000":0.1497483186,"1357344000000":0.6877383895},"date":{"1356998400000":1356998400000,"1357084800000":1356998400000,"1357171200000":1356998400000,"1357257600000":1356998400000,"1357344000000":1356998400000},"ints":{"1356998400000":0,"1357084800000":1,"1357171200000":2,"1357257600000":3,"1357344000000":4},"bools":{"1356998400000":true,"1357084800000":true,"1357171200000":true,"1357257600000":true,"1357344000000":true}} Fallback behavior# If the JSON serializer cannot handle the container contents directly it will fall back in the following manner: if the dtype is unsupported (e.g. np.complex_) then the default_handler, if provided, will be called for each value, otherwise an exception is raised. if an object is unsupported it will attempt the following: check if the object has defined a toDict method and call it. A toDict method should return a dict which will then be JSON serialized. invoke the default_handler if one was provided. convert the object to a dict by traversing its contents. However this will often fail with an OverflowError or give unexpected results. In general the best approach for unsupported objects or dtypes is to provide a default_handler. For example: >>> DataFrame([1.0, 2.0, complex(1.0, 2.0)]).to_json() # raises RuntimeError: Unhandled numpy dtype 15 can be dealt with by specifying a simple default_handler: In [249]: pd.DataFrame([1.0, 2.0, complex(1.0, 2.0)]).to_json(default_handler=str) Out[249]: '{"0":{"0":"(1+0j)","1":"(2+0j)","2":"(1+2j)"}}' Reading JSON# Reading a JSON string to pandas object can take a number of parameters. The parser will try to parse a DataFrame if typ is not supplied or is None. To explicitly force Series parsing, pass typ=series filepath_or_buffer : a VALID JSON string or file handle / StringIO. The string could be a URL. Valid URL schemes include http, ftp, S3, and file. For file URLs, a host is expected. For instance, a local file could be file ://localhost/path/to/table.json typ : type of object to recover (series or frame), default ‘frame’ orient : Series : default is index allowed values are {split, records, index} DataFrame default is columns allowed values are {split, records, index, columns, values, table} The format of the JSON string split dict like {index -> [index], columns -> [columns], data -> [values]} records list like [{column -> value}, … , {column -> value}] index dict like {index -> {column -> value}} columns dict like {column -> {index -> value}} values just the values array table adhering to the JSON Table Schema dtype : if True, infer dtypes, if a dict of column to dtype, then use those, if False, then don’t infer dtypes at all, default is True, apply only to the data. convert_axes : boolean, try to convert the axes to the proper dtypes, default is True convert_dates : a list of columns to parse for dates; If True, then try to parse date-like columns, default is True. keep_default_dates : boolean, default True. If parsing dates, then parse the default date-like columns. numpy : direct decoding to NumPy arrays. default is False; Supports numeric data only, although labels may be non-numeric. Also note that the JSON ordering MUST be the same for each term if numpy=True. precise_float : boolean, default False. Set to enable usage of higher precision (strtod) function when decoding string to double values. Default (False) is to use fast but less precise builtin functionality. date_unit : string, the timestamp unit to detect if converting dates. Default None. By default the timestamp precision will be detected, if this is not desired then pass one of ‘s’, ‘ms’, ‘us’ or ‘ns’ to force timestamp precision to seconds, milliseconds, microseconds or nanoseconds respectively. lines : reads file as one json object per line. encoding : The encoding to use to decode py3 bytes. chunksize : when used in combination with lines=True, return a JsonReader which reads in chunksize lines per iteration. The parser will raise one of ValueError/TypeError/AssertionError if the JSON is not parseable. If a non-default orient was used when encoding to JSON be sure to pass the same option here so that decoding produces sensible results, see Orient Options for an overview. Data conversion# The default of convert_axes=True, dtype=True, and convert_dates=True will try to parse the axes, and all of the data into appropriate types, including dates. If you need to override specific dtypes, pass a dict to dtype. convert_axes should only be set to False if you need to preserve string-like numbers (e.g. ‘1’, ‘2’) in an axes. Note Large integer values may be converted to dates if convert_dates=True and the data and / or column labels appear ‘date-like’. The exact threshold depends on the date_unit specified. ‘date-like’ means that the column label meets one of the following criteria: it ends with '_at' it ends with '_time' it begins with 'timestamp' it is 'modified' it is 'date' Warning When reading JSON data, automatic coercing into dtypes has some quirks: an index can be reconstructed in a different order from serialization, that is, the returned order is not guaranteed to be the same as before serialization a column that was float data will be converted to integer if it can be done safely, e.g. a column of 1. bool columns will be converted to integer on reconstruction Thus there are times where you may want to specify specific dtypes via the dtype keyword argument. Reading from a JSON string: In [250]: pd.read_json(json) Out[250]: date B A 0 2013-01-01 0.403310 0.176444 1 2013-01-01 0.301624 -0.154951 2 2013-01-01 -1.369849 -2.179861 3 2013-01-01 1.462696 -0.954208 4 2013-01-01 -0.826591 -1.743161 Reading from a file: In [251]: pd.read_json("test.json") Out[251]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True Don’t convert any data (but still convert axes and dates): In [252]: pd.read_json("test.json", dtype=object).dtypes Out[252]: A object B object date object ints object bools object dtype: object Specify dtypes for conversion: In [253]: pd.read_json("test.json", dtype={"A": "float32", "bools": "int8"}).dtypes Out[253]: A float32 B float64 date datetime64[ns] ints int64 bools int8 dtype: object Preserve string indices: In [254]: si = pd.DataFrame( .....: np.zeros((4, 4)), columns=list(range(4)), index=[str(i) for i in range(4)] .....: ) .....: In [255]: si Out[255]: 0 1 2 3 0 0.0 0.0 0.0 0.0 1 0.0 0.0 0.0 0.0 2 0.0 0.0 0.0 0.0 3 0.0 0.0 0.0 0.0 In [256]: si.index Out[256]: Index(['0', '1', '2', '3'], dtype='object') In [257]: si.columns Out[257]: Int64Index([0, 1, 2, 3], dtype='int64') In [258]: json = si.to_json() In [259]: sij = pd.read_json(json, convert_axes=False) In [260]: sij Out[260]: 0 1 2 3 0 0 0 0 0 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 In [261]: sij.index Out[261]: Index(['0', '1', '2', '3'], dtype='object') In [262]: sij.columns Out[262]: Index(['0', '1', '2', '3'], dtype='object') Dates written in nanoseconds need to be read back in nanoseconds: In [263]: json = dfj2.to_json(date_unit="ns") # Try to parse timestamps as milliseconds -> Won't Work In [264]: dfju = pd.read_json(json, date_unit="ms") In [265]: dfju Out[265]: A B date ints bools 1356998400000000000 -0.121306 -0.097883 1356998400000000000 0 True 1357084800000000000 0.695775 0.341734 1356998400000000000 1 True 1357171200000000000 0.959726 -1.110336 1356998400000000000 2 True 1357257600000000000 -0.619976 0.149748 1356998400000000000 3 True 1357344000000000000 -0.732339 0.687738 1356998400000000000 4 True # Let pandas detect the correct precision In [266]: dfju = pd.read_json(json) In [267]: dfju Out[267]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True # Or specify that all timestamps are in nanoseconds In [268]: dfju = pd.read_json(json, date_unit="ns") In [269]: dfju Out[269]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True The Numpy parameter# Note This param has been deprecated as of version 1.0.0 and will raise a FutureWarning. This supports numeric data only. Index and columns labels may be non-numeric, e.g. strings, dates etc. If numpy=True is passed to read_json an attempt will be made to sniff an appropriate dtype during deserialization and to subsequently decode directly to NumPy arrays, bypassing the need for intermediate Python objects. This can provide speedups if you are deserialising a large amount of numeric data: In [270]: randfloats = np.random.uniform(-100, 1000, 10000) In [271]: randfloats.shape = (1000, 10) In [272]: dffloats = pd.DataFrame(randfloats, columns=list("ABCDEFGHIJ")) In [273]: jsonfloats = dffloats.to_json() In [274]: %timeit pd.read_json(jsonfloats) 7.91 ms +- 77.3 us per loop (mean +- std. dev. of 7 runs, 100 loops each) In [275]: %timeit pd.read_json(jsonfloats, numpy=True) 5.71 ms +- 333 us per loop (mean +- std. dev. of 7 runs, 100 loops each) The speedup is less noticeable for smaller datasets: In [276]: jsonfloats = dffloats.head(100).to_json() In [277]: %timeit pd.read_json(jsonfloats) 4.46 ms +- 25.9 us per loop (mean +- std. dev. of 7 runs, 100 loops each) In [278]: %timeit pd.read_json(jsonfloats, numpy=True) 4.09 ms +- 32.3 us per loop (mean +- std. dev. of 7 runs, 100 loops each) Warning Direct NumPy decoding makes a number of assumptions and may fail or produce unexpected output if these assumptions are not satisfied: data is numeric. data is uniform. The dtype is sniffed from the first value decoded. A ValueError may be raised, or incorrect output may be produced if this condition is not satisfied. labels are ordered. Labels are only read from the first container, it is assumed that each subsequent row / column has been encoded in the same order. This should be satisfied if the data was encoded using to_json but may not be the case if the JSON is from another source. Normalization# pandas provides a utility function to take a dict or list of dicts and normalize this semi-structured data into a flat table. In [279]: data = [ .....: {"id": 1, "name": {"first": "Coleen", "last": "Volk"}}, .....: {"name": {"given": "Mark", "family": "Regner"}}, .....: {"id": 2, "name": "Faye Raker"}, .....: ] .....: In [280]: pd.json_normalize(data) Out[280]: id name.first name.last name.given name.family name 0 1.0 Coleen Volk NaN NaN NaN 1 NaN NaN NaN Mark Regner NaN 2 2.0 NaN NaN NaN NaN Faye Raker In [281]: data = [ .....: { .....: "state": "Florida", .....: "shortname": "FL", .....: "info": {"governor": "Rick Scott"}, .....: "county": [ .....: {"name": "Dade", "population": 12345}, .....: {"name": "Broward", "population": 40000}, .....: {"name": "Palm Beach", "population": 60000}, .....: ], .....: }, .....: { .....: "state": "Ohio", .....: "shortname": "OH", .....: "info": {"governor": "John Kasich"}, .....: "county": [ .....: {"name": "Summit", "population": 1234}, .....: {"name": "Cuyahoga", "population": 1337}, .....: ], .....: }, .....: ] .....: In [282]: pd.json_normalize(data, "county", ["state", "shortname", ["info", "governor"]]) Out[282]: name population state shortname info.governor 0 Dade 12345 Florida FL Rick Scott 1 Broward 40000 Florida FL Rick Scott 2 Palm Beach 60000 Florida FL Rick Scott 3 Summit 1234 Ohio OH John Kasich 4 Cuyahoga 1337 Ohio OH John Kasich The max_level parameter provides more control over which level to end normalization. With max_level=1 the following snippet normalizes until 1st nesting level of the provided dict. In [283]: data = [ .....: { .....: "CreatedBy": {"Name": "User001"}, .....: "Lookup": { .....: "TextField": "Some text", .....: "UserField": {"Id": "ID001", "Name": "Name001"}, .....: }, .....: "Image": {"a": "b"}, .....: } .....: ] .....: In [284]: pd.json_normalize(data, max_level=1) Out[284]: CreatedBy.Name Lookup.TextField Lookup.UserField Image.a 0 User001 Some text {'Id': 'ID001', 'Name': 'Name001'} b Line delimited json# pandas is able to read and write line-delimited json files that are common in data processing pipelines using Hadoop or Spark. For line-delimited json files, pandas can also return an iterator which reads in chunksize lines at a time. This can be useful for large files or to read from a stream. In [285]: jsonl = """ .....: {"a": 1, "b": 2} .....: {"a": 3, "b": 4} .....: """ .....: In [286]: df = pd.read_json(jsonl, lines=True) In [287]: df Out[287]: a b 0 1 2 1 3 4 In [288]: df.to_json(orient="records", lines=True) Out[288]: '{"a":1,"b":2}\n{"a":3,"b":4}\n' # reader is an iterator that returns ``chunksize`` lines each iteration In [289]: with pd.read_json(StringIO(jsonl), lines=True, chunksize=1) as reader: .....: reader .....: for chunk in reader: .....: print(chunk) .....: Empty DataFrame Columns: [] Index: [] a b 0 1 2 a b 1 3 4 Table schema# Table Schema is a spec for describing tabular datasets as a JSON object. The JSON includes information on the field names, types, and other attributes. You can use the orient table to build a JSON string with two fields, schema and data. In [290]: df = pd.DataFrame( .....: { .....: "A": [1, 2, 3], .....: "B": ["a", "b", "c"], .....: "C": pd.date_range("2016-01-01", freq="d", periods=3), .....: }, .....: index=pd.Index(range(3), name="idx"), .....: ) .....: In [291]: df Out[291]: A B C idx 0 1 a 2016-01-01 1 2 b 2016-01-02 2 3 c 2016-01-03 In [292]: df.to_json(orient="table", date_format="iso") Out[292]: '{"schema":{"fields":[{"name":"idx","type":"integer"},{"name":"A","type":"integer"},{"name":"B","type":"string"},{"name":"C","type":"datetime"}],"primaryKey":["idx"],"pandas_version":"1.4.0"},"data":[{"idx":0,"A":1,"B":"a","C":"2016-01-01T00:00:00.000"},{"idx":1,"A":2,"B":"b","C":"2016-01-02T00:00:00.000"},{"idx":2,"A":3,"B":"c","C":"2016-01-03T00:00:00.000"}]}' The schema field contains the fields key, which itself contains a list of column name to type pairs, including the Index or MultiIndex (see below for a list of types). The schema field also contains a primaryKey field if the (Multi)index is unique. The second field, data, contains the serialized data with the records orient. The index is included, and any datetimes are ISO 8601 formatted, as required by the Table Schema spec. The full list of types supported are described in the Table Schema spec. This table shows the mapping from pandas types: pandas type Table Schema type int64 integer float64 number bool boolean datetime64[ns] datetime timedelta64[ns] duration categorical any object str A few notes on the generated table schema: The schema object contains a pandas_version field. This contains the version of pandas’ dialect of the schema, and will be incremented with each revision. All dates are converted to UTC when serializing. Even timezone naive values, which are treated as UTC with an offset of 0. In [293]: from pandas.io.json import build_table_schema In [294]: s = pd.Series(pd.date_range("2016", periods=4)) In [295]: build_table_schema(s) Out[295]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'datetime'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} datetimes with a timezone (before serializing), include an additional field tz with the time zone name (e.g. 'US/Central'). In [296]: s_tz = pd.Series(pd.date_range("2016", periods=12, tz="US/Central")) In [297]: build_table_schema(s_tz) Out[297]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'datetime', 'tz': 'US/Central'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} Periods are converted to timestamps before serialization, and so have the same behavior of being converted to UTC. In addition, periods will contain and additional field freq with the period’s frequency, e.g. 'A-DEC'. In [298]: s_per = pd.Series(1, index=pd.period_range("2016", freq="A-DEC", periods=4)) In [299]: build_table_schema(s_per) Out[299]: {'fields': [{'name': 'index', 'type': 'datetime', 'freq': 'A-DEC'}, {'name': 'values', 'type': 'integer'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} Categoricals use the any type and an enum constraint listing the set of possible values. Additionally, an ordered field is included: In [300]: s_cat = pd.Series(pd.Categorical(["a", "b", "a"])) In [301]: build_table_schema(s_cat) Out[301]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'any', 'constraints': {'enum': ['a', 'b']}, 'ordered': False}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} A primaryKey field, containing an array of labels, is included if the index is unique: In [302]: s_dupe = pd.Series([1, 2], index=[1, 1]) In [303]: build_table_schema(s_dupe) Out[303]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'integer'}], 'pandas_version': '1.4.0'} The primaryKey behavior is the same with MultiIndexes, but in this case the primaryKey is an array: In [304]: s_multi = pd.Series(1, index=pd.MultiIndex.from_product([("a", "b"), (0, 1)])) In [305]: build_table_schema(s_multi) Out[305]: {'fields': [{'name': 'level_0', 'type': 'string'}, {'name': 'level_1', 'type': 'integer'}, {'name': 'values', 'type': 'integer'}], 'primaryKey': FrozenList(['level_0', 'level_1']), 'pandas_version': '1.4.0'} The default naming roughly follows these rules: For series, the object.name is used. If that’s none, then the name is values For DataFrames, the stringified version of the column name is used For Index (not MultiIndex), index.name is used, with a fallback to index if that is None. For MultiIndex, mi.names is used. If any level has no name, then level_<i> is used. read_json also accepts orient='table' as an argument. This allows for the preservation of metadata such as dtypes and index names in a round-trippable manner. In [306]: df = pd.DataFrame( .....: { .....: "foo": [1, 2, 3, 4], .....: "bar": ["a", "b", "c", "d"], .....: "baz": pd.date_range("2018-01-01", freq="d", periods=4), .....: "qux": pd.Categorical(["a", "b", "c", "c"]), .....: }, .....: index=pd.Index(range(4), name="idx"), .....: ) .....: In [307]: df Out[307]: foo bar baz qux idx 0 1 a 2018-01-01 a 1 2 b 2018-01-02 b 2 3 c 2018-01-03 c 3 4 d 2018-01-04 c In [308]: df.dtypes Out[308]: foo int64 bar object baz datetime64[ns] qux category dtype: object In [309]: df.to_json("test.json", orient="table") In [310]: new_df = pd.read_json("test.json", orient="table") In [311]: new_df Out[311]: foo bar baz qux idx 0 1 a 2018-01-01 a 1 2 b 2018-01-02 b 2 3 c 2018-01-03 c 3 4 d 2018-01-04 c In [312]: new_df.dtypes Out[312]: foo int64 bar object baz datetime64[ns] qux category dtype: object Please note that the literal string ‘index’ as the name of an Index is not round-trippable, nor are any names beginning with 'level_' within a MultiIndex. These are used by default in DataFrame.to_json() to indicate missing values and the subsequent read cannot distinguish the intent. In [313]: df.index.name = "index" In [314]: df.to_json("test.json", orient="table") In [315]: new_df = pd.read_json("test.json", orient="table") In [316]: print(new_df.index.name) None When using orient='table' along with user-defined ExtensionArray, the generated schema will contain an additional extDtype key in the respective fields element. This extra key is not standard but does enable JSON roundtrips for extension types (e.g. read_json(df.to_json(orient="table"), orient="table")). The extDtype key carries the name of the extension, if you have properly registered the ExtensionDtype, pandas will use said name to perform a lookup into the registry and re-convert the serialized data into your custom dtype. HTML# Reading HTML content# Warning We highly encourage you to read the HTML Table Parsing gotchas below regarding the issues surrounding the BeautifulSoup4/html5lib/lxml parsers. The top-level read_html() function can accept an HTML string/file/URL and will parse HTML tables into list of pandas DataFrames. Let’s look at a few examples. Note read_html returns a list of DataFrame objects, even if there is only a single table contained in the HTML content. Read a URL with no options: In [320]: "https://www.fdic.gov/resources/resolutions/bank-failures/failed-bank-list" In [321]: pd.read_html(url) Out[321]: [ Bank NameBank CityCity StateSt ... Acquiring InstitutionAI Closing DateClosing FundFund 0 Almena State Bank Almena KS ... Equity Bank October 23, 2020 10538 1 First City Bank of Florida Fort Walton Beach FL ... United Fidelity Bank, fsb October 16, 2020 10537 2 The First State Bank Barboursville WV ... MVB Bank, Inc. April 3, 2020 10536 3 Ericson State Bank Ericson NE ... Farmers and Merchants Bank February 14, 2020 10535 4 City National Bank of New Jersey Newark NJ ... Industrial Bank November 1, 2019 10534 .. ... ... ... ... ... ... ... 558 Superior Bank, FSB Hinsdale IL ... Superior Federal, FSB July 27, 2001 6004 559 Malta National Bank Malta OH ... North Valley Bank May 3, 2001 4648 560 First Alliance Bank & Trust Co. Manchester NH ... Southern New Hampshire Bank & Trust February 2, 2001 4647 561 National State Bank of Metropolis Metropolis IL ... Banterra Bank of Marion December 14, 2000 4646 562 Bank of Honolulu Honolulu HI ... Bank of the Orient October 13, 2000 4645 [563 rows x 7 columns]] Note The data from the above URL changes every Monday so the resulting data above may be slightly different. Read in the content of the file from the above URL and pass it to read_html as a string: In [317]: html_str = """ .....: <table> .....: <tr> .....: <th>A</th> .....: <th colspan="1">B</th> .....: <th rowspan="1">C</th> .....: </tr> .....: <tr> .....: <td>a</td> .....: <td>b</td> .....: <td>c</td> .....: </tr> .....: </table> .....: """ .....: In [318]: with open("tmp.html", "w") as f: .....: f.write(html_str) .....: In [319]: df = pd.read_html("tmp.html") In [320]: df[0] Out[320]: A B C 0 a b c You can even pass in an instance of StringIO if you so desire: In [321]: dfs = pd.read_html(StringIO(html_str)) In [322]: dfs[0] Out[322]: A B C 0 a b c Note The following examples are not run by the IPython evaluator due to the fact that having so many network-accessing functions slows down the documentation build. If you spot an error or an example that doesn’t run, please do not hesitate to report it over on pandas GitHub issues page. Read a URL and match a table that contains specific text: match = "Metcalf Bank" df_list = pd.read_html(url, match=match) Specify a header row (by default <th> or <td> elements located within a <thead> are used to form the column index, if multiple rows are contained within <thead> then a MultiIndex is created); if specified, the header row is taken from the data minus the parsed header elements (<th> elements). dfs = pd.read_html(url, header=0) Specify an index column: dfs = pd.read_html(url, index_col=0) Specify a number of rows to skip: dfs = pd.read_html(url, skiprows=0) Specify a number of rows to skip using a list (range works as well): dfs = pd.read_html(url, skiprows=range(2)) Specify an HTML attribute: dfs1 = pd.read_html(url, attrs={"id": "table"}) dfs2 = pd.read_html(url, attrs={"class": "sortable"}) print(np.array_equal(dfs1[0], dfs2[0])) # Should be True Specify values that should be converted to NaN: dfs = pd.read_html(url, na_values=["No Acquirer"]) Specify whether to keep the default set of NaN values: dfs = pd.read_html(url, keep_default_na=False) Specify converters for columns. This is useful for numerical text data that has leading zeros. By default columns that are numerical are cast to numeric types and the leading zeros are lost. To avoid this, we can convert these columns to strings. url_mcc = "https://en.wikipedia.org/wiki/Mobile_country_code" dfs = pd.read_html( url_mcc, match="Telekom Albania", header=0, converters={"MNC": str}, ) Use some combination of the above: dfs = pd.read_html(url, match="Metcalf Bank", index_col=0) Read in pandas to_html output (with some loss of floating point precision): df = pd.DataFrame(np.random.randn(2, 2)) s = df.to_html(float_format="{0:.40g}".format) dfin = pd.read_html(s, index_col=0) The lxml backend will raise an error on a failed parse if that is the only parser you provide. If you only have a single parser you can provide just a string, but it is considered good practice to pass a list with one string if, for example, the function expects a sequence of strings. You may use: dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor=["lxml"]) Or you could pass flavor='lxml' without a list: dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor="lxml") However, if you have bs4 and html5lib installed and pass None or ['lxml', 'bs4'] then the parse will most likely succeed. Note that as soon as a parse succeeds, the function will return. dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor=["lxml", "bs4"]) Links can be extracted from cells along with the text using extract_links="all". In [323]: html_table = """ .....: <table> .....: <tr> .....: <th>GitHub</th> .....: </tr> .....: <tr> .....: <td><a href="https://github.com/pandas-dev/pandas">pandas</a></td> .....: </tr> .....: </table> .....: """ .....: In [324]: df = pd.read_html( .....: html_table, .....: extract_links="all" .....: )[0] .....: In [325]: df Out[325]: (GitHub, None) 0 (pandas, https://github.com/pandas-dev/pandas) In [326]: df[("GitHub", None)] Out[326]: 0 (pandas, https://github.com/pandas-dev/pandas) Name: (GitHub, None), dtype: object In [327]: df[("GitHub", None)].str[1] Out[327]: 0 https://github.com/pandas-dev/pandas Name: (GitHub, None), dtype: object New in version 1.5.0. Writing to HTML files# DataFrame objects have an instance method to_html which renders the contents of the DataFrame as an HTML table. The function arguments are as in the method to_string described above. Note Not all of the possible options for DataFrame.to_html are shown here for brevity’s sake. See to_html() for the full set of options. Note In an HTML-rendering supported environment like a Jupyter Notebook, display(HTML(...))` will render the raw HTML into the environment. In [328]: from IPython.display import display, HTML In [329]: df = pd.DataFrame(np.random.randn(2, 2)) In [330]: df Out[330]: 0 1 0 0.070319 1.773907 1 0.253908 0.414581 In [331]: html = df.to_html() In [332]: print(html) # raw html <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <th>1</th> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> In [333]: display(HTML(html)) <IPython.core.display.HTML object> The columns argument will limit the columns shown: In [334]: html = df.to_html(columns=[0]) In [335]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> </tr> <tr> <th>1</th> <td>0.253908</td> </tr> </tbody> </table> In [336]: display(HTML(html)) <IPython.core.display.HTML object> float_format takes a Python callable to control the precision of floating point values: In [337]: html = df.to_html(float_format="{0:.10f}".format) In [338]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.0703192665</td> <td>1.7739074228</td> </tr> <tr> <th>1</th> <td>0.2539083433</td> <td>0.4145805920</td> </tr> </tbody> </table> In [339]: display(HTML(html)) <IPython.core.display.HTML object> bold_rows will make the row labels bold by default, but you can turn that off: In [340]: html = df.to_html(bold_rows=False) In [341]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <td>1</td> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> In [342]: display(HTML(html)) <IPython.core.display.HTML object> The classes argument provides the ability to give the resulting HTML table CSS classes. Note that these classes are appended to the existing 'dataframe' class. In [343]: print(df.to_html(classes=["awesome_table_class", "even_more_awesome_class"])) <table border="1" class="dataframe awesome_table_class even_more_awesome_class"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <th>1</th> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> The render_links argument provides the ability to add hyperlinks to cells that contain URLs. In [344]: url_df = pd.DataFrame( .....: { .....: "name": ["Python", "pandas"], .....: "url": ["https://www.python.org/", "https://pandas.pydata.org"], .....: } .....: ) .....: In [345]: html = url_df.to_html(render_links=True) In [346]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>name</th> <th>url</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>Python</td> <td><a href="https://www.python.org/" target="_blank">https://www.python.org/</a></td> </tr> <tr> <th>1</th> <td>pandas</td> <td><a href="https://pandas.pydata.org" target="_blank">https://pandas.pydata.org</a></td> </tr> </tbody> </table> In [347]: display(HTML(html)) <IPython.core.display.HTML object> Finally, the escape argument allows you to control whether the “<”, “>” and “&” characters escaped in the resulting HTML (by default it is True). So to get the HTML without escaped characters pass escape=False In [348]: df = pd.DataFrame({"a": list("&<>"), "b": np.random.randn(3)}) Escaped: In [349]: html = df.to_html() In [350]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>a</th> <th>b</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>&amp;</td> <td>0.842321</td> </tr> <tr> <th>1</th> <td>&lt;</td> <td>0.211337</td> </tr> <tr> <th>2</th> <td>&gt;</td> <td>-1.055427</td> </tr> </tbody> </table> In [351]: display(HTML(html)) <IPython.core.display.HTML object> Not escaped: In [352]: html = df.to_html(escape=False) In [353]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>a</th> <th>b</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>&</td> <td>0.842321</td> </tr> <tr> <th>1</th> <td><</td> <td>0.211337</td> </tr> <tr> <th>2</th> <td>></td> <td>-1.055427</td> </tr> </tbody> </table> In [354]: display(HTML(html)) <IPython.core.display.HTML object> Note Some browsers may not show a difference in the rendering of the previous two HTML tables. HTML Table Parsing Gotchas# There are some versioning issues surrounding the libraries that are used to parse HTML tables in the top-level pandas io function read_html. Issues with lxml Benefits lxml is very fast. lxml requires Cython to install correctly. Drawbacks lxml does not make any guarantees about the results of its parse unless it is given strictly valid markup. In light of the above, we have chosen to allow you, the user, to use the lxml backend, but this backend will use html5lib if lxml fails to parse It is therefore highly recommended that you install both BeautifulSoup4 and html5lib, so that you will still get a valid result (provided everything else is valid) even if lxml fails. Issues with BeautifulSoup4 using lxml as a backend The above issues hold here as well since BeautifulSoup4 is essentially just a wrapper around a parser backend. Issues with BeautifulSoup4 using html5lib as a backend Benefits html5lib is far more lenient than lxml and consequently deals with real-life markup in a much saner way rather than just, e.g., dropping an element without notifying you. html5lib generates valid HTML5 markup from invalid markup automatically. This is extremely important for parsing HTML tables, since it guarantees a valid document. However, that does NOT mean that it is “correct”, since the process of fixing markup does not have a single definition. html5lib is pure Python and requires no additional build steps beyond its own installation. Drawbacks The biggest drawback to using html5lib is that it is slow as molasses. However consider the fact that many tables on the web are not big enough for the parsing algorithm runtime to matter. It is more likely that the bottleneck will be in the process of reading the raw text from the URL over the web, i.e., IO (input-output). For very large tables, this might not be true. LaTeX# New in version 1.3.0. Currently there are no methods to read from LaTeX, only output methods. Writing to LaTeX files# Note DataFrame and Styler objects currently have a to_latex method. We recommend using the Styler.to_latex() method over DataFrame.to_latex() due to the former’s greater flexibility with conditional styling, and the latter’s possible future deprecation. Review the documentation for Styler.to_latex, which gives examples of conditional styling and explains the operation of its keyword arguments. For simple application the following pattern is sufficient. In [355]: df = pd.DataFrame([[1, 2], [3, 4]], index=["a", "b"], columns=["c", "d"]) In [356]: print(df.style.to_latex()) \begin{tabular}{lrr} & c & d \\ a & 1 & 2 \\ b & 3 & 4 \\ \end{tabular} To format values before output, chain the Styler.format method. In [357]: print(df.style.format("€ {}").to_latex()) \begin{tabular}{lrr} & c & d \\ a & € 1 & € 2 \\ b & € 3 & € 4 \\ \end{tabular} XML# Reading XML# New in version 1.3.0. The top-level read_xml() function can accept an XML string/file/URL and will parse nodes and attributes into a pandas DataFrame. Note Since there is no standard XML structure where design types can vary in many ways, read_xml works best with flatter, shallow versions. If an XML document is deeply nested, use the stylesheet feature to transform XML into a flatter version. Let’s look at a few examples. Read an XML string: In [358]: xml = """<?xml version="1.0" encoding="UTF-8"?> .....: <bookstore> .....: <book category="cooking"> .....: <title lang="en">Everyday Italian</title> .....: <author>Giada De Laurentiis</author> .....: <year>2005</year> .....: <price>30.00</price> .....: </book> .....: <book category="children"> .....: <title lang="en">Harry Potter</title> .....: <author>J K. Rowling</author> .....: <year>2005</year> .....: <price>29.99</price> .....: </book> .....: <book category="web"> .....: <title lang="en">Learning XML</title> .....: <author>Erik T. Ray</author> .....: <year>2003</year> .....: <price>39.95</price> .....: </book> .....: </bookstore>""" .....: In [359]: df = pd.read_xml(xml) In [360]: df Out[360]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Read a URL with no options: In [361]: df = pd.read_xml("https://www.w3schools.com/xml/books.xml") In [362]: df Out[362]: category title author year price cover 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 None 1 children Harry Potter J K. Rowling 2005 29.99 None 2 web XQuery Kick Start Vaidyanathan Nagarajan 2003 49.99 None 3 web Learning XML Erik T. Ray 2003 39.95 paperback Read in the content of the “books.xml” file and pass it to read_xml as a string: In [363]: file_path = "books.xml" In [364]: with open(file_path, "w") as f: .....: f.write(xml) .....: In [365]: with open(file_path, "r") as f: .....: df = pd.read_xml(f.read()) .....: In [366]: df Out[366]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Read in the content of the “books.xml” as instance of StringIO or BytesIO and pass it to read_xml: In [367]: with open(file_path, "r") as f: .....: sio = StringIO(f.read()) .....: In [368]: df = pd.read_xml(sio) In [369]: df Out[369]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 In [370]: with open(file_path, "rb") as f: .....: bio = BytesIO(f.read()) .....: In [371]: df = pd.read_xml(bio) In [372]: df Out[372]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Even read XML from AWS S3 buckets such as NIH NCBI PMC Article Datasets providing Biomedical and Life Science Jorurnals: In [373]: df = pd.read_xml( .....: "s3://pmc-oa-opendata/oa_comm/xml/all/PMC1236943.xml", .....: xpath=".//journal-meta", .....: ) .....: In [374]: df Out[374]: journal-id journal-title issn publisher 0 Cardiovasc Ultrasound Cardiovascular Ultrasound 1476-7120 NaN With lxml as default parser, you access the full-featured XML library that extends Python’s ElementTree API. One powerful tool is ability to query nodes selectively or conditionally with more expressive XPath: In [375]: df = pd.read_xml(file_path, xpath="//book[year=2005]") In [376]: df Out[376]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 Specify only elements or only attributes to parse: In [377]: df = pd.read_xml(file_path, elems_only=True) In [378]: df Out[378]: title author year price 0 Everyday Italian Giada De Laurentiis 2005 30.00 1 Harry Potter J K. Rowling 2005 29.99 2 Learning XML Erik T. Ray 2003 39.95 In [379]: df = pd.read_xml(file_path, attrs_only=True) In [380]: df Out[380]: category 0 cooking 1 children 2 web XML documents can have namespaces with prefixes and default namespaces without prefixes both of which are denoted with a special attribute xmlns. In order to parse by node under a namespace context, xpath must reference a prefix. For example, below XML contains a namespace with prefix, doc, and URI at https://example.com. In order to parse doc:row nodes, namespaces must be used. In [381]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <doc:data xmlns:doc="https://example.com"> .....: <doc:row> .....: <doc:shape>square</doc:shape> .....: <doc:degrees>360</doc:degrees> .....: <doc:sides>4.0</doc:sides> .....: </doc:row> .....: <doc:row> .....: <doc:shape>circle</doc:shape> .....: <doc:degrees>360</doc:degrees> .....: <doc:sides/> .....: </doc:row> .....: <doc:row> .....: <doc:shape>triangle</doc:shape> .....: <doc:degrees>180</doc:degrees> .....: <doc:sides>3.0</doc:sides> .....: </doc:row> .....: </doc:data>""" .....: In [382]: df = pd.read_xml(xml, .....: xpath="//doc:row", .....: namespaces={"doc": "https://example.com"}) .....: In [383]: df Out[383]: shape degrees sides 0 square 360 4.0 1 circle 360 NaN 2 triangle 180 3.0 Similarly, an XML document can have a default namespace without prefix. Failing to assign a temporary prefix will return no nodes and raise a ValueError. But assigning any temporary name to correct URI allows parsing by nodes. In [384]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <data xmlns="https://example.com"> .....: <row> .....: <shape>square</shape> .....: <degrees>360</degrees> .....: <sides>4.0</sides> .....: </row> .....: <row> .....: <shape>circle</shape> .....: <degrees>360</degrees> .....: <sides/> .....: </row> .....: <row> .....: <shape>triangle</shape> .....: <degrees>180</degrees> .....: <sides>3.0</sides> .....: </row> .....: </data>""" .....: In [385]: df = pd.read_xml(xml, .....: xpath="//pandas:row", .....: namespaces={"pandas": "https://example.com"}) .....: In [386]: df Out[386]: shape degrees sides 0 square 360 4.0 1 circle 360 NaN 2 triangle 180 3.0 However, if XPath does not reference node names such as default, /*, then namespaces is not required. With lxml as parser, you can flatten nested XML documents with an XSLT script which also can be string/file/URL types. As background, XSLT is a special-purpose language written in a special XML file that can transform original XML documents into other XML, HTML, even text (CSV, JSON, etc.) using an XSLT processor. For example, consider this somewhat nested structure of Chicago “L” Rides where station and rides elements encapsulate data in their own sections. With below XSLT, lxml can transform original nested document into a flatter output (as shown below for demonstration) for easier parse into DataFrame: In [387]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <response> .....: <row> .....: <station id="40850" name="Library"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>864.2</avg_weekday_rides> .....: <avg_saturday_rides>534</avg_saturday_rides> .....: <avg_sunday_holiday_rides>417.2</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: <row> .....: <station id="41700" name="Washington/Wabash"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>2707.4</avg_weekday_rides> .....: <avg_saturday_rides>1909.8</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1438.6</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: <row> .....: <station id="40380" name="Clark/Lake"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>2949.6</avg_weekday_rides> .....: <avg_saturday_rides>1657</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1453.8</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: </response>""" .....: In [388]: xsl = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> .....: <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/> .....: <xsl:strip-space elements="*"/> .....: <xsl:template match="/response"> .....: <xsl:copy> .....: <xsl:apply-templates select="row"/> .....: </xsl:copy> .....: </xsl:template> .....: <xsl:template match="row"> .....: <xsl:copy> .....: <station_id><xsl:value-of select="station/@id"/></station_id> .....: <station_name><xsl:value-of select="station/@name"/></station_name> .....: <xsl:copy-of select="month|rides/*"/> .....: </xsl:copy> .....: </xsl:template> .....: </xsl:stylesheet>""" .....: In [389]: output = """<?xml version='1.0' encoding='utf-8'?> .....: <response> .....: <row> .....: <station_id>40850</station_id> .....: <station_name>Library</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>864.2</avg_weekday_rides> .....: <avg_saturday_rides>534</avg_saturday_rides> .....: <avg_sunday_holiday_rides>417.2</avg_sunday_holiday_rides> .....: </row> .....: <row> .....: <station_id>41700</station_id> .....: <station_name>Washington/Wabash</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>2707.4</avg_weekday_rides> .....: <avg_saturday_rides>1909.8</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1438.6</avg_sunday_holiday_rides> .....: </row> .....: <row> .....: <station_id>40380</station_id> .....: <station_name>Clark/Lake</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>2949.6</avg_weekday_rides> .....: <avg_saturday_rides>1657</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1453.8</avg_sunday_holiday_rides> .....: </row> .....: </response>""" .....: In [390]: df = pd.read_xml(xml, stylesheet=xsl) In [391]: df Out[391]: station_id station_name ... avg_saturday_rides avg_sunday_holiday_rides 0 40850 Library ... 534.0 417.2 1 41700 Washington/Wabash ... 1909.8 1438.6 2 40380 Clark/Lake ... 1657.0 1453.8 [3 rows x 6 columns] For very large XML files that can range in hundreds of megabytes to gigabytes, pandas.read_xml() supports parsing such sizeable files using lxml’s iterparse and etree’s iterparse which are memory-efficient methods to iterate through an XML tree and extract specific elements and attributes. without holding entire tree in memory. New in version 1.5.0. To use this feature, you must pass a physical XML file path into read_xml and use the iterparse argument. Files should not be compressed or point to online sources but stored on local disk. Also, iterparse should be a dictionary where the key is the repeating nodes in document (which become the rows) and the value is a list of any element or attribute that is a descendant (i.e., child, grandchild) of repeating node. Since XPath is not used in this method, descendants do not need to share same relationship with one another. Below shows example of reading in Wikipedia’s very large (12 GB+) latest article data dump. In [1]: df = pd.read_xml( ... "/path/to/downloaded/enwikisource-latest-pages-articles.xml", ... iterparse = {"page": ["title", "ns", "id"]} ... ) ... df Out[2]: title ns id 0 Gettysburg Address 0 21450 1 Main Page 0 42950 2 Declaration by United Nations 0 8435 3 Constitution of the United States of America 0 8435 4 Declaration of Independence (Israel) 0 17858 ... ... ... ... 3578760 Page:Black cat 1897 07 v2 n10.pdf/17 104 219649 3578761 Page:Black cat 1897 07 v2 n10.pdf/43 104 219649 3578762 Page:Black cat 1897 07 v2 n10.pdf/44 104 219649 3578763 The History of Tom Jones, a Foundling/Book IX 0 12084291 3578764 Page:Shakespeare of Stratford (1926) Yale.djvu/91 104 21450 [3578765 rows x 3 columns] Writing XML# New in version 1.3.0. DataFrame objects have an instance method to_xml which renders the contents of the DataFrame as an XML document. Note This method does not support special properties of XML including DTD, CData, XSD schemas, processing instructions, comments, and others. Only namespaces at the root level is supported. However, stylesheet allows design changes after initial output. Let’s look at a few examples. Write an XML without options: In [392]: geom_df = pd.DataFrame( .....: { .....: "shape": ["square", "circle", "triangle"], .....: "degrees": [360, 360, 180], .....: "sides": [4, np.nan, 3], .....: } .....: ) .....: In [393]: print(geom_df.to_xml()) <?xml version='1.0' encoding='utf-8'?> <data> <row> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </row> <row> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </row> <row> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Write an XML with new root and row name: In [394]: print(geom_df.to_xml(root_name="geometry", row_name="objects")) <?xml version='1.0' encoding='utf-8'?> <geometry> <objects> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </objects> <objects> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </objects> <objects> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </objects> </geometry> Write an attribute-centric XML: In [395]: print(geom_df.to_xml(attr_cols=geom_df.columns.tolist())) <?xml version='1.0' encoding='utf-8'?> <data> <row index="0" shape="square" degrees="360" sides="4.0"/> <row index="1" shape="circle" degrees="360"/> <row index="2" shape="triangle" degrees="180" sides="3.0"/> </data> Write a mix of elements and attributes: In [396]: print( .....: geom_df.to_xml( .....: index=False, .....: attr_cols=['shape'], .....: elem_cols=['degrees', 'sides']) .....: ) .....: <?xml version='1.0' encoding='utf-8'?> <data> <row shape="square"> <degrees>360</degrees> <sides>4.0</sides> </row> <row shape="circle"> <degrees>360</degrees> <sides/> </row> <row shape="triangle"> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Any DataFrames with hierarchical columns will be flattened for XML element names with levels delimited by underscores: In [397]: ext_geom_df = pd.DataFrame( .....: { .....: "type": ["polygon", "other", "polygon"], .....: "shape": ["square", "circle", "triangle"], .....: "degrees": [360, 360, 180], .....: "sides": [4, np.nan, 3], .....: } .....: ) .....: In [398]: pvt_df = ext_geom_df.pivot_table(index='shape', .....: columns='type', .....: values=['degrees', 'sides'], .....: aggfunc='sum') .....: In [399]: pvt_df Out[399]: degrees sides type other polygon other polygon shape circle 360.0 NaN 0.0 NaN square NaN 360.0 NaN 4.0 triangle NaN 180.0 NaN 3.0 In [400]: print(pvt_df.to_xml()) <?xml version='1.0' encoding='utf-8'?> <data> <row> <shape>circle</shape> <degrees_other>360.0</degrees_other> <degrees_polygon/> <sides_other>0.0</sides_other> <sides_polygon/> </row> <row> <shape>square</shape> <degrees_other/> <degrees_polygon>360.0</degrees_polygon> <sides_other/> <sides_polygon>4.0</sides_polygon> </row> <row> <shape>triangle</shape> <degrees_other/> <degrees_polygon>180.0</degrees_polygon> <sides_other/> <sides_polygon>3.0</sides_polygon> </row> </data> Write an XML with default namespace: In [401]: print(geom_df.to_xml(namespaces={"": "https://example.com"})) <?xml version='1.0' encoding='utf-8'?> <data xmlns="https://example.com"> <row> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </row> <row> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </row> <row> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Write an XML with namespace prefix: In [402]: print( .....: geom_df.to_xml(namespaces={"doc": "https://example.com"}, .....: prefix="doc") .....: ) .....: <?xml version='1.0' encoding='utf-8'?> <doc:data xmlns:doc="https://example.com"> <doc:row> <doc:index>0</doc:index> <doc:shape>square</doc:shape> <doc:degrees>360</doc:degrees> <doc:sides>4.0</doc:sides> </doc:row> <doc:row> <doc:index>1</doc:index> <doc:shape>circle</doc:shape> <doc:degrees>360</doc:degrees> <doc:sides/> </doc:row> <doc:row> <doc:index>2</doc:index> <doc:shape>triangle</doc:shape> <doc:degrees>180</doc:degrees> <doc:sides>3.0</doc:sides> </doc:row> </doc:data> Write an XML without declaration or pretty print: In [403]: print( .....: geom_df.to_xml(xml_declaration=False, .....: pretty_print=False) .....: ) .....: <data><row><index>0</index><shape>square</shape><degrees>360</degrees><sides>4.0</sides></row><row><index>1</index><shape>circle</shape><degrees>360</degrees><sides/></row><row><index>2</index><shape>triangle</shape><degrees>180</degrees><sides>3.0</sides></row></data> Write an XML and transform with stylesheet: In [404]: xsl = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> .....: <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/> .....: <xsl:strip-space elements="*"/> .....: <xsl:template match="/data"> .....: <geometry> .....: <xsl:apply-templates select="row"/> .....: </geometry> .....: </xsl:template> .....: <xsl:template match="row"> .....: <object index="{index}"> .....: <xsl:if test="shape!='circle'"> .....: <xsl:attribute name="type">polygon</xsl:attribute> .....: </xsl:if> .....: <xsl:copy-of select="shape"/> .....: <property> .....: <xsl:copy-of select="degrees|sides"/> .....: </property> .....: </object> .....: </xsl:template> .....: </xsl:stylesheet>""" .....: In [405]: print(geom_df.to_xml(stylesheet=xsl)) <?xml version="1.0"?> <geometry> <object index="0" type="polygon"> <shape>square</shape> <property> <degrees>360</degrees> <sides>4.0</sides> </property> </object> <object index="1"> <shape>circle</shape> <property> <degrees>360</degrees> <sides/> </property> </object> <object index="2" type="polygon"> <shape>triangle</shape> <property> <degrees>180</degrees> <sides>3.0</sides> </property> </object> </geometry> XML Final Notes# All XML documents adhere to W3C specifications. Both etree and lxml parsers will fail to parse any markup document that is not well-formed or follows XML syntax rules. Do be aware HTML is not an XML document unless it follows XHTML specs. However, other popular markup types including KML, XAML, RSS, MusicML, MathML are compliant XML schemas. For above reason, if your application builds XML prior to pandas operations, use appropriate DOM libraries like etree and lxml to build the necessary document and not by string concatenation or regex adjustments. Always remember XML is a special text file with markup rules. With very large XML files (several hundred MBs to GBs), XPath and XSLT can become memory-intensive operations. Be sure to have enough available RAM for reading and writing to large XML files (roughly about 5 times the size of text). Because XSLT is a programming language, use it with caution since such scripts can pose a security risk in your environment and can run large or infinite recursive operations. Always test scripts on small fragments before full run. The etree parser supports all functionality of both read_xml and to_xml except for complex XPath and any XSLT. Though limited in features, etree is still a reliable and capable parser and tree builder. Its performance may trail lxml to a certain degree for larger files but relatively unnoticeable on small to medium size files. Excel files# The read_excel() method can read Excel 2007+ (.xlsx) files using the openpyxl Python module. Excel 2003 (.xls) files can be read using xlrd. Binary Excel (.xlsb) files can be read using pyxlsb. The to_excel() instance method is used for saving a DataFrame to Excel. Generally the semantics are similar to working with csv data. See the cookbook for some advanced strategies. Warning The xlwt package for writing old-style .xls excel files is no longer maintained. The xlrd package is now only for reading old-style .xls files. Before pandas 1.3.0, the default argument engine=None to read_excel() would result in using the xlrd engine in many cases, including new Excel 2007+ (.xlsx) files. pandas will now default to using the openpyxl engine. It is strongly encouraged to install openpyxl to read Excel 2007+ (.xlsx) files. Please do not report issues when using ``xlrd`` to read ``.xlsx`` files. This is no longer supported, switch to using openpyxl instead. Attempting to use the xlwt engine will raise a FutureWarning unless the option io.excel.xls.writer is set to "xlwt". While this option is now deprecated and will also raise a FutureWarning, it can be globally set and the warning suppressed. Users are recommended to write .xlsx files using the openpyxl engine instead. Reading Excel files# In the most basic use-case, read_excel takes a path to an Excel file, and the sheet_name indicating which sheet to parse. # Returns a DataFrame pd.read_excel("path_to_file.xls", sheet_name="Sheet1") ExcelFile class# To facilitate working with multiple sheets from the same file, the ExcelFile class can be used to wrap the file and can be passed into read_excel There will be a performance benefit for reading multiple sheets as the file is read into memory only once. xlsx = pd.ExcelFile("path_to_file.xls") df = pd.read_excel(xlsx, "Sheet1") The ExcelFile class can also be used as a context manager. with pd.ExcelFile("path_to_file.xls") as xls: df1 = pd.read_excel(xls, "Sheet1") df2 = pd.read_excel(xls, "Sheet2") The sheet_names property will generate a list of the sheet names in the file. The primary use-case for an ExcelFile is parsing multiple sheets with different parameters: data = {} # For when Sheet1's format differs from Sheet2 with pd.ExcelFile("path_to_file.xls") as xls: data["Sheet1"] = pd.read_excel(xls, "Sheet1", index_col=None, na_values=["NA"]) data["Sheet2"] = pd.read_excel(xls, "Sheet2", index_col=1) Note that if the same parsing parameters are used for all sheets, a list of sheet names can simply be passed to read_excel with no loss in performance. # using the ExcelFile class data = {} with pd.ExcelFile("path_to_file.xls") as xls: data["Sheet1"] = pd.read_excel(xls, "Sheet1", index_col=None, na_values=["NA"]) data["Sheet2"] = pd.read_excel(xls, "Sheet2", index_col=None, na_values=["NA"]) # equivalent using the read_excel function data = pd.read_excel( "path_to_file.xls", ["Sheet1", "Sheet2"], index_col=None, na_values=["NA"] ) ExcelFile can also be called with a xlrd.book.Book object as a parameter. This allows the user to control how the excel file is read. For example, sheets can be loaded on demand by calling xlrd.open_workbook() with on_demand=True. import xlrd xlrd_book = xlrd.open_workbook("path_to_file.xls", on_demand=True) with pd.ExcelFile(xlrd_book) as xls: df1 = pd.read_excel(xls, "Sheet1") df2 = pd.read_excel(xls, "Sheet2") Specifying sheets# Note The second argument is sheet_name, not to be confused with ExcelFile.sheet_names. Note An ExcelFile’s attribute sheet_names provides access to a list of sheets. The arguments sheet_name allows specifying the sheet or sheets to read. The default value for sheet_name is 0, indicating to read the first sheet Pass a string to refer to the name of a particular sheet in the workbook. Pass an integer to refer to the index of a sheet. Indices follow Python convention, beginning at 0. Pass a list of either strings or integers, to return a dictionary of specified sheets. Pass a None to return a dictionary of all available sheets. # Returns a DataFrame pd.read_excel("path_to_file.xls", "Sheet1", index_col=None, na_values=["NA"]) Using the sheet index: # Returns a DataFrame pd.read_excel("path_to_file.xls", 0, index_col=None, na_values=["NA"]) Using all default values: # Returns a DataFrame pd.read_excel("path_to_file.xls") Using None to get all sheets: # Returns a dictionary of DataFrames pd.read_excel("path_to_file.xls", sheet_name=None) Using a list to get multiple sheets: # Returns the 1st and 4th sheet, as a dictionary of DataFrames. pd.read_excel("path_to_file.xls", sheet_name=["Sheet1", 3]) read_excel can read more than one sheet, by setting sheet_name to either a list of sheet names, a list of sheet positions, or None to read all sheets. Sheets can be specified by sheet index or sheet name, using an integer or string, respectively. Reading a MultiIndex# read_excel can read a MultiIndex index, by passing a list of columns to index_col and a MultiIndex column by passing a list of rows to header. If either the index or columns have serialized level names those will be read in as well by specifying the rows/columns that make up the levels. For example, to read in a MultiIndex index without names: In [406]: df = pd.DataFrame( .....: {"a": [1, 2, 3, 4], "b": [5, 6, 7, 8]}, .....: index=pd.MultiIndex.from_product([["a", "b"], ["c", "d"]]), .....: ) .....: In [407]: df.to_excel("path_to_file.xlsx") In [408]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1]) In [409]: df Out[409]: a b a c 1 5 d 2 6 b c 3 7 d 4 8 If the index has level names, they will parsed as well, using the same parameters. In [410]: df.index = df.index.set_names(["lvl1", "lvl2"]) In [411]: df.to_excel("path_to_file.xlsx") In [412]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1]) In [413]: df Out[413]: a b lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 If the source file has both MultiIndex index and columns, lists specifying each should be passed to index_col and header: In [414]: df.columns = pd.MultiIndex.from_product([["a"], ["b", "d"]], names=["c1", "c2"]) In [415]: df.to_excel("path_to_file.xlsx") In [416]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1], header=[0, 1]) In [417]: df Out[417]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 Missing values in columns specified in index_col will be forward filled to allow roundtripping with to_excel for merged_cells=True. To avoid forward filling the missing values use set_index after reading the data instead of index_col. Parsing specific columns# It is often the case that users will insert columns to do temporary computations in Excel and you may not want to read in those columns. read_excel takes a usecols keyword to allow you to specify a subset of columns to parse. Changed in version 1.0.0. Passing in an integer for usecols will no longer work. Please pass in a list of ints from 0 to usecols inclusive instead. You can specify a comma-delimited set of Excel columns and ranges as a string: pd.read_excel("path_to_file.xls", "Sheet1", usecols="A,C:E") If usecols is a list of integers, then it is assumed to be the file column indices to be parsed. pd.read_excel("path_to_file.xls", "Sheet1", usecols=[0, 2, 3]) Element order is ignored, so usecols=[0, 1] is the same as [1, 0]. If usecols is a list of strings, it is assumed that each string corresponds to a column name provided either by the user in names or inferred from the document header row(s). Those strings define which columns will be parsed: pd.read_excel("path_to_file.xls", "Sheet1", usecols=["foo", "bar"]) Element order is ignored, so usecols=['baz', 'joe'] is the same as ['joe', 'baz']. If usecols is callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True. pd.read_excel("path_to_file.xls", "Sheet1", usecols=lambda x: x.isalpha()) Parsing dates# Datetime-like values are normally automatically converted to the appropriate dtype when reading the excel file. But if you have a column of strings that look like dates (but are not actually formatted as dates in excel), you can use the parse_dates keyword to parse those strings to datetimes: pd.read_excel("path_to_file.xls", "Sheet1", parse_dates=["date_strings"]) Cell converters# It is possible to transform the contents of Excel cells via the converters option. For instance, to convert a column to boolean: pd.read_excel("path_to_file.xls", "Sheet1", converters={"MyBools": bool}) This options handles missing values and treats exceptions in the converters as missing data. Transformations are applied cell by cell rather than to the column as a whole, so the array dtype is not guaranteed. For instance, a column of integers with missing values cannot be transformed to an array with integer dtype, because NaN is strictly a float. You can manually mask missing data to recover integer dtype: def cfun(x): return int(x) if x else -1 pd.read_excel("path_to_file.xls", "Sheet1", converters={"MyInts": cfun}) Dtype specifications# As an alternative to converters, the type for an entire column can be specified using the dtype keyword, which takes a dictionary mapping column names to types. To interpret data with no type inference, use the type str or object. pd.read_excel("path_to_file.xls", dtype={"MyInts": "int64", "MyText": str}) Writing Excel files# Writing Excel files to disk# To write a DataFrame object to a sheet of an Excel file, you can use the to_excel instance method. The arguments are largely the same as to_csv described above, the first argument being the name of the excel file, and the optional second argument the name of the sheet to which the DataFrame should be written. For example: df.to_excel("path_to_file.xlsx", sheet_name="Sheet1") Files with a .xls extension will be written using xlwt and those with a .xlsx extension will be written using xlsxwriter (if available) or openpyxl. The DataFrame will be written in a way that tries to mimic the REPL output. The index_label will be placed in the second row instead of the first. You can place it in the first row by setting the merge_cells option in to_excel() to False: df.to_excel("path_to_file.xlsx", index_label="label", merge_cells=False) In order to write separate DataFrames to separate sheets in a single Excel file, one can pass an ExcelWriter. with pd.ExcelWriter("path_to_file.xlsx") as writer: df1.to_excel(writer, sheet_name="Sheet1") df2.to_excel(writer, sheet_name="Sheet2") Writing Excel files to memory# pandas supports writing Excel files to buffer-like objects such as StringIO or BytesIO using ExcelWriter. from io import BytesIO bio = BytesIO() # By setting the 'engine' in the ExcelWriter constructor. writer = pd.ExcelWriter(bio, engine="xlsxwriter") df.to_excel(writer, sheet_name="Sheet1") # Save the workbook writer.save() # Seek to the beginning and read to copy the workbook to a variable in memory bio.seek(0) workbook = bio.read() Note engine is optional but recommended. Setting the engine determines the version of workbook produced. Setting engine='xlrd' will produce an Excel 2003-format workbook (xls). Using either 'openpyxl' or 'xlsxwriter' will produce an Excel 2007-format workbook (xlsx). If omitted, an Excel 2007-formatted workbook is produced. Excel writer engines# Deprecated since version 1.2.0: As the xlwt package is no longer maintained, the xlwt engine will be removed from a future version of pandas. This is the only engine in pandas that supports writing to .xls files. pandas chooses an Excel writer via two methods: the engine keyword argument the filename extension (via the default specified in config options) By default, pandas uses the XlsxWriter for .xlsx, openpyxl for .xlsm, and xlwt for .xls files. If you have multiple engines installed, you can set the default engine through setting the config options io.excel.xlsx.writer and io.excel.xls.writer. pandas will fall back on openpyxl for .xlsx files if Xlsxwriter is not available. To specify which writer you want to use, you can pass an engine keyword argument to to_excel and to ExcelWriter. The built-in engines are: openpyxl: version 2.4 or higher is required xlsxwriter xlwt # By setting the 'engine' in the DataFrame 'to_excel()' methods. df.to_excel("path_to_file.xlsx", sheet_name="Sheet1", engine="xlsxwriter") # By setting the 'engine' in the ExcelWriter constructor. writer = pd.ExcelWriter("path_to_file.xlsx", engine="xlsxwriter") # Or via pandas configuration. from pandas import options # noqa: E402 options.io.excel.xlsx.writer = "xlsxwriter" df.to_excel("path_to_file.xlsx", sheet_name="Sheet1") Style and formatting# The look and feel of Excel worksheets created from pandas can be modified using the following parameters on the DataFrame’s to_excel method. float_format : Format string for floating point numbers (default None). freeze_panes : A tuple of two integers representing the bottommost row and rightmost column to freeze. Each of these parameters is one-based, so (1, 1) will freeze the first row and first column (default None). Using the Xlsxwriter engine provides many options for controlling the format of an Excel worksheet created with the to_excel method. Excellent examples can be found in the Xlsxwriter documentation here: https://xlsxwriter.readthedocs.io/working_with_pandas.html OpenDocument Spreadsheets# New in version 0.25. The read_excel() method can also read OpenDocument spreadsheets using the odfpy module. The semantics and features for reading OpenDocument spreadsheets match what can be done for Excel files using engine='odf'. # Returns a DataFrame pd.read_excel("path_to_file.ods", engine="odf") Note Currently pandas only supports reading OpenDocument spreadsheets. Writing is not implemented. Binary Excel (.xlsb) files# New in version 1.0.0. The read_excel() method can also read binary Excel files using the pyxlsb module. The semantics and features for reading binary Excel files mostly match what can be done for Excel files using engine='pyxlsb'. pyxlsb does not recognize datetime types in files and will return floats instead. # Returns a DataFrame pd.read_excel("path_to_file.xlsb", engine="pyxlsb") Note Currently pandas only supports reading binary Excel files. Writing is not implemented. Clipboard# A handy way to grab data is to use the read_clipboard() method, which takes the contents of the clipboard buffer and passes them to the read_csv method. For instance, you can copy the following text to the clipboard (CTRL-C on many operating systems): A B C x 1 4 p y 2 5 q z 3 6 r And then import the data directly to a DataFrame by calling: >>> clipdf = pd.read_clipboard() >>> clipdf A B C x 1 4 p y 2 5 q z 3 6 r The to_clipboard method can be used to write the contents of a DataFrame to the clipboard. Following which you can paste the clipboard contents into other applications (CTRL-V on many operating systems). Here we illustrate writing a DataFrame into clipboard and reading it back. >>> df = pd.DataFrame( ... {"A": [1, 2, 3], "B": [4, 5, 6], "C": ["p", "q", "r"]}, index=["x", "y", "z"] ... ) >>> df A B C x 1 4 p y 2 5 q z 3 6 r >>> df.to_clipboard() >>> pd.read_clipboard() A B C x 1 4 p y 2 5 q z 3 6 r We can see that we got the same content back, which we had earlier written to the clipboard. Note You may need to install xclip or xsel (with PyQt5, PyQt4 or qtpy) on Linux to use these methods. Pickling# All pandas objects are equipped with to_pickle methods which use Python’s cPickle module to save data structures to disk using the pickle format. In [418]: df Out[418]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 In [419]: df.to_pickle("foo.pkl") The read_pickle function in the pandas namespace can be used to load any pickled pandas object (or any other pickled object) from file: In [420]: pd.read_pickle("foo.pkl") Out[420]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 Warning Loading pickled data received from untrusted sources can be unsafe. See: https://docs.python.org/3/library/pickle.html Warning read_pickle() is only guaranteed backwards compatible back to pandas version 0.20.3 Compressed pickle files# read_pickle(), DataFrame.to_pickle() and Series.to_pickle() can read and write compressed pickle files. The compression types of gzip, bz2, xz, zstd are supported for reading and writing. The zip file format only supports reading and must contain only one data file to be read. The compression type can be an explicit parameter or be inferred from the file extension. If ‘infer’, then use gzip, bz2, zip, xz, zstd if filename ends in '.gz', '.bz2', '.zip', '.xz', or '.zst', respectively. The compression parameter can also be a dict in order to pass options to the compression protocol. It must have a 'method' key set to the name of the compression protocol, which must be one of {'zip', 'gzip', 'bz2', 'xz', 'zstd'}. All other key-value pairs are passed to the underlying compression library. In [421]: df = pd.DataFrame( .....: { .....: "A": np.random.randn(1000), .....: "B": "foo", .....: "C": pd.date_range("20130101", periods=1000, freq="s"), .....: } .....: ) .....: In [422]: df Out[422]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] Using an explicit compression type: In [423]: df.to_pickle("data.pkl.compress", compression="gzip") In [424]: rt = pd.read_pickle("data.pkl.compress", compression="gzip") In [425]: rt Out[425]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] Inferring compression type from the extension: In [426]: df.to_pickle("data.pkl.xz", compression="infer") In [427]: rt = pd.read_pickle("data.pkl.xz", compression="infer") In [428]: rt Out[428]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] The default is to ‘infer’: In [429]: df.to_pickle("data.pkl.gz") In [430]: rt = pd.read_pickle("data.pkl.gz") In [431]: rt Out[431]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] In [432]: df["A"].to_pickle("s1.pkl.bz2") In [433]: rt = pd.read_pickle("s1.pkl.bz2") In [434]: rt Out[434]: 0 -0.828876 1 -0.110383 2 2.357598 3 -1.620073 4 0.440903 ... 995 -1.177365 996 1.236988 997 0.743946 998 -0.533097 999 -0.140850 Name: A, Length: 1000, dtype: float64 Passing options to the compression protocol in order to speed up compression: In [435]: df.to_pickle("data.pkl.gz", compression={"method": "gzip", "compresslevel": 1}) msgpack# pandas support for msgpack has been removed in version 1.0.0. It is recommended to use pickle instead. Alternatively, you can also the Arrow IPC serialization format for on-the-wire transmission of pandas objects. For documentation on pyarrow, see here. HDF5 (PyTables)# HDFStore is a dict-like object which reads and writes pandas using the high performance HDF5 format using the excellent PyTables library. See the cookbook for some advanced strategies Warning pandas uses PyTables for reading and writing HDF5 files, which allows serializing object-dtype data with pickle. Loading pickled data received from untrusted sources can be unsafe. See: https://docs.python.org/3/library/pickle.html for more. In [436]: store = pd.HDFStore("store.h5") In [437]: print(store) <class 'pandas.io.pytables.HDFStore'> File path: store.h5 Objects can be written to the file just like adding key-value pairs to a dict: In [438]: index = pd.date_range("1/1/2000", periods=8) In [439]: s = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"]) In [440]: df = pd.DataFrame(np.random.randn(8, 3), index=index, columns=["A", "B", "C"]) # store.put('s', s) is an equivalent method In [441]: store["s"] = s In [442]: store["df"] = df In [443]: store Out[443]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 In a current or later Python session, you can retrieve stored objects: # store.get('df') is an equivalent method In [444]: store["df"] Out[444]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 # dotted (attribute) access provides get as well In [445]: store.df Out[445]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Deletion of the object specified by the key: # store.remove('df') is an equivalent method In [446]: del store["df"] In [447]: store Out[447]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 Closing a Store and using a context manager: In [448]: store.close() In [449]: store Out[449]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 In [450]: store.is_open Out[450]: False # Working with, and automatically closing the store using a context manager In [451]: with pd.HDFStore("store.h5") as store: .....: store.keys() .....: Read/write API# HDFStore supports a top-level API using read_hdf for reading and to_hdf for writing, similar to how read_csv and to_csv work. In [452]: df_tl = pd.DataFrame({"A": list(range(5)), "B": list(range(5))}) In [453]: df_tl.to_hdf("store_tl.h5", "table", append=True) In [454]: pd.read_hdf("store_tl.h5", "table", where=["index>2"]) Out[454]: A B 3 3 3 4 4 4 HDFStore will by default not drop rows that are all missing. This behavior can be changed by setting dropna=True. In [455]: df_with_missing = pd.DataFrame( .....: { .....: "col1": [0, np.nan, 2], .....: "col2": [1, np.nan, np.nan], .....: } .....: ) .....: In [456]: df_with_missing Out[456]: col1 col2 0 0.0 1.0 1 NaN NaN 2 2.0 NaN In [457]: df_with_missing.to_hdf("file.h5", "df_with_missing", format="table", mode="w") In [458]: pd.read_hdf("file.h5", "df_with_missing") Out[458]: col1 col2 0 0.0 1.0 1 NaN NaN 2 2.0 NaN In [459]: df_with_missing.to_hdf( .....: "file.h5", "df_with_missing", format="table", mode="w", dropna=True .....: ) .....: In [460]: pd.read_hdf("file.h5", "df_with_missing") Out[460]: col1 col2 0 0.0 1.0 2 2.0 NaN Fixed format# The examples above show storing using put, which write the HDF5 to PyTables in a fixed array format, called the fixed format. These types of stores are not appendable once written (though you can simply remove them and rewrite). Nor are they queryable; they must be retrieved in their entirety. They also do not support dataframes with non-unique column names. The fixed format stores offer very fast writing and slightly faster reading than table stores. This format is specified by default when using put or to_hdf or by format='fixed' or format='f'. Warning A fixed format will raise a TypeError if you try to retrieve using a where: >>> pd.DataFrame(np.random.randn(10, 2)).to_hdf("test_fixed.h5", "df") >>> pd.read_hdf("test_fixed.h5", "df", where="index>5") TypeError: cannot pass a where specification when reading a fixed format. this store must be selected in its entirety Table format# HDFStore supports another PyTables format on disk, the table format. Conceptually a table is shaped very much like a DataFrame, with rows and columns. A table may be appended to in the same or other sessions. In addition, delete and query type operations are supported. This format is specified by format='table' or format='t' to append or put or to_hdf. This format can be set as an option as well pd.set_option('io.hdf.default_format','table') to enable put/append/to_hdf to by default store in the table format. In [461]: store = pd.HDFStore("store.h5") In [462]: df1 = df[0:4] In [463]: df2 = df[4:] # append data (creates a table automatically) In [464]: store.append("df", df1) In [465]: store.append("df", df2) In [466]: store Out[466]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # select the entire object In [467]: store.select("df") Out[467]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 # the type of stored data In [468]: store.root.df._v_attrs.pandas_type Out[468]: 'frame_table' Note You can also create a table by passing format='table' or format='t' to a put operation. Hierarchical keys# Keys to a store can be specified as a string. These can be in a hierarchical path-name like format (e.g. foo/bar/bah), which will generate a hierarchy of sub-stores (or Groups in PyTables parlance). Keys can be specified without the leading ‘/’ and are always absolute (e.g. ‘foo’ refers to ‘/foo’). Removal operations can remove everything in the sub-store and below, so be careful. In [469]: store.put("foo/bar/bah", df) In [470]: store.append("food/orange", df) In [471]: store.append("food/apple", df) In [472]: store Out[472]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # a list of keys are returned In [473]: store.keys() Out[473]: ['/df', '/food/apple', '/food/orange', '/foo/bar/bah'] # remove all nodes under this level In [474]: store.remove("food") In [475]: store Out[475]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 You can walk through the group hierarchy using the walk method which will yield a tuple for each group key along with the relative keys of its contents. In [476]: for (path, subgroups, subkeys) in store.walk(): .....: for subgroup in subgroups: .....: print("GROUP: {}/{}".format(path, subgroup)) .....: for subkey in subkeys: .....: key = "/".join([path, subkey]) .....: print("KEY: {}".format(key)) .....: print(store.get(key)) .....: GROUP: /foo KEY: /df A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 GROUP: /foo/bar KEY: /foo/bar/bah A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Warning Hierarchical keys cannot be retrieved as dotted (attribute) access as described above for items stored under the root node. In [8]: store.foo.bar.bah AttributeError: 'HDFStore' object has no attribute 'foo' # you can directly access the actual PyTables node but using the root node In [9]: store.root.foo.bar.bah Out[9]: /foo/bar/bah (Group) '' children := ['block0_items' (Array), 'block0_values' (Array), 'axis0' (Array), 'axis1' (Array)] Instead, use explicit string based keys: In [477]: store["foo/bar/bah"] Out[477]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Storing types# Storing mixed types in a table# Storing mixed-dtype data is supported. Strings are stored as a fixed-width using the maximum size of the appended column. Subsequent attempts at appending longer strings will raise a ValueError. Passing min_itemsize={`values`: size} as a parameter to append will set a larger minimum for the string columns. Storing floats, strings, ints, bools, datetime64 are currently supported. For string columns, passing nan_rep = 'nan' to append will change the default nan representation on disk (which converts to/from np.nan), this defaults to nan. In [478]: df_mixed = pd.DataFrame( .....: { .....: "A": np.random.randn(8), .....: "B": np.random.randn(8), .....: "C": np.array(np.random.randn(8), dtype="float32"), .....: "string": "string", .....: "int": 1, .....: "bool": True, .....: "datetime64": pd.Timestamp("20010102"), .....: }, .....: index=list(range(8)), .....: ) .....: In [479]: df_mixed.loc[df_mixed.index[3:5], ["A", "B", "string", "datetime64"]] = np.nan In [480]: store.append("df_mixed", df_mixed, min_itemsize={"values": 50}) In [481]: df_mixed1 = store.select("df_mixed") In [482]: df_mixed1 Out[482]: A B C string int bool datetime64 0 1.778161 -0.898283 -0.263043 string 1 True 2001-01-02 1 -0.913867 -0.218499 -0.639244 string 1 True 2001-01-02 2 -0.030004 1.408028 -0.866305 string 1 True 2001-01-02 3 NaN NaN -0.225250 NaN 1 True NaT 4 NaN NaN -0.890978 NaN 1 True NaT 5 0.081323 0.520995 -0.553839 string 1 True 2001-01-02 6 -0.268494 0.620028 -2.762875 string 1 True 2001-01-02 7 0.168016 0.159416 -1.244763 string 1 True 2001-01-02 In [483]: df_mixed1.dtypes.value_counts() Out[483]: float64 2 float32 1 object 1 int64 1 bool 1 datetime64[ns] 1 dtype: int64 # we have provided a minimum string column size In [484]: store.root.df_mixed.table Out[484]: /df_mixed/table (Table(8,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(2,), dflt=0.0, pos=1), "values_block_1": Float32Col(shape=(1,), dflt=0.0, pos=2), "values_block_2": StringCol(itemsize=50, shape=(1,), dflt=b'', pos=3), "values_block_3": Int64Col(shape=(1,), dflt=0, pos=4), "values_block_4": BoolCol(shape=(1,), dflt=False, pos=5), "values_block_5": Int64Col(shape=(1,), dflt=0, pos=6)} byteorder := 'little' chunkshape := (689,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False} Storing MultiIndex DataFrames# Storing MultiIndex DataFrames as tables is very similar to storing/selecting from homogeneous index DataFrames. In [485]: index = pd.MultiIndex( .....: levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], .....: codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], .....: names=["foo", "bar"], .....: ) .....: In [486]: df_mi = pd.DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"]) In [487]: df_mi Out[487]: A B C foo bar foo one -1.280289 0.692545 -0.536722 two 1.005707 0.296917 0.139796 three -1.083889 0.811865 1.648435 bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 baz two 0.183573 0.145277 0.308146 three -1.043530 -0.708145 1.430905 qux one -0.850136 0.813949 1.508891 two -1.556154 0.187597 1.176488 three -1.246093 -0.002726 -0.444249 In [488]: store.append("df_mi", df_mi) In [489]: store.select("df_mi") Out[489]: A B C foo bar foo one -1.280289 0.692545 -0.536722 two 1.005707 0.296917 0.139796 three -1.083889 0.811865 1.648435 bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 baz two 0.183573 0.145277 0.308146 three -1.043530 -0.708145 1.430905 qux one -0.850136 0.813949 1.508891 two -1.556154 0.187597 1.176488 three -1.246093 -0.002726 -0.444249 # the levels are automatically included as data columns In [490]: store.select("df_mi", "foo=bar") Out[490]: A B C foo bar bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 Note The index keyword is reserved and cannot be use as a level name. Querying# Querying a table# select and delete operations have an optional criterion that can be specified to select/delete only a subset of the data. This allows one to have a very large on-disk table and retrieve only a portion of the data. A query is specified using the Term class under the hood, as a boolean expression. index and columns are supported indexers of DataFrames. if data_columns are specified, these can be used as additional indexers. level name in a MultiIndex, with default name level_0, level_1, … if not provided. Valid comparison operators are: =, ==, !=, >, >=, <, <= Valid boolean expressions are combined with: | : or & : and ( and ) : for grouping These rules are similar to how boolean expressions are used in pandas for indexing. Note = will be automatically expanded to the comparison operator == ~ is the not operator, but can only be used in very limited circumstances If a list/tuple of expressions is passed they will be combined via & The following are valid expressions: 'index >= date' "columns = ['A', 'D']" "columns in ['A', 'D']" 'columns = A' 'columns == A' "~(columns = ['A', 'B'])" 'index > df.index[3] & string = "bar"' '(index > df.index[3] & index <= df.index[6]) | string = "bar"' "ts >= Timestamp('2012-02-01')" "major_axis>=20130101" The indexers are on the left-hand side of the sub-expression: columns, major_axis, ts The right-hand side of the sub-expression (after a comparison operator) can be: functions that will be evaluated, e.g. Timestamp('2012-02-01') strings, e.g. "bar" date-like, e.g. 20130101, or "20130101" lists, e.g. "['A', 'B']" variables that are defined in the local names space, e.g. date Note Passing a string to a query by interpolating it into the query expression is not recommended. Simply assign the string of interest to a variable and use that variable in an expression. For example, do this string = "HolyMoly'" store.select("df", "index == string") instead of this string = "HolyMoly'" store.select('df', f'index == {string}') The latter will not work and will raise a SyntaxError.Note that there’s a single quote followed by a double quote in the string variable. If you must interpolate, use the '%r' format specifier store.select("df", "index == %r" % string) which will quote string. Here are some examples: In [491]: dfq = pd.DataFrame( .....: np.random.randn(10, 4), .....: columns=list("ABCD"), .....: index=pd.date_range("20130101", periods=10), .....: ) .....: In [492]: store.append("dfq", dfq, format="table", data_columns=True) Use boolean expressions, with in-line function evaluation. In [493]: store.select("dfq", "index>pd.Timestamp('20130104') & columns=['A', 'B']") Out[493]: A B 2013-01-05 1.366810 1.073372 2013-01-06 2.119746 -2.628174 2013-01-07 0.337920 -0.634027 2013-01-08 1.053434 1.109090 2013-01-09 -0.772942 -0.269415 2013-01-10 0.048562 -0.285920 Use inline column reference. In [494]: store.select("dfq", where="A>0 or C>0") Out[494]: A B C D 2013-01-01 0.856838 1.491776 0.001283 0.701816 2013-01-02 -1.097917 0.102588 0.661740 0.443531 2013-01-03 0.559313 -0.459055 -1.222598 -0.455304 2013-01-05 1.366810 1.073372 -0.994957 0.755314 2013-01-06 2.119746 -2.628174 -0.089460 -0.133636 2013-01-07 0.337920 -0.634027 0.421107 0.604303 2013-01-08 1.053434 1.109090 -0.367891 -0.846206 2013-01-10 0.048562 -0.285920 1.334100 0.194462 The columns keyword can be supplied to select a list of columns to be returned, this is equivalent to passing a 'columns=list_of_columns_to_filter': In [495]: store.select("df", "columns=['A', 'B']") Out[495]: A B 2000-01-01 -0.398501 -0.677311 2000-01-02 -1.167564 -0.593353 2000-01-03 -0.131959 0.089012 2000-01-04 0.169405 -1.358046 2000-01-05 0.492195 0.076693 2000-01-06 -0.285283 -1.210529 2000-01-07 0.941577 -0.342447 2000-01-08 0.052607 2.093214 start and stop parameters can be specified to limit the total search space. These are in terms of the total number of rows in a table. Note select will raise a ValueError if the query expression has an unknown variable reference. Usually this means that you are trying to select on a column that is not a data_column. select will raise a SyntaxError if the query expression is not valid. Query timedelta64[ns]# You can store and query using the timedelta64[ns] type. Terms can be specified in the format: <float>(<unit>), where float may be signed (and fractional), and unit can be D,s,ms,us,ns for the timedelta. Here’s an example: In [496]: from datetime import timedelta In [497]: dftd = pd.DataFrame( .....: { .....: "A": pd.Timestamp("20130101"), .....: "B": [ .....: pd.Timestamp("20130101") + timedelta(days=i, seconds=10) .....: for i in range(10) .....: ], .....: } .....: ) .....: In [498]: dftd["C"] = dftd["A"] - dftd["B"] In [499]: dftd Out[499]: A B C 0 2013-01-01 2013-01-01 00:00:10 -1 days +23:59:50 1 2013-01-01 2013-01-02 00:00:10 -2 days +23:59:50 2 2013-01-01 2013-01-03 00:00:10 -3 days +23:59:50 3 2013-01-01 2013-01-04 00:00:10 -4 days +23:59:50 4 2013-01-01 2013-01-05 00:00:10 -5 days +23:59:50 5 2013-01-01 2013-01-06 00:00:10 -6 days +23:59:50 6 2013-01-01 2013-01-07 00:00:10 -7 days +23:59:50 7 2013-01-01 2013-01-08 00:00:10 -8 days +23:59:50 8 2013-01-01 2013-01-09 00:00:10 -9 days +23:59:50 9 2013-01-01 2013-01-10 00:00:10 -10 days +23:59:50 In [500]: store.append("dftd", dftd, data_columns=True) In [501]: store.select("dftd", "C<'-3.5D'") Out[501]: A B C 4 2013-01-01 2013-01-05 00:00:10 -5 days +23:59:50 5 2013-01-01 2013-01-06 00:00:10 -6 days +23:59:50 6 2013-01-01 2013-01-07 00:00:10 -7 days +23:59:50 7 2013-01-01 2013-01-08 00:00:10 -8 days +23:59:50 8 2013-01-01 2013-01-09 00:00:10 -9 days +23:59:50 9 2013-01-01 2013-01-10 00:00:10 -10 days +23:59:50 Query MultiIndex# Selecting from a MultiIndex can be achieved by using the name of the level. In [502]: df_mi.index.names Out[502]: FrozenList(['foo', 'bar']) In [503]: store.select("df_mi", "foo=baz and bar=two") Out[503]: A B C foo bar baz two 0.183573 0.145277 0.308146 If the MultiIndex levels names are None, the levels are automatically made available via the level_n keyword with n the level of the MultiIndex you want to select from. In [504]: index = pd.MultiIndex( .....: levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], .....: codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], .....: ) .....: In [505]: df_mi_2 = pd.DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"]) In [506]: df_mi_2 Out[506]: A B C foo one -0.646538 1.210676 -0.315409 two 1.528366 0.376542 0.174490 three 1.247943 -0.742283 0.710400 bar one 0.434128 -1.246384 1.139595 two 1.388668 -0.413554 -0.666287 baz two 0.010150 -0.163820 -0.115305 three 0.216467 0.633720 0.473945 qux one -0.155446 1.287082 0.320201 two -1.256989 0.874920 0.765944 three 0.025557 -0.729782 -0.127439 In [507]: store.append("df_mi_2", df_mi_2) # the levels are automatically included as data columns with keyword level_n In [508]: store.select("df_mi_2", "level_0=foo and level_1=two") Out[508]: A B C foo two 1.528366 0.376542 0.17449 Indexing# You can create/modify an index for a table with create_table_index after data is already in the table (after and append/put operation). Creating a table index is highly encouraged. This will speed your queries a great deal when you use a select with the indexed dimension as the where. Note Indexes are automagically created on the indexables and any data columns you specify. This behavior can be turned off by passing index=False to append. # we have automagically already created an index (in the first section) In [509]: i = store.root.df.table.cols.index.index In [510]: i.optlevel, i.kind Out[510]: (6, 'medium') # change an index by passing new parameters In [511]: store.create_table_index("df", optlevel=9, kind="full") In [512]: i = store.root.df.table.cols.index.index In [513]: i.optlevel, i.kind Out[513]: (9, 'full') Oftentimes when appending large amounts of data to a store, it is useful to turn off index creation for each append, then recreate at the end. In [514]: df_1 = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [515]: df_2 = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [516]: st = pd.HDFStore("appends.h5", mode="w") In [517]: st.append("df", df_1, data_columns=["B"], index=False) In [518]: st.append("df", df_2, data_columns=["B"], index=False) In [519]: st.get_storer("df").table Out[519]: /df/table (Table(20,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2)} byteorder := 'little' chunkshape := (2730,) Then create the index when finished appending. In [520]: st.create_table_index("df", columns=["B"], optlevel=9, kind="full") In [521]: st.get_storer("df").table Out[521]: /df/table (Table(20,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2)} byteorder := 'little' chunkshape := (2730,) autoindex := True colindexes := { "B": Index(9, fullshuffle, zlib(1)).is_csi=True} In [522]: st.close() See here for how to create a completely-sorted-index (CSI) on an existing store. Query via data columns# You can designate (and index) certain columns that you want to be able to perform queries (other than the indexable columns, which you can always query). For instance say you want to perform this common operation, on-disk, and return just the frame that matches this query. You can specify data_columns = True to force all columns to be data_columns. In [523]: df_dc = df.copy() In [524]: df_dc["string"] = "foo" In [525]: df_dc.loc[df_dc.index[4:6], "string"] = np.nan In [526]: df_dc.loc[df_dc.index[7:9], "string"] = "bar" In [527]: df_dc["string2"] = "cool" In [528]: df_dc.loc[df_dc.index[1:3], ["B", "C"]] = 1.0 In [529]: df_dc Out[529]: A B C string string2 2000-01-01 -0.398501 -0.677311 -0.874991 foo cool 2000-01-02 -1.167564 1.000000 1.000000 foo cool 2000-01-03 -0.131959 1.000000 1.000000 foo cool 2000-01-04 0.169405 -1.358046 -0.105563 foo cool 2000-01-05 0.492195 0.076693 0.213685 NaN cool 2000-01-06 -0.285283 -1.210529 -1.408386 NaN cool 2000-01-07 0.941577 -0.342447 0.222031 foo cool 2000-01-08 0.052607 2.093214 1.064908 bar cool # on-disk operations In [530]: store.append("df_dc", df_dc, data_columns=["B", "C", "string", "string2"]) In [531]: store.select("df_dc", where="B > 0") Out[531]: A B C string string2 2000-01-02 -1.167564 1.000000 1.000000 foo cool 2000-01-03 -0.131959 1.000000 1.000000 foo cool 2000-01-05 0.492195 0.076693 0.213685 NaN cool 2000-01-08 0.052607 2.093214 1.064908 bar cool # getting creative In [532]: store.select("df_dc", "B > 0 & C > 0 & string == foo") Out[532]: A B C string string2 2000-01-02 -1.167564 1.0 1.0 foo cool 2000-01-03 -0.131959 1.0 1.0 foo cool # this is in-memory version of this type of selection In [533]: df_dc[(df_dc.B > 0) & (df_dc.C > 0) & (df_dc.string == "foo")] Out[533]: A B C string string2 2000-01-02 -1.167564 1.0 1.0 foo cool 2000-01-03 -0.131959 1.0 1.0 foo cool # we have automagically created this index and the B/C/string/string2 # columns are stored separately as ``PyTables`` columns In [534]: store.root.df_dc.table Out[534]: /df_dc/table (Table(8,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2), "C": Float64Col(shape=(), dflt=0.0, pos=3), "string": StringCol(itemsize=3, shape=(), dflt=b'', pos=4), "string2": StringCol(itemsize=4, shape=(), dflt=b'', pos=5)} byteorder := 'little' chunkshape := (1680,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False, "B": Index(6, mediumshuffle, zlib(1)).is_csi=False, "C": Index(6, mediumshuffle, zlib(1)).is_csi=False, "string": Index(6, mediumshuffle, zlib(1)).is_csi=False, "string2": Index(6, mediumshuffle, zlib(1)).is_csi=False} There is some performance degradation by making lots of columns into data columns, so it is up to the user to designate these. In addition, you cannot change data columns (nor indexables) after the first append/put operation (Of course you can simply read in the data and create a new table!). Iterator# You can pass iterator=True or chunksize=number_in_a_chunk to select and select_as_multiple to return an iterator on the results. The default is 50,000 rows returned in a chunk. In [535]: for df in store.select("df", chunksize=3): .....: print(df) .....: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 A B C 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 A B C 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Note You can also use the iterator with read_hdf which will open, then automatically close the store when finished iterating. for df in pd.read_hdf("store.h5", "df", chunksize=3): print(df) Note, that the chunksize keyword applies to the source rows. So if you are doing a query, then the chunksize will subdivide the total rows in the table and the query applied, returning an iterator on potentially unequal sized chunks. Here is a recipe for generating a query and using it to create equal sized return chunks. In [536]: dfeq = pd.DataFrame({"number": np.arange(1, 11)}) In [537]: dfeq Out[537]: number 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 In [538]: store.append("dfeq", dfeq, data_columns=["number"]) In [539]: def chunks(l, n): .....: return [l[i: i + n] for i in range(0, len(l), n)] .....: In [540]: evens = [2, 4, 6, 8, 10] In [541]: coordinates = store.select_as_coordinates("dfeq", "number=evens") In [542]: for c in chunks(coordinates, 2): .....: print(store.select("dfeq", where=c)) .....: number 1 2 3 4 number 5 6 7 8 number 9 10 Advanced queries# Select a single column# To retrieve a single indexable or data column, use the method select_column. This will, for example, enable you to get the index very quickly. These return a Series of the result, indexed by the row number. These do not currently accept the where selector. In [543]: store.select_column("df_dc", "index") Out[543]: 0 2000-01-01 1 2000-01-02 2 2000-01-03 3 2000-01-04 4 2000-01-05 5 2000-01-06 6 2000-01-07 7 2000-01-08 Name: index, dtype: datetime64[ns] In [544]: store.select_column("df_dc", "string") Out[544]: 0 foo 1 foo 2 foo 3 foo 4 NaN 5 NaN 6 foo 7 bar Name: string, dtype: object Selecting coordinates# Sometimes you want to get the coordinates (a.k.a the index locations) of your query. This returns an Int64Index of the resulting locations. These coordinates can also be passed to subsequent where operations. In [545]: df_coord = pd.DataFrame( .....: np.random.randn(1000, 2), index=pd.date_range("20000101", periods=1000) .....: ) .....: In [546]: store.append("df_coord", df_coord) In [547]: c = store.select_as_coordinates("df_coord", "index > 20020101") In [548]: c Out[548]: Int64Index([732, 733, 734, 735, 736, 737, 738, 739, 740, 741, ... 990, 991, 992, 993, 994, 995, 996, 997, 998, 999], dtype='int64', length=268) In [549]: store.select("df_coord", where=c) Out[549]: 0 1 2002-01-02 0.009035 0.921784 2002-01-03 -1.476563 -1.376375 2002-01-04 1.266731 2.173681 2002-01-05 0.147621 0.616468 2002-01-06 0.008611 2.136001 ... ... ... 2002-09-22 0.781169 -0.791687 2002-09-23 -0.764810 -2.000933 2002-09-24 -0.345662 0.393915 2002-09-25 -0.116661 0.834638 2002-09-26 -1.341780 0.686366 [268 rows x 2 columns] Selecting using a where mask# Sometime your query can involve creating a list of rows to select. Usually this mask would be a resulting index from an indexing operation. This example selects the months of a datetimeindex which are 5. In [550]: df_mask = pd.DataFrame( .....: np.random.randn(1000, 2), index=pd.date_range("20000101", periods=1000) .....: ) .....: In [551]: store.append("df_mask", df_mask) In [552]: c = store.select_column("df_mask", "index") In [553]: where = c[pd.DatetimeIndex(c).month == 5].index In [554]: store.select("df_mask", where=where) Out[554]: 0 1 2000-05-01 -0.386742 -0.977433 2000-05-02 -0.228819 0.471671 2000-05-03 0.337307 1.840494 2000-05-04 0.050249 0.307149 2000-05-05 -0.802947 -0.946730 ... ... ... 2002-05-27 1.605281 1.741415 2002-05-28 -0.804450 -0.715040 2002-05-29 -0.874851 0.037178 2002-05-30 -0.161167 -1.294944 2002-05-31 -0.258463 -0.731969 [93 rows x 2 columns] Storer object# If you want to inspect the stored object, retrieve via get_storer. You could use this programmatically to say get the number of rows in an object. In [555]: store.get_storer("df_dc").nrows Out[555]: 8 Multiple table queries# The methods append_to_multiple and select_as_multiple can perform appending/selecting from multiple tables at once. The idea is to have one table (call it the selector table) that you index most/all of the columns, and perform your queries. The other table(s) are data tables with an index matching the selector table’s index. You can then perform a very fast query on the selector table, yet get lots of data back. This method is similar to having a very wide table, but enables more efficient queries. The append_to_multiple method splits a given single DataFrame into multiple tables according to d, a dictionary that maps the table names to a list of ‘columns’ you want in that table. If None is used in place of a list, that table will have the remaining unspecified columns of the given DataFrame. The argument selector defines which table is the selector table (which you can make queries from). The argument dropna will drop rows from the input DataFrame to ensure tables are synchronized. This means that if a row for one of the tables being written to is entirely np.NaN, that row will be dropped from all tables. If dropna is False, THE USER IS RESPONSIBLE FOR SYNCHRONIZING THE TABLES. Remember that entirely np.Nan rows are not written to the HDFStore, so if you choose to call dropna=False, some tables may have more rows than others, and therefore select_as_multiple may not work or it may return unexpected results. In [556]: df_mt = pd.DataFrame( .....: np.random.randn(8, 6), .....: index=pd.date_range("1/1/2000", periods=8), .....: columns=["A", "B", "C", "D", "E", "F"], .....: ) .....: In [557]: df_mt["foo"] = "bar" In [558]: df_mt.loc[df_mt.index[1], ("A", "B")] = np.nan # you can also create the tables individually In [559]: store.append_to_multiple( .....: {"df1_mt": ["A", "B"], "df2_mt": None}, df_mt, selector="df1_mt" .....: ) .....: In [560]: store Out[560]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # individual tables were created In [561]: store.select("df1_mt") Out[561]: A B 2000-01-01 0.079529 -1.459471 2000-01-02 NaN NaN 2000-01-03 -0.423113 2.314361 2000-01-04 0.756744 -0.792372 2000-01-05 -0.184971 0.170852 2000-01-06 0.678830 0.633974 2000-01-07 0.034973 0.974369 2000-01-08 -2.110103 0.243062 In [562]: store.select("df2_mt") Out[562]: C D E F foo 2000-01-01 -0.596306 -0.910022 -1.057072 -0.864360 bar 2000-01-02 0.477849 0.283128 -2.045700 -0.338206 bar 2000-01-03 -0.033100 -0.965461 -0.001079 -0.351689 bar 2000-01-04 -0.513555 -1.484776 -0.796280 -0.182321 bar 2000-01-05 -0.872407 -1.751515 0.934334 0.938818 bar 2000-01-06 -1.398256 1.347142 -0.029520 0.082738 bar 2000-01-07 -0.755544 0.380786 -1.634116 1.293610 bar 2000-01-08 1.453064 0.500558 -0.574475 0.694324 bar # as a multiple In [563]: store.select_as_multiple( .....: ["df1_mt", "df2_mt"], .....: where=["A>0", "B>0"], .....: selector="df1_mt", .....: ) .....: Out[563]: A B C D E F foo 2000-01-06 0.678830 0.633974 -1.398256 1.347142 -0.029520 0.082738 bar 2000-01-07 0.034973 0.974369 -0.755544 0.380786 -1.634116 1.293610 bar Delete from a table# You can delete from a table selectively by specifying a where. In deleting rows, it is important to understand the PyTables deletes rows by erasing the rows, then moving the following data. Thus deleting can potentially be a very expensive operation depending on the orientation of your data. To get optimal performance, it’s worthwhile to have the dimension you are deleting be the first of the indexables. Data is ordered (on the disk) in terms of the indexables. Here’s a simple use case. You store panel-type data, with dates in the major_axis and ids in the minor_axis. The data is then interleaved like this: date_1 id_1 id_2 . id_n date_2 id_1 . id_n It should be clear that a delete operation on the major_axis will be fairly quick, as one chunk is removed, then the following data moved. On the other hand a delete operation on the minor_axis will be very expensive. In this case it would almost certainly be faster to rewrite the table using a where that selects all but the missing data. Warning Please note that HDF5 DOES NOT RECLAIM SPACE in the h5 files automatically. Thus, repeatedly deleting (or removing nodes) and adding again, WILL TEND TO INCREASE THE FILE SIZE. To repack and clean the file, use ptrepack. Notes & caveats# Compression# PyTables allows the stored data to be compressed. This applies to all kinds of stores, not just tables. Two parameters are used to control compression: complevel and complib. complevel specifies if and how hard data is to be compressed. complevel=0 and complevel=None disables compression and 0<complevel<10 enables compression. complib specifies which compression library to use. If nothing is specified the default library zlib is used. A compression library usually optimizes for either good compression rates or speed and the results will depend on the type of data. Which type of compression to choose depends on your specific needs and data. The list of supported compression libraries: zlib: The default compression library. A classic in terms of compression, achieves good compression rates but is somewhat slow. lzo: Fast compression and decompression. bzip2: Good compression rates. blosc: Fast compression and decompression. Support for alternative blosc compressors: blosc:blosclz This is the default compressor for blosc blosc:lz4: A compact, very popular and fast compressor. blosc:lz4hc: A tweaked version of LZ4, produces better compression ratios at the expense of speed. blosc:snappy: A popular compressor used in many places. blosc:zlib: A classic; somewhat slower than the previous ones, but achieving better compression ratios. blosc:zstd: An extremely well balanced codec; it provides the best compression ratios among the others above, and at reasonably fast speed. If complib is defined as something other than the listed libraries a ValueError exception is issued. Note If the library specified with the complib option is missing on your platform, compression defaults to zlib without further ado. Enable compression for all objects within the file: store_compressed = pd.HDFStore( "store_compressed.h5", complevel=9, complib="blosc:blosclz" ) Or on-the-fly compression (this only applies to tables) in stores where compression is not enabled: store.append("df", df, complib="zlib", complevel=5) ptrepack# PyTables offers better write performance when tables are compressed after they are written, as opposed to turning on compression at the very beginning. You can use the supplied PyTables utility ptrepack. In addition, ptrepack can change compression levels after the fact. ptrepack --chunkshape=auto --propindexes --complevel=9 --complib=blosc in.h5 out.h5 Furthermore ptrepack in.h5 out.h5 will repack the file to allow you to reuse previously deleted space. Alternatively, one can simply remove the file and write again, or use the copy method. Caveats# Warning HDFStore is not-threadsafe for writing. The underlying PyTables only supports concurrent reads (via threading or processes). If you need reading and writing at the same time, you need to serialize these operations in a single thread in a single process. You will corrupt your data otherwise. See the (GH2397) for more information. If you use locks to manage write access between multiple processes, you may want to use fsync() before releasing write locks. For convenience you can use store.flush(fsync=True) to do this for you. Once a table is created columns (DataFrame) are fixed; only exactly the same columns can be appended Be aware that timezones (e.g., pytz.timezone('US/Eastern')) are not necessarily equal across timezone versions. So if data is localized to a specific timezone in the HDFStore using one version of a timezone library and that data is updated with another version, the data will be converted to UTC since these timezones are not considered equal. Either use the same version of timezone library or use tz_convert with the updated timezone definition. Warning PyTables will show a NaturalNameWarning if a column name cannot be used as an attribute selector. Natural identifiers contain only letters, numbers, and underscores, and may not begin with a number. Other identifiers cannot be used in a where clause and are generally a bad idea. DataTypes# HDFStore will map an object dtype to the PyTables underlying dtype. This means the following types are known to work: Type Represents missing values floating : float64, float32, float16 np.nan integer : int64, int32, int8, uint64,uint32, uint8 boolean datetime64[ns] NaT timedelta64[ns] NaT categorical : see the section below object : strings np.nan unicode columns are not supported, and WILL FAIL. Categorical data# You can write data that contains category dtypes to a HDFStore. Queries work the same as if it was an object array. However, the category dtyped data is stored in a more efficient manner. In [564]: dfcat = pd.DataFrame( .....: {"A": pd.Series(list("aabbcdba")).astype("category"), "B": np.random.randn(8)} .....: ) .....: In [565]: dfcat Out[565]: A B 0 a -1.608059 1 a 0.851060 2 b -0.736931 3 b 0.003538 4 c -1.422611 5 d 2.060901 6 b 0.993899 7 a -1.371768 In [566]: dfcat.dtypes Out[566]: A category B float64 dtype: object In [567]: cstore = pd.HDFStore("cats.h5", mode="w") In [568]: cstore.append("dfcat", dfcat, format="table", data_columns=["A"]) In [569]: result = cstore.select("dfcat", where="A in ['b', 'c']") In [570]: result Out[570]: A B 2 b -0.736931 3 b 0.003538 4 c -1.422611 6 b 0.993899 In [571]: result.dtypes Out[571]: A category B float64 dtype: object String columns# min_itemsize The underlying implementation of HDFStore uses a fixed column width (itemsize) for string columns. A string column itemsize is calculated as the maximum of the length of data (for that column) that is passed to the HDFStore, in the first append. Subsequent appends, may introduce a string for a column larger than the column can hold, an Exception will be raised (otherwise you could have a silent truncation of these columns, leading to loss of information). In the future we may relax this and allow a user-specified truncation to occur. Pass min_itemsize on the first table creation to a-priori specify the minimum length of a particular string column. min_itemsize can be an integer, or a dict mapping a column name to an integer. You can pass values as a key to allow all indexables or data_columns to have this min_itemsize. Passing a min_itemsize dict will cause all passed columns to be created as data_columns automatically. Note If you are not passing any data_columns, then the min_itemsize will be the maximum of the length of any string passed In [572]: dfs = pd.DataFrame({"A": "foo", "B": "bar"}, index=list(range(5))) In [573]: dfs Out[573]: A B 0 foo bar 1 foo bar 2 foo bar 3 foo bar 4 foo bar # A and B have a size of 30 In [574]: store.append("dfs", dfs, min_itemsize=30) In [575]: store.get_storer("dfs").table Out[575]: /dfs/table (Table(5,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": StringCol(itemsize=30, shape=(2,), dflt=b'', pos=1)} byteorder := 'little' chunkshape := (963,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False} # A is created as a data_column with a size of 30 # B is size is calculated In [576]: store.append("dfs2", dfs, min_itemsize={"A": 30}) In [577]: store.get_storer("dfs2").table Out[577]: /dfs2/table (Table(5,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": StringCol(itemsize=3, shape=(1,), dflt=b'', pos=1), "A": StringCol(itemsize=30, shape=(), dflt=b'', pos=2)} byteorder := 'little' chunkshape := (1598,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False, "A": Index(6, mediumshuffle, zlib(1)).is_csi=False} nan_rep String columns will serialize a np.nan (a missing value) with the nan_rep string representation. This defaults to the string value nan. You could inadvertently turn an actual nan value into a missing value. In [578]: dfss = pd.DataFrame({"A": ["foo", "bar", "nan"]}) In [579]: dfss Out[579]: A 0 foo 1 bar 2 nan In [580]: store.append("dfss", dfss) In [581]: store.select("dfss") Out[581]: A 0 foo 1 bar 2 NaN # here you need to specify a different nan rep In [582]: store.append("dfss2", dfss, nan_rep="_nan_") In [583]: store.select("dfss2") Out[583]: A 0 foo 1 bar 2 nan External compatibility# HDFStore writes table format objects in specific formats suitable for producing loss-less round trips to pandas objects. For external compatibility, HDFStore can read native PyTables format tables. It is possible to write an HDFStore object that can easily be imported into R using the rhdf5 library (Package website). Create a table format store like this: In [584]: df_for_r = pd.DataFrame( .....: { .....: "first": np.random.rand(100), .....: "second": np.random.rand(100), .....: "class": np.random.randint(0, 2, (100,)), .....: }, .....: index=range(100), .....: ) .....: In [585]: df_for_r.head() Out[585]: first second class 0 0.013480 0.504941 0 1 0.690984 0.898188 1 2 0.510113 0.618748 1 3 0.357698 0.004972 0 4 0.451658 0.012065 1 In [586]: store_export = pd.HDFStore("export.h5") In [587]: store_export.append("df_for_r", df_for_r, data_columns=df_dc.columns) In [588]: store_export Out[588]: <class 'pandas.io.pytables.HDFStore'> File path: export.h5 In R this file can be read into a data.frame object using the rhdf5 library. The following example function reads the corresponding column names and data values from the values and assembles them into a data.frame: # Load values and column names for all datasets from corresponding nodes and # insert them into one data.frame object. library(rhdf5) loadhdf5data <- function(h5File) { listing <- h5ls(h5File) # Find all data nodes, values are stored in *_values and corresponding column # titles in *_items data_nodes <- grep("_values", listing$name) name_nodes <- grep("_items", listing$name) data_paths = paste(listing$group[data_nodes], listing$name[data_nodes], sep = "/") name_paths = paste(listing$group[name_nodes], listing$name[name_nodes], sep = "/") columns = list() for (idx in seq(data_paths)) { # NOTE: matrices returned by h5read have to be transposed to obtain # required Fortran order! data <- data.frame(t(h5read(h5File, data_paths[idx]))) names <- t(h5read(h5File, name_paths[idx])) entry <- data.frame(data) colnames(entry) <- names columns <- append(columns, entry) } data <- data.frame(columns) return(data) } Now you can import the DataFrame into R: > data = loadhdf5data("transfer.hdf5") > head(data) first second class 1 0.4170220047 0.3266449 0 2 0.7203244934 0.5270581 0 3 0.0001143748 0.8859421 1 4 0.3023325726 0.3572698 1 5 0.1467558908 0.9085352 1 6 0.0923385948 0.6233601 1 Note The R function lists the entire HDF5 file’s contents and assembles the data.frame object from all matching nodes, so use this only as a starting point if you have stored multiple DataFrame objects to a single HDF5 file. Performance# tables format come with a writing performance penalty as compared to fixed stores. The benefit is the ability to append/delete and query (potentially very large amounts of data). Write times are generally longer as compared with regular stores. Query times can be quite fast, especially on an indexed axis. You can pass chunksize=<int> to append, specifying the write chunksize (default is 50000). This will significantly lower your memory usage on writing. You can pass expectedrows=<int> to the first append, to set the TOTAL number of rows that PyTables will expect. This will optimize read/write performance. Duplicate rows can be written to tables, but are filtered out in selection (with the last items being selected; thus a table is unique on major, minor pairs) A PerformanceWarning will be raised if you are attempting to store types that will be pickled by PyTables (rather than stored as endemic types). See Here for more information and some solutions. Feather# Feather provides binary columnar serialization for data frames. It is designed to make reading and writing data frames efficient, and to make sharing data across data analysis languages easy. Feather is designed to faithfully serialize and de-serialize DataFrames, supporting all of the pandas dtypes, including extension dtypes such as categorical and datetime with tz. Several caveats: The format will NOT write an Index, or MultiIndex for the DataFrame and will raise an error if a non-default one is provided. You can .reset_index() to store the index or .reset_index(drop=True) to ignore it. Duplicate column names and non-string columns names are not supported Actual Python objects in object dtype columns are not supported. These will raise a helpful error message on an attempt at serialization. See the Full Documentation. In [589]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(3, 6).astype("u1"), .....: "d": np.arange(4.0, 7.0, dtype="float64"), .....: "e": [True, False, True], .....: "f": pd.Categorical(list("abc")), .....: "g": pd.date_range("20130101", periods=3), .....: "h": pd.date_range("20130101", periods=3, tz="US/Eastern"), .....: "i": pd.date_range("20130101", periods=3, freq="ns"), .....: } .....: ) .....: In [590]: df Out[590]: a b c ... g h i 0 a 1 3 ... 2013-01-01 2013-01-01 00:00:00-05:00 2013-01-01 00:00:00.000000000 1 b 2 4 ... 2013-01-02 2013-01-02 00:00:00-05:00 2013-01-01 00:00:00.000000001 2 c 3 5 ... 2013-01-03 2013-01-03 00:00:00-05:00 2013-01-01 00:00:00.000000002 [3 rows x 9 columns] In [591]: df.dtypes Out[591]: a object b int64 c uint8 d float64 e bool f category g datetime64[ns] h datetime64[ns, US/Eastern] i datetime64[ns] dtype: object Write to a feather file. In [592]: df.to_feather("example.feather") Read from a feather file. In [593]: result = pd.read_feather("example.feather") In [594]: result Out[594]: a b c ... g h i 0 a 1 3 ... 2013-01-01 2013-01-01 00:00:00-05:00 2013-01-01 00:00:00.000000000 1 b 2 4 ... 2013-01-02 2013-01-02 00:00:00-05:00 2013-01-01 00:00:00.000000001 2 c 3 5 ... 2013-01-03 2013-01-03 00:00:00-05:00 2013-01-01 00:00:00.000000002 [3 rows x 9 columns] # we preserve dtypes In [595]: result.dtypes Out[595]: a object b int64 c uint8 d float64 e bool f category g datetime64[ns] h datetime64[ns, US/Eastern] i datetime64[ns] dtype: object Parquet# Apache Parquet provides a partitioned binary columnar serialization for data frames. It is designed to make reading and writing data frames efficient, and to make sharing data across data analysis languages easy. Parquet can use a variety of compression techniques to shrink the file size as much as possible while still maintaining good read performance. Parquet is designed to faithfully serialize and de-serialize DataFrame s, supporting all of the pandas dtypes, including extension dtypes such as datetime with tz. Several caveats. Duplicate column names and non-string columns names are not supported. The pyarrow engine always writes the index to the output, but fastparquet only writes non-default indexes. This extra column can cause problems for non-pandas consumers that are not expecting it. You can force including or omitting indexes with the index argument, regardless of the underlying engine. Index level names, if specified, must be strings. In the pyarrow engine, categorical dtypes for non-string types can be serialized to parquet, but will de-serialize as their primitive dtype. The pyarrow engine preserves the ordered flag of categorical dtypes with string types. fastparquet does not preserve the ordered flag. Non supported types include Interval and actual Python object types. These will raise a helpful error message on an attempt at serialization. Period type is supported with pyarrow >= 0.16.0. The pyarrow engine preserves extension data types such as the nullable integer and string data type (requiring pyarrow >= 0.16.0, and requiring the extension type to implement the needed protocols, see the extension types documentation). You can specify an engine to direct the serialization. This can be one of pyarrow, or fastparquet, or auto. If the engine is NOT specified, then the pd.options.io.parquet.engine option is checked; if this is also auto, then pyarrow is tried, and falling back to fastparquet. See the documentation for pyarrow and fastparquet. Note These engines are very similar and should read/write nearly identical parquet format files. pyarrow>=8.0.0 supports timedelta data, fastparquet>=0.1.4 supports timezone aware datetimes. These libraries differ by having different underlying dependencies (fastparquet by using numba, while pyarrow uses a c-library). In [596]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(3, 6).astype("u1"), .....: "d": np.arange(4.0, 7.0, dtype="float64"), .....: "e": [True, False, True], .....: "f": pd.date_range("20130101", periods=3), .....: "g": pd.date_range("20130101", periods=3, tz="US/Eastern"), .....: "h": pd.Categorical(list("abc")), .....: "i": pd.Categorical(list("abc"), ordered=True), .....: } .....: ) .....: In [597]: df Out[597]: a b c d e f g h i 0 a 1 3 4.0 True 2013-01-01 2013-01-01 00:00:00-05:00 a a 1 b 2 4 5.0 False 2013-01-02 2013-01-02 00:00:00-05:00 b b 2 c 3 5 6.0 True 2013-01-03 2013-01-03 00:00:00-05:00 c c In [598]: df.dtypes Out[598]: a object b int64 c uint8 d float64 e bool f datetime64[ns] g datetime64[ns, US/Eastern] h category i category dtype: object Write to a parquet file. In [599]: df.to_parquet("example_pa.parquet", engine="pyarrow") In [600]: df.to_parquet("example_fp.parquet", engine="fastparquet") Read from a parquet file. In [601]: result = pd.read_parquet("example_fp.parquet", engine="fastparquet") In [602]: result = pd.read_parquet("example_pa.parquet", engine="pyarrow") In [603]: result.dtypes Out[603]: a object b int64 c uint8 d float64 e bool f datetime64[ns] g datetime64[ns, US/Eastern] h category i category dtype: object Read only certain columns of a parquet file. In [604]: result = pd.read_parquet( .....: "example_fp.parquet", .....: engine="fastparquet", .....: columns=["a", "b"], .....: ) .....: In [605]: result = pd.read_parquet( .....: "example_pa.parquet", .....: engine="pyarrow", .....: columns=["a", "b"], .....: ) .....: In [606]: result.dtypes Out[606]: a object b int64 dtype: object Handling indexes# Serializing a DataFrame to parquet may include the implicit index as one or more columns in the output file. Thus, this code: In [607]: df = pd.DataFrame({"a": [1, 2], "b": [3, 4]}) In [608]: df.to_parquet("test.parquet", engine="pyarrow") creates a parquet file with three columns if you use pyarrow for serialization: a, b, and __index_level_0__. If you’re using fastparquet, the index may or may not be written to the file. This unexpected extra column causes some databases like Amazon Redshift to reject the file, because that column doesn’t exist in the target table. If you want to omit a dataframe’s indexes when writing, pass index=False to to_parquet(): In [609]: df.to_parquet("test.parquet", index=False) This creates a parquet file with just the two expected columns, a and b. If your DataFrame has a custom index, you won’t get it back when you load this file into a DataFrame. Passing index=True will always write the index, even if that’s not the underlying engine’s default behavior. Partitioning Parquet files# Parquet supports partitioning of data based on the values of one or more columns. In [610]: df = pd.DataFrame({"a": [0, 0, 1, 1], "b": [0, 1, 0, 1]}) In [611]: df.to_parquet(path="test", engine="pyarrow", partition_cols=["a"], compression=None) The path specifies the parent directory to which data will be saved. The partition_cols are the column names by which the dataset will be partitioned. Columns are partitioned in the order they are given. The partition splits are determined by the unique values in the partition columns. The above example creates a partitioned dataset that may look like: test ├── a=0 │ ├── 0bac803e32dc42ae83fddfd029cbdebc.parquet │ └── ... └── a=1 ├── e6ab24a4f45147b49b54a662f0c412a3.parquet └── ... ORC# New in version 1.0.0. Similar to the parquet format, the ORC Format is a binary columnar serialization for data frames. It is designed to make reading data frames efficient. pandas provides both the reader and the writer for the ORC format, read_orc() and to_orc(). This requires the pyarrow library. Warning It is highly recommended to install pyarrow using conda due to some issues occurred by pyarrow. to_orc() requires pyarrow>=7.0.0. read_orc() and to_orc() are not supported on Windows yet, you can find valid environments on install optional dependencies. For supported dtypes please refer to supported ORC features in Arrow. Currently timezones in datetime columns are not preserved when a dataframe is converted into ORC files. In [612]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(4.0, 7.0, dtype="float64"), .....: "d": [True, False, True], .....: "e": pd.date_range("20130101", periods=3), .....: } .....: ) .....: In [613]: df Out[613]: a b c d e 0 a 1 4.0 True 2013-01-01 1 b 2 5.0 False 2013-01-02 2 c 3 6.0 True 2013-01-03 In [614]: df.dtypes Out[614]: a object b int64 c float64 d bool e datetime64[ns] dtype: object Write to an orc file. In [615]: df.to_orc("example_pa.orc", engine="pyarrow") Read from an orc file. In [616]: result = pd.read_orc("example_pa.orc") In [617]: result.dtypes Out[617]: a object b int64 c float64 d bool e datetime64[ns] dtype: object Read only certain columns of an orc file. In [618]: result = pd.read_orc( .....: "example_pa.orc", .....: columns=["a", "b"], .....: ) .....: In [619]: result.dtypes Out[619]: a object b int64 dtype: object SQL queries# The pandas.io.sql module provides a collection of query wrappers to both facilitate data retrieval and to reduce dependency on DB-specific API. Database abstraction is provided by SQLAlchemy if installed. In addition you will need a driver library for your database. Examples of such drivers are psycopg2 for PostgreSQL or pymysql for MySQL. For SQLite this is included in Python’s standard library by default. You can find an overview of supported drivers for each SQL dialect in the SQLAlchemy docs. If SQLAlchemy is not installed, a fallback is only provided for sqlite (and for mysql for backwards compatibility, but this is deprecated and will be removed in a future version). This mode requires a Python database adapter which respect the Python DB-API. See also some cookbook examples for some advanced strategies. The key functions are: read_sql_table(table_name, con[, schema, ...]) Read SQL database table into a DataFrame. read_sql_query(sql, con[, index_col, ...]) Read SQL query into a DataFrame. read_sql(sql, con[, index_col, ...]) Read SQL query or database table into a DataFrame. DataFrame.to_sql(name, con[, schema, ...]) Write records stored in a DataFrame to a SQL database. Note The function read_sql() is a convenience wrapper around read_sql_table() and read_sql_query() (and for backward compatibility) and will delegate to specific function depending on the provided input (database table name or sql query). Table names do not need to be quoted if they have special characters. In the following example, we use the SQlite SQL database engine. You can use a temporary SQLite database where data are stored in “memory”. To connect with SQLAlchemy you use the create_engine() function to create an engine object from database URI. You only need to create the engine once per database you are connecting to. For more information on create_engine() and the URI formatting, see the examples below and the SQLAlchemy documentation In [620]: from sqlalchemy import create_engine # Create your engine. In [621]: engine = create_engine("sqlite:///:memory:") If you want to manage your own connections you can pass one of those instead. The example below opens a connection to the database using a Python context manager that automatically closes the connection after the block has completed. See the SQLAlchemy docs for an explanation of how the database connection is handled. with engine.connect() as conn, conn.begin(): data = pd.read_sql_table("data", conn) Warning When you open a connection to a database you are also responsible for closing it. Side effects of leaving a connection open may include locking the database or other breaking behaviour. Writing DataFrames# Assuming the following data is in a DataFrame data, we can insert it into the database using to_sql(). id Date Col_1 Col_2 Col_3 26 2012-10-18 X 25.7 True 42 2012-10-19 Y -12.4 False 63 2012-10-20 Z 5.73 True In [622]: import datetime In [623]: c = ["id", "Date", "Col_1", "Col_2", "Col_3"] In [624]: d = [ .....: (26, datetime.datetime(2010, 10, 18), "X", 27.5, True), .....: (42, datetime.datetime(2010, 10, 19), "Y", -12.5, False), .....: (63, datetime.datetime(2010, 10, 20), "Z", 5.73, True), .....: ] .....: In [625]: data = pd.DataFrame(d, columns=c) In [626]: data Out[626]: id Date Col_1 Col_2 Col_3 0 26 2010-10-18 X 27.50 True 1 42 2010-10-19 Y -12.50 False 2 63 2010-10-20 Z 5.73 True In [627]: data.to_sql("data", engine) Out[627]: 3 With some databases, writing large DataFrames can result in errors due to packet size limitations being exceeded. This can be avoided by setting the chunksize parameter when calling to_sql. For example, the following writes data to the database in batches of 1000 rows at a time: In [628]: data.to_sql("data_chunked", engine, chunksize=1000) Out[628]: 3 SQL data types# to_sql() will try to map your data to an appropriate SQL data type based on the dtype of the data. When you have columns of dtype object, pandas will try to infer the data type. You can always override the default type by specifying the desired SQL type of any of the columns by using the dtype argument. This argument needs a dictionary mapping column names to SQLAlchemy types (or strings for the sqlite3 fallback mode). For example, specifying to use the sqlalchemy String type instead of the default Text type for string columns: In [629]: from sqlalchemy.types import String In [630]: data.to_sql("data_dtype", engine, dtype={"Col_1": String}) Out[630]: 3 Note Due to the limited support for timedelta’s in the different database flavors, columns with type timedelta64 will be written as integer values as nanoseconds to the database and a warning will be raised. Note Columns of category dtype will be converted to the dense representation as you would get with np.asarray(categorical) (e.g. for string categories this gives an array of strings). Because of this, reading the database table back in does not generate a categorical. Datetime data types# Using SQLAlchemy, to_sql() is capable of writing datetime data that is timezone naive or timezone aware. However, the resulting data stored in the database ultimately depends on the supported data type for datetime data of the database system being used. The following table lists supported data types for datetime data for some common databases. Other database dialects may have different data types for datetime data. Database SQL Datetime Types Timezone Support SQLite TEXT No MySQL TIMESTAMP or DATETIME No PostgreSQL TIMESTAMP or TIMESTAMP WITH TIME ZONE Yes When writing timezone aware data to databases that do not support timezones, the data will be written as timezone naive timestamps that are in local time with respect to the timezone. read_sql_table() is also capable of reading datetime data that is timezone aware or naive. When reading TIMESTAMP WITH TIME ZONE types, pandas will convert the data to UTC. Insertion method# The parameter method controls the SQL insertion clause used. Possible values are: None: Uses standard SQL INSERT clause (one per row). 'multi': Pass multiple values in a single INSERT clause. It uses a special SQL syntax not supported by all backends. This usually provides better performance for analytic databases like Presto and Redshift, but has worse performance for traditional SQL backend if the table contains many columns. For more information check the SQLAlchemy documentation. callable with signature (pd_table, conn, keys, data_iter): This can be used to implement a more performant insertion method based on specific backend dialect features. Example of a callable using PostgreSQL COPY clause: # Alternative to_sql() *method* for DBs that support COPY FROM import csv from io import StringIO def psql_insert_copy(table, conn, keys, data_iter): """ Execute SQL statement inserting data Parameters ---------- table : pandas.io.sql.SQLTable conn : sqlalchemy.engine.Engine or sqlalchemy.engine.Connection keys : list of str Column names data_iter : Iterable that iterates the values to be inserted """ # gets a DBAPI connection that can provide a cursor dbapi_conn = conn.connection with dbapi_conn.cursor() as cur: s_buf = StringIO() writer = csv.writer(s_buf) writer.writerows(data_iter) s_buf.seek(0) columns = ', '.join(['"{}"'.format(k) for k in keys]) if table.schema: table_name = '{}.{}'.format(table.schema, table.name) else: table_name = table.name sql = 'COPY {} ({}) FROM STDIN WITH CSV'.format( table_name, columns) cur.copy_expert(sql=sql, file=s_buf) Reading tables# read_sql_table() will read a database table given the table name and optionally a subset of columns to read. Note In order to use read_sql_table(), you must have the SQLAlchemy optional dependency installed. In [631]: pd.read_sql_table("data", engine) Out[631]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 X 27.50 True 1 1 42 2010-10-19 Y -12.50 False 2 2 63 2010-10-20 Z 5.73 True Note Note that pandas infers column dtypes from query outputs, and not by looking up data types in the physical database schema. For example, assume userid is an integer column in a table. Then, intuitively, select userid ... will return integer-valued series, while select cast(userid as text) ... will return object-valued (str) series. Accordingly, if the query output is empty, then all resulting columns will be returned as object-valued (since they are most general). If you foresee that your query will sometimes generate an empty result, you may want to explicitly typecast afterwards to ensure dtype integrity. You can also specify the name of the column as the DataFrame index, and specify a subset of columns to be read. In [632]: pd.read_sql_table("data", engine, index_col="id") Out[632]: index Date Col_1 Col_2 Col_3 id 26 0 2010-10-18 X 27.50 True 42 1 2010-10-19 Y -12.50 False 63 2 2010-10-20 Z 5.73 True In [633]: pd.read_sql_table("data", engine, columns=["Col_1", "Col_2"]) Out[633]: Col_1 Col_2 0 X 27.50 1 Y -12.50 2 Z 5.73 And you can explicitly force columns to be parsed as dates: In [634]: pd.read_sql_table("data", engine, parse_dates=["Date"]) Out[634]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 X 27.50 True 1 1 42 2010-10-19 Y -12.50 False 2 2 63 2010-10-20 Z 5.73 True If needed you can explicitly specify a format string, or a dict of arguments to pass to pandas.to_datetime(): pd.read_sql_table("data", engine, parse_dates={"Date": "%Y-%m-%d"}) pd.read_sql_table( "data", engine, parse_dates={"Date": {"format": "%Y-%m-%d %H:%M:%S"}}, ) You can check if a table exists using has_table() Schema support# Reading from and writing to different schema’s is supported through the schema keyword in the read_sql_table() and to_sql() functions. Note however that this depends on the database flavor (sqlite does not have schema’s). For example: df.to_sql("table", engine, schema="other_schema") pd.read_sql_table("table", engine, schema="other_schema") Querying# You can query using raw SQL in the read_sql_query() function. In this case you must use the SQL variant appropriate for your database. When using SQLAlchemy, you can also pass SQLAlchemy Expression language constructs, which are database-agnostic. In [635]: pd.read_sql_query("SELECT * FROM data", engine) Out[635]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 00:00:00.000000 X 27.50 1 1 1 42 2010-10-19 00:00:00.000000 Y -12.50 0 2 2 63 2010-10-20 00:00:00.000000 Z 5.73 1 Of course, you can specify a more “complex” query. In [636]: pd.read_sql_query("SELECT id, Col_1, Col_2 FROM data WHERE id = 42;", engine) Out[636]: id Col_1 Col_2 0 42 Y -12.5 The read_sql_query() function supports a chunksize argument. Specifying this will return an iterator through chunks of the query result: In [637]: df = pd.DataFrame(np.random.randn(20, 3), columns=list("abc")) In [638]: df.to_sql("data_chunks", engine, index=False) Out[638]: 20 In [639]: for chunk in pd.read_sql_query("SELECT * FROM data_chunks", engine, chunksize=5): .....: print(chunk) .....: a b c 0 0.070470 0.901320 0.937577 1 0.295770 1.420548 -0.005283 2 -1.518598 -0.730065 0.226497 3 -2.061465 0.632115 0.853619 4 2.719155 0.139018 0.214557 a b c 0 -1.538924 -0.366973 -0.748801 1 -0.478137 -1.559153 -3.097759 2 -2.320335 -0.221090 0.119763 3 0.608228 1.064810 -0.780506 4 -2.736887 0.143539 1.170191 a b c 0 -1.573076 0.075792 -1.722223 1 -0.774650 0.803627 0.221665 2 0.584637 0.147264 1.057825 3 -0.284136 0.912395 1.552808 4 0.189376 -0.109830 0.539341 a b c 0 0.592591 -0.155407 -1.356475 1 0.833837 1.524249 1.606722 2 -0.029487 -0.051359 1.700152 3 0.921484 -0.926347 0.979818 4 0.182380 -0.186376 0.049820 You can also run a plain query without creating a DataFrame with execute(). This is useful for queries that don’t return values, such as INSERT. This is functionally equivalent to calling execute on the SQLAlchemy engine or db connection object. Again, you must use the SQL syntax variant appropriate for your database. from pandas.io import sql sql.execute("SELECT * FROM table_name", engine) sql.execute( "INSERT INTO table_name VALUES(?, ?, ?)", engine, params=[("id", 1, 12.2, True)] ) Engine connection examples# To connect with SQLAlchemy you use the create_engine() function to create an engine object from database URI. You only need to create the engine once per database you are connecting to. from sqlalchemy import create_engine engine = create_engine("postgresql://scott:[email protected]:5432/mydatabase") engine = create_engine("mysql+mysqldb://scott:[email protected]/foo") engine = create_engine("oracle://scott:[email protected]:1521/sidname") engine = create_engine("mssql+pyodbc://mydsn") # sqlite://<nohostname>/<path> # where <path> is relative: engine = create_engine("sqlite:///foo.db") # or absolute, starting with a slash: engine = create_engine("sqlite:////absolute/path/to/foo.db") For more information see the examples the SQLAlchemy documentation Advanced SQLAlchemy queries# You can use SQLAlchemy constructs to describe your query. Use sqlalchemy.text() to specify query parameters in a backend-neutral way In [640]: import sqlalchemy as sa In [641]: pd.read_sql( .....: sa.text("SELECT * FROM data where Col_1=:col1"), engine, params={"col1": "X"} .....: ) .....: Out[641]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 00:00:00.000000 X 27.5 1 If you have an SQLAlchemy description of your database you can express where conditions using SQLAlchemy expressions In [642]: metadata = sa.MetaData() In [643]: data_table = sa.Table( .....: "data", .....: metadata, .....: sa.Column("index", sa.Integer), .....: sa.Column("Date", sa.DateTime), .....: sa.Column("Col_1", sa.String), .....: sa.Column("Col_2", sa.Float), .....: sa.Column("Col_3", sa.Boolean), .....: ) .....: In [644]: pd.read_sql(sa.select([data_table]).where(data_table.c.Col_3 is True), engine) Out[644]: Empty DataFrame Columns: [index, Date, Col_1, Col_2, Col_3] Index: [] You can combine SQLAlchemy expressions with parameters passed to read_sql() using sqlalchemy.bindparam() In [645]: import datetime as dt In [646]: expr = sa.select([data_table]).where(data_table.c.Date > sa.bindparam("date")) In [647]: pd.read_sql(expr, engine, params={"date": dt.datetime(2010, 10, 18)}) Out[647]: index Date Col_1 Col_2 Col_3 0 1 2010-10-19 Y -12.50 False 1 2 2010-10-20 Z 5.73 True Sqlite fallback# The use of sqlite is supported without using SQLAlchemy. This mode requires a Python database adapter which respect the Python DB-API. You can create connections like so: import sqlite3 con = sqlite3.connect(":memory:") And then issue the following queries: data.to_sql("data", con) pd.read_sql_query("SELECT * FROM data", con) Google BigQuery# Warning Starting in 0.20.0, pandas has split off Google BigQuery support into the separate package pandas-gbq. You can pip install pandas-gbq to get it. The pandas-gbq package provides functionality to read/write from Google BigQuery. pandas integrates with this external package. if pandas-gbq is installed, you can use the pandas methods pd.read_gbq and DataFrame.to_gbq, which will call the respective functions from pandas-gbq. Full documentation can be found here. Stata format# Writing to stata format# The method to_stata() will write a DataFrame into a .dta file. The format version of this file is always 115 (Stata 12). In [648]: df = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [649]: df.to_stata("stata.dta") Stata data files have limited data type support; only strings with 244 or fewer characters, int8, int16, int32, float32 and float64 can be stored in .dta files. Additionally, Stata reserves certain values to represent missing data. Exporting a non-missing value that is outside of the permitted range in Stata for a particular data type will retype the variable to the next larger size. For example, int8 values are restricted to lie between -127 and 100 in Stata, and so variables with values above 100 will trigger a conversion to int16. nan values in floating points data types are stored as the basic missing data type (. in Stata). Note It is not possible to export missing data values for integer data types. The Stata writer gracefully handles other data types including int64, bool, uint8, uint16, uint32 by casting to the smallest supported type that can represent the data. For example, data with a type of uint8 will be cast to int8 if all values are less than 100 (the upper bound for non-missing int8 data in Stata), or, if values are outside of this range, the variable is cast to int16. Warning Conversion from int64 to float64 may result in a loss of precision if int64 values are larger than 2**53. Warning StataWriter and to_stata() only support fixed width strings containing up to 244 characters, a limitation imposed by the version 115 dta file format. Attempting to write Stata dta files with strings longer than 244 characters raises a ValueError. Reading from Stata format# The top-level function read_stata will read a dta file and return either a DataFrame or a StataReader that can be used to read the file incrementally. In [650]: pd.read_stata("stata.dta") Out[650]: index A B 0 0 -1.690072 0.405144 1 1 -1.511309 -1.531396 2 2 0.572698 -1.106845 3 3 -1.185859 0.174564 4 4 0.603797 -1.796129 5 5 -0.791679 1.173795 6 6 -0.277710 1.859988 7 7 -0.258413 1.251808 8 8 1.443262 0.441553 9 9 1.168163 -2.054946 Specifying a chunksize yields a StataReader instance that can be used to read chunksize lines from the file at a time. The StataReader object can be used as an iterator. In [651]: with pd.read_stata("stata.dta", chunksize=3) as reader: .....: for df in reader: .....: print(df.shape) .....: (3, 3) (3, 3) (3, 3) (1, 3) For more fine-grained control, use iterator=True and specify chunksize with each call to read(). In [652]: with pd.read_stata("stata.dta", iterator=True) as reader: .....: chunk1 = reader.read(5) .....: chunk2 = reader.read(5) .....: Currently the index is retrieved as a column. The parameter convert_categoricals indicates whether value labels should be read and used to create a Categorical variable from them. Value labels can also be retrieved by the function value_labels, which requires read() to be called before use. The parameter convert_missing indicates whether missing value representations in Stata should be preserved. If False (the default), missing values are represented as np.nan. If True, missing values are represented using StataMissingValue objects, and columns containing missing values will have object data type. Note read_stata() and StataReader support .dta formats 113-115 (Stata 10-12), 117 (Stata 13), and 118 (Stata 14). Note Setting preserve_dtypes=False will upcast to the standard pandas data types: int64 for all integer types and float64 for floating point data. By default, the Stata data types are preserved when importing. Categorical data# Categorical data can be exported to Stata data files as value labeled data. The exported data consists of the underlying category codes as integer data values and the categories as value labels. Stata does not have an explicit equivalent to a Categorical and information about whether the variable is ordered is lost when exporting. Warning Stata only supports string value labels, and so str is called on the categories when exporting data. Exporting Categorical variables with non-string categories produces a warning, and can result a loss of information if the str representations of the categories are not unique. Labeled data can similarly be imported from Stata data files as Categorical variables using the keyword argument convert_categoricals (True by default). The keyword argument order_categoricals (True by default) determines whether imported Categorical variables are ordered. Note When importing categorical data, the values of the variables in the Stata data file are not preserved since Categorical variables always use integer data types between -1 and n-1 where n is the number of categories. If the original values in the Stata data file are required, these can be imported by setting convert_categoricals=False, which will import original data (but not the variable labels). The original values can be matched to the imported categorical data since there is a simple mapping between the original Stata data values and the category codes of imported Categorical variables: missing values are assigned code -1, and the smallest original value is assigned 0, the second smallest is assigned 1 and so on until the largest original value is assigned the code n-1. Note Stata supports partially labeled series. These series have value labels for some but not all data values. Importing a partially labeled series will produce a Categorical with string categories for the values that are labeled and numeric categories for values with no label. SAS formats# The top-level function read_sas() can read (but not write) SAS XPORT (.xpt) and (since v0.18.0) SAS7BDAT (.sas7bdat) format files. SAS files only contain two value types: ASCII text and floating point values (usually 8 bytes but sometimes truncated). For xport files, there is no automatic type conversion to integers, dates, or categoricals. For SAS7BDAT files, the format codes may allow date variables to be automatically converted to dates. By default the whole file is read and returned as a DataFrame. Specify a chunksize or use iterator=True to obtain reader objects (XportReader or SAS7BDATReader) for incrementally reading the file. The reader objects also have attributes that contain additional information about the file and its variables. Read a SAS7BDAT file: df = pd.read_sas("sas_data.sas7bdat") Obtain an iterator and read an XPORT file 100,000 lines at a time: def do_something(chunk): pass with pd.read_sas("sas_xport.xpt", chunk=100000) as rdr: for chunk in rdr: do_something(chunk) The specification for the xport file format is available from the SAS web site. No official documentation is available for the SAS7BDAT format. SPSS formats# New in version 0.25.0. The top-level function read_spss() can read (but not write) SPSS SAV (.sav) and ZSAV (.zsav) format files. SPSS files contain column names. By default the whole file is read, categorical columns are converted into pd.Categorical, and a DataFrame with all columns is returned. Specify the usecols parameter to obtain a subset of columns. Specify convert_categoricals=False to avoid converting categorical columns into pd.Categorical. Read an SPSS file: df = pd.read_spss("spss_data.sav") Extract a subset of columns contained in usecols from an SPSS file and avoid converting categorical columns into pd.Categorical: df = pd.read_spss( "spss_data.sav", usecols=["foo", "bar"], convert_categoricals=False, ) More information about the SAV and ZSAV file formats is available here. Other file formats# pandas itself only supports IO with a limited set of file formats that map cleanly to its tabular data model. For reading and writing other file formats into and from pandas, we recommend these packages from the broader community. netCDF# xarray provides data structures inspired by the pandas DataFrame for working with multi-dimensional datasets, with a focus on the netCDF file format and easy conversion to and from pandas. Performance considerations# This is an informal comparison of various IO methods, using pandas 0.24.2. Timings are machine dependent and small differences should be ignored. In [1]: sz = 1000000 In [2]: df = pd.DataFrame({'A': np.random.randn(sz), 'B': [1] * sz}) In [3]: df.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 1000000 entries, 0 to 999999 Data columns (total 2 columns): A 1000000 non-null float64 B 1000000 non-null int64 dtypes: float64(1), int64(1) memory usage: 15.3 MB The following test functions will be used below to compare the performance of several IO methods: import numpy as np import os sz = 1000000 df = pd.DataFrame({"A": np.random.randn(sz), "B": [1] * sz}) sz = 1000000 np.random.seed(42) df = pd.DataFrame({"A": np.random.randn(sz), "B": [1] * sz}) def test_sql_write(df): if os.path.exists("test.sql"): os.remove("test.sql") sql_db = sqlite3.connect("test.sql") df.to_sql(name="test_table", con=sql_db) sql_db.close() def test_sql_read(): sql_db = sqlite3.connect("test.sql") pd.read_sql_query("select * from test_table", sql_db) sql_db.close() def test_hdf_fixed_write(df): df.to_hdf("test_fixed.hdf", "test", mode="w") def test_hdf_fixed_read(): pd.read_hdf("test_fixed.hdf", "test") def test_hdf_fixed_write_compress(df): df.to_hdf("test_fixed_compress.hdf", "test", mode="w", complib="blosc") def test_hdf_fixed_read_compress(): pd.read_hdf("test_fixed_compress.hdf", "test") def test_hdf_table_write(df): df.to_hdf("test_table.hdf", "test", mode="w", format="table") def test_hdf_table_read(): pd.read_hdf("test_table.hdf", "test") def test_hdf_table_write_compress(df): df.to_hdf( "test_table_compress.hdf", "test", mode="w", complib="blosc", format="table" ) def test_hdf_table_read_compress(): pd.read_hdf("test_table_compress.hdf", "test") def test_csv_write(df): df.to_csv("test.csv", mode="w") def test_csv_read(): pd.read_csv("test.csv", index_col=0) def test_feather_write(df): df.to_feather("test.feather") def test_feather_read(): pd.read_feather("test.feather") def test_pickle_write(df): df.to_pickle("test.pkl") def test_pickle_read(): pd.read_pickle("test.pkl") def test_pickle_write_compress(df): df.to_pickle("test.pkl.compress", compression="xz") def test_pickle_read_compress(): pd.read_pickle("test.pkl.compress", compression="xz") def test_parquet_write(df): df.to_parquet("test.parquet") def test_parquet_read(): pd.read_parquet("test.parquet") When writing, the top three functions in terms of speed are test_feather_write, test_hdf_fixed_write and test_hdf_fixed_write_compress. In [4]: %timeit test_sql_write(df) 3.29 s ± 43.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [5]: %timeit test_hdf_fixed_write(df) 19.4 ms ± 560 µs per loop (mean ± std. dev. of 7 runs, 1 loop each) In [6]: %timeit test_hdf_fixed_write_compress(df) 19.6 ms ± 308 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [7]: %timeit test_hdf_table_write(df) 449 ms ± 5.61 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [8]: %timeit test_hdf_table_write_compress(df) 448 ms ± 11.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [9]: %timeit test_csv_write(df) 3.66 s ± 26.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [10]: %timeit test_feather_write(df) 9.75 ms ± 117 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [11]: %timeit test_pickle_write(df) 30.1 ms ± 229 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [12]: %timeit test_pickle_write_compress(df) 4.29 s ± 15.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [13]: %timeit test_parquet_write(df) 67.6 ms ± 706 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) When reading, the top three functions in terms of speed are test_feather_read, test_pickle_read and test_hdf_fixed_read. In [14]: %timeit test_sql_read() 1.77 s ± 17.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [15]: %timeit test_hdf_fixed_read() 19.4 ms ± 436 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [16]: %timeit test_hdf_fixed_read_compress() 19.5 ms ± 222 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [17]: %timeit test_hdf_table_read() 38.6 ms ± 857 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [18]: %timeit test_hdf_table_read_compress() 38.8 ms ± 1.49 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) In [19]: %timeit test_csv_read() 452 ms ± 9.04 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [20]: %timeit test_feather_read() 12.4 ms ± 99.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [21]: %timeit test_pickle_read() 18.4 ms ± 191 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [22]: %timeit test_pickle_read_compress() 915 ms ± 7.48 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [23]: %timeit test_parquet_read() 24.4 ms ± 146 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) The files test.pkl.compress, test.parquet and test.feather took the least space on disk (in bytes). 29519500 Oct 10 06:45 test.csv 16000248 Oct 10 06:45 test.feather 8281983 Oct 10 06:49 test.parquet 16000857 Oct 10 06:47 test.pkl 7552144 Oct 10 06:48 test.pkl.compress 34816000 Oct 10 06:42 test.sql 24009288 Oct 10 06:43 test_fixed.hdf 24009288 Oct 10 06:43 test_fixed_compress.hdf 24458940 Oct 10 06:44 test_table.hdf 24458940 Oct 10 06:44 test_table_compress.hdf
805
1,089
Is there an efficient way to retrieve values from dictionary I have a dictionary of values. This is for a company name. It has 3 keys: {'html_attributions': [], 'result' : {'Address': '123 Street', 'website' :'123street.com' 'status': 'Ok' } I have a dataframe of many dictionaries. I want to loop through each row's dictionary and get the necessary information I want. Currently I am writing for loops to retrieve these information. Is there a more efficient way to retrieve these information? addresses = [] for i in range(len(testing)): try: addresses.append(testing['Results_dict'][i]['result']['Address']) except: addresses.append('No info') What I have works perfectly fine. However I would like something that would be more efficient. Perhaps using the get() method? but I don't know how I can call to get the inside of 'result'.
Try this: def get_address(r): try: return r['result']['Address'] except Exception: return 'No info' addresses = df['Results_dict'].map(get_address) This guards against cases where Result_dict is None, not a dict, or any key along the path way does not exist.
69,400,771
converting only the floats of a column to integer
<p>I have a data-frame:</p> <pre><code>df = pd.DataFrame({&quot;ID&quot;: [1,2,3,4,5,6,7], &quot;value&quot;: [10,10.00,&quot;123JK&quot;,20,10-11,11.00,12.00]}) </code></pre> <pre><code>ID value 1 10 2 10.00 3 123JK 4 20 5 10-11 6 11.00 7 12.00 </code></pre> <p>I want to convert only the floating value to integer such that:</p> <pre><code>ID value 1 10 2 10 3 123JK 4 20 5 10-11 6 11 7 12 </code></pre> <p>I tried following code:</p> <pre><code>df['ID'] = df['ID'].apply(pd.to_numeric, errors='ignore') df['ID'].astype(np.int64,errors='ignore') </code></pre> <p>But It does not convert all the floating value to integer.</p>
69,400,787
"2021-10-01T05:17:41.353000"
3
null
3
95
python|pandas
<p>If need integers only for floats integers like <code>10.0</code> use custom function:</p> <pre><code>def f(x): try: x = pd.to_numeric(x, errors='ignore') if int(x) == x: return int(x) else: return x except: return x df['value'] = df['value'].apply(f) print (df) ID value 0 1 10 1 2 10 2 3 123JK 3 4 20 4 5 10-11 5 6 11 6 7 12 </code></pre>
"2021-10-01T05:19:41.427000"
1
https://pandas.pydata.org/docs/reference/api/pandas.to_numeric.html
pandas.to_numeric# pandas.to_numeric# pandas.to_numeric(arg, errors='raise', downcast=None)[source]# Convert argument to a numeric type. The default return dtype is float64 or int64 depending on the data supplied. Use the downcast parameter If need integers only for floats integers like 10.0 use custom function: def f(x): try: x = pd.to_numeric(x, errors='ignore') if int(x) == x: return int(x) else: return x except: return x df['value'] = df['value'].apply(f) print (df) ID value 0 1 10 1 2 10 2 3 123JK 3 4 20 4 5 10-11 5 6 11 6 7 12 to obtain other dtypes. Please note that precision loss may occur if really large numbers are passed in. Due to the internal limitations of ndarray, if numbers smaller than -9223372036854775808 (np.iinfo(np.int64).min) or larger than 18446744073709551615 (np.iinfo(np.uint64).max) are passed in, it is very likely they will be converted to float so that they can stored in an ndarray. These warnings apply similarly to Series since it internally leverages ndarray. Parameters argscalar, list, tuple, 1-d array, or SeriesArgument to be converted. errors{‘ignore’, ‘raise’, ‘coerce’}, default ‘raise’ If ‘raise’, then invalid parsing will raise an exception. If ‘coerce’, then invalid parsing will be set as NaN. If ‘ignore’, then invalid parsing will return the input. downcaststr, default NoneCan be ‘integer’, ‘signed’, ‘unsigned’, or ‘float’. If not None, and if the data has been successfully cast to a numerical dtype (or if the data was numeric to begin with), downcast that resulting data to the smallest numerical dtype possible according to the following rules: ‘integer’ or ‘signed’: smallest signed int dtype (min.: np.int8) ‘unsigned’: smallest unsigned int dtype (min.: np.uint8) ‘float’: smallest float dtype (min.: np.float32) As this behaviour is separate from the core conversion to numeric values, any errors raised during the downcasting will be surfaced regardless of the value of the ‘errors’ input. In addition, downcasting will only occur if the size of the resulting data’s dtype is strictly larger than the dtype it is to be cast to, so if none of the dtypes checked satisfy that specification, no downcasting will be performed on the data. Returns retNumeric if parsing succeeded. Return type depends on input. Series if Series, otherwise ndarray. See also DataFrame.astypeCast argument to a specified dtype. to_datetimeConvert argument to datetime. to_timedeltaConvert argument to timedelta. numpy.ndarray.astypeCast a numpy array to a specified type. DataFrame.convert_dtypesConvert dtypes. Examples Take separate series and convert to numeric, coercing when told to >>> s = pd.Series(['1.0', '2', -3]) >>> pd.to_numeric(s) 0 1.0 1 2.0 2 -3.0 dtype: float64 >>> pd.to_numeric(s, downcast='float') 0 1.0 1 2.0 2 -3.0 dtype: float32 >>> pd.to_numeric(s, downcast='signed') 0 1 1 2 2 -3 dtype: int8 >>> s = pd.Series(['apple', '1.0', '2', -3]) >>> pd.to_numeric(s, errors='ignore') 0 apple 1 1.0 2 2 3 -3 dtype: object >>> pd.to_numeric(s, errors='coerce') 0 NaN 1 1.0 2 2.0 3 -3.0 dtype: float64 Downcasting of nullable integer and floating dtypes is supported: >>> s = pd.Series([1, 2, 3], dtype="Int64") >>> pd.to_numeric(s, downcast="integer") 0 1 1 2 2 3 dtype: Int8 >>> s = pd.Series([1.0, 2.1, 3.0], dtype="Float64") >>> pd.to_numeric(s, downcast="float") 0 1.0 1 2.1 2 3.0 dtype: Float32
245
649
converting only the floats of a column to integer I have a data-frame: df = pd.DataFrame({"ID": [1,2,3,4,5,6,7], "value": [10,10.00,"123JK",20,10-11,11.00,12.00]}) ID value 1 10 2 10.00 3 123JK 4 20 5 10-11 6 11.00 7 12.00 I want to convert only the floating value to integer such that: ID value 1 10 2 10 3 123JK 4 20 5 10-11 6 11 7 12 I tried following code: df['ID'] = df['ID'].apply(pd.to_numeric, errors='ignore') df['ID'].astype(np.int64,errors='ignore') But It does not convert all the floating value to integer.
If need integers only for floats integers like 10.0 use custom function: def f(x): try: x = pd.to_numeric(x, errors='ignore') if int(x) == x: return int(x) else: return x except: return x df['value'] = df['value'].apply(f) print (df) ID value 0 1 10 1 2 10 2 3 123JK 3 4 20 4 5 10-11 5 6 11 6 7 12
61,886,658
Different output on different machine python code
<p>I'm trying to import multiple csv files into a dictionary. My idea is key is present the name of csv files, and value of each key is the table inside csv as DataFrame type.</p> <p>My code is like this : </p> <pre><code>import pandas as pd data = '.././data/raw/' all_files = [data + 'x.csv', data + 'y.csv'] list_a = [] result_dict = dict() for filename in all_files: df = pd.read_csv(filename, index_col=None, header=0, encoding='mac_roman') key = filename.split('/')[1] result_dict[key]=df # print(result_dict) def get_dataframe(name): dataframe = result_dict.get(name) return dataframe m_taiin =get_dataframe('x.csv') type(m_taiin) print(isinstance(m_taiin,pd.DataFrame)) </code></pre> <p>But when I run this code on my Macbook - Python3.7 and my Ubuntu 16.04 - Python3.6 has result True.</p> <p>But when I run on Arch Linux - Python 3.7 my result is False. The value is NoneType not DataFrame. </p> <p>I don't know which is the problem here.</p>
61,886,819
"2020-05-19T08:33:29.483000"
2
null
1
97
python|pandas
<p>Try like this in for loop ...</p> <pre><code>key = filename.split('/')[-1] </code></pre>
"2020-05-19T08:41:59.360000"
1
https://pandas.pydata.org/docs/user_guide/io.html
IO tools (text, CSV, HDF5, …)# IO tools (text, CSV, HDF5, …)# The pandas I/O API is a set of top level reader functions accessed like pandas.read_csv() that generally return a pandas object. The corresponding writer functions are object methods that are accessed like DataFrame.to_csv(). Below is a table containing available readers and writers. Format Type Data Description Reader Writer text CSV read_csv to_csv text Fixed-Width Text File read_fwf text JSON read_json to_json text HTML read_html to_html text LaTeX Styler.to_latex text XML read_xml to_xml text Local clipboard read_clipboard to_clipboard binary MS Excel read_excel to_excel binary OpenDocument read_excel binary HDF5 Format read_hdf to_hdf binary Feather Format read_feather to_feather binary Parquet Format read_parquet to_parquet binary ORC Format read_orc to_orc binary Stata read_stata to_stata binary SAS read_sas binary SPSS read_spss binary Python Pickle Format read_pickle to_pickle SQL SQL read_sql to_sql SQL Google BigQuery read_gbq to_gbq Here is an informal performance comparison for some of these IO methods. Note For examples that use the StringIO class, make sure you import it with from io import StringIO for Python 3. Try like this in for loop ... key = filename.split('/')[-1] CSV & text files# The workhorse function for reading text files (a.k.a. flat files) is read_csv(). See the cookbook for some advanced strategies. Parsing options# read_csv() accepts the following common arguments: Basic# filepath_or_buffervariousEither a path to a file (a str, pathlib.Path, or py:py._path.local.LocalPath), URL (including http, ftp, and S3 locations), or any object with a read() method (such as an open file or StringIO). sepstr, defaults to ',' for read_csv(), \t for read_table()Delimiter to use. If sep is None, the C engine cannot automatically detect the separator, but the Python parsing engine can, meaning the latter will be used and automatically detect the separator by Python’s builtin sniffer tool, csv.Sniffer. In addition, separators longer than 1 character and different from '\s+' will be interpreted as regular expressions and will also force the use of the Python parsing engine. Note that regex delimiters are prone to ignoring quoted data. Regex example: '\\r\\t'. delimiterstr, default NoneAlternative argument name for sep. delim_whitespaceboolean, default FalseSpecifies whether or not whitespace (e.g. ' ' or '\t') will be used as the delimiter. Equivalent to setting sep='\s+'. If this option is set to True, nothing should be passed in for the delimiter parameter. Column and index locations and names# headerint or list of ints, default 'infer'Row number(s) to use as the column names, and the start of the data. Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first line of the file, if column names are passed explicitly then the behavior is identical to header=None. Explicitly pass header=0 to be able to replace existing names. The header can be a list of ints that specify row locations for a MultiIndex on the columns e.g. [0,1,3]. Intervening rows that are not specified will be skipped (e.g. 2 in this example is skipped). Note that this parameter ignores commented lines and empty lines if skip_blank_lines=True, so header=0 denotes the first line of data rather than the first line of the file. namesarray-like, default NoneList of column names to use. If file contains no header row, then you should explicitly pass header=None. Duplicates in this list are not allowed. index_colint, str, sequence of int / str, or False, optional, default NoneColumn(s) to use as the row labels of the DataFrame, either given as string name or column index. If a sequence of int / str is given, a MultiIndex is used. Note index_col=False can be used to force pandas to not use the first column as the index, e.g. when you have a malformed file with delimiters at the end of each line. The default value of None instructs pandas to guess. If the number of fields in the column header row is equal to the number of fields in the body of the data file, then a default index is used. If it is larger, then the first columns are used as index so that the remaining number of fields in the body are equal to the number of fields in the header. The first row after the header is used to determine the number of columns, which will go into the index. If the subsequent rows contain less columns than the first row, they are filled with NaN. This can be avoided through usecols. This ensures that the columns are taken as is and the trailing data are ignored. usecolslist-like or callable, default NoneReturn a subset of the columns. If list-like, all elements must either be positional (i.e. integer indices into the document columns) or strings that correspond to column names provided either by the user in names or inferred from the document header row(s). If names are given, the document header row(s) are not taken into account. For example, a valid list-like usecols parameter would be [0, 1, 2] or ['foo', 'bar', 'baz']. Element order is ignored, so usecols=[0, 1] is the same as [1, 0]. To instantiate a DataFrame from data with element order preserved use pd.read_csv(data, usecols=['foo', 'bar'])[['foo', 'bar']] for columns in ['foo', 'bar'] order or pd.read_csv(data, usecols=['foo', 'bar'])[['bar', 'foo']] for ['bar', 'foo'] order. If callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True: In [1]: import pandas as pd In [2]: from io import StringIO In [3]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [4]: pd.read_csv(StringIO(data)) Out[4]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [5]: pd.read_csv(StringIO(data), usecols=lambda x: x.upper() in ["COL1", "COL3"]) Out[5]: col1 col3 0 a 1 1 a 2 2 c 3 Using this parameter results in much faster parsing time and lower memory usage when using the c engine. The Python engine loads the data first before deciding which columns to drop. squeezeboolean, default FalseIf the parsed data only contains one column then return a Series. Deprecated since version 1.4.0: Append .squeeze("columns") to the call to {func_name} to squeeze the data. prefixstr, default NonePrefix to add to column numbers when no header, e.g. ‘X’ for X0, X1, … Deprecated since version 1.4.0: Use a list comprehension on the DataFrame’s columns after calling read_csv. In [6]: data = "col1,col2,col3\na,b,1" In [7]: df = pd.read_csv(StringIO(data)) In [8]: df.columns = [f"pre_{col}" for col in df.columns] In [9]: df Out[9]: pre_col1 pre_col2 pre_col3 0 a b 1 mangle_dupe_colsboolean, default TrueDuplicate columns will be specified as ‘X’, ‘X.1’…’X.N’, rather than ‘X’…’X’. Passing in False will cause data to be overwritten if there are duplicate names in the columns. Deprecated since version 1.5.0: The argument was never implemented, and a new argument where the renaming pattern can be specified will be added instead. General parsing configuration# dtypeType name or dict of column -> type, default NoneData type for data or columns. E.g. {'a': np.float64, 'b': np.int32, 'c': 'Int64'} Use str or object together with suitable na_values settings to preserve and not interpret dtype. If converters are specified, they will be applied INSTEAD of dtype conversion. New in version 1.5.0: Support for defaultdict was added. Specify a defaultdict as input where the default determines the dtype of the columns which are not explicitly listed. engine{'c', 'python', 'pyarrow'}Parser engine to use. The C and pyarrow engines are faster, while the python engine is currently more feature-complete. Multithreading is currently only supported by the pyarrow engine. New in version 1.4.0: The “pyarrow” engine was added as an experimental engine, and some features are unsupported, or may not work correctly, with this engine. convertersdict, default NoneDict of functions for converting values in certain columns. Keys can either be integers or column labels. true_valueslist, default NoneValues to consider as True. false_valueslist, default NoneValues to consider as False. skipinitialspaceboolean, default FalseSkip spaces after delimiter. skiprowslist-like or integer, default NoneLine numbers to skip (0-indexed) or number of lines to skip (int) at the start of the file. If callable, the callable function will be evaluated against the row indices, returning True if the row should be skipped and False otherwise: In [10]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [11]: pd.read_csv(StringIO(data)) Out[11]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [12]: pd.read_csv(StringIO(data), skiprows=lambda x: x % 2 != 0) Out[12]: col1 col2 col3 0 a b 2 skipfooterint, default 0Number of lines at bottom of file to skip (unsupported with engine=’c’). nrowsint, default NoneNumber of rows of file to read. Useful for reading pieces of large files. low_memoryboolean, default TrueInternally process the file in chunks, resulting in lower memory use while parsing, but possibly mixed type inference. To ensure no mixed types either set False, or specify the type with the dtype parameter. Note that the entire file is read into a single DataFrame regardless, use the chunksize or iterator parameter to return the data in chunks. (Only valid with C parser) memory_mapboolean, default FalseIf a filepath is provided for filepath_or_buffer, map the file object directly onto memory and access the data directly from there. Using this option can improve performance because there is no longer any I/O overhead. NA and missing data handling# na_valuesscalar, str, list-like, or dict, default NoneAdditional strings to recognize as NA/NaN. If dict passed, specific per-column NA values. See na values const below for a list of the values interpreted as NaN by default. keep_default_naboolean, default TrueWhether or not to include the default NaN values when parsing the data. Depending on whether na_values is passed in, the behavior is as follows: If keep_default_na is True, and na_values are specified, na_values is appended to the default NaN values used for parsing. If keep_default_na is True, and na_values are not specified, only the default NaN values are used for parsing. If keep_default_na is False, and na_values are specified, only the NaN values specified na_values are used for parsing. If keep_default_na is False, and na_values are not specified, no strings will be parsed as NaN. Note that if na_filter is passed in as False, the keep_default_na and na_values parameters will be ignored. na_filterboolean, default TrueDetect missing value markers (empty strings and the value of na_values). In data without any NAs, passing na_filter=False can improve the performance of reading a large file. verboseboolean, default FalseIndicate number of NA values placed in non-numeric columns. skip_blank_linesboolean, default TrueIf True, skip over blank lines rather than interpreting as NaN values. Datetime handling# parse_datesboolean or list of ints or names or list of lists or dict, default False. If True -> try parsing the index. If [1, 2, 3] -> try parsing columns 1, 2, 3 each as a separate date column. If [[1, 3]] -> combine columns 1 and 3 and parse as a single date column. If {'foo': [1, 3]} -> parse columns 1, 3 as date and call result ‘foo’. Note A fast-path exists for iso8601-formatted dates. infer_datetime_formatboolean, default FalseIf True and parse_dates is enabled for a column, attempt to infer the datetime format to speed up the processing. keep_date_colboolean, default FalseIf True and parse_dates specifies combining multiple columns then keep the original columns. date_parserfunction, default NoneFunction to use for converting a sequence of string columns to an array of datetime instances. The default uses dateutil.parser.parser to do the conversion. pandas will try to call date_parser in three different ways, advancing to the next if an exception occurs: 1) Pass one or more arrays (as defined by parse_dates) as arguments; 2) concatenate (row-wise) the string values from the columns defined by parse_dates into a single array and pass that; and 3) call date_parser once for each row using one or more strings (corresponding to the columns defined by parse_dates) as arguments. dayfirstboolean, default FalseDD/MM format dates, international and European format. cache_datesboolean, default TrueIf True, use a cache of unique, converted dates to apply the datetime conversion. May produce significant speed-up when parsing duplicate date strings, especially ones with timezone offsets. New in version 0.25.0. Iteration# iteratorboolean, default FalseReturn TextFileReader object for iteration or getting chunks with get_chunk(). chunksizeint, default NoneReturn TextFileReader object for iteration. See iterating and chunking below. Quoting, compression, and file format# compression{'infer', 'gzip', 'bz2', 'zip', 'xz', 'zstd', None, dict}, default 'infer'For on-the-fly decompression of on-disk data. If ‘infer’, then use gzip, bz2, zip, xz, or zstandard if filepath_or_buffer is path-like ending in ‘.gz’, ‘.bz2’, ‘.zip’, ‘.xz’, ‘.zst’, respectively, and no decompression otherwise. If using ‘zip’, the ZIP file must contain only one data file to be read in. Set to None for no decompression. Can also be a dict with key 'method' set to one of {'zip', 'gzip', 'bz2', 'zstd'} and other key-value pairs are forwarded to zipfile.ZipFile, gzip.GzipFile, bz2.BZ2File, or zstandard.ZstdDecompressor. As an example, the following could be passed for faster compression and to create a reproducible gzip archive: compression={'method': 'gzip', 'compresslevel': 1, 'mtime': 1}. Changed in version 1.1.0: dict option extended to support gzip and bz2. Changed in version 1.2.0: Previous versions forwarded dict entries for ‘gzip’ to gzip.open. thousandsstr, default NoneThousands separator. decimalstr, default '.'Character to recognize as decimal point. E.g. use ',' for European data. float_precisionstring, default NoneSpecifies which converter the C engine should use for floating-point values. The options are None for the ordinary converter, high for the high-precision converter, and round_trip for the round-trip converter. lineterminatorstr (length 1), default NoneCharacter to break file into lines. Only valid with C parser. quotecharstr (length 1)The character used to denote the start and end of a quoted item. Quoted items can include the delimiter and it will be ignored. quotingint or csv.QUOTE_* instance, default 0Control field quoting behavior per csv.QUOTE_* constants. Use one of QUOTE_MINIMAL (0), QUOTE_ALL (1), QUOTE_NONNUMERIC (2) or QUOTE_NONE (3). doublequoteboolean, default TrueWhen quotechar is specified and quoting is not QUOTE_NONE, indicate whether or not to interpret two consecutive quotechar elements inside a field as a single quotechar element. escapecharstr (length 1), default NoneOne-character string used to escape delimiter when quoting is QUOTE_NONE. commentstr, default NoneIndicates remainder of line should not be parsed. If found at the beginning of a line, the line will be ignored altogether. This parameter must be a single character. Like empty lines (as long as skip_blank_lines=True), fully commented lines are ignored by the parameter header but not by skiprows. For example, if comment='#', parsing ‘#empty\na,b,c\n1,2,3’ with header=0 will result in ‘a,b,c’ being treated as the header. encodingstr, default NoneEncoding to use for UTF when reading/writing (e.g. 'utf-8'). List of Python standard encodings. dialectstr or csv.Dialect instance, default NoneIf provided, this parameter will override values (default or not) for the following parameters: delimiter, doublequote, escapechar, skipinitialspace, quotechar, and quoting. If it is necessary to override values, a ParserWarning will be issued. See csv.Dialect documentation for more details. Error handling# error_bad_linesboolean, optional, default NoneLines with too many fields (e.g. a csv line with too many commas) will by default cause an exception to be raised, and no DataFrame will be returned. If False, then these “bad lines” will dropped from the DataFrame that is returned. See bad lines below. Deprecated since version 1.3.0: The on_bad_lines parameter should be used instead to specify behavior upon encountering a bad line instead. warn_bad_linesboolean, optional, default NoneIf error_bad_lines is False, and warn_bad_lines is True, a warning for each “bad line” will be output. Deprecated since version 1.3.0: The on_bad_lines parameter should be used instead to specify behavior upon encountering a bad line instead. on_bad_lines(‘error’, ‘warn’, ‘skip’), default ‘error’Specifies what to do upon encountering a bad line (a line with too many fields). Allowed values are : ‘error’, raise an ParserError when a bad line is encountered. ‘warn’, print a warning when a bad line is encountered and skip that line. ‘skip’, skip bad lines without raising or warning when they are encountered. New in version 1.3.0. Specifying column data types# You can indicate the data type for the whole DataFrame or individual columns: In [13]: import numpy as np In [14]: data = "a,b,c,d\n1,2,3,4\n5,6,7,8\n9,10,11" In [15]: print(data) a,b,c,d 1,2,3,4 5,6,7,8 9,10,11 In [16]: df = pd.read_csv(StringIO(data), dtype=object) In [17]: df Out[17]: a b c d 0 1 2 3 4 1 5 6 7 8 2 9 10 11 NaN In [18]: df["a"][0] Out[18]: '1' In [19]: df = pd.read_csv(StringIO(data), dtype={"b": object, "c": np.float64, "d": "Int64"}) In [20]: df.dtypes Out[20]: a int64 b object c float64 d Int64 dtype: object Fortunately, pandas offers more than one way to ensure that your column(s) contain only one dtype. If you’re unfamiliar with these concepts, you can see here to learn more about dtypes, and here to learn more about object conversion in pandas. For instance, you can use the converters argument of read_csv(): In [21]: data = "col_1\n1\n2\n'A'\n4.22" In [22]: df = pd.read_csv(StringIO(data), converters={"col_1": str}) In [23]: df Out[23]: col_1 0 1 1 2 2 'A' 3 4.22 In [24]: df["col_1"].apply(type).value_counts() Out[24]: <class 'str'> 4 Name: col_1, dtype: int64 Or you can use the to_numeric() function to coerce the dtypes after reading in the data, In [25]: df2 = pd.read_csv(StringIO(data)) In [26]: df2["col_1"] = pd.to_numeric(df2["col_1"], errors="coerce") In [27]: df2 Out[27]: col_1 0 1.00 1 2.00 2 NaN 3 4.22 In [28]: df2["col_1"].apply(type).value_counts() Out[28]: <class 'float'> 4 Name: col_1, dtype: int64 which will convert all valid parsing to floats, leaving the invalid parsing as NaN. Ultimately, how you deal with reading in columns containing mixed dtypes depends on your specific needs. In the case above, if you wanted to NaN out the data anomalies, then to_numeric() is probably your best option. However, if you wanted for all the data to be coerced, no matter the type, then using the converters argument of read_csv() would certainly be worth trying. Note In some cases, reading in abnormal data with columns containing mixed dtypes will result in an inconsistent dataset. If you rely on pandas to infer the dtypes of your columns, the parsing engine will go and infer the dtypes for different chunks of the data, rather than the whole dataset at once. Consequently, you can end up with column(s) with mixed dtypes. For example, In [29]: col_1 = list(range(500000)) + ["a", "b"] + list(range(500000)) In [30]: df = pd.DataFrame({"col_1": col_1}) In [31]: df.to_csv("foo.csv") In [32]: mixed_df = pd.read_csv("foo.csv") In [33]: mixed_df["col_1"].apply(type).value_counts() Out[33]: <class 'int'> 737858 <class 'str'> 262144 Name: col_1, dtype: int64 In [34]: mixed_df["col_1"].dtype Out[34]: dtype('O') will result with mixed_df containing an int dtype for certain chunks of the column, and str for others due to the mixed dtypes from the data that was read in. It is important to note that the overall column will be marked with a dtype of object, which is used for columns with mixed dtypes. Specifying categorical dtype# Categorical columns can be parsed directly by specifying dtype='category' or dtype=CategoricalDtype(categories, ordered). In [35]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [36]: pd.read_csv(StringIO(data)) Out[36]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [37]: pd.read_csv(StringIO(data)).dtypes Out[37]: col1 object col2 object col3 int64 dtype: object In [38]: pd.read_csv(StringIO(data), dtype="category").dtypes Out[38]: col1 category col2 category col3 category dtype: object Individual columns can be parsed as a Categorical using a dict specification: In [39]: pd.read_csv(StringIO(data), dtype={"col1": "category"}).dtypes Out[39]: col1 category col2 object col3 int64 dtype: object Specifying dtype='category' will result in an unordered Categorical whose categories are the unique values observed in the data. For more control on the categories and order, create a CategoricalDtype ahead of time, and pass that for that column’s dtype. In [40]: from pandas.api.types import CategoricalDtype In [41]: dtype = CategoricalDtype(["d", "c", "b", "a"], ordered=True) In [42]: pd.read_csv(StringIO(data), dtype={"col1": dtype}).dtypes Out[42]: col1 category col2 object col3 int64 dtype: object When using dtype=CategoricalDtype, “unexpected” values outside of dtype.categories are treated as missing values. In [43]: dtype = CategoricalDtype(["a", "b", "d"]) # No 'c' In [44]: pd.read_csv(StringIO(data), dtype={"col1": dtype}).col1 Out[44]: 0 a 1 a 2 NaN Name: col1, dtype: category Categories (3, object): ['a', 'b', 'd'] This matches the behavior of Categorical.set_categories(). Note With dtype='category', the resulting categories will always be parsed as strings (object dtype). If the categories are numeric they can be converted using the to_numeric() function, or as appropriate, another converter such as to_datetime(). When dtype is a CategoricalDtype with homogeneous categories ( all numeric, all datetimes, etc.), the conversion is done automatically. In [45]: df = pd.read_csv(StringIO(data), dtype="category") In [46]: df.dtypes Out[46]: col1 category col2 category col3 category dtype: object In [47]: df["col3"] Out[47]: 0 1 1 2 2 3 Name: col3, dtype: category Categories (3, object): ['1', '2', '3'] In [48]: new_categories = pd.to_numeric(df["col3"].cat.categories) In [49]: df["col3"] = df["col3"].cat.rename_categories(new_categories) In [50]: df["col3"] Out[50]: 0 1 1 2 2 3 Name: col3, dtype: category Categories (3, int64): [1, 2, 3] Naming and using columns# Handling column names# A file may or may not have a header row. pandas assumes the first row should be used as the column names: In [51]: data = "a,b,c\n1,2,3\n4,5,6\n7,8,9" In [52]: print(data) a,b,c 1,2,3 4,5,6 7,8,9 In [53]: pd.read_csv(StringIO(data)) Out[53]: a b c 0 1 2 3 1 4 5 6 2 7 8 9 By specifying the names argument in conjunction with header you can indicate other names to use and whether or not to throw away the header row (if any): In [54]: print(data) a,b,c 1,2,3 4,5,6 7,8,9 In [55]: pd.read_csv(StringIO(data), names=["foo", "bar", "baz"], header=0) Out[55]: foo bar baz 0 1 2 3 1 4 5 6 2 7 8 9 In [56]: pd.read_csv(StringIO(data), names=["foo", "bar", "baz"], header=None) Out[56]: foo bar baz 0 a b c 1 1 2 3 2 4 5 6 3 7 8 9 If the header is in a row other than the first, pass the row number to header. This will skip the preceding rows: In [57]: data = "skip this skip it\na,b,c\n1,2,3\n4,5,6\n7,8,9" In [58]: pd.read_csv(StringIO(data), header=1) Out[58]: a b c 0 1 2 3 1 4 5 6 2 7 8 9 Note Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first non-blank line of the file, if column names are passed explicitly then the behavior is identical to header=None. Duplicate names parsing# Deprecated since version 1.5.0: mangle_dupe_cols was never implemented, and a new argument where the renaming pattern can be specified will be added instead. If the file or header contains duplicate names, pandas will by default distinguish between them so as to prevent overwriting data: In [59]: data = "a,b,a\n0,1,2\n3,4,5" In [60]: pd.read_csv(StringIO(data)) Out[60]: a b a.1 0 0 1 2 1 3 4 5 There is no more duplicate data because mangle_dupe_cols=True by default, which modifies a series of duplicate columns ‘X’, …, ‘X’ to become ‘X’, ‘X.1’, …, ‘X.N’. Filtering columns (usecols)# The usecols argument allows you to select any subset of the columns in a file, either using the column names, position numbers or a callable: In [61]: data = "a,b,c,d\n1,2,3,foo\n4,5,6,bar\n7,8,9,baz" In [62]: pd.read_csv(StringIO(data)) Out[62]: a b c d 0 1 2 3 foo 1 4 5 6 bar 2 7 8 9 baz In [63]: pd.read_csv(StringIO(data), usecols=["b", "d"]) Out[63]: b d 0 2 foo 1 5 bar 2 8 baz In [64]: pd.read_csv(StringIO(data), usecols=[0, 2, 3]) Out[64]: a c d 0 1 3 foo 1 4 6 bar 2 7 9 baz In [65]: pd.read_csv(StringIO(data), usecols=lambda x: x.upper() in ["A", "C"]) Out[65]: a c 0 1 3 1 4 6 2 7 9 The usecols argument can also be used to specify which columns not to use in the final result: In [66]: pd.read_csv(StringIO(data), usecols=lambda x: x not in ["a", "c"]) Out[66]: b d 0 2 foo 1 5 bar 2 8 baz In this case, the callable is specifying that we exclude the “a” and “c” columns from the output. Comments and empty lines# Ignoring line comments and empty lines# If the comment parameter is specified, then completely commented lines will be ignored. By default, completely blank lines will be ignored as well. In [67]: data = "\na,b,c\n \n# commented line\n1,2,3\n\n4,5,6" In [68]: print(data) a,b,c # commented line 1,2,3 4,5,6 In [69]: pd.read_csv(StringIO(data), comment="#") Out[69]: a b c 0 1 2 3 1 4 5 6 If skip_blank_lines=False, then read_csv will not ignore blank lines: In [70]: data = "a,b,c\n\n1,2,3\n\n\n4,5,6" In [71]: pd.read_csv(StringIO(data), skip_blank_lines=False) Out[71]: a b c 0 NaN NaN NaN 1 1.0 2.0 3.0 2 NaN NaN NaN 3 NaN NaN NaN 4 4.0 5.0 6.0 Warning The presence of ignored lines might create ambiguities involving line numbers; the parameter header uses row numbers (ignoring commented/empty lines), while skiprows uses line numbers (including commented/empty lines): In [72]: data = "#comment\na,b,c\nA,B,C\n1,2,3" In [73]: pd.read_csv(StringIO(data), comment="#", header=1) Out[73]: A B C 0 1 2 3 In [74]: data = "A,B,C\n#comment\na,b,c\n1,2,3" In [75]: pd.read_csv(StringIO(data), comment="#", skiprows=2) Out[75]: a b c 0 1 2 3 If both header and skiprows are specified, header will be relative to the end of skiprows. For example: In [76]: data = ( ....: "# empty\n" ....: "# second empty line\n" ....: "# third emptyline\n" ....: "X,Y,Z\n" ....: "1,2,3\n" ....: "A,B,C\n" ....: "1,2.,4.\n" ....: "5.,NaN,10.0\n" ....: ) ....: In [77]: print(data) # empty # second empty line # third emptyline X,Y,Z 1,2,3 A,B,C 1,2.,4. 5.,NaN,10.0 In [78]: pd.read_csv(StringIO(data), comment="#", skiprows=4, header=1) Out[78]: A B C 0 1.0 2.0 4.0 1 5.0 NaN 10.0 Comments# Sometimes comments or meta data may be included in a file: In [79]: print(open("tmp.csv").read()) ID,level,category Patient1,123000,x # really unpleasant Patient2,23000,y # wouldn't take his medicine Patient3,1234018,z # awesome By default, the parser includes the comments in the output: In [80]: df = pd.read_csv("tmp.csv") In [81]: df Out[81]: ID level category 0 Patient1 123000 x # really unpleasant 1 Patient2 23000 y # wouldn't take his medicine 2 Patient3 1234018 z # awesome We can suppress the comments using the comment keyword: In [82]: df = pd.read_csv("tmp.csv", comment="#") In [83]: df Out[83]: ID level category 0 Patient1 123000 x 1 Patient2 23000 y 2 Patient3 1234018 z Dealing with Unicode data# The encoding argument should be used for encoded unicode data, which will result in byte strings being decoded to unicode in the result: In [84]: from io import BytesIO In [85]: data = b"word,length\n" b"Tr\xc3\xa4umen,7\n" b"Gr\xc3\xbc\xc3\x9fe,5" In [86]: data = data.decode("utf8").encode("latin-1") In [87]: df = pd.read_csv(BytesIO(data), encoding="latin-1") In [88]: df Out[88]: word length 0 Träumen 7 1 Grüße 5 In [89]: df["word"][1] Out[89]: 'Grüße' Some formats which encode all characters as multiple bytes, like UTF-16, won’t parse correctly at all without specifying the encoding. Full list of Python standard encodings. Index columns and trailing delimiters# If a file has one more column of data than the number of column names, the first column will be used as the DataFrame’s row names: In [90]: data = "a,b,c\n4,apple,bat,5.7\n8,orange,cow,10" In [91]: pd.read_csv(StringIO(data)) Out[91]: a b c 4 apple bat 5.7 8 orange cow 10.0 In [92]: data = "index,a,b,c\n4,apple,bat,5.7\n8,orange,cow,10" In [93]: pd.read_csv(StringIO(data), index_col=0) Out[93]: a b c index 4 apple bat 5.7 8 orange cow 10.0 Ordinarily, you can achieve this behavior using the index_col option. There are some exception cases when a file has been prepared with delimiters at the end of each data line, confusing the parser. To explicitly disable the index column inference and discard the last column, pass index_col=False: In [94]: data = "a,b,c\n4,apple,bat,\n8,orange,cow," In [95]: print(data) a,b,c 4,apple,bat, 8,orange,cow, In [96]: pd.read_csv(StringIO(data)) Out[96]: a b c 4 apple bat NaN 8 orange cow NaN In [97]: pd.read_csv(StringIO(data), index_col=False) Out[97]: a b c 0 4 apple bat 1 8 orange cow If a subset of data is being parsed using the usecols option, the index_col specification is based on that subset, not the original data. In [98]: data = "a,b,c\n4,apple,bat,\n8,orange,cow," In [99]: print(data) a,b,c 4,apple,bat, 8,orange,cow, In [100]: pd.read_csv(StringIO(data), usecols=["b", "c"]) Out[100]: b c 4 bat NaN 8 cow NaN In [101]: pd.read_csv(StringIO(data), usecols=["b", "c"], index_col=0) Out[101]: b c 4 bat NaN 8 cow NaN Date Handling# Specifying date columns# To better facilitate working with datetime data, read_csv() uses the keyword arguments parse_dates and date_parser to allow users to specify a variety of columns and date/time formats to turn the input text data into datetime objects. The simplest case is to just pass in parse_dates=True: In [102]: with open("foo.csv", mode="w") as f: .....: f.write("date,A,B,C\n20090101,a,1,2\n20090102,b,3,4\n20090103,c,4,5") .....: # Use a column as an index, and parse it as dates. In [103]: df = pd.read_csv("foo.csv", index_col=0, parse_dates=True) In [104]: df Out[104]: A B C date 2009-01-01 a 1 2 2009-01-02 b 3 4 2009-01-03 c 4 5 # These are Python datetime objects In [105]: df.index Out[105]: DatetimeIndex(['2009-01-01', '2009-01-02', '2009-01-03'], dtype='datetime64[ns]', name='date', freq=None) It is often the case that we may want to store date and time data separately, or store various date fields separately. the parse_dates keyword can be used to specify a combination of columns to parse the dates and/or times from. You can specify a list of column lists to parse_dates, the resulting date columns will be prepended to the output (so as to not affect the existing column order) and the new column names will be the concatenation of the component column names: In [106]: data = ( .....: "KORD,19990127, 19:00:00, 18:56:00, 0.8100\n" .....: "KORD,19990127, 20:00:00, 19:56:00, 0.0100\n" .....: "KORD,19990127, 21:00:00, 20:56:00, -0.5900\n" .....: "KORD,19990127, 21:00:00, 21:18:00, -0.9900\n" .....: "KORD,19990127, 22:00:00, 21:56:00, -0.5900\n" .....: "KORD,19990127, 23:00:00, 22:56:00, -0.5900" .....: ) .....: In [107]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [108]: df = pd.read_csv("tmp.csv", header=None, parse_dates=[[1, 2], [1, 3]]) In [109]: df Out[109]: 1_2 1_3 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 By default the parser removes the component date columns, but you can choose to retain them via the keep_date_col keyword: In [110]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=[[1, 2], [1, 3]], keep_date_col=True .....: ) .....: In [111]: df Out[111]: 1_2 1_3 0 ... 2 3 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD ... 19:00:00 18:56:00 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD ... 20:00:00 19:56:00 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD ... 21:00:00 20:56:00 -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD ... 21:00:00 21:18:00 -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD ... 22:00:00 21:56:00 -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD ... 23:00:00 22:56:00 -0.59 [6 rows x 7 columns] Note that if you wish to combine multiple columns into a single date column, a nested list must be used. In other words, parse_dates=[1, 2] indicates that the second and third columns should each be parsed as separate date columns while parse_dates=[[1, 2]] means the two columns should be parsed into a single column. You can also use a dict to specify custom name columns: In [112]: date_spec = {"nominal": [1, 2], "actual": [1, 3]} In [113]: df = pd.read_csv("tmp.csv", header=None, parse_dates=date_spec) In [114]: df Out[114]: nominal actual 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 It is important to remember that if multiple text columns are to be parsed into a single date column, then a new column is prepended to the data. The index_col specification is based off of this new set of columns rather than the original data columns: In [115]: date_spec = {"nominal": [1, 2], "actual": [1, 3]} In [116]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=date_spec, index_col=0 .....: ) # index is the nominal column .....: In [117]: df Out[117]: actual 0 4 nominal 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 Note If a column or index contains an unparsable date, the entire column or index will be returned unaltered as an object data type. For non-standard datetime parsing, use to_datetime() after pd.read_csv. Note read_csv has a fast_path for parsing datetime strings in iso8601 format, e.g “2000-01-01T00:01:02+00:00” and similar variations. If you can arrange for your data to store datetimes in this format, load times will be significantly faster, ~20x has been observed. Date parsing functions# Finally, the parser allows you to specify a custom date_parser function to take full advantage of the flexibility of the date parsing API: In [118]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=date_spec, date_parser=pd.to_datetime .....: ) .....: In [119]: df Out[119]: nominal actual 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 pandas will try to call the date_parser function in three different ways. If an exception is raised, the next one is tried: date_parser is first called with one or more arrays as arguments, as defined using parse_dates (e.g., date_parser(['2013', '2013'], ['1', '2'])). If #1 fails, date_parser is called with all the columns concatenated row-wise into a single array (e.g., date_parser(['2013 1', '2013 2'])). Note that performance-wise, you should try these methods of parsing dates in order: Try to infer the format using infer_datetime_format=True (see section below). If you know the format, use pd.to_datetime(): date_parser=lambda x: pd.to_datetime(x, format=...). If you have a really non-standard format, use a custom date_parser function. For optimal performance, this should be vectorized, i.e., it should accept arrays as arguments. Parsing a CSV with mixed timezones# pandas cannot natively represent a column or index with mixed timezones. If your CSV file contains columns with a mixture of timezones, the default result will be an object-dtype column with strings, even with parse_dates. In [120]: content = """\ .....: a .....: 2000-01-01T00:00:00+05:00 .....: 2000-01-01T00:00:00+06:00""" .....: In [121]: df = pd.read_csv(StringIO(content), parse_dates=["a"]) In [122]: df["a"] Out[122]: 0 2000-01-01 00:00:00+05:00 1 2000-01-01 00:00:00+06:00 Name: a, dtype: object To parse the mixed-timezone values as a datetime column, pass a partially-applied to_datetime() with utc=True as the date_parser. In [123]: df = pd.read_csv( .....: StringIO(content), .....: parse_dates=["a"], .....: date_parser=lambda col: pd.to_datetime(col, utc=True), .....: ) .....: In [124]: df["a"] Out[124]: 0 1999-12-31 19:00:00+00:00 1 1999-12-31 18:00:00+00:00 Name: a, dtype: datetime64[ns, UTC] Inferring datetime format# If you have parse_dates enabled for some or all of your columns, and your datetime strings are all formatted the same way, you may get a large speed up by setting infer_datetime_format=True. If set, pandas will attempt to guess the format of your datetime strings, and then use a faster means of parsing the strings. 5-10x parsing speeds have been observed. pandas will fallback to the usual parsing if either the format cannot be guessed or the format that was guessed cannot properly parse the entire column of strings. So in general, infer_datetime_format should not have any negative consequences if enabled. Here are some examples of datetime strings that can be guessed (All representing December 30th, 2011 at 00:00:00): “20111230” “2011/12/30” “20111230 00:00:00” “12/30/2011 00:00:00” “30/Dec/2011 00:00:00” “30/December/2011 00:00:00” Note that infer_datetime_format is sensitive to dayfirst. With dayfirst=True, it will guess “01/12/2011” to be December 1st. With dayfirst=False (default) it will guess “01/12/2011” to be January 12th. # Try to infer the format for the index column In [125]: df = pd.read_csv( .....: "foo.csv", .....: index_col=0, .....: parse_dates=True, .....: infer_datetime_format=True, .....: ) .....: In [126]: df Out[126]: A B C date 2009-01-01 a 1 2 2009-01-02 b 3 4 2009-01-03 c 4 5 International date formats# While US date formats tend to be MM/DD/YYYY, many international formats use DD/MM/YYYY instead. For convenience, a dayfirst keyword is provided: In [127]: data = "date,value,cat\n1/6/2000,5,a\n2/6/2000,10,b\n3/6/2000,15,c" In [128]: print(data) date,value,cat 1/6/2000,5,a 2/6/2000,10,b 3/6/2000,15,c In [129]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [130]: pd.read_csv("tmp.csv", parse_dates=[0]) Out[130]: date value cat 0 2000-01-06 5 a 1 2000-02-06 10 b 2 2000-03-06 15 c In [131]: pd.read_csv("tmp.csv", dayfirst=True, parse_dates=[0]) Out[131]: date value cat 0 2000-06-01 5 a 1 2000-06-02 10 b 2 2000-06-03 15 c Writing CSVs to binary file objects# New in version 1.2.0. df.to_csv(..., mode="wb") allows writing a CSV to a file object opened binary mode. In most cases, it is not necessary to specify mode as Pandas will auto-detect whether the file object is opened in text or binary mode. In [132]: import io In [133]: data = pd.DataFrame([0, 1, 2]) In [134]: buffer = io.BytesIO() In [135]: data.to_csv(buffer, encoding="utf-8", compression="gzip") Specifying method for floating-point conversion# The parameter float_precision can be specified in order to use a specific floating-point converter during parsing with the C engine. The options are the ordinary converter, the high-precision converter, and the round-trip converter (which is guaranteed to round-trip values after writing to a file). For example: In [136]: val = "0.3066101993807095471566981359501369297504425048828125" In [137]: data = "a,b,c\n1,2,{0}".format(val) In [138]: abs( .....: pd.read_csv( .....: StringIO(data), .....: engine="c", .....: float_precision=None, .....: )["c"][0] - float(val) .....: ) .....: Out[138]: 5.551115123125783e-17 In [139]: abs( .....: pd.read_csv( .....: StringIO(data), .....: engine="c", .....: float_precision="high", .....: )["c"][0] - float(val) .....: ) .....: Out[139]: 5.551115123125783e-17 In [140]: abs( .....: pd.read_csv(StringIO(data), engine="c", float_precision="round_trip")["c"][0] .....: - float(val) .....: ) .....: Out[140]: 0.0 Thousand separators# For large numbers that have been written with a thousands separator, you can set the thousands keyword to a string of length 1 so that integers will be parsed correctly: By default, numbers with a thousands separator will be parsed as strings: In [141]: data = ( .....: "ID|level|category\n" .....: "Patient1|123,000|x\n" .....: "Patient2|23,000|y\n" .....: "Patient3|1,234,018|z" .....: ) .....: In [142]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [143]: df = pd.read_csv("tmp.csv", sep="|") In [144]: df Out[144]: ID level category 0 Patient1 123,000 x 1 Patient2 23,000 y 2 Patient3 1,234,018 z In [145]: df.level.dtype Out[145]: dtype('O') The thousands keyword allows integers to be parsed correctly: In [146]: df = pd.read_csv("tmp.csv", sep="|", thousands=",") In [147]: df Out[147]: ID level category 0 Patient1 123000 x 1 Patient2 23000 y 2 Patient3 1234018 z In [148]: df.level.dtype Out[148]: dtype('int64') NA values# To control which values are parsed as missing values (which are signified by NaN), specify a string in na_values. If you specify a list of strings, then all values in it are considered to be missing values. If you specify a number (a float, like 5.0 or an integer like 5), the corresponding equivalent values will also imply a missing value (in this case effectively [5.0, 5] are recognized as NaN). To completely override the default values that are recognized as missing, specify keep_default_na=False. The default NaN recognized values are ['-1.#IND', '1.#QNAN', '1.#IND', '-1.#QNAN', '#N/A N/A', '#N/A', 'N/A', 'n/a', 'NA', '<NA>', '#NA', 'NULL', 'null', 'NaN', '-NaN', 'nan', '-nan', '']. Let us consider some examples: pd.read_csv("path_to_file.csv", na_values=[5]) In the example above 5 and 5.0 will be recognized as NaN, in addition to the defaults. A string will first be interpreted as a numerical 5, then as a NaN. pd.read_csv("path_to_file.csv", keep_default_na=False, na_values=[""]) Above, only an empty field will be recognized as NaN. pd.read_csv("path_to_file.csv", keep_default_na=False, na_values=["NA", "0"]) Above, both NA and 0 as strings are NaN. pd.read_csv("path_to_file.csv", na_values=["Nope"]) The default values, in addition to the string "Nope" are recognized as NaN. Infinity# inf like values will be parsed as np.inf (positive infinity), and -inf as -np.inf (negative infinity). These will ignore the case of the value, meaning Inf, will also be parsed as np.inf. Returning Series# Using the squeeze keyword, the parser will return output with a single column as a Series: Deprecated since version 1.4.0: Users should append .squeeze("columns") to the DataFrame returned by read_csv instead. In [149]: data = "level\nPatient1,123000\nPatient2,23000\nPatient3,1234018" In [150]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [151]: print(open("tmp.csv").read()) level Patient1,123000 Patient2,23000 Patient3,1234018 In [152]: output = pd.read_csv("tmp.csv", squeeze=True) In [153]: output Out[153]: Patient1 123000 Patient2 23000 Patient3 1234018 Name: level, dtype: int64 In [154]: type(output) Out[154]: pandas.core.series.Series Boolean values# The common values True, False, TRUE, and FALSE are all recognized as boolean. Occasionally you might want to recognize other values as being boolean. To do this, use the true_values and false_values options as follows: In [155]: data = "a,b,c\n1,Yes,2\n3,No,4" In [156]: print(data) a,b,c 1,Yes,2 3,No,4 In [157]: pd.read_csv(StringIO(data)) Out[157]: a b c 0 1 Yes 2 1 3 No 4 In [158]: pd.read_csv(StringIO(data), true_values=["Yes"], false_values=["No"]) Out[158]: a b c 0 1 True 2 1 3 False 4 Handling “bad” lines# Some files may have malformed lines with too few fields or too many. Lines with too few fields will have NA values filled in the trailing fields. Lines with too many fields will raise an error by default: In [159]: data = "a,b,c\n1,2,3\n4,5,6,7\n8,9,10" In [160]: pd.read_csv(StringIO(data)) --------------------------------------------------------------------------- ParserError Traceback (most recent call last) Cell In[160], line 1 ----> 1 pd.read_csv(StringIO(data)) File ~/work/pandas/pandas/pandas/util/_decorators.py:211, in deprecate_kwarg.<locals>._deprecate_kwarg.<locals>.wrapper(*args, **kwargs) 209 else: 210 kwargs[new_arg_name] = new_arg_value --> 211 return func(*args, **kwargs) File ~/work/pandas/pandas/pandas/util/_decorators.py:331, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs) 325 if len(args) > num_allow_args: 326 warnings.warn( 327 msg.format(arguments=_format_argument_list(allow_args)), 328 FutureWarning, 329 stacklevel=find_stack_level(), 330 ) --> 331 return func(*args, **kwargs) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:950, in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, error_bad_lines, warn_bad_lines, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options) 935 kwds_defaults = _refine_defaults_read( 936 dialect, 937 delimiter, (...) 946 defaults={"delimiter": ","}, 947 ) 948 kwds.update(kwds_defaults) --> 950 return _read(filepath_or_buffer, kwds) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:611, in _read(filepath_or_buffer, kwds) 608 return parser 610 with parser: --> 611 return parser.read(nrows) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:1778, in TextFileReader.read(self, nrows) 1771 nrows = validate_integer("nrows", nrows) 1772 try: 1773 # error: "ParserBase" has no attribute "read" 1774 ( 1775 index, 1776 columns, 1777 col_dict, -> 1778 ) = self._engine.read( # type: ignore[attr-defined] 1779 nrows 1780 ) 1781 except Exception: 1782 self.close() File ~/work/pandas/pandas/pandas/io/parsers/c_parser_wrapper.py:230, in CParserWrapper.read(self, nrows) 228 try: 229 if self.low_memory: --> 230 chunks = self._reader.read_low_memory(nrows) 231 # destructive to chunks 232 data = _concatenate_chunks(chunks) File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:808, in pandas._libs.parsers.TextReader.read_low_memory() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:866, in pandas._libs.parsers.TextReader._read_rows() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:852, in pandas._libs.parsers.TextReader._tokenize_rows() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:1973, in pandas._libs.parsers.raise_parser_error() ParserError: Error tokenizing data. C error: Expected 3 fields in line 3, saw 4 You can elect to skip bad lines: In [29]: pd.read_csv(StringIO(data), on_bad_lines="warn") Skipping line 3: expected 3 fields, saw 4 Out[29]: a b c 0 1 2 3 1 8 9 10 Or pass a callable function to handle the bad line if engine="python". The bad line will be a list of strings that was split by the sep: In [29]: external_list = [] In [30]: def bad_lines_func(line): ...: external_list.append(line) ...: return line[-3:] In [31]: pd.read_csv(StringIO(data), on_bad_lines=bad_lines_func, engine="python") Out[31]: a b c 0 1 2 3 1 5 6 7 2 8 9 10 In [32]: external_list Out[32]: [4, 5, 6, 7] .. versionadded:: 1.4.0 You can also use the usecols parameter to eliminate extraneous column data that appear in some lines but not others: In [33]: pd.read_csv(StringIO(data), usecols=[0, 1, 2]) Out[33]: a b c 0 1 2 3 1 4 5 6 2 8 9 10 In case you want to keep all data including the lines with too many fields, you can specify a sufficient number of names. This ensures that lines with not enough fields are filled with NaN. In [34]: pd.read_csv(StringIO(data), names=['a', 'b', 'c', 'd']) Out[34]: a b c d 0 1 2 3 NaN 1 4 5 6 7 2 8 9 10 NaN Dialect# The dialect keyword gives greater flexibility in specifying the file format. By default it uses the Excel dialect but you can specify either the dialect name or a csv.Dialect instance. Suppose you had data with unenclosed quotes: In [161]: data = "label1,label2,label3\n" 'index1,"a,c,e\n' "index2,b,d,f" In [162]: print(data) label1,label2,label3 index1,"a,c,e index2,b,d,f By default, read_csv uses the Excel dialect and treats the double quote as the quote character, which causes it to fail when it finds a newline before it finds the closing double quote. We can get around this using dialect: In [163]: import csv In [164]: dia = csv.excel() In [165]: dia.quoting = csv.QUOTE_NONE In [166]: pd.read_csv(StringIO(data), dialect=dia) Out[166]: label1 label2 label3 index1 "a c e index2 b d f All of the dialect options can be specified separately by keyword arguments: In [167]: data = "a,b,c~1,2,3~4,5,6" In [168]: pd.read_csv(StringIO(data), lineterminator="~") Out[168]: a b c 0 1 2 3 1 4 5 6 Another common dialect option is skipinitialspace, to skip any whitespace after a delimiter: In [169]: data = "a, b, c\n1, 2, 3\n4, 5, 6" In [170]: print(data) a, b, c 1, 2, 3 4, 5, 6 In [171]: pd.read_csv(StringIO(data), skipinitialspace=True) Out[171]: a b c 0 1 2 3 1 4 5 6 The parsers make every attempt to “do the right thing” and not be fragile. Type inference is a pretty big deal. If a column can be coerced to integer dtype without altering the contents, the parser will do so. Any non-numeric columns will come through as object dtype as with the rest of pandas objects. Quoting and Escape Characters# Quotes (and other escape characters) in embedded fields can be handled in any number of ways. One way is to use backslashes; to properly parse this data, you should pass the escapechar option: In [172]: data = 'a,b\n"hello, \\"Bob\\", nice to see you",5' In [173]: print(data) a,b "hello, \"Bob\", nice to see you",5 In [174]: pd.read_csv(StringIO(data), escapechar="\\") Out[174]: a b 0 hello, "Bob", nice to see you 5 Files with fixed width columns# While read_csv() reads delimited data, the read_fwf() function works with data files that have known and fixed column widths. The function parameters to read_fwf are largely the same as read_csv with two extra parameters, and a different usage of the delimiter parameter: colspecs: A list of pairs (tuples) giving the extents of the fixed-width fields of each line as half-open intervals (i.e., [from, to[ ). String value ‘infer’ can be used to instruct the parser to try detecting the column specifications from the first 100 rows of the data. Default behavior, if not specified, is to infer. widths: A list of field widths which can be used instead of ‘colspecs’ if the intervals are contiguous. delimiter: Characters to consider as filler characters in the fixed-width file. Can be used to specify the filler character of the fields if it is not spaces (e.g., ‘~’). Consider a typical fixed-width data file: In [175]: data1 = ( .....: "id8141 360.242940 149.910199 11950.7\n" .....: "id1594 444.953632 166.985655 11788.4\n" .....: "id1849 364.136849 183.628767 11806.2\n" .....: "id1230 413.836124 184.375703 11916.8\n" .....: "id1948 502.953953 173.237159 12468.3" .....: ) .....: In [176]: with open("bar.csv", "w") as f: .....: f.write(data1) .....: In order to parse this file into a DataFrame, we simply need to supply the column specifications to the read_fwf function along with the file name: # Column specifications are a list of half-intervals In [177]: colspecs = [(0, 6), (8, 20), (21, 33), (34, 43)] In [178]: df = pd.read_fwf("bar.csv", colspecs=colspecs, header=None, index_col=0) In [179]: df Out[179]: 1 2 3 0 id8141 360.242940 149.910199 11950.7 id1594 444.953632 166.985655 11788.4 id1849 364.136849 183.628767 11806.2 id1230 413.836124 184.375703 11916.8 id1948 502.953953 173.237159 12468.3 Note how the parser automatically picks column names X.<column number> when header=None argument is specified. Alternatively, you can supply just the column widths for contiguous columns: # Widths are a list of integers In [180]: widths = [6, 14, 13, 10] In [181]: df = pd.read_fwf("bar.csv", widths=widths, header=None) In [182]: df Out[182]: 0 1 2 3 0 id8141 360.242940 149.910199 11950.7 1 id1594 444.953632 166.985655 11788.4 2 id1849 364.136849 183.628767 11806.2 3 id1230 413.836124 184.375703 11916.8 4 id1948 502.953953 173.237159 12468.3 The parser will take care of extra white spaces around the columns so it’s ok to have extra separation between the columns in the file. By default, read_fwf will try to infer the file’s colspecs by using the first 100 rows of the file. It can do it only in cases when the columns are aligned and correctly separated by the provided delimiter (default delimiter is whitespace). In [183]: df = pd.read_fwf("bar.csv", header=None, index_col=0) In [184]: df Out[184]: 1 2 3 0 id8141 360.242940 149.910199 11950.7 id1594 444.953632 166.985655 11788.4 id1849 364.136849 183.628767 11806.2 id1230 413.836124 184.375703 11916.8 id1948 502.953953 173.237159 12468.3 read_fwf supports the dtype parameter for specifying the types of parsed columns to be different from the inferred type. In [185]: pd.read_fwf("bar.csv", header=None, index_col=0).dtypes Out[185]: 1 float64 2 float64 3 float64 dtype: object In [186]: pd.read_fwf("bar.csv", header=None, dtype={2: "object"}).dtypes Out[186]: 0 object 1 float64 2 object 3 float64 dtype: object Indexes# Files with an “implicit” index column# Consider a file with one less entry in the header than the number of data column: In [187]: data = "A,B,C\n20090101,a,1,2\n20090102,b,3,4\n20090103,c,4,5" In [188]: print(data) A,B,C 20090101,a,1,2 20090102,b,3,4 20090103,c,4,5 In [189]: with open("foo.csv", "w") as f: .....: f.write(data) .....: In this special case, read_csv assumes that the first column is to be used as the index of the DataFrame: In [190]: pd.read_csv("foo.csv") Out[190]: A B C 20090101 a 1 2 20090102 b 3 4 20090103 c 4 5 Note that the dates weren’t automatically parsed. In that case you would need to do as before: In [191]: df = pd.read_csv("foo.csv", parse_dates=True) In [192]: df.index Out[192]: DatetimeIndex(['2009-01-01', '2009-01-02', '2009-01-03'], dtype='datetime64[ns]', freq=None) Reading an index with a MultiIndex# Suppose you have data indexed by two columns: In [193]: data = 'year,indiv,zit,xit\n1977,"A",1.2,.6\n1977,"B",1.5,.5' In [194]: print(data) year,indiv,zit,xit 1977,"A",1.2,.6 1977,"B",1.5,.5 In [195]: with open("mindex_ex.csv", mode="w") as f: .....: f.write(data) .....: The index_col argument to read_csv can take a list of column numbers to turn multiple columns into a MultiIndex for the index of the returned object: In [196]: df = pd.read_csv("mindex_ex.csv", index_col=[0, 1]) In [197]: df Out[197]: zit xit year indiv 1977 A 1.2 0.6 B 1.5 0.5 In [198]: df.loc[1977] Out[198]: zit xit indiv A 1.2 0.6 B 1.5 0.5 Reading columns with a MultiIndex# By specifying list of row locations for the header argument, you can read in a MultiIndex for the columns. Specifying non-consecutive rows will skip the intervening rows. In [199]: from pandas._testing import makeCustomDataframe as mkdf In [200]: df = mkdf(5, 3, r_idx_nlevels=2, c_idx_nlevels=4) In [201]: df.to_csv("mi.csv") In [202]: print(open("mi.csv").read()) C0,,C_l0_g0,C_l0_g1,C_l0_g2 C1,,C_l1_g0,C_l1_g1,C_l1_g2 C2,,C_l2_g0,C_l2_g1,C_l2_g2 C3,,C_l3_g0,C_l3_g1,C_l3_g2 R0,R1,,, R_l0_g0,R_l1_g0,R0C0,R0C1,R0C2 R_l0_g1,R_l1_g1,R1C0,R1C1,R1C2 R_l0_g2,R_l1_g2,R2C0,R2C1,R2C2 R_l0_g3,R_l1_g3,R3C0,R3C1,R3C2 R_l0_g4,R_l1_g4,R4C0,R4C1,R4C2 In [203]: pd.read_csv("mi.csv", header=[0, 1, 2, 3], index_col=[0, 1]) Out[203]: C0 C_l0_g0 C_l0_g1 C_l0_g2 C1 C_l1_g0 C_l1_g1 C_l1_g2 C2 C_l2_g0 C_l2_g1 C_l2_g2 C3 C_l3_g0 C_l3_g1 C_l3_g2 R0 R1 R_l0_g0 R_l1_g0 R0C0 R0C1 R0C2 R_l0_g1 R_l1_g1 R1C0 R1C1 R1C2 R_l0_g2 R_l1_g2 R2C0 R2C1 R2C2 R_l0_g3 R_l1_g3 R3C0 R3C1 R3C2 R_l0_g4 R_l1_g4 R4C0 R4C1 R4C2 read_csv is also able to interpret a more common format of multi-columns indices. In [204]: data = ",a,a,a,b,c,c\n,q,r,s,t,u,v\none,1,2,3,4,5,6\ntwo,7,8,9,10,11,12" In [205]: print(data) ,a,a,a,b,c,c ,q,r,s,t,u,v one,1,2,3,4,5,6 two,7,8,9,10,11,12 In [206]: with open("mi2.csv", "w") as fh: .....: fh.write(data) .....: In [207]: pd.read_csv("mi2.csv", header=[0, 1], index_col=0) Out[207]: a b c q r s t u v one 1 2 3 4 5 6 two 7 8 9 10 11 12 Note If an index_col is not specified (e.g. you don’t have an index, or wrote it with df.to_csv(..., index=False), then any names on the columns index will be lost. Automatically “sniffing” the delimiter# read_csv is capable of inferring delimited (not necessarily comma-separated) files, as pandas uses the csv.Sniffer class of the csv module. For this, you have to specify sep=None. In [208]: df = pd.DataFrame(np.random.randn(10, 4)) In [209]: df.to_csv("tmp.csv", sep="|") In [210]: df.to_csv("tmp2.csv", sep=":") In [211]: pd.read_csv("tmp2.csv", sep=None, engine="python") Out[211]: Unnamed: 0 0 1 2 3 0 0 0.469112 -0.282863 -1.509059 -1.135632 1 1 1.212112 -0.173215 0.119209 -1.044236 2 2 -0.861849 -2.104569 -0.494929 1.071804 3 3 0.721555 -0.706771 -1.039575 0.271860 4 4 -0.424972 0.567020 0.276232 -1.087401 5 5 -0.673690 0.113648 -1.478427 0.524988 6 6 0.404705 0.577046 -1.715002 -1.039268 7 7 -0.370647 -1.157892 -1.344312 0.844885 8 8 1.075770 -0.109050 1.643563 -1.469388 9 9 0.357021 -0.674600 -1.776904 -0.968914 Reading multiple files to create a single DataFrame# It’s best to use concat() to combine multiple files. See the cookbook for an example. Iterating through files chunk by chunk# Suppose you wish to iterate through a (potentially very large) file lazily rather than reading the entire file into memory, such as the following: In [212]: df = pd.DataFrame(np.random.randn(10, 4)) In [213]: df.to_csv("tmp.csv", sep="|") In [214]: table = pd.read_csv("tmp.csv", sep="|") In [215]: table Out[215]: Unnamed: 0 0 1 2 3 0 0 -1.294524 0.413738 0.276662 -0.472035 1 1 -0.013960 -0.362543 -0.006154 -0.923061 2 2 0.895717 0.805244 -1.206412 2.565646 3 3 1.431256 1.340309 -1.170299 -0.226169 4 4 0.410835 0.813850 0.132003 -0.827317 5 5 -0.076467 -1.187678 1.130127 -1.436737 6 6 -1.413681 1.607920 1.024180 0.569605 7 7 0.875906 -2.211372 0.974466 -2.006747 8 8 -0.410001 -0.078638 0.545952 -1.219217 9 9 -1.226825 0.769804 -1.281247 -0.727707 By specifying a chunksize to read_csv, the return value will be an iterable object of type TextFileReader: In [216]: with pd.read_csv("tmp.csv", sep="|", chunksize=4) as reader: .....: reader .....: for chunk in reader: .....: print(chunk) .....: Unnamed: 0 0 1 2 3 0 0 -1.294524 0.413738 0.276662 -0.472035 1 1 -0.013960 -0.362543 -0.006154 -0.923061 2 2 0.895717 0.805244 -1.206412 2.565646 3 3 1.431256 1.340309 -1.170299 -0.226169 Unnamed: 0 0 1 2 3 4 4 0.410835 0.813850 0.132003 -0.827317 5 5 -0.076467 -1.187678 1.130127 -1.436737 6 6 -1.413681 1.607920 1.024180 0.569605 7 7 0.875906 -2.211372 0.974466 -2.006747 Unnamed: 0 0 1 2 3 8 8 -0.410001 -0.078638 0.545952 -1.219217 9 9 -1.226825 0.769804 -1.281247 -0.727707 Changed in version 1.2: read_csv/json/sas return a context-manager when iterating through a file. Specifying iterator=True will also return the TextFileReader object: In [217]: with pd.read_csv("tmp.csv", sep="|", iterator=True) as reader: .....: reader.get_chunk(5) .....: Specifying the parser engine# Pandas currently supports three engines, the C engine, the python engine, and an experimental pyarrow engine (requires the pyarrow package). In general, the pyarrow engine is fastest on larger workloads and is equivalent in speed to the C engine on most other workloads. The python engine tends to be slower than the pyarrow and C engines on most workloads. However, the pyarrow engine is much less robust than the C engine, which lacks a few features compared to the Python engine. Where possible, pandas uses the C parser (specified as engine='c'), but it may fall back to Python if C-unsupported options are specified. Currently, options unsupported by the C and pyarrow engines include: sep other than a single character (e.g. regex separators) skipfooter sep=None with delim_whitespace=False Specifying any of the above options will produce a ParserWarning unless the python engine is selected explicitly using engine='python'. Options that are unsupported by the pyarrow engine which are not covered by the list above include: float_precision chunksize comment nrows thousands memory_map dialect warn_bad_lines error_bad_lines on_bad_lines delim_whitespace quoting lineterminator converters decimal iterator dayfirst infer_datetime_format verbose skipinitialspace low_memory Specifying these options with engine='pyarrow' will raise a ValueError. Reading/writing remote files# You can pass in a URL to read or write remote files to many of pandas’ IO functions - the following example shows reading a CSV file: df = pd.read_csv("https://download.bls.gov/pub/time.series/cu/cu.item", sep="\t") New in version 1.3.0. A custom header can be sent alongside HTTP(s) requests by passing a dictionary of header key value mappings to the storage_options keyword argument as shown below: headers = {"User-Agent": "pandas"} df = pd.read_csv( "https://download.bls.gov/pub/time.series/cu/cu.item", sep="\t", storage_options=headers ) All URLs which are not local files or HTTP(s) are handled by fsspec, if installed, and its various filesystem implementations (including Amazon S3, Google Cloud, SSH, FTP, webHDFS…). Some of these implementations will require additional packages to be installed, for example S3 URLs require the s3fs library: df = pd.read_json("s3://pandas-test/adatafile.json") When dealing with remote storage systems, you might need extra configuration with environment variables or config files in special locations. For example, to access data in your S3 bucket, you will need to define credentials in one of the several ways listed in the S3Fs documentation. The same is true for several of the storage backends, and you should follow the links at fsimpl1 for implementations built into fsspec and fsimpl2 for those not included in the main fsspec distribution. You can also pass parameters directly to the backend driver. For example, if you do not have S3 credentials, you can still access public data by specifying an anonymous connection, such as New in version 1.2.0. pd.read_csv( "s3://ncei-wcsd-archive/data/processed/SH1305/18kHz/SaKe2013" "-D20130523-T080854_to_SaKe2013-D20130523-T085643.csv", storage_options={"anon": True}, ) fsspec also allows complex URLs, for accessing data in compressed archives, local caching of files, and more. To locally cache the above example, you would modify the call to pd.read_csv( "simplecache::s3://ncei-wcsd-archive/data/processed/SH1305/18kHz/" "SaKe2013-D20130523-T080854_to_SaKe2013-D20130523-T085643.csv", storage_options={"s3": {"anon": True}}, ) where we specify that the “anon” parameter is meant for the “s3” part of the implementation, not to the caching implementation. Note that this caches to a temporary directory for the duration of the session only, but you can also specify a permanent store. Writing out data# Writing to CSV format# The Series and DataFrame objects have an instance method to_csv which allows storing the contents of the object as a comma-separated-values file. The function takes a number of arguments. Only the first is required. path_or_buf: A string path to the file to write or a file object. If a file object it must be opened with newline='' sep : Field delimiter for the output file (default “,”) na_rep: A string representation of a missing value (default ‘’) float_format: Format string for floating point numbers columns: Columns to write (default None) header: Whether to write out the column names (default True) index: whether to write row (index) names (default True) index_label: Column label(s) for index column(s) if desired. If None (default), and header and index are True, then the index names are used. (A sequence should be given if the DataFrame uses MultiIndex). mode : Python write mode, default ‘w’ encoding: a string representing the encoding to use if the contents are non-ASCII, for Python versions prior to 3 lineterminator: Character sequence denoting line end (default os.linesep) quoting: Set quoting rules as in csv module (default csv.QUOTE_MINIMAL). Note that if you have set a float_format then floats are converted to strings and csv.QUOTE_NONNUMERIC will treat them as non-numeric quotechar: Character used to quote fields (default ‘”’) doublequote: Control quoting of quotechar in fields (default True) escapechar: Character used to escape sep and quotechar when appropriate (default None) chunksize: Number of rows to write at a time date_format: Format string for datetime objects Writing a formatted string# The DataFrame object has an instance method to_string which allows control over the string representation of the object. All arguments are optional: buf default None, for example a StringIO object columns default None, which columns to write col_space default None, minimum width of each column. na_rep default NaN, representation of NA value formatters default None, a dictionary (by column) of functions each of which takes a single argument and returns a formatted string float_format default None, a function which takes a single (float) argument and returns a formatted string; to be applied to floats in the DataFrame. sparsify default True, set to False for a DataFrame with a hierarchical index to print every MultiIndex key at each row. index_names default True, will print the names of the indices index default True, will print the index (ie, row labels) header default True, will print the column labels justify default left, will print column headers left- or right-justified The Series object also has a to_string method, but with only the buf, na_rep, float_format arguments. There is also a length argument which, if set to True, will additionally output the length of the Series. JSON# Read and write JSON format files and strings. Writing JSON# A Series or DataFrame can be converted to a valid JSON string. Use to_json with optional parameters: path_or_buf : the pathname or buffer to write the output This can be None in which case a JSON string is returned orient : Series: default is index allowed values are {split, records, index} DataFrame: default is columns allowed values are {split, records, index, columns, values, table} The format of the JSON string split dict like {index -> [index], columns -> [columns], data -> [values]} records list like [{column -> value}, … , {column -> value}] index dict like {index -> {column -> value}} columns dict like {column -> {index -> value}} values just the values array table adhering to the JSON Table Schema date_format : string, type of date conversion, ‘epoch’ for timestamp, ‘iso’ for ISO8601. double_precision : The number of decimal places to use when encoding floating point values, default 10. force_ascii : force encoded string to be ASCII, default True. date_unit : The time unit to encode to, governs timestamp and ISO8601 precision. One of ‘s’, ‘ms’, ‘us’ or ‘ns’ for seconds, milliseconds, microseconds and nanoseconds respectively. Default ‘ms’. default_handler : The handler to call if an object cannot otherwise be converted to a suitable format for JSON. Takes a single argument, which is the object to convert, and returns a serializable object. lines : If records orient, then will write each record per line as json. Note NaN’s, NaT’s and None will be converted to null and datetime objects will be converted based on the date_format and date_unit parameters. In [218]: dfj = pd.DataFrame(np.random.randn(5, 2), columns=list("AB")) In [219]: json = dfj.to_json() In [220]: json Out[220]: '{"A":{"0":-0.1213062281,"1":0.6957746499,"2":0.9597255933,"3":-0.6199759194,"4":-0.7323393705},"B":{"0":-0.0978826728,"1":0.3417343559,"2":-1.1103361029,"3":0.1497483186,"4":0.6877383895}}' Orient options# There are a number of different options for the format of the resulting JSON file / string. Consider the following DataFrame and Series: In [221]: dfjo = pd.DataFrame( .....: dict(A=range(1, 4), B=range(4, 7), C=range(7, 10)), .....: columns=list("ABC"), .....: index=list("xyz"), .....: ) .....: In [222]: dfjo Out[222]: A B C x 1 4 7 y 2 5 8 z 3 6 9 In [223]: sjo = pd.Series(dict(x=15, y=16, z=17), name="D") In [224]: sjo Out[224]: x 15 y 16 z 17 Name: D, dtype: int64 Column oriented (the default for DataFrame) serializes the data as nested JSON objects with column labels acting as the primary index: In [225]: dfjo.to_json(orient="columns") Out[225]: '{"A":{"x":1,"y":2,"z":3},"B":{"x":4,"y":5,"z":6},"C":{"x":7,"y":8,"z":9}}' # Not available for Series Index oriented (the default for Series) similar to column oriented but the index labels are now primary: In [226]: dfjo.to_json(orient="index") Out[226]: '{"x":{"A":1,"B":4,"C":7},"y":{"A":2,"B":5,"C":8},"z":{"A":3,"B":6,"C":9}}' In [227]: sjo.to_json(orient="index") Out[227]: '{"x":15,"y":16,"z":17}' Record oriented serializes the data to a JSON array of column -> value records, index labels are not included. This is useful for passing DataFrame data to plotting libraries, for example the JavaScript library d3.js: In [228]: dfjo.to_json(orient="records") Out[228]: '[{"A":1,"B":4,"C":7},{"A":2,"B":5,"C":8},{"A":3,"B":6,"C":9}]' In [229]: sjo.to_json(orient="records") Out[229]: '[15,16,17]' Value oriented is a bare-bones option which serializes to nested JSON arrays of values only, column and index labels are not included: In [230]: dfjo.to_json(orient="values") Out[230]: '[[1,4,7],[2,5,8],[3,6,9]]' # Not available for Series Split oriented serializes to a JSON object containing separate entries for values, index and columns. Name is also included for Series: In [231]: dfjo.to_json(orient="split") Out[231]: '{"columns":["A","B","C"],"index":["x","y","z"],"data":[[1,4,7],[2,5,8],[3,6,9]]}' In [232]: sjo.to_json(orient="split") Out[232]: '{"name":"D","index":["x","y","z"],"data":[15,16,17]}' Table oriented serializes to the JSON Table Schema, allowing for the preservation of metadata including but not limited to dtypes and index names. Note Any orient option that encodes to a JSON object will not preserve the ordering of index and column labels during round-trip serialization. If you wish to preserve label ordering use the split option as it uses ordered containers. Date handling# Writing in ISO date format: In [233]: dfd = pd.DataFrame(np.random.randn(5, 2), columns=list("AB")) In [234]: dfd["date"] = pd.Timestamp("20130101") In [235]: dfd = dfd.sort_index(axis=1, ascending=False) In [236]: json = dfd.to_json(date_format="iso") In [237]: json Out[237]: '{"date":{"0":"2013-01-01T00:00:00.000","1":"2013-01-01T00:00:00.000","2":"2013-01-01T00:00:00.000","3":"2013-01-01T00:00:00.000","4":"2013-01-01T00:00:00.000"},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Writing in ISO date format, with microseconds: In [238]: json = dfd.to_json(date_format="iso", date_unit="us") In [239]: json Out[239]: '{"date":{"0":"2013-01-01T00:00:00.000000","1":"2013-01-01T00:00:00.000000","2":"2013-01-01T00:00:00.000000","3":"2013-01-01T00:00:00.000000","4":"2013-01-01T00:00:00.000000"},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Epoch timestamps, in seconds: In [240]: json = dfd.to_json(date_format="epoch", date_unit="s") In [241]: json Out[241]: '{"date":{"0":1356998400,"1":1356998400,"2":1356998400,"3":1356998400,"4":1356998400},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Writing to a file, with a date index and a date column: In [242]: dfj2 = dfj.copy() In [243]: dfj2["date"] = pd.Timestamp("20130101") In [244]: dfj2["ints"] = list(range(5)) In [245]: dfj2["bools"] = True In [246]: dfj2.index = pd.date_range("20130101", periods=5) In [247]: dfj2.to_json("test.json") In [248]: with open("test.json") as fh: .....: print(fh.read()) .....: {"A":{"1356998400000":-0.1213062281,"1357084800000":0.6957746499,"1357171200000":0.9597255933,"1357257600000":-0.6199759194,"1357344000000":-0.7323393705},"B":{"1356998400000":-0.0978826728,"1357084800000":0.3417343559,"1357171200000":-1.1103361029,"1357257600000":0.1497483186,"1357344000000":0.6877383895},"date":{"1356998400000":1356998400000,"1357084800000":1356998400000,"1357171200000":1356998400000,"1357257600000":1356998400000,"1357344000000":1356998400000},"ints":{"1356998400000":0,"1357084800000":1,"1357171200000":2,"1357257600000":3,"1357344000000":4},"bools":{"1356998400000":true,"1357084800000":true,"1357171200000":true,"1357257600000":true,"1357344000000":true}} Fallback behavior# If the JSON serializer cannot handle the container contents directly it will fall back in the following manner: if the dtype is unsupported (e.g. np.complex_) then the default_handler, if provided, will be called for each value, otherwise an exception is raised. if an object is unsupported it will attempt the following: check if the object has defined a toDict method and call it. A toDict method should return a dict which will then be JSON serialized. invoke the default_handler if one was provided. convert the object to a dict by traversing its contents. However this will often fail with an OverflowError or give unexpected results. In general the best approach for unsupported objects or dtypes is to provide a default_handler. For example: >>> DataFrame([1.0, 2.0, complex(1.0, 2.0)]).to_json() # raises RuntimeError: Unhandled numpy dtype 15 can be dealt with by specifying a simple default_handler: In [249]: pd.DataFrame([1.0, 2.0, complex(1.0, 2.0)]).to_json(default_handler=str) Out[249]: '{"0":{"0":"(1+0j)","1":"(2+0j)","2":"(1+2j)"}}' Reading JSON# Reading a JSON string to pandas object can take a number of parameters. The parser will try to parse a DataFrame if typ is not supplied or is None. To explicitly force Series parsing, pass typ=series filepath_or_buffer : a VALID JSON string or file handle / StringIO. The string could be a URL. Valid URL schemes include http, ftp, S3, and file. For file URLs, a host is expected. For instance, a local file could be file ://localhost/path/to/table.json typ : type of object to recover (series or frame), default ‘frame’ orient : Series : default is index allowed values are {split, records, index} DataFrame default is columns allowed values are {split, records, index, columns, values, table} The format of the JSON string split dict like {index -> [index], columns -> [columns], data -> [values]} records list like [{column -> value}, … , {column -> value}] index dict like {index -> {column -> value}} columns dict like {column -> {index -> value}} values just the values array table adhering to the JSON Table Schema dtype : if True, infer dtypes, if a dict of column to dtype, then use those, if False, then don’t infer dtypes at all, default is True, apply only to the data. convert_axes : boolean, try to convert the axes to the proper dtypes, default is True convert_dates : a list of columns to parse for dates; If True, then try to parse date-like columns, default is True. keep_default_dates : boolean, default True. If parsing dates, then parse the default date-like columns. numpy : direct decoding to NumPy arrays. default is False; Supports numeric data only, although labels may be non-numeric. Also note that the JSON ordering MUST be the same for each term if numpy=True. precise_float : boolean, default False. Set to enable usage of higher precision (strtod) function when decoding string to double values. Default (False) is to use fast but less precise builtin functionality. date_unit : string, the timestamp unit to detect if converting dates. Default None. By default the timestamp precision will be detected, if this is not desired then pass one of ‘s’, ‘ms’, ‘us’ or ‘ns’ to force timestamp precision to seconds, milliseconds, microseconds or nanoseconds respectively. lines : reads file as one json object per line. encoding : The encoding to use to decode py3 bytes. chunksize : when used in combination with lines=True, return a JsonReader which reads in chunksize lines per iteration. The parser will raise one of ValueError/TypeError/AssertionError if the JSON is not parseable. If a non-default orient was used when encoding to JSON be sure to pass the same option here so that decoding produces sensible results, see Orient Options for an overview. Data conversion# The default of convert_axes=True, dtype=True, and convert_dates=True will try to parse the axes, and all of the data into appropriate types, including dates. If you need to override specific dtypes, pass a dict to dtype. convert_axes should only be set to False if you need to preserve string-like numbers (e.g. ‘1’, ‘2’) in an axes. Note Large integer values may be converted to dates if convert_dates=True and the data and / or column labels appear ‘date-like’. The exact threshold depends on the date_unit specified. ‘date-like’ means that the column label meets one of the following criteria: it ends with '_at' it ends with '_time' it begins with 'timestamp' it is 'modified' it is 'date' Warning When reading JSON data, automatic coercing into dtypes has some quirks: an index can be reconstructed in a different order from serialization, that is, the returned order is not guaranteed to be the same as before serialization a column that was float data will be converted to integer if it can be done safely, e.g. a column of 1. bool columns will be converted to integer on reconstruction Thus there are times where you may want to specify specific dtypes via the dtype keyword argument. Reading from a JSON string: In [250]: pd.read_json(json) Out[250]: date B A 0 2013-01-01 0.403310 0.176444 1 2013-01-01 0.301624 -0.154951 2 2013-01-01 -1.369849 -2.179861 3 2013-01-01 1.462696 -0.954208 4 2013-01-01 -0.826591 -1.743161 Reading from a file: In [251]: pd.read_json("test.json") Out[251]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True Don’t convert any data (but still convert axes and dates): In [252]: pd.read_json("test.json", dtype=object).dtypes Out[252]: A object B object date object ints object bools object dtype: object Specify dtypes for conversion: In [253]: pd.read_json("test.json", dtype={"A": "float32", "bools": "int8"}).dtypes Out[253]: A float32 B float64 date datetime64[ns] ints int64 bools int8 dtype: object Preserve string indices: In [254]: si = pd.DataFrame( .....: np.zeros((4, 4)), columns=list(range(4)), index=[str(i) for i in range(4)] .....: ) .....: In [255]: si Out[255]: 0 1 2 3 0 0.0 0.0 0.0 0.0 1 0.0 0.0 0.0 0.0 2 0.0 0.0 0.0 0.0 3 0.0 0.0 0.0 0.0 In [256]: si.index Out[256]: Index(['0', '1', '2', '3'], dtype='object') In [257]: si.columns Out[257]: Int64Index([0, 1, 2, 3], dtype='int64') In [258]: json = si.to_json() In [259]: sij = pd.read_json(json, convert_axes=False) In [260]: sij Out[260]: 0 1 2 3 0 0 0 0 0 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 In [261]: sij.index Out[261]: Index(['0', '1', '2', '3'], dtype='object') In [262]: sij.columns Out[262]: Index(['0', '1', '2', '3'], dtype='object') Dates written in nanoseconds need to be read back in nanoseconds: In [263]: json = dfj2.to_json(date_unit="ns") # Try to parse timestamps as milliseconds -> Won't Work In [264]: dfju = pd.read_json(json, date_unit="ms") In [265]: dfju Out[265]: A B date ints bools 1356998400000000000 -0.121306 -0.097883 1356998400000000000 0 True 1357084800000000000 0.695775 0.341734 1356998400000000000 1 True 1357171200000000000 0.959726 -1.110336 1356998400000000000 2 True 1357257600000000000 -0.619976 0.149748 1356998400000000000 3 True 1357344000000000000 -0.732339 0.687738 1356998400000000000 4 True # Let pandas detect the correct precision In [266]: dfju = pd.read_json(json) In [267]: dfju Out[267]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True # Or specify that all timestamps are in nanoseconds In [268]: dfju = pd.read_json(json, date_unit="ns") In [269]: dfju Out[269]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True The Numpy parameter# Note This param has been deprecated as of version 1.0.0 and will raise a FutureWarning. This supports numeric data only. Index and columns labels may be non-numeric, e.g. strings, dates etc. If numpy=True is passed to read_json an attempt will be made to sniff an appropriate dtype during deserialization and to subsequently decode directly to NumPy arrays, bypassing the need for intermediate Python objects. This can provide speedups if you are deserialising a large amount of numeric data: In [270]: randfloats = np.random.uniform(-100, 1000, 10000) In [271]: randfloats.shape = (1000, 10) In [272]: dffloats = pd.DataFrame(randfloats, columns=list("ABCDEFGHIJ")) In [273]: jsonfloats = dffloats.to_json() In [274]: %timeit pd.read_json(jsonfloats) 7.91 ms +- 77.3 us per loop (mean +- std. dev. of 7 runs, 100 loops each) In [275]: %timeit pd.read_json(jsonfloats, numpy=True) 5.71 ms +- 333 us per loop (mean +- std. dev. of 7 runs, 100 loops each) The speedup is less noticeable for smaller datasets: In [276]: jsonfloats = dffloats.head(100).to_json() In [277]: %timeit pd.read_json(jsonfloats) 4.46 ms +- 25.9 us per loop (mean +- std. dev. of 7 runs, 100 loops each) In [278]: %timeit pd.read_json(jsonfloats, numpy=True) 4.09 ms +- 32.3 us per loop (mean +- std. dev. of 7 runs, 100 loops each) Warning Direct NumPy decoding makes a number of assumptions and may fail or produce unexpected output if these assumptions are not satisfied: data is numeric. data is uniform. The dtype is sniffed from the first value decoded. A ValueError may be raised, or incorrect output may be produced if this condition is not satisfied. labels are ordered. Labels are only read from the first container, it is assumed that each subsequent row / column has been encoded in the same order. This should be satisfied if the data was encoded using to_json but may not be the case if the JSON is from another source. Normalization# pandas provides a utility function to take a dict or list of dicts and normalize this semi-structured data into a flat table. In [279]: data = [ .....: {"id": 1, "name": {"first": "Coleen", "last": "Volk"}}, .....: {"name": {"given": "Mark", "family": "Regner"}}, .....: {"id": 2, "name": "Faye Raker"}, .....: ] .....: In [280]: pd.json_normalize(data) Out[280]: id name.first name.last name.given name.family name 0 1.0 Coleen Volk NaN NaN NaN 1 NaN NaN NaN Mark Regner NaN 2 2.0 NaN NaN NaN NaN Faye Raker In [281]: data = [ .....: { .....: "state": "Florida", .....: "shortname": "FL", .....: "info": {"governor": "Rick Scott"}, .....: "county": [ .....: {"name": "Dade", "population": 12345}, .....: {"name": "Broward", "population": 40000}, .....: {"name": "Palm Beach", "population": 60000}, .....: ], .....: }, .....: { .....: "state": "Ohio", .....: "shortname": "OH", .....: "info": {"governor": "John Kasich"}, .....: "county": [ .....: {"name": "Summit", "population": 1234}, .....: {"name": "Cuyahoga", "population": 1337}, .....: ], .....: }, .....: ] .....: In [282]: pd.json_normalize(data, "county", ["state", "shortname", ["info", "governor"]]) Out[282]: name population state shortname info.governor 0 Dade 12345 Florida FL Rick Scott 1 Broward 40000 Florida FL Rick Scott 2 Palm Beach 60000 Florida FL Rick Scott 3 Summit 1234 Ohio OH John Kasich 4 Cuyahoga 1337 Ohio OH John Kasich The max_level parameter provides more control over which level to end normalization. With max_level=1 the following snippet normalizes until 1st nesting level of the provided dict. In [283]: data = [ .....: { .....: "CreatedBy": {"Name": "User001"}, .....: "Lookup": { .....: "TextField": "Some text", .....: "UserField": {"Id": "ID001", "Name": "Name001"}, .....: }, .....: "Image": {"a": "b"}, .....: } .....: ] .....: In [284]: pd.json_normalize(data, max_level=1) Out[284]: CreatedBy.Name Lookup.TextField Lookup.UserField Image.a 0 User001 Some text {'Id': 'ID001', 'Name': 'Name001'} b Line delimited json# pandas is able to read and write line-delimited json files that are common in data processing pipelines using Hadoop or Spark. For line-delimited json files, pandas can also return an iterator which reads in chunksize lines at a time. This can be useful for large files or to read from a stream. In [285]: jsonl = """ .....: {"a": 1, "b": 2} .....: {"a": 3, "b": 4} .....: """ .....: In [286]: df = pd.read_json(jsonl, lines=True) In [287]: df Out[287]: a b 0 1 2 1 3 4 In [288]: df.to_json(orient="records", lines=True) Out[288]: '{"a":1,"b":2}\n{"a":3,"b":4}\n' # reader is an iterator that returns ``chunksize`` lines each iteration In [289]: with pd.read_json(StringIO(jsonl), lines=True, chunksize=1) as reader: .....: reader .....: for chunk in reader: .....: print(chunk) .....: Empty DataFrame Columns: [] Index: [] a b 0 1 2 a b 1 3 4 Table schema# Table Schema is a spec for describing tabular datasets as a JSON object. The JSON includes information on the field names, types, and other attributes. You can use the orient table to build a JSON string with two fields, schema and data. In [290]: df = pd.DataFrame( .....: { .....: "A": [1, 2, 3], .....: "B": ["a", "b", "c"], .....: "C": pd.date_range("2016-01-01", freq="d", periods=3), .....: }, .....: index=pd.Index(range(3), name="idx"), .....: ) .....: In [291]: df Out[291]: A B C idx 0 1 a 2016-01-01 1 2 b 2016-01-02 2 3 c 2016-01-03 In [292]: df.to_json(orient="table", date_format="iso") Out[292]: '{"schema":{"fields":[{"name":"idx","type":"integer"},{"name":"A","type":"integer"},{"name":"B","type":"string"},{"name":"C","type":"datetime"}],"primaryKey":["idx"],"pandas_version":"1.4.0"},"data":[{"idx":0,"A":1,"B":"a","C":"2016-01-01T00:00:00.000"},{"idx":1,"A":2,"B":"b","C":"2016-01-02T00:00:00.000"},{"idx":2,"A":3,"B":"c","C":"2016-01-03T00:00:00.000"}]}' The schema field contains the fields key, which itself contains a list of column name to type pairs, including the Index or MultiIndex (see below for a list of types). The schema field also contains a primaryKey field if the (Multi)index is unique. The second field, data, contains the serialized data with the records orient. The index is included, and any datetimes are ISO 8601 formatted, as required by the Table Schema spec. The full list of types supported are described in the Table Schema spec. This table shows the mapping from pandas types: pandas type Table Schema type int64 integer float64 number bool boolean datetime64[ns] datetime timedelta64[ns] duration categorical any object str A few notes on the generated table schema: The schema object contains a pandas_version field. This contains the version of pandas’ dialect of the schema, and will be incremented with each revision. All dates are converted to UTC when serializing. Even timezone naive values, which are treated as UTC with an offset of 0. In [293]: from pandas.io.json import build_table_schema In [294]: s = pd.Series(pd.date_range("2016", periods=4)) In [295]: build_table_schema(s) Out[295]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'datetime'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} datetimes with a timezone (before serializing), include an additional field tz with the time zone name (e.g. 'US/Central'). In [296]: s_tz = pd.Series(pd.date_range("2016", periods=12, tz="US/Central")) In [297]: build_table_schema(s_tz) Out[297]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'datetime', 'tz': 'US/Central'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} Periods are converted to timestamps before serialization, and so have the same behavior of being converted to UTC. In addition, periods will contain and additional field freq with the period’s frequency, e.g. 'A-DEC'. In [298]: s_per = pd.Series(1, index=pd.period_range("2016", freq="A-DEC", periods=4)) In [299]: build_table_schema(s_per) Out[299]: {'fields': [{'name': 'index', 'type': 'datetime', 'freq': 'A-DEC'}, {'name': 'values', 'type': 'integer'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} Categoricals use the any type and an enum constraint listing the set of possible values. Additionally, an ordered field is included: In [300]: s_cat = pd.Series(pd.Categorical(["a", "b", "a"])) In [301]: build_table_schema(s_cat) Out[301]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'any', 'constraints': {'enum': ['a', 'b']}, 'ordered': False}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} A primaryKey field, containing an array of labels, is included if the index is unique: In [302]: s_dupe = pd.Series([1, 2], index=[1, 1]) In [303]: build_table_schema(s_dupe) Out[303]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'integer'}], 'pandas_version': '1.4.0'} The primaryKey behavior is the same with MultiIndexes, but in this case the primaryKey is an array: In [304]: s_multi = pd.Series(1, index=pd.MultiIndex.from_product([("a", "b"), (0, 1)])) In [305]: build_table_schema(s_multi) Out[305]: {'fields': [{'name': 'level_0', 'type': 'string'}, {'name': 'level_1', 'type': 'integer'}, {'name': 'values', 'type': 'integer'}], 'primaryKey': FrozenList(['level_0', 'level_1']), 'pandas_version': '1.4.0'} The default naming roughly follows these rules: For series, the object.name is used. If that’s none, then the name is values For DataFrames, the stringified version of the column name is used For Index (not MultiIndex), index.name is used, with a fallback to index if that is None. For MultiIndex, mi.names is used. If any level has no name, then level_<i> is used. read_json also accepts orient='table' as an argument. This allows for the preservation of metadata such as dtypes and index names in a round-trippable manner. In [306]: df = pd.DataFrame( .....: { .....: "foo": [1, 2, 3, 4], .....: "bar": ["a", "b", "c", "d"], .....: "baz": pd.date_range("2018-01-01", freq="d", periods=4), .....: "qux": pd.Categorical(["a", "b", "c", "c"]), .....: }, .....: index=pd.Index(range(4), name="idx"), .....: ) .....: In [307]: df Out[307]: foo bar baz qux idx 0 1 a 2018-01-01 a 1 2 b 2018-01-02 b 2 3 c 2018-01-03 c 3 4 d 2018-01-04 c In [308]: df.dtypes Out[308]: foo int64 bar object baz datetime64[ns] qux category dtype: object In [309]: df.to_json("test.json", orient="table") In [310]: new_df = pd.read_json("test.json", orient="table") In [311]: new_df Out[311]: foo bar baz qux idx 0 1 a 2018-01-01 a 1 2 b 2018-01-02 b 2 3 c 2018-01-03 c 3 4 d 2018-01-04 c In [312]: new_df.dtypes Out[312]: foo int64 bar object baz datetime64[ns] qux category dtype: object Please note that the literal string ‘index’ as the name of an Index is not round-trippable, nor are any names beginning with 'level_' within a MultiIndex. These are used by default in DataFrame.to_json() to indicate missing values and the subsequent read cannot distinguish the intent. In [313]: df.index.name = "index" In [314]: df.to_json("test.json", orient="table") In [315]: new_df = pd.read_json("test.json", orient="table") In [316]: print(new_df.index.name) None When using orient='table' along with user-defined ExtensionArray, the generated schema will contain an additional extDtype key in the respective fields element. This extra key is not standard but does enable JSON roundtrips for extension types (e.g. read_json(df.to_json(orient="table"), orient="table")). The extDtype key carries the name of the extension, if you have properly registered the ExtensionDtype, pandas will use said name to perform a lookup into the registry and re-convert the serialized data into your custom dtype. HTML# Reading HTML content# Warning We highly encourage you to read the HTML Table Parsing gotchas below regarding the issues surrounding the BeautifulSoup4/html5lib/lxml parsers. The top-level read_html() function can accept an HTML string/file/URL and will parse HTML tables into list of pandas DataFrames. Let’s look at a few examples. Note read_html returns a list of DataFrame objects, even if there is only a single table contained in the HTML content. Read a URL with no options: In [320]: "https://www.fdic.gov/resources/resolutions/bank-failures/failed-bank-list" In [321]: pd.read_html(url) Out[321]: [ Bank NameBank CityCity StateSt ... Acquiring InstitutionAI Closing DateClosing FundFund 0 Almena State Bank Almena KS ... Equity Bank October 23, 2020 10538 1 First City Bank of Florida Fort Walton Beach FL ... United Fidelity Bank, fsb October 16, 2020 10537 2 The First State Bank Barboursville WV ... MVB Bank, Inc. April 3, 2020 10536 3 Ericson State Bank Ericson NE ... Farmers and Merchants Bank February 14, 2020 10535 4 City National Bank of New Jersey Newark NJ ... Industrial Bank November 1, 2019 10534 .. ... ... ... ... ... ... ... 558 Superior Bank, FSB Hinsdale IL ... Superior Federal, FSB July 27, 2001 6004 559 Malta National Bank Malta OH ... North Valley Bank May 3, 2001 4648 560 First Alliance Bank & Trust Co. Manchester NH ... Southern New Hampshire Bank & Trust February 2, 2001 4647 561 National State Bank of Metropolis Metropolis IL ... Banterra Bank of Marion December 14, 2000 4646 562 Bank of Honolulu Honolulu HI ... Bank of the Orient October 13, 2000 4645 [563 rows x 7 columns]] Note The data from the above URL changes every Monday so the resulting data above may be slightly different. Read in the content of the file from the above URL and pass it to read_html as a string: In [317]: html_str = """ .....: <table> .....: <tr> .....: <th>A</th> .....: <th colspan="1">B</th> .....: <th rowspan="1">C</th> .....: </tr> .....: <tr> .....: <td>a</td> .....: <td>b</td> .....: <td>c</td> .....: </tr> .....: </table> .....: """ .....: In [318]: with open("tmp.html", "w") as f: .....: f.write(html_str) .....: In [319]: df = pd.read_html("tmp.html") In [320]: df[0] Out[320]: A B C 0 a b c You can even pass in an instance of StringIO if you so desire: In [321]: dfs = pd.read_html(StringIO(html_str)) In [322]: dfs[0] Out[322]: A B C 0 a b c Note The following examples are not run by the IPython evaluator due to the fact that having so many network-accessing functions slows down the documentation build. If you spot an error or an example that doesn’t run, please do not hesitate to report it over on pandas GitHub issues page. Read a URL and match a table that contains specific text: match = "Metcalf Bank" df_list = pd.read_html(url, match=match) Specify a header row (by default <th> or <td> elements located within a <thead> are used to form the column index, if multiple rows are contained within <thead> then a MultiIndex is created); if specified, the header row is taken from the data minus the parsed header elements (<th> elements). dfs = pd.read_html(url, header=0) Specify an index column: dfs = pd.read_html(url, index_col=0) Specify a number of rows to skip: dfs = pd.read_html(url, skiprows=0) Specify a number of rows to skip using a list (range works as well): dfs = pd.read_html(url, skiprows=range(2)) Specify an HTML attribute: dfs1 = pd.read_html(url, attrs={"id": "table"}) dfs2 = pd.read_html(url, attrs={"class": "sortable"}) print(np.array_equal(dfs1[0], dfs2[0])) # Should be True Specify values that should be converted to NaN: dfs = pd.read_html(url, na_values=["No Acquirer"]) Specify whether to keep the default set of NaN values: dfs = pd.read_html(url, keep_default_na=False) Specify converters for columns. This is useful for numerical text data that has leading zeros. By default columns that are numerical are cast to numeric types and the leading zeros are lost. To avoid this, we can convert these columns to strings. url_mcc = "https://en.wikipedia.org/wiki/Mobile_country_code" dfs = pd.read_html( url_mcc, match="Telekom Albania", header=0, converters={"MNC": str}, ) Use some combination of the above: dfs = pd.read_html(url, match="Metcalf Bank", index_col=0) Read in pandas to_html output (with some loss of floating point precision): df = pd.DataFrame(np.random.randn(2, 2)) s = df.to_html(float_format="{0:.40g}".format) dfin = pd.read_html(s, index_col=0) The lxml backend will raise an error on a failed parse if that is the only parser you provide. If you only have a single parser you can provide just a string, but it is considered good practice to pass a list with one string if, for example, the function expects a sequence of strings. You may use: dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor=["lxml"]) Or you could pass flavor='lxml' without a list: dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor="lxml") However, if you have bs4 and html5lib installed and pass None or ['lxml', 'bs4'] then the parse will most likely succeed. Note that as soon as a parse succeeds, the function will return. dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor=["lxml", "bs4"]) Links can be extracted from cells along with the text using extract_links="all". In [323]: html_table = """ .....: <table> .....: <tr> .....: <th>GitHub</th> .....: </tr> .....: <tr> .....: <td><a href="https://github.com/pandas-dev/pandas">pandas</a></td> .....: </tr> .....: </table> .....: """ .....: In [324]: df = pd.read_html( .....: html_table, .....: extract_links="all" .....: )[0] .....: In [325]: df Out[325]: (GitHub, None) 0 (pandas, https://github.com/pandas-dev/pandas) In [326]: df[("GitHub", None)] Out[326]: 0 (pandas, https://github.com/pandas-dev/pandas) Name: (GitHub, None), dtype: object In [327]: df[("GitHub", None)].str[1] Out[327]: 0 https://github.com/pandas-dev/pandas Name: (GitHub, None), dtype: object New in version 1.5.0. Writing to HTML files# DataFrame objects have an instance method to_html which renders the contents of the DataFrame as an HTML table. The function arguments are as in the method to_string described above. Note Not all of the possible options for DataFrame.to_html are shown here for brevity’s sake. See to_html() for the full set of options. Note In an HTML-rendering supported environment like a Jupyter Notebook, display(HTML(...))` will render the raw HTML into the environment. In [328]: from IPython.display import display, HTML In [329]: df = pd.DataFrame(np.random.randn(2, 2)) In [330]: df Out[330]: 0 1 0 0.070319 1.773907 1 0.253908 0.414581 In [331]: html = df.to_html() In [332]: print(html) # raw html <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <th>1</th> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> In [333]: display(HTML(html)) <IPython.core.display.HTML object> The columns argument will limit the columns shown: In [334]: html = df.to_html(columns=[0]) In [335]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> </tr> <tr> <th>1</th> <td>0.253908</td> </tr> </tbody> </table> In [336]: display(HTML(html)) <IPython.core.display.HTML object> float_format takes a Python callable to control the precision of floating point values: In [337]: html = df.to_html(float_format="{0:.10f}".format) In [338]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.0703192665</td> <td>1.7739074228</td> </tr> <tr> <th>1</th> <td>0.2539083433</td> <td>0.4145805920</td> </tr> </tbody> </table> In [339]: display(HTML(html)) <IPython.core.display.HTML object> bold_rows will make the row labels bold by default, but you can turn that off: In [340]: html = df.to_html(bold_rows=False) In [341]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <td>1</td> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> In [342]: display(HTML(html)) <IPython.core.display.HTML object> The classes argument provides the ability to give the resulting HTML table CSS classes. Note that these classes are appended to the existing 'dataframe' class. In [343]: print(df.to_html(classes=["awesome_table_class", "even_more_awesome_class"])) <table border="1" class="dataframe awesome_table_class even_more_awesome_class"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <th>1</th> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> The render_links argument provides the ability to add hyperlinks to cells that contain URLs. In [344]: url_df = pd.DataFrame( .....: { .....: "name": ["Python", "pandas"], .....: "url": ["https://www.python.org/", "https://pandas.pydata.org"], .....: } .....: ) .....: In [345]: html = url_df.to_html(render_links=True) In [346]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>name</th> <th>url</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>Python</td> <td><a href="https://www.python.org/" target="_blank">https://www.python.org/</a></td> </tr> <tr> <th>1</th> <td>pandas</td> <td><a href="https://pandas.pydata.org" target="_blank">https://pandas.pydata.org</a></td> </tr> </tbody> </table> In [347]: display(HTML(html)) <IPython.core.display.HTML object> Finally, the escape argument allows you to control whether the “<”, “>” and “&” characters escaped in the resulting HTML (by default it is True). So to get the HTML without escaped characters pass escape=False In [348]: df = pd.DataFrame({"a": list("&<>"), "b": np.random.randn(3)}) Escaped: In [349]: html = df.to_html() In [350]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>a</th> <th>b</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>&amp;</td> <td>0.842321</td> </tr> <tr> <th>1</th> <td>&lt;</td> <td>0.211337</td> </tr> <tr> <th>2</th> <td>&gt;</td> <td>-1.055427</td> </tr> </tbody> </table> In [351]: display(HTML(html)) <IPython.core.display.HTML object> Not escaped: In [352]: html = df.to_html(escape=False) In [353]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>a</th> <th>b</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>&</td> <td>0.842321</td> </tr> <tr> <th>1</th> <td><</td> <td>0.211337</td> </tr> <tr> <th>2</th> <td>></td> <td>-1.055427</td> </tr> </tbody> </table> In [354]: display(HTML(html)) <IPython.core.display.HTML object> Note Some browsers may not show a difference in the rendering of the previous two HTML tables. HTML Table Parsing Gotchas# There are some versioning issues surrounding the libraries that are used to parse HTML tables in the top-level pandas io function read_html. Issues with lxml Benefits lxml is very fast. lxml requires Cython to install correctly. Drawbacks lxml does not make any guarantees about the results of its parse unless it is given strictly valid markup. In light of the above, we have chosen to allow you, the user, to use the lxml backend, but this backend will use html5lib if lxml fails to parse It is therefore highly recommended that you install both BeautifulSoup4 and html5lib, so that you will still get a valid result (provided everything else is valid) even if lxml fails. Issues with BeautifulSoup4 using lxml as a backend The above issues hold here as well since BeautifulSoup4 is essentially just a wrapper around a parser backend. Issues with BeautifulSoup4 using html5lib as a backend Benefits html5lib is far more lenient than lxml and consequently deals with real-life markup in a much saner way rather than just, e.g., dropping an element without notifying you. html5lib generates valid HTML5 markup from invalid markup automatically. This is extremely important for parsing HTML tables, since it guarantees a valid document. However, that does NOT mean that it is “correct”, since the process of fixing markup does not have a single definition. html5lib is pure Python and requires no additional build steps beyond its own installation. Drawbacks The biggest drawback to using html5lib is that it is slow as molasses. However consider the fact that many tables on the web are not big enough for the parsing algorithm runtime to matter. It is more likely that the bottleneck will be in the process of reading the raw text from the URL over the web, i.e., IO (input-output). For very large tables, this might not be true. LaTeX# New in version 1.3.0. Currently there are no methods to read from LaTeX, only output methods. Writing to LaTeX files# Note DataFrame and Styler objects currently have a to_latex method. We recommend using the Styler.to_latex() method over DataFrame.to_latex() due to the former’s greater flexibility with conditional styling, and the latter’s possible future deprecation. Review the documentation for Styler.to_latex, which gives examples of conditional styling and explains the operation of its keyword arguments. For simple application the following pattern is sufficient. In [355]: df = pd.DataFrame([[1, 2], [3, 4]], index=["a", "b"], columns=["c", "d"]) In [356]: print(df.style.to_latex()) \begin{tabular}{lrr} & c & d \\ a & 1 & 2 \\ b & 3 & 4 \\ \end{tabular} To format values before output, chain the Styler.format method. In [357]: print(df.style.format("€ {}").to_latex()) \begin{tabular}{lrr} & c & d \\ a & € 1 & € 2 \\ b & € 3 & € 4 \\ \end{tabular} XML# Reading XML# New in version 1.3.0. The top-level read_xml() function can accept an XML string/file/URL and will parse nodes and attributes into a pandas DataFrame. Note Since there is no standard XML structure where design types can vary in many ways, read_xml works best with flatter, shallow versions. If an XML document is deeply nested, use the stylesheet feature to transform XML into a flatter version. Let’s look at a few examples. Read an XML string: In [358]: xml = """<?xml version="1.0" encoding="UTF-8"?> .....: <bookstore> .....: <book category="cooking"> .....: <title lang="en">Everyday Italian</title> .....: <author>Giada De Laurentiis</author> .....: <year>2005</year> .....: <price>30.00</price> .....: </book> .....: <book category="children"> .....: <title lang="en">Harry Potter</title> .....: <author>J K. Rowling</author> .....: <year>2005</year> .....: <price>29.99</price> .....: </book> .....: <book category="web"> .....: <title lang="en">Learning XML</title> .....: <author>Erik T. Ray</author> .....: <year>2003</year> .....: <price>39.95</price> .....: </book> .....: </bookstore>""" .....: In [359]: df = pd.read_xml(xml) In [360]: df Out[360]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Read a URL with no options: In [361]: df = pd.read_xml("https://www.w3schools.com/xml/books.xml") In [362]: df Out[362]: category title author year price cover 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 None 1 children Harry Potter J K. Rowling 2005 29.99 None 2 web XQuery Kick Start Vaidyanathan Nagarajan 2003 49.99 None 3 web Learning XML Erik T. Ray 2003 39.95 paperback Read in the content of the “books.xml” file and pass it to read_xml as a string: In [363]: file_path = "books.xml" In [364]: with open(file_path, "w") as f: .....: f.write(xml) .....: In [365]: with open(file_path, "r") as f: .....: df = pd.read_xml(f.read()) .....: In [366]: df Out[366]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Read in the content of the “books.xml” as instance of StringIO or BytesIO and pass it to read_xml: In [367]: with open(file_path, "r") as f: .....: sio = StringIO(f.read()) .....: In [368]: df = pd.read_xml(sio) In [369]: df Out[369]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 In [370]: with open(file_path, "rb") as f: .....: bio = BytesIO(f.read()) .....: In [371]: df = pd.read_xml(bio) In [372]: df Out[372]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Even read XML from AWS S3 buckets such as NIH NCBI PMC Article Datasets providing Biomedical and Life Science Jorurnals: In [373]: df = pd.read_xml( .....: "s3://pmc-oa-opendata/oa_comm/xml/all/PMC1236943.xml", .....: xpath=".//journal-meta", .....: ) .....: In [374]: df Out[374]: journal-id journal-title issn publisher 0 Cardiovasc Ultrasound Cardiovascular Ultrasound 1476-7120 NaN With lxml as default parser, you access the full-featured XML library that extends Python’s ElementTree API. One powerful tool is ability to query nodes selectively or conditionally with more expressive XPath: In [375]: df = pd.read_xml(file_path, xpath="//book[year=2005]") In [376]: df Out[376]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 Specify only elements or only attributes to parse: In [377]: df = pd.read_xml(file_path, elems_only=True) In [378]: df Out[378]: title author year price 0 Everyday Italian Giada De Laurentiis 2005 30.00 1 Harry Potter J K. Rowling 2005 29.99 2 Learning XML Erik T. Ray 2003 39.95 In [379]: df = pd.read_xml(file_path, attrs_only=True) In [380]: df Out[380]: category 0 cooking 1 children 2 web XML documents can have namespaces with prefixes and default namespaces without prefixes both of which are denoted with a special attribute xmlns. In order to parse by node under a namespace context, xpath must reference a prefix. For example, below XML contains a namespace with prefix, doc, and URI at https://example.com. In order to parse doc:row nodes, namespaces must be used. In [381]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <doc:data xmlns:doc="https://example.com"> .....: <doc:row> .....: <doc:shape>square</doc:shape> .....: <doc:degrees>360</doc:degrees> .....: <doc:sides>4.0</doc:sides> .....: </doc:row> .....: <doc:row> .....: <doc:shape>circle</doc:shape> .....: <doc:degrees>360</doc:degrees> .....: <doc:sides/> .....: </doc:row> .....: <doc:row> .....: <doc:shape>triangle</doc:shape> .....: <doc:degrees>180</doc:degrees> .....: <doc:sides>3.0</doc:sides> .....: </doc:row> .....: </doc:data>""" .....: In [382]: df = pd.read_xml(xml, .....: xpath="//doc:row", .....: namespaces={"doc": "https://example.com"}) .....: In [383]: df Out[383]: shape degrees sides 0 square 360 4.0 1 circle 360 NaN 2 triangle 180 3.0 Similarly, an XML document can have a default namespace without prefix. Failing to assign a temporary prefix will return no nodes and raise a ValueError. But assigning any temporary name to correct URI allows parsing by nodes. In [384]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <data xmlns="https://example.com"> .....: <row> .....: <shape>square</shape> .....: <degrees>360</degrees> .....: <sides>4.0</sides> .....: </row> .....: <row> .....: <shape>circle</shape> .....: <degrees>360</degrees> .....: <sides/> .....: </row> .....: <row> .....: <shape>triangle</shape> .....: <degrees>180</degrees> .....: <sides>3.0</sides> .....: </row> .....: </data>""" .....: In [385]: df = pd.read_xml(xml, .....: xpath="//pandas:row", .....: namespaces={"pandas": "https://example.com"}) .....: In [386]: df Out[386]: shape degrees sides 0 square 360 4.0 1 circle 360 NaN 2 triangle 180 3.0 However, if XPath does not reference node names such as default, /*, then namespaces is not required. With lxml as parser, you can flatten nested XML documents with an XSLT script which also can be string/file/URL types. As background, XSLT is a special-purpose language written in a special XML file that can transform original XML documents into other XML, HTML, even text (CSV, JSON, etc.) using an XSLT processor. For example, consider this somewhat nested structure of Chicago “L” Rides where station and rides elements encapsulate data in their own sections. With below XSLT, lxml can transform original nested document into a flatter output (as shown below for demonstration) for easier parse into DataFrame: In [387]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <response> .....: <row> .....: <station id="40850" name="Library"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>864.2</avg_weekday_rides> .....: <avg_saturday_rides>534</avg_saturday_rides> .....: <avg_sunday_holiday_rides>417.2</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: <row> .....: <station id="41700" name="Washington/Wabash"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>2707.4</avg_weekday_rides> .....: <avg_saturday_rides>1909.8</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1438.6</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: <row> .....: <station id="40380" name="Clark/Lake"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>2949.6</avg_weekday_rides> .....: <avg_saturday_rides>1657</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1453.8</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: </response>""" .....: In [388]: xsl = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> .....: <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/> .....: <xsl:strip-space elements="*"/> .....: <xsl:template match="/response"> .....: <xsl:copy> .....: <xsl:apply-templates select="row"/> .....: </xsl:copy> .....: </xsl:template> .....: <xsl:template match="row"> .....: <xsl:copy> .....: <station_id><xsl:value-of select="station/@id"/></station_id> .....: <station_name><xsl:value-of select="station/@name"/></station_name> .....: <xsl:copy-of select="month|rides/*"/> .....: </xsl:copy> .....: </xsl:template> .....: </xsl:stylesheet>""" .....: In [389]: output = """<?xml version='1.0' encoding='utf-8'?> .....: <response> .....: <row> .....: <station_id>40850</station_id> .....: <station_name>Library</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>864.2</avg_weekday_rides> .....: <avg_saturday_rides>534</avg_saturday_rides> .....: <avg_sunday_holiday_rides>417.2</avg_sunday_holiday_rides> .....: </row> .....: <row> .....: <station_id>41700</station_id> .....: <station_name>Washington/Wabash</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>2707.4</avg_weekday_rides> .....: <avg_saturday_rides>1909.8</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1438.6</avg_sunday_holiday_rides> .....: </row> .....: <row> .....: <station_id>40380</station_id> .....: <station_name>Clark/Lake</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>2949.6</avg_weekday_rides> .....: <avg_saturday_rides>1657</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1453.8</avg_sunday_holiday_rides> .....: </row> .....: </response>""" .....: In [390]: df = pd.read_xml(xml, stylesheet=xsl) In [391]: df Out[391]: station_id station_name ... avg_saturday_rides avg_sunday_holiday_rides 0 40850 Library ... 534.0 417.2 1 41700 Washington/Wabash ... 1909.8 1438.6 2 40380 Clark/Lake ... 1657.0 1453.8 [3 rows x 6 columns] For very large XML files that can range in hundreds of megabytes to gigabytes, pandas.read_xml() supports parsing such sizeable files using lxml’s iterparse and etree’s iterparse which are memory-efficient methods to iterate through an XML tree and extract specific elements and attributes. without holding entire tree in memory. New in version 1.5.0. To use this feature, you must pass a physical XML file path into read_xml and use the iterparse argument. Files should not be compressed or point to online sources but stored on local disk. Also, iterparse should be a dictionary where the key is the repeating nodes in document (which become the rows) and the value is a list of any element or attribute that is a descendant (i.e., child, grandchild) of repeating node. Since XPath is not used in this method, descendants do not need to share same relationship with one another. Below shows example of reading in Wikipedia’s very large (12 GB+) latest article data dump. In [1]: df = pd.read_xml( ... "/path/to/downloaded/enwikisource-latest-pages-articles.xml", ... iterparse = {"page": ["title", "ns", "id"]} ... ) ... df Out[2]: title ns id 0 Gettysburg Address 0 21450 1 Main Page 0 42950 2 Declaration by United Nations 0 8435 3 Constitution of the United States of America 0 8435 4 Declaration of Independence (Israel) 0 17858 ... ... ... ... 3578760 Page:Black cat 1897 07 v2 n10.pdf/17 104 219649 3578761 Page:Black cat 1897 07 v2 n10.pdf/43 104 219649 3578762 Page:Black cat 1897 07 v2 n10.pdf/44 104 219649 3578763 The History of Tom Jones, a Foundling/Book IX 0 12084291 3578764 Page:Shakespeare of Stratford (1926) Yale.djvu/91 104 21450 [3578765 rows x 3 columns] Writing XML# New in version 1.3.0. DataFrame objects have an instance method to_xml which renders the contents of the DataFrame as an XML document. Note This method does not support special properties of XML including DTD, CData, XSD schemas, processing instructions, comments, and others. Only namespaces at the root level is supported. However, stylesheet allows design changes after initial output. Let’s look at a few examples. Write an XML without options: In [392]: geom_df = pd.DataFrame( .....: { .....: "shape": ["square", "circle", "triangle"], .....: "degrees": [360, 360, 180], .....: "sides": [4, np.nan, 3], .....: } .....: ) .....: In [393]: print(geom_df.to_xml()) <?xml version='1.0' encoding='utf-8'?> <data> <row> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </row> <row> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </row> <row> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Write an XML with new root and row name: In [394]: print(geom_df.to_xml(root_name="geometry", row_name="objects")) <?xml version='1.0' encoding='utf-8'?> <geometry> <objects> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </objects> <objects> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </objects> <objects> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </objects> </geometry> Write an attribute-centric XML: In [395]: print(geom_df.to_xml(attr_cols=geom_df.columns.tolist())) <?xml version='1.0' encoding='utf-8'?> <data> <row index="0" shape="square" degrees="360" sides="4.0"/> <row index="1" shape="circle" degrees="360"/> <row index="2" shape="triangle" degrees="180" sides="3.0"/> </data> Write a mix of elements and attributes: In [396]: print( .....: geom_df.to_xml( .....: index=False, .....: attr_cols=['shape'], .....: elem_cols=['degrees', 'sides']) .....: ) .....: <?xml version='1.0' encoding='utf-8'?> <data> <row shape="square"> <degrees>360</degrees> <sides>4.0</sides> </row> <row shape="circle"> <degrees>360</degrees> <sides/> </row> <row shape="triangle"> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Any DataFrames with hierarchical columns will be flattened for XML element names with levels delimited by underscores: In [397]: ext_geom_df = pd.DataFrame( .....: { .....: "type": ["polygon", "other", "polygon"], .....: "shape": ["square", "circle", "triangle"], .....: "degrees": [360, 360, 180], .....: "sides": [4, np.nan, 3], .....: } .....: ) .....: In [398]: pvt_df = ext_geom_df.pivot_table(index='shape', .....: columns='type', .....: values=['degrees', 'sides'], .....: aggfunc='sum') .....: In [399]: pvt_df Out[399]: degrees sides type other polygon other polygon shape circle 360.0 NaN 0.0 NaN square NaN 360.0 NaN 4.0 triangle NaN 180.0 NaN 3.0 In [400]: print(pvt_df.to_xml()) <?xml version='1.0' encoding='utf-8'?> <data> <row> <shape>circle</shape> <degrees_other>360.0</degrees_other> <degrees_polygon/> <sides_other>0.0</sides_other> <sides_polygon/> </row> <row> <shape>square</shape> <degrees_other/> <degrees_polygon>360.0</degrees_polygon> <sides_other/> <sides_polygon>4.0</sides_polygon> </row> <row> <shape>triangle</shape> <degrees_other/> <degrees_polygon>180.0</degrees_polygon> <sides_other/> <sides_polygon>3.0</sides_polygon> </row> </data> Write an XML with default namespace: In [401]: print(geom_df.to_xml(namespaces={"": "https://example.com"})) <?xml version='1.0' encoding='utf-8'?> <data xmlns="https://example.com"> <row> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </row> <row> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </row> <row> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Write an XML with namespace prefix: In [402]: print( .....: geom_df.to_xml(namespaces={"doc": "https://example.com"}, .....: prefix="doc") .....: ) .....: <?xml version='1.0' encoding='utf-8'?> <doc:data xmlns:doc="https://example.com"> <doc:row> <doc:index>0</doc:index> <doc:shape>square</doc:shape> <doc:degrees>360</doc:degrees> <doc:sides>4.0</doc:sides> </doc:row> <doc:row> <doc:index>1</doc:index> <doc:shape>circle</doc:shape> <doc:degrees>360</doc:degrees> <doc:sides/> </doc:row> <doc:row> <doc:index>2</doc:index> <doc:shape>triangle</doc:shape> <doc:degrees>180</doc:degrees> <doc:sides>3.0</doc:sides> </doc:row> </doc:data> Write an XML without declaration or pretty print: In [403]: print( .....: geom_df.to_xml(xml_declaration=False, .....: pretty_print=False) .....: ) .....: <data><row><index>0</index><shape>square</shape><degrees>360</degrees><sides>4.0</sides></row><row><index>1</index><shape>circle</shape><degrees>360</degrees><sides/></row><row><index>2</index><shape>triangle</shape><degrees>180</degrees><sides>3.0</sides></row></data> Write an XML and transform with stylesheet: In [404]: xsl = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> .....: <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/> .....: <xsl:strip-space elements="*"/> .....: <xsl:template match="/data"> .....: <geometry> .....: <xsl:apply-templates select="row"/> .....: </geometry> .....: </xsl:template> .....: <xsl:template match="row"> .....: <object index="{index}"> .....: <xsl:if test="shape!='circle'"> .....: <xsl:attribute name="type">polygon</xsl:attribute> .....: </xsl:if> .....: <xsl:copy-of select="shape"/> .....: <property> .....: <xsl:copy-of select="degrees|sides"/> .....: </property> .....: </object> .....: </xsl:template> .....: </xsl:stylesheet>""" .....: In [405]: print(geom_df.to_xml(stylesheet=xsl)) <?xml version="1.0"?> <geometry> <object index="0" type="polygon"> <shape>square</shape> <property> <degrees>360</degrees> <sides>4.0</sides> </property> </object> <object index="1"> <shape>circle</shape> <property> <degrees>360</degrees> <sides/> </property> </object> <object index="2" type="polygon"> <shape>triangle</shape> <property> <degrees>180</degrees> <sides>3.0</sides> </property> </object> </geometry> XML Final Notes# All XML documents adhere to W3C specifications. Both etree and lxml parsers will fail to parse any markup document that is not well-formed or follows XML syntax rules. Do be aware HTML is not an XML document unless it follows XHTML specs. However, other popular markup types including KML, XAML, RSS, MusicML, MathML are compliant XML schemas. For above reason, if your application builds XML prior to pandas operations, use appropriate DOM libraries like etree and lxml to build the necessary document and not by string concatenation or regex adjustments. Always remember XML is a special text file with markup rules. With very large XML files (several hundred MBs to GBs), XPath and XSLT can become memory-intensive operations. Be sure to have enough available RAM for reading and writing to large XML files (roughly about 5 times the size of text). Because XSLT is a programming language, use it with caution since such scripts can pose a security risk in your environment and can run large or infinite recursive operations. Always test scripts on small fragments before full run. The etree parser supports all functionality of both read_xml and to_xml except for complex XPath and any XSLT. Though limited in features, etree is still a reliable and capable parser and tree builder. Its performance may trail lxml to a certain degree for larger files but relatively unnoticeable on small to medium size files. Excel files# The read_excel() method can read Excel 2007+ (.xlsx) files using the openpyxl Python module. Excel 2003 (.xls) files can be read using xlrd. Binary Excel (.xlsb) files can be read using pyxlsb. The to_excel() instance method is used for saving a DataFrame to Excel. Generally the semantics are similar to working with csv data. See the cookbook for some advanced strategies. Warning The xlwt package for writing old-style .xls excel files is no longer maintained. The xlrd package is now only for reading old-style .xls files. Before pandas 1.3.0, the default argument engine=None to read_excel() would result in using the xlrd engine in many cases, including new Excel 2007+ (.xlsx) files. pandas will now default to using the openpyxl engine. It is strongly encouraged to install openpyxl to read Excel 2007+ (.xlsx) files. Please do not report issues when using ``xlrd`` to read ``.xlsx`` files. This is no longer supported, switch to using openpyxl instead. Attempting to use the xlwt engine will raise a FutureWarning unless the option io.excel.xls.writer is set to "xlwt". While this option is now deprecated and will also raise a FutureWarning, it can be globally set and the warning suppressed. Users are recommended to write .xlsx files using the openpyxl engine instead. Reading Excel files# In the most basic use-case, read_excel takes a path to an Excel file, and the sheet_name indicating which sheet to parse. # Returns a DataFrame pd.read_excel("path_to_file.xls", sheet_name="Sheet1") ExcelFile class# To facilitate working with multiple sheets from the same file, the ExcelFile class can be used to wrap the file and can be passed into read_excel There will be a performance benefit for reading multiple sheets as the file is read into memory only once. xlsx = pd.ExcelFile("path_to_file.xls") df = pd.read_excel(xlsx, "Sheet1") The ExcelFile class can also be used as a context manager. with pd.ExcelFile("path_to_file.xls") as xls: df1 = pd.read_excel(xls, "Sheet1") df2 = pd.read_excel(xls, "Sheet2") The sheet_names property will generate a list of the sheet names in the file. The primary use-case for an ExcelFile is parsing multiple sheets with different parameters: data = {} # For when Sheet1's format differs from Sheet2 with pd.ExcelFile("path_to_file.xls") as xls: data["Sheet1"] = pd.read_excel(xls, "Sheet1", index_col=None, na_values=["NA"]) data["Sheet2"] = pd.read_excel(xls, "Sheet2", index_col=1) Note that if the same parsing parameters are used for all sheets, a list of sheet names can simply be passed to read_excel with no loss in performance. # using the ExcelFile class data = {} with pd.ExcelFile("path_to_file.xls") as xls: data["Sheet1"] = pd.read_excel(xls, "Sheet1", index_col=None, na_values=["NA"]) data["Sheet2"] = pd.read_excel(xls, "Sheet2", index_col=None, na_values=["NA"]) # equivalent using the read_excel function data = pd.read_excel( "path_to_file.xls", ["Sheet1", "Sheet2"], index_col=None, na_values=["NA"] ) ExcelFile can also be called with a xlrd.book.Book object as a parameter. This allows the user to control how the excel file is read. For example, sheets can be loaded on demand by calling xlrd.open_workbook() with on_demand=True. import xlrd xlrd_book = xlrd.open_workbook("path_to_file.xls", on_demand=True) with pd.ExcelFile(xlrd_book) as xls: df1 = pd.read_excel(xls, "Sheet1") df2 = pd.read_excel(xls, "Sheet2") Specifying sheets# Note The second argument is sheet_name, not to be confused with ExcelFile.sheet_names. Note An ExcelFile’s attribute sheet_names provides access to a list of sheets. The arguments sheet_name allows specifying the sheet or sheets to read. The default value for sheet_name is 0, indicating to read the first sheet Pass a string to refer to the name of a particular sheet in the workbook. Pass an integer to refer to the index of a sheet. Indices follow Python convention, beginning at 0. Pass a list of either strings or integers, to return a dictionary of specified sheets. Pass a None to return a dictionary of all available sheets. # Returns a DataFrame pd.read_excel("path_to_file.xls", "Sheet1", index_col=None, na_values=["NA"]) Using the sheet index: # Returns a DataFrame pd.read_excel("path_to_file.xls", 0, index_col=None, na_values=["NA"]) Using all default values: # Returns a DataFrame pd.read_excel("path_to_file.xls") Using None to get all sheets: # Returns a dictionary of DataFrames pd.read_excel("path_to_file.xls", sheet_name=None) Using a list to get multiple sheets: # Returns the 1st and 4th sheet, as a dictionary of DataFrames. pd.read_excel("path_to_file.xls", sheet_name=["Sheet1", 3]) read_excel can read more than one sheet, by setting sheet_name to either a list of sheet names, a list of sheet positions, or None to read all sheets. Sheets can be specified by sheet index or sheet name, using an integer or string, respectively. Reading a MultiIndex# read_excel can read a MultiIndex index, by passing a list of columns to index_col and a MultiIndex column by passing a list of rows to header. If either the index or columns have serialized level names those will be read in as well by specifying the rows/columns that make up the levels. For example, to read in a MultiIndex index without names: In [406]: df = pd.DataFrame( .....: {"a": [1, 2, 3, 4], "b": [5, 6, 7, 8]}, .....: index=pd.MultiIndex.from_product([["a", "b"], ["c", "d"]]), .....: ) .....: In [407]: df.to_excel("path_to_file.xlsx") In [408]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1]) In [409]: df Out[409]: a b a c 1 5 d 2 6 b c 3 7 d 4 8 If the index has level names, they will parsed as well, using the same parameters. In [410]: df.index = df.index.set_names(["lvl1", "lvl2"]) In [411]: df.to_excel("path_to_file.xlsx") In [412]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1]) In [413]: df Out[413]: a b lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 If the source file has both MultiIndex index and columns, lists specifying each should be passed to index_col and header: In [414]: df.columns = pd.MultiIndex.from_product([["a"], ["b", "d"]], names=["c1", "c2"]) In [415]: df.to_excel("path_to_file.xlsx") In [416]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1], header=[0, 1]) In [417]: df Out[417]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 Missing values in columns specified in index_col will be forward filled to allow roundtripping with to_excel for merged_cells=True. To avoid forward filling the missing values use set_index after reading the data instead of index_col. Parsing specific columns# It is often the case that users will insert columns to do temporary computations in Excel and you may not want to read in those columns. read_excel takes a usecols keyword to allow you to specify a subset of columns to parse. Changed in version 1.0.0. Passing in an integer for usecols will no longer work. Please pass in a list of ints from 0 to usecols inclusive instead. You can specify a comma-delimited set of Excel columns and ranges as a string: pd.read_excel("path_to_file.xls", "Sheet1", usecols="A,C:E") If usecols is a list of integers, then it is assumed to be the file column indices to be parsed. pd.read_excel("path_to_file.xls", "Sheet1", usecols=[0, 2, 3]) Element order is ignored, so usecols=[0, 1] is the same as [1, 0]. If usecols is a list of strings, it is assumed that each string corresponds to a column name provided either by the user in names or inferred from the document header row(s). Those strings define which columns will be parsed: pd.read_excel("path_to_file.xls", "Sheet1", usecols=["foo", "bar"]) Element order is ignored, so usecols=['baz', 'joe'] is the same as ['joe', 'baz']. If usecols is callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True. pd.read_excel("path_to_file.xls", "Sheet1", usecols=lambda x: x.isalpha()) Parsing dates# Datetime-like values are normally automatically converted to the appropriate dtype when reading the excel file. But if you have a column of strings that look like dates (but are not actually formatted as dates in excel), you can use the parse_dates keyword to parse those strings to datetimes: pd.read_excel("path_to_file.xls", "Sheet1", parse_dates=["date_strings"]) Cell converters# It is possible to transform the contents of Excel cells via the converters option. For instance, to convert a column to boolean: pd.read_excel("path_to_file.xls", "Sheet1", converters={"MyBools": bool}) This options handles missing values and treats exceptions in the converters as missing data. Transformations are applied cell by cell rather than to the column as a whole, so the array dtype is not guaranteed. For instance, a column of integers with missing values cannot be transformed to an array with integer dtype, because NaN is strictly a float. You can manually mask missing data to recover integer dtype: def cfun(x): return int(x) if x else -1 pd.read_excel("path_to_file.xls", "Sheet1", converters={"MyInts": cfun}) Dtype specifications# As an alternative to converters, the type for an entire column can be specified using the dtype keyword, which takes a dictionary mapping column names to types. To interpret data with no type inference, use the type str or object. pd.read_excel("path_to_file.xls", dtype={"MyInts": "int64", "MyText": str}) Writing Excel files# Writing Excel files to disk# To write a DataFrame object to a sheet of an Excel file, you can use the to_excel instance method. The arguments are largely the same as to_csv described above, the first argument being the name of the excel file, and the optional second argument the name of the sheet to which the DataFrame should be written. For example: df.to_excel("path_to_file.xlsx", sheet_name="Sheet1") Files with a .xls extension will be written using xlwt and those with a .xlsx extension will be written using xlsxwriter (if available) or openpyxl. The DataFrame will be written in a way that tries to mimic the REPL output. The index_label will be placed in the second row instead of the first. You can place it in the first row by setting the merge_cells option in to_excel() to False: df.to_excel("path_to_file.xlsx", index_label="label", merge_cells=False) In order to write separate DataFrames to separate sheets in a single Excel file, one can pass an ExcelWriter. with pd.ExcelWriter("path_to_file.xlsx") as writer: df1.to_excel(writer, sheet_name="Sheet1") df2.to_excel(writer, sheet_name="Sheet2") Writing Excel files to memory# pandas supports writing Excel files to buffer-like objects such as StringIO or BytesIO using ExcelWriter. from io import BytesIO bio = BytesIO() # By setting the 'engine' in the ExcelWriter constructor. writer = pd.ExcelWriter(bio, engine="xlsxwriter") df.to_excel(writer, sheet_name="Sheet1") # Save the workbook writer.save() # Seek to the beginning and read to copy the workbook to a variable in memory bio.seek(0) workbook = bio.read() Note engine is optional but recommended. Setting the engine determines the version of workbook produced. Setting engine='xlrd' will produce an Excel 2003-format workbook (xls). Using either 'openpyxl' or 'xlsxwriter' will produce an Excel 2007-format workbook (xlsx). If omitted, an Excel 2007-formatted workbook is produced. Excel writer engines# Deprecated since version 1.2.0: As the xlwt package is no longer maintained, the xlwt engine will be removed from a future version of pandas. This is the only engine in pandas that supports writing to .xls files. pandas chooses an Excel writer via two methods: the engine keyword argument the filename extension (via the default specified in config options) By default, pandas uses the XlsxWriter for .xlsx, openpyxl for .xlsm, and xlwt for .xls files. If you have multiple engines installed, you can set the default engine through setting the config options io.excel.xlsx.writer and io.excel.xls.writer. pandas will fall back on openpyxl for .xlsx files if Xlsxwriter is not available. To specify which writer you want to use, you can pass an engine keyword argument to to_excel and to ExcelWriter. The built-in engines are: openpyxl: version 2.4 or higher is required xlsxwriter xlwt # By setting the 'engine' in the DataFrame 'to_excel()' methods. df.to_excel("path_to_file.xlsx", sheet_name="Sheet1", engine="xlsxwriter") # By setting the 'engine' in the ExcelWriter constructor. writer = pd.ExcelWriter("path_to_file.xlsx", engine="xlsxwriter") # Or via pandas configuration. from pandas import options # noqa: E402 options.io.excel.xlsx.writer = "xlsxwriter" df.to_excel("path_to_file.xlsx", sheet_name="Sheet1") Style and formatting# The look and feel of Excel worksheets created from pandas can be modified using the following parameters on the DataFrame’s to_excel method. float_format : Format string for floating point numbers (default None). freeze_panes : A tuple of two integers representing the bottommost row and rightmost column to freeze. Each of these parameters is one-based, so (1, 1) will freeze the first row and first column (default None). Using the Xlsxwriter engine provides many options for controlling the format of an Excel worksheet created with the to_excel method. Excellent examples can be found in the Xlsxwriter documentation here: https://xlsxwriter.readthedocs.io/working_with_pandas.html OpenDocument Spreadsheets# New in version 0.25. The read_excel() method can also read OpenDocument spreadsheets using the odfpy module. The semantics and features for reading OpenDocument spreadsheets match what can be done for Excel files using engine='odf'. # Returns a DataFrame pd.read_excel("path_to_file.ods", engine="odf") Note Currently pandas only supports reading OpenDocument spreadsheets. Writing is not implemented. Binary Excel (.xlsb) files# New in version 1.0.0. The read_excel() method can also read binary Excel files using the pyxlsb module. The semantics and features for reading binary Excel files mostly match what can be done for Excel files using engine='pyxlsb'. pyxlsb does not recognize datetime types in files and will return floats instead. # Returns a DataFrame pd.read_excel("path_to_file.xlsb", engine="pyxlsb") Note Currently pandas only supports reading binary Excel files. Writing is not implemented. Clipboard# A handy way to grab data is to use the read_clipboard() method, which takes the contents of the clipboard buffer and passes them to the read_csv method. For instance, you can copy the following text to the clipboard (CTRL-C on many operating systems): A B C x 1 4 p y 2 5 q z 3 6 r And then import the data directly to a DataFrame by calling: >>> clipdf = pd.read_clipboard() >>> clipdf A B C x 1 4 p y 2 5 q z 3 6 r The to_clipboard method can be used to write the contents of a DataFrame to the clipboard. Following which you can paste the clipboard contents into other applications (CTRL-V on many operating systems). Here we illustrate writing a DataFrame into clipboard and reading it back. >>> df = pd.DataFrame( ... {"A": [1, 2, 3], "B": [4, 5, 6], "C": ["p", "q", "r"]}, index=["x", "y", "z"] ... ) >>> df A B C x 1 4 p y 2 5 q z 3 6 r >>> df.to_clipboard() >>> pd.read_clipboard() A B C x 1 4 p y 2 5 q z 3 6 r We can see that we got the same content back, which we had earlier written to the clipboard. Note You may need to install xclip or xsel (with PyQt5, PyQt4 or qtpy) on Linux to use these methods. Pickling# All pandas objects are equipped with to_pickle methods which use Python’s cPickle module to save data structures to disk using the pickle format. In [418]: df Out[418]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 In [419]: df.to_pickle("foo.pkl") The read_pickle function in the pandas namespace can be used to load any pickled pandas object (or any other pickled object) from file: In [420]: pd.read_pickle("foo.pkl") Out[420]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 Warning Loading pickled data received from untrusted sources can be unsafe. See: https://docs.python.org/3/library/pickle.html Warning read_pickle() is only guaranteed backwards compatible back to pandas version 0.20.3 Compressed pickle files# read_pickle(), DataFrame.to_pickle() and Series.to_pickle() can read and write compressed pickle files. The compression types of gzip, bz2, xz, zstd are supported for reading and writing. The zip file format only supports reading and must contain only one data file to be read. The compression type can be an explicit parameter or be inferred from the file extension. If ‘infer’, then use gzip, bz2, zip, xz, zstd if filename ends in '.gz', '.bz2', '.zip', '.xz', or '.zst', respectively. The compression parameter can also be a dict in order to pass options to the compression protocol. It must have a 'method' key set to the name of the compression protocol, which must be one of {'zip', 'gzip', 'bz2', 'xz', 'zstd'}. All other key-value pairs are passed to the underlying compression library. In [421]: df = pd.DataFrame( .....: { .....: "A": np.random.randn(1000), .....: "B": "foo", .....: "C": pd.date_range("20130101", periods=1000, freq="s"), .....: } .....: ) .....: In [422]: df Out[422]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] Using an explicit compression type: In [423]: df.to_pickle("data.pkl.compress", compression="gzip") In [424]: rt = pd.read_pickle("data.pkl.compress", compression="gzip") In [425]: rt Out[425]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] Inferring compression type from the extension: In [426]: df.to_pickle("data.pkl.xz", compression="infer") In [427]: rt = pd.read_pickle("data.pkl.xz", compression="infer") In [428]: rt Out[428]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] The default is to ‘infer’: In [429]: df.to_pickle("data.pkl.gz") In [430]: rt = pd.read_pickle("data.pkl.gz") In [431]: rt Out[431]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] In [432]: df["A"].to_pickle("s1.pkl.bz2") In [433]: rt = pd.read_pickle("s1.pkl.bz2") In [434]: rt Out[434]: 0 -0.828876 1 -0.110383 2 2.357598 3 -1.620073 4 0.440903 ... 995 -1.177365 996 1.236988 997 0.743946 998 -0.533097 999 -0.140850 Name: A, Length: 1000, dtype: float64 Passing options to the compression protocol in order to speed up compression: In [435]: df.to_pickle("data.pkl.gz", compression={"method": "gzip", "compresslevel": 1}) msgpack# pandas support for msgpack has been removed in version 1.0.0. It is recommended to use pickle instead. Alternatively, you can also the Arrow IPC serialization format for on-the-wire transmission of pandas objects. For documentation on pyarrow, see here. HDF5 (PyTables)# HDFStore is a dict-like object which reads and writes pandas using the high performance HDF5 format using the excellent PyTables library. See the cookbook for some advanced strategies Warning pandas uses PyTables for reading and writing HDF5 files, which allows serializing object-dtype data with pickle. Loading pickled data received from untrusted sources can be unsafe. See: https://docs.python.org/3/library/pickle.html for more. In [436]: store = pd.HDFStore("store.h5") In [437]: print(store) <class 'pandas.io.pytables.HDFStore'> File path: store.h5 Objects can be written to the file just like adding key-value pairs to a dict: In [438]: index = pd.date_range("1/1/2000", periods=8) In [439]: s = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"]) In [440]: df = pd.DataFrame(np.random.randn(8, 3), index=index, columns=["A", "B", "C"]) # store.put('s', s) is an equivalent method In [441]: store["s"] = s In [442]: store["df"] = df In [443]: store Out[443]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 In a current or later Python session, you can retrieve stored objects: # store.get('df') is an equivalent method In [444]: store["df"] Out[444]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 # dotted (attribute) access provides get as well In [445]: store.df Out[445]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Deletion of the object specified by the key: # store.remove('df') is an equivalent method In [446]: del store["df"] In [447]: store Out[447]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 Closing a Store and using a context manager: In [448]: store.close() In [449]: store Out[449]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 In [450]: store.is_open Out[450]: False # Working with, and automatically closing the store using a context manager In [451]: with pd.HDFStore("store.h5") as store: .....: store.keys() .....: Read/write API# HDFStore supports a top-level API using read_hdf for reading and to_hdf for writing, similar to how read_csv and to_csv work. In [452]: df_tl = pd.DataFrame({"A": list(range(5)), "B": list(range(5))}) In [453]: df_tl.to_hdf("store_tl.h5", "table", append=True) In [454]: pd.read_hdf("store_tl.h5", "table", where=["index>2"]) Out[454]: A B 3 3 3 4 4 4 HDFStore will by default not drop rows that are all missing. This behavior can be changed by setting dropna=True. In [455]: df_with_missing = pd.DataFrame( .....: { .....: "col1": [0, np.nan, 2], .....: "col2": [1, np.nan, np.nan], .....: } .....: ) .....: In [456]: df_with_missing Out[456]: col1 col2 0 0.0 1.0 1 NaN NaN 2 2.0 NaN In [457]: df_with_missing.to_hdf("file.h5", "df_with_missing", format="table", mode="w") In [458]: pd.read_hdf("file.h5", "df_with_missing") Out[458]: col1 col2 0 0.0 1.0 1 NaN NaN 2 2.0 NaN In [459]: df_with_missing.to_hdf( .....: "file.h5", "df_with_missing", format="table", mode="w", dropna=True .....: ) .....: In [460]: pd.read_hdf("file.h5", "df_with_missing") Out[460]: col1 col2 0 0.0 1.0 2 2.0 NaN Fixed format# The examples above show storing using put, which write the HDF5 to PyTables in a fixed array format, called the fixed format. These types of stores are not appendable once written (though you can simply remove them and rewrite). Nor are they queryable; they must be retrieved in their entirety. They also do not support dataframes with non-unique column names. The fixed format stores offer very fast writing and slightly faster reading than table stores. This format is specified by default when using put or to_hdf or by format='fixed' or format='f'. Warning A fixed format will raise a TypeError if you try to retrieve using a where: >>> pd.DataFrame(np.random.randn(10, 2)).to_hdf("test_fixed.h5", "df") >>> pd.read_hdf("test_fixed.h5", "df", where="index>5") TypeError: cannot pass a where specification when reading a fixed format. this store must be selected in its entirety Table format# HDFStore supports another PyTables format on disk, the table format. Conceptually a table is shaped very much like a DataFrame, with rows and columns. A table may be appended to in the same or other sessions. In addition, delete and query type operations are supported. This format is specified by format='table' or format='t' to append or put or to_hdf. This format can be set as an option as well pd.set_option('io.hdf.default_format','table') to enable put/append/to_hdf to by default store in the table format. In [461]: store = pd.HDFStore("store.h5") In [462]: df1 = df[0:4] In [463]: df2 = df[4:] # append data (creates a table automatically) In [464]: store.append("df", df1) In [465]: store.append("df", df2) In [466]: store Out[466]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # select the entire object In [467]: store.select("df") Out[467]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 # the type of stored data In [468]: store.root.df._v_attrs.pandas_type Out[468]: 'frame_table' Note You can also create a table by passing format='table' or format='t' to a put operation. Hierarchical keys# Keys to a store can be specified as a string. These can be in a hierarchical path-name like format (e.g. foo/bar/bah), which will generate a hierarchy of sub-stores (or Groups in PyTables parlance). Keys can be specified without the leading ‘/’ and are always absolute (e.g. ‘foo’ refers to ‘/foo’). Removal operations can remove everything in the sub-store and below, so be careful. In [469]: store.put("foo/bar/bah", df) In [470]: store.append("food/orange", df) In [471]: store.append("food/apple", df) In [472]: store Out[472]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # a list of keys are returned In [473]: store.keys() Out[473]: ['/df', '/food/apple', '/food/orange', '/foo/bar/bah'] # remove all nodes under this level In [474]: store.remove("food") In [475]: store Out[475]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 You can walk through the group hierarchy using the walk method which will yield a tuple for each group key along with the relative keys of its contents. In [476]: for (path, subgroups, subkeys) in store.walk(): .....: for subgroup in subgroups: .....: print("GROUP: {}/{}".format(path, subgroup)) .....: for subkey in subkeys: .....: key = "/".join([path, subkey]) .....: print("KEY: {}".format(key)) .....: print(store.get(key)) .....: GROUP: /foo KEY: /df A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 GROUP: /foo/bar KEY: /foo/bar/bah A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Warning Hierarchical keys cannot be retrieved as dotted (attribute) access as described above for items stored under the root node. In [8]: store.foo.bar.bah AttributeError: 'HDFStore' object has no attribute 'foo' # you can directly access the actual PyTables node but using the root node In [9]: store.root.foo.bar.bah Out[9]: /foo/bar/bah (Group) '' children := ['block0_items' (Array), 'block0_values' (Array), 'axis0' (Array), 'axis1' (Array)] Instead, use explicit string based keys: In [477]: store["foo/bar/bah"] Out[477]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Storing types# Storing mixed types in a table# Storing mixed-dtype data is supported. Strings are stored as a fixed-width using the maximum size of the appended column. Subsequent attempts at appending longer strings will raise a ValueError. Passing min_itemsize={`values`: size} as a parameter to append will set a larger minimum for the string columns. Storing floats, strings, ints, bools, datetime64 are currently supported. For string columns, passing nan_rep = 'nan' to append will change the default nan representation on disk (which converts to/from np.nan), this defaults to nan. In [478]: df_mixed = pd.DataFrame( .....: { .....: "A": np.random.randn(8), .....: "B": np.random.randn(8), .....: "C": np.array(np.random.randn(8), dtype="float32"), .....: "string": "string", .....: "int": 1, .....: "bool": True, .....: "datetime64": pd.Timestamp("20010102"), .....: }, .....: index=list(range(8)), .....: ) .....: In [479]: df_mixed.loc[df_mixed.index[3:5], ["A", "B", "string", "datetime64"]] = np.nan In [480]: store.append("df_mixed", df_mixed, min_itemsize={"values": 50}) In [481]: df_mixed1 = store.select("df_mixed") In [482]: df_mixed1 Out[482]: A B C string int bool datetime64 0 1.778161 -0.898283 -0.263043 string 1 True 2001-01-02 1 -0.913867 -0.218499 -0.639244 string 1 True 2001-01-02 2 -0.030004 1.408028 -0.866305 string 1 True 2001-01-02 3 NaN NaN -0.225250 NaN 1 True NaT 4 NaN NaN -0.890978 NaN 1 True NaT 5 0.081323 0.520995 -0.553839 string 1 True 2001-01-02 6 -0.268494 0.620028 -2.762875 string 1 True 2001-01-02 7 0.168016 0.159416 -1.244763 string 1 True 2001-01-02 In [483]: df_mixed1.dtypes.value_counts() Out[483]: float64 2 float32 1 object 1 int64 1 bool 1 datetime64[ns] 1 dtype: int64 # we have provided a minimum string column size In [484]: store.root.df_mixed.table Out[484]: /df_mixed/table (Table(8,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(2,), dflt=0.0, pos=1), "values_block_1": Float32Col(shape=(1,), dflt=0.0, pos=2), "values_block_2": StringCol(itemsize=50, shape=(1,), dflt=b'', pos=3), "values_block_3": Int64Col(shape=(1,), dflt=0, pos=4), "values_block_4": BoolCol(shape=(1,), dflt=False, pos=5), "values_block_5": Int64Col(shape=(1,), dflt=0, pos=6)} byteorder := 'little' chunkshape := (689,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False} Storing MultiIndex DataFrames# Storing MultiIndex DataFrames as tables is very similar to storing/selecting from homogeneous index DataFrames. In [485]: index = pd.MultiIndex( .....: levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], .....: codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], .....: names=["foo", "bar"], .....: ) .....: In [486]: df_mi = pd.DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"]) In [487]: df_mi Out[487]: A B C foo bar foo one -1.280289 0.692545 -0.536722 two 1.005707 0.296917 0.139796 three -1.083889 0.811865 1.648435 bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 baz two 0.183573 0.145277 0.308146 three -1.043530 -0.708145 1.430905 qux one -0.850136 0.813949 1.508891 two -1.556154 0.187597 1.176488 three -1.246093 -0.002726 -0.444249 In [488]: store.append("df_mi", df_mi) In [489]: store.select("df_mi") Out[489]: A B C foo bar foo one -1.280289 0.692545 -0.536722 two 1.005707 0.296917 0.139796 three -1.083889 0.811865 1.648435 bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 baz two 0.183573 0.145277 0.308146 three -1.043530 -0.708145 1.430905 qux one -0.850136 0.813949 1.508891 two -1.556154 0.187597 1.176488 three -1.246093 -0.002726 -0.444249 # the levels are automatically included as data columns In [490]: store.select("df_mi", "foo=bar") Out[490]: A B C foo bar bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 Note The index keyword is reserved and cannot be use as a level name. Querying# Querying a table# select and delete operations have an optional criterion that can be specified to select/delete only a subset of the data. This allows one to have a very large on-disk table and retrieve only a portion of the data. A query is specified using the Term class under the hood, as a boolean expression. index and columns are supported indexers of DataFrames. if data_columns are specified, these can be used as additional indexers. level name in a MultiIndex, with default name level_0, level_1, … if not provided. Valid comparison operators are: =, ==, !=, >, >=, <, <= Valid boolean expressions are combined with: | : or & : and ( and ) : for grouping These rules are similar to how boolean expressions are used in pandas for indexing. Note = will be automatically expanded to the comparison operator == ~ is the not operator, but can only be used in very limited circumstances If a list/tuple of expressions is passed they will be combined via & The following are valid expressions: 'index >= date' "columns = ['A', 'D']" "columns in ['A', 'D']" 'columns = A' 'columns == A' "~(columns = ['A', 'B'])" 'index > df.index[3] & string = "bar"' '(index > df.index[3] & index <= df.index[6]) | string = "bar"' "ts >= Timestamp('2012-02-01')" "major_axis>=20130101" The indexers are on the left-hand side of the sub-expression: columns, major_axis, ts The right-hand side of the sub-expression (after a comparison operator) can be: functions that will be evaluated, e.g. Timestamp('2012-02-01') strings, e.g. "bar" date-like, e.g. 20130101, or "20130101" lists, e.g. "['A', 'B']" variables that are defined in the local names space, e.g. date Note Passing a string to a query by interpolating it into the query expression is not recommended. Simply assign the string of interest to a variable and use that variable in an expression. For example, do this string = "HolyMoly'" store.select("df", "index == string") instead of this string = "HolyMoly'" store.select('df', f'index == {string}') The latter will not work and will raise a SyntaxError.Note that there’s a single quote followed by a double quote in the string variable. If you must interpolate, use the '%r' format specifier store.select("df", "index == %r" % string) which will quote string. Here are some examples: In [491]: dfq = pd.DataFrame( .....: np.random.randn(10, 4), .....: columns=list("ABCD"), .....: index=pd.date_range("20130101", periods=10), .....: ) .....: In [492]: store.append("dfq", dfq, format="table", data_columns=True) Use boolean expressions, with in-line function evaluation. In [493]: store.select("dfq", "index>pd.Timestamp('20130104') & columns=['A', 'B']") Out[493]: A B 2013-01-05 1.366810 1.073372 2013-01-06 2.119746 -2.628174 2013-01-07 0.337920 -0.634027 2013-01-08 1.053434 1.109090 2013-01-09 -0.772942 -0.269415 2013-01-10 0.048562 -0.285920 Use inline column reference. In [494]: store.select("dfq", where="A>0 or C>0") Out[494]: A B C D 2013-01-01 0.856838 1.491776 0.001283 0.701816 2013-01-02 -1.097917 0.102588 0.661740 0.443531 2013-01-03 0.559313 -0.459055 -1.222598 -0.455304 2013-01-05 1.366810 1.073372 -0.994957 0.755314 2013-01-06 2.119746 -2.628174 -0.089460 -0.133636 2013-01-07 0.337920 -0.634027 0.421107 0.604303 2013-01-08 1.053434 1.109090 -0.367891 -0.846206 2013-01-10 0.048562 -0.285920 1.334100 0.194462 The columns keyword can be supplied to select a list of columns to be returned, this is equivalent to passing a 'columns=list_of_columns_to_filter': In [495]: store.select("df", "columns=['A', 'B']") Out[495]: A B 2000-01-01 -0.398501 -0.677311 2000-01-02 -1.167564 -0.593353 2000-01-03 -0.131959 0.089012 2000-01-04 0.169405 -1.358046 2000-01-05 0.492195 0.076693 2000-01-06 -0.285283 -1.210529 2000-01-07 0.941577 -0.342447 2000-01-08 0.052607 2.093214 start and stop parameters can be specified to limit the total search space. These are in terms of the total number of rows in a table. Note select will raise a ValueError if the query expression has an unknown variable reference. Usually this means that you are trying to select on a column that is not a data_column. select will raise a SyntaxError if the query expression is not valid. Query timedelta64[ns]# You can store and query using the timedelta64[ns] type. Terms can be specified in the format: <float>(<unit>), where float may be signed (and fractional), and unit can be D,s,ms,us,ns for the timedelta. Here’s an example: In [496]: from datetime import timedelta In [497]: dftd = pd.DataFrame( .....: { .....: "A": pd.Timestamp("20130101"), .....: "B": [ .....: pd.Timestamp("20130101") + timedelta(days=i, seconds=10) .....: for i in range(10) .....: ], .....: } .....: ) .....: In [498]: dftd["C"] = dftd["A"] - dftd["B"] In [499]: dftd Out[499]: A B C 0 2013-01-01 2013-01-01 00:00:10 -1 days +23:59:50 1 2013-01-01 2013-01-02 00:00:10 -2 days +23:59:50 2 2013-01-01 2013-01-03 00:00:10 -3 days +23:59:50 3 2013-01-01 2013-01-04 00:00:10 -4 days +23:59:50 4 2013-01-01 2013-01-05 00:00:10 -5 days +23:59:50 5 2013-01-01 2013-01-06 00:00:10 -6 days +23:59:50 6 2013-01-01 2013-01-07 00:00:10 -7 days +23:59:50 7 2013-01-01 2013-01-08 00:00:10 -8 days +23:59:50 8 2013-01-01 2013-01-09 00:00:10 -9 days +23:59:50 9 2013-01-01 2013-01-10 00:00:10 -10 days +23:59:50 In [500]: store.append("dftd", dftd, data_columns=True) In [501]: store.select("dftd", "C<'-3.5D'") Out[501]: A B C 4 2013-01-01 2013-01-05 00:00:10 -5 days +23:59:50 5 2013-01-01 2013-01-06 00:00:10 -6 days +23:59:50 6 2013-01-01 2013-01-07 00:00:10 -7 days +23:59:50 7 2013-01-01 2013-01-08 00:00:10 -8 days +23:59:50 8 2013-01-01 2013-01-09 00:00:10 -9 days +23:59:50 9 2013-01-01 2013-01-10 00:00:10 -10 days +23:59:50 Query MultiIndex# Selecting from a MultiIndex can be achieved by using the name of the level. In [502]: df_mi.index.names Out[502]: FrozenList(['foo', 'bar']) In [503]: store.select("df_mi", "foo=baz and bar=two") Out[503]: A B C foo bar baz two 0.183573 0.145277 0.308146 If the MultiIndex levels names are None, the levels are automatically made available via the level_n keyword with n the level of the MultiIndex you want to select from. In [504]: index = pd.MultiIndex( .....: levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], .....: codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], .....: ) .....: In [505]: df_mi_2 = pd.DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"]) In [506]: df_mi_2 Out[506]: A B C foo one -0.646538 1.210676 -0.315409 two 1.528366 0.376542 0.174490 three 1.247943 -0.742283 0.710400 bar one 0.434128 -1.246384 1.139595 two 1.388668 -0.413554 -0.666287 baz two 0.010150 -0.163820 -0.115305 three 0.216467 0.633720 0.473945 qux one -0.155446 1.287082 0.320201 two -1.256989 0.874920 0.765944 three 0.025557 -0.729782 -0.127439 In [507]: store.append("df_mi_2", df_mi_2) # the levels are automatically included as data columns with keyword level_n In [508]: store.select("df_mi_2", "level_0=foo and level_1=two") Out[508]: A B C foo two 1.528366 0.376542 0.17449 Indexing# You can create/modify an index for a table with create_table_index after data is already in the table (after and append/put operation). Creating a table index is highly encouraged. This will speed your queries a great deal when you use a select with the indexed dimension as the where. Note Indexes are automagically created on the indexables and any data columns you specify. This behavior can be turned off by passing index=False to append. # we have automagically already created an index (in the first section) In [509]: i = store.root.df.table.cols.index.index In [510]: i.optlevel, i.kind Out[510]: (6, 'medium') # change an index by passing new parameters In [511]: store.create_table_index("df", optlevel=9, kind="full") In [512]: i = store.root.df.table.cols.index.index In [513]: i.optlevel, i.kind Out[513]: (9, 'full') Oftentimes when appending large amounts of data to a store, it is useful to turn off index creation for each append, then recreate at the end. In [514]: df_1 = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [515]: df_2 = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [516]: st = pd.HDFStore("appends.h5", mode="w") In [517]: st.append("df", df_1, data_columns=["B"], index=False) In [518]: st.append("df", df_2, data_columns=["B"], index=False) In [519]: st.get_storer("df").table Out[519]: /df/table (Table(20,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2)} byteorder := 'little' chunkshape := (2730,) Then create the index when finished appending. In [520]: st.create_table_index("df", columns=["B"], optlevel=9, kind="full") In [521]: st.get_storer("df").table Out[521]: /df/table (Table(20,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2)} byteorder := 'little' chunkshape := (2730,) autoindex := True colindexes := { "B": Index(9, fullshuffle, zlib(1)).is_csi=True} In [522]: st.close() See here for how to create a completely-sorted-index (CSI) on an existing store. Query via data columns# You can designate (and index) certain columns that you want to be able to perform queries (other than the indexable columns, which you can always query). For instance say you want to perform this common operation, on-disk, and return just the frame that matches this query. You can specify data_columns = True to force all columns to be data_columns. In [523]: df_dc = df.copy() In [524]: df_dc["string"] = "foo" In [525]: df_dc.loc[df_dc.index[4:6], "string"] = np.nan In [526]: df_dc.loc[df_dc.index[7:9], "string"] = "bar" In [527]: df_dc["string2"] = "cool" In [528]: df_dc.loc[df_dc.index[1:3], ["B", "C"]] = 1.0 In [529]: df_dc Out[529]: A B C string string2 2000-01-01 -0.398501 -0.677311 -0.874991 foo cool 2000-01-02 -1.167564 1.000000 1.000000 foo cool 2000-01-03 -0.131959 1.000000 1.000000 foo cool 2000-01-04 0.169405 -1.358046 -0.105563 foo cool 2000-01-05 0.492195 0.076693 0.213685 NaN cool 2000-01-06 -0.285283 -1.210529 -1.408386 NaN cool 2000-01-07 0.941577 -0.342447 0.222031 foo cool 2000-01-08 0.052607 2.093214 1.064908 bar cool # on-disk operations In [530]: store.append("df_dc", df_dc, data_columns=["B", "C", "string", "string2"]) In [531]: store.select("df_dc", where="B > 0") Out[531]: A B C string string2 2000-01-02 -1.167564 1.000000 1.000000 foo cool 2000-01-03 -0.131959 1.000000 1.000000 foo cool 2000-01-05 0.492195 0.076693 0.213685 NaN cool 2000-01-08 0.052607 2.093214 1.064908 bar cool # getting creative In [532]: store.select("df_dc", "B > 0 & C > 0 & string == foo") Out[532]: A B C string string2 2000-01-02 -1.167564 1.0 1.0 foo cool 2000-01-03 -0.131959 1.0 1.0 foo cool # this is in-memory version of this type of selection In [533]: df_dc[(df_dc.B > 0) & (df_dc.C > 0) & (df_dc.string == "foo")] Out[533]: A B C string string2 2000-01-02 -1.167564 1.0 1.0 foo cool 2000-01-03 -0.131959 1.0 1.0 foo cool # we have automagically created this index and the B/C/string/string2 # columns are stored separately as ``PyTables`` columns In [534]: store.root.df_dc.table Out[534]: /df_dc/table (Table(8,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2), "C": Float64Col(shape=(), dflt=0.0, pos=3), "string": StringCol(itemsize=3, shape=(), dflt=b'', pos=4), "string2": StringCol(itemsize=4, shape=(), dflt=b'', pos=5)} byteorder := 'little' chunkshape := (1680,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False, "B": Index(6, mediumshuffle, zlib(1)).is_csi=False, "C": Index(6, mediumshuffle, zlib(1)).is_csi=False, "string": Index(6, mediumshuffle, zlib(1)).is_csi=False, "string2": Index(6, mediumshuffle, zlib(1)).is_csi=False} There is some performance degradation by making lots of columns into data columns, so it is up to the user to designate these. In addition, you cannot change data columns (nor indexables) after the first append/put operation (Of course you can simply read in the data and create a new table!). Iterator# You can pass iterator=True or chunksize=number_in_a_chunk to select and select_as_multiple to return an iterator on the results. The default is 50,000 rows returned in a chunk. In [535]: for df in store.select("df", chunksize=3): .....: print(df) .....: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 A B C 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 A B C 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Note You can also use the iterator with read_hdf which will open, then automatically close the store when finished iterating. for df in pd.read_hdf("store.h5", "df", chunksize=3): print(df) Note, that the chunksize keyword applies to the source rows. So if you are doing a query, then the chunksize will subdivide the total rows in the table and the query applied, returning an iterator on potentially unequal sized chunks. Here is a recipe for generating a query and using it to create equal sized return chunks. In [536]: dfeq = pd.DataFrame({"number": np.arange(1, 11)}) In [537]: dfeq Out[537]: number 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 In [538]: store.append("dfeq", dfeq, data_columns=["number"]) In [539]: def chunks(l, n): .....: return [l[i: i + n] for i in range(0, len(l), n)] .....: In [540]: evens = [2, 4, 6, 8, 10] In [541]: coordinates = store.select_as_coordinates("dfeq", "number=evens") In [542]: for c in chunks(coordinates, 2): .....: print(store.select("dfeq", where=c)) .....: number 1 2 3 4 number 5 6 7 8 number 9 10 Advanced queries# Select a single column# To retrieve a single indexable or data column, use the method select_column. This will, for example, enable you to get the index very quickly. These return a Series of the result, indexed by the row number. These do not currently accept the where selector. In [543]: store.select_column("df_dc", "index") Out[543]: 0 2000-01-01 1 2000-01-02 2 2000-01-03 3 2000-01-04 4 2000-01-05 5 2000-01-06 6 2000-01-07 7 2000-01-08 Name: index, dtype: datetime64[ns] In [544]: store.select_column("df_dc", "string") Out[544]: 0 foo 1 foo 2 foo 3 foo 4 NaN 5 NaN 6 foo 7 bar Name: string, dtype: object Selecting coordinates# Sometimes you want to get the coordinates (a.k.a the index locations) of your query. This returns an Int64Index of the resulting locations. These coordinates can also be passed to subsequent where operations. In [545]: df_coord = pd.DataFrame( .....: np.random.randn(1000, 2), index=pd.date_range("20000101", periods=1000) .....: ) .....: In [546]: store.append("df_coord", df_coord) In [547]: c = store.select_as_coordinates("df_coord", "index > 20020101") In [548]: c Out[548]: Int64Index([732, 733, 734, 735, 736, 737, 738, 739, 740, 741, ... 990, 991, 992, 993, 994, 995, 996, 997, 998, 999], dtype='int64', length=268) In [549]: store.select("df_coord", where=c) Out[549]: 0 1 2002-01-02 0.009035 0.921784 2002-01-03 -1.476563 -1.376375 2002-01-04 1.266731 2.173681 2002-01-05 0.147621 0.616468 2002-01-06 0.008611 2.136001 ... ... ... 2002-09-22 0.781169 -0.791687 2002-09-23 -0.764810 -2.000933 2002-09-24 -0.345662 0.393915 2002-09-25 -0.116661 0.834638 2002-09-26 -1.341780 0.686366 [268 rows x 2 columns] Selecting using a where mask# Sometime your query can involve creating a list of rows to select. Usually this mask would be a resulting index from an indexing operation. This example selects the months of a datetimeindex which are 5. In [550]: df_mask = pd.DataFrame( .....: np.random.randn(1000, 2), index=pd.date_range("20000101", periods=1000) .....: ) .....: In [551]: store.append("df_mask", df_mask) In [552]: c = store.select_column("df_mask", "index") In [553]: where = c[pd.DatetimeIndex(c).month == 5].index In [554]: store.select("df_mask", where=where) Out[554]: 0 1 2000-05-01 -0.386742 -0.977433 2000-05-02 -0.228819 0.471671 2000-05-03 0.337307 1.840494 2000-05-04 0.050249 0.307149 2000-05-05 -0.802947 -0.946730 ... ... ... 2002-05-27 1.605281 1.741415 2002-05-28 -0.804450 -0.715040 2002-05-29 -0.874851 0.037178 2002-05-30 -0.161167 -1.294944 2002-05-31 -0.258463 -0.731969 [93 rows x 2 columns] Storer object# If you want to inspect the stored object, retrieve via get_storer. You could use this programmatically to say get the number of rows in an object. In [555]: store.get_storer("df_dc").nrows Out[555]: 8 Multiple table queries# The methods append_to_multiple and select_as_multiple can perform appending/selecting from multiple tables at once. The idea is to have one table (call it the selector table) that you index most/all of the columns, and perform your queries. The other table(s) are data tables with an index matching the selector table’s index. You can then perform a very fast query on the selector table, yet get lots of data back. This method is similar to having a very wide table, but enables more efficient queries. The append_to_multiple method splits a given single DataFrame into multiple tables according to d, a dictionary that maps the table names to a list of ‘columns’ you want in that table. If None is used in place of a list, that table will have the remaining unspecified columns of the given DataFrame. The argument selector defines which table is the selector table (which you can make queries from). The argument dropna will drop rows from the input DataFrame to ensure tables are synchronized. This means that if a row for one of the tables being written to is entirely np.NaN, that row will be dropped from all tables. If dropna is False, THE USER IS RESPONSIBLE FOR SYNCHRONIZING THE TABLES. Remember that entirely np.Nan rows are not written to the HDFStore, so if you choose to call dropna=False, some tables may have more rows than others, and therefore select_as_multiple may not work or it may return unexpected results. In [556]: df_mt = pd.DataFrame( .....: np.random.randn(8, 6), .....: index=pd.date_range("1/1/2000", periods=8), .....: columns=["A", "B", "C", "D", "E", "F"], .....: ) .....: In [557]: df_mt["foo"] = "bar" In [558]: df_mt.loc[df_mt.index[1], ("A", "B")] = np.nan # you can also create the tables individually In [559]: store.append_to_multiple( .....: {"df1_mt": ["A", "B"], "df2_mt": None}, df_mt, selector="df1_mt" .....: ) .....: In [560]: store Out[560]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # individual tables were created In [561]: store.select("df1_mt") Out[561]: A B 2000-01-01 0.079529 -1.459471 2000-01-02 NaN NaN 2000-01-03 -0.423113 2.314361 2000-01-04 0.756744 -0.792372 2000-01-05 -0.184971 0.170852 2000-01-06 0.678830 0.633974 2000-01-07 0.034973 0.974369 2000-01-08 -2.110103 0.243062 In [562]: store.select("df2_mt") Out[562]: C D E F foo 2000-01-01 -0.596306 -0.910022 -1.057072 -0.864360 bar 2000-01-02 0.477849 0.283128 -2.045700 -0.338206 bar 2000-01-03 -0.033100 -0.965461 -0.001079 -0.351689 bar 2000-01-04 -0.513555 -1.484776 -0.796280 -0.182321 bar 2000-01-05 -0.872407 -1.751515 0.934334 0.938818 bar 2000-01-06 -1.398256 1.347142 -0.029520 0.082738 bar 2000-01-07 -0.755544 0.380786 -1.634116 1.293610 bar 2000-01-08 1.453064 0.500558 -0.574475 0.694324 bar # as a multiple In [563]: store.select_as_multiple( .....: ["df1_mt", "df2_mt"], .....: where=["A>0", "B>0"], .....: selector="df1_mt", .....: ) .....: Out[563]: A B C D E F foo 2000-01-06 0.678830 0.633974 -1.398256 1.347142 -0.029520 0.082738 bar 2000-01-07 0.034973 0.974369 -0.755544 0.380786 -1.634116 1.293610 bar Delete from a table# You can delete from a table selectively by specifying a where. In deleting rows, it is important to understand the PyTables deletes rows by erasing the rows, then moving the following data. Thus deleting can potentially be a very expensive operation depending on the orientation of your data. To get optimal performance, it’s worthwhile to have the dimension you are deleting be the first of the indexables. Data is ordered (on the disk) in terms of the indexables. Here’s a simple use case. You store panel-type data, with dates in the major_axis and ids in the minor_axis. The data is then interleaved like this: date_1 id_1 id_2 . id_n date_2 id_1 . id_n It should be clear that a delete operation on the major_axis will be fairly quick, as one chunk is removed, then the following data moved. On the other hand a delete operation on the minor_axis will be very expensive. In this case it would almost certainly be faster to rewrite the table using a where that selects all but the missing data. Warning Please note that HDF5 DOES NOT RECLAIM SPACE in the h5 files automatically. Thus, repeatedly deleting (or removing nodes) and adding again, WILL TEND TO INCREASE THE FILE SIZE. To repack and clean the file, use ptrepack. Notes & caveats# Compression# PyTables allows the stored data to be compressed. This applies to all kinds of stores, not just tables. Two parameters are used to control compression: complevel and complib. complevel specifies if and how hard data is to be compressed. complevel=0 and complevel=None disables compression and 0<complevel<10 enables compression. complib specifies which compression library to use. If nothing is specified the default library zlib is used. A compression library usually optimizes for either good compression rates or speed and the results will depend on the type of data. Which type of compression to choose depends on your specific needs and data. The list of supported compression libraries: zlib: The default compression library. A classic in terms of compression, achieves good compression rates but is somewhat slow. lzo: Fast compression and decompression. bzip2: Good compression rates. blosc: Fast compression and decompression. Support for alternative blosc compressors: blosc:blosclz This is the default compressor for blosc blosc:lz4: A compact, very popular and fast compressor. blosc:lz4hc: A tweaked version of LZ4, produces better compression ratios at the expense of speed. blosc:snappy: A popular compressor used in many places. blosc:zlib: A classic; somewhat slower than the previous ones, but achieving better compression ratios. blosc:zstd: An extremely well balanced codec; it provides the best compression ratios among the others above, and at reasonably fast speed. If complib is defined as something other than the listed libraries a ValueError exception is issued. Note If the library specified with the complib option is missing on your platform, compression defaults to zlib without further ado. Enable compression for all objects within the file: store_compressed = pd.HDFStore( "store_compressed.h5", complevel=9, complib="blosc:blosclz" ) Or on-the-fly compression (this only applies to tables) in stores where compression is not enabled: store.append("df", df, complib="zlib", complevel=5) ptrepack# PyTables offers better write performance when tables are compressed after they are written, as opposed to turning on compression at the very beginning. You can use the supplied PyTables utility ptrepack. In addition, ptrepack can change compression levels after the fact. ptrepack --chunkshape=auto --propindexes --complevel=9 --complib=blosc in.h5 out.h5 Furthermore ptrepack in.h5 out.h5 will repack the file to allow you to reuse previously deleted space. Alternatively, one can simply remove the file and write again, or use the copy method. Caveats# Warning HDFStore is not-threadsafe for writing. The underlying PyTables only supports concurrent reads (via threading or processes). If you need reading and writing at the same time, you need to serialize these operations in a single thread in a single process. You will corrupt your data otherwise. See the (GH2397) for more information. If you use locks to manage write access between multiple processes, you may want to use fsync() before releasing write locks. For convenience you can use store.flush(fsync=True) to do this for you. Once a table is created columns (DataFrame) are fixed; only exactly the same columns can be appended Be aware that timezones (e.g., pytz.timezone('US/Eastern')) are not necessarily equal across timezone versions. So if data is localized to a specific timezone in the HDFStore using one version of a timezone library and that data is updated with another version, the data will be converted to UTC since these timezones are not considered equal. Either use the same version of timezone library or use tz_convert with the updated timezone definition. Warning PyTables will show a NaturalNameWarning if a column name cannot be used as an attribute selector. Natural identifiers contain only letters, numbers, and underscores, and may not begin with a number. Other identifiers cannot be used in a where clause and are generally a bad idea. DataTypes# HDFStore will map an object dtype to the PyTables underlying dtype. This means the following types are known to work: Type Represents missing values floating : float64, float32, float16 np.nan integer : int64, int32, int8, uint64,uint32, uint8 boolean datetime64[ns] NaT timedelta64[ns] NaT categorical : see the section below object : strings np.nan unicode columns are not supported, and WILL FAIL. Categorical data# You can write data that contains category dtypes to a HDFStore. Queries work the same as if it was an object array. However, the category dtyped data is stored in a more efficient manner. In [564]: dfcat = pd.DataFrame( .....: {"A": pd.Series(list("aabbcdba")).astype("category"), "B": np.random.randn(8)} .....: ) .....: In [565]: dfcat Out[565]: A B 0 a -1.608059 1 a 0.851060 2 b -0.736931 3 b 0.003538 4 c -1.422611 5 d 2.060901 6 b 0.993899 7 a -1.371768 In [566]: dfcat.dtypes Out[566]: A category B float64 dtype: object In [567]: cstore = pd.HDFStore("cats.h5", mode="w") In [568]: cstore.append("dfcat", dfcat, format="table", data_columns=["A"]) In [569]: result = cstore.select("dfcat", where="A in ['b', 'c']") In [570]: result Out[570]: A B 2 b -0.736931 3 b 0.003538 4 c -1.422611 6 b 0.993899 In [571]: result.dtypes Out[571]: A category B float64 dtype: object String columns# min_itemsize The underlying implementation of HDFStore uses a fixed column width (itemsize) for string columns. A string column itemsize is calculated as the maximum of the length of data (for that column) that is passed to the HDFStore, in the first append. Subsequent appends, may introduce a string for a column larger than the column can hold, an Exception will be raised (otherwise you could have a silent truncation of these columns, leading to loss of information). In the future we may relax this and allow a user-specified truncation to occur. Pass min_itemsize on the first table creation to a-priori specify the minimum length of a particular string column. min_itemsize can be an integer, or a dict mapping a column name to an integer. You can pass values as a key to allow all indexables or data_columns to have this min_itemsize. Passing a min_itemsize dict will cause all passed columns to be created as data_columns automatically. Note If you are not passing any data_columns, then the min_itemsize will be the maximum of the length of any string passed In [572]: dfs = pd.DataFrame({"A": "foo", "B": "bar"}, index=list(range(5))) In [573]: dfs Out[573]: A B 0 foo bar 1 foo bar 2 foo bar 3 foo bar 4 foo bar # A and B have a size of 30 In [574]: store.append("dfs", dfs, min_itemsize=30) In [575]: store.get_storer("dfs").table Out[575]: /dfs/table (Table(5,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": StringCol(itemsize=30, shape=(2,), dflt=b'', pos=1)} byteorder := 'little' chunkshape := (963,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False} # A is created as a data_column with a size of 30 # B is size is calculated In [576]: store.append("dfs2", dfs, min_itemsize={"A": 30}) In [577]: store.get_storer("dfs2").table Out[577]: /dfs2/table (Table(5,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": StringCol(itemsize=3, shape=(1,), dflt=b'', pos=1), "A": StringCol(itemsize=30, shape=(), dflt=b'', pos=2)} byteorder := 'little' chunkshape := (1598,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False, "A": Index(6, mediumshuffle, zlib(1)).is_csi=False} nan_rep String columns will serialize a np.nan (a missing value) with the nan_rep string representation. This defaults to the string value nan. You could inadvertently turn an actual nan value into a missing value. In [578]: dfss = pd.DataFrame({"A": ["foo", "bar", "nan"]}) In [579]: dfss Out[579]: A 0 foo 1 bar 2 nan In [580]: store.append("dfss", dfss) In [581]: store.select("dfss") Out[581]: A 0 foo 1 bar 2 NaN # here you need to specify a different nan rep In [582]: store.append("dfss2", dfss, nan_rep="_nan_") In [583]: store.select("dfss2") Out[583]: A 0 foo 1 bar 2 nan External compatibility# HDFStore writes table format objects in specific formats suitable for producing loss-less round trips to pandas objects. For external compatibility, HDFStore can read native PyTables format tables. It is possible to write an HDFStore object that can easily be imported into R using the rhdf5 library (Package website). Create a table format store like this: In [584]: df_for_r = pd.DataFrame( .....: { .....: "first": np.random.rand(100), .....: "second": np.random.rand(100), .....: "class": np.random.randint(0, 2, (100,)), .....: }, .....: index=range(100), .....: ) .....: In [585]: df_for_r.head() Out[585]: first second class 0 0.013480 0.504941 0 1 0.690984 0.898188 1 2 0.510113 0.618748 1 3 0.357698 0.004972 0 4 0.451658 0.012065 1 In [586]: store_export = pd.HDFStore("export.h5") In [587]: store_export.append("df_for_r", df_for_r, data_columns=df_dc.columns) In [588]: store_export Out[588]: <class 'pandas.io.pytables.HDFStore'> File path: export.h5 In R this file can be read into a data.frame object using the rhdf5 library. The following example function reads the corresponding column names and data values from the values and assembles them into a data.frame: # Load values and column names for all datasets from corresponding nodes and # insert them into one data.frame object. library(rhdf5) loadhdf5data <- function(h5File) { listing <- h5ls(h5File) # Find all data nodes, values are stored in *_values and corresponding column # titles in *_items data_nodes <- grep("_values", listing$name) name_nodes <- grep("_items", listing$name) data_paths = paste(listing$group[data_nodes], listing$name[data_nodes], sep = "/") name_paths = paste(listing$group[name_nodes], listing$name[name_nodes], sep = "/") columns = list() for (idx in seq(data_paths)) { # NOTE: matrices returned by h5read have to be transposed to obtain # required Fortran order! data <- data.frame(t(h5read(h5File, data_paths[idx]))) names <- t(h5read(h5File, name_paths[idx])) entry <- data.frame(data) colnames(entry) <- names columns <- append(columns, entry) } data <- data.frame(columns) return(data) } Now you can import the DataFrame into R: > data = loadhdf5data("transfer.hdf5") > head(data) first second class 1 0.4170220047 0.3266449 0 2 0.7203244934 0.5270581 0 3 0.0001143748 0.8859421 1 4 0.3023325726 0.3572698 1 5 0.1467558908 0.9085352 1 6 0.0923385948 0.6233601 1 Note The R function lists the entire HDF5 file’s contents and assembles the data.frame object from all matching nodes, so use this only as a starting point if you have stored multiple DataFrame objects to a single HDF5 file. Performance# tables format come with a writing performance penalty as compared to fixed stores. The benefit is the ability to append/delete and query (potentially very large amounts of data). Write times are generally longer as compared with regular stores. Query times can be quite fast, especially on an indexed axis. You can pass chunksize=<int> to append, specifying the write chunksize (default is 50000). This will significantly lower your memory usage on writing. You can pass expectedrows=<int> to the first append, to set the TOTAL number of rows that PyTables will expect. This will optimize read/write performance. Duplicate rows can be written to tables, but are filtered out in selection (with the last items being selected; thus a table is unique on major, minor pairs) A PerformanceWarning will be raised if you are attempting to store types that will be pickled by PyTables (rather than stored as endemic types). See Here for more information and some solutions. Feather# Feather provides binary columnar serialization for data frames. It is designed to make reading and writing data frames efficient, and to make sharing data across data analysis languages easy. Feather is designed to faithfully serialize and de-serialize DataFrames, supporting all of the pandas dtypes, including extension dtypes such as categorical and datetime with tz. Several caveats: The format will NOT write an Index, or MultiIndex for the DataFrame and will raise an error if a non-default one is provided. You can .reset_index() to store the index or .reset_index(drop=True) to ignore it. Duplicate column names and non-string columns names are not supported Actual Python objects in object dtype columns are not supported. These will raise a helpful error message on an attempt at serialization. See the Full Documentation. In [589]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(3, 6).astype("u1"), .....: "d": np.arange(4.0, 7.0, dtype="float64"), .....: "e": [True, False, True], .....: "f": pd.Categorical(list("abc")), .....: "g": pd.date_range("20130101", periods=3), .....: "h": pd.date_range("20130101", periods=3, tz="US/Eastern"), .....: "i": pd.date_range("20130101", periods=3, freq="ns"), .....: } .....: ) .....: In [590]: df Out[590]: a b c ... g h i 0 a 1 3 ... 2013-01-01 2013-01-01 00:00:00-05:00 2013-01-01 00:00:00.000000000 1 b 2 4 ... 2013-01-02 2013-01-02 00:00:00-05:00 2013-01-01 00:00:00.000000001 2 c 3 5 ... 2013-01-03 2013-01-03 00:00:00-05:00 2013-01-01 00:00:00.000000002 [3 rows x 9 columns] In [591]: df.dtypes Out[591]: a object b int64 c uint8 d float64 e bool f category g datetime64[ns] h datetime64[ns, US/Eastern] i datetime64[ns] dtype: object Write to a feather file. In [592]: df.to_feather("example.feather") Read from a feather file. In [593]: result = pd.read_feather("example.feather") In [594]: result Out[594]: a b c ... g h i 0 a 1 3 ... 2013-01-01 2013-01-01 00:00:00-05:00 2013-01-01 00:00:00.000000000 1 b 2 4 ... 2013-01-02 2013-01-02 00:00:00-05:00 2013-01-01 00:00:00.000000001 2 c 3 5 ... 2013-01-03 2013-01-03 00:00:00-05:00 2013-01-01 00:00:00.000000002 [3 rows x 9 columns] # we preserve dtypes In [595]: result.dtypes Out[595]: a object b int64 c uint8 d float64 e bool f category g datetime64[ns] h datetime64[ns, US/Eastern] i datetime64[ns] dtype: object Parquet# Apache Parquet provides a partitioned binary columnar serialization for data frames. It is designed to make reading and writing data frames efficient, and to make sharing data across data analysis languages easy. Parquet can use a variety of compression techniques to shrink the file size as much as possible while still maintaining good read performance. Parquet is designed to faithfully serialize and de-serialize DataFrame s, supporting all of the pandas dtypes, including extension dtypes such as datetime with tz. Several caveats. Duplicate column names and non-string columns names are not supported. The pyarrow engine always writes the index to the output, but fastparquet only writes non-default indexes. This extra column can cause problems for non-pandas consumers that are not expecting it. You can force including or omitting indexes with the index argument, regardless of the underlying engine. Index level names, if specified, must be strings. In the pyarrow engine, categorical dtypes for non-string types can be serialized to parquet, but will de-serialize as their primitive dtype. The pyarrow engine preserves the ordered flag of categorical dtypes with string types. fastparquet does not preserve the ordered flag. Non supported types include Interval and actual Python object types. These will raise a helpful error message on an attempt at serialization. Period type is supported with pyarrow >= 0.16.0. The pyarrow engine preserves extension data types such as the nullable integer and string data type (requiring pyarrow >= 0.16.0, and requiring the extension type to implement the needed protocols, see the extension types documentation). You can specify an engine to direct the serialization. This can be one of pyarrow, or fastparquet, or auto. If the engine is NOT specified, then the pd.options.io.parquet.engine option is checked; if this is also auto, then pyarrow is tried, and falling back to fastparquet. See the documentation for pyarrow and fastparquet. Note These engines are very similar and should read/write nearly identical parquet format files. pyarrow>=8.0.0 supports timedelta data, fastparquet>=0.1.4 supports timezone aware datetimes. These libraries differ by having different underlying dependencies (fastparquet by using numba, while pyarrow uses a c-library). In [596]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(3, 6).astype("u1"), .....: "d": np.arange(4.0, 7.0, dtype="float64"), .....: "e": [True, False, True], .....: "f": pd.date_range("20130101", periods=3), .....: "g": pd.date_range("20130101", periods=3, tz="US/Eastern"), .....: "h": pd.Categorical(list("abc")), .....: "i": pd.Categorical(list("abc"), ordered=True), .....: } .....: ) .....: In [597]: df Out[597]: a b c d e f g h i 0 a 1 3 4.0 True 2013-01-01 2013-01-01 00:00:00-05:00 a a 1 b 2 4 5.0 False 2013-01-02 2013-01-02 00:00:00-05:00 b b 2 c 3 5 6.0 True 2013-01-03 2013-01-03 00:00:00-05:00 c c In [598]: df.dtypes Out[598]: a object b int64 c uint8 d float64 e bool f datetime64[ns] g datetime64[ns, US/Eastern] h category i category dtype: object Write to a parquet file. In [599]: df.to_parquet("example_pa.parquet", engine="pyarrow") In [600]: df.to_parquet("example_fp.parquet", engine="fastparquet") Read from a parquet file. In [601]: result = pd.read_parquet("example_fp.parquet", engine="fastparquet") In [602]: result = pd.read_parquet("example_pa.parquet", engine="pyarrow") In [603]: result.dtypes Out[603]: a object b int64 c uint8 d float64 e bool f datetime64[ns] g datetime64[ns, US/Eastern] h category i category dtype: object Read only certain columns of a parquet file. In [604]: result = pd.read_parquet( .....: "example_fp.parquet", .....: engine="fastparquet", .....: columns=["a", "b"], .....: ) .....: In [605]: result = pd.read_parquet( .....: "example_pa.parquet", .....: engine="pyarrow", .....: columns=["a", "b"], .....: ) .....: In [606]: result.dtypes Out[606]: a object b int64 dtype: object Handling indexes# Serializing a DataFrame to parquet may include the implicit index as one or more columns in the output file. Thus, this code: In [607]: df = pd.DataFrame({"a": [1, 2], "b": [3, 4]}) In [608]: df.to_parquet("test.parquet", engine="pyarrow") creates a parquet file with three columns if you use pyarrow for serialization: a, b, and __index_level_0__. If you’re using fastparquet, the index may or may not be written to the file. This unexpected extra column causes some databases like Amazon Redshift to reject the file, because that column doesn’t exist in the target table. If you want to omit a dataframe’s indexes when writing, pass index=False to to_parquet(): In [609]: df.to_parquet("test.parquet", index=False) This creates a parquet file with just the two expected columns, a and b. If your DataFrame has a custom index, you won’t get it back when you load this file into a DataFrame. Passing index=True will always write the index, even if that’s not the underlying engine’s default behavior. Partitioning Parquet files# Parquet supports partitioning of data based on the values of one or more columns. In [610]: df = pd.DataFrame({"a": [0, 0, 1, 1], "b": [0, 1, 0, 1]}) In [611]: df.to_parquet(path="test", engine="pyarrow", partition_cols=["a"], compression=None) The path specifies the parent directory to which data will be saved. The partition_cols are the column names by which the dataset will be partitioned. Columns are partitioned in the order they are given. The partition splits are determined by the unique values in the partition columns. The above example creates a partitioned dataset that may look like: test ├── a=0 │ ├── 0bac803e32dc42ae83fddfd029cbdebc.parquet │ └── ... └── a=1 ├── e6ab24a4f45147b49b54a662f0c412a3.parquet └── ... ORC# New in version 1.0.0. Similar to the parquet format, the ORC Format is a binary columnar serialization for data frames. It is designed to make reading data frames efficient. pandas provides both the reader and the writer for the ORC format, read_orc() and to_orc(). This requires the pyarrow library. Warning It is highly recommended to install pyarrow using conda due to some issues occurred by pyarrow. to_orc() requires pyarrow>=7.0.0. read_orc() and to_orc() are not supported on Windows yet, you can find valid environments on install optional dependencies. For supported dtypes please refer to supported ORC features in Arrow. Currently timezones in datetime columns are not preserved when a dataframe is converted into ORC files. In [612]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(4.0, 7.0, dtype="float64"), .....: "d": [True, False, True], .....: "e": pd.date_range("20130101", periods=3), .....: } .....: ) .....: In [613]: df Out[613]: a b c d e 0 a 1 4.0 True 2013-01-01 1 b 2 5.0 False 2013-01-02 2 c 3 6.0 True 2013-01-03 In [614]: df.dtypes Out[614]: a object b int64 c float64 d bool e datetime64[ns] dtype: object Write to an orc file. In [615]: df.to_orc("example_pa.orc", engine="pyarrow") Read from an orc file. In [616]: result = pd.read_orc("example_pa.orc") In [617]: result.dtypes Out[617]: a object b int64 c float64 d bool e datetime64[ns] dtype: object Read only certain columns of an orc file. In [618]: result = pd.read_orc( .....: "example_pa.orc", .....: columns=["a", "b"], .....: ) .....: In [619]: result.dtypes Out[619]: a object b int64 dtype: object SQL queries# The pandas.io.sql module provides a collection of query wrappers to both facilitate data retrieval and to reduce dependency on DB-specific API. Database abstraction is provided by SQLAlchemy if installed. In addition you will need a driver library for your database. Examples of such drivers are psycopg2 for PostgreSQL or pymysql for MySQL. For SQLite this is included in Python’s standard library by default. You can find an overview of supported drivers for each SQL dialect in the SQLAlchemy docs. If SQLAlchemy is not installed, a fallback is only provided for sqlite (and for mysql for backwards compatibility, but this is deprecated and will be removed in a future version). This mode requires a Python database adapter which respect the Python DB-API. See also some cookbook examples for some advanced strategies. The key functions are: read_sql_table(table_name, con[, schema, ...]) Read SQL database table into a DataFrame. read_sql_query(sql, con[, index_col, ...]) Read SQL query into a DataFrame. read_sql(sql, con[, index_col, ...]) Read SQL query or database table into a DataFrame. DataFrame.to_sql(name, con[, schema, ...]) Write records stored in a DataFrame to a SQL database. Note The function read_sql() is a convenience wrapper around read_sql_table() and read_sql_query() (and for backward compatibility) and will delegate to specific function depending on the provided input (database table name or sql query). Table names do not need to be quoted if they have special characters. In the following example, we use the SQlite SQL database engine. You can use a temporary SQLite database where data are stored in “memory”. To connect with SQLAlchemy you use the create_engine() function to create an engine object from database URI. You only need to create the engine once per database you are connecting to. For more information on create_engine() and the URI formatting, see the examples below and the SQLAlchemy documentation In [620]: from sqlalchemy import create_engine # Create your engine. In [621]: engine = create_engine("sqlite:///:memory:") If you want to manage your own connections you can pass one of those instead. The example below opens a connection to the database using a Python context manager that automatically closes the connection after the block has completed. See the SQLAlchemy docs for an explanation of how the database connection is handled. with engine.connect() as conn, conn.begin(): data = pd.read_sql_table("data", conn) Warning When you open a connection to a database you are also responsible for closing it. Side effects of leaving a connection open may include locking the database or other breaking behaviour. Writing DataFrames# Assuming the following data is in a DataFrame data, we can insert it into the database using to_sql(). id Date Col_1 Col_2 Col_3 26 2012-10-18 X 25.7 True 42 2012-10-19 Y -12.4 False 63 2012-10-20 Z 5.73 True In [622]: import datetime In [623]: c = ["id", "Date", "Col_1", "Col_2", "Col_3"] In [624]: d = [ .....: (26, datetime.datetime(2010, 10, 18), "X", 27.5, True), .....: (42, datetime.datetime(2010, 10, 19), "Y", -12.5, False), .....: (63, datetime.datetime(2010, 10, 20), "Z", 5.73, True), .....: ] .....: In [625]: data = pd.DataFrame(d, columns=c) In [626]: data Out[626]: id Date Col_1 Col_2 Col_3 0 26 2010-10-18 X 27.50 True 1 42 2010-10-19 Y -12.50 False 2 63 2010-10-20 Z 5.73 True In [627]: data.to_sql("data", engine) Out[627]: 3 With some databases, writing large DataFrames can result in errors due to packet size limitations being exceeded. This can be avoided by setting the chunksize parameter when calling to_sql. For example, the following writes data to the database in batches of 1000 rows at a time: In [628]: data.to_sql("data_chunked", engine, chunksize=1000) Out[628]: 3 SQL data types# to_sql() will try to map your data to an appropriate SQL data type based on the dtype of the data. When you have columns of dtype object, pandas will try to infer the data type. You can always override the default type by specifying the desired SQL type of any of the columns by using the dtype argument. This argument needs a dictionary mapping column names to SQLAlchemy types (or strings for the sqlite3 fallback mode). For example, specifying to use the sqlalchemy String type instead of the default Text type for string columns: In [629]: from sqlalchemy.types import String In [630]: data.to_sql("data_dtype", engine, dtype={"Col_1": String}) Out[630]: 3 Note Due to the limited support for timedelta’s in the different database flavors, columns with type timedelta64 will be written as integer values as nanoseconds to the database and a warning will be raised. Note Columns of category dtype will be converted to the dense representation as you would get with np.asarray(categorical) (e.g. for string categories this gives an array of strings). Because of this, reading the database table back in does not generate a categorical. Datetime data types# Using SQLAlchemy, to_sql() is capable of writing datetime data that is timezone naive or timezone aware. However, the resulting data stored in the database ultimately depends on the supported data type for datetime data of the database system being used. The following table lists supported data types for datetime data for some common databases. Other database dialects may have different data types for datetime data. Database SQL Datetime Types Timezone Support SQLite TEXT No MySQL TIMESTAMP or DATETIME No PostgreSQL TIMESTAMP or TIMESTAMP WITH TIME ZONE Yes When writing timezone aware data to databases that do not support timezones, the data will be written as timezone naive timestamps that are in local time with respect to the timezone. read_sql_table() is also capable of reading datetime data that is timezone aware or naive. When reading TIMESTAMP WITH TIME ZONE types, pandas will convert the data to UTC. Insertion method# The parameter method controls the SQL insertion clause used. Possible values are: None: Uses standard SQL INSERT clause (one per row). 'multi': Pass multiple values in a single INSERT clause. It uses a special SQL syntax not supported by all backends. This usually provides better performance for analytic databases like Presto and Redshift, but has worse performance for traditional SQL backend if the table contains many columns. For more information check the SQLAlchemy documentation. callable with signature (pd_table, conn, keys, data_iter): This can be used to implement a more performant insertion method based on specific backend dialect features. Example of a callable using PostgreSQL COPY clause: # Alternative to_sql() *method* for DBs that support COPY FROM import csv from io import StringIO def psql_insert_copy(table, conn, keys, data_iter): """ Execute SQL statement inserting data Parameters ---------- table : pandas.io.sql.SQLTable conn : sqlalchemy.engine.Engine or sqlalchemy.engine.Connection keys : list of str Column names data_iter : Iterable that iterates the values to be inserted """ # gets a DBAPI connection that can provide a cursor dbapi_conn = conn.connection with dbapi_conn.cursor() as cur: s_buf = StringIO() writer = csv.writer(s_buf) writer.writerows(data_iter) s_buf.seek(0) columns = ', '.join(['"{}"'.format(k) for k in keys]) if table.schema: table_name = '{}.{}'.format(table.schema, table.name) else: table_name = table.name sql = 'COPY {} ({}) FROM STDIN WITH CSV'.format( table_name, columns) cur.copy_expert(sql=sql, file=s_buf) Reading tables# read_sql_table() will read a database table given the table name and optionally a subset of columns to read. Note In order to use read_sql_table(), you must have the SQLAlchemy optional dependency installed. In [631]: pd.read_sql_table("data", engine) Out[631]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 X 27.50 True 1 1 42 2010-10-19 Y -12.50 False 2 2 63 2010-10-20 Z 5.73 True Note Note that pandas infers column dtypes from query outputs, and not by looking up data types in the physical database schema. For example, assume userid is an integer column in a table. Then, intuitively, select userid ... will return integer-valued series, while select cast(userid as text) ... will return object-valued (str) series. Accordingly, if the query output is empty, then all resulting columns will be returned as object-valued (since they are most general). If you foresee that your query will sometimes generate an empty result, you may want to explicitly typecast afterwards to ensure dtype integrity. You can also specify the name of the column as the DataFrame index, and specify a subset of columns to be read. In [632]: pd.read_sql_table("data", engine, index_col="id") Out[632]: index Date Col_1 Col_2 Col_3 id 26 0 2010-10-18 X 27.50 True 42 1 2010-10-19 Y -12.50 False 63 2 2010-10-20 Z 5.73 True In [633]: pd.read_sql_table("data", engine, columns=["Col_1", "Col_2"]) Out[633]: Col_1 Col_2 0 X 27.50 1 Y -12.50 2 Z 5.73 And you can explicitly force columns to be parsed as dates: In [634]: pd.read_sql_table("data", engine, parse_dates=["Date"]) Out[634]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 X 27.50 True 1 1 42 2010-10-19 Y -12.50 False 2 2 63 2010-10-20 Z 5.73 True If needed you can explicitly specify a format string, or a dict of arguments to pass to pandas.to_datetime(): pd.read_sql_table("data", engine, parse_dates={"Date": "%Y-%m-%d"}) pd.read_sql_table( "data", engine, parse_dates={"Date": {"format": "%Y-%m-%d %H:%M:%S"}}, ) You can check if a table exists using has_table() Schema support# Reading from and writing to different schema’s is supported through the schema keyword in the read_sql_table() and to_sql() functions. Note however that this depends on the database flavor (sqlite does not have schema’s). For example: df.to_sql("table", engine, schema="other_schema") pd.read_sql_table("table", engine, schema="other_schema") Querying# You can query using raw SQL in the read_sql_query() function. In this case you must use the SQL variant appropriate for your database. When using SQLAlchemy, you can also pass SQLAlchemy Expression language constructs, which are database-agnostic. In [635]: pd.read_sql_query("SELECT * FROM data", engine) Out[635]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 00:00:00.000000 X 27.50 1 1 1 42 2010-10-19 00:00:00.000000 Y -12.50 0 2 2 63 2010-10-20 00:00:00.000000 Z 5.73 1 Of course, you can specify a more “complex” query. In [636]: pd.read_sql_query("SELECT id, Col_1, Col_2 FROM data WHERE id = 42;", engine) Out[636]: id Col_1 Col_2 0 42 Y -12.5 The read_sql_query() function supports a chunksize argument. Specifying this will return an iterator through chunks of the query result: In [637]: df = pd.DataFrame(np.random.randn(20, 3), columns=list("abc")) In [638]: df.to_sql("data_chunks", engine, index=False) Out[638]: 20 In [639]: for chunk in pd.read_sql_query("SELECT * FROM data_chunks", engine, chunksize=5): .....: print(chunk) .....: a b c 0 0.070470 0.901320 0.937577 1 0.295770 1.420548 -0.005283 2 -1.518598 -0.730065 0.226497 3 -2.061465 0.632115 0.853619 4 2.719155 0.139018 0.214557 a b c 0 -1.538924 -0.366973 -0.748801 1 -0.478137 -1.559153 -3.097759 2 -2.320335 -0.221090 0.119763 3 0.608228 1.064810 -0.780506 4 -2.736887 0.143539 1.170191 a b c 0 -1.573076 0.075792 -1.722223 1 -0.774650 0.803627 0.221665 2 0.584637 0.147264 1.057825 3 -0.284136 0.912395 1.552808 4 0.189376 -0.109830 0.539341 a b c 0 0.592591 -0.155407 -1.356475 1 0.833837 1.524249 1.606722 2 -0.029487 -0.051359 1.700152 3 0.921484 -0.926347 0.979818 4 0.182380 -0.186376 0.049820 You can also run a plain query without creating a DataFrame with execute(). This is useful for queries that don’t return values, such as INSERT. This is functionally equivalent to calling execute on the SQLAlchemy engine or db connection object. Again, you must use the SQL syntax variant appropriate for your database. from pandas.io import sql sql.execute("SELECT * FROM table_name", engine) sql.execute( "INSERT INTO table_name VALUES(?, ?, ?)", engine, params=[("id", 1, 12.2, True)] ) Engine connection examples# To connect with SQLAlchemy you use the create_engine() function to create an engine object from database URI. You only need to create the engine once per database you are connecting to. from sqlalchemy import create_engine engine = create_engine("postgresql://scott:[email protected]:5432/mydatabase") engine = create_engine("mysql+mysqldb://scott:[email protected]/foo") engine = create_engine("oracle://scott:[email protected]:1521/sidname") engine = create_engine("mssql+pyodbc://mydsn") # sqlite://<nohostname>/<path> # where <path> is relative: engine = create_engine("sqlite:///foo.db") # or absolute, starting with a slash: engine = create_engine("sqlite:////absolute/path/to/foo.db") For more information see the examples the SQLAlchemy documentation Advanced SQLAlchemy queries# You can use SQLAlchemy constructs to describe your query. Use sqlalchemy.text() to specify query parameters in a backend-neutral way In [640]: import sqlalchemy as sa In [641]: pd.read_sql( .....: sa.text("SELECT * FROM data where Col_1=:col1"), engine, params={"col1": "X"} .....: ) .....: Out[641]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 00:00:00.000000 X 27.5 1 If you have an SQLAlchemy description of your database you can express where conditions using SQLAlchemy expressions In [642]: metadata = sa.MetaData() In [643]: data_table = sa.Table( .....: "data", .....: metadata, .....: sa.Column("index", sa.Integer), .....: sa.Column("Date", sa.DateTime), .....: sa.Column("Col_1", sa.String), .....: sa.Column("Col_2", sa.Float), .....: sa.Column("Col_3", sa.Boolean), .....: ) .....: In [644]: pd.read_sql(sa.select([data_table]).where(data_table.c.Col_3 is True), engine) Out[644]: Empty DataFrame Columns: [index, Date, Col_1, Col_2, Col_3] Index: [] You can combine SQLAlchemy expressions with parameters passed to read_sql() using sqlalchemy.bindparam() In [645]: import datetime as dt In [646]: expr = sa.select([data_table]).where(data_table.c.Date > sa.bindparam("date")) In [647]: pd.read_sql(expr, engine, params={"date": dt.datetime(2010, 10, 18)}) Out[647]: index Date Col_1 Col_2 Col_3 0 1 2010-10-19 Y -12.50 False 1 2 2010-10-20 Z 5.73 True Sqlite fallback# The use of sqlite is supported without using SQLAlchemy. This mode requires a Python database adapter which respect the Python DB-API. You can create connections like so: import sqlite3 con = sqlite3.connect(":memory:") And then issue the following queries: data.to_sql("data", con) pd.read_sql_query("SELECT * FROM data", con) Google BigQuery# Warning Starting in 0.20.0, pandas has split off Google BigQuery support into the separate package pandas-gbq. You can pip install pandas-gbq to get it. The pandas-gbq package provides functionality to read/write from Google BigQuery. pandas integrates with this external package. if pandas-gbq is installed, you can use the pandas methods pd.read_gbq and DataFrame.to_gbq, which will call the respective functions from pandas-gbq. Full documentation can be found here. Stata format# Writing to stata format# The method to_stata() will write a DataFrame into a .dta file. The format version of this file is always 115 (Stata 12). In [648]: df = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [649]: df.to_stata("stata.dta") Stata data files have limited data type support; only strings with 244 or fewer characters, int8, int16, int32, float32 and float64 can be stored in .dta files. Additionally, Stata reserves certain values to represent missing data. Exporting a non-missing value that is outside of the permitted range in Stata for a particular data type will retype the variable to the next larger size. For example, int8 values are restricted to lie between -127 and 100 in Stata, and so variables with values above 100 will trigger a conversion to int16. nan values in floating points data types are stored as the basic missing data type (. in Stata). Note It is not possible to export missing data values for integer data types. The Stata writer gracefully handles other data types including int64, bool, uint8, uint16, uint32 by casting to the smallest supported type that can represent the data. For example, data with a type of uint8 will be cast to int8 if all values are less than 100 (the upper bound for non-missing int8 data in Stata), or, if values are outside of this range, the variable is cast to int16. Warning Conversion from int64 to float64 may result in a loss of precision if int64 values are larger than 2**53. Warning StataWriter and to_stata() only support fixed width strings containing up to 244 characters, a limitation imposed by the version 115 dta file format. Attempting to write Stata dta files with strings longer than 244 characters raises a ValueError. Reading from Stata format# The top-level function read_stata will read a dta file and return either a DataFrame or a StataReader that can be used to read the file incrementally. In [650]: pd.read_stata("stata.dta") Out[650]: index A B 0 0 -1.690072 0.405144 1 1 -1.511309 -1.531396 2 2 0.572698 -1.106845 3 3 -1.185859 0.174564 4 4 0.603797 -1.796129 5 5 -0.791679 1.173795 6 6 -0.277710 1.859988 7 7 -0.258413 1.251808 8 8 1.443262 0.441553 9 9 1.168163 -2.054946 Specifying a chunksize yields a StataReader instance that can be used to read chunksize lines from the file at a time. The StataReader object can be used as an iterator. In [651]: with pd.read_stata("stata.dta", chunksize=3) as reader: .....: for df in reader: .....: print(df.shape) .....: (3, 3) (3, 3) (3, 3) (1, 3) For more fine-grained control, use iterator=True and specify chunksize with each call to read(). In [652]: with pd.read_stata("stata.dta", iterator=True) as reader: .....: chunk1 = reader.read(5) .....: chunk2 = reader.read(5) .....: Currently the index is retrieved as a column. The parameter convert_categoricals indicates whether value labels should be read and used to create a Categorical variable from them. Value labels can also be retrieved by the function value_labels, which requires read() to be called before use. The parameter convert_missing indicates whether missing value representations in Stata should be preserved. If False (the default), missing values are represented as np.nan. If True, missing values are represented using StataMissingValue objects, and columns containing missing values will have object data type. Note read_stata() and StataReader support .dta formats 113-115 (Stata 10-12), 117 (Stata 13), and 118 (Stata 14). Note Setting preserve_dtypes=False will upcast to the standard pandas data types: int64 for all integer types and float64 for floating point data. By default, the Stata data types are preserved when importing. Categorical data# Categorical data can be exported to Stata data files as value labeled data. The exported data consists of the underlying category codes as integer data values and the categories as value labels. Stata does not have an explicit equivalent to a Categorical and information about whether the variable is ordered is lost when exporting. Warning Stata only supports string value labels, and so str is called on the categories when exporting data. Exporting Categorical variables with non-string categories produces a warning, and can result a loss of information if the str representations of the categories are not unique. Labeled data can similarly be imported from Stata data files as Categorical variables using the keyword argument convert_categoricals (True by default). The keyword argument order_categoricals (True by default) determines whether imported Categorical variables are ordered. Note When importing categorical data, the values of the variables in the Stata data file are not preserved since Categorical variables always use integer data types between -1 and n-1 where n is the number of categories. If the original values in the Stata data file are required, these can be imported by setting convert_categoricals=False, which will import original data (but not the variable labels). The original values can be matched to the imported categorical data since there is a simple mapping between the original Stata data values and the category codes of imported Categorical variables: missing values are assigned code -1, and the smallest original value is assigned 0, the second smallest is assigned 1 and so on until the largest original value is assigned the code n-1. Note Stata supports partially labeled series. These series have value labels for some but not all data values. Importing a partially labeled series will produce a Categorical with string categories for the values that are labeled and numeric categories for values with no label. SAS formats# The top-level function read_sas() can read (but not write) SAS XPORT (.xpt) and (since v0.18.0) SAS7BDAT (.sas7bdat) format files. SAS files only contain two value types: ASCII text and floating point values (usually 8 bytes but sometimes truncated). For xport files, there is no automatic type conversion to integers, dates, or categoricals. For SAS7BDAT files, the format codes may allow date variables to be automatically converted to dates. By default the whole file is read and returned as a DataFrame. Specify a chunksize or use iterator=True to obtain reader objects (XportReader or SAS7BDATReader) for incrementally reading the file. The reader objects also have attributes that contain additional information about the file and its variables. Read a SAS7BDAT file: df = pd.read_sas("sas_data.sas7bdat") Obtain an iterator and read an XPORT file 100,000 lines at a time: def do_something(chunk): pass with pd.read_sas("sas_xport.xpt", chunk=100000) as rdr: for chunk in rdr: do_something(chunk) The specification for the xport file format is available from the SAS web site. No official documentation is available for the SAS7BDAT format. SPSS formats# New in version 0.25.0. The top-level function read_spss() can read (but not write) SPSS SAV (.sav) and ZSAV (.zsav) format files. SPSS files contain column names. By default the whole file is read, categorical columns are converted into pd.Categorical, and a DataFrame with all columns is returned. Specify the usecols parameter to obtain a subset of columns. Specify convert_categoricals=False to avoid converting categorical columns into pd.Categorical. Read an SPSS file: df = pd.read_spss("spss_data.sav") Extract a subset of columns contained in usecols from an SPSS file and avoid converting categorical columns into pd.Categorical: df = pd.read_spss( "spss_data.sav", usecols=["foo", "bar"], convert_categoricals=False, ) More information about the SAV and ZSAV file formats is available here. Other file formats# pandas itself only supports IO with a limited set of file formats that map cleanly to its tabular data model. For reading and writing other file formats into and from pandas, we recommend these packages from the broader community. netCDF# xarray provides data structures inspired by the pandas DataFrame for working with multi-dimensional datasets, with a focus on the netCDF file format and easy conversion to and from pandas. Performance considerations# This is an informal comparison of various IO methods, using pandas 0.24.2. Timings are machine dependent and small differences should be ignored. In [1]: sz = 1000000 In [2]: df = pd.DataFrame({'A': np.random.randn(sz), 'B': [1] * sz}) In [3]: df.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 1000000 entries, 0 to 999999 Data columns (total 2 columns): A 1000000 non-null float64 B 1000000 non-null int64 dtypes: float64(1), int64(1) memory usage: 15.3 MB The following test functions will be used below to compare the performance of several IO methods: import numpy as np import os sz = 1000000 df = pd.DataFrame({"A": np.random.randn(sz), "B": [1] * sz}) sz = 1000000 np.random.seed(42) df = pd.DataFrame({"A": np.random.randn(sz), "B": [1] * sz}) def test_sql_write(df): if os.path.exists("test.sql"): os.remove("test.sql") sql_db = sqlite3.connect("test.sql") df.to_sql(name="test_table", con=sql_db) sql_db.close() def test_sql_read(): sql_db = sqlite3.connect("test.sql") pd.read_sql_query("select * from test_table", sql_db) sql_db.close() def test_hdf_fixed_write(df): df.to_hdf("test_fixed.hdf", "test", mode="w") def test_hdf_fixed_read(): pd.read_hdf("test_fixed.hdf", "test") def test_hdf_fixed_write_compress(df): df.to_hdf("test_fixed_compress.hdf", "test", mode="w", complib="blosc") def test_hdf_fixed_read_compress(): pd.read_hdf("test_fixed_compress.hdf", "test") def test_hdf_table_write(df): df.to_hdf("test_table.hdf", "test", mode="w", format="table") def test_hdf_table_read(): pd.read_hdf("test_table.hdf", "test") def test_hdf_table_write_compress(df): df.to_hdf( "test_table_compress.hdf", "test", mode="w", complib="blosc", format="table" ) def test_hdf_table_read_compress(): pd.read_hdf("test_table_compress.hdf", "test") def test_csv_write(df): df.to_csv("test.csv", mode="w") def test_csv_read(): pd.read_csv("test.csv", index_col=0) def test_feather_write(df): df.to_feather("test.feather") def test_feather_read(): pd.read_feather("test.feather") def test_pickle_write(df): df.to_pickle("test.pkl") def test_pickle_read(): pd.read_pickle("test.pkl") def test_pickle_write_compress(df): df.to_pickle("test.pkl.compress", compression="xz") def test_pickle_read_compress(): pd.read_pickle("test.pkl.compress", compression="xz") def test_parquet_write(df): df.to_parquet("test.parquet") def test_parquet_read(): pd.read_parquet("test.parquet") When writing, the top three functions in terms of speed are test_feather_write, test_hdf_fixed_write and test_hdf_fixed_write_compress. In [4]: %timeit test_sql_write(df) 3.29 s ± 43.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [5]: %timeit test_hdf_fixed_write(df) 19.4 ms ± 560 µs per loop (mean ± std. dev. of 7 runs, 1 loop each) In [6]: %timeit test_hdf_fixed_write_compress(df) 19.6 ms ± 308 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [7]: %timeit test_hdf_table_write(df) 449 ms ± 5.61 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [8]: %timeit test_hdf_table_write_compress(df) 448 ms ± 11.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [9]: %timeit test_csv_write(df) 3.66 s ± 26.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [10]: %timeit test_feather_write(df) 9.75 ms ± 117 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [11]: %timeit test_pickle_write(df) 30.1 ms ± 229 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [12]: %timeit test_pickle_write_compress(df) 4.29 s ± 15.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [13]: %timeit test_parquet_write(df) 67.6 ms ± 706 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) When reading, the top three functions in terms of speed are test_feather_read, test_pickle_read and test_hdf_fixed_read. In [14]: %timeit test_sql_read() 1.77 s ± 17.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [15]: %timeit test_hdf_fixed_read() 19.4 ms ± 436 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [16]: %timeit test_hdf_fixed_read_compress() 19.5 ms ± 222 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [17]: %timeit test_hdf_table_read() 38.6 ms ± 857 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [18]: %timeit test_hdf_table_read_compress() 38.8 ms ± 1.49 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) In [19]: %timeit test_csv_read() 452 ms ± 9.04 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [20]: %timeit test_feather_read() 12.4 ms ± 99.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [21]: %timeit test_pickle_read() 18.4 ms ± 191 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [22]: %timeit test_pickle_read_compress() 915 ms ± 7.48 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [23]: %timeit test_parquet_read() 24.4 ms ± 146 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) The files test.pkl.compress, test.parquet and test.feather took the least space on disk (in bytes). 29519500 Oct 10 06:45 test.csv 16000248 Oct 10 06:45 test.feather 8281983 Oct 10 06:49 test.parquet 16000857 Oct 10 06:47 test.pkl 7552144 Oct 10 06:48 test.pkl.compress 34816000 Oct 10 06:42 test.sql 24009288 Oct 10 06:43 test_fixed.hdf 24009288 Oct 10 06:43 test_fixed_compress.hdf 24458940 Oct 10 06:44 test_table.hdf 24458940 Oct 10 06:44 test_table_compress.hdf
1,248
1,308
Different output on different machine python code I'm trying to import multiple csv files into a dictionary. My idea is key is present the name of csv files, and value of each key is the table inside csv as DataFrame type. My code is like this : import pandas as pd data = '.././data/raw/' all_files = [data + 'x.csv', data + 'y.csv'] list_a = [] result_dict = dict() for filename in all_files: df = pd.read_csv(filename, index_col=None, header=0, encoding='mac_roman') key = filename.split('/')[1] result_dict[key]=df # print(result_dict) def get_dataframe(name): dataframe = result_dict.get(name) return dataframe m_taiin =get_dataframe('x.csv') type(m_taiin) print(isinstance(m_taiin,pd.DataFrame)) But when I run this code on my Macbook - Python3.7 and my Ubuntu 16.04 - Python3.6 has result True. But when I run on Arch Linux - Python 3.7 my result is False. The value is NoneType not DataFrame. I don't know which is the problem here.
/ [CLS] Python machine python code multiple csv files import [SEP]r if the query expression is not valid. Query timedelta64 [ ns ] # You can store and query using the timedelta64 [ ns ] type. Terms can be specified in the format : < float > ( < unit > ), where float may be signed ( and fractional ), and unit can be D, s, ms, us, ns for the timedelta. Here ’ s an example : In [ 496 ] : from datetime import timedelta / [CLS] Python machine python code multiple csv files import [SEP] try to map your data to an appropriate SQL data type based on the dtype of the data. When you have columns of dtype object, pandas will try to infer the data type. You can always override the default type by specifying the desired SQL type of any of the columns by using the dtype argument. This argument needs a dictionary mapping column names to SQLAlchemy types ( or strings for the sqlite3 fallback mode ). For example, specifying to use the sqlalchemy String type instead of the default Text type for string columns : In [ 629 ] : from sqlalchemy. types import String / /
Try like this in for loop ... key = filename.split('/')[-1]
68,188,437
How to invert a Boolean column (i.e True to False and vice versa) in a DataFrame?
<p>I have 3 columns in a Pandas DataFrame where I have to compare 2 columns.</p> <p>in simple terms.. result_column = not(column1) or (column2)</p> <p>I tried the following</p> <pre><code>df['status'] = ~df['offline'] | df['online'] </code></pre> <p>but the above line is resulting in the error.</p> <p>TypeError: bad operand type for unary ~: 'float'</p> <p>I searched around for solution and found that '~' is used for Series data structure. There isn't any example for the same for DataFrame. Appreciate your time.</p>
68,188,754
"2021-06-30T04:25:58.367000"
1
null
-2
639
python|pandas
<p>Something in your dataframe is not the type of data you are expecting. I'm not sure what data is causing the error. Sometimes null values cause the error you are getting, in which case you can use ', na=False' to fix it. But in this case, I have no problem with floats (2.2) or nulls (np.nan) so I don't know what data would produce that error. See this toy example:</p> <pre><code>row1list = [True, False] row2list = [True, True] row3list = [False, 2.2] row4list = [False, np.nan] df = pd.DataFrame([row1list, row2list, row3list, row4list], columns=['column1', 'column2']) df['result_column'] = ~df['column1'] | df['column2'] print(df) # column1 column2 result_column # 0 True False False # 1 True True True # 2 False 2.2 True # 3 False NaN True </code></pre>
"2021-06-30T05:09:33.080000"
1
https://pandas.pydata.org/docs/user_guide/reshaping.html
Reshaping and pivot tables# Reshaping and pivot tables# Reshaping by pivoting DataFrame objects# Something in your dataframe is not the type of data you are expecting. I'm not sure what data is causing the error. Sometimes null values cause the error you are getting, in which case you can use ', na=False' to fix it. But in this case, I have no problem with floats (2.2) or nulls (np.nan) so I don't know what data would produce that error. See this toy example: row1list = [True, False] row2list = [True, True] row3list = [False, 2.2] row4list = [False, np.nan] df = pd.DataFrame([row1list, row2list, row3list, row4list], columns=['column1', 'column2']) df['result_column'] = ~df['column1'] | df['column2'] print(df) # column1 column2 result_column # 0 True False False # 1 True True True # 2 False 2.2 True # 3 False NaN True Data is often stored in so-called “stacked” or “record” format: In [1]: import pandas._testing as tm In [2]: def unpivot(frame): ...: N, K = frame.shape ...: data = { ...: "value": frame.to_numpy().ravel("F"), ...: "variable": np.asarray(frame.columns).repeat(N), ...: "date": np.tile(np.asarray(frame.index), K), ...: } ...: return pd.DataFrame(data, columns=["date", "variable", "value"]) ...: In [3]: df = unpivot(tm.makeTimeDataFrame(3)) In [4]: df Out[4]: date variable value 0 2000-01-03 A 0.469112 1 2000-01-04 A -0.282863 2 2000-01-05 A -1.509059 3 2000-01-03 B -1.135632 4 2000-01-04 B 1.212112 5 2000-01-05 B -0.173215 6 2000-01-03 C 0.119209 7 2000-01-04 C -1.044236 8 2000-01-05 C -0.861849 9 2000-01-03 D -2.104569 10 2000-01-04 D -0.494929 11 2000-01-05 D 1.071804 To select out everything for variable A we could do: In [5]: filtered = df[df["variable"] == "A"] In [6]: filtered Out[6]: date variable value 0 2000-01-03 A 0.469112 1 2000-01-04 A -0.282863 2 2000-01-05 A -1.509059 But suppose we wish to do time series operations with the variables. A better representation would be where the columns are the unique variables and an index of dates identifies individual observations. To reshape the data into this form, we use the DataFrame.pivot() method (also implemented as a top level function pivot()): In [7]: pivoted = df.pivot(index="date", columns="variable", values="value") In [8]: pivoted Out[8]: variable A B C D date 2000-01-03 0.469112 -1.135632 0.119209 -2.104569 2000-01-04 -0.282863 1.212112 -1.044236 -0.494929 2000-01-05 -1.509059 -0.173215 -0.861849 1.071804 If the values argument is omitted, and the input DataFrame has more than one column of values which are not used as column or index inputs to pivot(), then the resulting “pivoted” DataFrame will have hierarchical columns whose topmost level indicates the respective value column: In [9]: df["value2"] = df["value"] * 2 In [10]: pivoted = df.pivot(index="date", columns="variable") In [11]: pivoted Out[11]: value ... value2 variable A B C ... B C D date ... 2000-01-03 0.469112 -1.135632 0.119209 ... -2.271265 0.238417 -4.209138 2000-01-04 -0.282863 1.212112 -1.044236 ... 2.424224 -2.088472 -0.989859 2000-01-05 -1.509059 -0.173215 -0.861849 ... -0.346429 -1.723698 2.143608 [3 rows x 8 columns] You can then select subsets from the pivoted DataFrame: In [12]: pivoted["value2"] Out[12]: variable A B C D date 2000-01-03 0.938225 -2.271265 0.238417 -4.209138 2000-01-04 -0.565727 2.424224 -2.088472 -0.989859 2000-01-05 -3.018117 -0.346429 -1.723698 2.143608 Note that this returns a view on the underlying data in the case where the data are homogeneously-typed. Note pivot() will error with a ValueError: Index contains duplicate entries, cannot reshape if the index/column pair is not unique. In this case, consider using pivot_table() which is a generalization of pivot that can handle duplicate values for one index/column pair. Reshaping by stacking and unstacking# Closely related to the pivot() method are the related stack() and unstack() methods available on Series and DataFrame. These methods are designed to work together with MultiIndex objects (see the section on hierarchical indexing). Here are essentially what these methods do: stack(): “pivot” a level of the (possibly hierarchical) column labels, returning a DataFrame with an index with a new inner-most level of row labels. unstack(): (inverse operation of stack()) “pivot” a level of the (possibly hierarchical) row index to the column axis, producing a reshaped DataFrame with a new inner-most level of column labels. The clearest way to explain is by example. Let’s take a prior example data set from the hierarchical indexing section: In [13]: tuples = list( ....: zip( ....: *[ ....: ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"], ....: ["one", "two", "one", "two", "one", "two", "one", "two"], ....: ] ....: ) ....: ) ....: In [14]: index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"]) In [15]: df = pd.DataFrame(np.random.randn(8, 2), index=index, columns=["A", "B"]) In [16]: df2 = df[:4] In [17]: df2 Out[17]: A B first second bar one 0.721555 -0.706771 two -1.039575 0.271860 baz one -0.424972 0.567020 two 0.276232 -1.087401 The stack() function “compresses” a level in the DataFrame columns to produce either: A Series, in the case of a simple column Index. A DataFrame, in the case of a MultiIndex in the columns. If the columns have a MultiIndex, you can choose which level to stack. The stacked level becomes the new lowest level in a MultiIndex on the columns: In [18]: stacked = df2.stack() In [19]: stacked Out[19]: first second bar one A 0.721555 B -0.706771 two A -1.039575 B 0.271860 baz one A -0.424972 B 0.567020 two A 0.276232 B -1.087401 dtype: float64 With a “stacked” DataFrame or Series (having a MultiIndex as the index), the inverse operation of stack() is unstack(), which by default unstacks the last level: In [20]: stacked.unstack() Out[20]: A B first second bar one 0.721555 -0.706771 two -1.039575 0.271860 baz one -0.424972 0.567020 two 0.276232 -1.087401 In [21]: stacked.unstack(1) Out[21]: second one two first bar A 0.721555 -1.039575 B -0.706771 0.271860 baz A -0.424972 0.276232 B 0.567020 -1.087401 In [22]: stacked.unstack(0) Out[22]: first bar baz second one A 0.721555 -0.424972 B -0.706771 0.567020 two A -1.039575 0.276232 B 0.271860 -1.087401 If the indexes have names, you can use the level names instead of specifying the level numbers: In [23]: stacked.unstack("second") Out[23]: second one two first bar A 0.721555 -1.039575 B -0.706771 0.271860 baz A -0.424972 0.276232 B 0.567020 -1.087401 Notice that the stack() and unstack() methods implicitly sort the index levels involved. Hence a call to stack() and then unstack(), or vice versa, will result in a sorted copy of the original DataFrame or Series: In [24]: index = pd.MultiIndex.from_product([[2, 1], ["a", "b"]]) In [25]: df = pd.DataFrame(np.random.randn(4), index=index, columns=["A"]) In [26]: df Out[26]: A 2 a -0.370647 b -1.157892 1 a -1.344312 b 0.844885 In [27]: all(df.unstack().stack() == df.sort_index()) Out[27]: True The above code will raise a TypeError if the call to sort_index() is removed. Multiple levels# You may also stack or unstack more than one level at a time by passing a list of levels, in which case the end result is as if each level in the list were processed individually. In [28]: columns = pd.MultiIndex.from_tuples( ....: [ ....: ("A", "cat", "long"), ....: ("B", "cat", "long"), ....: ("A", "dog", "short"), ....: ("B", "dog", "short"), ....: ], ....: names=["exp", "animal", "hair_length"], ....: ) ....: In [29]: df = pd.DataFrame(np.random.randn(4, 4), columns=columns) In [30]: df Out[30]: exp A B A B animal cat cat dog dog hair_length long long short short 0 1.075770 -0.109050 1.643563 -1.469388 1 0.357021 -0.674600 -1.776904 -0.968914 2 -1.294524 0.413738 0.276662 -0.472035 3 -0.013960 -0.362543 -0.006154 -0.923061 In [31]: df.stack(level=["animal", "hair_length"]) Out[31]: exp A B animal hair_length 0 cat long 1.075770 -0.109050 dog short 1.643563 -1.469388 1 cat long 0.357021 -0.674600 dog short -1.776904 -0.968914 2 cat long -1.294524 0.413738 dog short 0.276662 -0.472035 3 cat long -0.013960 -0.362543 dog short -0.006154 -0.923061 The list of levels can contain either level names or level numbers (but not a mixture of the two). # df.stack(level=['animal', 'hair_length']) # from above is equivalent to: In [32]: df.stack(level=[1, 2]) Out[32]: exp A B animal hair_length 0 cat long 1.075770 -0.109050 dog short 1.643563 -1.469388 1 cat long 0.357021 -0.674600 dog short -1.776904 -0.968914 2 cat long -1.294524 0.413738 dog short 0.276662 -0.472035 3 cat long -0.013960 -0.362543 dog short -0.006154 -0.923061 Missing data# These functions are intelligent about handling missing data and do not expect each subgroup within the hierarchical index to have the same set of labels. They also can handle the index being unsorted (but you can make it sorted by calling sort_index(), of course). Here is a more complex example: In [33]: columns = pd.MultiIndex.from_tuples( ....: [ ....: ("A", "cat"), ....: ("B", "dog"), ....: ("B", "cat"), ....: ("A", "dog"), ....: ], ....: names=["exp", "animal"], ....: ) ....: In [34]: index = pd.MultiIndex.from_product( ....: [("bar", "baz", "foo", "qux"), ("one", "two")], names=["first", "second"] ....: ) ....: In [35]: df = pd.DataFrame(np.random.randn(8, 4), index=index, columns=columns) In [36]: df2 = df.iloc[[0, 1, 2, 4, 5, 7]] In [37]: df2 Out[37]: exp A B A animal cat dog cat dog first second bar one 0.895717 0.805244 -1.206412 2.565646 two 1.431256 1.340309 -1.170299 -0.226169 baz one 0.410835 0.813850 0.132003 -0.827317 foo one -1.413681 1.607920 1.024180 0.569605 two 0.875906 -2.211372 0.974466 -2.006747 qux two -1.226825 0.769804 -1.281247 -0.727707 As mentioned above, stack() can be called with a level argument to select which level in the columns to stack: In [38]: df2.stack("exp") Out[38]: animal cat dog first second exp bar one A 0.895717 2.565646 B -1.206412 0.805244 two A 1.431256 -0.226169 B -1.170299 1.340309 baz one A 0.410835 -0.827317 B 0.132003 0.813850 foo one A -1.413681 0.569605 B 1.024180 1.607920 two A 0.875906 -2.006747 B 0.974466 -2.211372 qux two A -1.226825 -0.727707 B -1.281247 0.769804 In [39]: df2.stack("animal") Out[39]: exp A B first second animal bar one cat 0.895717 -1.206412 dog 2.565646 0.805244 two cat 1.431256 -1.170299 dog -0.226169 1.340309 baz one cat 0.410835 0.132003 dog -0.827317 0.813850 foo one cat -1.413681 1.024180 dog 0.569605 1.607920 two cat 0.875906 0.974466 dog -2.006747 -2.211372 qux two cat -1.226825 -1.281247 dog -0.727707 0.769804 Unstacking can result in missing values if subgroups do not have the same set of labels. By default, missing values will be replaced with the default fill value for that data type, NaN for float, NaT for datetimelike, etc. For integer types, by default data will converted to float and missing values will be set to NaN. In [40]: df3 = df.iloc[[0, 1, 4, 7], [1, 2]] In [41]: df3 Out[41]: exp B animal dog cat first second bar one 0.805244 -1.206412 two 1.340309 -1.170299 foo one 1.607920 1.024180 qux two 0.769804 -1.281247 In [42]: df3.unstack() Out[42]: exp B animal dog cat second one two one two first bar 0.805244 1.340309 -1.206412 -1.170299 foo 1.607920 NaN 1.024180 NaN qux NaN 0.769804 NaN -1.281247 Alternatively, unstack takes an optional fill_value argument, for specifying the value of missing data. In [43]: df3.unstack(fill_value=-1e9) Out[43]: exp B animal dog cat second one two one two first bar 8.052440e-01 1.340309e+00 -1.206412e+00 -1.170299e+00 foo 1.607920e+00 -1.000000e+09 1.024180e+00 -1.000000e+09 qux -1.000000e+09 7.698036e-01 -1.000000e+09 -1.281247e+00 With a MultiIndex# Unstacking when the columns are a MultiIndex is also careful about doing the right thing: In [44]: df[:3].unstack(0) Out[44]: exp A B ... A animal cat dog ... cat dog first bar baz bar ... baz bar baz second ... one 0.895717 0.410835 0.805244 ... 0.132003 2.565646 -0.827317 two 1.431256 NaN 1.340309 ... NaN -0.226169 NaN [2 rows x 8 columns] In [45]: df2.unstack(1) Out[45]: exp A B ... A animal cat dog ... cat dog second one two one ... two one two first ... bar 0.895717 1.431256 0.805244 ... -1.170299 2.565646 -0.226169 baz 0.410835 NaN 0.813850 ... NaN -0.827317 NaN foo -1.413681 0.875906 1.607920 ... 0.974466 0.569605 -2.006747 qux NaN -1.226825 NaN ... -1.281247 NaN -0.727707 [4 rows x 8 columns] Reshaping by melt# The top-level melt() function and the corresponding DataFrame.melt() are useful to massage a DataFrame into a format where one or more columns are identifier variables, while all other columns, considered measured variables, are “unpivoted” to the row axis, leaving just two non-identifier columns, “variable” and “value”. The names of those columns can be customized by supplying the var_name and value_name parameters. For instance, In [46]: cheese = pd.DataFrame( ....: { ....: "first": ["John", "Mary"], ....: "last": ["Doe", "Bo"], ....: "height": [5.5, 6.0], ....: "weight": [130, 150], ....: } ....: ) ....: In [47]: cheese Out[47]: first last height weight 0 John Doe 5.5 130 1 Mary Bo 6.0 150 In [48]: cheese.melt(id_vars=["first", "last"]) Out[48]: first last variable value 0 John Doe height 5.5 1 Mary Bo height 6.0 2 John Doe weight 130.0 3 Mary Bo weight 150.0 In [49]: cheese.melt(id_vars=["first", "last"], var_name="quantity") Out[49]: first last quantity value 0 John Doe height 5.5 1 Mary Bo height 6.0 2 John Doe weight 130.0 3 Mary Bo weight 150.0 When transforming a DataFrame using melt(), the index will be ignored. The original index values can be kept around by setting the ignore_index parameter to False (default is True). This will however duplicate them. New in version 1.1.0. In [50]: index = pd.MultiIndex.from_tuples([("person", "A"), ("person", "B")]) In [51]: cheese = pd.DataFrame( ....: { ....: "first": ["John", "Mary"], ....: "last": ["Doe", "Bo"], ....: "height": [5.5, 6.0], ....: "weight": [130, 150], ....: }, ....: index=index, ....: ) ....: In [52]: cheese Out[52]: first last height weight person A John Doe 5.5 130 B Mary Bo 6.0 150 In [53]: cheese.melt(id_vars=["first", "last"]) Out[53]: first last variable value 0 John Doe height 5.5 1 Mary Bo height 6.0 2 John Doe weight 130.0 3 Mary Bo weight 150.0 In [54]: cheese.melt(id_vars=["first", "last"], ignore_index=False) Out[54]: first last variable value person A John Doe height 5.5 B Mary Bo height 6.0 A John Doe weight 130.0 B Mary Bo weight 150.0 Another way to transform is to use the wide_to_long() panel data convenience function. It is less flexible than melt(), but more user-friendly. In [55]: dft = pd.DataFrame( ....: { ....: "A1970": {0: "a", 1: "b", 2: "c"}, ....: "A1980": {0: "d", 1: "e", 2: "f"}, ....: "B1970": {0: 2.5, 1: 1.2, 2: 0.7}, ....: "B1980": {0: 3.2, 1: 1.3, 2: 0.1}, ....: "X": dict(zip(range(3), np.random.randn(3))), ....: } ....: ) ....: In [56]: dft["id"] = dft.index In [57]: dft Out[57]: A1970 A1980 B1970 B1980 X id 0 a d 2.5 3.2 -0.121306 0 1 b e 1.2 1.3 -0.097883 1 2 c f 0.7 0.1 0.695775 2 In [58]: pd.wide_to_long(dft, ["A", "B"], i="id", j="year") Out[58]: X A B id year 0 1970 -0.121306 a 2.5 1 1970 -0.097883 b 1.2 2 1970 0.695775 c 0.7 0 1980 -0.121306 d 3.2 1 1980 -0.097883 e 1.3 2 1980 0.695775 f 0.1 Combining with stats and GroupBy# It should be no shock that combining pivot() / stack() / unstack() with GroupBy and the basic Series and DataFrame statistical functions can produce some very expressive and fast data manipulations. In [59]: df Out[59]: exp A B A animal cat dog cat dog first second bar one 0.895717 0.805244 -1.206412 2.565646 two 1.431256 1.340309 -1.170299 -0.226169 baz one 0.410835 0.813850 0.132003 -0.827317 two -0.076467 -1.187678 1.130127 -1.436737 foo one -1.413681 1.607920 1.024180 0.569605 two 0.875906 -2.211372 0.974466 -2.006747 qux one -0.410001 -0.078638 0.545952 -1.219217 two -1.226825 0.769804 -1.281247 -0.727707 In [60]: df.stack().mean(1).unstack() Out[60]: animal cat dog first second bar one -0.155347 1.685445 two 0.130479 0.557070 baz one 0.271419 -0.006733 two 0.526830 -1.312207 foo one -0.194750 1.088763 two 0.925186 -2.109060 qux one 0.067976 -0.648927 two -1.254036 0.021048 # same result, another way In [61]: df.groupby(level=1, axis=1).mean() Out[61]: animal cat dog first second bar one -0.155347 1.685445 two 0.130479 0.557070 baz one 0.271419 -0.006733 two 0.526830 -1.312207 foo one -0.194750 1.088763 two 0.925186 -2.109060 qux one 0.067976 -0.648927 two -1.254036 0.021048 In [62]: df.stack().groupby(level=1).mean() Out[62]: exp A B second one 0.071448 0.455513 two -0.424186 -0.204486 In [63]: df.mean().unstack(0) Out[63]: exp A B animal cat 0.060843 0.018596 dog -0.413580 0.232430 Pivot tables# While pivot() provides general purpose pivoting with various data types (strings, numerics, etc.), pandas also provides pivot_table() for pivoting with aggregation of numeric data. The function pivot_table() can be used to create spreadsheet-style pivot tables. See the cookbook for some advanced strategies. It takes a number of arguments: data: a DataFrame object. values: a column or a list of columns to aggregate. index: a column, Grouper, array which has the same length as data, or list of them. Keys to group by on the pivot table index. If an array is passed, it is being used as the same manner as column values. columns: a column, Grouper, array which has the same length as data, or list of them. Keys to group by on the pivot table column. If an array is passed, it is being used as the same manner as column values. aggfunc: function to use for aggregation, defaulting to numpy.mean. Consider a data set like this: In [64]: import datetime In [65]: df = pd.DataFrame( ....: { ....: "A": ["one", "one", "two", "three"] * 6, ....: "B": ["A", "B", "C"] * 8, ....: "C": ["foo", "foo", "foo", "bar", "bar", "bar"] * 4, ....: "D": np.random.randn(24), ....: "E": np.random.randn(24), ....: "F": [datetime.datetime(2013, i, 1) for i in range(1, 13)] ....: + [datetime.datetime(2013, i, 15) for i in range(1, 13)], ....: } ....: ) ....: In [66]: df Out[66]: A B C D E F 0 one A foo 0.341734 -0.317441 2013-01-01 1 one B foo 0.959726 -1.236269 2013-02-01 2 two C foo -1.110336 0.896171 2013-03-01 3 three A bar -0.619976 -0.487602 2013-04-01 4 one B bar 0.149748 -0.082240 2013-05-01 .. ... .. ... ... ... ... 19 three B foo 0.690579 -2.213588 2013-08-15 20 one C foo 0.995761 1.063327 2013-09-15 21 one A bar 2.396780 1.266143 2013-10-15 22 two B bar 0.014871 0.299368 2013-11-15 23 three C bar 3.357427 -0.863838 2013-12-15 [24 rows x 6 columns] We can produce pivot tables from this data very easily: In [67]: pd.pivot_table(df, values="D", index=["A", "B"], columns=["C"]) Out[67]: C bar foo A B one A 1.120915 -0.514058 B -0.338421 0.002759 C -0.538846 0.699535 three A -1.181568 NaN B NaN 0.433512 C 0.588783 NaN two A NaN 1.000985 B 0.158248 NaN C NaN 0.176180 In [68]: pd.pivot_table(df, values="D", index=["B"], columns=["A", "C"], aggfunc=np.sum) Out[68]: A one three two C bar foo bar foo bar foo B A 2.241830 -1.028115 -2.363137 NaN NaN 2.001971 B -0.676843 0.005518 NaN 0.867024 0.316495 NaN C -1.077692 1.399070 1.177566 NaN NaN 0.352360 In [69]: pd.pivot_table( ....: df, values=["D", "E"], ....: index=["B"], ....: columns=["A", "C"], ....: aggfunc=np.sum, ....: ) ....: Out[69]: D ... E A one three ... three two C bar foo bar ... foo bar foo B ... A 2.241830 -1.028115 -2.363137 ... NaN NaN 0.128491 B -0.676843 0.005518 NaN ... -2.128743 -0.194294 NaN C -1.077692 1.399070 1.177566 ... NaN NaN 0.872482 [3 rows x 12 columns] The result object is a DataFrame having potentially hierarchical indexes on the rows and columns. If the values column name is not given, the pivot table will include all of the data in an additional level of hierarchy in the columns: In [70]: pd.pivot_table(df[["A", "B", "C", "D", "E"]], index=["A", "B"], columns=["C"]) Out[70]: D E C bar foo bar foo A B one A 1.120915 -0.514058 1.393057 -0.021605 B -0.338421 0.002759 0.684140 -0.551692 C -0.538846 0.699535 -0.988442 0.747859 three A -1.181568 NaN 0.961289 NaN B NaN 0.433512 NaN -1.064372 C 0.588783 NaN -0.131830 NaN two A NaN 1.000985 NaN 0.064245 B 0.158248 NaN -0.097147 NaN C NaN 0.176180 NaN 0.436241 Also, you can use Grouper for index and columns keywords. For detail of Grouper, see Grouping with a Grouper specification. In [71]: pd.pivot_table(df, values="D", index=pd.Grouper(freq="M", key="F"), columns="C") Out[71]: C bar foo F 2013-01-31 NaN -0.514058 2013-02-28 NaN 0.002759 2013-03-31 NaN 0.176180 2013-04-30 -1.181568 NaN 2013-05-31 -0.338421 NaN 2013-06-30 -0.538846 NaN 2013-07-31 NaN 1.000985 2013-08-31 NaN 0.433512 2013-09-30 NaN 0.699535 2013-10-31 1.120915 NaN 2013-11-30 0.158248 NaN 2013-12-31 0.588783 NaN You can render a nice output of the table omitting the missing values by calling to_string() if you wish: In [72]: table = pd.pivot_table(df, index=["A", "B"], columns=["C"], values=["D", "E"]) In [73]: print(table.to_string(na_rep="")) D E C bar foo bar foo A B one A 1.120915 -0.514058 1.393057 -0.021605 B -0.338421 0.002759 0.684140 -0.551692 C -0.538846 0.699535 -0.988442 0.747859 three A -1.181568 0.961289 B 0.433512 -1.064372 C 0.588783 -0.131830 two A 1.000985 0.064245 B 0.158248 -0.097147 C 0.176180 0.436241 Note that pivot_table() is also available as an instance method on DataFrame,i.e. DataFrame.pivot_table(). Adding margins# If you pass margins=True to pivot_table(), special All columns and rows will be added with partial group aggregates across the categories on the rows and columns: In [74]: table = df.pivot_table( ....: index=["A", "B"], ....: columns="C", ....: values=["D", "E"], ....: margins=True, ....: aggfunc=np.std ....: ) ....: In [75]: table Out[75]: D E C bar foo All bar foo All A B one A 1.804346 1.210272 1.569879 0.179483 0.418374 0.858005 B 0.690376 1.353355 0.898998 1.083825 0.968138 1.101401 C 0.273641 0.418926 0.771139 1.689271 0.446140 1.422136 three A 0.794212 NaN 0.794212 2.049040 NaN 2.049040 B NaN 0.363548 0.363548 NaN 1.625237 1.625237 C 3.915454 NaN 3.915454 1.035215 NaN 1.035215 two A NaN 0.442998 0.442998 NaN 0.447104 0.447104 B 0.202765 NaN 0.202765 0.560757 NaN 0.560757 C NaN 1.819408 1.819408 NaN 0.650439 0.650439 All 1.556686 0.952552 1.246608 1.250924 0.899904 1.059389 Additionally, you can call DataFrame.stack() to display a pivoted DataFrame as having a multi-level index: In [76]: table.stack() Out[76]: D E A B C one A All 1.569879 0.858005 bar 1.804346 0.179483 foo 1.210272 0.418374 B All 0.898998 1.101401 bar 0.690376 1.083825 ... ... ... two C All 1.819408 0.650439 foo 1.819408 0.650439 All All 1.246608 1.059389 bar 1.556686 1.250924 foo 0.952552 0.899904 [24 rows x 2 columns] Cross tabulations# Use crosstab() to compute a cross-tabulation of two (or more) factors. By default crosstab() computes a frequency table of the factors unless an array of values and an aggregation function are passed. It takes a number of arguments index: array-like, values to group by in the rows. columns: array-like, values to group by in the columns. values: array-like, optional, array of values to aggregate according to the factors. aggfunc: function, optional, If no values array is passed, computes a frequency table. rownames: sequence, default None, must match number of row arrays passed. colnames: sequence, default None, if passed, must match number of column arrays passed. margins: boolean, default False, Add row/column margins (subtotals) normalize: boolean, {‘all’, ‘index’, ‘columns’}, or {0,1}, default False. Normalize by dividing all values by the sum of values. Any Series passed will have their name attributes used unless row or column names for the cross-tabulation are specified For example: In [77]: foo, bar, dull, shiny, one, two = "foo", "bar", "dull", "shiny", "one", "two" In [78]: a = np.array([foo, foo, bar, bar, foo, foo], dtype=object) In [79]: b = np.array([one, one, two, one, two, one], dtype=object) In [80]: c = np.array([dull, dull, shiny, dull, dull, shiny], dtype=object) In [81]: pd.crosstab(a, [b, c], rownames=["a"], colnames=["b", "c"]) Out[81]: b one two c dull shiny dull shiny a bar 1 0 0 1 foo 2 1 1 0 If crosstab() receives only two Series, it will provide a frequency table. In [82]: df = pd.DataFrame( ....: {"A": [1, 2, 2, 2, 2], "B": [3, 3, 4, 4, 4], "C": [1, 1, np.nan, 1, 1]} ....: ) ....: In [83]: df Out[83]: A B C 0 1 3 1.0 1 2 3 1.0 2 2 4 NaN 3 2 4 1.0 4 2 4 1.0 In [84]: pd.crosstab(df["A"], df["B"]) Out[84]: B 3 4 A 1 1 0 2 1 3 crosstab() can also be implemented to Categorical data. In [85]: foo = pd.Categorical(["a", "b"], categories=["a", "b", "c"]) In [86]: bar = pd.Categorical(["d", "e"], categories=["d", "e", "f"]) In [87]: pd.crosstab(foo, bar) Out[87]: col_0 d e row_0 a 1 0 b 0 1 If you want to include all of data categories even if the actual data does not contain any instances of a particular category, you should set dropna=False. For example: In [88]: pd.crosstab(foo, bar, dropna=False) Out[88]: col_0 d e f row_0 a 1 0 0 b 0 1 0 c 0 0 0 Normalization# Frequency tables can also be normalized to show percentages rather than counts using the normalize argument: In [89]: pd.crosstab(df["A"], df["B"], normalize=True) Out[89]: B 3 4 A 1 0.2 0.0 2 0.2 0.6 normalize can also normalize values within each row or within each column: In [90]: pd.crosstab(df["A"], df["B"], normalize="columns") Out[90]: B 3 4 A 1 0.5 0.0 2 0.5 1.0 crosstab() can also be passed a third Series and an aggregation function (aggfunc) that will be applied to the values of the third Series within each group defined by the first two Series: In [91]: pd.crosstab(df["A"], df["B"], values=df["C"], aggfunc=np.sum) Out[91]: B 3 4 A 1 1.0 NaN 2 1.0 2.0 Adding margins# Finally, one can also add margins or normalize this output. In [92]: pd.crosstab( ....: df["A"], df["B"], values=df["C"], aggfunc=np.sum, normalize=True, margins=True ....: ) ....: Out[92]: B 3 4 All A 1 0.25 0.0 0.25 2 0.25 0.5 0.75 All 0.50 0.5 1.00 Tiling# The cut() function computes groupings for the values of the input array and is often used to transform continuous variables to discrete or categorical variables: In [93]: ages = np.array([10, 15, 13, 12, 23, 25, 28, 59, 60]) In [94]: pd.cut(ages, bins=3) Out[94]: [(9.95, 26.667], (9.95, 26.667], (9.95, 26.667], (9.95, 26.667], (9.95, 26.667], (9.95, 26.667], (26.667, 43.333], (43.333, 60.0], (43.333, 60.0]] Categories (3, interval[float64, right]): [(9.95, 26.667] < (26.667, 43.333] < (43.333, 60.0]] If the bins keyword is an integer, then equal-width bins are formed. Alternatively we can specify custom bin-edges: In [95]: c = pd.cut(ages, bins=[0, 18, 35, 70]) In [96]: c Out[96]: [(0, 18], (0, 18], (0, 18], (0, 18], (18, 35], (18, 35], (18, 35], (35, 70], (35, 70]] Categories (3, interval[int64, right]): [(0, 18] < (18, 35] < (35, 70]] If the bins keyword is an IntervalIndex, then these will be used to bin the passed data.: pd.cut([25, 20, 50], bins=c.categories) Computing indicator / dummy variables# To convert a categorical variable into a “dummy” or “indicator” DataFrame, for example a column in a DataFrame (a Series) which has k distinct values, can derive a DataFrame containing k columns of 1s and 0s using get_dummies(): In [97]: df = pd.DataFrame({"key": list("bbacab"), "data1": range(6)}) In [98]: pd.get_dummies(df["key"]) Out[98]: a b c 0 0 1 0 1 0 1 0 2 1 0 0 3 0 0 1 4 1 0 0 5 0 1 0 Sometimes it’s useful to prefix the column names, for example when merging the result with the original DataFrame: In [99]: dummies = pd.get_dummies(df["key"], prefix="key") In [100]: dummies Out[100]: key_a key_b key_c 0 0 1 0 1 0 1 0 2 1 0 0 3 0 0 1 4 1 0 0 5 0 1 0 In [101]: df[["data1"]].join(dummies) Out[101]: data1 key_a key_b key_c 0 0 0 1 0 1 1 0 1 0 2 2 1 0 0 3 3 0 0 1 4 4 1 0 0 5 5 0 1 0 This function is often used along with discretization functions like cut(): In [102]: values = np.random.randn(10) In [103]: values Out[103]: array([ 0.4082, -1.0481, -0.0257, -0.9884, 0.0941, 1.2627, 1.29 , 0.0824, -0.0558, 0.5366]) In [104]: bins = [0, 0.2, 0.4, 0.6, 0.8, 1] In [105]: pd.get_dummies(pd.cut(values, bins)) Out[105]: (0.0, 0.2] (0.2, 0.4] (0.4, 0.6] (0.6, 0.8] (0.8, 1.0] 0 0 0 1 0 0 1 0 0 0 0 0 2 0 0 0 0 0 3 0 0 0 0 0 4 1 0 0 0 0 5 0 0 0 0 0 6 0 0 0 0 0 7 1 0 0 0 0 8 0 0 0 0 0 9 0 0 1 0 0 See also Series.str.get_dummies. get_dummies() also accepts a DataFrame. By default all categorical variables (categorical in the statistical sense, those with object or categorical dtype) are encoded as dummy variables. In [106]: df = pd.DataFrame({"A": ["a", "b", "a"], "B": ["c", "c", "b"], "C": [1, 2, 3]}) In [107]: pd.get_dummies(df) Out[107]: C A_a A_b B_b B_c 0 1 1 0 0 1 1 2 0 1 0 1 2 3 1 0 1 0 All non-object columns are included untouched in the output. You can control the columns that are encoded with the columns keyword. In [108]: pd.get_dummies(df, columns=["A"]) Out[108]: B C A_a A_b 0 c 1 1 0 1 c 2 0 1 2 b 3 1 0 Notice that the B column is still included in the output, it just hasn’t been encoded. You can drop B before calling get_dummies if you don’t want to include it in the output. As with the Series version, you can pass values for the prefix and prefix_sep. By default the column name is used as the prefix, and _ as the prefix separator. You can specify prefix and prefix_sep in 3 ways: string: Use the same value for prefix or prefix_sep for each column to be encoded. list: Must be the same length as the number of columns being encoded. dict: Mapping column name to prefix. In [109]: simple = pd.get_dummies(df, prefix="new_prefix") In [110]: simple Out[110]: C new_prefix_a new_prefix_b new_prefix_b new_prefix_c 0 1 1 0 0 1 1 2 0 1 0 1 2 3 1 0 1 0 In [111]: from_list = pd.get_dummies(df, prefix=["from_A", "from_B"]) In [112]: from_list Out[112]: C from_A_a from_A_b from_B_b from_B_c 0 1 1 0 0 1 1 2 0 1 0 1 2 3 1 0 1 0 In [113]: from_dict = pd.get_dummies(df, prefix={"B": "from_B", "A": "from_A"}) In [114]: from_dict Out[114]: C from_A_a from_A_b from_B_b from_B_c 0 1 1 0 0 1 1 2 0 1 0 1 2 3 1 0 1 0 Sometimes it will be useful to only keep k-1 levels of a categorical variable to avoid collinearity when feeding the result to statistical models. You can switch to this mode by turn on drop_first. In [115]: s = pd.Series(list("abcaa")) In [116]: pd.get_dummies(s) Out[116]: a b c 0 1 0 0 1 0 1 0 2 0 0 1 3 1 0 0 4 1 0 0 In [117]: pd.get_dummies(s, drop_first=True) Out[117]: b c 0 0 0 1 1 0 2 0 1 3 0 0 4 0 0 When a column contains only one level, it will be omitted in the result. In [118]: df = pd.DataFrame({"A": list("aaaaa"), "B": list("ababc")}) In [119]: pd.get_dummies(df) Out[119]: A_a B_a B_b B_c 0 1 1 0 0 1 1 0 1 0 2 1 1 0 0 3 1 0 1 0 4 1 0 0 1 In [120]: pd.get_dummies(df, drop_first=True) Out[120]: B_b B_c 0 0 0 1 1 0 2 0 0 3 1 0 4 0 1 By default new columns will have np.uint8 dtype. To choose another dtype, use the dtype argument: In [121]: df = pd.DataFrame({"A": list("abc"), "B": [1.1, 2.2, 3.3]}) In [122]: pd.get_dummies(df, dtype=bool).dtypes Out[122]: B float64 A_a bool A_b bool A_c bool dtype: object New in version 1.5.0. To convert a “dummy” or “indicator” DataFrame, into a categorical DataFrame, for example k columns of a DataFrame containing 1s and 0s can derive a DataFrame which has k distinct values using from_dummies(): In [123]: df = pd.DataFrame({"prefix_a": [0, 1, 0], "prefix_b": [1, 0, 1]}) In [124]: df Out[124]: prefix_a prefix_b 0 0 1 1 1 0 2 0 1 In [125]: pd.from_dummies(df, sep="_") Out[125]: prefix 0 b 1 a 2 b Dummy coded data only requires k - 1 categories to be included, in this case the k th category is the default category, implied by not being assigned any of the other k - 1 categories, can be passed via default_category. In [126]: df = pd.DataFrame({"prefix_a": [0, 1, 0]}) In [127]: df Out[127]: prefix_a 0 0 1 1 2 0 In [128]: pd.from_dummies(df, sep="_", default_category="b") Out[128]: prefix 0 b 1 a 2 b Factorizing values# To encode 1-d values as an enumerated type use factorize(): In [129]: x = pd.Series(["A", "A", np.nan, "B", 3.14, np.inf]) In [130]: x Out[130]: 0 A 1 A 2 NaN 3 B 4 3.14 5 inf dtype: object In [131]: labels, uniques = pd.factorize(x) In [132]: labels Out[132]: array([ 0, 0, -1, 1, 2, 3]) In [133]: uniques Out[133]: Index(['A', 'B', 3.14, inf], dtype='object') Note that factorize() is similar to numpy.unique, but differs in its handling of NaN: Note The following numpy.unique will fail under Python 3 with a TypeError because of an ordering bug. See also here. In [134]: ser = pd.Series(['A', 'A', np.nan, 'B', 3.14, np.inf]) In [135]: pd.factorize(ser, sort=True) Out[135]: (array([ 2, 2, -1, 3, 0, 1]), Index([3.14, inf, 'A', 'B'], dtype='object')) In [136]: np.unique(ser, return_inverse=True)[::-1] --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[136], line 1 ----> 1 np.unique(ser, return_inverse=True)[::-1] File <__array_function__ internals>:180, in unique(*args, **kwargs) File ~/micromamba/envs/test/lib/python3.8/site-packages/numpy/lib/arraysetops.py:274, in unique(ar, return_index, return_inverse, return_counts, axis, equal_nan) 272 ar = np.asanyarray(ar) 273 if axis is None: --> 274 ret = _unique1d(ar, return_index, return_inverse, return_counts, 275 equal_nan=equal_nan) 276 return _unpack_tuple(ret) 278 # axis was specified and not None File ~/micromamba/envs/test/lib/python3.8/site-packages/numpy/lib/arraysetops.py:333, in _unique1d(ar, return_index, return_inverse, return_counts, equal_nan) 330 optional_indices = return_index or return_inverse 332 if optional_indices: --> 333 perm = ar.argsort(kind='mergesort' if return_index else 'quicksort') 334 aux = ar[perm] 335 else: TypeError: '<' not supported between instances of 'float' and 'str' Note If you just want to handle one column as a categorical variable (like R’s factor), you can use df["cat_col"] = pd.Categorical(df["col"]) or df["cat_col"] = df["col"].astype("category"). For full docs on Categorical, see the Categorical introduction and the API documentation. Examples# In this section, we will review frequently asked questions and examples. The column names and relevant column values are named to correspond with how this DataFrame will be pivoted in the answers below. In [137]: np.random.seed([3, 1415]) In [138]: n = 20 In [139]: cols = np.array(["key", "row", "item", "col"]) In [140]: df = cols + pd.DataFrame( .....: (np.random.randint(5, size=(n, 4)) // [2, 1, 2, 1]).astype(str) .....: ) .....: In [141]: df.columns = cols In [142]: df = df.join(pd.DataFrame(np.random.rand(n, 2).round(2)).add_prefix("val")) In [143]: df Out[143]: key row item col val0 val1 0 key0 row3 item1 col3 0.81 0.04 1 key1 row2 item1 col2 0.44 0.07 2 key1 row0 item1 col0 0.77 0.01 3 key0 row4 item0 col2 0.15 0.59 4 key1 row0 item2 col1 0.81 0.64 .. ... ... ... ... ... ... 15 key0 row3 item1 col1 0.31 0.23 16 key0 row0 item2 col3 0.86 0.01 17 key0 row4 item0 col3 0.64 0.21 18 key2 row2 item2 col0 0.13 0.45 19 key0 row2 item0 col4 0.37 0.70 [20 rows x 6 columns] Pivoting with single aggregations# Suppose we wanted to pivot df such that the col values are columns, row values are the index, and the mean of val0 are the values? In particular, the resulting DataFrame should look like: col col0 col1 col2 col3 col4 row row0 0.77 0.605 NaN 0.860 0.65 row2 0.13 NaN 0.395 0.500 0.25 row3 NaN 0.310 NaN 0.545 NaN row4 NaN 0.100 0.395 0.760 0.24 This solution uses pivot_table(). Also note that aggfunc='mean' is the default. It is included here to be explicit. In [144]: df.pivot_table(values="val0", index="row", columns="col", aggfunc="mean") Out[144]: col col0 col1 col2 col3 col4 row row0 0.77 0.605 NaN 0.860 0.65 row2 0.13 NaN 0.395 0.500 0.25 row3 NaN 0.310 NaN 0.545 NaN row4 NaN 0.100 0.395 0.760 0.24 Note that we can also replace the missing values by using the fill_value parameter. In [145]: df.pivot_table( .....: values="val0", .....: index="row", .....: columns="col", .....: aggfunc="mean", .....: fill_value=0, .....: ) .....: Out[145]: col col0 col1 col2 col3 col4 row row0 0.77 0.605 0.000 0.860 0.65 row2 0.13 0.000 0.395 0.500 0.25 row3 0.00 0.310 0.000 0.545 0.00 row4 0.00 0.100 0.395 0.760 0.24 Also note that we can pass in other aggregation functions as well. For example, we can also pass in sum. In [146]: df.pivot_table( .....: values="val0", .....: index="row", .....: columns="col", .....: aggfunc="sum", .....: fill_value=0, .....: ) .....: Out[146]: col col0 col1 col2 col3 col4 row row0 0.77 1.21 0.00 0.86 0.65 row2 0.13 0.00 0.79 0.50 0.50 row3 0.00 0.31 0.00 1.09 0.00 row4 0.00 0.10 0.79 1.52 0.24 Another aggregation we can do is calculate the frequency in which the columns and rows occur together a.k.a. “cross tabulation”. To do this, we can pass size to the aggfunc parameter. In [147]: df.pivot_table(index="row", columns="col", fill_value=0, aggfunc="size") Out[147]: col col0 col1 col2 col3 col4 row row0 1 2 0 1 1 row2 1 0 2 1 2 row3 0 1 0 2 0 row4 0 1 2 2 1 Pivoting with multiple aggregations# We can also perform multiple aggregations. For example, to perform both a sum and mean, we can pass in a list to the aggfunc argument. In [148]: df.pivot_table( .....: values="val0", .....: index="row", .....: columns="col", .....: aggfunc=["mean", "sum"], .....: ) .....: Out[148]: mean sum col col0 col1 col2 col3 col4 col0 col1 col2 col3 col4 row row0 0.77 0.605 NaN 0.860 0.65 0.77 1.21 NaN 0.86 0.65 row2 0.13 NaN 0.395 0.500 0.25 0.13 NaN 0.79 0.50 0.50 row3 NaN 0.310 NaN 0.545 NaN NaN 0.31 NaN 1.09 NaN row4 NaN 0.100 0.395 0.760 0.24 NaN 0.10 0.79 1.52 0.24 Note to aggregate over multiple value columns, we can pass in a list to the values parameter. In [149]: df.pivot_table( .....: values=["val0", "val1"], .....: index="row", .....: columns="col", .....: aggfunc=["mean"], .....: ) .....: Out[149]: mean val0 val1 col col0 col1 col2 col3 col4 col0 col1 col2 col3 col4 row row0 0.77 0.605 NaN 0.860 0.65 0.01 0.745 NaN 0.010 0.02 row2 0.13 NaN 0.395 0.500 0.25 0.45 NaN 0.34 0.440 0.79 row3 NaN 0.310 NaN 0.545 NaN NaN 0.230 NaN 0.075 NaN row4 NaN 0.100 0.395 0.760 0.24 NaN 0.070 0.42 0.300 0.46 Note to subdivide over multiple columns we can pass in a list to the columns parameter. In [150]: df.pivot_table( .....: values=["val0"], .....: index="row", .....: columns=["item", "col"], .....: aggfunc=["mean"], .....: ) .....: Out[150]: mean val0 item item0 item1 item2 col col2 col3 col4 col0 col1 col2 col3 col4 col0 col1 col3 col4 row row0 NaN NaN NaN 0.77 NaN NaN NaN NaN NaN 0.605 0.86 0.65 row2 0.35 NaN 0.37 NaN NaN 0.44 NaN NaN 0.13 NaN 0.50 0.13 row3 NaN NaN NaN NaN 0.31 NaN 0.81 NaN NaN NaN 0.28 NaN row4 0.15 0.64 NaN NaN 0.10 0.64 0.88 0.24 NaN NaN NaN NaN Exploding a list-like column# New in version 0.25.0. Sometimes the values in a column are list-like. In [151]: keys = ["panda1", "panda2", "panda3"] In [152]: values = [["eats", "shoots"], ["shoots", "leaves"], ["eats", "leaves"]] In [153]: df = pd.DataFrame({"keys": keys, "values": values}) In [154]: df Out[154]: keys values 0 panda1 [eats, shoots] 1 panda2 [shoots, leaves] 2 panda3 [eats, leaves] We can ‘explode’ the values column, transforming each list-like to a separate row, by using explode(). This will replicate the index values from the original row: In [155]: df["values"].explode() Out[155]: 0 eats 0 shoots 1 shoots 1 leaves 2 eats 2 leaves Name: values, dtype: object You can also explode the column in the DataFrame. In [156]: df.explode("values") Out[156]: keys values 0 panda1 eats 0 panda1 shoots 1 panda2 shoots 1 panda2 leaves 2 panda3 eats 2 panda3 leaves Series.explode() will replace empty lists with np.nan and preserve scalar entries. The dtype of the resulting Series is always object. In [157]: s = pd.Series([[1, 2, 3], "foo", [], ["a", "b"]]) In [158]: s Out[158]: 0 [1, 2, 3] 1 foo 2 [] 3 [a, b] dtype: object In [159]: s.explode() Out[159]: 0 1 0 2 0 3 1 foo 2 NaN 3 a 3 b dtype: object Here is a typical usecase. You have comma separated strings in a column and want to expand this. In [160]: df = pd.DataFrame([{"var1": "a,b,c", "var2": 1}, {"var1": "d,e,f", "var2": 2}]) In [161]: df Out[161]: var1 var2 0 a,b,c 1 1 d,e,f 2 Creating a long form DataFrame is now straightforward using explode and chained operations In [162]: df.assign(var1=df.var1.str.split(",")).explode("var1") Out[162]: var1 var2 0 a 1 0 b 1 0 c 1 1 d 2 1 e 2 1 f 2
100
927
How to invert a Boolean column (i.e True to False and vice versa) in a DataFrame? I have 3 columns in a Pandas DataFrame where I have to compare 2 columns. in simple terms.. result_column = not(column1) or (column2) I tried the following df['status'] = ~df['offline'] | df['online'] but the above line is resulting in the error. TypeError: bad operand type for unary ~: 'float' I searched around for solution and found that '~' is used for Series data structure. There isn't any example for the same for DataFrame. Appreciate your time.
Something in your dataframe is not the type of data you are expecting. I'm not sure what data is causing the error. Sometimes null values cause the error you are getting, in which case you can use ', na=False' to fix it. But in this case, I have no problem with floats (2.2) or nulls (np.nan) so I don't know what data would produce that error. See this toy example: row1list = [True, False] row2list = [True, True] row3list = [False, 2.2] row4list = [False, np.nan] df = pd.DataFrame([row1list, row2list, row3list, row4list], columns=['column1', 'column2']) df['result_column'] = ~df['column1'] | df['column2'] print(df) # column1 column2 result_column # 0 True False False # 1 True True True # 2 False 2.2 True # 3 False NaN True
61,354,755
Pandas - drop first column and add index
<p>Doing:</p> <pre><code># extract data df = pd.read_csv('https://covid.ourworldindata.org/data/ecdc/total_cases.csv') df.set_index('date', inplace=True) #transpose columns to rows df = df.T # swap all NAN with 0.0 df = df.fillna(0.0) </code></pre> <p>I have:</p> <pre><code>date 2020-04-20 2020-04-21 World 2355853.0 2431890.0 Afghanistan 996.0 1031.0 Albania 562.0 584.0 Algeria 2629.0 2718.0 </code></pre> <p>Now I wish to drop the first column create an index, ending up with:</p> <pre><code> 2020-04-20 2020-04-21 0 2355853.0 2431890.0 1 996.0 1031.0 2 562.0 584.0 3 2629.0 2718.0 </code></pre> <p>How?</p> <p>Edit:</p> <pre><code>date 2019-12-31 2020-01-01 2020-01-02 2020-01-03 2020-01-04 2020-01-05 \ 0 27.0 27.0 27.0 44.0 44.0 59.0 1 0.0 0.0 0.0 0.0 0.0 0.0 2 0.0 0.0 0.0 0.0 0.0 0.0 </code></pre>
61,354,816
"2020-04-21T23:16:13.487000"
1
null
1
131
python|pandas
<p>Try this:</p> <pre><code>df = df.reset_index().drop('date', axis=1) </code></pre> <p>OR</p> <pre><code>df = df.reset_index(drop=True) </code></pre>
"2020-04-21T23:21:47.140000"
1
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.set_index.html
pandas.DataFrame.set_index# pandas.DataFrame.set_index# DataFrame.set_index(keys, *, drop=True, append=False, inplace=False, verify_integrity=False)[source]# Set the DataFrame index using existing columns. Set the DataFrame index (row labels) using one or more existing columns or arrays (of the correct length). The index can replace the existing index or expand on it. Parameters keyslabel or array-like or list of labels/arraysThis parameter can be either a single column key, a single array of the same length as the calling DataFrame, or a list containing an arbitrary combination of column keys and arrays. Here, “array” encompasses Series, Index, np.ndarray, and instances of Iterator. Try this: df = df.reset_index().drop('date', axis=1) OR df = df.reset_index(drop=True) dropbool, default TrueDelete columns to be used as the new index. appendbool, default FalseWhether to append columns to existing index. inplacebool, default FalseWhether to modify the DataFrame rather than creating a new one. verify_integritybool, default FalseCheck the new index for duplicates. Otherwise defer the check until necessary. Setting to False will improve the performance of this method. Returns DataFrame or NoneChanged row labels or None if inplace=True. See also DataFrame.reset_indexOpposite of set_index. DataFrame.reindexChange to new indices or expand indices. DataFrame.reindex_likeChange to same indices as other DataFrame. Examples >>> df = pd.DataFrame({'month': [1, 4, 7, 10], ... 'year': [2012, 2014, 2013, 2014], ... 'sale': [55, 40, 84, 31]}) >>> df month year sale 0 1 2012 55 1 4 2014 40 2 7 2013 84 3 10 2014 31 Set the index to become the ‘month’ column: >>> df.set_index('month') year sale month 1 2012 55 4 2014 40 7 2013 84 10 2014 31 Create a MultiIndex using columns ‘year’ and ‘month’: >>> df.set_index(['year', 'month']) sale year month 2012 1 55 2014 4 40 2013 7 84 2014 10 31 Create a MultiIndex using an Index and a column: >>> df.set_index([pd.Index([1, 2, 3, 4]), 'year']) month sale year 1 2012 1 55 2 2014 4 40 3 2013 7 84 4 2014 10 31 Create a MultiIndex using two Series: >>> s = pd.Series([1, 2, 3, 4]) >>> df.set_index([s, s**2]) month year sale 1 1 1 2012 55 2 4 4 2014 40 3 9 7 2013 84 4 16 10 2014 31
699
787
Pandas - drop first column and add index Doing: # extract data df = pd.read_csv('https://covid.ourworldindata.org/data/ecdc/total_cases.csv') df.set_index('date', inplace=True) #transpose columns to rows df = df.T # swap all NAN with 0.0 df = df.fillna(0.0) I have: date 2020-04-20 2020-04-21 World 2355853.0 2431890.0 Afghanistan 996.0 1031.0 Albania 562.0 584.0 Algeria 2629.0 2718.0 Now I wish to drop the first column create an index, ending up with: 2020-04-20 2020-04-21 0 2355853.0 2431890.0 1 996.0 1031.0 2 562.0 584.0 3 2629.0 2718.0 How? Edit: date 2019-12-31 2020-01-01 2020-01-02 2020-01-03 2020-01-04 2020-01-05 \ 0 27.0 27.0 27.0 44.0 44.0 59.0 1 0.0 0.0 0.0 0.0 0.0 0.0 2 0.0 0.0 0.0 0.0 0.0 0.0
Try this: df = df.reset_index().drop('date', axis=1) OR df = df.reset_index(drop=True)
65,435,236
Pandas - split column with dictionary into two columns with key and value
<p>This is my column:</p> <pre><code>transcript[&quot;value&quot;][1:4] 1 {'offer id': '0b1e1539f2cc45b7b9fa7c272da2e1d7'} 2 {'offer id': '2906b810c7d4411798c6938adc9daaa5'} 3 {'offer id': 'fafdcd668e3743c1bb461111dcafc2a4'} </code></pre> <p>What I try to achieve is this:</p> <pre><code> type offer_id 0 offer_id 0b1e1539f2cc45b7b9fa7c272da2e1d7 1 offer_id 2906b810c7d4411798c6938adc9daaa5 2 offer_id fafdcd668e3743c1bb461111dcafc2a4 </code></pre> <p>I tried to convert it into an <code>str</code> and then split it, this this seems error prone and actually did not work at all:</p> <pre><code>transcript[&quot;value&quot;].str.split(&quot;:&quot;) </code></pre> <p>Does anyone know how to achieve this? Preferably something that could handle multiple dictionaries in one column?</p>
65,435,308
"2020-12-24T07:25:45.567000"
3
1
1
976
python|pandas
<p>You could do:</p> <pre><code>import pandas as pd transcript = pd.DataFrame([ [{'offer_id': '0b1e1539f2cc45b7b9fa7c272da2e1d7'}], [{'offer_id': '2906b810c7d4411798c6938adc9daaa5'}], [{'offer_id': 'fafdcd668e3743c1bb461111dcafc2a4'}] ], columns=['value']) res = pd.DataFrame([{'type' : key, 'offer_id' : value } for d in transcript['value'].tolist() for key, value in d.items()]) print(res) </code></pre> <p><strong>Output</strong></p> <pre><code> type offer_id 0 offer_id 0b1e1539f2cc45b7b9fa7c272da2e1d7 1 offer_id 2906b810c7d4411798c6938adc9daaa5 2 offer_id fafdcd668e3743c1bb461111dcafc2a4 </code></pre>
"2020-12-24T07:32:58.653000"
1
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_dict.html
pandas.DataFrame.to_dict# pandas.DataFrame.to_dict# DataFrame.to_dict(orient='dict', into=<class 'dict'>)[source]# Convert the DataFrame to a dictionary. The type of the key-value pairs can be customized with the parameters (see below). Parameters orientstr {‘dict’, ‘list’, ‘series’, ‘split’, ‘tight’, ‘records’, ‘index’}Determines the type of the values of the dictionary. ‘dict’ (default) : dict like {column -> {index -> value}} ‘list’ : dict like {column -> [values]} ‘series’ : dict like {column -> Series(values)} You could do: import pandas as pd transcript = pd.DataFrame([ [{'offer_id': '0b1e1539f2cc45b7b9fa7c272da2e1d7'}], [{'offer_id': '2906b810c7d4411798c6938adc9daaa5'}], [{'offer_id': 'fafdcd668e3743c1bb461111dcafc2a4'}] ], columns=['value']) res = pd.DataFrame([{'type' : key, 'offer_id' : value } for d in transcript['value'].tolist() for key, value in d.items()]) print(res) Output type offer_id 0 offer_id 0b1e1539f2cc45b7b9fa7c272da2e1d7 1 offer_id 2906b810c7d4411798c6938adc9daaa5 2 offer_id fafdcd668e3743c1bb461111dcafc2a4 ‘split’ : dict like {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values]} ‘tight’ : dict like {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values], ‘index_names’ -> [index.names], ‘column_names’ -> [column.names]} ‘records’ : list like [{column -> value}, … , {column -> value}] ‘index’ : dict like {index -> {column -> value}} Abbreviations are allowed. s indicates series and sp indicates split. New in version 1.4.0: ‘tight’ as an allowed value for the orient argument intoclass, default dictThe collections.abc.Mapping subclass used for all Mappings in the return value. Can be the actual class or an empty instance of the mapping type you want. If you want a collections.defaultdict, you must pass it initialized. Returns dict, list or collections.abc.MappingReturn a collections.abc.Mapping object representing the DataFrame. The resulting transformation depends on the orient parameter. See also DataFrame.from_dictCreate a DataFrame from a dictionary. DataFrame.to_jsonConvert a DataFrame to JSON format. Examples >>> df = pd.DataFrame({'col1': [1, 2], ... 'col2': [0.5, 0.75]}, ... index=['row1', 'row2']) >>> df col1 col2 row1 1 0.50 row2 2 0.75 >>> df.to_dict() {'col1': {'row1': 1, 'row2': 2}, 'col2': {'row1': 0.5, 'row2': 0.75}} You can specify the return orientation. >>> df.to_dict('series') {'col1': row1 1 row2 2 Name: col1, dtype: int64, 'col2': row1 0.50 row2 0.75 Name: col2, dtype: float64} >>> df.to_dict('split') {'index': ['row1', 'row2'], 'columns': ['col1', 'col2'], 'data': [[1, 0.5], [2, 0.75]]} >>> df.to_dict('records') [{'col1': 1, 'col2': 0.5}, {'col1': 2, 'col2': 0.75}] >>> df.to_dict('index') {'row1': {'col1': 1, 'col2': 0.5}, 'row2': {'col1': 2, 'col2': 0.75}} >>> df.to_dict('tight') {'index': ['row1', 'row2'], 'columns': ['col1', 'col2'], 'data': [[1, 0.5], [2, 0.75]], 'index_names': [None], 'column_names': [None]} You can also specify the mapping type. >>> from collections import OrderedDict, defaultdict >>> df.to_dict(into=OrderedDict) OrderedDict([('col1', OrderedDict([('row1', 1), ('row2', 2)])), ('col2', OrderedDict([('row1', 0.5), ('row2', 0.75)]))]) If you want a defaultdict, you need to initialize it: >>> dd = defaultdict(list) >>> df.to_dict('records', into=dd) [defaultdict(<class 'list'>, {'col1': 1, 'col2': 0.5}), defaultdict(<class 'list'>, {'col1': 2, 'col2': 0.75})]
528
1,109
Pandas - split column with dictionary into two columns with key and value This is my column: transcript["value"][1:4] 1 {'offer id': '0b1e1539f2cc45b7b9fa7c272da2e1d7'} 2 {'offer id': '2906b810c7d4411798c6938adc9daaa5'} 3 {'offer id': 'fafdcd668e3743c1bb461111dcafc2a4'} What I try to achieve is this: type offer_id 0 offer_id 0b1e1539f2cc45b7b9fa7c272da2e1d7 1 offer_id 2906b810c7d4411798c6938adc9daaa5 2 offer_id fafdcd668e3743c1bb461111dcafc2a4 I tried to convert it into an str and then split it, this this seems error prone and actually did not work at all: transcript["value"].str.split(":") Does anyone know how to achieve this? Preferably something that could handle multiple dictionaries in one column?
You could do: import pandas as pd transcript = pd.DataFrame([ [{'offer_id': '0b1e1539f2cc45b7b9fa7c272da2e1d7'}], [{'offer_id': '2906b810c7d4411798c6938adc9daaa5'}], [{'offer_id': 'fafdcd668e3743c1bb461111dcafc2a4'}] ], columns=['value']) res = pd.DataFrame([{'type' : key, 'offer_id' : value } for d in transcript['value'].tolist() for key, value in d.items()]) print(res) Output type offer_id 0 offer_id 0b1e1539f2cc45b7b9fa7c272da2e1d7 1 offer_id 2906b810c7d4411798c6938adc9daaa5 2 offer_id fafdcd668e3743c1bb461111dcafc2a4
62,735,314
Looking for words in a subset of a dataframe
<p>I have this dataset:</p> <pre><code> Word Date paper pen 03/02/2020 pen 03/02/2020 salt 03/03/2020 Batch traditional loaf 03/04/2020 Hi-fibre 03/08/2020 The pen is on the table 03/11/2020 I went to the gym 03/10/2020 </code></pre> <p>and its subset</p> <pre><code> Num Date 03/02/2020 43 03/02/2020 03/03/2020 12 03/03/2020 03/16/2020 32 03/16/2020 03/08/2020 42 03/08/2020 03/10/2020 21 03/10/2020 </code></pre> <p>I would like to create a function which loops over <code>Date</code> in the subset in order to extract rows which have <code>Word</code> values containing <code>pen</code> in the original dataset.</p> <p>To look for strings I using this:</p> <pre><code>df[df[['Date','Word']].apply(lambda x : x.str.contains('pen'))] </code></pre> <p>where <code>df</code> is the original dataset. However I do not know how to loop over Date in the subset (<code>sub</code>) to get rows containing <code>pen</code> in <code>df</code>.</p> <p>My expected output, looking for the dates below in df:</p> <pre><code> 03/02/2020 03/03/2020 03/16/2020 03/08/2020 03/10/2020 </code></pre> <p>would be</p> <pre><code> Word Date paper pen 03/02/2020 pen 03/02/2020 </code></pre>
62,735,619
"2020-07-04T23:06:41.197000"
3
null
-2
233
python|pandas
<ul> <li>Check if <code>word</code> contains <code>pen</code> and check if <code>Date</code> <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.isin.html" rel="nofollow noreferrer">isin</a> the list of <code>Date</code> from <code>subset</code>.</li> <li><a href="https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#indexing-and-selecting-data" rel="nofollow noreferrer">Pandas: Indexing and selecting data</a></li> </ul> <pre class="lang-py prettyprint-override"><code>df[df.word.str.contains('pen') &amp; (df.Date.isin(subset.Date.unique().tolist()))] </code></pre>
"2020-07-05T00:05:25.527000"
1
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.query.html
pandas.DataFrame.query# pandas.DataFrame.query# DataFrame.query(expr, *, inplace=False, **kwargs)[source]# Query the columns of a DataFrame with a boolean expression. Parameters exprstrThe query string to evaluate. You can refer to variables in the environment by prefixing them with an ‘@’ character like @a + b. You can refer to column names that are not valid Python variable names by surrounding them in backticks. Thus, column names containing spaces or punctuations (besides underscores) or starting with digits must be surrounded by backticks. (For example, a column named “Area (cm^2)” would be referenced as `Area (cm^2)`). Column names which are Python keywords (like “list”, “for”, “import”, etc) cannot be used. For example, if one of your columns is called a a and you want Check if word contains pen and check if Date isin the list of Date from subset. Pandas: Indexing and selecting data df[df.word.str.contains('pen') & (df.Date.isin(subset.Date.unique().tolist()))] to sum it with b, your query should be `a a` + b. New in version 0.25.0: Backtick quoting introduced. New in version 1.0.0: Expanding functionality of backtick quoting for more than only spaces. inplaceboolWhether to modify the DataFrame rather than creating a new one. **kwargsSee the documentation for eval() for complete details on the keyword arguments accepted by DataFrame.query(). Returns DataFrame or NoneDataFrame resulting from the provided query expression or None if inplace=True. See also evalEvaluate a string describing operations on DataFrame columns. DataFrame.evalEvaluate a string describing operations on DataFrame columns. Notes The result of the evaluation of this expression is first passed to DataFrame.loc and if that fails because of a multidimensional key (e.g., a DataFrame) then the result will be passed to DataFrame.__getitem__(). This method uses the top-level eval() function to evaluate the passed query. The query() method uses a slightly modified Python syntax by default. For example, the & and | (bitwise) operators have the precedence of their boolean cousins, and and or. This is syntactically valid Python, however the semantics are different. You can change the semantics of the expression by passing the keyword argument parser='python'. This enforces the same semantics as evaluation in Python space. Likewise, you can pass engine='python' to evaluate an expression using Python itself as a backend. This is not recommended as it is inefficient compared to using numexpr as the engine. The DataFrame.index and DataFrame.columns attributes of the DataFrame instance are placed in the query namespace by default, which allows you to treat both the index and columns of the frame as a column in the frame. The identifier index is used for the frame index; you can also use the name of the index to identify it in a query. Please note that Python keywords may not be used as identifiers. For further details and examples see the query documentation in indexing. Backtick quoted variables Backtick quoted variables are parsed as literal Python code and are converted internally to a Python valid identifier. This can lead to the following problems. During parsing a number of disallowed characters inside the backtick quoted string are replaced by strings that are allowed as a Python identifier. These characters include all operators in Python, the space character, the question mark, the exclamation mark, the dollar sign, and the euro sign. For other characters that fall outside the ASCII range (U+0001..U+007F) and those that are not further specified in PEP 3131, the query parser will raise an error. This excludes whitespace different than the space character, but also the hashtag (as it is used for comments) and the backtick itself (backtick can also not be escaped). In a special case, quotes that make a pair around a backtick can confuse the parser. For example, `it's` > `that's` will raise an error, as it forms a quoted string ('s > `that') with a backtick inside. See also the Python documentation about lexical analysis (https://docs.python.org/3/reference/lexical_analysis.html) in combination with the source code in pandas.core.computation.parsing. Examples >>> df = pd.DataFrame({'A': range(1, 6), ... 'B': range(10, 0, -2), ... 'C C': range(10, 5, -1)}) >>> df A B C C 0 1 10 10 1 2 8 9 2 3 6 8 3 4 4 7 4 5 2 6 >>> df.query('A > B') A B C C 4 5 2 6 The previous expression is equivalent to >>> df[df.A > df.B] A B C C 4 5 2 6 For columns with spaces in their name, you can use backtick quoting. >>> df.query('B == `C C`') A B C C 0 1 10 10 The previous expression is equivalent to >>> df[df.B == df['C C']] A B C C 0 1 10 10
793
991
Looking for words in a subset of a dataframe I have this dataset: Word Date paper pen 03/02/2020 pen 03/02/2020 salt 03/03/2020 Batch traditional loaf 03/04/2020 Hi-fibre 03/08/2020 The pen is on the table 03/11/2020 I went to the gym 03/10/2020 and its subset Num Date 03/02/2020 43 03/02/2020 03/03/2020 12 03/03/2020 03/16/2020 32 03/16/2020 03/08/2020 42 03/08/2020 03/10/2020 21 03/10/2020 I would like to create a function which loops over Date in the subset in order to extract rows which have Word values containing pen in the original dataset. To look for strings I using this: df[df[['Date','Word']].apply(lambda x : x.str.contains('pen'))] where df is the original dataset. However I do not know how to loop over Date in the subset (sub) to get rows containing pen in df. My expected output, looking for the dates below in df: 03/02/2020 03/03/2020 03/16/2020 03/08/2020 03/10/2020 would be Word Date paper pen 03/02/2020 pen 03/02/2020
Check if word contains pen and check if Date isin the list of Date from subset. Pandas: Indexing and selecting data df[df.word.str.contains('pen') & (df.Date.isin(subset.Date.unique().tolist()))]
69,495,830
Recognising Missing Values compared to previous days and creating new dataframe, Pandas
<pre><code>df = pd.DataFrame({'Date': {0:'2020-04-01', 1:'2020-04-01', 2:'2020-04-01', 3:'2020-04-02', 4:'2020-04-02', 5:'2020-04-03', 6:'2020-04-03', 7:'2020-04-03'}, 'Name': {0:'AA', 1:'BB', 2:'CC', 3:'AA', 4:'BB', 5:'AA', 6:'BB', 7:'CC'}, 'Weight':{0:1, 1:3, 2:6, 3:1, 4:4, 5:2, 6:5, 7:7}}) df=df.set_index(['Date']) </code></pre> <p>For each unique date in df.index - I would like to compare the Name columns to those present for the previous date, and if a name is missing, add a value of 0 to the weight column</p> <p>For example..How would I recognise that 2020-04-02 was missing a row for 'CC' and insert a weight of 0 for this row.</p> <p>Thereby achieving a DataFrame that looks like this...</p> <div class="s-table-container"> <table class="s-table"> <thead> <tr> <th></th> <th>Name</th> <th>Weight</th> </tr> </thead> <tbody> <tr> <td>2020-04-01</td> <td>AA</td> <td>1</td> </tr> <tr> <td>2020-04-01</td> <td>BB</td> <td>3</td> </tr> <tr> <td>2020-04-01</td> <td>CC</td> <td>6</td> </tr> <tr> <td>2020-04-02</td> <td>AA</td> <td>1</td> </tr> <tr> <td>2020-04-02</td> <td>BB</td> <td>4</td> </tr> <tr> <td>2020-04-02</td> <td>CC</td> <td>0</td> </tr> <tr> <td>2020-04-03</td> <td>AA</td> <td>2</td> </tr> <tr> <td>2020-04-03</td> <td>BB</td> <td>5</td> </tr> <tr> <td>2020-04-03</td> <td>CC</td> <td>7</td> </tr> </tbody> </table> </div>
69,496,006
"2021-10-08T12:26:20.123000"
1
null
2
27
python|pandas
<p>You can add <code>Name</code> to <code>index</code>, then reshape by <a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.unstack.html" rel="nofollow noreferrer"><code>DataFrame.unstack</code></a> with add <code>0</code> with <a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.stack.html" rel="nofollow noreferrer"><code>DataFrame.stack</code></a> for original format of <code>DataFrame</code>:</p> <pre><code>df = df.set_index('Name', append=True).unstack(fill_value=0).stack().reset_index(level=1) print (df) Name Weight Date 2020-04-01 AA 1 2020-04-01 BB 3 2020-04-01 CC 6 2020-04-02 AA 1 2020-04-02 BB 4 2020-04-02 CC 0 2020-04-03 AA 2 2020-04-03 BB 5 2020-04-03 CC 7 </code></pre>
"2021-10-08T12:41:02.837000"
2
https://pandas.pydata.org/docs/dev/user_guide/merging.html
Merge, join, concatenate and compare# Merge, join, concatenate and compare# pandas provides various facilities for easily combining together Series or DataFrame with various kinds of set logic for the indexes and relational algebra functionality in the case of join / merge-type operations. In addition, pandas also provides utilities to compare two Series or DataFrame and summarize their differences. Concatenating objects# The concat() function (in the main pandas namespace) does all of the heavy lifting of performing concatenation operations along an axis while performing optional set logic (union or intersection) of the indexes (if any) on You can add Name to index, then reshape by DataFrame.unstack with add 0 with DataFrame.stack for original format of DataFrame: df = df.set_index('Name', append=True).unstack(fill_value=0).stack().reset_index(level=1) print (df) Name Weight Date 2020-04-01 AA 1 2020-04-01 BB 3 2020-04-01 CC 6 2020-04-02 AA 1 2020-04-02 BB 4 2020-04-02 CC 0 2020-04-03 AA 2 2020-04-03 BB 5 2020-04-03 CC 7 the other axes. Note that I say “if any” because there is only a single possible axis of concatenation for Series. Before diving into all of the details of concat and what it can do, here is a simple example: In [1]: df1 = pd.DataFrame( ...: { ...: "A": ["A0", "A1", "A2", "A3"], ...: "B": ["B0", "B1", "B2", "B3"], ...: "C": ["C0", "C1", "C2", "C3"], ...: "D": ["D0", "D1", "D2", "D3"], ...: }, ...: index=[0, 1, 2, 3], ...: ) ...: In [2]: df2 = pd.DataFrame( ...: { ...: "A": ["A4", "A5", "A6", "A7"], ...: "B": ["B4", "B5", "B6", "B7"], ...: "C": ["C4", "C5", "C6", "C7"], ...: "D": ["D4", "D5", "D6", "D7"], ...: }, ...: index=[4, 5, 6, 7], ...: ) ...: In [3]: df3 = pd.DataFrame( ...: { ...: "A": ["A8", "A9", "A10", "A11"], ...: "B": ["B8", "B9", "B10", "B11"], ...: "C": ["C8", "C9", "C10", "C11"], ...: "D": ["D8", "D9", "D10", "D11"], ...: }, ...: index=[8, 9, 10, 11], ...: ) ...: In [4]: frames = [df1, df2, df3] In [5]: result = pd.concat(frames) Like its sibling function on ndarrays, numpy.concatenate, pandas.concat takes a list or dict of homogeneously-typed objects and concatenates them with some configurable handling of “what to do with the other axes”: pd.concat( objs, axis=0, join="outer", ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, copy=True, ) objs : a sequence or mapping of Series or DataFrame objects. If a dict is passed, the sorted keys will be used as the keys argument, unless it is passed, in which case the values will be selected (see below). Any None objects will be dropped silently unless they are all None in which case a ValueError will be raised. axis : {0, 1, …}, default 0. The axis to concatenate along. join : {‘inner’, ‘outer’}, default ‘outer’. How to handle indexes on other axis(es). Outer for union and inner for intersection. ignore_index : boolean, default False. If True, do not use the index values on the concatenation axis. The resulting axis will be labeled 0, …, n - 1. This is useful if you are concatenating objects where the concatenation axis does not have meaningful indexing information. Note the index values on the other axes are still respected in the join. keys : sequence, default None. Construct hierarchical index using the passed keys as the outermost level. If multiple levels passed, should contain tuples. levels : list of sequences, default None. Specific levels (unique values) to use for constructing a MultiIndex. Otherwise they will be inferred from the keys. names : list, default None. Names for the levels in the resulting hierarchical index. verify_integrity : boolean, default False. Check whether the new concatenated axis contains duplicates. This can be very expensive relative to the actual data concatenation. copy : boolean, default True. If False, do not copy data unnecessarily. Without a little bit of context many of these arguments don’t make much sense. Let’s revisit the above example. Suppose we wanted to associate specific keys with each of the pieces of the chopped up DataFrame. We can do this using the keys argument: In [6]: result = pd.concat(frames, keys=["x", "y", "z"]) As you can see (if you’ve read the rest of the documentation), the resulting object’s index has a hierarchical index. This means that we can now select out each chunk by key: In [7]: result.loc["y"] Out[7]: A B C D 4 A4 B4 C4 D4 5 A5 B5 C5 D5 6 A6 B6 C6 D6 7 A7 B7 C7 D7 It’s not a stretch to see how this can be very useful. More detail on this functionality below. Note It is worth noting that concat() makes a full copy of the data, and that constantly reusing this function can create a significant performance hit. If you need to use the operation over several datasets, use a list comprehension. frames = [ process_your_file(f) for f in files ] result = pd.concat(frames) Note When concatenating DataFrames with named axes, pandas will attempt to preserve these index/column names whenever possible. In the case where all inputs share a common name, this name will be assigned to the result. When the input names do not all agree, the result will be unnamed. The same is true for MultiIndex, but the logic is applied separately on a level-by-level basis. Set logic on the other axes# When gluing together multiple DataFrames, you have a choice of how to handle the other axes (other than the one being concatenated). This can be done in the following two ways: Take the union of them all, join='outer'. This is the default option as it results in zero information loss. Take the intersection, join='inner'. Here is an example of each of these methods. First, the default join='outer' behavior: In [8]: df4 = pd.DataFrame( ...: { ...: "B": ["B2", "B3", "B6", "B7"], ...: "D": ["D2", "D3", "D6", "D7"], ...: "F": ["F2", "F3", "F6", "F7"], ...: }, ...: index=[2, 3, 6, 7], ...: ) ...: In [9]: result = pd.concat([df1, df4], axis=1) Here is the same thing with join='inner': In [10]: result = pd.concat([df1, df4], axis=1, join="inner") Lastly, suppose we just wanted to reuse the exact index from the original DataFrame: In [11]: result = pd.concat([df1, df4], axis=1).reindex(df1.index) Similarly, we could index before the concatenation: In [12]: pd.concat([df1, df4.reindex(df1.index)], axis=1) Out[12]: A B C D B D F 0 A0 B0 C0 D0 NaN NaN NaN 1 A1 B1 C1 D1 NaN NaN NaN 2 A2 B2 C2 D2 B2 D2 F2 3 A3 B3 C3 D3 B3 D3 F3 Ignoring indexes on the concatenation axis# For DataFrame objects which don’t have a meaningful index, you may wish to append them and ignore the fact that they may have overlapping indexes. To do this, use the ignore_index argument: In [13]: result = pd.concat([df1, df4], ignore_index=True, sort=False) Concatenating with mixed ndims# You can concatenate a mix of Series and DataFrame objects. The Series will be transformed to DataFrame with the column name as the name of the Series. In [14]: s1 = pd.Series(["X0", "X1", "X2", "X3"], name="X") In [15]: result = pd.concat([df1, s1], axis=1) Note Since we’re concatenating a Series to a DataFrame, we could have achieved the same result with DataFrame.assign(). To concatenate an arbitrary number of pandas objects (DataFrame or Series), use concat. If unnamed Series are passed they will be numbered consecutively. In [16]: s2 = pd.Series(["_0", "_1", "_2", "_3"]) In [17]: result = pd.concat([df1, s2, s2, s2], axis=1) Passing ignore_index=True will drop all name references. In [18]: result = pd.concat([df1, s1], axis=1, ignore_index=True) More concatenating with group keys# A fairly common use of the keys argument is to override the column names when creating a new DataFrame based on existing Series. Notice how the default behaviour consists on letting the resulting DataFrame inherit the parent Series’ name, when these existed. In [19]: s3 = pd.Series([0, 1, 2, 3], name="foo") In [20]: s4 = pd.Series([0, 1, 2, 3]) In [21]: s5 = pd.Series([0, 1, 4, 5]) In [22]: pd.concat([s3, s4, s5], axis=1) Out[22]: foo 0 1 0 0 0 0 1 1 1 1 2 2 2 4 3 3 3 5 Through the keys argument we can override the existing column names. In [23]: pd.concat([s3, s4, s5], axis=1, keys=["red", "blue", "yellow"]) Out[23]: red blue yellow 0 0 0 0 1 1 1 1 2 2 2 4 3 3 3 5 Let’s consider a variation of the very first example presented: In [24]: result = pd.concat(frames, keys=["x", "y", "z"]) You can also pass a dict to concat in which case the dict keys will be used for the keys argument (unless other keys are specified): In [25]: pieces = {"x": df1, "y": df2, "z": df3} In [26]: result = pd.concat(pieces) In [27]: result = pd.concat(pieces, keys=["z", "y"]) The MultiIndex created has levels that are constructed from the passed keys and the index of the DataFrame pieces: In [28]: result.index.levels Out[28]: FrozenList([['z', 'y'], [4, 5, 6, 7, 8, 9, 10, 11]]) If you wish to specify other levels (as will occasionally be the case), you can do so using the levels argument: In [29]: result = pd.concat( ....: pieces, keys=["x", "y", "z"], levels=[["z", "y", "x", "w"]], names=["group_key"] ....: ) ....: In [30]: result.index.levels Out[30]: FrozenList([['z', 'y', 'x', 'w'], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]) This is fairly esoteric, but it is actually necessary for implementing things like GroupBy where the order of a categorical variable is meaningful. Appending rows to a DataFrame# If you have a series that you want to append as a single row to a DataFrame, you can convert the row into a DataFrame and use concat In [31]: s2 = pd.Series(["X0", "X1", "X2", "X3"], index=["A", "B", "C", "D"]) In [32]: result = pd.concat([df1, s2.to_frame().T], ignore_index=True) You should use ignore_index with this method to instruct DataFrame to discard its index. If you wish to preserve the index, you should construct an appropriately-indexed DataFrame and append or concatenate those objects. Database-style DataFrame or named Series joining/merging# pandas has full-featured, high performance in-memory join operations idiomatically very similar to relational databases like SQL. These methods perform significantly better (in some cases well over an order of magnitude better) than other open source implementations (like base::merge.data.frame in R). The reason for this is careful algorithmic design and the internal layout of the data in DataFrame. See the cookbook for some advanced strategies. Users who are familiar with SQL but new to pandas might be interested in a comparison with SQL. pandas provides a single function, merge(), as the entry point for all standard database join operations between DataFrame or named Series objects: pd.merge( left, right, how="inner", on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=True, suffixes=("_x", "_y"), copy=True, indicator=False, validate=None, ) left: A DataFrame or named Series object. right: Another DataFrame or named Series object. on: Column or index level names to join on. Must be found in both the left and right DataFrame and/or Series objects. If not passed and left_index and right_index are False, the intersection of the columns in the DataFrames and/or Series will be inferred to be the join keys. left_on: Columns or index levels from the left DataFrame or Series to use as keys. Can either be column names, index level names, or arrays with length equal to the length of the DataFrame or Series. right_on: Columns or index levels from the right DataFrame or Series to use as keys. Can either be column names, index level names, or arrays with length equal to the length of the DataFrame or Series. left_index: If True, use the index (row labels) from the left DataFrame or Series as its join key(s). In the case of a DataFrame or Series with a MultiIndex (hierarchical), the number of levels must match the number of join keys from the right DataFrame or Series. right_index: Same usage as left_index for the right DataFrame or Series how: One of 'left', 'right', 'outer', 'inner', 'cross'. Defaults to inner. See below for more detailed description of each method. sort: Sort the result DataFrame by the join keys in lexicographical order. Defaults to True, setting to False will improve performance substantially in many cases. suffixes: A tuple of string suffixes to apply to overlapping columns. Defaults to ('_x', '_y'). copy: Always copy data (default True) from the passed DataFrame or named Series objects, even when reindexing is not necessary. Cannot be avoided in many cases but may improve performance / memory usage. The cases where copying can be avoided are somewhat pathological but this option is provided nonetheless. indicator: Add a column to the output DataFrame called _merge with information on the source of each row. _merge is Categorical-type and takes on a value of left_only for observations whose merge key only appears in 'left' DataFrame or Series, right_only for observations whose merge key only appears in 'right' DataFrame or Series, and both if the observation’s merge key is found in both. validate : string, default None. If specified, checks if merge is of specified type. “one_to_one” or “1:1”: checks if merge keys are unique in both left and right datasets. “one_to_many” or “1:m”: checks if merge keys are unique in left dataset. “many_to_one” or “m:1”: checks if merge keys are unique in right dataset. “many_to_many” or “m:m”: allowed, but does not result in checks. Note Support for specifying index levels as the on, left_on, and right_on parameters was added in version 0.23.0. Support for merging named Series objects was added in version 0.24.0. The return type will be the same as left. If left is a DataFrame or named Series and right is a subclass of DataFrame, the return type will still be DataFrame. merge is a function in the pandas namespace, and it is also available as a DataFrame instance method merge(), with the calling DataFrame being implicitly considered the left object in the join. The related join() method, uses merge internally for the index-on-index (by default) and column(s)-on-index join. If you are joining on index only, you may wish to use DataFrame.join to save yourself some typing. Brief primer on merge methods (relational algebra)# Experienced users of relational databases like SQL will be familiar with the terminology used to describe join operations between two SQL-table like structures (DataFrame objects). There are several cases to consider which are very important to understand: one-to-one joins: for example when joining two DataFrame objects on their indexes (which must contain unique values). many-to-one joins: for example when joining an index (unique) to one or more columns in a different DataFrame. many-to-many joins: joining columns on columns. Note When joining columns on columns (potentially a many-to-many join), any indexes on the passed DataFrame objects will be discarded. It is worth spending some time understanding the result of the many-to-many join case. In SQL / standard relational algebra, if a key combination appears more than once in both tables, the resulting table will have the Cartesian product of the associated data. Here is a very basic example with one unique key combination: In [33]: left = pd.DataFrame( ....: { ....: "key": ["K0", "K1", "K2", "K3"], ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: } ....: ) ....: In [34]: right = pd.DataFrame( ....: { ....: "key": ["K0", "K1", "K2", "K3"], ....: "C": ["C0", "C1", "C2", "C3"], ....: "D": ["D0", "D1", "D2", "D3"], ....: } ....: ) ....: In [35]: result = pd.merge(left, right, on="key") Here is a more complicated example with multiple join keys. Only the keys appearing in left and right are present (the intersection), since how='inner' by default. In [36]: left = pd.DataFrame( ....: { ....: "key1": ["K0", "K0", "K1", "K2"], ....: "key2": ["K0", "K1", "K0", "K1"], ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: } ....: ) ....: In [37]: right = pd.DataFrame( ....: { ....: "key1": ["K0", "K1", "K1", "K2"], ....: "key2": ["K0", "K0", "K0", "K0"], ....: "C": ["C0", "C1", "C2", "C3"], ....: "D": ["D0", "D1", "D2", "D3"], ....: } ....: ) ....: In [38]: result = pd.merge(left, right, on=["key1", "key2"]) The how argument to merge specifies how to determine which keys are to be included in the resulting table. If a key combination does not appear in either the left or right tables, the values in the joined table will be NA. Here is a summary of the how options and their SQL equivalent names: Merge method SQL Join Name Description left LEFT OUTER JOIN Use keys from left frame only right RIGHT OUTER JOIN Use keys from right frame only outer FULL OUTER JOIN Use union of keys from both frames inner INNER JOIN Use intersection of keys from both frames cross CROSS JOIN Create the cartesian product of rows of both frames In [39]: result = pd.merge(left, right, how="left", on=["key1", "key2"]) In [40]: result = pd.merge(left, right, how="right", on=["key1", "key2"]) In [41]: result = pd.merge(left, right, how="outer", on=["key1", "key2"]) In [42]: result = pd.merge(left, right, how="inner", on=["key1", "key2"]) In [43]: result = pd.merge(left, right, how="cross") You can merge a mult-indexed Series and a DataFrame, if the names of the MultiIndex correspond to the columns from the DataFrame. Transform the Series to a DataFrame using Series.reset_index() before merging, as shown in the following example. In [44]: df = pd.DataFrame({"Let": ["A", "B", "C"], "Num": [1, 2, 3]}) In [45]: df Out[45]: Let Num 0 A 1 1 B 2 2 C 3 In [46]: ser = pd.Series( ....: ["a", "b", "c", "d", "e", "f"], ....: index=pd.MultiIndex.from_arrays( ....: [["A", "B", "C"] * 2, [1, 2, 3, 4, 5, 6]], names=["Let", "Num"] ....: ), ....: ) ....: In [47]: ser Out[47]: Let Num A 1 a B 2 b C 3 c A 4 d B 5 e C 6 f dtype: object In [48]: pd.merge(df, ser.reset_index(), on=["Let", "Num"]) Out[48]: Let Num 0 0 A 1 a 1 B 2 b 2 C 3 c Here is another example with duplicate join keys in DataFrames: In [49]: left = pd.DataFrame({"A": [1, 2], "B": [2, 2]}) In [50]: right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]}) In [51]: result = pd.merge(left, right, on="B", how="outer") Warning Joining / merging on duplicate keys can cause a returned frame that is the multiplication of the row dimensions, which may result in memory overflow. It is the user’ s responsibility to manage duplicate values in keys before joining large DataFrames. Checking for duplicate keys# Users can use the validate argument to automatically check whether there are unexpected duplicates in their merge keys. Key uniqueness is checked before merge operations and so should protect against memory overflows. Checking key uniqueness is also a good way to ensure user data structures are as expected. In the following example, there are duplicate values of B in the right DataFrame. As this is not a one-to-one merge – as specified in the validate argument – an exception will be raised. In [52]: left = pd.DataFrame({"A": [1, 2], "B": [1, 2]}) In [53]: right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]}) In [53]: result = pd.merge(left, right, on="B", how="outer", validate="one_to_one") ... MergeError: Merge keys are not unique in right dataset; not a one-to-one merge If the user is aware of the duplicates in the right DataFrame but wants to ensure there are no duplicates in the left DataFrame, one can use the validate='one_to_many' argument instead, which will not raise an exception. In [54]: pd.merge(left, right, on="B", how="outer", validate="one_to_many") Out[54]: A_x B A_y 0 1 1 NaN 1 2 2 4.0 2 2 2 5.0 3 2 2 6.0 The merge indicator# merge() accepts the argument indicator. If True, a Categorical-type column called _merge will be added to the output object that takes on values: Observation Origin _merge value Merge key only in 'left' frame left_only Merge key only in 'right' frame right_only Merge key in both frames both In [55]: df1 = pd.DataFrame({"col1": [0, 1], "col_left": ["a", "b"]}) In [56]: df2 = pd.DataFrame({"col1": [1, 2, 2], "col_right": [2, 2, 2]}) In [57]: pd.merge(df1, df2, on="col1", how="outer", indicator=True) Out[57]: col1 col_left col_right _merge 0 0 a NaN left_only 1 1 b 2.0 both 2 2 NaN 2.0 right_only 3 2 NaN 2.0 right_only The indicator argument will also accept string arguments, in which case the indicator function will use the value of the passed string as the name for the indicator column. In [58]: pd.merge(df1, df2, on="col1", how="outer", indicator="indicator_column") Out[58]: col1 col_left col_right indicator_column 0 0 a NaN left_only 1 1 b 2.0 both 2 2 NaN 2.0 right_only 3 2 NaN 2.0 right_only Merge dtypes# Merging will preserve the dtype of the join keys. In [59]: left = pd.DataFrame({"key": [1], "v1": [10]}) In [60]: left Out[60]: key v1 0 1 10 In [61]: right = pd.DataFrame({"key": [1, 2], "v1": [20, 30]}) In [62]: right Out[62]: key v1 0 1 20 1 2 30 We are able to preserve the join keys: In [63]: pd.merge(left, right, how="outer") Out[63]: key v1 0 1 10 1 1 20 2 2 30 In [64]: pd.merge(left, right, how="outer").dtypes Out[64]: key int64 v1 int64 dtype: object Of course if you have missing values that are introduced, then the resulting dtype will be upcast. In [65]: pd.merge(left, right, how="outer", on="key") Out[65]: key v1_x v1_y 0 1 10.0 20 1 2 NaN 30 In [66]: pd.merge(left, right, how="outer", on="key").dtypes Out[66]: key int64 v1_x float64 v1_y int64 dtype: object Merging will preserve category dtypes of the mergands. See also the section on categoricals. The left frame. In [67]: from pandas.api.types import CategoricalDtype In [68]: X = pd.Series(np.random.choice(["foo", "bar"], size=(10,))) In [69]: X = X.astype(CategoricalDtype(categories=["foo", "bar"])) In [70]: left = pd.DataFrame( ....: {"X": X, "Y": np.random.choice(["one", "two", "three"], size=(10,))} ....: ) ....: In [71]: left Out[71]: X Y 0 bar one 1 foo one 2 foo three 3 bar three 4 foo one 5 bar one 6 bar three 7 bar three 8 bar three 9 foo three In [72]: left.dtypes Out[72]: X category Y object dtype: object The right frame. In [73]: right = pd.DataFrame( ....: { ....: "X": pd.Series(["foo", "bar"], dtype=CategoricalDtype(["foo", "bar"])), ....: "Z": [1, 2], ....: } ....: ) ....: In [74]: right Out[74]: X Z 0 foo 1 1 bar 2 In [75]: right.dtypes Out[75]: X category Z int64 dtype: object The merged result: In [76]: result = pd.merge(left, right, how="outer") In [77]: result Out[77]: X Y Z 0 bar one 2 1 bar three 2 2 bar one 2 3 bar three 2 4 bar three 2 5 bar three 2 6 foo one 1 7 foo three 1 8 foo one 1 9 foo three 1 In [78]: result.dtypes Out[78]: X category Y object Z int64 dtype: object Note The category dtypes must be exactly the same, meaning the same categories and the ordered attribute. Otherwise the result will coerce to the categories’ dtype. Note Merging on category dtypes that are the same can be quite performant compared to object dtype merging. Joining on index# DataFrame.join() is a convenient method for combining the columns of two potentially differently-indexed DataFrames into a single result DataFrame. Here is a very basic example: In [79]: left = pd.DataFrame( ....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=["K0", "K1", "K2"] ....: ) ....: In [80]: right = pd.DataFrame( ....: {"C": ["C0", "C2", "C3"], "D": ["D0", "D2", "D3"]}, index=["K0", "K2", "K3"] ....: ) ....: In [81]: result = left.join(right) In [82]: result = left.join(right, how="outer") The same as above, but with how='inner'. In [83]: result = left.join(right, how="inner") The data alignment here is on the indexes (row labels). This same behavior can be achieved using merge plus additional arguments instructing it to use the indexes: In [84]: result = pd.merge(left, right, left_index=True, right_index=True, how="outer") In [85]: result = pd.merge(left, right, left_index=True, right_index=True, how="inner") Joining key columns on an index# join() takes an optional on argument which may be a column or multiple column names, which specifies that the passed DataFrame is to be aligned on that column in the DataFrame. These two function calls are completely equivalent: left.join(right, on=key_or_keys) pd.merge( left, right, left_on=key_or_keys, right_index=True, how="left", sort=False ) Obviously you can choose whichever form you find more convenient. For many-to-one joins (where one of the DataFrame’s is already indexed by the join key), using join may be more convenient. Here is a simple example: In [86]: left = pd.DataFrame( ....: { ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: "key": ["K0", "K1", "K0", "K1"], ....: } ....: ) ....: In [87]: right = pd.DataFrame({"C": ["C0", "C1"], "D": ["D0", "D1"]}, index=["K0", "K1"]) In [88]: result = left.join(right, on="key") In [89]: result = pd.merge( ....: left, right, left_on="key", right_index=True, how="left", sort=False ....: ) ....: To join on multiple keys, the passed DataFrame must have a MultiIndex: In [90]: left = pd.DataFrame( ....: { ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: "key1": ["K0", "K0", "K1", "K2"], ....: "key2": ["K0", "K1", "K0", "K1"], ....: } ....: ) ....: In [91]: index = pd.MultiIndex.from_tuples( ....: [("K0", "K0"), ("K1", "K0"), ("K2", "K0"), ("K2", "K1")] ....: ) ....: In [92]: right = pd.DataFrame( ....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=index ....: ) ....: Now this can be joined by passing the two key column names: In [93]: result = left.join(right, on=["key1", "key2"]) The default for DataFrame.join is to perform a left join (essentially a “VLOOKUP” operation, for Excel users), which uses only the keys found in the calling DataFrame. Other join types, for example inner join, can be just as easily performed: In [94]: result = left.join(right, on=["key1", "key2"], how="inner") As you can see, this drops any rows where there was no match. Joining a single Index to a MultiIndex# You can join a singly-indexed DataFrame with a level of a MultiIndexed DataFrame. The level will match on the name of the index of the singly-indexed frame against a level name of the MultiIndexed frame. In [95]: left = pd.DataFrame( ....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, ....: index=pd.Index(["K0", "K1", "K2"], name="key"), ....: ) ....: In [96]: index = pd.MultiIndex.from_tuples( ....: [("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")], ....: names=["key", "Y"], ....: ) ....: In [97]: right = pd.DataFrame( ....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, ....: index=index, ....: ) ....: In [98]: result = left.join(right, how="inner") This is equivalent but less verbose and more memory efficient / faster than this. In [99]: result = pd.merge( ....: left.reset_index(), right.reset_index(), on=["key"], how="inner" ....: ).set_index(["key","Y"]) ....: Joining with two MultiIndexes# This is supported in a limited way, provided that the index for the right argument is completely used in the join, and is a subset of the indices in the left argument, as in this example: In [100]: leftindex = pd.MultiIndex.from_product( .....: [list("abc"), list("xy"), [1, 2]], names=["abc", "xy", "num"] .....: ) .....: In [101]: left = pd.DataFrame({"v1": range(12)}, index=leftindex) In [102]: left Out[102]: v1 abc xy num a x 1 0 2 1 y 1 2 2 3 b x 1 4 2 5 y 1 6 2 7 c x 1 8 2 9 y 1 10 2 11 In [103]: rightindex = pd.MultiIndex.from_product( .....: [list("abc"), list("xy")], names=["abc", "xy"] .....: ) .....: In [104]: right = pd.DataFrame({"v2": [100 * i for i in range(1, 7)]}, index=rightindex) In [105]: right Out[105]: v2 abc xy a x 100 y 200 b x 300 y 400 c x 500 y 600 In [106]: left.join(right, on=["abc", "xy"], how="inner") Out[106]: v1 v2 abc xy num a x 1 0 100 2 1 100 y 1 2 200 2 3 200 b x 1 4 300 2 5 300 y 1 6 400 2 7 400 c x 1 8 500 2 9 500 y 1 10 600 2 11 600 If that condition is not satisfied, a join with two multi-indexes can be done using the following code. In [107]: leftindex = pd.MultiIndex.from_tuples( .....: [("K0", "X0"), ("K0", "X1"), ("K1", "X2")], names=["key", "X"] .....: ) .....: In [108]: left = pd.DataFrame( .....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=leftindex .....: ) .....: In [109]: rightindex = pd.MultiIndex.from_tuples( .....: [("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")], names=["key", "Y"] .....: ) .....: In [110]: right = pd.DataFrame( .....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=rightindex .....: ) .....: In [111]: result = pd.merge( .....: left.reset_index(), right.reset_index(), on=["key"], how="inner" .....: ).set_index(["key", "X", "Y"]) .....: Merging on a combination of columns and index levels# Strings passed as the on, left_on, and right_on parameters may refer to either column names or index level names. This enables merging DataFrame instances on a combination of index levels and columns without resetting indexes. In [112]: left_index = pd.Index(["K0", "K0", "K1", "K2"], name="key1") In [113]: left = pd.DataFrame( .....: { .....: "A": ["A0", "A1", "A2", "A3"], .....: "B": ["B0", "B1", "B2", "B3"], .....: "key2": ["K0", "K1", "K0", "K1"], .....: }, .....: index=left_index, .....: ) .....: In [114]: right_index = pd.Index(["K0", "K1", "K2", "K2"], name="key1") In [115]: right = pd.DataFrame( .....: { .....: "C": ["C0", "C1", "C2", "C3"], .....: "D": ["D0", "D1", "D2", "D3"], .....: "key2": ["K0", "K0", "K0", "K1"], .....: }, .....: index=right_index, .....: ) .....: In [116]: result = left.merge(right, on=["key1", "key2"]) Note When DataFrames are merged on a string that matches an index level in both frames, the index level is preserved as an index level in the resulting DataFrame. Note When DataFrames are merged using only some of the levels of a MultiIndex, the extra levels will be dropped from the resulting merge. In order to preserve those levels, use reset_index on those level names to move those levels to columns prior to doing the merge. Note If a string matches both a column name and an index level name, then a warning is issued and the column takes precedence. This will result in an ambiguity error in a future version. Overlapping value columns# The merge suffixes argument takes a tuple of list of strings to append to overlapping column names in the input DataFrames to disambiguate the result columns: In [117]: left = pd.DataFrame({"k": ["K0", "K1", "K2"], "v": [1, 2, 3]}) In [118]: right = pd.DataFrame({"k": ["K0", "K0", "K3"], "v": [4, 5, 6]}) In [119]: result = pd.merge(left, right, on="k") In [120]: result = pd.merge(left, right, on="k", suffixes=("_l", "_r")) DataFrame.join() has lsuffix and rsuffix arguments which behave similarly. In [121]: left = left.set_index("k") In [122]: right = right.set_index("k") In [123]: result = left.join(right, lsuffix="_l", rsuffix="_r") Joining multiple DataFrames# A list or tuple of DataFrames can also be passed to join() to join them together on their indexes. In [124]: right2 = pd.DataFrame({"v": [7, 8, 9]}, index=["K1", "K1", "K2"]) In [125]: result = left.join([right, right2]) Merging together values within Series or DataFrame columns# Another fairly common situation is to have two like-indexed (or similarly indexed) Series or DataFrame objects and wanting to “patch” values in one object from values for matching indices in the other. Here is an example: In [126]: df1 = pd.DataFrame( .....: [[np.nan, 3.0, 5.0], [-4.6, np.nan, np.nan], [np.nan, 7.0, np.nan]] .....: ) .....: In [127]: df2 = pd.DataFrame([[-42.6, np.nan, -8.2], [-5.0, 1.6, 4]], index=[1, 2]) For this, use the combine_first() method: In [128]: result = df1.combine_first(df2) Note that this method only takes values from the right DataFrame if they are missing in the left DataFrame. A related method, update(), alters non-NA values in place: In [129]: df1.update(df2) Timeseries friendly merging# Merging ordered data# A merge_ordered() function allows combining time series and other ordered data. In particular it has an optional fill_method keyword to fill/interpolate missing data: In [130]: left = pd.DataFrame( .....: {"k": ["K0", "K1", "K1", "K2"], "lv": [1, 2, 3, 4], "s": ["a", "b", "c", "d"]} .....: ) .....: In [131]: right = pd.DataFrame({"k": ["K1", "K2", "K4"], "rv": [1, 2, 3]}) In [132]: pd.merge_ordered(left, right, fill_method="ffill", left_by="s") Out[132]: k lv s rv 0 K0 1.0 a NaN 1 K1 1.0 a 1.0 2 K2 1.0 a 2.0 3 K4 1.0 a 3.0 4 K1 2.0 b 1.0 5 K2 2.0 b 2.0 6 K4 2.0 b 3.0 7 K1 3.0 c 1.0 8 K2 3.0 c 2.0 9 K4 3.0 c 3.0 10 K1 NaN d 1.0 11 K2 4.0 d 2.0 12 K4 4.0 d 3.0 Merging asof# A merge_asof() is similar to an ordered left-join except that we match on nearest key rather than equal keys. For each row in the left DataFrame, we select the last row in the right DataFrame whose on key is less than the left’s key. Both DataFrames must be sorted by the key. Optionally an asof merge can perform a group-wise merge. This matches the by key equally, in addition to the nearest match on the on key. For example; we might have trades and quotes and we want to asof merge them. In [133]: trades = pd.DataFrame( .....: { .....: "time": pd.to_datetime( .....: [ .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.038", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.048", .....: ] .....: ), .....: "ticker": ["MSFT", "MSFT", "GOOG", "GOOG", "AAPL"], .....: "price": [51.95, 51.95, 720.77, 720.92, 98.00], .....: "quantity": [75, 155, 100, 100, 100], .....: }, .....: columns=["time", "ticker", "price", "quantity"], .....: ) .....: In [134]: quotes = pd.DataFrame( .....: { .....: "time": pd.to_datetime( .....: [ .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.030", .....: "20160525 13:30:00.041", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.049", .....: "20160525 13:30:00.072", .....: "20160525 13:30:00.075", .....: ] .....: ), .....: "ticker": ["GOOG", "MSFT", "MSFT", "MSFT", "GOOG", "AAPL", "GOOG", "MSFT"], .....: "bid": [720.50, 51.95, 51.97, 51.99, 720.50, 97.99, 720.50, 52.01], .....: "ask": [720.93, 51.96, 51.98, 52.00, 720.93, 98.01, 720.88, 52.03], .....: }, .....: columns=["time", "ticker", "bid", "ask"], .....: ) .....: In [135]: trades Out[135]: time ticker price quantity 0 2016-05-25 13:30:00.023 MSFT 51.95 75 1 2016-05-25 13:30:00.038 MSFT 51.95 155 2 2016-05-25 13:30:00.048 GOOG 720.77 100 3 2016-05-25 13:30:00.048 GOOG 720.92 100 4 2016-05-25 13:30:00.048 AAPL 98.00 100 In [136]: quotes Out[136]: time ticker bid ask 0 2016-05-25 13:30:00.023 GOOG 720.50 720.93 1 2016-05-25 13:30:00.023 MSFT 51.95 51.96 2 2016-05-25 13:30:00.030 MSFT 51.97 51.98 3 2016-05-25 13:30:00.041 MSFT 51.99 52.00 4 2016-05-25 13:30:00.048 GOOG 720.50 720.93 5 2016-05-25 13:30:00.049 AAPL 97.99 98.01 6 2016-05-25 13:30:00.072 GOOG 720.50 720.88 7 2016-05-25 13:30:00.075 MSFT 52.01 52.03 By default we are taking the asof of the quotes. In [137]: pd.merge_asof(trades, quotes, on="time", by="ticker") Out[137]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 51.95 51.96 1 2016-05-25 13:30:00.038 MSFT 51.95 155 51.97 51.98 2 2016-05-25 13:30:00.048 GOOG 720.77 100 720.50 720.93 3 2016-05-25 13:30:00.048 GOOG 720.92 100 720.50 720.93 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN We only asof within 2ms between the quote time and the trade time. In [138]: pd.merge_asof(trades, quotes, on="time", by="ticker", tolerance=pd.Timedelta("2ms")) Out[138]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 51.95 51.96 1 2016-05-25 13:30:00.038 MSFT 51.95 155 NaN NaN 2 2016-05-25 13:30:00.048 GOOG 720.77 100 720.50 720.93 3 2016-05-25 13:30:00.048 GOOG 720.92 100 720.50 720.93 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN We only asof within 10ms between the quote time and the trade time and we exclude exact matches on time. Note that though we exclude the exact matches (of the quotes), prior quotes do propagate to that point in time. In [139]: pd.merge_asof( .....: trades, .....: quotes, .....: on="time", .....: by="ticker", .....: tolerance=pd.Timedelta("10ms"), .....: allow_exact_matches=False, .....: ) .....: Out[139]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 NaN NaN 1 2016-05-25 13:30:00.038 MSFT 51.95 155 51.97 51.98 2 2016-05-25 13:30:00.048 GOOG 720.77 100 NaN NaN 3 2016-05-25 13:30:00.048 GOOG 720.92 100 NaN NaN 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN Comparing objects# The compare() and compare() methods allow you to compare two DataFrame or Series, respectively, and summarize their differences. This feature was added in V1.1.0. For example, you might want to compare two DataFrame and stack their differences side by side. In [140]: df = pd.DataFrame( .....: { .....: "col1": ["a", "a", "b", "b", "a"], .....: "col2": [1.0, 2.0, 3.0, np.nan, 5.0], .....: "col3": [1.0, 2.0, 3.0, 4.0, 5.0], .....: }, .....: columns=["col1", "col2", "col3"], .....: ) .....: In [141]: df Out[141]: col1 col2 col3 0 a 1.0 1.0 1 a 2.0 2.0 2 b 3.0 3.0 3 b NaN 4.0 4 a 5.0 5.0 In [142]: df2 = df.copy() In [143]: df2.loc[0, "col1"] = "c" In [144]: df2.loc[2, "col3"] = 4.0 In [145]: df2 Out[145]: col1 col2 col3 0 c 1.0 1.0 1 a 2.0 2.0 2 b 3.0 4.0 3 b NaN 4.0 4 a 5.0 5.0 In [146]: df.compare(df2) Out[146]: col1 col3 self other self other 0 a c NaN NaN 2 NaN NaN 3.0 4.0 By default, if two corresponding values are equal, they will be shown as NaN. Furthermore, if all values in an entire row / column, the row / column will be omitted from the result. The remaining differences will be aligned on columns. If you wish, you may choose to stack the differences on rows. In [147]: df.compare(df2, align_axis=0) Out[147]: col1 col3 0 self a NaN other c NaN 2 self NaN 3.0 other NaN 4.0 If you wish to keep all original rows and columns, set keep_shape argument to True. In [148]: df.compare(df2, keep_shape=True) Out[148]: col1 col2 col3 self other self other self other 0 a c NaN NaN NaN NaN 1 NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN 3.0 4.0 3 NaN NaN NaN NaN NaN NaN 4 NaN NaN NaN NaN NaN NaN You may also keep all the original values even if they are equal. In [149]: df.compare(df2, keep_shape=True, keep_equal=True) Out[149]: col1 col2 col3 self other self other self other 0 a c 1.0 1.0 1.0 1.0 1 a a 2.0 2.0 2.0 2.0 2 b b 3.0 3.0 3.0 4.0 3 b b NaN NaN 4.0 4.0 4 a a 5.0 5.0 5.0 5.0
652
1,144
Recognising Missing Values compared to previous days and creating new dataframe, Pandas df = pd.DataFrame({'Date': {0:'2020-04-01', 1:'2020-04-01', 2:'2020-04-01', 3:'2020-04-02', 4:'2020-04-02', 5:'2020-04-03', 6:'2020-04-03', 7:'2020-04-03'}, 'Name': {0:'AA', 1:'BB', 2:'CC', 3:'AA', 4:'BB', 5:'AA', 6:'BB', 7:'CC'}, 'Weight':{0:1, 1:3, 2:6, 3:1, 4:4, 5:2, 6:5, 7:7}}) df=df.set_index(['Date']) For each unique date in df.index - I would like to compare the Name columns to those present for the previous date, and if a name is missing, add a value of 0 to the weight column For example..How would I recognise that 2020-04-02 was missing a row for 'CC' and insert a weight of 0 for this row. Thereby achieving a DataFrame that looks like this... Name Weight 2020-04-01 AA 1 2020-04-01 BB 3 2020-04-01 CC 6 2020-04-02 AA 1 2020-04-02 BB 4 2020-04-02 CC 0 2020-04-03 AA 2 2020-04-03 BB 5 2020-04-03 CC 7
You can add Name to index, then reshape by DataFrame.unstack with add 0 with DataFrame.stack for original format of DataFrame: df = df.set_index('Name', append=True).unstack(fill_value=0).stack().reset_index(level=1) print (df) Name Weight Date 2020-04-01 AA 1 2020-04-01 BB 3 2020-04-01 CC 6 2020-04-02 AA 1 2020-04-02 BB 4 2020-04-02 CC 0 2020-04-03 AA 2 2020-04-03 BB 5 2020-04-03 CC 7
65,190,202
Convert numbers toTrue values in pandas column
<p>There is a lot of information how to convert bool values to numbers 1 an 0, but I have not found how to do the opposite conversion:</p> <p>My DataFrame:</p> <pre><code>col1 col2 1 NaN 2 0.2222 3 NaN 4 4.555 </code></pre> <p>Expected output:</p> <pre><code>col1 col2 1 NaN 2 True 3 NaN 4 True </code></pre> <p>Numbers became True values.</p>
65,190,250
"2020-12-07T22:10:56.667000"
2
null
1
27
python|pandas
<pre><code>df.loc[~df['col2'].isnull(),'col2']=True </code></pre>
"2020-12-07T22:14:15.570000"
2
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.isna.html
pandas.DataFrame.isna# pandas.DataFrame.isna# DataFrame.isna()[source]# Detect missing values. Return a boolean same-sized object indicating if the values are NA. NA values, such as None or numpy.NaN, gets mapped to True values. Everything else gets mapped to False values. Characters such as empty strings '' or numpy.inf are not considered NA values (unless you set pandas.options.mode.use_inf_as_na = True). Returns DataFrameMask of bool values for each element in DataFrame that indicates whether an element is an NA value. See also DataFrame.isnullAlias of isna. DataFrame.notnaBoolean inverse of isna. DataFrame.dropnaOmit axes labels with missing values. isnaTop-level isna. Examples Show which entries in a DataFrame are NA. >>> df = pd.DataFrame(dict(age=[5, 6, np.NaN], ... born=[pd.NaT, pd.Timestamp('1939-05-27'), ... pd.Timestamp('1940-04-25')], ... name=['Alfred', 'Batman', ''], ... toy=[None, 'Batmobile', 'Joker'])) >>> df age born name toy 0 5.0 NaT Alfred None 1 6.0 1939-05-27 Batman Batmobile 2 NaN 1940-04-25 Joker df.loc[~df['col2'].isnull(),'col2']=True >>> df.isna() age born name toy 0 False True False True 1 False False False False 2 True False False False Show which entries in a Series are NA. >>> ser = pd.Series([5, 6, np.NaN]) >>> ser 0 5.0 1 6.0 2 NaN dtype: float64 >>> ser.isna() 0 False 1 False 2 True dtype: bool
1,188
1,229
Convert numbers toTrue values in pandas column There is a lot of information how to convert bool values to numbers 1 an 0, but I have not found how to do the opposite conversion: My DataFrame: col1 col2 1 NaN 2 0.2222 3 NaN 4 4.555 Expected output: col1 col2 1 NaN 2 True 3 NaN 4 True Numbers became True values.
bool /
df.loc[~df['col2'].isnull(),'col2']=True
62,429,779
How to build a Pandas DataFrame from a multi-index dictionary
<p>I'm trying to figure out how to build a Pandas DataFrame from a multi-index dictionary. Right now my data in one row per index and I would like it if it was in multiple rows per index.</p> <pre><code>x = [1,2,3,4,5,6,7,8,9,10] data1 = {'symbol_1':x,'symbol_2': x,'symbol_3':x} data2 = {'symbol_1':x,'symbol_2': x,'symbol_3':x} data3 = {'symbol_1':x,'symbol_2': x,'symbol_3':x} y = {'data_1':data1, 'data_2':data2, 'data_3':data3} </code></pre> <p><a href="https://i.stack.imgur.com/emiv0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/emiv0.png" alt="Need to make data in multiple rows"></a></p>
62,429,865
"2020-06-17T13:08:54.980000"
4
null
0
38
python|pandas
<p>If need multiple values per rows in <code>MultiIndex</code> use <a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.concat.html" rel="nofollow noreferrer"><code>concat</code></a> with dict comprehension:</p> <pre><code>df = pd.concat({k:pd.DataFrame(v) for k, v in y.items()}) print (df) symbol_1 symbol_2 symbol_3 data_1 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 10 10 10 data_2 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 10 10 10 data_3 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 10 10 10 </code></pre>
"2020-06-17T13:12:24.260000"
2
https://pandas.pydata.org/docs/reference/api/pandas.MultiIndex.html
If need multiple values per rows in MultiIndex use concat with dict comprehension: df = pd.concat({k:pd.DataFrame(v) for k, v in y.items()}) print (df) symbol_1 symbol_2 symbol_3 data_1 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 10 10 10 data_2 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 10 10 10 data_3 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 10 10 10
0
1,360
How to build a Pandas DataFrame from a multi-index dictionary I'm trying to figure out how to build a Pandas DataFrame from a multi-index dictionary. Right now my data in one row per index and I would like it if it was in multiple rows per index. x = [1,2,3,4,5,6,7,8,9,10] data1 = {'symbol_1':x,'symbol_2': x,'symbol_3':x} data2 = {'symbol_1':x,'symbol_2': x,'symbol_3':x} data3 = {'symbol_1':x,'symbol_2': x,'symbol_3':x} y = {'data_1':data1, 'data_2':data2, 'data_3':data3}
How to build a Pandas DataFrame from a multi-index dictionary
If need multiple values per rows in MultiIndex use concat with dict comprehension: df = pd.concat({k:pd.DataFrame(v) for k, v in y.items()}) print (df) symbol_1 symbol_2 symbol_3 data_1 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 10 10 10 data_2 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 10 10 10 data_3 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 10 10 10
64,938,669
Replace rows with different number of characters
<p>I have a column having strings of different number of characters. Most of rows have the following number of characters:</p> <pre><code>xx.xx.xxxx xx-xx-xx </code></pre> <p>but there are also rows having different number, for instance</p> <pre><code>xxx.xxx.xxxx xxxx xxxxxxxxxxxxxxx </code></pre> <p>I would like to replace those columns that have a number of characters different from <code>xx.xx.xxxx xx-xx-xx</code> with a null value (e.g. NA). My approach would be to calculate length of <code>xx.xx.xxxx xx-xx-xx</code> and then filter rows which have a different number of characters: <code>df[df['Char']!=len('xx.xx.xxxx xx-xx-xx')]</code>. But I would need also to replace the values of those rows. Can you please tell me how to do it?</p> <p>My column looks like</p> <pre><code>Char xx.xx.xxxx xx-xx-xx xxx.xxx.xxxx xxxx xxxxxxxxxxxxxxx xx.xx.xxxx xx-xx-xx xx.xx.xxxx xx-xx-xx xx.xx.xxxx xx-xx-xx xx.xx.xxxx xx-xx-xx </code></pre> <p>and my expected output would be</p> <pre><code>Char xx.xx.xxxx xx-xx-xx Na NA NA xx.xx.xxxx xx-xx-xx xx.xx.xxxx xx-xx-xx xx.xx.xxxx xx-xx-xx xx.xx.xxxx xx-xx-xx </code></pre>
64,938,720
"2020-11-21T00:37:46.200000"
2
null
1
61
python|pandas
<p>Try with <code>loc</code></p> <pre><code>df.loc[df['Char'].str.len()!=len('xx.xx.xxxx xx-xx-xx'),'Char']=np.nan </code></pre>
"2020-11-21T00:47:04.477000"
2
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.replace.html
pandas.DataFrame.replace# pandas.DataFrame.replace# DataFrame.replace(to_replace=None, value=_NoDefault.no_default, *, inplace=False, limit=None, regex=False, method=_NoDefault.no_default)[source]# Replace values given in to_replace with value. Values of the DataFrame are replaced with other values dynamically. This differs from updating with .loc or .iloc, which require you to specify a location to update with some value. Parameters to_replacestr, regex, list, dict, Series, int, float, or NoneHow to find the values that will be replaced. numeric, str or regex: numeric: numeric values equal to to_replace will be replaced with value str: string exactly matching to_replace will be replaced with value regex: regexs matching to_replace will be replaced with value list of str, regex, or numeric: First, if to_replace and value are both lists, they must be the same length. Second, if regex=True then all of the strings in both Try with loc df.loc[df['Char'].str.len()!=len('xx.xx.xxxx xx-xx-xx'),'Char']=np.nan lists will be interpreted as regexs otherwise they will match directly. This doesn’t matter much for value since there are only a few possible substitution regexes you can use. str, regex and numeric rules apply as above. dict: Dicts can be used to specify different replacement values for different existing values. For example, {'a': 'b', 'y': 'z'} replaces the value ‘a’ with ‘b’ and ‘y’ with ‘z’. To use a dict in this way, the optional value parameter should not be given. For a DataFrame a dict can specify that different values should be replaced in different columns. For example, {'a': 1, 'b': 'z'} looks for the value 1 in column ‘a’ and the value ‘z’ in column ‘b’ and replaces these values with whatever is specified in value. The value parameter should not be None in this case. You can treat this as a special case of passing two lists except that you are specifying the column to search in. For a DataFrame nested dictionaries, e.g., {'a': {'b': np.nan}}, are read as follows: look in column ‘a’ for the value ‘b’ and replace it with NaN. The optional value parameter should not be specified to use a nested dict in this way. You can nest regular expressions as well. Note that column names (the top-level dictionary keys in a nested dictionary) cannot be regular expressions. None: This means that the regex argument must be a string, compiled regular expression, or list, dict, ndarray or Series of such elements. If value is also None then this must be a nested dictionary or Series. See the examples section for examples of each of these. valuescalar, dict, list, str, regex, default NoneValue to replace any values matching to_replace with. For a DataFrame a dict of values can be used to specify which value to use for each column (columns not in the dict will not be filled). Regular expressions, strings and lists or dicts of such objects are also allowed. inplacebool, default FalseWhether to modify the DataFrame rather than creating a new one. limitint, default NoneMaximum size gap to forward or backward fill. regexbool or same types as to_replace, default FalseWhether to interpret to_replace and/or value as regular expressions. If this is True then to_replace must be a string. Alternatively, this could be a regular expression or a list, dict, or array of regular expressions in which case to_replace must be None. method{‘pad’, ‘ffill’, ‘bfill’}The method to use when for replacement, when to_replace is a scalar, list or tuple and value is None. Changed in version 0.23.0: Added to DataFrame. Returns DataFrameObject after replacement. Raises AssertionError If regex is not a bool and to_replace is not None. TypeError If to_replace is not a scalar, array-like, dict, or None If to_replace is a dict and value is not a list, dict, ndarray, or Series If to_replace is None and regex is not compilable into a regular expression or is a list, dict, ndarray, or Series. When replacing multiple bool or datetime64 objects and the arguments to to_replace does not match the type of the value being replaced ValueError If a list or an ndarray is passed to to_replace and value but they are not the same length. See also DataFrame.fillnaFill NA values. DataFrame.whereReplace values based on boolean condition. Series.str.replaceSimple string replacement. Notes Regex substitution is performed under the hood with re.sub. The rules for substitution for re.sub are the same. Regular expressions will only substitute on strings, meaning you cannot provide, for example, a regular expression matching floating point numbers and expect the columns in your frame that have a numeric dtype to be matched. However, if those floating point numbers are strings, then you can do this. This method has a lot of options. You are encouraged to experiment and play with this method to gain intuition about how it works. When dict is used as the to_replace value, it is like key(s) in the dict are the to_replace part and value(s) in the dict are the value parameter. Examples Scalar `to_replace` and `value` >>> s = pd.Series([1, 2, 3, 4, 5]) >>> s.replace(1, 5) 0 5 1 2 2 3 3 4 4 5 dtype: int64 >>> df = pd.DataFrame({'A': [0, 1, 2, 3, 4], ... 'B': [5, 6, 7, 8, 9], ... 'C': ['a', 'b', 'c', 'd', 'e']}) >>> df.replace(0, 5) A B C 0 5 5 a 1 1 6 b 2 2 7 c 3 3 8 d 4 4 9 e List-like `to_replace` >>> df.replace([0, 1, 2, 3], 4) A B C 0 4 5 a 1 4 6 b 2 4 7 c 3 4 8 d 4 4 9 e >>> df.replace([0, 1, 2, 3], [4, 3, 2, 1]) A B C 0 4 5 a 1 3 6 b 2 2 7 c 3 1 8 d 4 4 9 e >>> s.replace([1, 2], method='bfill') 0 3 1 3 2 3 3 4 4 5 dtype: int64 dict-like `to_replace` >>> df.replace({0: 10, 1: 100}) A B C 0 10 5 a 1 100 6 b 2 2 7 c 3 3 8 d 4 4 9 e >>> df.replace({'A': 0, 'B': 5}, 100) A B C 0 100 100 a 1 1 6 b 2 2 7 c 3 3 8 d 4 4 9 e >>> df.replace({'A': {0: 100, 4: 400}}) A B C 0 100 5 a 1 1 6 b 2 2 7 c 3 3 8 d 4 400 9 e Regular expression `to_replace` >>> df = pd.DataFrame({'A': ['bat', 'foo', 'bait'], ... 'B': ['abc', 'bar', 'xyz']}) >>> df.replace(to_replace=r'^ba.$', value='new', regex=True) A B 0 new abc 1 foo new 2 bait xyz >>> df.replace({'A': r'^ba.$'}, {'A': 'new'}, regex=True) A B 0 new abc 1 foo bar 2 bait xyz >>> df.replace(regex=r'^ba.$', value='new') A B 0 new abc 1 foo new 2 bait xyz >>> df.replace(regex={r'^ba.$': 'new', 'foo': 'xyz'}) A B 0 new abc 1 xyz new 2 bait xyz >>> df.replace(regex=[r'^ba.$', 'foo'], value='new') A B 0 new abc 1 new new 2 bait xyz Compare the behavior of s.replace({'a': None}) and s.replace('a', None) to understand the peculiarities of the to_replace parameter: >>> s = pd.Series([10, 'a', 'a', 'b', 'a']) When one uses a dict as the to_replace value, it is like the value(s) in the dict are equal to the value parameter. s.replace({'a': None}) is equivalent to s.replace(to_replace={'a': None}, value=None, method=None): >>> s.replace({'a': None}) 0 10 1 None 2 None 3 b 4 None dtype: object When value is not explicitly passed and to_replace is a scalar, list or tuple, replace uses the method parameter (default ‘pad’) to do the replacement. So this is why the ‘a’ values are being replaced by 10 in rows 1 and 2 and ‘b’ in row 4 in this case. >>> s.replace('a') 0 10 1 10 2 10 3 b 4 b dtype: object On the other hand, if None is explicitly passed for value, it will be respected: >>> s.replace('a', None) 0 10 1 None 2 None 3 b 4 None dtype: object Changed in version 1.4.0: Previously the explicit None was silently ignored.
947
1,031
Replace rows with different number of characters I have a column having strings of different number of characters. Most of rows have the following number of characters: xx.xx.xxxx xx-xx-xx but there are also rows having different number, for instance xxx.xxx.xxxx xxxx xxxxxxxxxxxxxxx I would like to replace those columns that have a number of characters different from xx.xx.xxxx xx-xx-xx with a null value (e.g. NA). My approach would be to calculate length of xx.xx.xxxx xx-xx-xx and then filter rows which have a different number of characters: df[df['Char']!=len('xx.xx.xxxx xx-xx-xx')]. But I would need also to replace the values of those rows. Can you please tell me how to do it? My column looks like Char xx.xx.xxxx xx-xx-xx xxx.xxx.xxxx xxxx xxxxxxxxxxxxxxx xx.xx.xxxx xx-xx-xx xx.xx.xxxx xx-xx-xx xx.xx.xxxx xx-xx-xx xx.xx.xxxx xx-xx-xx and my expected output would be Char xx.xx.xxxx xx-xx-xx Na NA NA xx.xx.xxxx xx-xx-xx xx.xx.xxxx xx-xx-xx xx.xx.xxxx xx-xx-xx xx.xx.xxxx xx-xx-xx
Try with loc df.loc[df['Char'].str.len()!=len('xx.xx.xxxx xx-xx-xx'),'Char']=np.nan
62,563,033
return first column number that fulfills a condition in pandas
<p>I have a dataset with several columns of cumulative sums. For every row, I want to return the first column number that satisfies a condition.</p> <p>Toy example:</p> <pre><code>df = pd.DataFrame(np.array(range(20)).reshape(4,5).T).cumsum(axis=1) &gt;&gt;&gt; df 0 1 2 3 0 0 5 15 30 1 1 7 18 34 2 2 9 21 38 3 3 11 24 42 4 4 13 27 46 </code></pre> <p>If I want to return the first column whose value is greater than 20 for instance.</p> <p>Desired output:</p> <pre><code>3 3 2 2 2 </code></pre> <p>Many thanks as always!</p>
62,563,047
"2020-06-24T19:52:02.073000"
2
null
2
73
python|pandas
<p>Try with <code>idxmax</code></p> <pre><code>df.gt(20).idxmax(1) Out[66]: 0 3 1 3 2 2 3 2 4 2 dtype: object </code></pre>
"2020-06-24T19:53:09.417000"
2
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.any.html
pandas.DataFrame.any# pandas.DataFrame.any# DataFrame.any(*, axis=0, bool_only=None, skipna=True, level=None, **kwargs)[source]# Return whether any element is True, potentially over an axis. Returns False unless there is at least one element within a series or along a Dataframe axis that is True or equivalent (e.g. non-zero or non-empty). Parameters axis{0 or ‘index’, 1 or ‘columns’, None}, default 0Indicate which axis or axes should be reduced. For Series this parameter is unused and defaults to 0. 0 / ‘index’ : reduce the index, return a Series whose index is the original column labels. 1 / ‘columns’ : reduce the columns, return a Series whose index is the original index. Try with idxmax df.gt(20).idxmax(1) Out[66]: 0 3 1 3 2 2 3 2 4 2 dtype: object None : reduce all axes, return a scalar. bool_onlybool, default NoneInclude only boolean columns. If None, will attempt to use everything, then use only boolean data. Not implemented for Series. skipnabool, default TrueExclude NA/null values. If the entire row/column is NA and skipna is True, then the result will be False, as for an empty row/column. If skipna is False, then NA are treated as True, because these are not equal to zero. levelint or level name, default NoneIf the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series. Deprecated since version 1.3.0: The level keyword is deprecated. Use groupby instead. **kwargsany, default NoneAdditional keywords have no effect but might be accepted for compatibility with NumPy. Returns Series or DataFrameIf level is specified, then, DataFrame is returned; otherwise, Series is returned. See also numpy.anyNumpy version of this method. Series.anyReturn whether any element is True. Series.allReturn whether all elements are True. DataFrame.anyReturn whether any element is True over requested axis. DataFrame.allReturn whether all elements are True over requested axis. Examples Series For Series input, the output is a scalar indicating whether any element is True. >>> pd.Series([False, False]).any() False >>> pd.Series([True, False]).any() True >>> pd.Series([], dtype="float64").any() False >>> pd.Series([np.nan]).any() False >>> pd.Series([np.nan]).any(skipna=False) True DataFrame Whether each column contains at least one True element (the default). >>> df = pd.DataFrame({"A": [1, 2], "B": [0, 2], "C": [0, 0]}) >>> df A B C 0 1 0 0 1 2 2 0 >>> df.any() A True B True C False dtype: bool Aggregating over the columns. >>> df = pd.DataFrame({"A": [True, False], "B": [1, 2]}) >>> df A B 0 True 1 1 False 2 >>> df.any(axis='columns') 0 True 1 True dtype: bool >>> df = pd.DataFrame({"A": [True, False], "B": [1, 0]}) >>> df A B 0 True 1 1 False 0 >>> df.any(axis='columns') 0 True 1 False dtype: bool Aggregating over the entire DataFrame with axis=None. >>> df.any(axis=None) True any for an empty DataFrame is an empty Series. >>> pd.DataFrame([]).any() Series([], dtype: bool)
690
785
return first column number that fulfills a condition in pandas I have a dataset with several columns of cumulative sums. For every row, I want to return the first column number that satisfies a condition. Toy example: df = pd.DataFrame(np.array(range(20)).reshape(4,5).T).cumsum(axis=1) >>> df 0 1 2 3 0 0 5 15 30 1 1 7 18 34 2 2 9 21 38 3 3 11 24 42 4 4 13 27 46 If I want to return the first column whose value is greater than 20 for instance. Desired output: 3 3 2 2 2 Many thanks as always!
Try with idxmax df.gt(20).idxmax(1) Out[66]: 0 3 1 3 2 2 3 2 4 2 dtype: object
69,252,907
Groupby, sort and join
<p>I have this data:</p> <p><a href="https://i.stack.imgur.com/p5Csm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/p5Csm.png" alt="enter image description here" /></a></p> <p>What I want to do is group this data by name, sort by number and join by the <code>inter_type</code> column.</p> <p>So I want to have this kind of output:</p> <p><a href="https://i.stack.imgur.com/PYJpg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PYJpg.png" alt="enter image description here" /></a></p> <p>I don't know how to do it. I know how to join them by group, but how to sort them? I have no idea how to implement here sort_values()?</p> <pre><code>df.groupby(['name'])['inter_type'].apply(lambda x: ','.join(x)).reset_index() </code></pre>
69,252,986
"2021-09-20T10:28:46.097000"
2
null
2
82
python|pandas
<p>Just use <code>sort_value</code> in the beginning on the <code>number</code> column. Also I modified your code to use <code>agg</code> with <code>','.join</code> without <code>lambda</code> directly, it's more efficient and also it's shorter.</p> <p>Code:</p> <pre><code>df.sort_values('number').groupby('name')['inter_type'].agg(','.join) </code></pre>
"2021-09-20T10:34:21.950000"
2
https://pandas.pydata.org/docs/user_guide/groupby.html
Group by: split-apply-combine# Group by: split-apply-combine# By “group by” we are referring to a process involving one or more of the following steps: Splitting the data into groups based on some criteria. Applying a function to each group independently. Combining the results into a data structure. Out of these, the split step is the most straightforward. In fact, in many situations we may wish to split the data set into groups and do something with those groups. In the apply step, we might wish to do one of the following: Aggregation: compute a summary statistic (or statistics) for each group. Some examples: Compute group sums or means. Compute group sizes / counts. Transformation: perform some group-specific computations and return a like-indexed object. Some examples: Standardize data (zscore) within a group. Filling NAs within groups with a value derived from each group. Filtration: discard some groups, according to a group-wise computation Just use sort_value in the beginning on the number column. Also I modified your code to use agg with ','.join without lambda directly, it's more efficient and also it's shorter. Code: df.sort_values('number').groupby('name')['inter_type'].agg(','.join) that evaluates True or False. Some examples: Discard data that belongs to groups with only a few members. Filter out data based on the group sum or mean. Some combination of the above: GroupBy will examine the results of the apply step and try to return a sensibly combined result if it doesn’t fit into either of the above two categories. Since the set of object instance methods on pandas data structures are generally rich and expressive, we often simply want to invoke, say, a DataFrame function on each group. The name GroupBy should be quite familiar to those who have used a SQL-based tool (or itertools), in which you can write code like: SELECT Column1, Column2, mean(Column3), sum(Column4) FROM SomeTable GROUP BY Column1, Column2 We aim to make operations like this natural and easy to express using pandas. We’ll address each area of GroupBy functionality then provide some non-trivial examples / use cases. See the cookbook for some advanced strategies. Splitting an object into groups# pandas objects can be split on any of their axes. The abstract definition of grouping is to provide a mapping of labels to group names. To create a GroupBy object (more on what the GroupBy object is later), you may do the following: In [1]: df = pd.DataFrame( ...: [ ...: ("bird", "Falconiformes", 389.0), ...: ("bird", "Psittaciformes", 24.0), ...: ("mammal", "Carnivora", 80.2), ...: ("mammal", "Primates", np.nan), ...: ("mammal", "Carnivora", 58), ...: ], ...: index=["falcon", "parrot", "lion", "monkey", "leopard"], ...: columns=("class", "order", "max_speed"), ...: ) ...: In [2]: df Out[2]: class order max_speed falcon bird Falconiformes 389.0 parrot bird Psittaciformes 24.0 lion mammal Carnivora 80.2 monkey mammal Primates NaN leopard mammal Carnivora 58.0 # default is axis=0 In [3]: grouped = df.groupby("class") In [4]: grouped = df.groupby("order", axis="columns") In [5]: grouped = df.groupby(["class", "order"]) The mapping can be specified many different ways: A Python function, to be called on each of the axis labels. A list or NumPy array of the same length as the selected axis. A dict or Series, providing a label -> group name mapping. For DataFrame objects, a string indicating either a column name or an index level name to be used to group. df.groupby('A') is just syntactic sugar for df.groupby(df['A']). A list of any of the above things. Collectively we refer to the grouping objects as the keys. For example, consider the following DataFrame: Note A string passed to groupby may refer to either a column or an index level. If a string matches both a column name and an index level name, a ValueError will be raised. In [6]: df = pd.DataFrame( ...: { ...: "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"], ...: "B": ["one", "one", "two", "three", "two", "two", "one", "three"], ...: "C": np.random.randn(8), ...: "D": np.random.randn(8), ...: } ...: ) ...: In [7]: df Out[7]: A B C D 0 foo one 0.469112 -0.861849 1 bar one -0.282863 -2.104569 2 foo two -1.509059 -0.494929 3 bar three -1.135632 1.071804 4 foo two 1.212112 0.721555 5 bar two -0.173215 -0.706771 6 foo one 0.119209 -1.039575 7 foo three -1.044236 0.271860 On a DataFrame, we obtain a GroupBy object by calling groupby(). We could naturally group by either the A or B columns, or both: In [8]: grouped = df.groupby("A") In [9]: grouped = df.groupby(["A", "B"]) If we also have a MultiIndex on columns A and B, we can group by all but the specified columns In [10]: df2 = df.set_index(["A", "B"]) In [11]: grouped = df2.groupby(level=df2.index.names.difference(["B"])) In [12]: grouped.sum() Out[12]: C D A bar -1.591710 -1.739537 foo -0.752861 -1.402938 These will split the DataFrame on its index (rows). We could also split by the columns: In [13]: def get_letter_type(letter): ....: if letter.lower() in 'aeiou': ....: return 'vowel' ....: else: ....: return 'consonant' ....: In [14]: grouped = df.groupby(get_letter_type, axis=1) pandas Index objects support duplicate values. If a non-unique index is used as the group key in a groupby operation, all values for the same index value will be considered to be in one group and thus the output of aggregation functions will only contain unique index values: In [15]: lst = [1, 2, 3, 1, 2, 3] In [16]: s = pd.Series([1, 2, 3, 10, 20, 30], lst) In [17]: grouped = s.groupby(level=0) In [18]: grouped.first() Out[18]: 1 1 2 2 3 3 dtype: int64 In [19]: grouped.last() Out[19]: 1 10 2 20 3 30 dtype: int64 In [20]: grouped.sum() Out[20]: 1 11 2 22 3 33 dtype: int64 Note that no splitting occurs until it’s needed. Creating the GroupBy object only verifies that you’ve passed a valid mapping. Note Many kinds of complicated data manipulations can be expressed in terms of GroupBy operations (though can’t be guaranteed to be the most efficient). You can get quite creative with the label mapping functions. GroupBy sorting# By default the group keys are sorted during the groupby operation. You may however pass sort=False for potential speedups: In [21]: df2 = pd.DataFrame({"X": ["B", "B", "A", "A"], "Y": [1, 2, 3, 4]}) In [22]: df2.groupby(["X"]).sum() Out[22]: Y X A 7 B 3 In [23]: df2.groupby(["X"], sort=False).sum() Out[23]: Y X B 3 A 7 Note that groupby will preserve the order in which observations are sorted within each group. For example, the groups created by groupby() below are in the order they appeared in the original DataFrame: In [24]: df3 = pd.DataFrame({"X": ["A", "B", "A", "B"], "Y": [1, 4, 3, 2]}) In [25]: df3.groupby(["X"]).get_group("A") Out[25]: X Y 0 A 1 2 A 3 In [26]: df3.groupby(["X"]).get_group("B") Out[26]: X Y 1 B 4 3 B 2 New in version 1.1.0. GroupBy dropna# By default NA values are excluded from group keys during the groupby operation. However, in case you want to include NA values in group keys, you could pass dropna=False to achieve it. In [27]: df_list = [[1, 2, 3], [1, None, 4], [2, 1, 3], [1, 2, 2]] In [28]: df_dropna = pd.DataFrame(df_list, columns=["a", "b", "c"]) In [29]: df_dropna Out[29]: a b c 0 1 2.0 3 1 1 NaN 4 2 2 1.0 3 3 1 2.0 2 # Default ``dropna`` is set to True, which will exclude NaNs in keys In [30]: df_dropna.groupby(by=["b"], dropna=True).sum() Out[30]: a c b 1.0 2 3 2.0 2 5 # In order to allow NaN in keys, set ``dropna`` to False In [31]: df_dropna.groupby(by=["b"], dropna=False).sum() Out[31]: a c b 1.0 2 3 2.0 2 5 NaN 1 4 The default setting of dropna argument is True which means NA are not included in group keys. GroupBy object attributes# The groups attribute is a dict whose keys are the computed unique groups and corresponding values being the axis labels belonging to each group. In the above example we have: In [32]: df.groupby("A").groups Out[32]: {'bar': [1, 3, 5], 'foo': [0, 2, 4, 6, 7]} In [33]: df.groupby(get_letter_type, axis=1).groups Out[33]: {'consonant': ['B', 'C', 'D'], 'vowel': ['A']} Calling the standard Python len function on the GroupBy object just returns the length of the groups dict, so it is largely just a convenience: In [34]: grouped = df.groupby(["A", "B"]) In [35]: grouped.groups Out[35]: {('bar', 'one'): [1], ('bar', 'three'): [3], ('bar', 'two'): [5], ('foo', 'one'): [0, 6], ('foo', 'three'): [7], ('foo', 'two'): [2, 4]} In [36]: len(grouped) Out[36]: 6 GroupBy will tab complete column names (and other attributes): In [37]: df Out[37]: height weight gender 2000-01-01 42.849980 157.500553 male 2000-01-02 49.607315 177.340407 male 2000-01-03 56.293531 171.524640 male 2000-01-04 48.421077 144.251986 female 2000-01-05 46.556882 152.526206 male 2000-01-06 68.448851 168.272968 female 2000-01-07 70.757698 136.431469 male 2000-01-08 58.909500 176.499753 female 2000-01-09 76.435631 174.094104 female 2000-01-10 45.306120 177.540920 male In [38]: gb = df.groupby("gender") In [39]: gb.<TAB> # noqa: E225, E999 gb.agg gb.boxplot gb.cummin gb.describe gb.filter gb.get_group gb.height gb.last gb.median gb.ngroups gb.plot gb.rank gb.std gb.transform gb.aggregate gb.count gb.cumprod gb.dtype gb.first gb.groups gb.hist gb.max gb.min gb.nth gb.prod gb.resample gb.sum gb.var gb.apply gb.cummax gb.cumsum gb.fillna gb.gender gb.head gb.indices gb.mean gb.name gb.ohlc gb.quantile gb.size gb.tail gb.weight GroupBy with MultiIndex# With hierarchically-indexed data, it’s quite natural to group by one of the levels of the hierarchy. Let’s create a Series with a two-level MultiIndex. In [40]: arrays = [ ....: ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"], ....: ["one", "two", "one", "two", "one", "two", "one", "two"], ....: ] ....: In [41]: index = pd.MultiIndex.from_arrays(arrays, names=["first", "second"]) In [42]: s = pd.Series(np.random.randn(8), index=index) In [43]: s Out[43]: first second bar one -0.919854 two -0.042379 baz one 1.247642 two -0.009920 foo one 0.290213 two 0.495767 qux one 0.362949 two 1.548106 dtype: float64 We can then group by one of the levels in s. In [44]: grouped = s.groupby(level=0) In [45]: grouped.sum() Out[45]: first bar -0.962232 baz 1.237723 foo 0.785980 qux 1.911055 dtype: float64 If the MultiIndex has names specified, these can be passed instead of the level number: In [46]: s.groupby(level="second").sum() Out[46]: second one 0.980950 two 1.991575 dtype: float64 Grouping with multiple levels is supported. In [47]: s Out[47]: first second third bar doo one -1.131345 two -0.089329 baz bee one 0.337863 two -0.945867 foo bop one -0.932132 two 1.956030 qux bop one 0.017587 two -0.016692 dtype: float64 In [48]: s.groupby(level=["first", "second"]).sum() Out[48]: first second bar doo -1.220674 baz bee -0.608004 foo bop 1.023898 qux bop 0.000895 dtype: float64 Index level names may be supplied as keys. In [49]: s.groupby(["first", "second"]).sum() Out[49]: first second bar doo -1.220674 baz bee -0.608004 foo bop 1.023898 qux bop 0.000895 dtype: float64 More on the sum function and aggregation later. Grouping DataFrame with Index levels and columns# A DataFrame may be grouped by a combination of columns and index levels by specifying the column names as strings and the index levels as pd.Grouper objects. In [50]: arrays = [ ....: ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"], ....: ["one", "two", "one", "two", "one", "two", "one", "two"], ....: ] ....: In [51]: index = pd.MultiIndex.from_arrays(arrays, names=["first", "second"]) In [52]: df = pd.DataFrame({"A": [1, 1, 1, 1, 2, 2, 3, 3], "B": np.arange(8)}, index=index) In [53]: df Out[53]: A B first second bar one 1 0 two 1 1 baz one 1 2 two 1 3 foo one 2 4 two 2 5 qux one 3 6 two 3 7 The following example groups df by the second index level and the A column. In [54]: df.groupby([pd.Grouper(level=1), "A"]).sum() Out[54]: B second A one 1 2 2 4 3 6 two 1 4 2 5 3 7 Index levels may also be specified by name. In [55]: df.groupby([pd.Grouper(level="second"), "A"]).sum() Out[55]: B second A one 1 2 2 4 3 6 two 1 4 2 5 3 7 Index level names may be specified as keys directly to groupby. In [56]: df.groupby(["second", "A"]).sum() Out[56]: B second A one 1 2 2 4 3 6 two 1 4 2 5 3 7 DataFrame column selection in GroupBy# Once you have created the GroupBy object from a DataFrame, you might want to do something different for each of the columns. Thus, using [] similar to getting a column from a DataFrame, you can do: In [57]: df = pd.DataFrame( ....: { ....: "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"], ....: "B": ["one", "one", "two", "three", "two", "two", "one", "three"], ....: "C": np.random.randn(8), ....: "D": np.random.randn(8), ....: } ....: ) ....: In [58]: df Out[58]: A B C D 0 foo one -0.575247 1.346061 1 bar one 0.254161 1.511763 2 foo two -1.143704 1.627081 3 bar three 0.215897 -0.990582 4 foo two 1.193555 -0.441652 5 bar two -0.077118 1.211526 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 In [59]: grouped = df.groupby(["A"]) In [60]: grouped_C = grouped["C"] In [61]: grouped_D = grouped["D"] This is mainly syntactic sugar for the alternative and much more verbose: In [62]: df["C"].groupby(df["A"]) Out[62]: <pandas.core.groupby.generic.SeriesGroupBy object at 0x7f1ea100a490> Additionally this method avoids recomputing the internal grouping information derived from the passed key. Iterating through groups# With the GroupBy object in hand, iterating through the grouped data is very natural and functions similarly to itertools.groupby(): In [63]: grouped = df.groupby('A') In [64]: for name, group in grouped: ....: print(name) ....: print(group) ....: bar A B C D 1 bar one 0.254161 1.511763 3 bar three 0.215897 -0.990582 5 bar two -0.077118 1.211526 foo A B C D 0 foo one -0.575247 1.346061 2 foo two -1.143704 1.627081 4 foo two 1.193555 -0.441652 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 In the case of grouping by multiple keys, the group name will be a tuple: In [65]: for name, group in df.groupby(['A', 'B']): ....: print(name) ....: print(group) ....: ('bar', 'one') A B C D 1 bar one 0.254161 1.511763 ('bar', 'three') A B C D 3 bar three 0.215897 -0.990582 ('bar', 'two') A B C D 5 bar two -0.077118 1.211526 ('foo', 'one') A B C D 0 foo one -0.575247 1.346061 6 foo one -0.408530 0.268520 ('foo', 'three') A B C D 7 foo three -0.862495 0.02458 ('foo', 'two') A B C D 2 foo two -1.143704 1.627081 4 foo two 1.193555 -0.441652 See Iterating through groups. Selecting a group# A single group can be selected using get_group(): In [66]: grouped.get_group("bar") Out[66]: A B C D 1 bar one 0.254161 1.511763 3 bar three 0.215897 -0.990582 5 bar two -0.077118 1.211526 Or for an object grouped on multiple columns: In [67]: df.groupby(["A", "B"]).get_group(("bar", "one")) Out[67]: A B C D 1 bar one 0.254161 1.511763 Aggregation# Once the GroupBy object has been created, several methods are available to perform a computation on the grouped data. These operations are similar to the aggregating API, window API, and resample API. An obvious one is aggregation via the aggregate() or equivalently agg() method: In [68]: grouped = df.groupby("A") In [69]: grouped[["C", "D"]].aggregate(np.sum) Out[69]: C D A bar 0.392940 1.732707 foo -1.796421 2.824590 In [70]: grouped = df.groupby(["A", "B"]) In [71]: grouped.aggregate(np.sum) Out[71]: C D A B bar one 0.254161 1.511763 three 0.215897 -0.990582 two -0.077118 1.211526 foo one -0.983776 1.614581 three -0.862495 0.024580 two 0.049851 1.185429 As you can see, the result of the aggregation will have the group names as the new index along the grouped axis. In the case of multiple keys, the result is a MultiIndex by default, though this can be changed by using the as_index option: In [72]: grouped = df.groupby(["A", "B"], as_index=False) In [73]: grouped.aggregate(np.sum) Out[73]: A B C D 0 bar one 0.254161 1.511763 1 bar three 0.215897 -0.990582 2 bar two -0.077118 1.211526 3 foo one -0.983776 1.614581 4 foo three -0.862495 0.024580 5 foo two 0.049851 1.185429 In [74]: df.groupby("A", as_index=False)[["C", "D"]].sum() Out[74]: A C D 0 bar 0.392940 1.732707 1 foo -1.796421 2.824590 Note that you could use the reset_index DataFrame function to achieve the same result as the column names are stored in the resulting MultiIndex: In [75]: df.groupby(["A", "B"]).sum().reset_index() Out[75]: A B C D 0 bar one 0.254161 1.511763 1 bar three 0.215897 -0.990582 2 bar two -0.077118 1.211526 3 foo one -0.983776 1.614581 4 foo three -0.862495 0.024580 5 foo two 0.049851 1.185429 Another simple aggregation example is to compute the size of each group. This is included in GroupBy as the size method. It returns a Series whose index are the group names and whose values are the sizes of each group. In [76]: grouped.size() Out[76]: A B size 0 bar one 1 1 bar three 1 2 bar two 1 3 foo one 2 4 foo three 1 5 foo two 2 In [77]: grouped.describe() Out[77]: C ... D count mean std min ... 25% 50% 75% max 0 1.0 0.254161 NaN 0.254161 ... 1.511763 1.511763 1.511763 1.511763 1 1.0 0.215897 NaN 0.215897 ... -0.990582 -0.990582 -0.990582 -0.990582 2 1.0 -0.077118 NaN -0.077118 ... 1.211526 1.211526 1.211526 1.211526 3 2.0 -0.491888 0.117887 -0.575247 ... 0.537905 0.807291 1.076676 1.346061 4 1.0 -0.862495 NaN -0.862495 ... 0.024580 0.024580 0.024580 0.024580 5 2.0 0.024925 1.652692 -1.143704 ... 0.075531 0.592714 1.109898 1.627081 [6 rows x 16 columns] Another aggregation example is to compute the number of unique values of each group. This is similar to the value_counts function, except that it only counts unique values. In [78]: ll = [['foo', 1], ['foo', 2], ['foo', 2], ['bar', 1], ['bar', 1]] In [79]: df4 = pd.DataFrame(ll, columns=["A", "B"]) In [80]: df4 Out[80]: A B 0 foo 1 1 foo 2 2 foo 2 3 bar 1 4 bar 1 In [81]: df4.groupby("A")["B"].nunique() Out[81]: A bar 1 foo 2 Name: B, dtype: int64 Note Aggregation functions will not return the groups that you are aggregating over if they are named columns, when as_index=True, the default. The grouped columns will be the indices of the returned object. Passing as_index=False will return the groups that you are aggregating over, if they are named columns. Aggregating functions are the ones that reduce the dimension of the returned objects. Some common aggregating functions are tabulated below: Function Description mean() Compute mean of groups sum() Compute sum of group values size() Compute group sizes count() Compute count of group std() Standard deviation of groups var() Compute variance of groups sem() Standard error of the mean of groups describe() Generates descriptive statistics first() Compute first of group values last() Compute last of group values nth() Take nth value, or a subset if n is a list min() Compute min of group values max() Compute max of group values The aggregating functions above will exclude NA values. Any function which reduces a Series to a scalar value is an aggregation function and will work, a trivial example is df.groupby('A').agg(lambda ser: 1). Note that nth() can act as a reducer or a filter, see here. Applying multiple functions at once# With grouped Series you can also pass a list or dict of functions to do aggregation with, outputting a DataFrame: In [82]: grouped = df.groupby("A") In [83]: grouped["C"].agg([np.sum, np.mean, np.std]) Out[83]: sum mean std A bar 0.392940 0.130980 0.181231 foo -1.796421 -0.359284 0.912265 On a grouped DataFrame, you can pass a list of functions to apply to each column, which produces an aggregated result with a hierarchical index: In [84]: grouped[["C", "D"]].agg([np.sum, np.mean, np.std]) Out[84]: C D sum mean std sum mean std A bar 0.392940 0.130980 0.181231 1.732707 0.577569 1.366330 foo -1.796421 -0.359284 0.912265 2.824590 0.564918 0.884785 The resulting aggregations are named for the functions themselves. If you need to rename, then you can add in a chained operation for a Series like this: In [85]: ( ....: grouped["C"] ....: .agg([np.sum, np.mean, np.std]) ....: .rename(columns={"sum": "foo", "mean": "bar", "std": "baz"}) ....: ) ....: Out[85]: foo bar baz A bar 0.392940 0.130980 0.181231 foo -1.796421 -0.359284 0.912265 For a grouped DataFrame, you can rename in a similar manner: In [86]: ( ....: grouped[["C", "D"]].agg([np.sum, np.mean, np.std]).rename( ....: columns={"sum": "foo", "mean": "bar", "std": "baz"} ....: ) ....: ) ....: Out[86]: C D foo bar baz foo bar baz A bar 0.392940 0.130980 0.181231 1.732707 0.577569 1.366330 foo -1.796421 -0.359284 0.912265 2.824590 0.564918 0.884785 Note In general, the output column names should be unique. You can’t apply the same function (or two functions with the same name) to the same column. In [87]: grouped["C"].agg(["sum", "sum"]) Out[87]: sum sum A bar 0.392940 0.392940 foo -1.796421 -1.796421 pandas does allow you to provide multiple lambdas. In this case, pandas will mangle the name of the (nameless) lambda functions, appending _<i> to each subsequent lambda. In [88]: grouped["C"].agg([lambda x: x.max() - x.min(), lambda x: x.median() - x.mean()]) Out[88]: <lambda_0> <lambda_1> A bar 0.331279 0.084917 foo 2.337259 -0.215962 Named aggregation# New in version 0.25.0. To support column-specific aggregation with control over the output column names, pandas accepts the special syntax in GroupBy.agg(), known as “named aggregation”, where The keywords are the output column names The values are tuples whose first element is the column to select and the second element is the aggregation to apply to that column. pandas provides the pandas.NamedAgg namedtuple with the fields ['column', 'aggfunc'] to make it clearer what the arguments are. As usual, the aggregation can be a callable or a string alias. In [89]: animals = pd.DataFrame( ....: { ....: "kind": ["cat", "dog", "cat", "dog"], ....: "height": [9.1, 6.0, 9.5, 34.0], ....: "weight": [7.9, 7.5, 9.9, 198.0], ....: } ....: ) ....: In [90]: animals Out[90]: kind height weight 0 cat 9.1 7.9 1 dog 6.0 7.5 2 cat 9.5 9.9 3 dog 34.0 198.0 In [91]: animals.groupby("kind").agg( ....: min_height=pd.NamedAgg(column="height", aggfunc="min"), ....: max_height=pd.NamedAgg(column="height", aggfunc="max"), ....: average_weight=pd.NamedAgg(column="weight", aggfunc=np.mean), ....: ) ....: Out[91]: min_height max_height average_weight kind cat 9.1 9.5 8.90 dog 6.0 34.0 102.75 pandas.NamedAgg is just a namedtuple. Plain tuples are allowed as well. In [92]: animals.groupby("kind").agg( ....: min_height=("height", "min"), ....: max_height=("height", "max"), ....: average_weight=("weight", np.mean), ....: ) ....: Out[92]: min_height max_height average_weight kind cat 9.1 9.5 8.90 dog 6.0 34.0 102.75 If your desired output column names are not valid Python keywords, construct a dictionary and unpack the keyword arguments In [93]: animals.groupby("kind").agg( ....: **{ ....: "total weight": pd.NamedAgg(column="weight", aggfunc=sum) ....: } ....: ) ....: Out[93]: total weight kind cat 17.8 dog 205.5 Additional keyword arguments are not passed through to the aggregation functions. Only pairs of (column, aggfunc) should be passed as **kwargs. If your aggregation functions requires additional arguments, partially apply them with functools.partial(). Note For Python 3.5 and earlier, the order of **kwargs in a functions was not preserved. This means that the output column ordering would not be consistent. To ensure consistent ordering, the keys (and so output columns) will always be sorted for Python 3.5. Named aggregation is also valid for Series groupby aggregations. In this case there’s no column selection, so the values are just the functions. In [94]: animals.groupby("kind").height.agg( ....: min_height="min", ....: max_height="max", ....: ) ....: Out[94]: min_height max_height kind cat 9.1 9.5 dog 6.0 34.0 Applying different functions to DataFrame columns# By passing a dict to aggregate you can apply a different aggregation to the columns of a DataFrame: In [95]: grouped.agg({"C": np.sum, "D": lambda x: np.std(x, ddof=1)}) Out[95]: C D A bar 0.392940 1.366330 foo -1.796421 0.884785 The function names can also be strings. In order for a string to be valid it must be either implemented on GroupBy or available via dispatching: In [96]: grouped.agg({"C": "sum", "D": "std"}) Out[96]: C D A bar 0.392940 1.366330 foo -1.796421 0.884785 Cython-optimized aggregation functions# Some common aggregations, currently only sum, mean, std, and sem, have optimized Cython implementations: In [97]: df.groupby("A")[["C", "D"]].sum() Out[97]: C D A bar 0.392940 1.732707 foo -1.796421 2.824590 In [98]: df.groupby(["A", "B"]).mean() Out[98]: C D A B bar one 0.254161 1.511763 three 0.215897 -0.990582 two -0.077118 1.211526 foo one -0.491888 0.807291 three -0.862495 0.024580 two 0.024925 0.592714 Of course sum and mean are implemented on pandas objects, so the above code would work even without the special versions via dispatching (see below). Aggregations with User-Defined Functions# Users can also provide their own functions for custom aggregations. When aggregating with a User-Defined Function (UDF), the UDF should not mutate the provided Series, see Mutating with User Defined Function (UDF) methods for more information. In [99]: animals.groupby("kind")[["height"]].agg(lambda x: set(x)) Out[99]: height kind cat {9.1, 9.5} dog {34.0, 6.0} The resulting dtype will reflect that of the aggregating function. If the results from different groups have different dtypes, then a common dtype will be determined in the same way as DataFrame construction. In [100]: animals.groupby("kind")[["height"]].agg(lambda x: x.astype(int).sum()) Out[100]: height kind cat 18 dog 40 Transformation# The transform method returns an object that is indexed the same as the one being grouped. The transform function must: Return a result that is either the same size as the group chunk or broadcastable to the size of the group chunk (e.g., a scalar, grouped.transform(lambda x: x.iloc[-1])). Operate column-by-column on the group chunk. The transform is applied to the first group chunk using chunk.apply. Not perform in-place operations on the group chunk. Group chunks should be treated as immutable, and changes to a group chunk may produce unexpected results. For example, when using fillna, inplace must be False (grouped.transform(lambda x: x.fillna(inplace=False))). (Optionally) operates on the entire group chunk. If this is supported, a fast path is used starting from the second chunk. Deprecated since version 1.5.0: When using .transform on a grouped DataFrame and the transformation function returns a DataFrame, currently pandas does not align the result’s index with the input’s index. This behavior is deprecated and alignment will be performed in a future version of pandas. You can apply .to_numpy() to the result of the transformation function to avoid alignment. Similar to Aggregations with User-Defined Functions, the resulting dtype will reflect that of the transformation function. If the results from different groups have different dtypes, then a common dtype will be determined in the same way as DataFrame construction. Suppose we wished to standardize the data within each group: In [101]: index = pd.date_range("10/1/1999", periods=1100) In [102]: ts = pd.Series(np.random.normal(0.5, 2, 1100), index) In [103]: ts = ts.rolling(window=100, min_periods=100).mean().dropna() In [104]: ts.head() Out[104]: 2000-01-08 0.779333 2000-01-09 0.778852 2000-01-10 0.786476 2000-01-11 0.782797 2000-01-12 0.798110 Freq: D, dtype: float64 In [105]: ts.tail() Out[105]: 2002-09-30 0.660294 2002-10-01 0.631095 2002-10-02 0.673601 2002-10-03 0.709213 2002-10-04 0.719369 Freq: D, dtype: float64 In [106]: transformed = ts.groupby(lambda x: x.year).transform( .....: lambda x: (x - x.mean()) / x.std() .....: ) .....: We would expect the result to now have mean 0 and standard deviation 1 within each group, which we can easily check: # Original Data In [107]: grouped = ts.groupby(lambda x: x.year) In [108]: grouped.mean() Out[108]: 2000 0.442441 2001 0.526246 2002 0.459365 dtype: float64 In [109]: grouped.std() Out[109]: 2000 0.131752 2001 0.210945 2002 0.128753 dtype: float64 # Transformed Data In [110]: grouped_trans = transformed.groupby(lambda x: x.year) In [111]: grouped_trans.mean() Out[111]: 2000 -4.870756e-16 2001 -1.545187e-16 2002 4.136282e-16 dtype: float64 In [112]: grouped_trans.std() Out[112]: 2000 1.0 2001 1.0 2002 1.0 dtype: float64 We can also visually compare the original and transformed data sets. In [113]: compare = pd.DataFrame({"Original": ts, "Transformed": transformed}) In [114]: compare.plot() Out[114]: <AxesSubplot: > Transformation functions that have lower dimension outputs are broadcast to match the shape of the input array. In [115]: ts.groupby(lambda x: x.year).transform(lambda x: x.max() - x.min()) Out[115]: 2000-01-08 0.623893 2000-01-09 0.623893 2000-01-10 0.623893 2000-01-11 0.623893 2000-01-12 0.623893 ... 2002-09-30 0.558275 2002-10-01 0.558275 2002-10-02 0.558275 2002-10-03 0.558275 2002-10-04 0.558275 Freq: D, Length: 1001, dtype: float64 Alternatively, the built-in methods could be used to produce the same outputs. In [116]: max_ts = ts.groupby(lambda x: x.year).transform("max") In [117]: min_ts = ts.groupby(lambda x: x.year).transform("min") In [118]: max_ts - min_ts Out[118]: 2000-01-08 0.623893 2000-01-09 0.623893 2000-01-10 0.623893 2000-01-11 0.623893 2000-01-12 0.623893 ... 2002-09-30 0.558275 2002-10-01 0.558275 2002-10-02 0.558275 2002-10-03 0.558275 2002-10-04 0.558275 Freq: D, Length: 1001, dtype: float64 Another common data transform is to replace missing data with the group mean. In [119]: data_df Out[119]: A B C 0 1.539708 -1.166480 0.533026 1 1.302092 -0.505754 NaN 2 -0.371983 1.104803 -0.651520 3 -1.309622 1.118697 -1.161657 4 -1.924296 0.396437 0.812436 .. ... ... ... 995 -0.093110 0.683847 -0.774753 996 -0.185043 1.438572 NaN 997 -0.394469 -0.642343 0.011374 998 -1.174126 1.857148 NaN 999 0.234564 0.517098 0.393534 [1000 rows x 3 columns] In [120]: countries = np.array(["US", "UK", "GR", "JP"]) In [121]: key = countries[np.random.randint(0, 4, 1000)] In [122]: grouped = data_df.groupby(key) # Non-NA count in each group In [123]: grouped.count() Out[123]: A B C GR 209 217 189 JP 240 255 217 UK 216 231 193 US 239 250 217 In [124]: transformed = grouped.transform(lambda x: x.fillna(x.mean())) We can verify that the group means have not changed in the transformed data and that the transformed data contains no NAs. In [125]: grouped_trans = transformed.groupby(key) In [126]: grouped.mean() # original group means Out[126]: A B C GR -0.098371 -0.015420 0.068053 JP 0.069025 0.023100 -0.077324 UK 0.034069 -0.052580 -0.116525 US 0.058664 -0.020399 0.028603 In [127]: grouped_trans.mean() # transformation did not change group means Out[127]: A B C GR -0.098371 -0.015420 0.068053 JP 0.069025 0.023100 -0.077324 UK 0.034069 -0.052580 -0.116525 US 0.058664 -0.020399 0.028603 In [128]: grouped.count() # original has some missing data points Out[128]: A B C GR 209 217 189 JP 240 255 217 UK 216 231 193 US 239 250 217 In [129]: grouped_trans.count() # counts after transformation Out[129]: A B C GR 228 228 228 JP 267 267 267 UK 247 247 247 US 258 258 258 In [130]: grouped_trans.size() # Verify non-NA count equals group size Out[130]: GR 228 JP 267 UK 247 US 258 dtype: int64 Note Some functions will automatically transform the input when applied to a GroupBy object, but returning an object of the same shape as the original. Passing as_index=False will not affect these transformation methods. For example: fillna, ffill, bfill, shift.. In [131]: grouped.ffill() Out[131]: A B C 0 1.539708 -1.166480 0.533026 1 1.302092 -0.505754 0.533026 2 -0.371983 1.104803 -0.651520 3 -1.309622 1.118697 -1.161657 4 -1.924296 0.396437 0.812436 .. ... ... ... 995 -0.093110 0.683847 -0.774753 996 -0.185043 1.438572 -0.774753 997 -0.394469 -0.642343 0.011374 998 -1.174126 1.857148 -0.774753 999 0.234564 0.517098 0.393534 [1000 rows x 3 columns] Window and resample operations# It is possible to use resample(), expanding() and rolling() as methods on groupbys. The example below will apply the rolling() method on the samples of the column B based on the groups of column A. In [132]: df_re = pd.DataFrame({"A": [1] * 10 + [5] * 10, "B": np.arange(20)}) In [133]: df_re Out[133]: A B 0 1 0 1 1 1 2 1 2 3 1 3 4 1 4 .. .. .. 15 5 15 16 5 16 17 5 17 18 5 18 19 5 19 [20 rows x 2 columns] In [134]: df_re.groupby("A").rolling(4).B.mean() Out[134]: A 1 0 NaN 1 NaN 2 NaN 3 1.5 4 2.5 ... 5 15 13.5 16 14.5 17 15.5 18 16.5 19 17.5 Name: B, Length: 20, dtype: float64 The expanding() method will accumulate a given operation (sum() in the example) for all the members of each particular group. In [135]: df_re.groupby("A").expanding().sum() Out[135]: B A 1 0 0.0 1 1.0 2 3.0 3 6.0 4 10.0 ... ... 5 15 75.0 16 91.0 17 108.0 18 126.0 19 145.0 [20 rows x 1 columns] Suppose you want to use the resample() method to get a daily frequency in each group of your dataframe and wish to complete the missing values with the ffill() method. In [136]: df_re = pd.DataFrame( .....: { .....: "date": pd.date_range(start="2016-01-01", periods=4, freq="W"), .....: "group": [1, 1, 2, 2], .....: "val": [5, 6, 7, 8], .....: } .....: ).set_index("date") .....: In [137]: df_re Out[137]: group val date 2016-01-03 1 5 2016-01-10 1 6 2016-01-17 2 7 2016-01-24 2 8 In [138]: df_re.groupby("group").resample("1D").ffill() Out[138]: group val group date 1 2016-01-03 1 5 2016-01-04 1 5 2016-01-05 1 5 2016-01-06 1 5 2016-01-07 1 5 ... ... ... 2 2016-01-20 2 7 2016-01-21 2 7 2016-01-22 2 7 2016-01-23 2 7 2016-01-24 2 8 [16 rows x 2 columns] Filtration# The filter method returns a subset of the original object. Suppose we want to take only elements that belong to groups with a group sum greater than 2. In [139]: sf = pd.Series([1, 1, 2, 3, 3, 3]) In [140]: sf.groupby(sf).filter(lambda x: x.sum() > 2) Out[140]: 3 3 4 3 5 3 dtype: int64 The argument of filter must be a function that, applied to the group as a whole, returns True or False. Another useful operation is filtering out elements that belong to groups with only a couple members. In [141]: dff = pd.DataFrame({"A": np.arange(8), "B": list("aabbbbcc")}) In [142]: dff.groupby("B").filter(lambda x: len(x) > 2) Out[142]: A B 2 2 b 3 3 b 4 4 b 5 5 b Alternatively, instead of dropping the offending groups, we can return a like-indexed objects where the groups that do not pass the filter are filled with NaNs. In [143]: dff.groupby("B").filter(lambda x: len(x) > 2, dropna=False) Out[143]: A B 0 NaN NaN 1 NaN NaN 2 2.0 b 3 3.0 b 4 4.0 b 5 5.0 b 6 NaN NaN 7 NaN NaN For DataFrames with multiple columns, filters should explicitly specify a column as the filter criterion. In [144]: dff["C"] = np.arange(8) In [145]: dff.groupby("B").filter(lambda x: len(x["C"]) > 2) Out[145]: A B C 2 2 b 2 3 3 b 3 4 4 b 4 5 5 b 5 Note Some functions when applied to a groupby object will act as a filter on the input, returning a reduced shape of the original (and potentially eliminating groups), but with the index unchanged. Passing as_index=False will not affect these transformation methods. For example: head, tail. In [146]: dff.groupby("B").head(2) Out[146]: A B C 0 0 a 0 1 1 a 1 2 2 b 2 3 3 b 3 6 6 c 6 7 7 c 7 Dispatching to instance methods# When doing an aggregation or transformation, you might just want to call an instance method on each data group. This is pretty easy to do by passing lambda functions: In [147]: grouped = df.groupby("A") In [148]: grouped.agg(lambda x: x.std()) Out[148]: C D A bar 0.181231 1.366330 foo 0.912265 0.884785 But, it’s rather verbose and can be untidy if you need to pass additional arguments. Using a bit of metaprogramming cleverness, GroupBy now has the ability to “dispatch” method calls to the groups: In [149]: grouped.std() Out[149]: C D A bar 0.181231 1.366330 foo 0.912265 0.884785 What is actually happening here is that a function wrapper is being generated. When invoked, it takes any passed arguments and invokes the function with any arguments on each group (in the above example, the std function). The results are then combined together much in the style of agg and transform (it actually uses apply to infer the gluing, documented next). This enables some operations to be carried out rather succinctly: In [150]: tsdf = pd.DataFrame( .....: np.random.randn(1000, 3), .....: index=pd.date_range("1/1/2000", periods=1000), .....: columns=["A", "B", "C"], .....: ) .....: In [151]: tsdf.iloc[::2] = np.nan In [152]: grouped = tsdf.groupby(lambda x: x.year) In [153]: grouped.fillna(method="pad") Out[153]: A B C 2000-01-01 NaN NaN NaN 2000-01-02 -0.353501 -0.080957 -0.876864 2000-01-03 -0.353501 -0.080957 -0.876864 2000-01-04 0.050976 0.044273 -0.559849 2000-01-05 0.050976 0.044273 -0.559849 ... ... ... ... 2002-09-22 0.005011 0.053897 -1.026922 2002-09-23 0.005011 0.053897 -1.026922 2002-09-24 -0.456542 -1.849051 1.559856 2002-09-25 -0.456542 -1.849051 1.559856 2002-09-26 1.123162 0.354660 1.128135 [1000 rows x 3 columns] In this example, we chopped the collection of time series into yearly chunks then independently called fillna on the groups. The nlargest and nsmallest methods work on Series style groupbys: In [154]: s = pd.Series([9, 8, 7, 5, 19, 1, 4.2, 3.3]) In [155]: g = pd.Series(list("abababab")) In [156]: gb = s.groupby(g) In [157]: gb.nlargest(3) Out[157]: a 4 19.0 0 9.0 2 7.0 b 1 8.0 3 5.0 7 3.3 dtype: float64 In [158]: gb.nsmallest(3) Out[158]: a 6 4.2 2 7.0 0 9.0 b 5 1.0 7 3.3 3 5.0 dtype: float64 Flexible apply# Some operations on the grouped data might not fit into either the aggregate or transform categories. Or, you may simply want GroupBy to infer how to combine the results. For these, use the apply function, which can be substituted for both aggregate and transform in many standard use cases. However, apply can handle some exceptional use cases. Note apply can act as a reducer, transformer, or filter function, depending on exactly what is passed to it. It can depend on the passed function and exactly what you are grouping. Thus the grouped column(s) may be included in the output as well as set the indices. In [159]: df Out[159]: A B C D 0 foo one -0.575247 1.346061 1 bar one 0.254161 1.511763 2 foo two -1.143704 1.627081 3 bar three 0.215897 -0.990582 4 foo two 1.193555 -0.441652 5 bar two -0.077118 1.211526 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 In [160]: grouped = df.groupby("A") # could also just call .describe() In [161]: grouped["C"].apply(lambda x: x.describe()) Out[161]: A bar count 3.000000 mean 0.130980 std 0.181231 min -0.077118 25% 0.069390 ... foo min -1.143704 25% -0.862495 50% -0.575247 75% -0.408530 max 1.193555 Name: C, Length: 16, dtype: float64 The dimension of the returned result can also change: In [162]: grouped = df.groupby('A')['C'] In [163]: def f(group): .....: return pd.DataFrame({'original': group, .....: 'demeaned': group - group.mean()}) .....: apply on a Series can operate on a returned value from the applied function, that is itself a series, and possibly upcast the result to a DataFrame: In [164]: def f(x): .....: return pd.Series([x, x ** 2], index=["x", "x^2"]) .....: In [165]: s = pd.Series(np.random.rand(5)) In [166]: s Out[166]: 0 0.321438 1 0.493496 2 0.139505 3 0.910103 4 0.194158 dtype: float64 In [167]: s.apply(f) Out[167]: x x^2 0 0.321438 0.103323 1 0.493496 0.243538 2 0.139505 0.019462 3 0.910103 0.828287 4 0.194158 0.037697 Control grouped column(s) placement with group_keys# Note If group_keys=True is specified when calling groupby(), functions passed to apply that return like-indexed outputs will have the group keys added to the result index. Previous versions of pandas would add the group keys only when the result from the applied function had a different index than the input. If group_keys is not specified, the group keys will not be added for like-indexed outputs. In the future this behavior will change to always respect group_keys, which defaults to True. Changed in version 1.5.0. To control whether the grouped column(s) are included in the indices, you can use the argument group_keys. Compare In [168]: df.groupby("A", group_keys=True).apply(lambda x: x) Out[168]: A B C D A bar 1 bar one 0.254161 1.511763 3 bar three 0.215897 -0.990582 5 bar two -0.077118 1.211526 foo 0 foo one -0.575247 1.346061 2 foo two -1.143704 1.627081 4 foo two 1.193555 -0.441652 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 with In [169]: df.groupby("A", group_keys=False).apply(lambda x: x) Out[169]: A B C D 0 foo one -0.575247 1.346061 1 bar one 0.254161 1.511763 2 foo two -1.143704 1.627081 3 bar three 0.215897 -0.990582 4 foo two 1.193555 -0.441652 5 bar two -0.077118 1.211526 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 Similar to Aggregations with User-Defined Functions, the resulting dtype will reflect that of the apply function. If the results from different groups have different dtypes, then a common dtype will be determined in the same way as DataFrame construction. Numba Accelerated Routines# New in version 1.1. If Numba is installed as an optional dependency, the transform and aggregate methods support engine='numba' and engine_kwargs arguments. See enhancing performance with Numba for general usage of the arguments and performance considerations. The function signature must start with values, index exactly as the data belonging to each group will be passed into values, and the group index will be passed into index. Warning When using engine='numba', there will be no “fall back” behavior internally. The group data and group index will be passed as NumPy arrays to the JITed user defined function, and no alternative execution attempts will be tried. Other useful features# Automatic exclusion of “nuisance” columns# Again consider the example DataFrame we’ve been looking at: In [170]: df Out[170]: A B C D 0 foo one -0.575247 1.346061 1 bar one 0.254161 1.511763 2 foo two -1.143704 1.627081 3 bar three 0.215897 -0.990582 4 foo two 1.193555 -0.441652 5 bar two -0.077118 1.211526 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 Suppose we wish to compute the standard deviation grouped by the A column. There is a slight problem, namely that we don’t care about the data in column B. We refer to this as a “nuisance” column. You can avoid nuisance columns by specifying numeric_only=True: In [171]: df.groupby("A").std(numeric_only=True) Out[171]: C D A bar 0.181231 1.366330 foo 0.912265 0.884785 Note that df.groupby('A').colname.std(). is more efficient than df.groupby('A').std().colname, so if the result of an aggregation function is only interesting over one column (here colname), it may be filtered before applying the aggregation function. Note Any object column, also if it contains numerical values such as Decimal objects, is considered as a “nuisance” columns. They are excluded from aggregate functions automatically in groupby. If you do wish to include decimal or object columns in an aggregation with other non-nuisance data types, you must do so explicitly. Warning The automatic dropping of nuisance columns has been deprecated and will be removed in a future version of pandas. If columns are included that cannot be operated on, pandas will instead raise an error. In order to avoid this, either select the columns you wish to operate on or specify numeric_only=True. In [172]: from decimal import Decimal In [173]: df_dec = pd.DataFrame( .....: { .....: "id": [1, 2, 1, 2], .....: "int_column": [1, 2, 3, 4], .....: "dec_column": [ .....: Decimal("0.50"), .....: Decimal("0.15"), .....: Decimal("0.25"), .....: Decimal("0.40"), .....: ], .....: } .....: ) .....: # Decimal columns can be sum'd explicitly by themselves... In [174]: df_dec.groupby(["id"])[["dec_column"]].sum() Out[174]: dec_column id 1 0.75 2 0.55 # ...but cannot be combined with standard data types or they will be excluded In [175]: df_dec.groupby(["id"])[["int_column", "dec_column"]].sum() Out[175]: int_column id 1 4 2 6 # Use .agg function to aggregate over standard and "nuisance" data types # at the same time In [176]: df_dec.groupby(["id"]).agg({"int_column": "sum", "dec_column": "sum"}) Out[176]: int_column dec_column id 1 4 0.75 2 6 0.55 Handling of (un)observed Categorical values# When using a Categorical grouper (as a single grouper, or as part of multiple groupers), the observed keyword controls whether to return a cartesian product of all possible groupers values (observed=False) or only those that are observed groupers (observed=True). Show all values: In [177]: pd.Series([1, 1, 1]).groupby( .....: pd.Categorical(["a", "a", "a"], categories=["a", "b"]), observed=False .....: ).count() .....: Out[177]: a 3 b 0 dtype: int64 Show only the observed values: In [178]: pd.Series([1, 1, 1]).groupby( .....: pd.Categorical(["a", "a", "a"], categories=["a", "b"]), observed=True .....: ).count() .....: Out[178]: a 3 dtype: int64 The returned dtype of the grouped will always include all of the categories that were grouped. In [179]: s = ( .....: pd.Series([1, 1, 1]) .....: .groupby(pd.Categorical(["a", "a", "a"], categories=["a", "b"]), observed=False) .....: .count() .....: ) .....: In [180]: s.index.dtype Out[180]: CategoricalDtype(categories=['a', 'b'], ordered=False) NA and NaT group handling# If there are any NaN or NaT values in the grouping key, these will be automatically excluded. In other words, there will never be an “NA group” or “NaT group”. This was not the case in older versions of pandas, but users were generally discarding the NA group anyway (and supporting it was an implementation headache). Grouping with ordered factors# Categorical variables represented as instance of pandas’s Categorical class can be used as group keys. If so, the order of the levels will be preserved: In [181]: data = pd.Series(np.random.randn(100)) In [182]: factor = pd.qcut(data, [0, 0.25, 0.5, 0.75, 1.0]) In [183]: data.groupby(factor).mean() Out[183]: (-2.645, -0.523] -1.362896 (-0.523, 0.0296] -0.260266 (0.0296, 0.654] 0.361802 (0.654, 2.21] 1.073801 dtype: float64 Grouping with a grouper specification# You may need to specify a bit more data to properly group. You can use the pd.Grouper to provide this local control. In [184]: import datetime In [185]: df = pd.DataFrame( .....: { .....: "Branch": "A A A A A A A B".split(), .....: "Buyer": "Carl Mark Carl Carl Joe Joe Joe Carl".split(), .....: "Quantity": [1, 3, 5, 1, 8, 1, 9, 3], .....: "Date": [ .....: datetime.datetime(2013, 1, 1, 13, 0), .....: datetime.datetime(2013, 1, 1, 13, 5), .....: datetime.datetime(2013, 10, 1, 20, 0), .....: datetime.datetime(2013, 10, 2, 10, 0), .....: datetime.datetime(2013, 10, 1, 20, 0), .....: datetime.datetime(2013, 10, 2, 10, 0), .....: datetime.datetime(2013, 12, 2, 12, 0), .....: datetime.datetime(2013, 12, 2, 14, 0), .....: ], .....: } .....: ) .....: In [186]: df Out[186]: Branch Buyer Quantity Date 0 A Carl 1 2013-01-01 13:00:00 1 A Mark 3 2013-01-01 13:05:00 2 A Carl 5 2013-10-01 20:00:00 3 A Carl 1 2013-10-02 10:00:00 4 A Joe 8 2013-10-01 20:00:00 5 A Joe 1 2013-10-02 10:00:00 6 A Joe 9 2013-12-02 12:00:00 7 B Carl 3 2013-12-02 14:00:00 Groupby a specific column with the desired frequency. This is like resampling. In [187]: df.groupby([pd.Grouper(freq="1M", key="Date"), "Buyer"])[["Quantity"]].sum() Out[187]: Quantity Date Buyer 2013-01-31 Carl 1 Mark 3 2013-10-31 Carl 6 Joe 9 2013-12-31 Carl 3 Joe 9 You have an ambiguous specification in that you have a named index and a column that could be potential groupers. In [188]: df = df.set_index("Date") In [189]: df["Date"] = df.index + pd.offsets.MonthEnd(2) In [190]: df.groupby([pd.Grouper(freq="6M", key="Date"), "Buyer"])[["Quantity"]].sum() Out[190]: Quantity Date Buyer 2013-02-28 Carl 1 Mark 3 2014-02-28 Carl 9 Joe 18 In [191]: df.groupby([pd.Grouper(freq="6M", level="Date"), "Buyer"])[["Quantity"]].sum() Out[191]: Quantity Date Buyer 2013-01-31 Carl 1 Mark 3 2014-01-31 Carl 9 Joe 18 Taking the first rows of each group# Just like for a DataFrame or Series you can call head and tail on a groupby: In [192]: df = pd.DataFrame([[1, 2], [1, 4], [5, 6]], columns=["A", "B"]) In [193]: df Out[193]: A B 0 1 2 1 1 4 2 5 6 In [194]: g = df.groupby("A") In [195]: g.head(1) Out[195]: A B 0 1 2 2 5 6 In [196]: g.tail(1) Out[196]: A B 1 1 4 2 5 6 This shows the first or last n rows from each group. Taking the nth row of each group# To select from a DataFrame or Series the nth item, use nth(). This is a reduction method, and will return a single row (or no row) per group if you pass an int for n: In [197]: df = pd.DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=["A", "B"]) In [198]: g = df.groupby("A") In [199]: g.nth(0) Out[199]: B A 1 NaN 5 6.0 In [200]: g.nth(-1) Out[200]: B A 1 4.0 5 6.0 In [201]: g.nth(1) Out[201]: B A 1 4.0 If you want to select the nth not-null item, use the dropna kwarg. For a DataFrame this should be either 'any' or 'all' just like you would pass to dropna: # nth(0) is the same as g.first() In [202]: g.nth(0, dropna="any") Out[202]: B A 1 4.0 5 6.0 In [203]: g.first() Out[203]: B A 1 4.0 5 6.0 # nth(-1) is the same as g.last() In [204]: g.nth(-1, dropna="any") # NaNs denote group exhausted when using dropna Out[204]: B A 1 4.0 5 6.0 In [205]: g.last() Out[205]: B A 1 4.0 5 6.0 In [206]: g.B.nth(0, dropna="all") Out[206]: A 1 4.0 5 6.0 Name: B, dtype: float64 As with other methods, passing as_index=False, will achieve a filtration, which returns the grouped row. In [207]: df = pd.DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=["A", "B"]) In [208]: g = df.groupby("A", as_index=False) In [209]: g.nth(0) Out[209]: A B 0 1 NaN 2 5 6.0 In [210]: g.nth(-1) Out[210]: A B 1 1 4.0 2 5 6.0 You can also select multiple rows from each group by specifying multiple nth values as a list of ints. In [211]: business_dates = pd.date_range(start="4/1/2014", end="6/30/2014", freq="B") In [212]: df = pd.DataFrame(1, index=business_dates, columns=["a", "b"]) # get the first, 4th, and last date index for each month In [213]: df.groupby([df.index.year, df.index.month]).nth([0, 3, -1]) Out[213]: a b 2014 4 1 1 4 1 1 4 1 1 5 1 1 5 1 1 5 1 1 6 1 1 6 1 1 6 1 1 Enumerate group items# To see the order in which each row appears within its group, use the cumcount method: In [214]: dfg = pd.DataFrame(list("aaabba"), columns=["A"]) In [215]: dfg Out[215]: A 0 a 1 a 2 a 3 b 4 b 5 a In [216]: dfg.groupby("A").cumcount() Out[216]: 0 0 1 1 2 2 3 0 4 1 5 3 dtype: int64 In [217]: dfg.groupby("A").cumcount(ascending=False) Out[217]: 0 3 1 2 2 1 3 1 4 0 5 0 dtype: int64 Enumerate groups# To see the ordering of the groups (as opposed to the order of rows within a group given by cumcount) you can use ngroup(). Note that the numbers given to the groups match the order in which the groups would be seen when iterating over the groupby object, not the order they are first observed. In [218]: dfg = pd.DataFrame(list("aaabba"), columns=["A"]) In [219]: dfg Out[219]: A 0 a 1 a 2 a 3 b 4 b 5 a In [220]: dfg.groupby("A").ngroup() Out[220]: 0 0 1 0 2 0 3 1 4 1 5 0 dtype: int64 In [221]: dfg.groupby("A").ngroup(ascending=False) Out[221]: 0 1 1 1 2 1 3 0 4 0 5 1 dtype: int64 Plotting# Groupby also works with some plotting methods. For example, suppose we suspect that some features in a DataFrame may differ by group, in this case, the values in column 1 where the group is “B” are 3 higher on average. In [222]: np.random.seed(1234) In [223]: df = pd.DataFrame(np.random.randn(50, 2)) In [224]: df["g"] = np.random.choice(["A", "B"], size=50) In [225]: df.loc[df["g"] == "B", 1] += 3 We can easily visualize this with a boxplot: In [226]: df.groupby("g").boxplot() Out[226]: A AxesSubplot(0.1,0.15;0.363636x0.75) B AxesSubplot(0.536364,0.15;0.363636x0.75) dtype: object The result of calling boxplot is a dictionary whose keys are the values of our grouping column g (“A” and “B”). The values of the resulting dictionary can be controlled by the return_type keyword of boxplot. See the visualization documentation for more. Warning For historical reasons, df.groupby("g").boxplot() is not equivalent to df.boxplot(by="g"). See here for an explanation. Piping function calls# Similar to the functionality provided by DataFrame and Series, functions that take GroupBy objects can be chained together using a pipe method to allow for a cleaner, more readable syntax. To read about .pipe in general terms, see here. Combining .groupby and .pipe is often useful when you need to reuse GroupBy objects. As an example, imagine having a DataFrame with columns for stores, products, revenue and quantity sold. We’d like to do a groupwise calculation of prices (i.e. revenue/quantity) per store and per product. We could do this in a multi-step operation, but expressing it in terms of piping can make the code more readable. First we set the data: In [227]: n = 1000 In [228]: df = pd.DataFrame( .....: { .....: "Store": np.random.choice(["Store_1", "Store_2"], n), .....: "Product": np.random.choice(["Product_1", "Product_2"], n), .....: "Revenue": (np.random.random(n) * 50 + 10).round(2), .....: "Quantity": np.random.randint(1, 10, size=n), .....: } .....: ) .....: In [229]: df.head(2) Out[229]: Store Product Revenue Quantity 0 Store_2 Product_1 26.12 1 1 Store_2 Product_1 28.86 1 Now, to find prices per store/product, we can simply do: In [230]: ( .....: df.groupby(["Store", "Product"]) .....: .pipe(lambda grp: grp.Revenue.sum() / grp.Quantity.sum()) .....: .unstack() .....: .round(2) .....: ) .....: Out[230]: Product Product_1 Product_2 Store Store_1 6.82 7.05 Store_2 6.30 6.64 Piping can also be expressive when you want to deliver a grouped object to some arbitrary function, for example: In [231]: def mean(groupby): .....: return groupby.mean() .....: In [232]: df.groupby(["Store", "Product"]).pipe(mean) Out[232]: Revenue Quantity Store Product Store_1 Product_1 34.622727 5.075758 Product_2 35.482815 5.029630 Store_2 Product_1 32.972837 5.237589 Product_2 34.684360 5.224000 where mean takes a GroupBy object and finds the mean of the Revenue and Quantity columns respectively for each Store-Product combination. The mean function can be any function that takes in a GroupBy object; the .pipe will pass the GroupBy object as a parameter into the function you specify. Examples# Regrouping by factor# Regroup columns of a DataFrame according to their sum, and sum the aggregated ones. In [233]: df = pd.DataFrame({"a": [1, 0, 0], "b": [0, 1, 0], "c": [1, 0, 0], "d": [2, 3, 4]}) In [234]: df Out[234]: a b c d 0 1 0 1 2 1 0 1 0 3 2 0 0 0 4 In [235]: df.groupby(df.sum(), axis=1).sum() Out[235]: 1 9 0 2 2 1 1 3 2 0 4 Multi-column factorization# By using ngroup(), we can extract information about the groups in a way similar to factorize() (as described further in the reshaping API) but which applies naturally to multiple columns of mixed type and different sources. This can be useful as an intermediate categorical-like step in processing, when the relationships between the group rows are more important than their content, or as input to an algorithm which only accepts the integer encoding. (For more information about support in pandas for full categorical data, see the Categorical introduction and the API documentation.) In [236]: dfg = pd.DataFrame({"A": [1, 1, 2, 3, 2], "B": list("aaaba")}) In [237]: dfg Out[237]: A B 0 1 a 1 1 a 2 2 a 3 3 b 4 2 a In [238]: dfg.groupby(["A", "B"]).ngroup() Out[238]: 0 0 1 0 2 1 3 2 4 1 dtype: int64 In [239]: dfg.groupby(["A", [0, 0, 0, 1, 1]]).ngroup() Out[239]: 0 0 1 0 2 1 3 3 4 2 dtype: int64 Groupby by indexer to ‘resample’ data# Resampling produces new hypothetical samples (resamples) from already existing observed data or from a model that generates data. These new samples are similar to the pre-existing samples. In order to resample to work on indices that are non-datetimelike, the following procedure can be utilized. In the following examples, df.index // 5 returns a binary array which is used to determine what gets selected for the groupby operation. Note The below example shows how we can downsample by consolidation of samples into fewer samples. Here by using df.index // 5, we are aggregating the samples in bins. By applying std() function, we aggregate the information contained in many samples into a small subset of values which is their standard deviation thereby reducing the number of samples. In [240]: df = pd.DataFrame(np.random.randn(10, 2)) In [241]: df Out[241]: 0 1 0 -0.793893 0.321153 1 0.342250 1.618906 2 -0.975807 1.918201 3 -0.810847 -1.405919 4 -1.977759 0.461659 5 0.730057 -1.316938 6 -0.751328 0.528290 7 -0.257759 -1.081009 8 0.505895 -1.701948 9 -1.006349 0.020208 In [242]: df.index // 5 Out[242]: Int64Index([0, 0, 0, 0, 0, 1, 1, 1, 1, 1], dtype='int64') In [243]: df.groupby(df.index // 5).std() Out[243]: 0 1 0 0.823647 1.312912 1 0.760109 0.942941 Returning a Series to propagate names# Group DataFrame columns, compute a set of metrics and return a named Series. The Series name is used as the name for the column index. This is especially useful in conjunction with reshaping operations such as stacking in which the column index name will be used as the name of the inserted column: In [244]: df = pd.DataFrame( .....: { .....: "a": [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], .....: "b": [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1], .....: "c": [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], .....: "d": [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1], .....: } .....: ) .....: In [245]: def compute_metrics(x): .....: result = {"b_sum": x["b"].sum(), "c_mean": x["c"].mean()} .....: return pd.Series(result, name="metrics") .....: In [246]: result = df.groupby("a").apply(compute_metrics) In [247]: result Out[247]: metrics b_sum c_mean a 0 2.0 0.5 1 2.0 0.5 2 2.0 0.5 In [248]: result.stack() Out[248]: a metrics 0 b_sum 2.0 c_mean 0.5 1 b_sum 2.0 c_mean 0.5 2 b_sum 2.0 c_mean 0.5 dtype: float64
975
1,228
Groupby, sort and join I have this data: What I want to do is group this data by name, sort by number and join by the inter_type column. So I want to have this kind of output: I don't know how to do it. I know how to join them by group, but how to sort them? I have no idea how to implement here sort_values()? df.groupby(['name'])['inter_type'].apply(lambda x: ','.join(x)).reset_index()
float64 /
Just use sort_value in the beginning on the number column. Also I modified your code to use agg with ','.join without lambda directly, it's more efficient and also it's shorter. Code: df.sort_values('number').groupby('name')['inter_type'].agg(','.join)
65,568,995
Collapse multiple timestamp rows into a single one
<p>I have a series like that:</p> <pre><code>s = pd.DataFrame({'ts': [1, 2, 3, 6, 7, 11, 12, 13]}) s ts 0 1 1 2 2 3 3 6 4 7 5 11 6 12 7 13 </code></pre> <p>I would like to collapse rows that have difference less than MAX_DIFF (2). That means that the desired output must be:</p> <pre><code>[{'ts_from': 1, 'ts_to': 3}, {'ts_from': 6, 'ts_to': 7}, {'ts_from': 11, 'ts_to': 13}] </code></pre> <p>I did some coding:</p> <pre><code>s['close'] = s.diff().shift(-1) s['close'] = s[s['close'] &gt; MAX_DIFF].astype('bool') s['close'].iloc[-1] = True parts = [] ts_from = None for _, row in s.iterrows(): if row['close'] is True: part = {'ts_from': ts_from, 'ts_to': row['ts']} parts.append(part) ts_from = None continue if not ts_from: ts_from = row['ts'] </code></pre> <p>This works but does not seem optimal because of iterrows(). I thought about ranks but couldn't figure out how to implement them so as to groupby rank further.</p> <p>Is there way to optimes algorithm?</p>
65,569,094
"2021-01-04T19:55:43.110000"
1
1
2
88
python|pandas
<p>You can create groups by checking where the difference is more than your threshold and take a cumsum. Then agg however you'd like, perhaps <code>first</code> and <code>last</code> in this case.</p> <pre><code>gp = s['ts'].diff().abs().ge(2).cumsum().rename(None) res = s.groupby(gp).agg(ts_from=('ts', 'first'), ts_to=('ts', 'last')) # ts_from ts_to #0 1 3 #1 6 7 #2 11 13 </code></pre> <p>And if you want the list of dicts then:</p> <pre><code>res.to_dict('records') #[{'ts_from': 1, 'ts_to': 3}, # {'ts_from': 6, 'ts_to': 7}, # {'ts_from': 11, 'ts_to': 13}] </code></pre> <hr /> <p>For completeness here is how the grouper aligns with the DataFrame:</p> <pre><code>s['gp'] = gp print(s) ts gp 0 1 0 # `1` becomes ts_from for group 0 1 2 0 2 3 0 # `3` becomes ts_to for group 0 3 6 1 # `6` becomes ts_from for group 1 4 7 1 # `7` becomes ts_to for group 1 5 11 2 # `11` becomes ts_from for group 2 6 12 2 7 13 2 # `13` becomes ts_to for group 2 </code></pre>
"2021-01-04T20:03:20.217000"
2
https://pandas.pydata.org/docs/getting_started/intro_tutorials/09_timeseries.html
How to handle time series data with ease?# In [1]: import pandas as pd In [2]: import matplotlib.pyplot as plt Data used for this tutorial: Air quality data For this tutorial, air quality data about \(NO_2\) and Particulate You can create groups by checking where the difference is more than your threshold and take a cumsum. Then agg however you'd like, perhaps first and last in this case. gp = s['ts'].diff().abs().ge(2).cumsum().rename(None) res = s.groupby(gp).agg(ts_from=('ts', 'first'), ts_to=('ts', 'last')) # ts_from ts_to #0 1 3 #1 6 7 #2 11 13 And if you want the list of dicts then: res.to_dict('records') #[{'ts_from': 1, 'ts_to': 3}, # {'ts_from': 6, 'ts_to': 7}, # {'ts_from': 11, 'ts_to': 13}] For completeness here is how the grouper aligns with the DataFrame: s['gp'] = gp print(s) ts gp 0 1 0 # `1` becomes ts_from for group 0 1 2 0 2 3 0 # `3` becomes ts_to for group 0 3 6 1 # `6` becomes ts_from for group 1 4 7 1 # `7` becomes ts_to for group 1 5 11 2 # `11` becomes ts_from for group 2 6 12 2 7 13 2 # `13` becomes ts_to for group 2 matter less than 2.5 micrometers is used, made available by OpenAQ and downloaded using the py-openaq package. The air_quality_no2_long.csv" data set provides \(NO_2\) values for the measurement stations FR04014, BETR801 and London Westminster in respectively Paris, Antwerp and London. To raw data In [3]: air_quality = pd.read_csv("data/air_quality_no2_long.csv") In [4]: air_quality = air_quality.rename(columns={"date.utc": "datetime"}) In [5]: air_quality.head() Out[5]: city country datetime location parameter value unit 0 Paris FR 2019-06-21 00:00:00+00:00 FR04014 no2 20.0 µg/m³ 1 Paris FR 2019-06-20 23:00:00+00:00 FR04014 no2 21.8 µg/m³ 2 Paris FR 2019-06-20 22:00:00+00:00 FR04014 no2 26.5 µg/m³ 3 Paris FR 2019-06-20 21:00:00+00:00 FR04014 no2 24.9 µg/m³ 4 Paris FR 2019-06-20 20:00:00+00:00 FR04014 no2 21.4 µg/m³ In [6]: air_quality.city.unique() Out[6]: array(['Paris', 'Antwerpen', 'London'], dtype=object) How to handle time series data with ease?# Using pandas datetime properties# I want to work with the dates in the column datetime as datetime objects instead of plain text In [7]: air_quality["datetime"] = pd.to_datetime(air_quality["datetime"]) In [8]: air_quality["datetime"] Out[8]: 0 2019-06-21 00:00:00+00:00 1 2019-06-20 23:00:00+00:00 2 2019-06-20 22:00:00+00:00 3 2019-06-20 21:00:00+00:00 4 2019-06-20 20:00:00+00:00 ... 2063 2019-05-07 06:00:00+00:00 2064 2019-05-07 04:00:00+00:00 2065 2019-05-07 03:00:00+00:00 2066 2019-05-07 02:00:00+00:00 2067 2019-05-07 01:00:00+00:00 Name: datetime, Length: 2068, dtype: datetime64[ns, UTC] Initially, the values in datetime are character strings and do not provide any datetime operations (e.g. extract the year, day of the week,…). By applying the to_datetime function, pandas interprets the strings and convert these to datetime (i.e. datetime64[ns, UTC]) objects. In pandas we call these datetime objects similar to datetime.datetime from the standard library as pandas.Timestamp. Note As many data sets do contain datetime information in one of the columns, pandas input function like pandas.read_csv() and pandas.read_json() can do the transformation to dates when reading the data using the parse_dates parameter with a list of the columns to read as Timestamp: pd.read_csv("../data/air_quality_no2_long.csv", parse_dates=["datetime"]) Why are these pandas.Timestamp objects useful? Let’s illustrate the added value with some example cases. What is the start and end date of the time series data set we are working with? In [9]: air_quality["datetime"].min(), air_quality["datetime"].max() Out[9]: (Timestamp('2019-05-07 01:00:00+0000', tz='UTC'), Timestamp('2019-06-21 00:00:00+0000', tz='UTC')) Using pandas.Timestamp for datetimes enables us to calculate with date information and make them comparable. Hence, we can use this to get the length of our time series: In [10]: air_quality["datetime"].max() - air_quality["datetime"].min() Out[10]: Timedelta('44 days 23:00:00') The result is a pandas.Timedelta object, similar to datetime.timedelta from the standard Python library and defining a time duration. To user guideThe various time concepts supported by pandas are explained in the user guide section on time related concepts. I want to add a new column to the DataFrame containing only the month of the measurement In [11]: air_quality["month"] = air_quality["datetime"].dt.month In [12]: air_quality.head() Out[12]: city country datetime ... value unit month 0 Paris FR 2019-06-21 00:00:00+00:00 ... 20.0 µg/m³ 6 1 Paris FR 2019-06-20 23:00:00+00:00 ... 21.8 µg/m³ 6 2 Paris FR 2019-06-20 22:00:00+00:00 ... 26.5 µg/m³ 6 3 Paris FR 2019-06-20 21:00:00+00:00 ... 24.9 µg/m³ 6 4 Paris FR 2019-06-20 20:00:00+00:00 ... 21.4 µg/m³ 6 [5 rows x 8 columns] By using Timestamp objects for dates, a lot of time-related properties are provided by pandas. For example the month, but also year, weekofyear, quarter,… All of these properties are accessible by the dt accessor. To user guideAn overview of the existing date properties is given in the time and date components overview table. More details about the dt accessor to return datetime like properties are explained in a dedicated section on the dt accessor. What is the average \(NO_2\) concentration for each day of the week for each of the measurement locations? In [13]: air_quality.groupby( ....: [air_quality["datetime"].dt.weekday, "location"])["value"].mean() ....: Out[13]: datetime location 0 BETR801 27.875000 FR04014 24.856250 London Westminster 23.969697 1 BETR801 22.214286 FR04014 30.999359 ... 5 FR04014 25.266154 London Westminster 24.977612 6 BETR801 21.896552 FR04014 23.274306 London Westminster 24.859155 Name: value, Length: 21, dtype: float64 Remember the split-apply-combine pattern provided by groupby from the tutorial on statistics calculation? Here, we want to calculate a given statistic (e.g. mean \(NO_2\)) for each weekday and for each measurement location. To group on weekdays, we use the datetime property weekday (with Monday=0 and Sunday=6) of pandas Timestamp, which is also accessible by the dt accessor. The grouping on both locations and weekdays can be done to split the calculation of the mean on each of these combinations. Danger As we are working with a very short time series in these examples, the analysis does not provide a long-term representative result! Plot the typical \(NO_2\) pattern during the day of our time series of all stations together. In other words, what is the average value for each hour of the day? In [14]: fig, axs = plt.subplots(figsize=(12, 4)) In [15]: air_quality.groupby(air_quality["datetime"].dt.hour)["value"].mean().plot( ....: kind='bar', rot=0, ax=axs ....: ) ....: Out[15]: <AxesSubplot: xlabel='datetime'> In [16]: plt.xlabel("Hour of the day"); # custom x label using Matplotlib In [17]: plt.ylabel("$NO_2 (µg/m^3)$"); Similar to the previous case, we want to calculate a given statistic (e.g. mean \(NO_2\)) for each hour of the day and we can use the split-apply-combine approach again. For this case, we use the datetime property hour of pandas Timestamp, which is also accessible by the dt accessor. Datetime as index# In the tutorial on reshaping, pivot() was introduced to reshape the data table with each of the measurements locations as a separate column: In [18]: no_2 = air_quality.pivot(index="datetime", columns="location", values="value") In [19]: no_2.head() Out[19]: location BETR801 FR04014 London Westminster datetime 2019-05-07 01:00:00+00:00 50.5 25.0 23.0 2019-05-07 02:00:00+00:00 45.0 27.7 19.0 2019-05-07 03:00:00+00:00 NaN 50.4 19.0 2019-05-07 04:00:00+00:00 NaN 61.9 16.0 2019-05-07 05:00:00+00:00 NaN 72.4 NaN Note By pivoting the data, the datetime information became the index of the table. In general, setting a column as an index can be achieved by the set_index function. Working with a datetime index (i.e. DatetimeIndex) provides powerful functionalities. For example, we do not need the dt accessor to get the time series properties, but have these properties available on the index directly: In [20]: no_2.index.year, no_2.index.weekday Out[20]: (Int64Index([2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, ... 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019], dtype='int64', name='datetime', length=1033), Int64Index([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ... 3, 3, 3, 3, 3, 3, 3, 3, 3, 4], dtype='int64', name='datetime', length=1033)) Some other advantages are the convenient subsetting of time period or the adapted time scale on plots. Let’s apply this on our data. Create a plot of the \(NO_2\) values in the different stations from the 20th of May till the end of 21st of May In [21]: no_2["2019-05-20":"2019-05-21"].plot(); By providing a string that parses to a datetime, a specific subset of the data can be selected on a DatetimeIndex. To user guideMore information on the DatetimeIndex and the slicing by using strings is provided in the section on time series indexing. Resample a time series to another frequency# Aggregate the current hourly time series values to the monthly maximum value in each of the stations. In [22]: monthly_max = no_2.resample("M").max() In [23]: monthly_max Out[23]: location BETR801 FR04014 London Westminster datetime 2019-05-31 00:00:00+00:00 74.5 97.0 97.0 2019-06-30 00:00:00+00:00 52.5 84.7 52.0 A very powerful method on time series data with a datetime index, is the ability to resample() time series to another frequency (e.g., converting secondly data into 5-minutely data). The resample() method is similar to a groupby operation: it provides a time-based grouping, by using a string (e.g. M, 5H,…) that defines the target frequency it requires an aggregation function such as mean, max,… To user guideAn overview of the aliases used to define time series frequencies is given in the offset aliases overview table. When defined, the frequency of the time series is provided by the freq attribute: In [24]: monthly_max.index.freq Out[24]: <MonthEnd> Make a plot of the daily mean \(NO_2\) value in each of the stations. In [25]: no_2.resample("D").mean().plot(style="-o", figsize=(10, 5)); To user guideMore details on the power of time series resampling is provided in the user guide section on resampling. REMEMBER Valid date strings can be converted to datetime objects using to_datetime function or as part of read functions. Datetime objects in pandas support calculations, logical operations and convenient date-related properties using the dt accessor. A DatetimeIndex contains these date-related properties and supports convenient slicing. Resample is a powerful method to change the frequency of a time series. To user guideA full overview on time series is given on the pages on time series and date functionality.
239
1,195
Collapse multiple timestamp rows into a single one I have a series like that: s = pd.DataFrame({'ts': [1, 2, 3, 6, 7, 11, 12, 13]}) s ts 0 1 1 2 2 3 3 6 4 7 5 11 6 12 7 13 I would like to collapse rows that have difference less than MAX_DIFF (2). That means that the desired output must be: [{'ts_from': 1, 'ts_to': 3}, {'ts_from': 6, 'ts_to': 7}, {'ts_from': 11, 'ts_to': 13}] I did some coding: s['close'] = s.diff().shift(-1) s['close'] = s[s['close'] > MAX_DIFF].astype('bool') s['close'].iloc[-1] = True parts = [] ts_from = None for _, row in s.iterrows(): if row['close'] is True: part = {'ts_from': ts_from, 'ts_to': row['ts']} parts.append(part) ts_from = None continue if not ts_from: ts_from = row['ts'] This works but does not seem optimal because of iterrows(). I thought about ranks but couldn't figure out how to implement them so as to groupby rank further. Is there way to optimes algorithm?
You can create groups by checking where the difference is more than your threshold and take a cumsum. Then agg however you'd like, perhaps first and last in this case. gp = s['ts'].diff().abs().ge(2).cumsum().rename(None) res = s.groupby(gp).agg(ts_from=('ts', 'first'), ts_to=('ts', 'last')) # ts_from ts_to #0 1 3 #1 6 7 #2 11 13 And if you want the list of dicts then: res.to_dict('records') #[{'ts_from': 1, 'ts_to': 3}, # {'ts_from': 6, 'ts_to': 7}, # {'ts_from': 11, 'ts_to': 13}] For completeness here is how the grouper aligns with the DataFrame: s['gp'] = gp print(s) ts gp 0 1 0 # `1` becomes ts_from for group 0 1 2 0 2 3 0 # `3` becomes ts_to for group 0 3 6 1 # `6` becomes ts_from for group 1 4 7 1 # `7` becomes ts_to for group 1 5 11 2 # `11` becomes ts_from for group 2 6 12 2 7 13 2 # `13` becomes ts_to for group 2
66,418,955
how to correctly check if dataframe column has "False" in it
<p>I have a dataframe with boolean values. If any of the values is <code>False</code>, I want to do something. I have 2 options to do so:</p> <pre><code>In [1]: import pandas as pd In [2]: df = pd.DataFrame(data = [{'bool': False}, {'bool': True}]) In [3]: df Out[3]: bool 0 False 1 True In [4]: if (df['bool'] is False).any(): ...: print ('yay!!!!') ...: --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) &lt;ipython-input-4-0c98c5d85cb6&gt; in &lt;module&gt; ----&gt; 1 if (df['bool'] is False).any(): 2 print ('yay!!!!') 3 AttributeError: 'bool' object has no attribute 'any' In [5]: if (df['bool'] == False).any(): ...: print ('yay!!!!') ...: yay!!!! </code></pre> <p>As you can see, the 1st (<code>is</code>) fails, and the 2nd (<code>==</code>) succeeds. However, when running pre-commit (version 1.20.0) on this, I get the following error:</p> <pre><code>E712 comparison to False should be 'if cond is not False:' or 'if cond:' </code></pre> <p>which reverts me back to the 1st solution (which doesn't work)</p> <p>How can I solve this?</p> <pre><code>Python 3.6.12 pandas==1.1.5 </code></pre>
66,418,969
"2021-03-01T08:42:16.677000"
1
null
1
1,161
python|pandas
<p>You can test if not all values are <code>True</code>s by test <code>Series</code> by <a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.all.html" rel="nofollow noreferrer"><code>Series.all</code></a> and then invert scalar by <code>not</code>:</p> <pre><code>if not df['bool'].all(): print ('yay!!!!') yay!!!! </code></pre> <p>If want invert Series:</p> <pre><code>if (~df['bool']).any(): print ('yay!!!!') yay!!!! </code></pre> <p>In pandas working with <code>1d</code> (<code>Series</code>) or <code>2d</code> (<code>DataFrame</code>) data, so for test is not used <code>is</code> , which is not used for <a href="https://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is">check for identity of arrays</a>.</p>
"2021-03-01T08:43:56.720000"
2
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.any.html
pandas.DataFrame.any# pandas.DataFrame.any# DataFrame.any(*, axis=0, bool_only=None, skipna=True, level=None, **kwargs)[source]# Return whether any element is True, potentially over an axis. Returns False unless there is at least one element within a series or along a Dataframe axis that is True or equivalent (e.g. non-zero or non-empty). Parameters axis{0 or ‘index’, 1 or ‘columns’, None}, default 0Indicate which axis or axes should be reduced. For Series this parameter You can test if not all values are Trues by test Series by Series.all and then invert scalar by not: if not df['bool'].all(): print ('yay!!!!') yay!!!! If want invert Series: if (~df['bool']).any(): print ('yay!!!!') yay!!!! In pandas working with 1d (Series) or 2d (DataFrame) data, so for test is not used is , which is not used for check for identity of arrays. is unused and defaults to 0. 0 / ‘index’ : reduce the index, return a Series whose index is the original column labels. 1 / ‘columns’ : reduce the columns, return a Series whose index is the original index. None : reduce all axes, return a scalar. bool_onlybool, default NoneInclude only boolean columns. If None, will attempt to use everything, then use only boolean data. Not implemented for Series. skipnabool, default TrueExclude NA/null values. If the entire row/column is NA and skipna is True, then the result will be False, as for an empty row/column. If skipna is False, then NA are treated as True, because these are not equal to zero. levelint or level name, default NoneIf the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series. Deprecated since version 1.3.0: The level keyword is deprecated. Use groupby instead. **kwargsany, default NoneAdditional keywords have no effect but might be accepted for compatibility with NumPy. Returns Series or DataFrameIf level is specified, then, DataFrame is returned; otherwise, Series is returned. See also numpy.anyNumpy version of this method. Series.anyReturn whether any element is True. Series.allReturn whether all elements are True. DataFrame.anyReturn whether any element is True over requested axis. DataFrame.allReturn whether all elements are True over requested axis. Examples Series For Series input, the output is a scalar indicating whether any element is True. >>> pd.Series([False, False]).any() False >>> pd.Series([True, False]).any() True >>> pd.Series([], dtype="float64").any() False >>> pd.Series([np.nan]).any() False >>> pd.Series([np.nan]).any(skipna=False) True DataFrame Whether each column contains at least one True element (the default). >>> df = pd.DataFrame({"A": [1, 2], "B": [0, 2], "C": [0, 0]}) >>> df A B C 0 1 0 0 1 2 2 0 >>> df.any() A True B True C False dtype: bool Aggregating over the columns. >>> df = pd.DataFrame({"A": [True, False], "B": [1, 2]}) >>> df A B 0 True 1 1 False 2 >>> df.any(axis='columns') 0 True 1 True dtype: bool >>> df = pd.DataFrame({"A": [True, False], "B": [1, 0]}) >>> df A B 0 True 1 1 False 0 >>> df.any(axis='columns') 0 True 1 False dtype: bool Aggregating over the entire DataFrame with axis=None. >>> df.any(axis=None) True any for an empty DataFrame is an empty Series. >>> pd.DataFrame([]).any() Series([], dtype: bool)
482
856
how to correctly check if dataframe column has "False" in it I have a dataframe with boolean values. If any of the values is False, I want to do something. I have 2 options to do so: In [1]: import pandas as pd In [2]: df = pd.DataFrame(data = [{'bool': False}, {'bool': True}]) In [3]: df Out[3]: bool 0 False 1 True In [4]: if (df['bool'] is False).any(): ...: print ('yay!!!!') ...: --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-4-0c98c5d85cb6> in <module> ----> 1 if (df['bool'] is False).any(): 2 print ('yay!!!!') 3 AttributeError: 'bool' object has no attribute 'any' In [5]: if (df['bool'] == False).any(): ...: print ('yay!!!!') ...: yay!!!! As you can see, the 1st (is) fails, and the 2nd (==) succeeds. However, when running pre-commit (version 1.20.0) on this, I get the following error: E712 comparison to False should be 'if cond is not False:' or 'if cond:' which reverts me back to the 1st solution (which doesn't work) How can I solve this? Python 3.6.12 pandas==1.1.5
You can test if not all values are Trues by test Series by Series.all and then invert scalar by not: if not df['bool'].all(): print ('yay!!!!') yay!!!! If want invert Series: if (~df['bool']).any(): print ('yay!!!!') yay!!!! In pandas working with 1d (Series) or 2d (DataFrame) data, so for test is not used is , which is not used for check for identity of arrays.
62,822,125
Python - Assign a value from one pandas df to another based on a string appearing in text
<p>Here is an example of my data:</p> <pre><code>import pandas as pd data = {'Text':['This is an example,', 'Another sentence is here.', 'Lets have fun.', 'this happened weeks ago.', 'I am not sure what to put here.', 'Another fake sentence.'], 'Score':[20, 21, 19, 18, 16, 12]} # Create DataFrame df = pd.DataFrame(data) data_words = {'words':['is', 'fun', 'happened', 'example'], 'frequency':[127, 112, 1234, 32]} # Create DataFrame df2 = pd.DataFrame(data_words) #Final Result: data_result = {'words':['is', 'fun', 'happened', 'example'], 'frequency':[127, 112, 1234, 32], 'Text': ['This is an example,', 'Lets have fun.', 'this happened weeks ago.', 'This is an example,']} df_final = pd.DataFrame(data_result) </code></pre> <p>I am trying to match the <code>df['text']</code> with the <code>df2['words']</code> based on whether the word appears in the text. I just need one text per word, and ideally it would be based on <code>&quot;Score&quot;</code> but it's not completely necessary.</p> <p>So, the final <code>df</code> would have columns: <code>&quot;Text&quot;</code>, <code>&quot;Score&quot;</code>, <code>&quot;words&quot;</code>, and <code>&quot;frequency&quot;</code></p>
62,822,396
"2020-07-09T19:17:45.233000"
1
2
2
177
python|pandas
<p>Simple list comprehension between the two dataframes and take the first occurence with <code>[0]</code></p> <pre><code>df2['Text'] = df2['words'].apply(lambda x: [y for y in df['Text'] if x in y][0]) </code></pre> <p>output:</p> <pre><code> words frequency Text 0 is 127 This is an example, 1 fun 112 Lets have fun. 2 happened 1234 this happened weeks ago. 3 example 32 This is an example, </code></pre> <p>Explaining the list comprehension, I am returning the value &quot;y&quot; while searching for &quot;x&quot; in &quot;y&quot; where x is each row for <code>words</code> and y is each row for <code>text</code>. This returns a list of all matches per row. Some rows had multiple values in the list, since multiple matches, so per your expected output I added a <code>[0]</code> to the end in order to take the first value that was returned in each list for the list comprehension that was applied row-by-row with lambda x. Otherwise, without the <code>[0]</code>, a list of all matches would be returned.</p>
"2020-07-09T19:35:04.470000"
2
https://pandas.pydata.org/docs/dev/user_guide/merging.html
Merge, join, concatenate and compare# Merge, join, concatenate and compare# pandas provides various facilities for easily combining together Series or DataFrame with various kinds of set logic for the indexes and relational algebra functionality in the case of join / merge-type Simple list comprehension between the two dataframes and take the first occurence with [0] df2['Text'] = df2['words'].apply(lambda x: [y for y in df['Text'] if x in y][0]) output: words frequency Text 0 is 127 This is an example, 1 fun 112 Lets have fun. 2 happened 1234 this happened weeks ago. 3 example 32 This is an example, Explaining the list comprehension, I am returning the value "y" while searching for "x" in "y" where x is each row for words and y is each row for text. This returns a list of all matches per row. Some rows had multiple values in the list, since multiple matches, so per your expected output I added a [0] to the end in order to take the first value that was returned in each list for the list comprehension that was applied row-by-row with lambda x. Otherwise, without the [0], a list of all matches would be returned. operations. In addition, pandas also provides utilities to compare two Series or DataFrame and summarize their differences. Concatenating objects# The concat() function (in the main pandas namespace) does all of the heavy lifting of performing concatenation operations along an axis while performing optional set logic (union or intersection) of the indexes (if any) on the other axes. Note that I say “if any” because there is only a single possible axis of concatenation for Series. Before diving into all of the details of concat and what it can do, here is a simple example: In [1]: df1 = pd.DataFrame( ...: { ...: "A": ["A0", "A1", "A2", "A3"], ...: "B": ["B0", "B1", "B2", "B3"], ...: "C": ["C0", "C1", "C2", "C3"], ...: "D": ["D0", "D1", "D2", "D3"], ...: }, ...: index=[0, 1, 2, 3], ...: ) ...: In [2]: df2 = pd.DataFrame( ...: { ...: "A": ["A4", "A5", "A6", "A7"], ...: "B": ["B4", "B5", "B6", "B7"], ...: "C": ["C4", "C5", "C6", "C7"], ...: "D": ["D4", "D5", "D6", "D7"], ...: }, ...: index=[4, 5, 6, 7], ...: ) ...: In [3]: df3 = pd.DataFrame( ...: { ...: "A": ["A8", "A9", "A10", "A11"], ...: "B": ["B8", "B9", "B10", "B11"], ...: "C": ["C8", "C9", "C10", "C11"], ...: "D": ["D8", "D9", "D10", "D11"], ...: }, ...: index=[8, 9, 10, 11], ...: ) ...: In [4]: frames = [df1, df2, df3] In [5]: result = pd.concat(frames) Like its sibling function on ndarrays, numpy.concatenate, pandas.concat takes a list or dict of homogeneously-typed objects and concatenates them with some configurable handling of “what to do with the other axes”: pd.concat( objs, axis=0, join="outer", ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, copy=True, ) objs : a sequence or mapping of Series or DataFrame objects. If a dict is passed, the sorted keys will be used as the keys argument, unless it is passed, in which case the values will be selected (see below). Any None objects will be dropped silently unless they are all None in which case a ValueError will be raised. axis : {0, 1, …}, default 0. The axis to concatenate along. join : {‘inner’, ‘outer’}, default ‘outer’. How to handle indexes on other axis(es). Outer for union and inner for intersection. ignore_index : boolean, default False. If True, do not use the index values on the concatenation axis. The resulting axis will be labeled 0, …, n - 1. This is useful if you are concatenating objects where the concatenation axis does not have meaningful indexing information. Note the index values on the other axes are still respected in the join. keys : sequence, default None. Construct hierarchical index using the passed keys as the outermost level. If multiple levels passed, should contain tuples. levels : list of sequences, default None. Specific levels (unique values) to use for constructing a MultiIndex. Otherwise they will be inferred from the keys. names : list, default None. Names for the levels in the resulting hierarchical index. verify_integrity : boolean, default False. Check whether the new concatenated axis contains duplicates. This can be very expensive relative to the actual data concatenation. copy : boolean, default True. If False, do not copy data unnecessarily. Without a little bit of context many of these arguments don’t make much sense. Let’s revisit the above example. Suppose we wanted to associate specific keys with each of the pieces of the chopped up DataFrame. We can do this using the keys argument: In [6]: result = pd.concat(frames, keys=["x", "y", "z"]) As you can see (if you’ve read the rest of the documentation), the resulting object’s index has a hierarchical index. This means that we can now select out each chunk by key: In [7]: result.loc["y"] Out[7]: A B C D 4 A4 B4 C4 D4 5 A5 B5 C5 D5 6 A6 B6 C6 D6 7 A7 B7 C7 D7 It’s not a stretch to see how this can be very useful. More detail on this functionality below. Note It is worth noting that concat() makes a full copy of the data, and that constantly reusing this function can create a significant performance hit. If you need to use the operation over several datasets, use a list comprehension. frames = [ process_your_file(f) for f in files ] result = pd.concat(frames) Note When concatenating DataFrames with named axes, pandas will attempt to preserve these index/column names whenever possible. In the case where all inputs share a common name, this name will be assigned to the result. When the input names do not all agree, the result will be unnamed. The same is true for MultiIndex, but the logic is applied separately on a level-by-level basis. Set logic on the other axes# When gluing together multiple DataFrames, you have a choice of how to handle the other axes (other than the one being concatenated). This can be done in the following two ways: Take the union of them all, join='outer'. This is the default option as it results in zero information loss. Take the intersection, join='inner'. Here is an example of each of these methods. First, the default join='outer' behavior: In [8]: df4 = pd.DataFrame( ...: { ...: "B": ["B2", "B3", "B6", "B7"], ...: "D": ["D2", "D3", "D6", "D7"], ...: "F": ["F2", "F3", "F6", "F7"], ...: }, ...: index=[2, 3, 6, 7], ...: ) ...: In [9]: result = pd.concat([df1, df4], axis=1) Here is the same thing with join='inner': In [10]: result = pd.concat([df1, df4], axis=1, join="inner") Lastly, suppose we just wanted to reuse the exact index from the original DataFrame: In [11]: result = pd.concat([df1, df4], axis=1).reindex(df1.index) Similarly, we could index before the concatenation: In [12]: pd.concat([df1, df4.reindex(df1.index)], axis=1) Out[12]: A B C D B D F 0 A0 B0 C0 D0 NaN NaN NaN 1 A1 B1 C1 D1 NaN NaN NaN 2 A2 B2 C2 D2 B2 D2 F2 3 A3 B3 C3 D3 B3 D3 F3 Ignoring indexes on the concatenation axis# For DataFrame objects which don’t have a meaningful index, you may wish to append them and ignore the fact that they may have overlapping indexes. To do this, use the ignore_index argument: In [13]: result = pd.concat([df1, df4], ignore_index=True, sort=False) Concatenating with mixed ndims# You can concatenate a mix of Series and DataFrame objects. The Series will be transformed to DataFrame with the column name as the name of the Series. In [14]: s1 = pd.Series(["X0", "X1", "X2", "X3"], name="X") In [15]: result = pd.concat([df1, s1], axis=1) Note Since we’re concatenating a Series to a DataFrame, we could have achieved the same result with DataFrame.assign(). To concatenate an arbitrary number of pandas objects (DataFrame or Series), use concat. If unnamed Series are passed they will be numbered consecutively. In [16]: s2 = pd.Series(["_0", "_1", "_2", "_3"]) In [17]: result = pd.concat([df1, s2, s2, s2], axis=1) Passing ignore_index=True will drop all name references. In [18]: result = pd.concat([df1, s1], axis=1, ignore_index=True) More concatenating with group keys# A fairly common use of the keys argument is to override the column names when creating a new DataFrame based on existing Series. Notice how the default behaviour consists on letting the resulting DataFrame inherit the parent Series’ name, when these existed. In [19]: s3 = pd.Series([0, 1, 2, 3], name="foo") In [20]: s4 = pd.Series([0, 1, 2, 3]) In [21]: s5 = pd.Series([0, 1, 4, 5]) In [22]: pd.concat([s3, s4, s5], axis=1) Out[22]: foo 0 1 0 0 0 0 1 1 1 1 2 2 2 4 3 3 3 5 Through the keys argument we can override the existing column names. In [23]: pd.concat([s3, s4, s5], axis=1, keys=["red", "blue", "yellow"]) Out[23]: red blue yellow 0 0 0 0 1 1 1 1 2 2 2 4 3 3 3 5 Let’s consider a variation of the very first example presented: In [24]: result = pd.concat(frames, keys=["x", "y", "z"]) You can also pass a dict to concat in which case the dict keys will be used for the keys argument (unless other keys are specified): In [25]: pieces = {"x": df1, "y": df2, "z": df3} In [26]: result = pd.concat(pieces) In [27]: result = pd.concat(pieces, keys=["z", "y"]) The MultiIndex created has levels that are constructed from the passed keys and the index of the DataFrame pieces: In [28]: result.index.levels Out[28]: FrozenList([['z', 'y'], [4, 5, 6, 7, 8, 9, 10, 11]]) If you wish to specify other levels (as will occasionally be the case), you can do so using the levels argument: In [29]: result = pd.concat( ....: pieces, keys=["x", "y", "z"], levels=[["z", "y", "x", "w"]], names=["group_key"] ....: ) ....: In [30]: result.index.levels Out[30]: FrozenList([['z', 'y', 'x', 'w'], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]) This is fairly esoteric, but it is actually necessary for implementing things like GroupBy where the order of a categorical variable is meaningful. Appending rows to a DataFrame# If you have a series that you want to append as a single row to a DataFrame, you can convert the row into a DataFrame and use concat In [31]: s2 = pd.Series(["X0", "X1", "X2", "X3"], index=["A", "B", "C", "D"]) In [32]: result = pd.concat([df1, s2.to_frame().T], ignore_index=True) You should use ignore_index with this method to instruct DataFrame to discard its index. If you wish to preserve the index, you should construct an appropriately-indexed DataFrame and append or concatenate those objects. Database-style DataFrame or named Series joining/merging# pandas has full-featured, high performance in-memory join operations idiomatically very similar to relational databases like SQL. These methods perform significantly better (in some cases well over an order of magnitude better) than other open source implementations (like base::merge.data.frame in R). The reason for this is careful algorithmic design and the internal layout of the data in DataFrame. See the cookbook for some advanced strategies. Users who are familiar with SQL but new to pandas might be interested in a comparison with SQL. pandas provides a single function, merge(), as the entry point for all standard database join operations between DataFrame or named Series objects: pd.merge( left, right, how="inner", on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=True, suffixes=("_x", "_y"), copy=True, indicator=False, validate=None, ) left: A DataFrame or named Series object. right: Another DataFrame or named Series object. on: Column or index level names to join on. Must be found in both the left and right DataFrame and/or Series objects. If not passed and left_index and right_index are False, the intersection of the columns in the DataFrames and/or Series will be inferred to be the join keys. left_on: Columns or index levels from the left DataFrame or Series to use as keys. Can either be column names, index level names, or arrays with length equal to the length of the DataFrame or Series. right_on: Columns or index levels from the right DataFrame or Series to use as keys. Can either be column names, index level names, or arrays with length equal to the length of the DataFrame or Series. left_index: If True, use the index (row labels) from the left DataFrame or Series as its join key(s). In the case of a DataFrame or Series with a MultiIndex (hierarchical), the number of levels must match the number of join keys from the right DataFrame or Series. right_index: Same usage as left_index for the right DataFrame or Series how: One of 'left', 'right', 'outer', 'inner', 'cross'. Defaults to inner. See below for more detailed description of each method. sort: Sort the result DataFrame by the join keys in lexicographical order. Defaults to True, setting to False will improve performance substantially in many cases. suffixes: A tuple of string suffixes to apply to overlapping columns. Defaults to ('_x', '_y'). copy: Always copy data (default True) from the passed DataFrame or named Series objects, even when reindexing is not necessary. Cannot be avoided in many cases but may improve performance / memory usage. The cases where copying can be avoided are somewhat pathological but this option is provided nonetheless. indicator: Add a column to the output DataFrame called _merge with information on the source of each row. _merge is Categorical-type and takes on a value of left_only for observations whose merge key only appears in 'left' DataFrame or Series, right_only for observations whose merge key only appears in 'right' DataFrame or Series, and both if the observation’s merge key is found in both. validate : string, default None. If specified, checks if merge is of specified type. “one_to_one” or “1:1”: checks if merge keys are unique in both left and right datasets. “one_to_many” or “1:m”: checks if merge keys are unique in left dataset. “many_to_one” or “m:1”: checks if merge keys are unique in right dataset. “many_to_many” or “m:m”: allowed, but does not result in checks. Note Support for specifying index levels as the on, left_on, and right_on parameters was added in version 0.23.0. Support for merging named Series objects was added in version 0.24.0. The return type will be the same as left. If left is a DataFrame or named Series and right is a subclass of DataFrame, the return type will still be DataFrame. merge is a function in the pandas namespace, and it is also available as a DataFrame instance method merge(), with the calling DataFrame being implicitly considered the left object in the join. The related join() method, uses merge internally for the index-on-index (by default) and column(s)-on-index join. If you are joining on index only, you may wish to use DataFrame.join to save yourself some typing. Brief primer on merge methods (relational algebra)# Experienced users of relational databases like SQL will be familiar with the terminology used to describe join operations between two SQL-table like structures (DataFrame objects). There are several cases to consider which are very important to understand: one-to-one joins: for example when joining two DataFrame objects on their indexes (which must contain unique values). many-to-one joins: for example when joining an index (unique) to one or more columns in a different DataFrame. many-to-many joins: joining columns on columns. Note When joining columns on columns (potentially a many-to-many join), any indexes on the passed DataFrame objects will be discarded. It is worth spending some time understanding the result of the many-to-many join case. In SQL / standard relational algebra, if a key combination appears more than once in both tables, the resulting table will have the Cartesian product of the associated data. Here is a very basic example with one unique key combination: In [33]: left = pd.DataFrame( ....: { ....: "key": ["K0", "K1", "K2", "K3"], ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: } ....: ) ....: In [34]: right = pd.DataFrame( ....: { ....: "key": ["K0", "K1", "K2", "K3"], ....: "C": ["C0", "C1", "C2", "C3"], ....: "D": ["D0", "D1", "D2", "D3"], ....: } ....: ) ....: In [35]: result = pd.merge(left, right, on="key") Here is a more complicated example with multiple join keys. Only the keys appearing in left and right are present (the intersection), since how='inner' by default. In [36]: left = pd.DataFrame( ....: { ....: "key1": ["K0", "K0", "K1", "K2"], ....: "key2": ["K0", "K1", "K0", "K1"], ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: } ....: ) ....: In [37]: right = pd.DataFrame( ....: { ....: "key1": ["K0", "K1", "K1", "K2"], ....: "key2": ["K0", "K0", "K0", "K0"], ....: "C": ["C0", "C1", "C2", "C3"], ....: "D": ["D0", "D1", "D2", "D3"], ....: } ....: ) ....: In [38]: result = pd.merge(left, right, on=["key1", "key2"]) The how argument to merge specifies how to determine which keys are to be included in the resulting table. If a key combination does not appear in either the left or right tables, the values in the joined table will be NA. Here is a summary of the how options and their SQL equivalent names: Merge method SQL Join Name Description left LEFT OUTER JOIN Use keys from left frame only right RIGHT OUTER JOIN Use keys from right frame only outer FULL OUTER JOIN Use union of keys from both frames inner INNER JOIN Use intersection of keys from both frames cross CROSS JOIN Create the cartesian product of rows of both frames In [39]: result = pd.merge(left, right, how="left", on=["key1", "key2"]) In [40]: result = pd.merge(left, right, how="right", on=["key1", "key2"]) In [41]: result = pd.merge(left, right, how="outer", on=["key1", "key2"]) In [42]: result = pd.merge(left, right, how="inner", on=["key1", "key2"]) In [43]: result = pd.merge(left, right, how="cross") You can merge a mult-indexed Series and a DataFrame, if the names of the MultiIndex correspond to the columns from the DataFrame. Transform the Series to a DataFrame using Series.reset_index() before merging, as shown in the following example. In [44]: df = pd.DataFrame({"Let": ["A", "B", "C"], "Num": [1, 2, 3]}) In [45]: df Out[45]: Let Num 0 A 1 1 B 2 2 C 3 In [46]: ser = pd.Series( ....: ["a", "b", "c", "d", "e", "f"], ....: index=pd.MultiIndex.from_arrays( ....: [["A", "B", "C"] * 2, [1, 2, 3, 4, 5, 6]], names=["Let", "Num"] ....: ), ....: ) ....: In [47]: ser Out[47]: Let Num A 1 a B 2 b C 3 c A 4 d B 5 e C 6 f dtype: object In [48]: pd.merge(df, ser.reset_index(), on=["Let", "Num"]) Out[48]: Let Num 0 0 A 1 a 1 B 2 b 2 C 3 c Here is another example with duplicate join keys in DataFrames: In [49]: left = pd.DataFrame({"A": [1, 2], "B": [2, 2]}) In [50]: right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]}) In [51]: result = pd.merge(left, right, on="B", how="outer") Warning Joining / merging on duplicate keys can cause a returned frame that is the multiplication of the row dimensions, which may result in memory overflow. It is the user’ s responsibility to manage duplicate values in keys before joining large DataFrames. Checking for duplicate keys# Users can use the validate argument to automatically check whether there are unexpected duplicates in their merge keys. Key uniqueness is checked before merge operations and so should protect against memory overflows. Checking key uniqueness is also a good way to ensure user data structures are as expected. In the following example, there are duplicate values of B in the right DataFrame. As this is not a one-to-one merge – as specified in the validate argument – an exception will be raised. In [52]: left = pd.DataFrame({"A": [1, 2], "B": [1, 2]}) In [53]: right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]}) In [53]: result = pd.merge(left, right, on="B", how="outer", validate="one_to_one") ... MergeError: Merge keys are not unique in right dataset; not a one-to-one merge If the user is aware of the duplicates in the right DataFrame but wants to ensure there are no duplicates in the left DataFrame, one can use the validate='one_to_many' argument instead, which will not raise an exception. In [54]: pd.merge(left, right, on="B", how="outer", validate="one_to_many") Out[54]: A_x B A_y 0 1 1 NaN 1 2 2 4.0 2 2 2 5.0 3 2 2 6.0 The merge indicator# merge() accepts the argument indicator. If True, a Categorical-type column called _merge will be added to the output object that takes on values: Observation Origin _merge value Merge key only in 'left' frame left_only Merge key only in 'right' frame right_only Merge key in both frames both In [55]: df1 = pd.DataFrame({"col1": [0, 1], "col_left": ["a", "b"]}) In [56]: df2 = pd.DataFrame({"col1": [1, 2, 2], "col_right": [2, 2, 2]}) In [57]: pd.merge(df1, df2, on="col1", how="outer", indicator=True) Out[57]: col1 col_left col_right _merge 0 0 a NaN left_only 1 1 b 2.0 both 2 2 NaN 2.0 right_only 3 2 NaN 2.0 right_only The indicator argument will also accept string arguments, in which case the indicator function will use the value of the passed string as the name for the indicator column. In [58]: pd.merge(df1, df2, on="col1", how="outer", indicator="indicator_column") Out[58]: col1 col_left col_right indicator_column 0 0 a NaN left_only 1 1 b 2.0 both 2 2 NaN 2.0 right_only 3 2 NaN 2.0 right_only Merge dtypes# Merging will preserve the dtype of the join keys. In [59]: left = pd.DataFrame({"key": [1], "v1": [10]}) In [60]: left Out[60]: key v1 0 1 10 In [61]: right = pd.DataFrame({"key": [1, 2], "v1": [20, 30]}) In [62]: right Out[62]: key v1 0 1 20 1 2 30 We are able to preserve the join keys: In [63]: pd.merge(left, right, how="outer") Out[63]: key v1 0 1 10 1 1 20 2 2 30 In [64]: pd.merge(left, right, how="outer").dtypes Out[64]: key int64 v1 int64 dtype: object Of course if you have missing values that are introduced, then the resulting dtype will be upcast. In [65]: pd.merge(left, right, how="outer", on="key") Out[65]: key v1_x v1_y 0 1 10.0 20 1 2 NaN 30 In [66]: pd.merge(left, right, how="outer", on="key").dtypes Out[66]: key int64 v1_x float64 v1_y int64 dtype: object Merging will preserve category dtypes of the mergands. See also the section on categoricals. The left frame. In [67]: from pandas.api.types import CategoricalDtype In [68]: X = pd.Series(np.random.choice(["foo", "bar"], size=(10,))) In [69]: X = X.astype(CategoricalDtype(categories=["foo", "bar"])) In [70]: left = pd.DataFrame( ....: {"X": X, "Y": np.random.choice(["one", "two", "three"], size=(10,))} ....: ) ....: In [71]: left Out[71]: X Y 0 bar one 1 foo one 2 foo three 3 bar three 4 foo one 5 bar one 6 bar three 7 bar three 8 bar three 9 foo three In [72]: left.dtypes Out[72]: X category Y object dtype: object The right frame. In [73]: right = pd.DataFrame( ....: { ....: "X": pd.Series(["foo", "bar"], dtype=CategoricalDtype(["foo", "bar"])), ....: "Z": [1, 2], ....: } ....: ) ....: In [74]: right Out[74]: X Z 0 foo 1 1 bar 2 In [75]: right.dtypes Out[75]: X category Z int64 dtype: object The merged result: In [76]: result = pd.merge(left, right, how="outer") In [77]: result Out[77]: X Y Z 0 bar one 2 1 bar three 2 2 bar one 2 3 bar three 2 4 bar three 2 5 bar three 2 6 foo one 1 7 foo three 1 8 foo one 1 9 foo three 1 In [78]: result.dtypes Out[78]: X category Y object Z int64 dtype: object Note The category dtypes must be exactly the same, meaning the same categories and the ordered attribute. Otherwise the result will coerce to the categories’ dtype. Note Merging on category dtypes that are the same can be quite performant compared to object dtype merging. Joining on index# DataFrame.join() is a convenient method for combining the columns of two potentially differently-indexed DataFrames into a single result DataFrame. Here is a very basic example: In [79]: left = pd.DataFrame( ....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=["K0", "K1", "K2"] ....: ) ....: In [80]: right = pd.DataFrame( ....: {"C": ["C0", "C2", "C3"], "D": ["D0", "D2", "D3"]}, index=["K0", "K2", "K3"] ....: ) ....: In [81]: result = left.join(right) In [82]: result = left.join(right, how="outer") The same as above, but with how='inner'. In [83]: result = left.join(right, how="inner") The data alignment here is on the indexes (row labels). This same behavior can be achieved using merge plus additional arguments instructing it to use the indexes: In [84]: result = pd.merge(left, right, left_index=True, right_index=True, how="outer") In [85]: result = pd.merge(left, right, left_index=True, right_index=True, how="inner") Joining key columns on an index# join() takes an optional on argument which may be a column or multiple column names, which specifies that the passed DataFrame is to be aligned on that column in the DataFrame. These two function calls are completely equivalent: left.join(right, on=key_or_keys) pd.merge( left, right, left_on=key_or_keys, right_index=True, how="left", sort=False ) Obviously you can choose whichever form you find more convenient. For many-to-one joins (where one of the DataFrame’s is already indexed by the join key), using join may be more convenient. Here is a simple example: In [86]: left = pd.DataFrame( ....: { ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: "key": ["K0", "K1", "K0", "K1"], ....: } ....: ) ....: In [87]: right = pd.DataFrame({"C": ["C0", "C1"], "D": ["D0", "D1"]}, index=["K0", "K1"]) In [88]: result = left.join(right, on="key") In [89]: result = pd.merge( ....: left, right, left_on="key", right_index=True, how="left", sort=False ....: ) ....: To join on multiple keys, the passed DataFrame must have a MultiIndex: In [90]: left = pd.DataFrame( ....: { ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: "key1": ["K0", "K0", "K1", "K2"], ....: "key2": ["K0", "K1", "K0", "K1"], ....: } ....: ) ....: In [91]: index = pd.MultiIndex.from_tuples( ....: [("K0", "K0"), ("K1", "K0"), ("K2", "K0"), ("K2", "K1")] ....: ) ....: In [92]: right = pd.DataFrame( ....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=index ....: ) ....: Now this can be joined by passing the two key column names: In [93]: result = left.join(right, on=["key1", "key2"]) The default for DataFrame.join is to perform a left join (essentially a “VLOOKUP” operation, for Excel users), which uses only the keys found in the calling DataFrame. Other join types, for example inner join, can be just as easily performed: In [94]: result = left.join(right, on=["key1", "key2"], how="inner") As you can see, this drops any rows where there was no match. Joining a single Index to a MultiIndex# You can join a singly-indexed DataFrame with a level of a MultiIndexed DataFrame. The level will match on the name of the index of the singly-indexed frame against a level name of the MultiIndexed frame. In [95]: left = pd.DataFrame( ....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, ....: index=pd.Index(["K0", "K1", "K2"], name="key"), ....: ) ....: In [96]: index = pd.MultiIndex.from_tuples( ....: [("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")], ....: names=["key", "Y"], ....: ) ....: In [97]: right = pd.DataFrame( ....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, ....: index=index, ....: ) ....: In [98]: result = left.join(right, how="inner") This is equivalent but less verbose and more memory efficient / faster than this. In [99]: result = pd.merge( ....: left.reset_index(), right.reset_index(), on=["key"], how="inner" ....: ).set_index(["key","Y"]) ....: Joining with two MultiIndexes# This is supported in a limited way, provided that the index for the right argument is completely used in the join, and is a subset of the indices in the left argument, as in this example: In [100]: leftindex = pd.MultiIndex.from_product( .....: [list("abc"), list("xy"), [1, 2]], names=["abc", "xy", "num"] .....: ) .....: In [101]: left = pd.DataFrame({"v1": range(12)}, index=leftindex) In [102]: left Out[102]: v1 abc xy num a x 1 0 2 1 y 1 2 2 3 b x 1 4 2 5 y 1 6 2 7 c x 1 8 2 9 y 1 10 2 11 In [103]: rightindex = pd.MultiIndex.from_product( .....: [list("abc"), list("xy")], names=["abc", "xy"] .....: ) .....: In [104]: right = pd.DataFrame({"v2": [100 * i for i in range(1, 7)]}, index=rightindex) In [105]: right Out[105]: v2 abc xy a x 100 y 200 b x 300 y 400 c x 500 y 600 In [106]: left.join(right, on=["abc", "xy"], how="inner") Out[106]: v1 v2 abc xy num a x 1 0 100 2 1 100 y 1 2 200 2 3 200 b x 1 4 300 2 5 300 y 1 6 400 2 7 400 c x 1 8 500 2 9 500 y 1 10 600 2 11 600 If that condition is not satisfied, a join with two multi-indexes can be done using the following code. In [107]: leftindex = pd.MultiIndex.from_tuples( .....: [("K0", "X0"), ("K0", "X1"), ("K1", "X2")], names=["key", "X"] .....: ) .....: In [108]: left = pd.DataFrame( .....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=leftindex .....: ) .....: In [109]: rightindex = pd.MultiIndex.from_tuples( .....: [("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")], names=["key", "Y"] .....: ) .....: In [110]: right = pd.DataFrame( .....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=rightindex .....: ) .....: In [111]: result = pd.merge( .....: left.reset_index(), right.reset_index(), on=["key"], how="inner" .....: ).set_index(["key", "X", "Y"]) .....: Merging on a combination of columns and index levels# Strings passed as the on, left_on, and right_on parameters may refer to either column names or index level names. This enables merging DataFrame instances on a combination of index levels and columns without resetting indexes. In [112]: left_index = pd.Index(["K0", "K0", "K1", "K2"], name="key1") In [113]: left = pd.DataFrame( .....: { .....: "A": ["A0", "A1", "A2", "A3"], .....: "B": ["B0", "B1", "B2", "B3"], .....: "key2": ["K0", "K1", "K0", "K1"], .....: }, .....: index=left_index, .....: ) .....: In [114]: right_index = pd.Index(["K0", "K1", "K2", "K2"], name="key1") In [115]: right = pd.DataFrame( .....: { .....: "C": ["C0", "C1", "C2", "C3"], .....: "D": ["D0", "D1", "D2", "D3"], .....: "key2": ["K0", "K0", "K0", "K1"], .....: }, .....: index=right_index, .....: ) .....: In [116]: result = left.merge(right, on=["key1", "key2"]) Note When DataFrames are merged on a string that matches an index level in both frames, the index level is preserved as an index level in the resulting DataFrame. Note When DataFrames are merged using only some of the levels of a MultiIndex, the extra levels will be dropped from the resulting merge. In order to preserve those levels, use reset_index on those level names to move those levels to columns prior to doing the merge. Note If a string matches both a column name and an index level name, then a warning is issued and the column takes precedence. This will result in an ambiguity error in a future version. Overlapping value columns# The merge suffixes argument takes a tuple of list of strings to append to overlapping column names in the input DataFrames to disambiguate the result columns: In [117]: left = pd.DataFrame({"k": ["K0", "K1", "K2"], "v": [1, 2, 3]}) In [118]: right = pd.DataFrame({"k": ["K0", "K0", "K3"], "v": [4, 5, 6]}) In [119]: result = pd.merge(left, right, on="k") In [120]: result = pd.merge(left, right, on="k", suffixes=("_l", "_r")) DataFrame.join() has lsuffix and rsuffix arguments which behave similarly. In [121]: left = left.set_index("k") In [122]: right = right.set_index("k") In [123]: result = left.join(right, lsuffix="_l", rsuffix="_r") Joining multiple DataFrames# A list or tuple of DataFrames can also be passed to join() to join them together on their indexes. In [124]: right2 = pd.DataFrame({"v": [7, 8, 9]}, index=["K1", "K1", "K2"]) In [125]: result = left.join([right, right2]) Merging together values within Series or DataFrame columns# Another fairly common situation is to have two like-indexed (or similarly indexed) Series or DataFrame objects and wanting to “patch” values in one object from values for matching indices in the other. Here is an example: In [126]: df1 = pd.DataFrame( .....: [[np.nan, 3.0, 5.0], [-4.6, np.nan, np.nan], [np.nan, 7.0, np.nan]] .....: ) .....: In [127]: df2 = pd.DataFrame([[-42.6, np.nan, -8.2], [-5.0, 1.6, 4]], index=[1, 2]) For this, use the combine_first() method: In [128]: result = df1.combine_first(df2) Note that this method only takes values from the right DataFrame if they are missing in the left DataFrame. A related method, update(), alters non-NA values in place: In [129]: df1.update(df2) Timeseries friendly merging# Merging ordered data# A merge_ordered() function allows combining time series and other ordered data. In particular it has an optional fill_method keyword to fill/interpolate missing data: In [130]: left = pd.DataFrame( .....: {"k": ["K0", "K1", "K1", "K2"], "lv": [1, 2, 3, 4], "s": ["a", "b", "c", "d"]} .....: ) .....: In [131]: right = pd.DataFrame({"k": ["K1", "K2", "K4"], "rv": [1, 2, 3]}) In [132]: pd.merge_ordered(left, right, fill_method="ffill", left_by="s") Out[132]: k lv s rv 0 K0 1.0 a NaN 1 K1 1.0 a 1.0 2 K2 1.0 a 2.0 3 K4 1.0 a 3.0 4 K1 2.0 b 1.0 5 K2 2.0 b 2.0 6 K4 2.0 b 3.0 7 K1 3.0 c 1.0 8 K2 3.0 c 2.0 9 K4 3.0 c 3.0 10 K1 NaN d 1.0 11 K2 4.0 d 2.0 12 K4 4.0 d 3.0 Merging asof# A merge_asof() is similar to an ordered left-join except that we match on nearest key rather than equal keys. For each row in the left DataFrame, we select the last row in the right DataFrame whose on key is less than the left’s key. Both DataFrames must be sorted by the key. Optionally an asof merge can perform a group-wise merge. This matches the by key equally, in addition to the nearest match on the on key. For example; we might have trades and quotes and we want to asof merge them. In [133]: trades = pd.DataFrame( .....: { .....: "time": pd.to_datetime( .....: [ .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.038", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.048", .....: ] .....: ), .....: "ticker": ["MSFT", "MSFT", "GOOG", "GOOG", "AAPL"], .....: "price": [51.95, 51.95, 720.77, 720.92, 98.00], .....: "quantity": [75, 155, 100, 100, 100], .....: }, .....: columns=["time", "ticker", "price", "quantity"], .....: ) .....: In [134]: quotes = pd.DataFrame( .....: { .....: "time": pd.to_datetime( .....: [ .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.030", .....: "20160525 13:30:00.041", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.049", .....: "20160525 13:30:00.072", .....: "20160525 13:30:00.075", .....: ] .....: ), .....: "ticker": ["GOOG", "MSFT", "MSFT", "MSFT", "GOOG", "AAPL", "GOOG", "MSFT"], .....: "bid": [720.50, 51.95, 51.97, 51.99, 720.50, 97.99, 720.50, 52.01], .....: "ask": [720.93, 51.96, 51.98, 52.00, 720.93, 98.01, 720.88, 52.03], .....: }, .....: columns=["time", "ticker", "bid", "ask"], .....: ) .....: In [135]: trades Out[135]: time ticker price quantity 0 2016-05-25 13:30:00.023 MSFT 51.95 75 1 2016-05-25 13:30:00.038 MSFT 51.95 155 2 2016-05-25 13:30:00.048 GOOG 720.77 100 3 2016-05-25 13:30:00.048 GOOG 720.92 100 4 2016-05-25 13:30:00.048 AAPL 98.00 100 In [136]: quotes Out[136]: time ticker bid ask 0 2016-05-25 13:30:00.023 GOOG 720.50 720.93 1 2016-05-25 13:30:00.023 MSFT 51.95 51.96 2 2016-05-25 13:30:00.030 MSFT 51.97 51.98 3 2016-05-25 13:30:00.041 MSFT 51.99 52.00 4 2016-05-25 13:30:00.048 GOOG 720.50 720.93 5 2016-05-25 13:30:00.049 AAPL 97.99 98.01 6 2016-05-25 13:30:00.072 GOOG 720.50 720.88 7 2016-05-25 13:30:00.075 MSFT 52.01 52.03 By default we are taking the asof of the quotes. In [137]: pd.merge_asof(trades, quotes, on="time", by="ticker") Out[137]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 51.95 51.96 1 2016-05-25 13:30:00.038 MSFT 51.95 155 51.97 51.98 2 2016-05-25 13:30:00.048 GOOG 720.77 100 720.50 720.93 3 2016-05-25 13:30:00.048 GOOG 720.92 100 720.50 720.93 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN We only asof within 2ms between the quote time and the trade time. In [138]: pd.merge_asof(trades, quotes, on="time", by="ticker", tolerance=pd.Timedelta("2ms")) Out[138]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 51.95 51.96 1 2016-05-25 13:30:00.038 MSFT 51.95 155 NaN NaN 2 2016-05-25 13:30:00.048 GOOG 720.77 100 720.50 720.93 3 2016-05-25 13:30:00.048 GOOG 720.92 100 720.50 720.93 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN We only asof within 10ms between the quote time and the trade time and we exclude exact matches on time. Note that though we exclude the exact matches (of the quotes), prior quotes do propagate to that point in time. In [139]: pd.merge_asof( .....: trades, .....: quotes, .....: on="time", .....: by="ticker", .....: tolerance=pd.Timedelta("10ms"), .....: allow_exact_matches=False, .....: ) .....: Out[139]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 NaN NaN 1 2016-05-25 13:30:00.038 MSFT 51.95 155 51.97 51.98 2 2016-05-25 13:30:00.048 GOOG 720.77 100 NaN NaN 3 2016-05-25 13:30:00.048 GOOG 720.92 100 NaN NaN 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN Comparing objects# The compare() and compare() methods allow you to compare two DataFrame or Series, respectively, and summarize their differences. This feature was added in V1.1.0. For example, you might want to compare two DataFrame and stack their differences side by side. In [140]: df = pd.DataFrame( .....: { .....: "col1": ["a", "a", "b", "b", "a"], .....: "col2": [1.0, 2.0, 3.0, np.nan, 5.0], .....: "col3": [1.0, 2.0, 3.0, 4.0, 5.0], .....: }, .....: columns=["col1", "col2", "col3"], .....: ) .....: In [141]: df Out[141]: col1 col2 col3 0 a 1.0 1.0 1 a 2.0 2.0 2 b 3.0 3.0 3 b NaN 4.0 4 a 5.0 5.0 In [142]: df2 = df.copy() In [143]: df2.loc[0, "col1"] = "c" In [144]: df2.loc[2, "col3"] = 4.0 In [145]: df2 Out[145]: col1 col2 col3 0 c 1.0 1.0 1 a 2.0 2.0 2 b 3.0 4.0 3 b NaN 4.0 4 a 5.0 5.0 In [146]: df.compare(df2) Out[146]: col1 col3 self other self other 0 a c NaN NaN 2 NaN NaN 3.0 4.0 By default, if two corresponding values are equal, they will be shown as NaN. Furthermore, if all values in an entire row / column, the row / column will be omitted from the result. The remaining differences will be aligned on columns. If you wish, you may choose to stack the differences on rows. In [147]: df.compare(df2, align_axis=0) Out[147]: col1 col3 0 self a NaN other c NaN 2 self NaN 3.0 other NaN 4.0 If you wish to keep all original rows and columns, set keep_shape argument to True. In [148]: df.compare(df2, keep_shape=True) Out[148]: col1 col2 col3 self other self other self other 0 a c NaN NaN NaN NaN 1 NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN 3.0 4.0 3 NaN NaN NaN NaN NaN NaN 4 NaN NaN NaN NaN NaN NaN You may also keep all the original values even if they are equal. In [149]: df.compare(df2, keep_shape=True, keep_equal=True) Out[149]: col1 col2 col3 self other self other self other 0 a c 1.0 1.0 1.0 1.0 1 a a 2.0 2.0 2.0 2.0 2 b b 3.0 3.0 3.0 4.0 3 b b NaN NaN 4.0 4.0 4 a a 5.0 5.0 5.0 5.0
281
1,207
Python - Assign a value from one pandas df to another based on a string appearing in text Here is an example of my data: import pandas as pd data = {'Text':['This is an example,', 'Another sentence is here.', 'Lets have fun.', 'this happened weeks ago.', 'I am not sure what to put here.', 'Another fake sentence.'], 'Score':[20, 21, 19, 18, 16, 12]} # Create DataFrame df = pd.DataFrame(data) data_words = {'words':['is', 'fun', 'happened', 'example'], 'frequency':[127, 112, 1234, 32]} # Create DataFrame df2 = pd.DataFrame(data_words) #Final Result: data_result = {'words':['is', 'fun', 'happened', 'example'], 'frequency':[127, 112, 1234, 32], 'Text': ['This is an example,', 'Lets have fun.', 'this happened weeks ago.', 'This is an example,']} df_final = pd.DataFrame(data_result) I am trying to match the df['text'] with the df2['words'] based on whether the word appears in the text. I just need one text per word, and ideally it would be based on "Score" but it's not completely necessary. So, the final df would have columns: "Text", "Score", "words", and "frequency"
Simple list comprehension between the two dataframes and take the first occurence with [0] df2['Text'] = df2['words'].apply(lambda x: [y for y in df['Text'] if x in y][0]) output: words frequency Text 0 is 127 This is an example, 1 fun 112 Lets have fun. 2 happened 1234 this happened weeks ago. 3 example 32 This is an example, Explaining the list comprehension, I am returning the value "y" while searching for "x" in "y" where x is each row for words and y is each row for text. This returns a list of all matches per row. Some rows had multiple values in the list, since multiple matches, so per your expected output I added a [0] to the end in order to take the first value that was returned in each list for the list comprehension that was applied row-by-row with lambda x. Otherwise, without the [0], a list of all matches would be returned.
60,254,851
Access rightmost two columns of pandas dataframe, by number
<p>I'm hoping to overwrite some values in a <code>df</code> when a condition is met. Using the <code>df</code> below, when <code>col B</code> is equal to values in <code>lst</code>, I want to replace values in <code>col C,D</code> with <code>X</code>.</p> <p>This can achieved using the method below but I'm hoping to use indexing to select the last two columns, rather than using hard coded labels.</p> <pre><code>df = pd.DataFrame({ 'A' : [1,1,1,1,1,1,1,1], 'B' : ['X','Foo','X','Cat','A','A','X','D'], 'C' : [1,1,1,1,1,1,1,1], 'D' : [1,1,1,1,1,1,1,1], }) lst = ['Foo','Cat'] df.loc[df.B.isin(lst), ['C','D']] = 'X' </code></pre> <p>Attempt:</p> <pre><code>df.loc[df.B.isin(lst), df.loc[:-2]] = 'X' </code></pre> <p>Intended:</p> <pre><code> A B C D 0 1 X 1 1 1 1 Foo X X 2 1 X 1 1 3 1 Cat X X 4 1 A 1 1 5 1 A 1 1 6 1 X 1 1 7 1 D 1 1 </code></pre>
60,255,077
"2020-02-17T01:26:23.583000"
2
null
-1
186
python|pandas
<p>If I understood the question, it looks like you are searching for <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iloc.html" rel="nofollow noreferrer"><code>iloc</code></a>:</p> <pre><code>df.iloc[df.B.isin(lst).values, -2:] = 'X' </code></pre> <p>In most cases, <code>df.loc[df.B.isin(lst), -2:] = 'X'</code> will also return the same result, but the interpretation of <code>-2:</code> slice will vary if the column names are of integer type.</p>
"2020-02-17T02:08:08.810000"
2
https://pandas.pydata.org/docs/reference/api/pandas.Series.loc.html
pandas.Series.loc# pandas.Series.loc# property Series.loc[source]# Access a group of rows and columns by label(s) or a boolean array. .loc[] is primarily label based, but may also be used with a boolean array. Allowed inputs are: A single label, e.g. 5 or 'a', (note that 5 is interpreted as a label of the index, and never as an integer position along the index). A list or array of labels, e.g. ['a', 'b', 'c']. A slice object with labels, e.g. 'a':'f'. Warning Note that contrary to usual python slices, both the start and the stop are included A boolean array of the same length as the axis being sliced, e.g. [True, False, True]. An alignable boolean Series. The index of the key will be aligned before masking. An alignable Index. The Index of the returned selection will be the input. A callable function with one argument (the calling Series or DataFrame) and that returns valid output for indexing (one of the above) See more at Selection by Label. Raises KeyErrorIf any items are not found. If I understood the question, it looks like you are searching for iloc: df.iloc[df.B.isin(lst).values, -2:] = 'X' In most cases, df.loc[df.B.isin(lst), -2:] = 'X' will also return the same result, but the interpretation of -2: slice will vary if the column names are of integer type. IndexingErrorIf an indexed key is passed and its index is unalignable to the frame index. See also DataFrame.atAccess a single value for a row/column label pair. DataFrame.ilocAccess group of rows and columns by integer position(s). DataFrame.xsReturns a cross-section (row(s) or column(s)) from the Series/DataFrame. Series.locAccess group of values using labels. Examples Getting values >>> df = pd.DataFrame([[1, 2], [4, 5], [7, 8]], ... index=['cobra', 'viper', 'sidewinder'], ... columns=['max_speed', 'shield']) >>> df max_speed shield cobra 1 2 viper 4 5 sidewinder 7 8 Single label. Note this returns the row as a Series. >>> df.loc['viper'] max_speed 4 shield 5 Name: viper, dtype: int64 List of labels. Note using [[]] returns a DataFrame. >>> df.loc[['viper', 'sidewinder']] max_speed shield viper 4 5 sidewinder 7 8 Single label for row and column >>> df.loc['cobra', 'shield'] 2 Slice with labels for row and single label for column. As mentioned above, note that both the start and stop of the slice are included. >>> df.loc['cobra':'viper', 'max_speed'] cobra 1 viper 4 Name: max_speed, dtype: int64 Boolean list with the same length as the row axis >>> df.loc[[False, False, True]] max_speed shield sidewinder 7 8 Alignable boolean Series: >>> df.loc[pd.Series([False, True, False], ... index=['viper', 'sidewinder', 'cobra'])] max_speed shield sidewinder 7 8 Index (same behavior as df.reindex) >>> df.loc[pd.Index(["cobra", "viper"], name="foo")] max_speed shield foo cobra 1 2 viper 4 5 Conditional that returns a boolean Series >>> df.loc[df['shield'] > 6] max_speed shield sidewinder 7 8 Conditional that returns a boolean Series with column labels specified >>> df.loc[df['shield'] > 6, ['max_speed']] max_speed sidewinder 7 Callable that returns a boolean Series >>> df.loc[lambda df: df['shield'] == 8] max_speed shield sidewinder 7 8 Setting values Set value for all items matching the list of labels >>> df.loc[['viper', 'sidewinder'], ['shield']] = 50 >>> df max_speed shield cobra 1 2 viper 4 50 sidewinder 7 50 Set value for an entire row >>> df.loc['cobra'] = 10 >>> df max_speed shield cobra 10 10 viper 4 50 sidewinder 7 50 Set value for an entire column >>> df.loc[:, 'max_speed'] = 30 >>> df max_speed shield cobra 30 10 viper 30 50 sidewinder 30 50 Set value for rows matching callable condition >>> df.loc[df['shield'] > 35] = 0 >>> df max_speed shield cobra 30 10 viper 0 0 sidewinder 0 0 Getting values on a DataFrame with an index that has integer labels Another example using integers for the index >>> df = pd.DataFrame([[1, 2], [4, 5], [7, 8]], ... index=[7, 8, 9], columns=['max_speed', 'shield']) >>> df max_speed shield 7 1 2 8 4 5 9 7 8 Slice with integer labels for rows. As mentioned above, note that both the start and stop of the slice are included. >>> df.loc[7:9] max_speed shield 7 1 2 8 4 5 9 7 8 Getting values with a MultiIndex A number of examples using a DataFrame with a MultiIndex >>> tuples = [ ... ('cobra', 'mark i'), ('cobra', 'mark ii'), ... ('sidewinder', 'mark i'), ('sidewinder', 'mark ii'), ... ('viper', 'mark ii'), ('viper', 'mark iii') ... ] >>> index = pd.MultiIndex.from_tuples(tuples) >>> values = [[12, 2], [0, 4], [10, 20], ... [1, 4], [7, 1], [16, 36]] >>> df = pd.DataFrame(values, columns=['max_speed', 'shield'], index=index) >>> df max_speed shield cobra mark i 12 2 mark ii 0 4 sidewinder mark i 10 20 mark ii 1 4 viper mark ii 7 1 mark iii 16 36 Single label. Note this returns a DataFrame with a single index. >>> df.loc['cobra'] max_speed shield mark i 12 2 mark ii 0 4 Single index tuple. Note this returns a Series. >>> df.loc[('cobra', 'mark ii')] max_speed 0 shield 4 Name: (cobra, mark ii), dtype: int64 Single label for row and column. Similar to passing in a tuple, this returns a Series. >>> df.loc['cobra', 'mark i'] max_speed 12 shield 2 Name: (cobra, mark i), dtype: int64 Single tuple. Note using [[]] returns a DataFrame. >>> df.loc[[('cobra', 'mark ii')]] max_speed shield cobra mark ii 0 4 Single tuple for the index with a single label for the column >>> df.loc[('cobra', 'mark i'), 'shield'] 2 Slice from index tuple to single label >>> df.loc[('cobra', 'mark i'):'viper'] max_speed shield cobra mark i 12 2 mark ii 0 4 sidewinder mark i 10 20 mark ii 1 4 viper mark ii 7 1 mark iii 16 36 Slice from index tuple to index tuple >>> df.loc[('cobra', 'mark i'):('viper', 'mark ii')] max_speed shield cobra mark i 12 2 mark ii 0 4 sidewinder mark i 10 20 mark ii 1 4 viper mark ii 7 1 Please see the user guide for more details and explanations of advanced indexing.
1,012
1,296
Access rightmost two columns of pandas dataframe, by number I'm hoping to overwrite some values in a df when a condition is met. Using the df below, when col B is equal to values in lst, I want to replace values in col C,D with X. This can achieved using the method below but I'm hoping to use indexing to select the last two columns, rather than using hard coded labels. df = pd.DataFrame({ 'A' : [1,1,1,1,1,1,1,1], 'B' : ['X','Foo','X','Cat','A','A','X','D'], 'C' : [1,1,1,1,1,1,1,1], 'D' : [1,1,1,1,1,1,1,1], }) lst = ['Foo','Cat'] df.loc[df.B.isin(lst), ['C','D']] = 'X' Attempt: df.loc[df.B.isin(lst), df.loc[:-2]] = 'X' Intended: A B C D 0 1 X 1 1 1 1 Foo X X 2 1 X 1 1 3 1 Cat X X 4 1 A 1 1 5 1 A 1 1 6 1 X 1 1 7 1 D 1 1
If I understood the question, it looks like you are searching for iloc: df.iloc[df.B.isin(lst).values, -2:] = 'X' In most cases, df.loc[df.B.isin(lst), -2:] = 'X' will also return the same result, but the interpretation of -2: slice will vary if the column names are of integer type.
65,234,934
Replace NaN inside masked dataframe
<pre><code>some_dict = {'a': [1,2,3,4], 'b': [5,6,7,8],} df = pd.DataFrame(some_dict) mask1 = pd.Series([False, True, False, True]) df['c'] = df.loc[mask1, 'a'] </code></pre> <p>This will create a new column <code>c</code> with NaN-values where <code>mask1</code> is <code>False</code>, and the values from column <code>a</code> where <code>mask1</code> is <code>True</code>. Now I add <code>b</code> to <code>c</code>, on a different condition</p> <pre><code>mask2 = pd.Series([True, False, False, False]) df['c'] += df.loc[mask2, 'b'] </code></pre> <p>However, this will put ALL values to NaN. I'm guessing this is because <code>NaN + 0</code> and <code>0 + NaN</code> are both equal to <code>NaN</code>. I tried solving this by writing</p> <pre><code>df['c'] = df.loc[mask1, 'a'].fillna(0) df['c'] = df.loc[mask2, 'b'].fillna(0) </code></pre> <p>Why is replacing <code>NaN</code> with <code>0</code> through <code>fillna()</code> not working?</p>
65,235,092
"2020-12-10T12:53:44.320000"
2
null
1
704
python|pandas
<p>If check how it working missing values are added only for not matched rows - False valuss in mask:</p> <pre><code>print (df.loc[mask1, 'a']) 1 2 3 4 Name: a, dtype: int64 </code></pre> <p>So if want replace NaN there are no missing values, so cannot do it:</p> <pre><code>print (df.loc[mask1, 'a'].fillna(0)) 1 2 3 4 Name: a, dtype: int64 </code></pre> <p>If assign to column then not matched values has to be created too - and because pandas no idea what should be is created <code>NaN</code>s:</p> <pre><code>df['c'] = df.loc[mask1, 'a'] print (df) a b c 0 1 5 NaN &lt;- False 1 2 6 2.0 2 3 7 NaN &lt;- False 3 4 8 4.0 </code></pre> <p>So if need replace NaN to <code>0</code> need <a href="https://numpy.org/doc/stable/reference/generated/numpy.where.html" rel="nofollow noreferrer"><code>numpy.where</code></a> - if <code>True</code>s is passed values from <code>a</code> if <code>False</code> is passed <code>0</code>:</p> <pre><code>df['c'] = np.where(mask1, df['a'], 0) print (df) a b c 0 1 5 0 1 2 6 2 2 3 7 0 3 4 8 4 </code></pre> <p>Another pandas alternative is <a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.where.html" rel="nofollow noreferrer"><code>Series.where</code></a>:</p> <pre><code>df['c'] = df['a'].where(mask1, 0) print (df) a b c 0 1 5 0 1 2 6 2 2 3 7 0 3 4 8 4 </code></pre> <p>All together:</p> <pre><code>some_dict = {'a': [1,2,3,4], 'b': [5,6,7,8],} df = pd.DataFrame(some_dict) mask1 = pd.Series([False, True, False, True]) df['c'] = np.where(mask1, df['a'], 0) mask2 = pd.Series([True, False, False, False]) df['c'] += np.where(mask2, df['b'], 0) print (df) a b c 0 1 5 5 1 2 6 2 2 3 7 0 3 4 8 4 </code></pre> <p>Another idea is use <a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.add.html" rel="nofollow noreferrer"><code>Series.add</code></a> with <code>fill_value=0</code>:</p> <pre><code>some_dict = {'a': [1,2,3,4], 'b': [5,6,7,8],} df = pd.DataFrame(some_dict) mask1 = pd.Series([False, True, False, True]) df['c'] = df.loc[mask1, 'a'] print (df) a b c 0 1 5 NaN 1 2 6 2.0 2 3 7 NaN 3 4 8 4.0 mask2 = pd.Series([True, False, False, False]) df['c'] = df['c'].add(df.loc[mask2, 'b'], fill_value=0) print (df) a b c 0 1 5 5.0 1 2 6 2.0 2 3 7 NaN 3 4 8 4.0 </code></pre>
"2020-12-10T13:03:17.253000"
2
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.mask.html
If check how it working missing values are added only for not matched rows - False valuss in mask: print (df.loc[mask1, 'a']) 1 2 3 4 Name: a, dtype: int64 So if want replace NaN there are no missing values, so cannot do it: print (df.loc[mask1, 'a'].fillna(0)) 1 2 3 4 Name: a, dtype: int64 If assign to column then not matched values has to be created too - and because pandas no idea what should be is created NaNs: df['c'] = df.loc[mask1, 'a'] print (df) a b c 0 1 5 NaN <- False 1 2 6 2.0 2 3 7 NaN <- False 3 4 8 4.0 So if need replace NaN to 0 need numpy.where - if Trues is passed values from a if False is passed 0: df['c'] = np.where(mask1, df['a'], 0) print (df) a b c 0 1 5 0 1 2 6 2 2 3 7 0 3 4 8 4 Another pandas alternative is Series.where: df['c'] = df['a'].where(mask1, 0) print (df) a b c 0 1 5 0 1 2 6 2 2 3 7 0 3 4 8 4 All together: some_dict = {'a': [1,2,3,4], 'b': [5,6,7,8],} df = pd.DataFrame(some_dict) mask1 = pd.Series([False, True, False, True]) df['c'] = np.where(mask1, df['a'], 0) mask2 = pd.Series([True, False, False, False]) df['c'] += np.where(mask2, df['b'], 0) print (df) a b c 0 1 5 5 1 2 6 2 2 3 7 0 3 4 8 4 Another idea is use Series.add with fill_value=0: some_dict = {'a': [1,2,3,4], 'b': [5,6,7,8],} df = pd.DataFrame(some_dict) mask1 = pd.Series([False, True, False, True]) df['c'] = df.loc[mask1, 'a'] print (df) a b c 0 1 5 NaN 1 2 6 2.0 2 3 7 NaN 3 4 8 4.0 mask2 = pd.Series([True, False, False, False]) df['c'] = df['c'].add(df.loc[mask2, 'b'], fill_value=0) print (df) a b c 0 1 5 5.0 1 2 6 2.0 2 3 7 NaN 3 4 8 4.0
0
1,698
Replace NaN inside masked dataframe some_dict = {'a': [1,2,3,4], 'b': [5,6,7,8],} df = pd.DataFrame(some_dict) mask1 = pd.Series([False, True, False, True]) df['c'] = df.loc[mask1, 'a'] This will create a new column c with NaN-values where mask1 is False, and the values from column a where mask1 is True. Now I add b to c, on a different condition mask2 = pd.Series([True, False, False, False]) df['c'] += df.loc[mask2, 'b'] However, this will put ALL values to NaN. I'm guessing this is because NaN + 0 and 0 + NaN are both equal to NaN. I tried solving this by writing df['c'] = df.loc[mask1, 'a'].fillna(0) df['c'] = df.loc[mask2, 'b'].fillna(0) Why is replacing NaN with 0 through fillna() not working?
If check how it working missing values are added only for not matched rows - False valuss in mask: print (df.loc[mask1, 'a']) 1 2 3 4 Name: a, dtype: int64 So if want replace NaN there are no missing values, so cannot do it: print (df.loc[mask1, 'a'].fillna(0)) 1 2 3 4 Name: a, dtype: int64 If assign to column then not matched values has to be created too - and because pandas no idea what should be is created NaNs: df['c'] = df.loc[mask1, 'a'] print (df) a b c 0 1 5 NaN <- False 1 2 6 2.0 2 3 7 NaN <- False 3 4 8 4.0 So if need replace NaN to 0 need numpy.where - if Trues is passed values from a if False is passed 0: df['c'] = np.where(mask1, df['a'], 0) print (df) a b c 0 1 5 0 1 2 6 2 2 3 7 0 3 4 8 4 Another pandas alternative is Series.where: df['c'] = df['a'].where(mask1, 0) print (df) a b c 0 1 5 0 1 2 6 2 2 3 7 0 3 4 8 4 All together: some_dict = {'a': [1,2,3,4], 'b': [5,6,7,8],} df = pd.DataFrame(some_dict) mask1 = pd.Series([False, True, False, True]) df['c'] = np.where(mask1, df['a'], 0) mask2 = pd.Series([True, False, False, False]) df['c'] += np.where(mask2, df['b'], 0) print (df) a b c 0 1 5 5 1 2 6 2 2 3 7 0 3 4 8 4 Another idea is use Series.add with fill_value=0: some_dict = {'a': [1,2,3,4], 'b': [5,6,7,8],} df = pd.DataFrame(some_dict) mask1 = pd.Series([False, True, False, True]) df['c'] = df.loc[mask1, 'a'] print (df) a b c 0 1 5 NaN 1 2 6 2.0 2 3 7 NaN 3 4 8 4.0 mask2 = pd.Series([True, False, False, False]) df['c'] = df['c'].add(df.loc[mask2, 'b'], fill_value=0) print (df) a b c 0 1 5 5.0 1 2 6 2.0 2 3 7 NaN 3 4 8 4.0
64,020,403
Pandas multiply selected columns by previous column
<p>Assume I have a 3 x 9 Dataframe with index from 0 - 2 and columns from 0 - 8</p> <pre><code>nums = np.arange(1, 28) arr = np.array(nums) arr = arr.reshape((3, 9)) df = pd.DataFrame(arr) </code></pre> <p>I want to multiply selected columns (example [2, 5, 7]) by the columns behind them (example [1, 4, 6]) My obstacle is getting the correct index of the previous column to match with the column I want to multiply</p> <p>Issue:</p> <pre><code>df[[2, 5, 7]] = df[[2, 5, 7]].multiply(___, axis=&quot;index&quot;) # in this case I want the blank to be df[[1, 4, 6]], but how to get these indexes for the general case when selected columns vary? </code></pre>
64,020,430
"2020-09-23T02:57:21.860000"
1
null
1
62
python|pandas
<p>Let's try working with the numpy array:</p> <pre><code>cols = np.array([2,5,7]) df[cols] *= df[cols-1].values </code></pre> <p>Output:</p> <pre><code> 0 1 2 3 4 5 6 7 8 0 1 2 6 4 5 30 7 56 9 1 10 11 132 13 14 210 16 272 18 2 19 20 420 22 23 552 25 650 27 </code></pre> <p>Or you can use:</p> <pre><code>df.update(df[cols]*df.shift(-1, axis=1)) </code></pre> <p>which gives:</p> <pre><code> 0 1 2 3 4 5 6 7 8 0 1 2 12.0 4 5 42.0 7 72.0 9 1 10 11 156.0 13 14 240.0 16 306.0 18 2 19 20 462.0 22 23 600.0 25 702.0 27 </code></pre>
"2020-09-23T03:00:39.147000"
3
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.multiply.html
pandas.DataFrame.multiply# pandas.DataFrame.multiply# DataFrame.multiply(other, axis='columns', level=None, fill_value=None)[source]# Get Multiplication of dataframe and other, element-wise (binary operator mul). Equivalent to dataframe * other, but with support to substitute a fill_value for missing data in one of the inputs. With reverse version, rmul. Among flexible wrappers (add, sub, mul, div, mod, pow) to arithmetic operators: +, -, *, /, //, %, **. Parameters otherscalar, sequence, Series, dict or DataFrameAny single or multiple element data structure, or list-like object. Let's try working with the numpy array: cols = np.array([2,5,7]) df[cols] *= df[cols-1].values Output: 0 1 2 3 4 5 6 7 8 0 1 2 6 4 5 30 7 56 9 1 10 11 132 13 14 210 16 272 18 2 19 20 420 22 23 552 25 650 27 Or you can use: df.update(df[cols]*df.shift(-1, axis=1)) which gives: 0 1 2 3 4 5 6 7 8 0 1 2 12.0 4 5 42.0 7 72.0 9 1 10 11 156.0 13 14 240.0 16 306.0 18 2 19 20 462.0 22 23 600.0 25 702.0 27 axis{0 or ‘index’, 1 or ‘columns’}Whether to compare by the index (0 or ‘index’) or columns. (1 or ‘columns’). For Series input, axis to match Series index on. levelint or labelBroadcast across a level, matching Index values on the passed MultiIndex level. fill_valuefloat or None, default NoneFill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing. Returns DataFrameResult of the arithmetic operation. See also DataFrame.addAdd DataFrames. DataFrame.subSubtract DataFrames. DataFrame.mulMultiply DataFrames. DataFrame.divDivide DataFrames (float division). DataFrame.truedivDivide DataFrames (float division). DataFrame.floordivDivide DataFrames (integer division). DataFrame.modCalculate modulo (remainder after division). DataFrame.powCalculate exponential power. Notes Mismatched indices will be unioned together. Examples >>> df = pd.DataFrame({'angles': [0, 3, 4], ... 'degrees': [360, 180, 360]}, ... index=['circle', 'triangle', 'rectangle']) >>> df angles degrees circle 0 360 triangle 3 180 rectangle 4 360 Add a scalar with operator version which return the same results. >>> df + 1 angles degrees circle 1 361 triangle 4 181 rectangle 5 361 >>> df.add(1) angles degrees circle 1 361 triangle 4 181 rectangle 5 361 Divide by constant with reverse version. >>> df.div(10) angles degrees circle 0.0 36.0 triangle 0.3 18.0 rectangle 0.4 36.0 >>> df.rdiv(10) angles degrees circle inf 0.027778 triangle 3.333333 0.055556 rectangle 2.500000 0.027778 Subtract a list and Series by axis with operator version. >>> df - [1, 2] angles degrees circle -1 358 triangle 2 178 rectangle 3 358 >>> df.sub([1, 2], axis='columns') angles degrees circle -1 358 triangle 2 178 rectangle 3 358 >>> df.sub(pd.Series([1, 1, 1], index=['circle', 'triangle', 'rectangle']), ... axis='index') angles degrees circle -1 359 triangle 2 179 rectangle 3 359 Multiply a dictionary by axis. >>> df.mul({'angles': 0, 'degrees': 2}) angles degrees circle 0 720 triangle 0 360 rectangle 0 720 >>> df.mul({'circle': 0, 'triangle': 2, 'rectangle': 3}, axis='index') angles degrees circle 0 0 triangle 6 360 rectangle 12 1080 Multiply a DataFrame of different shape with operator version. >>> other = pd.DataFrame({'angles': [0, 3, 4]}, ... index=['circle', 'triangle', 'rectangle']) >>> other angles circle 0 triangle 3 rectangle 4 >>> df * other angles degrees circle 0 NaN triangle 9 NaN rectangle 16 NaN >>> df.mul(other, fill_value=0) angles degrees circle 0 0.0 triangle 9 0.0 rectangle 16 0.0 Divide by a MultiIndex by level. >>> df_multindex = pd.DataFrame({'angles': [0, 3, 4, 4, 5, 6], ... 'degrees': [360, 180, 360, 360, 540, 720]}, ... index=[['A', 'A', 'A', 'B', 'B', 'B'], ... ['circle', 'triangle', 'rectangle', ... 'square', 'pentagon', 'hexagon']]) >>> df_multindex angles degrees A circle 0 360 triangle 3 180 rectangle 4 360 B square 4 360 pentagon 5 540 hexagon 6 720 >>> df.div(df_multindex, level=1, fill_value=0) angles degrees A circle NaN 1.0 triangle 1.0 1.0 rectangle 1.0 1.0 B square 0.0 0.0 pentagon 0.0 0.0 hexagon 0.0 0.0
593
1,122
Pandas multiply selected columns by previous column Assume I have a 3 x 9 Dataframe with index from 0 - 2 and columns from 0 - 8 nums = np.arange(1, 28) arr = np.array(nums) arr = arr.reshape((3, 9)) df = pd.DataFrame(arr) I want to multiply selected columns (example [2, 5, 7]) by the columns behind them (example [1, 4, 6]) My obstacle is getting the correct index of the previous column to match with the column I want to multiply Issue: df[[2, 5, 7]] = df[[2, 5, 7]].multiply(___, axis="index") # in this case I want the blank to be df[[1, 4, 6]], but how to get these indexes for the general case when selected columns vary?
0. 0 hexagon 0. 0 0. 0 /
Let's try working with the numpy array: cols = np.array([2,5,7]) df[cols] *= df[cols-1].values Output: 0 1 2 3 4 5 6 7 8 0 1 2 6 4 5 30 7 56 9 1 10 11 132 13 14 210 16 272 18 2 19 20 420 22 23 552 25 650 27 Or you can use: df.update(df[cols]*df.shift(-1, axis=1)) which gives: 0 1 2 3 4 5 6 7 8 0 1 2 12.0 4 5 42.0 7 72.0 9 1 10 11 156.0 13 14 240.0 16 306.0 18 2 19 20 462.0 22 23 600.0 25 702.0 27
68,564,391
How to create an ordered column based on values in another column (NOT a cumulative count though)?
<pre><code>data = [ [&quot;Item_1&quot;, &quot;2020-06-01&quot;], [&quot;Item_1&quot;, &quot;2021-05-01&quot;], [&quot;Item_1&quot;, &quot;2019-02-10&quot;], [&quot;Item_2&quot;, &quot;2020-04-01&quot;], [&quot;Item_2&quot;, &quot;2018-05-01&quot;], [&quot;Item_2&quot;, &quot;2018-05-01&quot;], [&quot;Item_2&quot;, &quot;2018-05-06&quot;], ] df = pd.DataFrame(data, columns=[&quot;Item_ID&quot;, &quot;Dates&quot;]) df </code></pre> <p>I have a dataframe containing a column with a list of identifiers, and a list of dates. I would like to find a way to get the sequential counts of dates as a new column. I have done a <code>groupby</code> to a <code>cumcount()</code> however it doesn't take into account cases where the date is the same and thus the sequential count should also be the same.</p> <p>My desired output would be something like this:</p> <pre><code>Item ID Dates Date Order Item 1 2019-02-10 1 Item 1 2020-06-01 2 Item 1 2020-06-03 3 Item 2 2018-05-01 1 Item 2 2018-05-01 1 Item 2 2018-06-01 2 Item 2 2018-06-03 3 </code></pre>
68,564,579
"2021-07-28T16:48:51.947000"
1
1
2
65
python|pandas
<p>We can <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html" rel="nofollow noreferrer"><code>group</code></a> the dataframe on <code>Item Id</code>, then <a href="https://pandas.pydata.org/docs/reference/api/pandas.Series.rank.html" rel="nofollow noreferrer"><code>rank</code></a> the <code>Dates</code> column using <code>method='dense'</code></p> <pre><code>df = df.sort_values(['Item_ID', 'Dates'], ignore_index=True) # Use if sorting required df['Date Order'] = df.groupby('Item_ID')['Dates'].rank(method='dense') </code></pre> <hr /> <pre><code> Item_ID Dates Date Order 0 Item_1 2019-02-10 1 1 Item_1 2020-06-01 2 2 Item_1 2021-05-01 3 3 Item_2 2018-05-01 1 4 Item_2 2018-05-01 1 5 Item_2 2018-05-06 2 6 Item_2 2020-04-01 3 </code></pre>
"2021-07-28T17:01:56.810000"
3
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.cumsum.html
pandas.DataFrame.cumsum# pandas.DataFrame.cumsum# DataFrame.cumsum(axis=None, skipna=True, *args, **kwargs)[source]# We can group the dataframe on Item Id, then rank the Dates column using method='dense' df = df.sort_values(['Item_ID', 'Dates'], ignore_index=True) # Use if sorting required df['Date Order'] = df.groupby('Item_ID')['Dates'].rank(method='dense') Item_ID Dates Date Order 0 Item_1 2019-02-10 1 1 Item_1 2020-06-01 2 2 Item_1 2021-05-01 3 3 Item_2 2018-05-01 1 4 Item_2 2018-05-01 1 5 Item_2 2018-05-06 2 6 Item_2 2020-04-01 3 Return cumulative sum over a DataFrame or Series axis. Returns a DataFrame or Series of the same size containing the cumulative sum. Parameters axis{0 or ‘index’, 1 or ‘columns’}, default 0The index or the name of the axis. 0 is equivalent to None or ‘index’. For Series this parameter is unused and defaults to 0. skipnabool, default TrueExclude NA/null values. If an entire row/column is NA, the result will be NA. *args, **kwargsAdditional keywords have no effect but might be accepted for compatibility with NumPy. Returns Series or DataFrameReturn cumulative sum of Series or DataFrame. See also core.window.expanding.Expanding.sumSimilar functionality but ignores NaN values. DataFrame.sumReturn the sum over DataFrame axis. DataFrame.cummaxReturn cumulative maximum over DataFrame axis. DataFrame.cumminReturn cumulative minimum over DataFrame axis. DataFrame.cumsumReturn cumulative sum over DataFrame axis. DataFrame.cumprodReturn cumulative product over DataFrame axis. Examples Series >>> s = pd.Series([2, np.nan, 5, -1, 0]) >>> s 0 2.0 1 NaN 2 5.0 3 -1.0 4 0.0 dtype: float64 By default, NA values are ignored. >>> s.cumsum() 0 2.0 1 NaN 2 7.0 3 6.0 4 6.0 dtype: float64 To include NA values in the operation, use skipna=False >>> s.cumsum(skipna=False) 0 2.0 1 NaN 2 NaN 3 NaN 4 NaN dtype: float64 DataFrame >>> df = pd.DataFrame([[2.0, 1.0], ... [3.0, np.nan], ... [1.0, 0.0]], ... columns=list('AB')) >>> df A B 0 2.0 1.0 1 3.0 NaN 2 1.0 0.0 By default, iterates over rows and finds the sum in each column. This is equivalent to axis=None or axis='index'. >>> df.cumsum() A B 0 2.0 1.0 1 5.0 NaN 2 6.0 1.0 To iterate over columns and find the sum in each row, use axis=1 >>> df.cumsum(axis=1) A B 0 2.0 3.0 1 3.0 NaN 2 1.0 1.0
121
640
How to create an ordered column based on values in another column (NOT a cumulative count though)? data = [ ["Item_1", "2020-06-01"], ["Item_1", "2021-05-01"], ["Item_1", "2019-02-10"], ["Item_2", "2020-04-01"], ["Item_2", "2018-05-01"], ["Item_2", "2018-05-01"], ["Item_2", "2018-05-06"], ] df = pd.DataFrame(data, columns=["Item_ID", "Dates"]) df I have a dataframe containing a column with a list of identifiers, and a list of dates. I would like to find a way to get the sequential counts of dates as a new column. I have done a groupby to a cumcount() however it doesn't take into account cases where the date is the same and thus the sequential count should also be the same. My desired output would be something like this: Item ID Dates Date Order Item 1 2019-02-10 1 Item 1 2020-06-01 2 Item 1 2020-06-03 3 Item 2 2018-05-01 1 Item 2 2018-05-01 1 Item 2 2018-06-01 2 Item 2 2018-06-03 3
/
We can group the dataframe on Item Id, then rank the Dates column using method='dense' df = df.sort_values(['Item_ID', 'Dates'], ignore_index=True) # Use if sorting required df['Date Order'] = df.groupby('Item_ID')['Dates'].rank(method='dense') Item_ID Dates Date Order 0 Item_1 2019-02-10 1 1 Item_1 2020-06-01 2 2 Item_1 2021-05-01 3 3 Item_2 2018-05-01 1 4 Item_2 2018-05-01 1 5 Item_2 2018-05-06 2 6 Item_2 2020-04-01 3
66,692,148
format all columns of a dataframe
<p>Hi i am looking for a solution how to format all columns or a selection by position (column 0 and 1)</p> <pre><code>import pandas as pd d = {'value': [20, 10, -5, ], 'min': [0, 10, -10,], 'max': [40, 20, 0]} df = pd.DataFrame(data=d) #df = df.astype(float).map(&quot;{:,.2f} €&quot;.format) # style to €, does not work </code></pre> <p>thanks for help</p>
66,692,436
"2021-03-18T13:32:02.200000"
3
null
1
82
python|pandas
<p><code>applymap()</code> and <strong>f-string</strong> works</p> <pre><code>d = {'value': [20, 10, -5, ], 'min': [0, 10, -10,], 'max': [40, 20, 0]} df = pd.DataFrame(data=d) df.applymap(lambda x: f&quot;{x:,.2f} €&quot;) </code></pre> <div class="s-table-container"> <table class="s-table"> <thead> <tr> <th style="text-align: right;"></th> <th style="text-align: left;">value</th> <th style="text-align: left;">min</th> <th style="text-align: left;">max</th> </tr> </thead> <tbody> <tr> <td style="text-align: right;">0</td> <td style="text-align: left;">20.00 €</td> <td style="text-align: left;">0.00 €</td> <td style="text-align: left;">40.00 €</td> </tr> <tr> <td style="text-align: right;">1</td> <td style="text-align: left;">10.00 €</td> <td style="text-align: left;">10.00 €</td> <td style="text-align: left;">20.00 €</td> </tr> <tr> <td style="text-align: right;">2</td> <td style="text-align: left;">-5.00 €</td> <td style="text-align: left;">-10.00 €</td> <td style="text-align: left;">0.00 €</td> </tr> </tbody> </table> </div>
"2021-03-18T13:47:42.260000"
3
https://pandas.pydata.org/docs/reference/api/pandas.io.formats.style.Styler.format.html
pandas.io.formats.style.Styler.format# pandas.io.formats.style.Styler.format# Styler.format(formatter=None, subset=None, na_rep=None, precision=None, decimal='.', thousands=None, escape=None, hyperlinks=None)[source]# Format the text display value of cells. Parameters formatterstr, callable, dict or NoneObject to define how values are displayed. See notes. subsetlabel, array-like, IndexSlice, optionalA valid 2d input to DataFrame.loc[<subset>], or, in the case of a 1d input applymap() and f-string works d = {'value': [20, 10, -5, ], 'min': [0, 10, -10,], 'max': [40, 20, 0]} df = pd.DataFrame(data=d) df.applymap(lambda x: f"{x:,.2f} €") value min max 0 20.00 € 0.00 € 40.00 € 1 10.00 € 10.00 € 20.00 € 2 -5.00 € -10.00 € 0.00 € or single key, to DataFrame.loc[:, <subset>] where the columns are prioritised, to limit data to before applying the function. na_repstr, optionalRepresentation for missing values. If na_rep is None, no special formatting is applied. New in version 1.0.0. precisionint, optionalFloating point precision to use for display purposes, if not determined by the specified formatter. New in version 1.3.0. decimalstr, default “.”Character used as decimal separator for floats, complex and integers. New in version 1.3.0. thousandsstr, optional, default NoneCharacter used as thousands separator for floats, complex and integers. New in version 1.3.0. escapestr, optionalUse ‘html’ to replace the characters &, <, >, ', and " in cell display string with HTML-safe sequences. Use ‘latex’ to replace the characters &, %, $, #, _, {, }, ~, ^, and \ in the cell display string with LaTeX-safe sequences. Escaping is done before formatter. New in version 1.3.0. hyperlinks{“html”, “latex”}, optionalConvert string patterns containing https://, http://, ftp:// or www. to HTML <a> tags as clickable URL hyperlinks if “html”, or LaTeX href commands if “latex”. New in version 1.4.0. Returns selfStyler See also Styler.format_indexFormat the text display value of index labels. Notes This method assigns a formatting function, formatter, to each cell in the DataFrame. If formatter is None, then the default formatter is used. If a callable then that function should take a data value as input and return a displayable representation, such as a string. If formatter is given as a string this is assumed to be a valid Python format specification and is wrapped to a callable as string.format(x). If a dict is given, keys should correspond to column names, and values should be string or callable, as above. The default formatter currently expresses floats and complex numbers with the pandas display precision unless using the precision argument here. The default formatter does not adjust the representation of missing values unless the na_rep argument is used. The subset argument defines which region to apply the formatting function to. If the formatter argument is given in dict form but does not include all columns within the subset then these columns will have the default formatter applied. Any columns in the formatter dict excluded from the subset will be ignored. When using a formatter string the dtypes must be compatible, otherwise a ValueError will be raised. When instantiating a Styler, default formatting can be applied be setting the pandas.options: styler.format.formatter: default None. styler.format.na_rep: default None. styler.format.precision: default 6. styler.format.decimal: default “.”. styler.format.thousands: default None. styler.format.escape: default None. Warning Styler.format is ignored when using the output format Styler.to_excel, since Excel and Python have inherrently different formatting structures. However, it is possible to use the number-format pseudo CSS attribute to force Excel permissible formatting. See examples. Examples Using na_rep and precision with the default formatter >>> df = pd.DataFrame([[np.nan, 1.0, 'A'], [2.0, np.nan, 3.0]]) >>> df.style.format(na_rep='MISS', precision=3) 0 1 2 0 MISS 1.000 A 1 2.000 MISS 3.000 Using a formatter specification on consistent column dtypes >>> df.style.format('{:.2f}', na_rep='MISS', subset=[0,1]) 0 1 2 0 MISS 1.00 A 1 2.00 MISS 3.000000 Using the default formatter for unspecified columns >>> df.style.format({0: '{:.2f}', 1: '£ {:.1f}'}, na_rep='MISS', precision=1) ... 0 1 2 0 MISS £ 1.0 A 1 2.00 MISS 3.0 Multiple na_rep or precision specifications under the default formatter. >>> df.style.format(na_rep='MISS', precision=1, subset=[0]) ... .format(na_rep='PASS', precision=2, subset=[1, 2]) 0 1 2 0 MISS 1.00 A 1 2.0 PASS 3.00 Using a callable formatter function. >>> func = lambda s: 'STRING' if isinstance(s, str) else 'FLOAT' >>> df.style.format({0: '{:.1f}', 2: func}, precision=4, na_rep='MISS') ... 0 1 2 0 MISS 1.0000 STRING 1 2.0 MISS FLOAT Using a formatter with HTML escape and na_rep. >>> df = pd.DataFrame([['<div></div>', '"A&B"', None]]) >>> s = df.style.format( ... '<a href="a.com/{0}">{0}</a>', escape="html", na_rep="NA" ... ) >>> s.to_html() ... <td .. ><a href="a.com/&lt;div&gt;&lt;/div&gt;">&lt;div&gt;&lt;/div&gt;</a></td> <td .. ><a href="a.com/&#34;A&amp;B&#34;">&#34;A&amp;B&#34;</a></td> <td .. >NA</td> ... Using a formatter with LaTeX escape. >>> df = pd.DataFrame([["123"], ["~ ^"], ["$%#"]]) >>> df.style.format("\\textbf{{{}}}", escape="latex").to_latex() ... \begin{tabular}{ll} {} & {0} \\ 0 & \textbf{123} \\ 1 & \textbf{\textasciitilde \space \textasciicircum } \\ 2 & \textbf{\$\%\#} \\ \end{tabular} Pandas defines a number-format pseudo CSS attribute instead of the .format method to create to_excel permissible formatting. Note that semi-colons are CSS protected characters but used as separators in Excel’s format string. Replace semi-colons with the section separator character (ASCII-245) when defining the formatting here. >>> df = pd.DataFrame({"A": [1, 0, -1]}) >>> pseudo_css = "number-format: 0§[Red](0)§-§@;" >>> df.style.applymap(lambda v: css).to_excel("formatted_file.xlsx") ...
486
769
format all columns of a dataframe Hi i am looking for a solution how to format all columns or a selection by position (column 0 and 1) import pandas as pd d = {'value': [20, 10, -5, ], 'min': [0, 10, -10,], 'max': [40, 20, 0]} df = pd.DataFrame(data=d) #df = df.astype(float).map("{:,.2f} €".format) # style to €, does not work thanks for help
applymap() and f-string works d = {'value': [20, 10, -5, ], 'min': [0, 10, -10,], 'max': [40, 20, 0]} df = pd.DataFrame(data=d) df.applymap(lambda x: f"{x:,.2f} €") value min max 0 20.00 € 0.00 € 40.00 € 1 10.00 € 10.00 € 20.00 € 2 -5.00 € -10.00 € 0.00 €
62,055,058
Group by Pandas and replace null value by value
<p>I have a huge Pandas dataframe df with over 4 million rows which looks like below.</p> <pre><code> id value percent value_1 percent_1 0 1 0.530106 21%-31% NaN NaN 1 2 0.086647 10%-20% NaN NaN 2 3 0.073121 $30%-40% NaN NaN 3 4 0.76891 81%-90% NaN NaN 4 5 0.86536 41%-50% NaN NaN 5 1 NaN NaN 0.630106 91%-100% 6 2 NaN NaN 0.086647 11%-20% 7 3 NaN NaN 0.073121 $0%-10% 8 4 NaN NaN 0.376891 81%-90% 9 5 NaN NaN 0.186536 41%-50% </code></pre> <p>I want a dataframe that looks like the below</p> <pre><code> id value percent value_1 percent_1 0 1 0.530106 21%-31% 0.630106 91%-100% 1 2 0.086647 10%-20% 0.086647 11%-20% 2 3 0.073121 $30%-40% 0.073121 $0%-10% 3 4 0.76891 81%-90% 0.376891 81%-90% 4 5 0.86536 41%-50% 0.186536 41%-50% </code></pre> <p>One hacky way to do this is replace NaN with empty string, convert the entire df columns into strings and group them</p> <pre><code> df = df.replace(np.nan,'') df = df.astype(str) df.groupby(['id']).sum() </code></pre> <p>But this takes a long time because groupby takes a lot of time with strings. Is there a better way to do this? </p>
62,055,076
"2020-05-28T00:35:02.243000"
1
null
2
369
python|pandas
<p>Let us try <code>groupby</code> with <code>first</code> which will skip the NaN value </p> <pre><code>df=df.groupby('id').first().reset_index() </code></pre>
"2020-05-28T00:37:30.633000"
3
https://pandas.pydata.org/docs/user_guide/missing_data.html
Working with missing data# Working with missing data# In this section, we will discuss missing (also referred to as NA) values in pandas. Note The choice of using NaN internally to denote missing data was largely for simplicity and performance reasons. Starting from pandas 1.0, some optional data types start experimenting with a native NA scalar using a mask-based approach. See here for more. See the cookbook for some advanced strategies. Let us try groupby with first which will skip the NaN value df=df.groupby('id').first().reset_index() Values considered “missing”# As data comes in many shapes and forms, pandas aims to be flexible with regard to handling missing data. While NaN is the default missing value marker for reasons of computational speed and convenience, we need to be able to easily detect this value with data of different types: floating point, integer, boolean, and general object. In many cases, however, the Python None will arise and we wish to also consider that “missing” or “not available” or “NA”. Note If you want to consider inf and -inf to be “NA” in computations, you can set pandas.options.mode.use_inf_as_na = True. In [1]: df = pd.DataFrame( ...: np.random.randn(5, 3), ...: index=["a", "c", "e", "f", "h"], ...: columns=["one", "two", "three"], ...: ) ...: In [2]: df["four"] = "bar" In [3]: df["five"] = df["one"] > 0 In [4]: df Out[4]: one two three four five a 0.469112 -0.282863 -1.509059 bar True c -1.135632 1.212112 -0.173215 bar False e 0.119209 -1.044236 -0.861849 bar True f -2.104569 -0.494929 1.071804 bar False h 0.721555 -0.706771 -1.039575 bar True In [5]: df2 = df.reindex(["a", "b", "c", "d", "e", "f", "g", "h"]) In [6]: df2 Out[6]: one two three four five a 0.469112 -0.282863 -1.509059 bar True b NaN NaN NaN NaN NaN c -1.135632 1.212112 -0.173215 bar False d NaN NaN NaN NaN NaN e 0.119209 -1.044236 -0.861849 bar True f -2.104569 -0.494929 1.071804 bar False g NaN NaN NaN NaN NaN h 0.721555 -0.706771 -1.039575 bar True To make detecting missing values easier (and across different array dtypes), pandas provides the isna() and notna() functions, which are also methods on Series and DataFrame objects: In [7]: df2["one"] Out[7]: a 0.469112 b NaN c -1.135632 d NaN e 0.119209 f -2.104569 g NaN h 0.721555 Name: one, dtype: float64 In [8]: pd.isna(df2["one"]) Out[8]: a False b True c False d True e False f False g True h False Name: one, dtype: bool In [9]: df2["four"].notna() Out[9]: a True b False c True d False e True f True g False h True Name: four, dtype: bool In [10]: df2.isna() Out[10]: one two three four five a False False False False False b True True True True True c False False False False False d True True True True True e False False False False False f False False False False False g True True True True True h False False False False False Warning One has to be mindful that in Python (and NumPy), the nan's don’t compare equal, but None's do. Note that pandas/NumPy uses the fact that np.nan != np.nan, and treats None like np.nan. In [11]: None == None # noqa: E711 Out[11]: True In [12]: np.nan == np.nan Out[12]: False So as compared to above, a scalar equality comparison versus a None/np.nan doesn’t provide useful information. In [13]: df2["one"] == np.nan Out[13]: a False b False c False d False e False f False g False h False Name: one, dtype: bool Integer dtypes and missing data# Because NaN is a float, a column of integers with even one missing values is cast to floating-point dtype (see Support for integer NA for more). pandas provides a nullable integer array, which can be used by explicitly requesting the dtype: In [14]: pd.Series([1, 2, np.nan, 4], dtype=pd.Int64Dtype()) Out[14]: 0 1 1 2 2 <NA> 3 4 dtype: Int64 Alternatively, the string alias dtype='Int64' (note the capital "I") can be used. See Nullable integer data type for more. Datetimes# For datetime64[ns] types, NaT represents missing values. This is a pseudo-native sentinel value that can be represented by NumPy in a singular dtype (datetime64[ns]). pandas objects provide compatibility between NaT and NaN. In [15]: df2 = df.copy() In [16]: df2["timestamp"] = pd.Timestamp("20120101") In [17]: df2 Out[17]: one two three four five timestamp a 0.469112 -0.282863 -1.509059 bar True 2012-01-01 c -1.135632 1.212112 -0.173215 bar False 2012-01-01 e 0.119209 -1.044236 -0.861849 bar True 2012-01-01 f -2.104569 -0.494929 1.071804 bar False 2012-01-01 h 0.721555 -0.706771 -1.039575 bar True 2012-01-01 In [18]: df2.loc[["a", "c", "h"], ["one", "timestamp"]] = np.nan In [19]: df2 Out[19]: one two three four five timestamp a NaN -0.282863 -1.509059 bar True NaT c NaN 1.212112 -0.173215 bar False NaT e 0.119209 -1.044236 -0.861849 bar True 2012-01-01 f -2.104569 -0.494929 1.071804 bar False 2012-01-01 h NaN -0.706771 -1.039575 bar True NaT In [20]: df2.dtypes.value_counts() Out[20]: float64 3 object 1 bool 1 datetime64[ns] 1 dtype: int64 Inserting missing data# You can insert missing values by simply assigning to containers. The actual missing value used will be chosen based on the dtype. For example, numeric containers will always use NaN regardless of the missing value type chosen: In [21]: s = pd.Series([1, 2, 3]) In [22]: s.loc[0] = None In [23]: s Out[23]: 0 NaN 1 2.0 2 3.0 dtype: float64 Likewise, datetime containers will always use NaT. For object containers, pandas will use the value given: In [24]: s = pd.Series(["a", "b", "c"]) In [25]: s.loc[0] = None In [26]: s.loc[1] = np.nan In [27]: s Out[27]: 0 None 1 NaN 2 c dtype: object Calculations with missing data# Missing values propagate naturally through arithmetic operations between pandas objects. In [28]: a Out[28]: one two a NaN -0.282863 c NaN 1.212112 e 0.119209 -1.044236 f -2.104569 -0.494929 h -2.104569 -0.706771 In [29]: b Out[29]: one two three a NaN -0.282863 -1.509059 c NaN 1.212112 -0.173215 e 0.119209 -1.044236 -0.861849 f -2.104569 -0.494929 1.071804 h NaN -0.706771 -1.039575 In [30]: a + b Out[30]: one three two a NaN NaN -0.565727 c NaN NaN 2.424224 e 0.238417 NaN -2.088472 f -4.209138 NaN -0.989859 h NaN NaN -1.413542 The descriptive statistics and computational methods discussed in the data structure overview (and listed here and here) are all written to account for missing data. For example: When summing data, NA (missing) values will be treated as zero. If the data are all NA, the result will be 0. Cumulative methods like cumsum() and cumprod() ignore NA values by default, but preserve them in the resulting arrays. To override this behaviour and include NA values, use skipna=False. In [31]: df Out[31]: one two three a NaN -0.282863 -1.509059 c NaN 1.212112 -0.173215 e 0.119209 -1.044236 -0.861849 f -2.104569 -0.494929 1.071804 h NaN -0.706771 -1.039575 In [32]: df["one"].sum() Out[32]: -1.9853605075978744 In [33]: df.mean(1) Out[33]: a -0.895961 c 0.519449 e -0.595625 f -0.509232 h -0.873173 dtype: float64 In [34]: df.cumsum() Out[34]: one two three a NaN -0.282863 -1.509059 c NaN 0.929249 -1.682273 e 0.119209 -0.114987 -2.544122 f -1.985361 -0.609917 -1.472318 h NaN -1.316688 -2.511893 In [35]: df.cumsum(skipna=False) Out[35]: one two three a NaN -0.282863 -1.509059 c NaN 0.929249 -1.682273 e NaN -0.114987 -2.544122 f NaN -0.609917 -1.472318 h NaN -1.316688 -2.511893 Sum/prod of empties/nans# Warning This behavior is now standard as of v0.22.0 and is consistent with the default in numpy; previously sum/prod of all-NA or empty Series/DataFrames would return NaN. See v0.22.0 whatsnew for more. The sum of an empty or all-NA Series or column of a DataFrame is 0. In [36]: pd.Series([np.nan]).sum() Out[36]: 0.0 In [37]: pd.Series([], dtype="float64").sum() Out[37]: 0.0 The product of an empty or all-NA Series or column of a DataFrame is 1. In [38]: pd.Series([np.nan]).prod() Out[38]: 1.0 In [39]: pd.Series([], dtype="float64").prod() Out[39]: 1.0 NA values in GroupBy# NA groups in GroupBy are automatically excluded. This behavior is consistent with R, for example: In [40]: df Out[40]: one two three a NaN -0.282863 -1.509059 c NaN 1.212112 -0.173215 e 0.119209 -1.044236 -0.861849 f -2.104569 -0.494929 1.071804 h NaN -0.706771 -1.039575 In [41]: df.groupby("one").mean() Out[41]: two three one -2.104569 -0.494929 1.071804 0.119209 -1.044236 -0.861849 See the groupby section here for more information. Cleaning / filling missing data# pandas objects are equipped with various data manipulation methods for dealing with missing data. Filling missing values: fillna# fillna() can “fill in” NA values with non-NA data in a couple of ways, which we illustrate: Replace NA with a scalar value In [42]: df2 Out[42]: one two three four five timestamp a NaN -0.282863 -1.509059 bar True NaT c NaN 1.212112 -0.173215 bar False NaT e 0.119209 -1.044236 -0.861849 bar True 2012-01-01 f -2.104569 -0.494929 1.071804 bar False 2012-01-01 h NaN -0.706771 -1.039575 bar True NaT In [43]: df2.fillna(0) Out[43]: one two three four five timestamp a 0.000000 -0.282863 -1.509059 bar True 0 c 0.000000 1.212112 -0.173215 bar False 0 e 0.119209 -1.044236 -0.861849 bar True 2012-01-01 00:00:00 f -2.104569 -0.494929 1.071804 bar False 2012-01-01 00:00:00 h 0.000000 -0.706771 -1.039575 bar True 0 In [44]: df2["one"].fillna("missing") Out[44]: a missing c missing e 0.119209 f -2.104569 h missing Name: one, dtype: object Fill gaps forward or backward Using the same filling arguments as reindexing, we can propagate non-NA values forward or backward: In [45]: df Out[45]: one two three a NaN -0.282863 -1.509059 c NaN 1.212112 -0.173215 e 0.119209 -1.044236 -0.861849 f -2.104569 -0.494929 1.071804 h NaN -0.706771 -1.039575 In [46]: df.fillna(method="pad") Out[46]: one two three a NaN -0.282863 -1.509059 c NaN 1.212112 -0.173215 e 0.119209 -1.044236 -0.861849 f -2.104569 -0.494929 1.071804 h -2.104569 -0.706771 -1.039575 Limit the amount of filling If we only want consecutive gaps filled up to a certain number of data points, we can use the limit keyword: In [47]: df Out[47]: one two three a NaN -0.282863 -1.509059 c NaN 1.212112 -0.173215 e NaN NaN NaN f NaN NaN NaN h NaN -0.706771 -1.039575 In [48]: df.fillna(method="pad", limit=1) Out[48]: one two three a NaN -0.282863 -1.509059 c NaN 1.212112 -0.173215 e NaN 1.212112 -0.173215 f NaN NaN NaN h NaN -0.706771 -1.039575 To remind you, these are the available filling methods: Method Action pad / ffill Fill values forward bfill / backfill Fill values backward With time series data, using pad/ffill is extremely common so that the “last known value” is available at every time point. ffill() is equivalent to fillna(method='ffill') and bfill() is equivalent to fillna(method='bfill') Filling with a PandasObject# You can also fillna using a dict or Series that is alignable. The labels of the dict or index of the Series must match the columns of the frame you wish to fill. The use case of this is to fill a DataFrame with the mean of that column. In [49]: dff = pd.DataFrame(np.random.randn(10, 3), columns=list("ABC")) In [50]: dff.iloc[3:5, 0] = np.nan In [51]: dff.iloc[4:6, 1] = np.nan In [52]: dff.iloc[5:8, 2] = np.nan In [53]: dff Out[53]: A B C 0 0.271860 -0.424972 0.567020 1 0.276232 -1.087401 -0.673690 2 0.113648 -1.478427 0.524988 3 NaN 0.577046 -1.715002 4 NaN NaN -1.157892 5 -1.344312 NaN NaN 6 -0.109050 1.643563 NaN 7 0.357021 -0.674600 NaN 8 -0.968914 -1.294524 0.413738 9 0.276662 -0.472035 -0.013960 In [54]: dff.fillna(dff.mean()) Out[54]: A B C 0 0.271860 -0.424972 0.567020 1 0.276232 -1.087401 -0.673690 2 0.113648 -1.478427 0.524988 3 -0.140857 0.577046 -1.715002 4 -0.140857 -0.401419 -1.157892 5 -1.344312 -0.401419 -0.293543 6 -0.109050 1.643563 -0.293543 7 0.357021 -0.674600 -0.293543 8 -0.968914 -1.294524 0.413738 9 0.276662 -0.472035 -0.013960 In [55]: dff.fillna(dff.mean()["B":"C"]) Out[55]: A B C 0 0.271860 -0.424972 0.567020 1 0.276232 -1.087401 -0.673690 2 0.113648 -1.478427 0.524988 3 NaN 0.577046 -1.715002 4 NaN -0.401419 -1.157892 5 -1.344312 -0.401419 -0.293543 6 -0.109050 1.643563 -0.293543 7 0.357021 -0.674600 -0.293543 8 -0.968914 -1.294524 0.413738 9 0.276662 -0.472035 -0.013960 Same result as above, but is aligning the ‘fill’ value which is a Series in this case. In [56]: dff.where(pd.notna(dff), dff.mean(), axis="columns") Out[56]: A B C 0 0.271860 -0.424972 0.567020 1 0.276232 -1.087401 -0.673690 2 0.113648 -1.478427 0.524988 3 -0.140857 0.577046 -1.715002 4 -0.140857 -0.401419 -1.157892 5 -1.344312 -0.401419 -0.293543 6 -0.109050 1.643563 -0.293543 7 0.357021 -0.674600 -0.293543 8 -0.968914 -1.294524 0.413738 9 0.276662 -0.472035 -0.013960 Dropping axis labels with missing data: dropna# You may wish to simply exclude labels from a data set which refer to missing data. To do this, use dropna(): In [57]: df Out[57]: one two three a NaN -0.282863 -1.509059 c NaN 1.212112 -0.173215 e NaN 0.000000 0.000000 f NaN 0.000000 0.000000 h NaN -0.706771 -1.039575 In [58]: df.dropna(axis=0) Out[58]: Empty DataFrame Columns: [one, two, three] Index: [] In [59]: df.dropna(axis=1) Out[59]: two three a -0.282863 -1.509059 c 1.212112 -0.173215 e 0.000000 0.000000 f 0.000000 0.000000 h -0.706771 -1.039575 In [60]: df["one"].dropna() Out[60]: Series([], Name: one, dtype: float64) An equivalent dropna() is available for Series. DataFrame.dropna has considerably more options than Series.dropna, which can be examined in the API. Interpolation# Both Series and DataFrame objects have interpolate() that, by default, performs linear interpolation at missing data points. In [61]: ts Out[61]: 2000-01-31 0.469112 2000-02-29 NaN 2000-03-31 NaN 2000-04-28 NaN 2000-05-31 NaN ... 2007-12-31 -6.950267 2008-01-31 -7.904475 2008-02-29 -6.441779 2008-03-31 -8.184940 2008-04-30 -9.011531 Freq: BM, Length: 100, dtype: float64 In [62]: ts.count() Out[62]: 66 In [63]: ts.plot() Out[63]: <AxesSubplot: > In [64]: ts.interpolate() Out[64]: 2000-01-31 0.469112 2000-02-29 0.434469 2000-03-31 0.399826 2000-04-28 0.365184 2000-05-31 0.330541 ... 2007-12-31 -6.950267 2008-01-31 -7.904475 2008-02-29 -6.441779 2008-03-31 -8.184940 2008-04-30 -9.011531 Freq: BM, Length: 100, dtype: float64 In [65]: ts.interpolate().count() Out[65]: 100 In [66]: ts.interpolate().plot() Out[66]: <AxesSubplot: > Index aware interpolation is available via the method keyword: In [67]: ts2 Out[67]: 2000-01-31 0.469112 2000-02-29 NaN 2002-07-31 -5.785037 2005-01-31 NaN 2008-04-30 -9.011531 dtype: float64 In [68]: ts2.interpolate() Out[68]: 2000-01-31 0.469112 2000-02-29 -2.657962 2002-07-31 -5.785037 2005-01-31 -7.398284 2008-04-30 -9.011531 dtype: float64 In [69]: ts2.interpolate(method="time") Out[69]: 2000-01-31 0.469112 2000-02-29 0.270241 2002-07-31 -5.785037 2005-01-31 -7.190866 2008-04-30 -9.011531 dtype: float64 For a floating-point index, use method='values': In [70]: ser Out[70]: 0.0 0.0 1.0 NaN 10.0 10.0 dtype: float64 In [71]: ser.interpolate() Out[71]: 0.0 0.0 1.0 5.0 10.0 10.0 dtype: float64 In [72]: ser.interpolate(method="values") Out[72]: 0.0 0.0 1.0 1.0 10.0 10.0 dtype: float64 You can also interpolate with a DataFrame: In [73]: df = pd.DataFrame( ....: { ....: "A": [1, 2.1, np.nan, 4.7, 5.6, 6.8], ....: "B": [0.25, np.nan, np.nan, 4, 12.2, 14.4], ....: } ....: ) ....: In [74]: df Out[74]: A B 0 1.0 0.25 1 2.1 NaN 2 NaN NaN 3 4.7 4.00 4 5.6 12.20 5 6.8 14.40 In [75]: df.interpolate() Out[75]: A B 0 1.0 0.25 1 2.1 1.50 2 3.4 2.75 3 4.7 4.00 4 5.6 12.20 5 6.8 14.40 The method argument gives access to fancier interpolation methods. If you have scipy installed, you can pass the name of a 1-d interpolation routine to method. You’ll want to consult the full scipy interpolation documentation and reference guide for details. The appropriate interpolation method will depend on the type of data you are working with. If you are dealing with a time series that is growing at an increasing rate, method='quadratic' may be appropriate. If you have values approximating a cumulative distribution function, then method='pchip' should work well. To fill missing values with goal of smooth plotting, consider method='akima'. Warning These methods require scipy. In [76]: df.interpolate(method="barycentric") Out[76]: A B 0 1.00 0.250 1 2.10 -7.660 2 3.53 -4.515 3 4.70 4.000 4 5.60 12.200 5 6.80 14.400 In [77]: df.interpolate(method="pchip") Out[77]: A B 0 1.00000 0.250000 1 2.10000 0.672808 2 3.43454 1.928950 3 4.70000 4.000000 4 5.60000 12.200000 5 6.80000 14.400000 In [78]: df.interpolate(method="akima") Out[78]: A B 0 1.000000 0.250000 1 2.100000 -0.873316 2 3.406667 0.320034 3 4.700000 4.000000 4 5.600000 12.200000 5 6.800000 14.400000 When interpolating via a polynomial or spline approximation, you must also specify the degree or order of the approximation: In [79]: df.interpolate(method="spline", order=2) Out[79]: A B 0 1.000000 0.250000 1 2.100000 -0.428598 2 3.404545 1.206900 3 4.700000 4.000000 4 5.600000 12.200000 5 6.800000 14.400000 In [80]: df.interpolate(method="polynomial", order=2) Out[80]: A B 0 1.000000 0.250000 1 2.100000 -2.703846 2 3.451351 -1.453846 3 4.700000 4.000000 4 5.600000 12.200000 5 6.800000 14.400000 Compare several methods: In [81]: np.random.seed(2) In [82]: ser = pd.Series(np.arange(1, 10.1, 0.25) ** 2 + np.random.randn(37)) In [83]: missing = np.array([4, 13, 14, 15, 16, 17, 18, 20, 29]) In [84]: ser[missing] = np.nan In [85]: methods = ["linear", "quadratic", "cubic"] In [86]: df = pd.DataFrame({m: ser.interpolate(method=m) for m in methods}) In [87]: df.plot() Out[87]: <AxesSubplot: > Another use case is interpolation at new values. Suppose you have 100 observations from some distribution. And let’s suppose that you’re particularly interested in what’s happening around the middle. You can mix pandas’ reindex and interpolate methods to interpolate at the new values. In [88]: ser = pd.Series(np.sort(np.random.uniform(size=100))) # interpolate at new_index In [89]: new_index = ser.index.union(pd.Index([49.25, 49.5, 49.75, 50.25, 50.5, 50.75])) In [90]: interp_s = ser.reindex(new_index).interpolate(method="pchip") In [91]: interp_s[49:51] Out[91]: 49.00 0.471410 49.25 0.476841 49.50 0.481780 49.75 0.485998 50.00 0.489266 50.25 0.491814 50.50 0.493995 50.75 0.495763 51.00 0.497074 dtype: float64 Interpolation limits# Like other pandas fill methods, interpolate() accepts a limit keyword argument. Use this argument to limit the number of consecutive NaN values filled since the last valid observation: In [92]: ser = pd.Series([np.nan, np.nan, 5, np.nan, np.nan, np.nan, 13, np.nan, np.nan]) In [93]: ser Out[93]: 0 NaN 1 NaN 2 5.0 3 NaN 4 NaN 5 NaN 6 13.0 7 NaN 8 NaN dtype: float64 # fill all consecutive values in a forward direction In [94]: ser.interpolate() Out[94]: 0 NaN 1 NaN 2 5.0 3 7.0 4 9.0 5 11.0 6 13.0 7 13.0 8 13.0 dtype: float64 # fill one consecutive value in a forward direction In [95]: ser.interpolate(limit=1) Out[95]: 0 NaN 1 NaN 2 5.0 3 7.0 4 NaN 5 NaN 6 13.0 7 13.0 8 NaN dtype: float64 By default, NaN values are filled in a forward direction. Use limit_direction parameter to fill backward or from both directions. # fill one consecutive value backwards In [96]: ser.interpolate(limit=1, limit_direction="backward") Out[96]: 0 NaN 1 5.0 2 5.0 3 NaN 4 NaN 5 11.0 6 13.0 7 NaN 8 NaN dtype: float64 # fill one consecutive value in both directions In [97]: ser.interpolate(limit=1, limit_direction="both") Out[97]: 0 NaN 1 5.0 2 5.0 3 7.0 4 NaN 5 11.0 6 13.0 7 13.0 8 NaN dtype: float64 # fill all consecutive values in both directions In [98]: ser.interpolate(limit_direction="both") Out[98]: 0 5.0 1 5.0 2 5.0 3 7.0 4 9.0 5 11.0 6 13.0 7 13.0 8 13.0 dtype: float64 By default, NaN values are filled whether they are inside (surrounded by) existing valid values, or outside existing valid values. The limit_area parameter restricts filling to either inside or outside values. # fill one consecutive inside value in both directions In [99]: ser.interpolate(limit_direction="both", limit_area="inside", limit=1) Out[99]: 0 NaN 1 NaN 2 5.0 3 7.0 4 NaN 5 11.0 6 13.0 7 NaN 8 NaN dtype: float64 # fill all consecutive outside values backward In [100]: ser.interpolate(limit_direction="backward", limit_area="outside") Out[100]: 0 5.0 1 5.0 2 5.0 3 NaN 4 NaN 5 NaN 6 13.0 7 NaN 8 NaN dtype: float64 # fill all consecutive outside values in both directions In [101]: ser.interpolate(limit_direction="both", limit_area="outside") Out[101]: 0 5.0 1 5.0 2 5.0 3 NaN 4 NaN 5 NaN 6 13.0 7 13.0 8 13.0 dtype: float64 Replacing generic values# Often times we want to replace arbitrary values with other values. replace() in Series and replace() in DataFrame provides an efficient yet flexible way to perform such replacements. For a Series, you can replace a single value or a list of values by another value: In [102]: ser = pd.Series([0.0, 1.0, 2.0, 3.0, 4.0]) In [103]: ser.replace(0, 5) Out[103]: 0 5.0 1 1.0 2 2.0 3 3.0 4 4.0 dtype: float64 You can replace a list of values by a list of other values: In [104]: ser.replace([0, 1, 2, 3, 4], [4, 3, 2, 1, 0]) Out[104]: 0 4.0 1 3.0 2 2.0 3 1.0 4 0.0 dtype: float64 You can also specify a mapping dict: In [105]: ser.replace({0: 10, 1: 100}) Out[105]: 0 10.0 1 100.0 2 2.0 3 3.0 4 4.0 dtype: float64 For a DataFrame, you can specify individual values by column: In [106]: df = pd.DataFrame({"a": [0, 1, 2, 3, 4], "b": [5, 6, 7, 8, 9]}) In [107]: df.replace({"a": 0, "b": 5}, 100) Out[107]: a b 0 100 100 1 1 6 2 2 7 3 3 8 4 4 9 Instead of replacing with specified values, you can treat all given values as missing and interpolate over them: In [108]: ser.replace([1, 2, 3], method="pad") Out[108]: 0 0.0 1 0.0 2 0.0 3 0.0 4 4.0 dtype: float64 String/regular expression replacement# Note Python strings prefixed with the r character such as r'hello world' are so-called “raw” strings. They have different semantics regarding backslashes than strings without this prefix. Backslashes in raw strings will be interpreted as an escaped backslash, e.g., r'\' == '\\'. You should read about them if this is unclear. Replace the ‘.’ with NaN (str -> str): In [109]: d = {"a": list(range(4)), "b": list("ab.."), "c": ["a", "b", np.nan, "d"]} In [110]: df = pd.DataFrame(d) In [111]: df.replace(".", np.nan) Out[111]: a b c 0 0 a a 1 1 b b 2 2 NaN NaN 3 3 NaN d Now do it with a regular expression that removes surrounding whitespace (regex -> regex): In [112]: df.replace(r"\s*\.\s*", np.nan, regex=True) Out[112]: a b c 0 0 a a 1 1 b b 2 2 NaN NaN 3 3 NaN d Replace a few different values (list -> list): In [113]: df.replace(["a", "."], ["b", np.nan]) Out[113]: a b c 0 0 b b 1 1 b b 2 2 NaN NaN 3 3 NaN d list of regex -> list of regex: In [114]: df.replace([r"\.", r"(a)"], ["dot", r"\1stuff"], regex=True) Out[114]: a b c 0 0 astuff astuff 1 1 b b 2 2 dot NaN 3 3 dot d Only search in column 'b' (dict -> dict): In [115]: df.replace({"b": "."}, {"b": np.nan}) Out[115]: a b c 0 0 a a 1 1 b b 2 2 NaN NaN 3 3 NaN d Same as the previous example, but use a regular expression for searching instead (dict of regex -> dict): In [116]: df.replace({"b": r"\s*\.\s*"}, {"b": np.nan}, regex=True) Out[116]: a b c 0 0 a a 1 1 b b 2 2 NaN NaN 3 3 NaN d You can pass nested dictionaries of regular expressions that use regex=True: In [117]: df.replace({"b": {"b": r""}}, regex=True) Out[117]: a b c 0 0 a a 1 1 b 2 2 . NaN 3 3 . d Alternatively, you can pass the nested dictionary like so: In [118]: df.replace(regex={"b": {r"\s*\.\s*": np.nan}}) Out[118]: a b c 0 0 a a 1 1 b b 2 2 NaN NaN 3 3 NaN d You can also use the group of a regular expression match when replacing (dict of regex -> dict of regex), this works for lists as well. In [119]: df.replace({"b": r"\s*(\.)\s*"}, {"b": r"\1ty"}, regex=True) Out[119]: a b c 0 0 a a 1 1 b b 2 2 .ty NaN 3 3 .ty d You can pass a list of regular expressions, of which those that match will be replaced with a scalar (list of regex -> regex). In [120]: df.replace([r"\s*\.\s*", r"a|b"], np.nan, regex=True) Out[120]: a b c 0 0 NaN NaN 1 1 NaN NaN 2 2 NaN NaN 3 3 NaN d All of the regular expression examples can also be passed with the to_replace argument as the regex argument. In this case the value argument must be passed explicitly by name or regex must be a nested dictionary. The previous example, in this case, would then be: In [121]: df.replace(regex=[r"\s*\.\s*", r"a|b"], value=np.nan) Out[121]: a b c 0 0 NaN NaN 1 1 NaN NaN 2 2 NaN NaN 3 3 NaN d This can be convenient if you do not want to pass regex=True every time you want to use a regular expression. Note Anywhere in the above replace examples that you see a regular expression a compiled regular expression is valid as well. Numeric replacement# replace() is similar to fillna(). In [122]: df = pd.DataFrame(np.random.randn(10, 2)) In [123]: df[np.random.rand(df.shape[0]) > 0.5] = 1.5 In [124]: df.replace(1.5, np.nan) Out[124]: 0 1 0 -0.844214 -1.021415 1 0.432396 -0.323580 2 0.423825 0.799180 3 1.262614 0.751965 4 NaN NaN 5 NaN NaN 6 -0.498174 -1.060799 7 0.591667 -0.183257 8 1.019855 -1.482465 9 NaN NaN Replacing more than one value is possible by passing a list. In [125]: df00 = df.iloc[0, 0] In [126]: df.replace([1.5, df00], [np.nan, "a"]) Out[126]: 0 1 0 a -1.021415 1 0.432396 -0.323580 2 0.423825 0.799180 3 1.262614 0.751965 4 NaN NaN 5 NaN NaN 6 -0.498174 -1.060799 7 0.591667 -0.183257 8 1.019855 -1.482465 9 NaN NaN In [127]: df[1].dtype Out[127]: dtype('float64') You can also operate on the DataFrame in place: In [128]: df.replace(1.5, np.nan, inplace=True) Missing data casting rules and indexing# While pandas supports storing arrays of integer and boolean type, these types are not capable of storing missing data. Until we can switch to using a native NA type in NumPy, we’ve established some “casting rules”. When a reindexing operation introduces missing data, the Series will be cast according to the rules introduced in the table below. data type Cast to integer float boolean object float no cast object no cast For example: In [129]: s = pd.Series(np.random.randn(5), index=[0, 2, 4, 6, 7]) In [130]: s > 0 Out[130]: 0 True 2 True 4 True 6 True 7 True dtype: bool In [131]: (s > 0).dtype Out[131]: dtype('bool') In [132]: crit = (s > 0).reindex(list(range(8))) In [133]: crit Out[133]: 0 True 1 NaN 2 True 3 NaN 4 True 5 NaN 6 True 7 True dtype: object In [134]: crit.dtype Out[134]: dtype('O') Ordinarily NumPy will complain if you try to use an object array (even if it contains boolean values) instead of a boolean array to get or set values from an ndarray (e.g. selecting values based on some criteria). If a boolean vector contains NAs, an exception will be generated: In [135]: reindexed = s.reindex(list(range(8))).fillna(0) In [136]: reindexed[crit] --------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[136], line 1 ----> 1 reindexed[crit] File ~/work/pandas/pandas/pandas/core/series.py:1002, in Series.__getitem__(self, key) 999 if is_iterator(key): 1000 key = list(key) -> 1002 if com.is_bool_indexer(key): 1003 key = check_bool_indexer(self.index, key) 1004 key = np.asarray(key, dtype=bool) File ~/work/pandas/pandas/pandas/core/common.py:135, in is_bool_indexer(key) 131 na_msg = "Cannot mask with non-boolean array containing NA / NaN values" 132 if lib.infer_dtype(key_array) == "boolean" and isna(key_array).any(): 133 # Don't raise on e.g. ["A", "B", np.nan], see 134 # test_loc_getitem_list_of_labels_categoricalindex_with_na --> 135 raise ValueError(na_msg) 136 return False 137 return True ValueError: Cannot mask with non-boolean array containing NA / NaN values However, these can be filled in using fillna() and it will work fine: In [137]: reindexed[crit.fillna(False)] Out[137]: 0 0.126504 2 0.696198 4 0.697416 6 0.601516 7 0.003659 dtype: float64 In [138]: reindexed[crit.fillna(True)] Out[138]: 0 0.126504 1 0.000000 2 0.696198 3 0.000000 4 0.697416 5 0.000000 6 0.601516 7 0.003659 dtype: float64 pandas provides a nullable integer dtype, but you must explicitly request it when creating the series or column. Notice that we use a capital “I” in the dtype="Int64". In [139]: s = pd.Series([0, 1, np.nan, 3, 4], dtype="Int64") In [140]: s Out[140]: 0 0 1 1 2 <NA> 3 3 4 4 dtype: Int64 See Nullable integer data type for more. Experimental NA scalar to denote missing values# Warning Experimental: the behaviour of pd.NA can still change without warning. New in version 1.0.0. Starting from pandas 1.0, an experimental pd.NA value (singleton) is available to represent scalar missing values. At this moment, it is used in the nullable integer, boolean and dedicated string data types as the missing value indicator. The goal of pd.NA is provide a “missing” indicator that can be used consistently across data types (instead of np.nan, None or pd.NaT depending on the data type). For example, when having missing values in a Series with the nullable integer dtype, it will use pd.NA: In [141]: s = pd.Series([1, 2, None], dtype="Int64") In [142]: s Out[142]: 0 1 1 2 2 <NA> dtype: Int64 In [143]: s[2] Out[143]: <NA> In [144]: s[2] is pd.NA Out[144]: True Currently, pandas does not yet use those data types by default (when creating a DataFrame or Series, or when reading in data), so you need to specify the dtype explicitly. An easy way to convert to those dtypes is explained here. Propagation in arithmetic and comparison operations# In general, missing values propagate in operations involving pd.NA. When one of the operands is unknown, the outcome of the operation is also unknown. For example, pd.NA propagates in arithmetic operations, similarly to np.nan: In [145]: pd.NA + 1 Out[145]: <NA> In [146]: "a" * pd.NA Out[146]: <NA> There are a few special cases when the result is known, even when one of the operands is NA. In [147]: pd.NA ** 0 Out[147]: 1 In [148]: 1 ** pd.NA Out[148]: 1 In equality and comparison operations, pd.NA also propagates. This deviates from the behaviour of np.nan, where comparisons with np.nan always return False. In [149]: pd.NA == 1 Out[149]: <NA> In [150]: pd.NA == pd.NA Out[150]: <NA> In [151]: pd.NA < 2.5 Out[151]: <NA> To check if a value is equal to pd.NA, the isna() function can be used: In [152]: pd.isna(pd.NA) Out[152]: True An exception on this basic propagation rule are reductions (such as the mean or the minimum), where pandas defaults to skipping missing values. See above for more. Logical operations# For logical operations, pd.NA follows the rules of the three-valued logic (or Kleene logic, similarly to R, SQL and Julia). This logic means to only propagate missing values when it is logically required. For example, for the logical “or” operation (|), if one of the operands is True, we already know the result will be True, regardless of the other value (so regardless the missing value would be True or False). In this case, pd.NA does not propagate: In [153]: True | False Out[153]: True In [154]: True | pd.NA Out[154]: True In [155]: pd.NA | True Out[155]: True On the other hand, if one of the operands is False, the result depends on the value of the other operand. Therefore, in this case pd.NA propagates: In [156]: False | True Out[156]: True In [157]: False | False Out[157]: False In [158]: False | pd.NA Out[158]: <NA> The behaviour of the logical “and” operation (&) can be derived using similar logic (where now pd.NA will not propagate if one of the operands is already False): In [159]: False & True Out[159]: False In [160]: False & False Out[160]: False In [161]: False & pd.NA Out[161]: False In [162]: True & True Out[162]: True In [163]: True & False Out[163]: False In [164]: True & pd.NA Out[164]: <NA> NA in a boolean context# Since the actual value of an NA is unknown, it is ambiguous to convert NA to a boolean value. The following raises an error: In [165]: bool(pd.NA) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[165], line 1 ----> 1 bool(pd.NA) File ~/work/pandas/pandas/pandas/_libs/missing.pyx:382, in pandas._libs.missing.NAType.__bool__() TypeError: boolean value of NA is ambiguous This also means that pd.NA cannot be used in a context where it is evaluated to a boolean, such as if condition: ... where condition can potentially be pd.NA. In such cases, isna() can be used to check for pd.NA or condition being pd.NA can be avoided, for example by filling missing values beforehand. A similar situation occurs when using Series or DataFrame objects in if statements, see Using if/truth statements with pandas. NumPy ufuncs# pandas.NA implements NumPy’s __array_ufunc__ protocol. Most ufuncs work with NA, and generally return NA: In [166]: np.log(pd.NA) Out[166]: <NA> In [167]: np.add(pd.NA, 1) Out[167]: <NA> Warning Currently, ufuncs involving an ndarray and NA will return an object-dtype filled with NA values. In [168]: a = np.array([1, 2, 3]) In [169]: np.greater(a, pd.NA) Out[169]: array([<NA>, <NA>, <NA>], dtype=object) The return type here may change to return a different array type in the future. See DataFrame interoperability with NumPy functions for more on ufuncs. Conversion# If you have a DataFrame or Series using traditional types that have missing data represented using np.nan, there are convenience methods convert_dtypes() in Series and convert_dtypes() in DataFrame that can convert data to use the newer dtypes for integers, strings and booleans listed here. This is especially helpful after reading in data sets when letting the readers such as read_csv() and read_excel() infer default dtypes. In this example, while the dtypes of all columns are changed, we show the results for the first 10 columns. In [170]: bb = pd.read_csv("data/baseball.csv", index_col="id") In [171]: bb[bb.columns[:10]].dtypes Out[171]: player object year int64 stint int64 team object lg object g int64 ab int64 r int64 h int64 X2b int64 dtype: object In [172]: bbn = bb.convert_dtypes() In [173]: bbn[bbn.columns[:10]].dtypes Out[173]: player string year Int64 stint Int64 team string lg string g Int64 ab Int64 r Int64 h Int64 X2b Int64 dtype: object
447
550
Group by Pandas and replace null value by value I have a huge Pandas dataframe df with over 4 million rows which looks like below. id value percent value_1 percent_1 0 1 0.530106 21%-31% NaN NaN 1 2 0.086647 10%-20% NaN NaN 2 3 0.073121 $30%-40% NaN NaN 3 4 0.76891 81%-90% NaN NaN 4 5 0.86536 41%-50% NaN NaN 5 1 NaN NaN 0.630106 91%-100% 6 2 NaN NaN 0.086647 11%-20% 7 3 NaN NaN 0.073121 $0%-10% 8 4 NaN NaN 0.376891 81%-90% 9 5 NaN NaN 0.186536 41%-50% I want a dataframe that looks like the below id value percent value_1 percent_1 0 1 0.530106 21%-31% 0.630106 91%-100% 1 2 0.086647 10%-20% 0.086647 11%-20% 2 3 0.073121 $30%-40% 0.073121 $0%-10% 3 4 0.76891 81%-90% 0.376891 81%-90% 4 5 0.86536 41%-50% 0.186536 41%-50% One hacky way to do this is replace NaN with empty string, convert the entire df columns into strings and group them df = df.replace(np.nan,'') df = df.astype(str) df.groupby(['id']).sum() But this takes a long time because groupby takes a lot of time with strings. Is there a better way to do this?
Let us try groupby with first which will skip the NaN value df=df.groupby('id').first().reset_index()
67,993,365
Identify distinct mappings of two overlapping columns in Pandas
<p>Suppose I have a Pandas dataframe with two identifier columns like this:</p> <pre><code>import pandas as pd so_fake_data = pd.DataFrame( { 'id_1': ['A', 'A', 'D', 'F', 'H', 'H', 'K', 'M'], 'id_2': ['B', 'C', 'E', 'G', 'I', 'J', 'L', 'L'] } ) </code></pre> <p><a href="https://i.stack.imgur.com/p6aCv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/p6aCv.png" alt="Resulting so_fake_data df" /></a></p> <p>Suppose my interpretation of this table is:</p> <ul> <li>A is related to B</li> <li>A is also related to C</li> <li>D is related to E</li> <li>(and so on)</li> </ul> <p>Using the first two points: If A is related to both B and C, I'd want to conclude that A, B, and C are all in the same group.</p> <p>Essentially, I want to be able to identify these groupings...:</p> <ul> <li>A, B, C</li> <li>D, E</li> <li>F, G</li> <li>H, I, J</li> <li>K, L, M</li> </ul> <p>... and then give them a grouping value in a new column like this, where I can distinguish each grouping:</p> <p><a href="https://i.stack.imgur.com/LH2EG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LH2EG.png" alt="Hoping to create this" /></a></p> <p>I appreciate anyone's help on this!</p>
67,993,405
"2021-06-15T20:53:02.537000"
1
null
2
61
python|pandas
<p>Sounds like a network issue, try with <code>networkx</code></p> <pre><code>import networkx as nx G = nx.from_pandas_edgelist(df, 'id_1', 'id_2') l = list(nx.connected_components(G)) l Out[66]: [{'A', 'B', 'C'}, {'D', 'E'}, {'F', 'G'}, {'H', 'I', 'J'}, {'K', 'L', 'M'}] </code></pre> <p>Then we can try</p> <pre><code>from functools import reduce d = reduce(lambda a, b: {**a, **b}, [dict.fromkeys(y,x) for x, y in enumerate(l)]) df['g'] = df.id_1.map(d) df Out[76]: id_1 id_2 g 0 A B 0 1 A C 0 2 D E 1 3 F G 2 4 H I 3 5 H J 3 6 K L 4 7 M L 4 </code></pre>
"2021-06-15T20:56:25.943000"
4
https://pandas.pydata.org/docs/dev/user_guide/merging.html
Merge, join, concatenate and compare# Merge, join, concatenate and compare# pandas provides various facilities for easily combining together Series or DataFrame with various kinds of set logic for the indexes and relational algebra functionality in the case of join / merge-type operations. In addition, pandas also provides utilities to compare two Series or DataFrame and summarize their differences. Concatenating objects# The concat() function (in the main pandas namespace) does all of the heavy lifting of performing concatenation operations along an axis while Sounds like a network issue, try with networkx import networkx as nx G = nx.from_pandas_edgelist(df, 'id_1', 'id_2') l = list(nx.connected_components(G)) l Out[66]: [{'A', 'B', 'C'}, {'D', 'E'}, {'F', 'G'}, {'H', 'I', 'J'}, {'K', 'L', 'M'}] Then we can try from functools import reduce d = reduce(lambda a, b: {**a, **b}, [dict.fromkeys(y,x) for x, y in enumerate(l)]) df['g'] = df.id_1.map(d) df Out[76]: id_1 id_2 g 0 A B 0 1 A C 0 2 D E 1 3 F G 2 4 H I 3 5 H J 3 6 K L 4 7 M L 4 performing optional set logic (union or intersection) of the indexes (if any) on the other axes. Note that I say “if any” because there is only a single possible axis of concatenation for Series. Before diving into all of the details of concat and what it can do, here is a simple example: In [1]: df1 = pd.DataFrame( ...: { ...: "A": ["A0", "A1", "A2", "A3"], ...: "B": ["B0", "B1", "B2", "B3"], ...: "C": ["C0", "C1", "C2", "C3"], ...: "D": ["D0", "D1", "D2", "D3"], ...: }, ...: index=[0, 1, 2, 3], ...: ) ...: In [2]: df2 = pd.DataFrame( ...: { ...: "A": ["A4", "A5", "A6", "A7"], ...: "B": ["B4", "B5", "B6", "B7"], ...: "C": ["C4", "C5", "C6", "C7"], ...: "D": ["D4", "D5", "D6", "D7"], ...: }, ...: index=[4, 5, 6, 7], ...: ) ...: In [3]: df3 = pd.DataFrame( ...: { ...: "A": ["A8", "A9", "A10", "A11"], ...: "B": ["B8", "B9", "B10", "B11"], ...: "C": ["C8", "C9", "C10", "C11"], ...: "D": ["D8", "D9", "D10", "D11"], ...: }, ...: index=[8, 9, 10, 11], ...: ) ...: In [4]: frames = [df1, df2, df3] In [5]: result = pd.concat(frames) Like its sibling function on ndarrays, numpy.concatenate, pandas.concat takes a list or dict of homogeneously-typed objects and concatenates them with some configurable handling of “what to do with the other axes”: pd.concat( objs, axis=0, join="outer", ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, copy=True, ) objs : a sequence or mapping of Series or DataFrame objects. If a dict is passed, the sorted keys will be used as the keys argument, unless it is passed, in which case the values will be selected (see below). Any None objects will be dropped silently unless they are all None in which case a ValueError will be raised. axis : {0, 1, …}, default 0. The axis to concatenate along. join : {‘inner’, ‘outer’}, default ‘outer’. How to handle indexes on other axis(es). Outer for union and inner for intersection. ignore_index : boolean, default False. If True, do not use the index values on the concatenation axis. The resulting axis will be labeled 0, …, n - 1. This is useful if you are concatenating objects where the concatenation axis does not have meaningful indexing information. Note the index values on the other axes are still respected in the join. keys : sequence, default None. Construct hierarchical index using the passed keys as the outermost level. If multiple levels passed, should contain tuples. levels : list of sequences, default None. Specific levels (unique values) to use for constructing a MultiIndex. Otherwise they will be inferred from the keys. names : list, default None. Names for the levels in the resulting hierarchical index. verify_integrity : boolean, default False. Check whether the new concatenated axis contains duplicates. This can be very expensive relative to the actual data concatenation. copy : boolean, default True. If False, do not copy data unnecessarily. Without a little bit of context many of these arguments don’t make much sense. Let’s revisit the above example. Suppose we wanted to associate specific keys with each of the pieces of the chopped up DataFrame. We can do this using the keys argument: In [6]: result = pd.concat(frames, keys=["x", "y", "z"]) As you can see (if you’ve read the rest of the documentation), the resulting object’s index has a hierarchical index. This means that we can now select out each chunk by key: In [7]: result.loc["y"] Out[7]: A B C D 4 A4 B4 C4 D4 5 A5 B5 C5 D5 6 A6 B6 C6 D6 7 A7 B7 C7 D7 It’s not a stretch to see how this can be very useful. More detail on this functionality below. Note It is worth noting that concat() makes a full copy of the data, and that constantly reusing this function can create a significant performance hit. If you need to use the operation over several datasets, use a list comprehension. frames = [ process_your_file(f) for f in files ] result = pd.concat(frames) Note When concatenating DataFrames with named axes, pandas will attempt to preserve these index/column names whenever possible. In the case where all inputs share a common name, this name will be assigned to the result. When the input names do not all agree, the result will be unnamed. The same is true for MultiIndex, but the logic is applied separately on a level-by-level basis. Set logic on the other axes# When gluing together multiple DataFrames, you have a choice of how to handle the other axes (other than the one being concatenated). This can be done in the following two ways: Take the union of them all, join='outer'. This is the default option as it results in zero information loss. Take the intersection, join='inner'. Here is an example of each of these methods. First, the default join='outer' behavior: In [8]: df4 = pd.DataFrame( ...: { ...: "B": ["B2", "B3", "B6", "B7"], ...: "D": ["D2", "D3", "D6", "D7"], ...: "F": ["F2", "F3", "F6", "F7"], ...: }, ...: index=[2, 3, 6, 7], ...: ) ...: In [9]: result = pd.concat([df1, df4], axis=1) Here is the same thing with join='inner': In [10]: result = pd.concat([df1, df4], axis=1, join="inner") Lastly, suppose we just wanted to reuse the exact index from the original DataFrame: In [11]: result = pd.concat([df1, df4], axis=1).reindex(df1.index) Similarly, we could index before the concatenation: In [12]: pd.concat([df1, df4.reindex(df1.index)], axis=1) Out[12]: A B C D B D F 0 A0 B0 C0 D0 NaN NaN NaN 1 A1 B1 C1 D1 NaN NaN NaN 2 A2 B2 C2 D2 B2 D2 F2 3 A3 B3 C3 D3 B3 D3 F3 Ignoring indexes on the concatenation axis# For DataFrame objects which don’t have a meaningful index, you may wish to append them and ignore the fact that they may have overlapping indexes. To do this, use the ignore_index argument: In [13]: result = pd.concat([df1, df4], ignore_index=True, sort=False) Concatenating with mixed ndims# You can concatenate a mix of Series and DataFrame objects. The Series will be transformed to DataFrame with the column name as the name of the Series. In [14]: s1 = pd.Series(["X0", "X1", "X2", "X3"], name="X") In [15]: result = pd.concat([df1, s1], axis=1) Note Since we’re concatenating a Series to a DataFrame, we could have achieved the same result with DataFrame.assign(). To concatenate an arbitrary number of pandas objects (DataFrame or Series), use concat. If unnamed Series are passed they will be numbered consecutively. In [16]: s2 = pd.Series(["_0", "_1", "_2", "_3"]) In [17]: result = pd.concat([df1, s2, s2, s2], axis=1) Passing ignore_index=True will drop all name references. In [18]: result = pd.concat([df1, s1], axis=1, ignore_index=True) More concatenating with group keys# A fairly common use of the keys argument is to override the column names when creating a new DataFrame based on existing Series. Notice how the default behaviour consists on letting the resulting DataFrame inherit the parent Series’ name, when these existed. In [19]: s3 = pd.Series([0, 1, 2, 3], name="foo") In [20]: s4 = pd.Series([0, 1, 2, 3]) In [21]: s5 = pd.Series([0, 1, 4, 5]) In [22]: pd.concat([s3, s4, s5], axis=1) Out[22]: foo 0 1 0 0 0 0 1 1 1 1 2 2 2 4 3 3 3 5 Through the keys argument we can override the existing column names. In [23]: pd.concat([s3, s4, s5], axis=1, keys=["red", "blue", "yellow"]) Out[23]: red blue yellow 0 0 0 0 1 1 1 1 2 2 2 4 3 3 3 5 Let’s consider a variation of the very first example presented: In [24]: result = pd.concat(frames, keys=["x", "y", "z"]) You can also pass a dict to concat in which case the dict keys will be used for the keys argument (unless other keys are specified): In [25]: pieces = {"x": df1, "y": df2, "z": df3} In [26]: result = pd.concat(pieces) In [27]: result = pd.concat(pieces, keys=["z", "y"]) The MultiIndex created has levels that are constructed from the passed keys and the index of the DataFrame pieces: In [28]: result.index.levels Out[28]: FrozenList([['z', 'y'], [4, 5, 6, 7, 8, 9, 10, 11]]) If you wish to specify other levels (as will occasionally be the case), you can do so using the levels argument: In [29]: result = pd.concat( ....: pieces, keys=["x", "y", "z"], levels=[["z", "y", "x", "w"]], names=["group_key"] ....: ) ....: In [30]: result.index.levels Out[30]: FrozenList([['z', 'y', 'x', 'w'], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]) This is fairly esoteric, but it is actually necessary for implementing things like GroupBy where the order of a categorical variable is meaningful. Appending rows to a DataFrame# If you have a series that you want to append as a single row to a DataFrame, you can convert the row into a DataFrame and use concat In [31]: s2 = pd.Series(["X0", "X1", "X2", "X3"], index=["A", "B", "C", "D"]) In [32]: result = pd.concat([df1, s2.to_frame().T], ignore_index=True) You should use ignore_index with this method to instruct DataFrame to discard its index. If you wish to preserve the index, you should construct an appropriately-indexed DataFrame and append or concatenate those objects. Database-style DataFrame or named Series joining/merging# pandas has full-featured, high performance in-memory join operations idiomatically very similar to relational databases like SQL. These methods perform significantly better (in some cases well over an order of magnitude better) than other open source implementations (like base::merge.data.frame in R). The reason for this is careful algorithmic design and the internal layout of the data in DataFrame. See the cookbook for some advanced strategies. Users who are familiar with SQL but new to pandas might be interested in a comparison with SQL. pandas provides a single function, merge(), as the entry point for all standard database join operations between DataFrame or named Series objects: pd.merge( left, right, how="inner", on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=True, suffixes=("_x", "_y"), copy=True, indicator=False, validate=None, ) left: A DataFrame or named Series object. right: Another DataFrame or named Series object. on: Column or index level names to join on. Must be found in both the left and right DataFrame and/or Series objects. If not passed and left_index and right_index are False, the intersection of the columns in the DataFrames and/or Series will be inferred to be the join keys. left_on: Columns or index levels from the left DataFrame or Series to use as keys. Can either be column names, index level names, or arrays with length equal to the length of the DataFrame or Series. right_on: Columns or index levels from the right DataFrame or Series to use as keys. Can either be column names, index level names, or arrays with length equal to the length of the DataFrame or Series. left_index: If True, use the index (row labels) from the left DataFrame or Series as its join key(s). In the case of a DataFrame or Series with a MultiIndex (hierarchical), the number of levels must match the number of join keys from the right DataFrame or Series. right_index: Same usage as left_index for the right DataFrame or Series how: One of 'left', 'right', 'outer', 'inner', 'cross'. Defaults to inner. See below for more detailed description of each method. sort: Sort the result DataFrame by the join keys in lexicographical order. Defaults to True, setting to False will improve performance substantially in many cases. suffixes: A tuple of string suffixes to apply to overlapping columns. Defaults to ('_x', '_y'). copy: Always copy data (default True) from the passed DataFrame or named Series objects, even when reindexing is not necessary. Cannot be avoided in many cases but may improve performance / memory usage. The cases where copying can be avoided are somewhat pathological but this option is provided nonetheless. indicator: Add a column to the output DataFrame called _merge with information on the source of each row. _merge is Categorical-type and takes on a value of left_only for observations whose merge key only appears in 'left' DataFrame or Series, right_only for observations whose merge key only appears in 'right' DataFrame or Series, and both if the observation’s merge key is found in both. validate : string, default None. If specified, checks if merge is of specified type. “one_to_one” or “1:1”: checks if merge keys are unique in both left and right datasets. “one_to_many” or “1:m”: checks if merge keys are unique in left dataset. “many_to_one” or “m:1”: checks if merge keys are unique in right dataset. “many_to_many” or “m:m”: allowed, but does not result in checks. Note Support for specifying index levels as the on, left_on, and right_on parameters was added in version 0.23.0. Support for merging named Series objects was added in version 0.24.0. The return type will be the same as left. If left is a DataFrame or named Series and right is a subclass of DataFrame, the return type will still be DataFrame. merge is a function in the pandas namespace, and it is also available as a DataFrame instance method merge(), with the calling DataFrame being implicitly considered the left object in the join. The related join() method, uses merge internally for the index-on-index (by default) and column(s)-on-index join. If you are joining on index only, you may wish to use DataFrame.join to save yourself some typing. Brief primer on merge methods (relational algebra)# Experienced users of relational databases like SQL will be familiar with the terminology used to describe join operations between two SQL-table like structures (DataFrame objects). There are several cases to consider which are very important to understand: one-to-one joins: for example when joining two DataFrame objects on their indexes (which must contain unique values). many-to-one joins: for example when joining an index (unique) to one or more columns in a different DataFrame. many-to-many joins: joining columns on columns. Note When joining columns on columns (potentially a many-to-many join), any indexes on the passed DataFrame objects will be discarded. It is worth spending some time understanding the result of the many-to-many join case. In SQL / standard relational algebra, if a key combination appears more than once in both tables, the resulting table will have the Cartesian product of the associated data. Here is a very basic example with one unique key combination: In [33]: left = pd.DataFrame( ....: { ....: "key": ["K0", "K1", "K2", "K3"], ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: } ....: ) ....: In [34]: right = pd.DataFrame( ....: { ....: "key": ["K0", "K1", "K2", "K3"], ....: "C": ["C0", "C1", "C2", "C3"], ....: "D": ["D0", "D1", "D2", "D3"], ....: } ....: ) ....: In [35]: result = pd.merge(left, right, on="key") Here is a more complicated example with multiple join keys. Only the keys appearing in left and right are present (the intersection), since how='inner' by default. In [36]: left = pd.DataFrame( ....: { ....: "key1": ["K0", "K0", "K1", "K2"], ....: "key2": ["K0", "K1", "K0", "K1"], ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: } ....: ) ....: In [37]: right = pd.DataFrame( ....: { ....: "key1": ["K0", "K1", "K1", "K2"], ....: "key2": ["K0", "K0", "K0", "K0"], ....: "C": ["C0", "C1", "C2", "C3"], ....: "D": ["D0", "D1", "D2", "D3"], ....: } ....: ) ....: In [38]: result = pd.merge(left, right, on=["key1", "key2"]) The how argument to merge specifies how to determine which keys are to be included in the resulting table. If a key combination does not appear in either the left or right tables, the values in the joined table will be NA. Here is a summary of the how options and their SQL equivalent names: Merge method SQL Join Name Description left LEFT OUTER JOIN Use keys from left frame only right RIGHT OUTER JOIN Use keys from right frame only outer FULL OUTER JOIN Use union of keys from both frames inner INNER JOIN Use intersection of keys from both frames cross CROSS JOIN Create the cartesian product of rows of both frames In [39]: result = pd.merge(left, right, how="left", on=["key1", "key2"]) In [40]: result = pd.merge(left, right, how="right", on=["key1", "key2"]) In [41]: result = pd.merge(left, right, how="outer", on=["key1", "key2"]) In [42]: result = pd.merge(left, right, how="inner", on=["key1", "key2"]) In [43]: result = pd.merge(left, right, how="cross") You can merge a mult-indexed Series and a DataFrame, if the names of the MultiIndex correspond to the columns from the DataFrame. Transform the Series to a DataFrame using Series.reset_index() before merging, as shown in the following example. In [44]: df = pd.DataFrame({"Let": ["A", "B", "C"], "Num": [1, 2, 3]}) In [45]: df Out[45]: Let Num 0 A 1 1 B 2 2 C 3 In [46]: ser = pd.Series( ....: ["a", "b", "c", "d", "e", "f"], ....: index=pd.MultiIndex.from_arrays( ....: [["A", "B", "C"] * 2, [1, 2, 3, 4, 5, 6]], names=["Let", "Num"] ....: ), ....: ) ....: In [47]: ser Out[47]: Let Num A 1 a B 2 b C 3 c A 4 d B 5 e C 6 f dtype: object In [48]: pd.merge(df, ser.reset_index(), on=["Let", "Num"]) Out[48]: Let Num 0 0 A 1 a 1 B 2 b 2 C 3 c Here is another example with duplicate join keys in DataFrames: In [49]: left = pd.DataFrame({"A": [1, 2], "B": [2, 2]}) In [50]: right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]}) In [51]: result = pd.merge(left, right, on="B", how="outer") Warning Joining / merging on duplicate keys can cause a returned frame that is the multiplication of the row dimensions, which may result in memory overflow. It is the user’ s responsibility to manage duplicate values in keys before joining large DataFrames. Checking for duplicate keys# Users can use the validate argument to automatically check whether there are unexpected duplicates in their merge keys. Key uniqueness is checked before merge operations and so should protect against memory overflows. Checking key uniqueness is also a good way to ensure user data structures are as expected. In the following example, there are duplicate values of B in the right DataFrame. As this is not a one-to-one merge – as specified in the validate argument – an exception will be raised. In [52]: left = pd.DataFrame({"A": [1, 2], "B": [1, 2]}) In [53]: right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]}) In [53]: result = pd.merge(left, right, on="B", how="outer", validate="one_to_one") ... MergeError: Merge keys are not unique in right dataset; not a one-to-one merge If the user is aware of the duplicates in the right DataFrame but wants to ensure there are no duplicates in the left DataFrame, one can use the validate='one_to_many' argument instead, which will not raise an exception. In [54]: pd.merge(left, right, on="B", how="outer", validate="one_to_many") Out[54]: A_x B A_y 0 1 1 NaN 1 2 2 4.0 2 2 2 5.0 3 2 2 6.0 The merge indicator# merge() accepts the argument indicator. If True, a Categorical-type column called _merge will be added to the output object that takes on values: Observation Origin _merge value Merge key only in 'left' frame left_only Merge key only in 'right' frame right_only Merge key in both frames both In [55]: df1 = pd.DataFrame({"col1": [0, 1], "col_left": ["a", "b"]}) In [56]: df2 = pd.DataFrame({"col1": [1, 2, 2], "col_right": [2, 2, 2]}) In [57]: pd.merge(df1, df2, on="col1", how="outer", indicator=True) Out[57]: col1 col_left col_right _merge 0 0 a NaN left_only 1 1 b 2.0 both 2 2 NaN 2.0 right_only 3 2 NaN 2.0 right_only The indicator argument will also accept string arguments, in which case the indicator function will use the value of the passed string as the name for the indicator column. In [58]: pd.merge(df1, df2, on="col1", how="outer", indicator="indicator_column") Out[58]: col1 col_left col_right indicator_column 0 0 a NaN left_only 1 1 b 2.0 both 2 2 NaN 2.0 right_only 3 2 NaN 2.0 right_only Merge dtypes# Merging will preserve the dtype of the join keys. In [59]: left = pd.DataFrame({"key": [1], "v1": [10]}) In [60]: left Out[60]: key v1 0 1 10 In [61]: right = pd.DataFrame({"key": [1, 2], "v1": [20, 30]}) In [62]: right Out[62]: key v1 0 1 20 1 2 30 We are able to preserve the join keys: In [63]: pd.merge(left, right, how="outer") Out[63]: key v1 0 1 10 1 1 20 2 2 30 In [64]: pd.merge(left, right, how="outer").dtypes Out[64]: key int64 v1 int64 dtype: object Of course if you have missing values that are introduced, then the resulting dtype will be upcast. In [65]: pd.merge(left, right, how="outer", on="key") Out[65]: key v1_x v1_y 0 1 10.0 20 1 2 NaN 30 In [66]: pd.merge(left, right, how="outer", on="key").dtypes Out[66]: key int64 v1_x float64 v1_y int64 dtype: object Merging will preserve category dtypes of the mergands. See also the section on categoricals. The left frame. In [67]: from pandas.api.types import CategoricalDtype In [68]: X = pd.Series(np.random.choice(["foo", "bar"], size=(10,))) In [69]: X = X.astype(CategoricalDtype(categories=["foo", "bar"])) In [70]: left = pd.DataFrame( ....: {"X": X, "Y": np.random.choice(["one", "two", "three"], size=(10,))} ....: ) ....: In [71]: left Out[71]: X Y 0 bar one 1 foo one 2 foo three 3 bar three 4 foo one 5 bar one 6 bar three 7 bar three 8 bar three 9 foo three In [72]: left.dtypes Out[72]: X category Y object dtype: object The right frame. In [73]: right = pd.DataFrame( ....: { ....: "X": pd.Series(["foo", "bar"], dtype=CategoricalDtype(["foo", "bar"])), ....: "Z": [1, 2], ....: } ....: ) ....: In [74]: right Out[74]: X Z 0 foo 1 1 bar 2 In [75]: right.dtypes Out[75]: X category Z int64 dtype: object The merged result: In [76]: result = pd.merge(left, right, how="outer") In [77]: result Out[77]: X Y Z 0 bar one 2 1 bar three 2 2 bar one 2 3 bar three 2 4 bar three 2 5 bar three 2 6 foo one 1 7 foo three 1 8 foo one 1 9 foo three 1 In [78]: result.dtypes Out[78]: X category Y object Z int64 dtype: object Note The category dtypes must be exactly the same, meaning the same categories and the ordered attribute. Otherwise the result will coerce to the categories’ dtype. Note Merging on category dtypes that are the same can be quite performant compared to object dtype merging. Joining on index# DataFrame.join() is a convenient method for combining the columns of two potentially differently-indexed DataFrames into a single result DataFrame. Here is a very basic example: In [79]: left = pd.DataFrame( ....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=["K0", "K1", "K2"] ....: ) ....: In [80]: right = pd.DataFrame( ....: {"C": ["C0", "C2", "C3"], "D": ["D0", "D2", "D3"]}, index=["K0", "K2", "K3"] ....: ) ....: In [81]: result = left.join(right) In [82]: result = left.join(right, how="outer") The same as above, but with how='inner'. In [83]: result = left.join(right, how="inner") The data alignment here is on the indexes (row labels). This same behavior can be achieved using merge plus additional arguments instructing it to use the indexes: In [84]: result = pd.merge(left, right, left_index=True, right_index=True, how="outer") In [85]: result = pd.merge(left, right, left_index=True, right_index=True, how="inner") Joining key columns on an index# join() takes an optional on argument which may be a column or multiple column names, which specifies that the passed DataFrame is to be aligned on that column in the DataFrame. These two function calls are completely equivalent: left.join(right, on=key_or_keys) pd.merge( left, right, left_on=key_or_keys, right_index=True, how="left", sort=False ) Obviously you can choose whichever form you find more convenient. For many-to-one joins (where one of the DataFrame’s is already indexed by the join key), using join may be more convenient. Here is a simple example: In [86]: left = pd.DataFrame( ....: { ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: "key": ["K0", "K1", "K0", "K1"], ....: } ....: ) ....: In [87]: right = pd.DataFrame({"C": ["C0", "C1"], "D": ["D0", "D1"]}, index=["K0", "K1"]) In [88]: result = left.join(right, on="key") In [89]: result = pd.merge( ....: left, right, left_on="key", right_index=True, how="left", sort=False ....: ) ....: To join on multiple keys, the passed DataFrame must have a MultiIndex: In [90]: left = pd.DataFrame( ....: { ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: "key1": ["K0", "K0", "K1", "K2"], ....: "key2": ["K0", "K1", "K0", "K1"], ....: } ....: ) ....: In [91]: index = pd.MultiIndex.from_tuples( ....: [("K0", "K0"), ("K1", "K0"), ("K2", "K0"), ("K2", "K1")] ....: ) ....: In [92]: right = pd.DataFrame( ....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=index ....: ) ....: Now this can be joined by passing the two key column names: In [93]: result = left.join(right, on=["key1", "key2"]) The default for DataFrame.join is to perform a left join (essentially a “VLOOKUP” operation, for Excel users), which uses only the keys found in the calling DataFrame. Other join types, for example inner join, can be just as easily performed: In [94]: result = left.join(right, on=["key1", "key2"], how="inner") As you can see, this drops any rows where there was no match. Joining a single Index to a MultiIndex# You can join a singly-indexed DataFrame with a level of a MultiIndexed DataFrame. The level will match on the name of the index of the singly-indexed frame against a level name of the MultiIndexed frame. In [95]: left = pd.DataFrame( ....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, ....: index=pd.Index(["K0", "K1", "K2"], name="key"), ....: ) ....: In [96]: index = pd.MultiIndex.from_tuples( ....: [("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")], ....: names=["key", "Y"], ....: ) ....: In [97]: right = pd.DataFrame( ....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, ....: index=index, ....: ) ....: In [98]: result = left.join(right, how="inner") This is equivalent but less verbose and more memory efficient / faster than this. In [99]: result = pd.merge( ....: left.reset_index(), right.reset_index(), on=["key"], how="inner" ....: ).set_index(["key","Y"]) ....: Joining with two MultiIndexes# This is supported in a limited way, provided that the index for the right argument is completely used in the join, and is a subset of the indices in the left argument, as in this example: In [100]: leftindex = pd.MultiIndex.from_product( .....: [list("abc"), list("xy"), [1, 2]], names=["abc", "xy", "num"] .....: ) .....: In [101]: left = pd.DataFrame({"v1": range(12)}, index=leftindex) In [102]: left Out[102]: v1 abc xy num a x 1 0 2 1 y 1 2 2 3 b x 1 4 2 5 y 1 6 2 7 c x 1 8 2 9 y 1 10 2 11 In [103]: rightindex = pd.MultiIndex.from_product( .....: [list("abc"), list("xy")], names=["abc", "xy"] .....: ) .....: In [104]: right = pd.DataFrame({"v2": [100 * i for i in range(1, 7)]}, index=rightindex) In [105]: right Out[105]: v2 abc xy a x 100 y 200 b x 300 y 400 c x 500 y 600 In [106]: left.join(right, on=["abc", "xy"], how="inner") Out[106]: v1 v2 abc xy num a x 1 0 100 2 1 100 y 1 2 200 2 3 200 b x 1 4 300 2 5 300 y 1 6 400 2 7 400 c x 1 8 500 2 9 500 y 1 10 600 2 11 600 If that condition is not satisfied, a join with two multi-indexes can be done using the following code. In [107]: leftindex = pd.MultiIndex.from_tuples( .....: [("K0", "X0"), ("K0", "X1"), ("K1", "X2")], names=["key", "X"] .....: ) .....: In [108]: left = pd.DataFrame( .....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=leftindex .....: ) .....: In [109]: rightindex = pd.MultiIndex.from_tuples( .....: [("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")], names=["key", "Y"] .....: ) .....: In [110]: right = pd.DataFrame( .....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=rightindex .....: ) .....: In [111]: result = pd.merge( .....: left.reset_index(), right.reset_index(), on=["key"], how="inner" .....: ).set_index(["key", "X", "Y"]) .....: Merging on a combination of columns and index levels# Strings passed as the on, left_on, and right_on parameters may refer to either column names or index level names. This enables merging DataFrame instances on a combination of index levels and columns without resetting indexes. In [112]: left_index = pd.Index(["K0", "K0", "K1", "K2"], name="key1") In [113]: left = pd.DataFrame( .....: { .....: "A": ["A0", "A1", "A2", "A3"], .....: "B": ["B0", "B1", "B2", "B3"], .....: "key2": ["K0", "K1", "K0", "K1"], .....: }, .....: index=left_index, .....: ) .....: In [114]: right_index = pd.Index(["K0", "K1", "K2", "K2"], name="key1") In [115]: right = pd.DataFrame( .....: { .....: "C": ["C0", "C1", "C2", "C3"], .....: "D": ["D0", "D1", "D2", "D3"], .....: "key2": ["K0", "K0", "K0", "K1"], .....: }, .....: index=right_index, .....: ) .....: In [116]: result = left.merge(right, on=["key1", "key2"]) Note When DataFrames are merged on a string that matches an index level in both frames, the index level is preserved as an index level in the resulting DataFrame. Note When DataFrames are merged using only some of the levels of a MultiIndex, the extra levels will be dropped from the resulting merge. In order to preserve those levels, use reset_index on those level names to move those levels to columns prior to doing the merge. Note If a string matches both a column name and an index level name, then a warning is issued and the column takes precedence. This will result in an ambiguity error in a future version. Overlapping value columns# The merge suffixes argument takes a tuple of list of strings to append to overlapping column names in the input DataFrames to disambiguate the result columns: In [117]: left = pd.DataFrame({"k": ["K0", "K1", "K2"], "v": [1, 2, 3]}) In [118]: right = pd.DataFrame({"k": ["K0", "K0", "K3"], "v": [4, 5, 6]}) In [119]: result = pd.merge(left, right, on="k") In [120]: result = pd.merge(left, right, on="k", suffixes=("_l", "_r")) DataFrame.join() has lsuffix and rsuffix arguments which behave similarly. In [121]: left = left.set_index("k") In [122]: right = right.set_index("k") In [123]: result = left.join(right, lsuffix="_l", rsuffix="_r") Joining multiple DataFrames# A list or tuple of DataFrames can also be passed to join() to join them together on their indexes. In [124]: right2 = pd.DataFrame({"v": [7, 8, 9]}, index=["K1", "K1", "K2"]) In [125]: result = left.join([right, right2]) Merging together values within Series or DataFrame columns# Another fairly common situation is to have two like-indexed (or similarly indexed) Series or DataFrame objects and wanting to “patch” values in one object from values for matching indices in the other. Here is an example: In [126]: df1 = pd.DataFrame( .....: [[np.nan, 3.0, 5.0], [-4.6, np.nan, np.nan], [np.nan, 7.0, np.nan]] .....: ) .....: In [127]: df2 = pd.DataFrame([[-42.6, np.nan, -8.2], [-5.0, 1.6, 4]], index=[1, 2]) For this, use the combine_first() method: In [128]: result = df1.combine_first(df2) Note that this method only takes values from the right DataFrame if they are missing in the left DataFrame. A related method, update(), alters non-NA values in place: In [129]: df1.update(df2) Timeseries friendly merging# Merging ordered data# A merge_ordered() function allows combining time series and other ordered data. In particular it has an optional fill_method keyword to fill/interpolate missing data: In [130]: left = pd.DataFrame( .....: {"k": ["K0", "K1", "K1", "K2"], "lv": [1, 2, 3, 4], "s": ["a", "b", "c", "d"]} .....: ) .....: In [131]: right = pd.DataFrame({"k": ["K1", "K2", "K4"], "rv": [1, 2, 3]}) In [132]: pd.merge_ordered(left, right, fill_method="ffill", left_by="s") Out[132]: k lv s rv 0 K0 1.0 a NaN 1 K1 1.0 a 1.0 2 K2 1.0 a 2.0 3 K4 1.0 a 3.0 4 K1 2.0 b 1.0 5 K2 2.0 b 2.0 6 K4 2.0 b 3.0 7 K1 3.0 c 1.0 8 K2 3.0 c 2.0 9 K4 3.0 c 3.0 10 K1 NaN d 1.0 11 K2 4.0 d 2.0 12 K4 4.0 d 3.0 Merging asof# A merge_asof() is similar to an ordered left-join except that we match on nearest key rather than equal keys. For each row in the left DataFrame, we select the last row in the right DataFrame whose on key is less than the left’s key. Both DataFrames must be sorted by the key. Optionally an asof merge can perform a group-wise merge. This matches the by key equally, in addition to the nearest match on the on key. For example; we might have trades and quotes and we want to asof merge them. In [133]: trades = pd.DataFrame( .....: { .....: "time": pd.to_datetime( .....: [ .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.038", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.048", .....: ] .....: ), .....: "ticker": ["MSFT", "MSFT", "GOOG", "GOOG", "AAPL"], .....: "price": [51.95, 51.95, 720.77, 720.92, 98.00], .....: "quantity": [75, 155, 100, 100, 100], .....: }, .....: columns=["time", "ticker", "price", "quantity"], .....: ) .....: In [134]: quotes = pd.DataFrame( .....: { .....: "time": pd.to_datetime( .....: [ .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.030", .....: "20160525 13:30:00.041", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.049", .....: "20160525 13:30:00.072", .....: "20160525 13:30:00.075", .....: ] .....: ), .....: "ticker": ["GOOG", "MSFT", "MSFT", "MSFT", "GOOG", "AAPL", "GOOG", "MSFT"], .....: "bid": [720.50, 51.95, 51.97, 51.99, 720.50, 97.99, 720.50, 52.01], .....: "ask": [720.93, 51.96, 51.98, 52.00, 720.93, 98.01, 720.88, 52.03], .....: }, .....: columns=["time", "ticker", "bid", "ask"], .....: ) .....: In [135]: trades Out[135]: time ticker price quantity 0 2016-05-25 13:30:00.023 MSFT 51.95 75 1 2016-05-25 13:30:00.038 MSFT 51.95 155 2 2016-05-25 13:30:00.048 GOOG 720.77 100 3 2016-05-25 13:30:00.048 GOOG 720.92 100 4 2016-05-25 13:30:00.048 AAPL 98.00 100 In [136]: quotes Out[136]: time ticker bid ask 0 2016-05-25 13:30:00.023 GOOG 720.50 720.93 1 2016-05-25 13:30:00.023 MSFT 51.95 51.96 2 2016-05-25 13:30:00.030 MSFT 51.97 51.98 3 2016-05-25 13:30:00.041 MSFT 51.99 52.00 4 2016-05-25 13:30:00.048 GOOG 720.50 720.93 5 2016-05-25 13:30:00.049 AAPL 97.99 98.01 6 2016-05-25 13:30:00.072 GOOG 720.50 720.88 7 2016-05-25 13:30:00.075 MSFT 52.01 52.03 By default we are taking the asof of the quotes. In [137]: pd.merge_asof(trades, quotes, on="time", by="ticker") Out[137]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 51.95 51.96 1 2016-05-25 13:30:00.038 MSFT 51.95 155 51.97 51.98 2 2016-05-25 13:30:00.048 GOOG 720.77 100 720.50 720.93 3 2016-05-25 13:30:00.048 GOOG 720.92 100 720.50 720.93 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN We only asof within 2ms between the quote time and the trade time. In [138]: pd.merge_asof(trades, quotes, on="time", by="ticker", tolerance=pd.Timedelta("2ms")) Out[138]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 51.95 51.96 1 2016-05-25 13:30:00.038 MSFT 51.95 155 NaN NaN 2 2016-05-25 13:30:00.048 GOOG 720.77 100 720.50 720.93 3 2016-05-25 13:30:00.048 GOOG 720.92 100 720.50 720.93 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN We only asof within 10ms between the quote time and the trade time and we exclude exact matches on time. Note that though we exclude the exact matches (of the quotes), prior quotes do propagate to that point in time. In [139]: pd.merge_asof( .....: trades, .....: quotes, .....: on="time", .....: by="ticker", .....: tolerance=pd.Timedelta("10ms"), .....: allow_exact_matches=False, .....: ) .....: Out[139]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 NaN NaN 1 2016-05-25 13:30:00.038 MSFT 51.95 155 51.97 51.98 2 2016-05-25 13:30:00.048 GOOG 720.77 100 NaN NaN 3 2016-05-25 13:30:00.048 GOOG 720.92 100 NaN NaN 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN Comparing objects# The compare() and compare() methods allow you to compare two DataFrame or Series, respectively, and summarize their differences. This feature was added in V1.1.0. For example, you might want to compare two DataFrame and stack their differences side by side. In [140]: df = pd.DataFrame( .....: { .....: "col1": ["a", "a", "b", "b", "a"], .....: "col2": [1.0, 2.0, 3.0, np.nan, 5.0], .....: "col3": [1.0, 2.0, 3.0, 4.0, 5.0], .....: }, .....: columns=["col1", "col2", "col3"], .....: ) .....: In [141]: df Out[141]: col1 col2 col3 0 a 1.0 1.0 1 a 2.0 2.0 2 b 3.0 3.0 3 b NaN 4.0 4 a 5.0 5.0 In [142]: df2 = df.copy() In [143]: df2.loc[0, "col1"] = "c" In [144]: df2.loc[2, "col3"] = 4.0 In [145]: df2 Out[145]: col1 col2 col3 0 c 1.0 1.0 1 a 2.0 2.0 2 b 3.0 4.0 3 b NaN 4.0 4 a 5.0 5.0 In [146]: df.compare(df2) Out[146]: col1 col3 self other self other 0 a c NaN NaN 2 NaN NaN 3.0 4.0 By default, if two corresponding values are equal, they will be shown as NaN. Furthermore, if all values in an entire row / column, the row / column will be omitted from the result. The remaining differences will be aligned on columns. If you wish, you may choose to stack the differences on rows. In [147]: df.compare(df2, align_axis=0) Out[147]: col1 col3 0 self a NaN other c NaN 2 self NaN 3.0 other NaN 4.0 If you wish to keep all original rows and columns, set keep_shape argument to True. In [148]: df.compare(df2, keep_shape=True) Out[148]: col1 col2 col3 self other self other self other 0 a c NaN NaN NaN NaN 1 NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN 3.0 4.0 3 NaN NaN NaN NaN NaN NaN 4 NaN NaN NaN NaN NaN NaN You may also keep all the original values even if they are equal. In [149]: df.compare(df2, keep_shape=True, keep_equal=True) Out[149]: col1 col2 col3 self other self other self other 0 a c 1.0 1.0 1.0 1.0 1 a a 2.0 2.0 2.0 2.0 2 b b 3.0 3.0 3.0 4.0 3 b b NaN NaN 4.0 4.0 4 a a 5.0 5.0 5.0 5.0
571
1,114
Identify distinct mappings of two overlapping columns in Pandas Suppose I have a Pandas dataframe with two identifier columns like this: import pandas as pd so_fake_data = pd.DataFrame( { 'id_1': ['A', 'A', 'D', 'F', 'H', 'H', 'K', 'M'], 'id_2': ['B', 'C', 'E', 'G', 'I', 'J', 'L', 'L'] } ) Suppose my interpretation of this table is: A is related to B A is also related to C D is related to E (and so on) Using the first two points: If A is related to both B and C, I'd want to conclude that A, B, and C are all in the same group. Essentially, I want to be able to identify these groupings...: A, B, C D, E F, G H, I, J K, L, M ... and then give them a grouping value in a new column like this, where I can distinguish each grouping: I appreciate anyone's help on this!
Sounds like a network issue, try with networkx import networkx as nx G = nx.from_pandas_edgelist(df, 'id_1', 'id_2') l = list(nx.connected_components(G)) l Out[66]: [{'A', 'B', 'C'}, {'D', 'E'}, {'F', 'G'}, {'H', 'I', 'J'}, {'K', 'L', 'M'}] Then we can try from functools import reduce d = reduce(lambda a, b: {**a, **b}, [dict.fromkeys(y,x) for x, y in enumerate(l)]) df['g'] = df.id_1.map(d) df Out[76]: id_1 id_2 g 0 A B 0 1 A C 0 2 D E 1 3 F G 2 4 H I 3 5 H J 3 6 K L 4 7 M L 4
68,938,503
Confusion about modifying column in dataframe with pandas
<p>I'm working on a Bangaluru House Price Data csv from Kaggle. There is a column called 'total_sqft'. In this column, there are values that are a range of numbers (e.g.: 1000-1500), and I want to identify all those entries. I created this function to do so:</p> <pre><code>def is_float(x): try: float(x) except: return False return True </code></pre> <p>I applied it to the column:</p> <pre><code>df3[~df3['total_sqft'].apply(is_float)] </code></pre> <p>This works, but I don't understand why this doesn't:</p> <pre><code>df3['total_sqft'] = ~df3['total_sqft'].apply(is_float) </code></pre> <p>This just returns 'False' for everything instead of the actual entries</p>
68,949,038
"2021-08-26T12:27:12.147000"
1
null
0
27
pandas
<p><strong>Answer from comment:</strong></p> <blockquote> <p>In the first version you are selecting the rows that contain true values from the apply function. In the second you are setting the values to be the values of the apply function. Tilde means negation btw.</p> </blockquote>
"2021-08-27T06:43:48.737000"
0
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.melt.html
pandas.DataFrame.melt# pandas.DataFrame.melt# DataFrame.melt(id_vars=None, value_vars=None, var_name=None, value_name='value', col_level=None, ignore_index=True)[source]# Unpivot a DataFrame from wide to long format, optionally leaving identifiers set. This function is useful to massage a DataFrame into a format where one or more columns are identifier variables (id_vars), while all other columns, considered measured variables (value_vars), are “unpivoted” to the row axis, leaving just two non-identifier columns, ‘variable’ and ‘value’. Parameters id_varstuple, list, or ndarray, optionalColumn(s) to use as identifier variables. value_varstuple, list, or ndarray, optionalColumn(s) to unpivot. If not specified, uses all columns that are not set as id_vars. var_namescalarName to use for the ‘variable’ column. If None it uses Answer from comment: In the first version you are selecting the rows that contain true values from the apply function. In the second you are setting the values to be the values of the apply function. Tilde means negation btw. frame.columns.name or ‘variable’. value_namescalar, default ‘value’Name to use for the ‘value’ column. col_levelint or str, optionalIf columns are a MultiIndex then use this level to melt. ignore_indexbool, default TrueIf True, original index is ignored. If False, the original index is retained. Index labels will be repeated as necessary. New in version 1.1.0. Returns DataFrameUnpivoted DataFrame. See also meltIdentical method. pivot_tableCreate a spreadsheet-style pivot table as a DataFrame. DataFrame.pivotReturn reshaped DataFrame organized by given index / column values. DataFrame.explodeExplode a DataFrame from list-like columns to long format. Notes Reference the user guide for more examples. Examples >>> df = pd.DataFrame({'A': {0: 'a', 1: 'b', 2: 'c'}, ... 'B': {0: 1, 1: 3, 2: 5}, ... 'C': {0: 2, 1: 4, 2: 6}}) >>> df A B C 0 a 1 2 1 b 3 4 2 c 5 6 >>> df.melt(id_vars=['A'], value_vars=['B']) A variable value 0 a B 1 1 b B 3 2 c B 5 >>> df.melt(id_vars=['A'], value_vars=['B', 'C']) A variable value 0 a B 1 1 b B 3 2 c B 5 3 a C 2 4 b C 4 5 c C 6 The names of ‘variable’ and ‘value’ columns can be customized: >>> df.melt(id_vars=['A'], value_vars=['B'], ... var_name='myVarname', value_name='myValname') A myVarname myValname 0 a B 1 1 b B 3 2 c B 5 Original index values can be kept around: >>> df.melt(id_vars=['A'], value_vars=['B', 'C'], ignore_index=False) A variable value 0 a B 1 1 b B 3 2 c B 5 0 a C 2 1 b C 4 2 c C 6 If you have multi-index columns: >>> df.columns = [list('ABC'), list('DEF')] >>> df A B C D E F 0 a 1 2 1 b 3 4 2 c 5 6 >>> df.melt(col_level=0, id_vars=['A'], value_vars=['B']) A variable value 0 a B 1 1 b B 3 2 c B 5 >>> df.melt(id_vars=[('A', 'D')], value_vars=[('B', 'E')]) (A, D) variable_0 variable_1 value 0 a B E 1 1 b B E 3 2 c B E 5
842
1,069
Confusion about modifying column in dataframe with pandas I'm working on a Bangaluru House Price Data csv from Kaggle. There is a column called 'total_sqft'. In this column, there are values that are a range of numbers (e.g.: 1000-1500), and I want to identify all those entries. I created this function to do so: def is_float(x): try: float(x) except: return False return True I applied it to the column: df3[~df3['total_sqft'].apply(is_float)] This works, but I don't understand why this doesn't: df3['total_sqft'] = ~df3['total_sqft'].apply(is_float) This just returns 'False' for everything instead of the actual entries
Answer from comment: In the first version you are selecting the rows that contain true values from the apply function. In the second you are setting the values to be the values of the apply function. Tilde means negation btw.
64,441,516
How to filter rows with specified conditions in a dataframe and put them in a new dataframe?
<p>The data of test.csv likes this:</p> <pre><code>staff_id,clock_time,device_id,latitude,longitude 1001,2020/9/14 04:43:00,d_1,24.59652556,118.0824644 1001,2020/9/14 05:34:40,d_1,24.59732974,118.0859631 1001,2020/9/14 06:33:34,d_1,24.73208312,118.0957197 1001,2020/9/14 08:17:29,d_1,24.59222786,118.0955275 1001,2020/9/20 05:30:56,d_1,24.59689407,118.2863806 1001,2020/9/20 07:26:05,d_1,24.58237852,118.2858955 1001,2020/9/20 08:26:05,d_1,24.58237852,118.2858955 1001,2020/9/20 09:26:05,d_1,24.58237852,118.2858955 1001,2020/9/20 17:26:05,d_1,24.58237852,118.2858955 1001,2020/9/20 19:26:05,d_1,24.70237852,118.2858955 1001,2020/9/20 22:26:05,d_1,24.71237852,118.2858955 </code></pre> <p>I want to find any row where the difference between longitude or latitude of 2 consecutive rows is greater than 0.1,then put the result into a new dataframe.</p> <p>In my example,the latitude difference of rows 2,3,4,9,10 are greater than 0.1, and the longitude difference of rows 4,5 are greater than 0.1.</p> <p>I want the new dataframe likes this:</p> <pre><code>staff_id,clock_time,device_id,latitude,longitude 1001,2020/9/14 05:34:40,d_1,24.59732974,118.0859631 1001,2020/9/14 06:33:34,d_1,24.73208312,118.0957197 1001,2020/9/14 08:17:29,d_1,24.59222786,118.0955275 1001,2020/9/20 05:30:56,d_1,24.59689407,118.2863806 1001,2020/9/20 17:26:05,d_1,24.58237852,118.2858955 1001,2020/9/20 19:26:05,d_1,24.70237852,118.2858955 </code></pre> <p>My code:</p> <pre><code>import pandas as pd df = pd.read_csv(r'E:/test.csv', encoding='utf-8', parse_dates=[1]) m1 = df[['latitude', 'longitude']].diff().abs().gt(0.1) m2 = df[['latitude', 'longitude']].shift().diff().abs().gt(0.1) new_dataframe = [...] </code></pre> <p>How do do it?</p>
64,441,599
"2020-10-20T08:40:43.243000"
1
null
1
34
python|pandas
<p>Use <a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.any.html" rel="nofollow noreferrer"><code>DataFrame.any</code></a> for convert DataFrame of boolean to <code>Series</code> and for shifted mask add <a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.shift.html" rel="nofollow noreferrer"><code>Series.shift</code></a>, chain with <code>|</code> for bitwise <code>OR</code> and last add <a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.copy.html" rel="nofollow noreferrer"><code>DataFrame.copy</code></a> for avoid warning if <code>new_dataframe</code> will be processed some way after this filtration:</p> <pre><code>m1 = df[['latitude', 'longitude']].diff().abs().gt(0.1).any(axis=1) new_dataframe = df[m1 | m1.shift(-1)].copy() print (new_dataframe) staff_id clock_time device_id latitude longitude 1 1001 2020/9/14 05:34:40 d_1 24.597330 118.085963 2 1001 2020/9/14 06:33:34 d_1 24.732083 118.095720 3 1001 2020/9/14 08:17:29 d_1 24.592228 118.095527 4 1001 2020/9/20 05:30:56 d_1 24.596894 118.286381 8 1001 2020/9/20 17:26:05 d_1 24.582379 118.285896 9 1001 2020/9/20 19:26:05 d_1 24.702379 118.285896 </code></pre>
"2020-10-20T08:46:01.910000"
0
https://pandas.pydata.org/docs/dev/getting_started/intro_tutorials/03_subset_data.html
How do I select a subset of a DataFrame?# In [1]: import pandas as pd Data used for this tutorial: Use DataFrame.any for convert DataFrame of boolean to Series and for shifted mask add Series.shift, chain with | for bitwise OR and last add DataFrame.copy for avoid warning if new_dataframe will be processed some way after this filtration: m1 = df[['latitude', 'longitude']].diff().abs().gt(0.1).any(axis=1) new_dataframe = df[m1 | m1.shift(-1)].copy() print (new_dataframe) staff_id clock_time device_id latitude longitude 1 1001 2020/9/14 05:34:40 d_1 24.597330 118.085963 2 1001 2020/9/14 06:33:34 d_1 24.732083 118.095720 3 1001 2020/9/14 08:17:29 d_1 24.592228 118.095527 4 1001 2020/9/20 05:30:56 d_1 24.596894 118.286381 8 1001 2020/9/20 17:26:05 d_1 24.582379 118.285896 9 1001 2020/9/20 19:26:05 d_1 24.702379 118.285896 Titanic data This tutorial uses the Titanic data set, stored as CSV. The data consists of the following data columns: PassengerId: Id of every passenger. Survived: Indication whether passenger survived. 0 for yes and 1 for no. Pclass: One out of the 3 ticket classes: Class 1, Class 2 and Class 3. Name: Name of passenger. Sex: Gender of passenger. Age: Age of passenger in years. SibSp: Number of siblings or spouses aboard. Parch: Number of parents or children aboard. Ticket: Ticket number of passenger. Fare: Indicating the fare. Cabin: Cabin number of passenger. Embarked: Port of embarkation. To raw data In [2]: titanic = pd.read_csv("data/titanic.csv") In [3]: titanic.head() Out[3]: PassengerId Survived Pclass ... Fare Cabin Embarked 0 1 0 3 ... 7.2500 NaN S 1 2 1 1 ... 71.2833 C85 C 2 3 1 3 ... 7.9250 NaN S 3 4 1 1 ... 53.1000 C123 S 4 5 0 3 ... 8.0500 NaN S [5 rows x 12 columns] How do I select a subset of a DataFrame?# How do I select specific columns from a DataFrame?# I’m interested in the age of the Titanic passengers. In [4]: ages = titanic["Age"] In [5]: ages.head() Out[5]: 0 22.0 1 38.0 2 26.0 3 35.0 4 35.0 Name: Age, dtype: float64 To select a single column, use square brackets [] with the column name of the column of interest. Each column in a DataFrame is a Series. As a single column is selected, the returned object is a pandas Series. We can verify this by checking the type of the output: In [6]: type(titanic["Age"]) Out[6]: pandas.core.series.Series And have a look at the shape of the output: In [7]: titanic["Age"].shape Out[7]: (891,) DataFrame.shape is an attribute (remember tutorial on reading and writing, do not use parentheses for attributes) of a pandas Series and DataFrame containing the number of rows and columns: (nrows, ncolumns). A pandas Series is 1-dimensional and only the number of rows is returned. I’m interested in the age and sex of the Titanic passengers. In [8]: age_sex = titanic[["Age", "Sex"]] In [9]: age_sex.head() Out[9]: Age Sex 0 22.0 male 1 38.0 female 2 26.0 female 3 35.0 female 4 35.0 male To select multiple columns, use a list of column names within the selection brackets []. Note The inner square brackets define a Python list with column names, whereas the outer brackets are used to select the data from a pandas DataFrame as seen in the previous example. The returned data type is a pandas DataFrame: In [10]: type(titanic[["Age", "Sex"]]) Out[10]: pandas.core.frame.DataFrame In [11]: titanic[["Age", "Sex"]].shape Out[11]: (891, 2) The selection returned a DataFrame with 891 rows and 2 columns. Remember, a DataFrame is 2-dimensional with both a row and column dimension. To user guideFor basic information on indexing, see the user guide section on indexing and selecting data. How do I filter specific rows from a DataFrame?# I’m interested in the passengers older than 35 years. In [12]: above_35 = titanic[titanic["Age"] > 35] In [13]: above_35.head() Out[13]: PassengerId Survived Pclass ... Fare Cabin Embarked 1 2 1 1 ... 71.2833 C85 C 6 7 0 1 ... 51.8625 E46 S 11 12 1 1 ... 26.5500 C103 S 13 14 0 3 ... 31.2750 NaN S 15 16 1 2 ... 16.0000 NaN S [5 rows x 12 columns] To select rows based on a conditional expression, use a condition inside the selection brackets []. The condition inside the selection brackets titanic["Age"] > 35 checks for which rows the Age column has a value larger than 35: In [14]: titanic["Age"] > 35 Out[14]: 0 False 1 True 2 False 3 False 4 False ... 886 False 887 False 888 False 889 False 890 False Name: Age, Length: 891, dtype: bool The output of the conditional expression (>, but also ==, !=, <, <=,… would work) is actually a pandas Series of boolean values (either True or False) with the same number of rows as the original DataFrame. Such a Series of boolean values can be used to filter the DataFrame by putting it in between the selection brackets []. Only rows for which the value is True will be selected. We know from before that the original Titanic DataFrame consists of 891 rows. Let’s have a look at the number of rows which satisfy the condition by checking the shape attribute of the resulting DataFrame above_35: In [15]: above_35.shape Out[15]: (217, 12) I’m interested in the Titanic passengers from cabin class 2 and 3. In [16]: class_23 = titanic[titanic["Pclass"].isin([2, 3])] In [17]: class_23.head() Out[17]: PassengerId Survived Pclass ... Fare Cabin Embarked 0 1 0 3 ... 7.2500 NaN S 2 3 1 3 ... 7.9250 NaN S 4 5 0 3 ... 8.0500 NaN S 5 6 0 3 ... 8.4583 NaN Q 7 8 0 3 ... 21.0750 NaN S [5 rows x 12 columns] Similar to the conditional expression, the isin() conditional function returns a True for each row the values are in the provided list. To filter the rows based on such a function, use the conditional function inside the selection brackets []. In this case, the condition inside the selection brackets titanic["Pclass"].isin([2, 3]) checks for which rows the Pclass column is either 2 or 3. The above is equivalent to filtering by rows for which the class is either 2 or 3 and combining the two statements with an | (or) operator: In [18]: class_23 = titanic[(titanic["Pclass"] == 2) | (titanic["Pclass"] == 3)] In [19]: class_23.head() Out[19]: PassengerId Survived Pclass ... Fare Cabin Embarked 0 1 0 3 ... 7.2500 NaN S 2 3 1 3 ... 7.9250 NaN S 4 5 0 3 ... 8.0500 NaN S 5 6 0 3 ... 8.4583 NaN Q 7 8 0 3 ... 21.0750 NaN S [5 rows x 12 columns] Note When combining multiple conditional statements, each condition must be surrounded by parentheses (). Moreover, you can not use or/and but need to use the or operator | and the and operator &. To user guideSee the dedicated section in the user guide about boolean indexing or about the isin function. I want to work with passenger data for which the age is known. In [20]: age_no_na = titanic[titanic["Age"].notna()] In [21]: age_no_na.head() Out[21]: PassengerId Survived Pclass ... Fare Cabin Embarked 0 1 0 3 ... 7.2500 NaN S 1 2 1 1 ... 71.2833 C85 C 2 3 1 3 ... 7.9250 NaN S 3 4 1 1 ... 53.1000 C123 S 4 5 0 3 ... 8.0500 NaN S [5 rows x 12 columns] The notna() conditional function returns a True for each row the values are not a Null value. As such, this can be combined with the selection brackets [] to filter the data table. You might wonder what actually changed, as the first 5 lines are still the same values. One way to verify is to check if the shape has changed: In [22]: age_no_na.shape Out[22]: (714, 12) To user guideFor more dedicated functions on missing values, see the user guide section about handling missing data. How do I select specific rows and columns from a DataFrame?# I’m interested in the names of the passengers older than 35 years. In [23]: adult_names = titanic.loc[titanic["Age"] > 35, "Name"] In [24]: adult_names.head() Out[24]: 1 Cumings, Mrs. John Bradley (Florence Briggs Th... 6 McCarthy, Mr. Timothy J 11 Bonnell, Miss. Elizabeth 13 Andersson, Mr. Anders Johan 15 Hewlett, Mrs. (Mary D Kingcome) Name: Name, dtype: object In this case, a subset of both rows and columns is made in one go and just using selection brackets [] is not sufficient anymore. The loc/iloc operators are required in front of the selection brackets []. When using loc/iloc, the part before the comma is the rows you want, and the part after the comma is the columns you want to select. When using the column names, row labels or a condition expression, use the loc operator in front of the selection brackets []. For both the part before and after the comma, you can use a single label, a list of labels, a slice of labels, a conditional expression or a colon. Using a colon specifies you want to select all rows or columns. I’m interested in rows 10 till 25 and columns 3 to 5. In [25]: titanic.iloc[9:25, 2:5] Out[25]: Pclass Name Sex 9 2 Nasser, Mrs. Nicholas (Adele Achem) female 10 3 Sandstrom, Miss. Marguerite Rut female 11 1 Bonnell, Miss. Elizabeth female 12 3 Saundercock, Mr. William Henry male 13 3 Andersson, Mr. Anders Johan male .. ... ... ... 20 2 Fynney, Mr. Joseph J male 21 2 Beesley, Mr. Lawrence male 22 3 McGowan, Miss. Anna "Annie" female 23 1 Sloper, Mr. William Thompson male 24 3 Palsson, Miss. Torborg Danira female [16 rows x 3 columns] Again, a subset of both rows and columns is made in one go and just using selection brackets [] is not sufficient anymore. When specifically interested in certain rows and/or columns based on their position in the table, use the iloc operator in front of the selection brackets []. When selecting specific rows and/or columns with loc or iloc, new values can be assigned to the selected data. For example, to assign the name anonymous to the first 3 elements of the third column: In [26]: titanic.iloc[0:3, 3] = "anonymous" In [27]: titanic.head() Out[27]: PassengerId Survived Pclass ... Fare Cabin Embarked 0 1 0 3 ... 7.2500 NaN S 1 2 1 1 ... 71.2833 C85 C 2 3 1 3 ... 7.9250 NaN S 3 4 1 1 ... 53.1000 C123 S 4 5 0 3 ... 8.0500 NaN S [5 rows x 12 columns] To user guideSee the user guide section on different choices for indexing to get more insight in the usage of loc and iloc. REMEMBER When selecting subsets of data, square brackets [] are used. Inside these brackets, you can use a single column/row label, a list of column/row labels, a slice of labels, a conditional expression or a colon. Select specific rows and/or columns using loc when using the row and column names. Select specific rows and/or columns using iloc when using the positions in the table. You can assign new values to a selection based on loc/iloc. To user guideA full overview of indexing is provided in the user guide pages on indexing and selecting data.
105
937
How to filter rows with specified conditions in a dataframe and put them in a new dataframe? The data of test.csv likes this: staff_id,clock_time,device_id,latitude,longitude 1001,2020/9/14 04:43:00,d_1,24.59652556,118.0824644 1001,2020/9/14 05:34:40,d_1,24.59732974,118.0859631 1001,2020/9/14 06:33:34,d_1,24.73208312,118.0957197 1001,2020/9/14 08:17:29,d_1,24.59222786,118.0955275 1001,2020/9/20 05:30:56,d_1,24.59689407,118.2863806 1001,2020/9/20 07:26:05,d_1,24.58237852,118.2858955 1001,2020/9/20 08:26:05,d_1,24.58237852,118.2858955 1001,2020/9/20 09:26:05,d_1,24.58237852,118.2858955 1001,2020/9/20 17:26:05,d_1,24.58237852,118.2858955 1001,2020/9/20 19:26:05,d_1,24.70237852,118.2858955 1001,2020/9/20 22:26:05,d_1,24.71237852,118.2858955 I want to find any row where the difference between longitude or latitude of 2 consecutive rows is greater than 0.1,then put the result into a new dataframe. In my example,the latitude difference of rows 2,3,4,9,10 are greater than 0.1, and the longitude difference of rows 4,5 are greater than 0.1. I want the new dataframe likes this: staff_id,clock_time,device_id,latitude,longitude 1001,2020/9/14 05:34:40,d_1,24.59732974,118.0859631 1001,2020/9/14 06:33:34,d_1,24.73208312,118.0957197 1001,2020/9/14 08:17:29,d_1,24.59222786,118.0955275 1001,2020/9/20 05:30:56,d_1,24.59689407,118.2863806 1001,2020/9/20 17:26:05,d_1,24.58237852,118.2858955 1001,2020/9/20 19:26:05,d_1,24.70237852,118.2858955 My code: import pandas as pd df = pd.read_csv(r'E:/test.csv', encoding='utf-8', parse_dates=[1]) m1 = df[['latitude', 'longitude']].diff().abs().gt(0.1) m2 = df[['latitude', 'longitude']].shift().diff().abs().gt(0.1) new_dataframe = [...] How do do it?
Use DataFrame.any for convert DataFrame of boolean to Series and for shifted mask add Series.shift, chain with | for bitwise OR and last add DataFrame.copy for avoid warning if new_dataframe will be processed some way after this filtration: m1 = df[['latitude', 'longitude']].diff().abs().gt(0.1).any(axis=1) new_dataframe = df[m1 | m1.shift(-1)].copy() print (new_dataframe) staff_id clock_time device_id latitude longitude 1 1001 2020/9/14 05:34:40 d_1 24.597330 118.085963 2 1001 2020/9/14 06:33:34 d_1 24.732083 118.095720 3 1001 2020/9/14 08:17:29 d_1 24.592228 118.095527 4 1001 2020/9/20 05:30:56 d_1 24.596894 118.286381 8 1001 2020/9/20 17:26:05 d_1 24.582379 118.285896 9 1001 2020/9/20 19:26:05 d_1 24.702379 118.285896
68,842,912
How to detect which astype header in pandas is failing conversion?
<p>For some reason, my big dataframe is failing astype conversion and I get an error message as follows: <code>could not convert string to float: 'False'.</code></p> <p>Now since I have 100 columns, I want to detect in which column the astype conversion is failing, so looking at traceback:</p> <pre><code>result = result.astype(pdSchema) File &quot;C:\Users\spidey\AppData\Roaming\Python\Python39\site-packages\pandas\core\generic.py&quot;, line 5859, in astype col.astype(dtype=dtype[col_name], copy=copy, errors=errors) File &quot;C:\Users\spidey\AppData\Roaming\Python\Python39\site-packages\pandas\core\generic.py&quot;, line 5874, in astype new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors) File &quot;C:\Users\spidey\AppData\Roaming\Python\Python39\site-packages\pandas\core\internals\managers.py&quot;, line 631, in astype return self.apply(&quot;astype&quot;, dtype=dtype, copy=copy, errors=errors) File &quot;C:\Users\spidey\AppData\Roaming\Python\Python39\site-packages\pandas\core\internals\managers.py&quot;, line 427, in apply applied = getattr(b, f)(**kwargs) File &quot;C:\Users\spidey\AppData\Roaming\Python\Python39\site-packages\pandas\core\internals\blocks.py&quot;, line 673, in astype values = astype_nansafe(vals1d, dtype, copy=True) File &quot;C:\Users\spidey\AppData\Roaming\Python\Python39\site-packages\pandas\core\dtypes\cast.py&quot;, line 1097, in astype_nansafe return arr.astype(dtype, copy=True) ValueError: could not convert string to float: 'False' </code></pre> <p>I can determine the value is 'False' but I can't determnie in which column it's failing and since my multiple columns have similar value, In order to handle this exception, I want to know the column name where it's failing.</p>
68,843,223
"2021-08-19T06:19:05.153000"
1
null
1
45
pandas
<p>I think <code>pdSchema</code> is dict, so you can test it by:</p> <pre><code>for k, v in pdSchema.items(): try: result[k].astype(v) except ValueError: print (f'Column {k} failed for converting to {v}') </code></pre>
"2021-08-19T06:51:58.457000"
0
https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html
pandas.read_csv# pandas.read_csv# pandas.read_csv(filepath_or_buffer, *, sep=_NoDefault.no_default, delimiter=None, header='infer', names=_NoDefault.no_default, index_col=None, usecols=None, squeeze=None, prefix=_NoDefault.no_default, mangle_dupe_cols=True, dtype=None, engine=None, converters=None, true_values=None, false_values=None, skipinitialspace=False, skiprows=None, skipfooter=0, nrows=None, na_values=None, keep_default_na=True, na_filter=True, verbose=False, skip_blank_lines=True, parse_dates=None, infer_datetime_format=False, keep_date_col=False, date_parser=None, dayfirst=False, cache_dates=True, iterator=False, chunksize=None, compression='infer', thousands=None, decimal='.', lineterminator=None, quotechar='"', quoting=0, doublequote=True, escapechar=None, comment=None, encoding=None, encoding_errors='strict', dialect=None, error_bad_lines=None, warn_bad_lines=None, on_bad_lines=None, delim_whitespace=False, low_memory=True, memory_map=False, float_precision=None, storage_options=None)[source]# I think pdSchema is dict, so you can test it by: for k, v in pdSchema.items(): try: result[k].astype(v) except ValueError: print (f'Column {k} failed for converting to {v}') Read a comma-separated values (csv) file into DataFrame. Also supports optionally iterating or breaking of the file into chunks. Additional help can be found in the online docs for IO Tools. Parameters filepath_or_bufferstr, path object or file-like objectAny valid string path is acceptable. The string could be a URL. Valid URL schemes include http, ftp, s3, gs, and file. For file URLs, a host is expected. A local file could be: file://localhost/path/to/table.csv. If you want to pass in a path object, pandas accepts any os.PathLike. By file-like object, we refer to objects with a read() method, such as a file handle (e.g. via builtin open function) or StringIO. sepstr, default ‘,’Delimiter to use. If sep is None, the C engine cannot automatically detect the separator, but the Python parsing engine can, meaning the latter will be used and automatically detect the separator by Python’s builtin sniffer tool, csv.Sniffer. In addition, separators longer than 1 character and different from '\s+' will be interpreted as regular expressions and will also force the use of the Python parsing engine. Note that regex delimiters are prone to ignoring quoted data. Regex example: '\r\t'. delimiterstr, default NoneAlias for sep. headerint, list of int, None, default ‘infer’Row number(s) to use as the column names, and the start of the data. Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first line of the file, if column names are passed explicitly then the behavior is identical to header=None. Explicitly pass header=0 to be able to replace existing names. The header can be a list of integers that specify row locations for a multi-index on the columns e.g. [0,1,3]. Intervening rows that are not specified will be skipped (e.g. 2 in this example is skipped). Note that this parameter ignores commented lines and empty lines if skip_blank_lines=True, so header=0 denotes the first line of data rather than the first line of the file. namesarray-like, optionalList of column names to use. If the file contains a header row, then you should explicitly pass header=0 to override the column names. Duplicates in this list are not allowed. index_colint, str, sequence of int / str, or False, optional, default NoneColumn(s) to use as the row labels of the DataFrame, either given as string name or column index. If a sequence of int / str is given, a MultiIndex is used. Note: index_col=False can be used to force pandas to not use the first column as the index, e.g. when you have a malformed file with delimiters at the end of each line. usecolslist-like or callable, optionalReturn a subset of the columns. If list-like, all elements must either be positional (i.e. integer indices into the document columns) or strings that correspond to column names provided either by the user in names or inferred from the document header row(s). If names are given, the document header row(s) are not taken into account. For example, a valid list-like usecols parameter would be [0, 1, 2] or ['foo', 'bar', 'baz']. Element order is ignored, so usecols=[0, 1] is the same as [1, 0]. To instantiate a DataFrame from data with element order preserved use pd.read_csv(data, usecols=['foo', 'bar'])[['foo', 'bar']] for columns in ['foo', 'bar'] order or pd.read_csv(data, usecols=['foo', 'bar'])[['bar', 'foo']] for ['bar', 'foo'] order. If callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True. An example of a valid callable argument would be lambda x: x.upper() in ['AAA', 'BBB', 'DDD']. Using this parameter results in much faster parsing time and lower memory usage. squeezebool, default FalseIf the parsed data only contains one column then return a Series. Deprecated since version 1.4.0: Append .squeeze("columns") to the call to read_csv to squeeze the data. prefixstr, optionalPrefix to add to column numbers when no header, e.g. ‘X’ for X0, X1, … Deprecated since version 1.4.0: Use a list comprehension on the DataFrame’s columns after calling read_csv. mangle_dupe_colsbool, default TrueDuplicate columns will be specified as ‘X’, ‘X.1’, …’X.N’, rather than ‘X’…’X’. Passing in False will cause data to be overwritten if there are duplicate names in the columns. Deprecated since version 1.5.0: Not implemented, and a new argument to specify the pattern for the names of duplicated columns will be added instead dtypeType name or dict of column -> type, optionalData type for data or columns. E.g. {‘a’: np.float64, ‘b’: np.int32, ‘c’: ‘Int64’} Use str or object together with suitable na_values settings to preserve and not interpret dtype. If converters are specified, they will be applied INSTEAD of dtype conversion. New in version 1.5.0: Support for defaultdict was added. Specify a defaultdict as input where the default determines the dtype of the columns which are not explicitly listed. engine{‘c’, ‘python’, ‘pyarrow’}, optionalParser engine to use. The C and pyarrow engines are faster, while the python engine is currently more feature-complete. Multithreading is currently only supported by the pyarrow engine. New in version 1.4.0: The “pyarrow” engine was added as an experimental engine, and some features are unsupported, or may not work correctly, with this engine. convertersdict, optionalDict of functions for converting values in certain columns. Keys can either be integers or column labels. true_valueslist, optionalValues to consider as True. false_valueslist, optionalValues to consider as False. skipinitialspacebool, default FalseSkip spaces after delimiter. skiprowslist-like, int or callable, optionalLine numbers to skip (0-indexed) or number of lines to skip (int) at the start of the file. If callable, the callable function will be evaluated against the row indices, returning True if the row should be skipped and False otherwise. An example of a valid callable argument would be lambda x: x in [0, 2]. skipfooterint, default 0Number of lines at bottom of file to skip (Unsupported with engine=’c’). nrowsint, optionalNumber of rows of file to read. Useful for reading pieces of large files. na_valuesscalar, str, list-like, or dict, optionalAdditional strings to recognize as NA/NaN. If dict passed, specific per-column NA values. By default the following values are interpreted as NaN: ‘’, ‘#N/A’, ‘#N/A N/A’, ‘#NA’, ‘-1.#IND’, ‘-1.#QNAN’, ‘-NaN’, ‘-nan’, ‘1.#IND’, ‘1.#QNAN’, ‘<NA>’, ‘N/A’, ‘NA’, ‘NULL’, ‘NaN’, ‘n/a’, ‘nan’, ‘null’. keep_default_nabool, default TrueWhether or not to include the default NaN values when parsing the data. Depending on whether na_values is passed in, the behavior is as follows: If keep_default_na is True, and na_values are specified, na_values is appended to the default NaN values used for parsing. If keep_default_na is True, and na_values are not specified, only the default NaN values are used for parsing. If keep_default_na is False, and na_values are specified, only the NaN values specified na_values are used for parsing. If keep_default_na is False, and na_values are not specified, no strings will be parsed as NaN. Note that if na_filter is passed in as False, the keep_default_na and na_values parameters will be ignored. na_filterbool, default TrueDetect missing value markers (empty strings and the value of na_values). In data without any NAs, passing na_filter=False can improve the performance of reading a large file. verbosebool, default FalseIndicate number of NA values placed in non-numeric columns. skip_blank_linesbool, default TrueIf True, skip over blank lines rather than interpreting as NaN values. parse_datesbool or list of int or names or list of lists or dict, default FalseThe behavior is as follows: boolean. If True -> try parsing the index. list of int or names. e.g. If [1, 2, 3] -> try parsing columns 1, 2, 3 each as a separate date column. list of lists. e.g. If [[1, 3]] -> combine columns 1 and 3 and parse as a single date column. dict, e.g. {‘foo’ : [1, 3]} -> parse columns 1, 3 as date and call result ‘foo’ If a column or index cannot be represented as an array of datetimes, say because of an unparsable value or a mixture of timezones, the column or index will be returned unaltered as an object data type. For non-standard datetime parsing, use pd.to_datetime after pd.read_csv. To parse an index or column with a mixture of timezones, specify date_parser to be a partially-applied pandas.to_datetime() with utc=True. See Parsing a CSV with mixed timezones for more. Note: A fast-path exists for iso8601-formatted dates. infer_datetime_formatbool, default FalseIf True and parse_dates is enabled, pandas will attempt to infer the format of the datetime strings in the columns, and if it can be inferred, switch to a faster method of parsing them. In some cases this can increase the parsing speed by 5-10x. keep_date_colbool, default FalseIf True and parse_dates specifies combining multiple columns then keep the original columns. date_parserfunction, optionalFunction to use for converting a sequence of string columns to an array of datetime instances. The default uses dateutil.parser.parser to do the conversion. Pandas will try to call date_parser in three different ways, advancing to the next if an exception occurs: 1) Pass one or more arrays (as defined by parse_dates) as arguments; 2) concatenate (row-wise) the string values from the columns defined by parse_dates into a single array and pass that; and 3) call date_parser once for each row using one or more strings (corresponding to the columns defined by parse_dates) as arguments. dayfirstbool, default FalseDD/MM format dates, international and European format. cache_datesbool, default TrueIf True, use a cache of unique, converted dates to apply the datetime conversion. May produce significant speed-up when parsing duplicate date strings, especially ones with timezone offsets. New in version 0.25.0. iteratorbool, default FalseReturn TextFileReader object for iteration or getting chunks with get_chunk(). Changed in version 1.2: TextFileReader is a context manager. chunksizeint, optionalReturn TextFileReader object for iteration. See the IO Tools docs for more information on iterator and chunksize. Changed in version 1.2: TextFileReader is a context manager. compressionstr or dict, default ‘infer’For on-the-fly decompression of on-disk data. If ‘infer’ and ‘filepath_or_buffer’ is path-like, then detect compression from the following extensions: ‘.gz’, ‘.bz2’, ‘.zip’, ‘.xz’, ‘.zst’, ‘.tar’, ‘.tar.gz’, ‘.tar.xz’ or ‘.tar.bz2’ (otherwise no compression). If using ‘zip’ or ‘tar’, the ZIP file must contain only one data file to be read in. Set to None for no decompression. Can also be a dict with key 'method' set to one of {'zip', 'gzip', 'bz2', 'zstd', 'tar'} and other key-value pairs are forwarded to zipfile.ZipFile, gzip.GzipFile, bz2.BZ2File, zstandard.ZstdDecompressor or tarfile.TarFile, respectively. As an example, the following could be passed for Zstandard decompression using a custom compression dictionary: compression={'method': 'zstd', 'dict_data': my_compression_dict}. New in version 1.5.0: Added support for .tar files. Changed in version 1.4.0: Zstandard support. thousandsstr, optionalThousands separator. decimalstr, default ‘.’Character to recognize as decimal point (e.g. use ‘,’ for European data). lineterminatorstr (length 1), optionalCharacter to break file into lines. Only valid with C parser. quotecharstr (length 1), optionalThe character used to denote the start and end of a quoted item. Quoted items can include the delimiter and it will be ignored. quotingint or csv.QUOTE_* instance, default 0Control field quoting behavior per csv.QUOTE_* constants. Use one of QUOTE_MINIMAL (0), QUOTE_ALL (1), QUOTE_NONNUMERIC (2) or QUOTE_NONE (3). doublequotebool, default TrueWhen quotechar is specified and quoting is not QUOTE_NONE, indicate whether or not to interpret two consecutive quotechar elements INSIDE a field as a single quotechar element. escapecharstr (length 1), optionalOne-character string used to escape other characters. commentstr, optionalIndicates remainder of line should not be parsed. If found at the beginning of a line, the line will be ignored altogether. This parameter must be a single character. Like empty lines (as long as skip_blank_lines=True), fully commented lines are ignored by the parameter header but not by skiprows. For example, if comment='#', parsing #empty\na,b,c\n1,2,3 with header=0 will result in ‘a,b,c’ being treated as the header. encodingstr, optionalEncoding to use for UTF when reading/writing (ex. ‘utf-8’). List of Python standard encodings . Changed in version 1.2: When encoding is None, errors="replace" is passed to open(). Otherwise, errors="strict" is passed to open(). This behavior was previously only the case for engine="python". Changed in version 1.3.0: encoding_errors is a new argument. encoding has no longer an influence on how encoding errors are handled. encoding_errorsstr, optional, default “strict”How encoding errors are treated. List of possible values . New in version 1.3.0. dialectstr or csv.Dialect, optionalIf provided, this parameter will override values (default or not) for the following parameters: delimiter, doublequote, escapechar, skipinitialspace, quotechar, and quoting. If it is necessary to override values, a ParserWarning will be issued. See csv.Dialect documentation for more details. error_bad_linesbool, optional, default NoneLines with too many fields (e.g. a csv line with too many commas) will by default cause an exception to be raised, and no DataFrame will be returned. If False, then these “bad lines” will be dropped from the DataFrame that is returned. Deprecated since version 1.3.0: The on_bad_lines parameter should be used instead to specify behavior upon encountering a bad line instead. warn_bad_linesbool, optional, default NoneIf error_bad_lines is False, and warn_bad_lines is True, a warning for each “bad line” will be output. Deprecated since version 1.3.0: The on_bad_lines parameter should be used instead to specify behavior upon encountering a bad line instead. on_bad_lines{‘error’, ‘warn’, ‘skip’} or callable, default ‘error’Specifies what to do upon encountering a bad line (a line with too many fields). Allowed values are : ‘error’, raise an Exception when a bad line is encountered. ‘warn’, raise a warning when a bad line is encountered and skip that line. ‘skip’, skip bad lines without raising or warning when they are encountered. New in version 1.3.0. New in version 1.4.0: callable, function with signature (bad_line: list[str]) -> list[str] | None that will process a single bad line. bad_line is a list of strings split by the sep. If the function returns None, the bad line will be ignored. If the function returns a new list of strings with more elements than expected, a ParserWarning will be emitted while dropping extra elements. Only supported when engine="python" delim_whitespacebool, default FalseSpecifies whether or not whitespace (e.g. ' ' or '    ') will be used as the sep. Equivalent to setting sep='\s+'. If this option is set to True, nothing should be passed in for the delimiter parameter. low_memorybool, default TrueInternally process the file in chunks, resulting in lower memory use while parsing, but possibly mixed type inference. To ensure no mixed types either set False, or specify the type with the dtype parameter. Note that the entire file is read into a single DataFrame regardless, use the chunksize or iterator parameter to return the data in chunks. (Only valid with C parser). memory_mapbool, default FalseIf a filepath is provided for filepath_or_buffer, map the file object directly onto memory and access the data directly from there. Using this option can improve performance because there is no longer any I/O overhead. float_precisionstr, optionalSpecifies which converter the C engine should use for floating-point values. The options are None or ‘high’ for the ordinary converter, ‘legacy’ for the original lower precision pandas converter, and ‘round_trip’ for the round-trip converter. Changed in version 1.2. storage_optionsdict, optionalExtra options that make sense for a particular storage connection, e.g. host, port, username, password, etc. For HTTP(S) URLs the key-value pairs are forwarded to urllib.request.Request as header options. For other URLs (e.g. starting with “s3://”, and “gcs://”) the key-value pairs are forwarded to fsspec.open. Please see fsspec and urllib for more details, and for more examples on storage options refer here. New in version 1.2. Returns DataFrame or TextParserA comma-separated values (csv) file is returned as two-dimensional data structure with labeled axes. See also DataFrame.to_csvWrite DataFrame to a comma-separated values (csv) file. read_csvRead a comma-separated values (csv) file into DataFrame. read_fwfRead a table of fixed-width formatted lines into DataFrame. Examples >>> pd.read_csv('data.csv')
1,025
1,227
How to detect which astype header in pandas is failing conversion? For some reason, my big dataframe is failing astype conversion and I get an error message as follows: could not convert string to float: 'False'. Now since I have 100 columns, I want to detect in which column the astype conversion is failing, so looking at traceback: result = result.astype(pdSchema) File "C:\Users\spidey\AppData\Roaming\Python\Python39\site-packages\pandas\core\generic.py", line 5859, in astype col.astype(dtype=dtype[col_name], copy=copy, errors=errors) File "C:\Users\spidey\AppData\Roaming\Python\Python39\site-packages\pandas\core\generic.py", line 5874, in astype new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors) File "C:\Users\spidey\AppData\Roaming\Python\Python39\site-packages\pandas\core\internals\managers.py", line 631, in astype return self.apply("astype", dtype=dtype, copy=copy, errors=errors) File "C:\Users\spidey\AppData\Roaming\Python\Python39\site-packages\pandas\core\internals\managers.py", line 427, in apply applied = getattr(b, f)(**kwargs) File "C:\Users\spidey\AppData\Roaming\Python\Python39\site-packages\pandas\core\internals\blocks.py", line 673, in astype values = astype_nansafe(vals1d, dtype, copy=True) File "C:\Users\spidey\AppData\Roaming\Python\Python39\site-packages\pandas\core\dtypes\cast.py", line 1097, in astype_nansafe return arr.astype(dtype, copy=True) ValueError: could not convert string to float: 'False' I can determine the value is 'False' but I can't determnie in which column it's failing and since my multiple columns have similar value, In order to handle this exception, I want to know the column name where it's failing.
I think pdSchema is dict, so you can test it by: for k, v in pdSchema.items(): try: result[k].astype(v) except ValueError: print (f'Column {k} failed for converting to {v}')
70,531,839
Value error when calculating standard deviation on dataframe
<p>I need the standard deviation of column <code>distance</code> for each row in my dataframe.</p> <p>Dataframe <code>df_stats</code>:</p> <pre><code> a1/a2 distance mean_distance date_time 2021-10-29T21:00:00+00:00 105.007574 -2.530492 0.234318 2021-10-29T22:00:00+00:00 104.459527 -5.232787 0.012537 2021-10-29T23:00:00+00:00 104.648467 1.807101 0.093432 2021-10-30T00:00:00+00:00 104.758201 1.048046 0.164502 2021-10-30T01:00:00+00:00 104.820132 0.591004 0.246095 2021-10-30T02:00:00+00:00 104.474062 -3.307024 -0.194917 2021-10-30T03:00:00+00:00 104.284146 -1.819483 -0.231843 2021-10-30T04:00:00+00:00 104.072383 -2.032697 -0.249918 2021-10-30T05:00:00+00:00 103.690546 -3.675699 -0.484996 2021-10-30T06:00:00+00:00 103.755979 0.630837 -0.823674 2021-10-30T07:00:00+00:00 102.721667 -10.018720 -1.181811 2021-10-30T08:00:00+00:00 102.998153 2.687995 -1.015365 2021-10-30T09:00:00+00:00 103.236159 2.308109 -0.876012 2021-10-30T10:00:00+00:00 103.471932 2.281216 -0.930593 2021-10-30T11:00:00+00:00 103.376928 -0.918579 -1.142659 2021-10-30T12:00:00+00:00 103.587805 2.037809 -1.110613 2021-10-30T13:00:00+00:00 104.359756 7.424508 -0.468987 2021-10-30T14:00:00+00:00 104.612493 2.418853 -0.383811 2021-10-30T15:00:00+00:00 104.607392 -0.048755 -0.562828 2021-10-30T16:00:00+00:00 104.846049 2.278849 -0.203495 2021-10-30T17:00:00+00:00 104.997437 1.442872 -0.004827 </code></pre> <p>Trying to do it this way:</p> <pre><code>df_stats['std'] = df_stats.distance.std(axis=1) </code></pre> <p>But I get this error:</p> <pre><code>No axis named 1 for object type &lt;class 'pandas.core.series.Series'&gt; </code></pre> <p>Why is it not working?</p>
70,531,874
"2021-12-30T12:41:21.370000"
1
null
-2
49
python|pandas
<blockquote> <p>Why is it not working?</p> </blockquote> <p>Because <code>axis=1</code> is for <code>std</code> per columns, but you count <code>Series</code>, <code>df_stats.distance</code>, there is no columns so error raised.</p> <p>If use <code>std</code> of column, output is scalar:</p> <pre><code>print (df_stats.distance.std()) df_stats['std'] = df_stats.distance.std() </code></pre> <p>If need processing per multiple columns then <code>axis=1</code> count <code>std</code> per rows:</p> <pre><code>df_stats['std'] = df_stats[['distance','a1/a2','mean_distance']].std(axis=1) </code></pre> <p>If need <code>std</code> per some datetimes, e.g. days:</p> <pre><code>df_stats['std'] = df_stats.groupby(pd.Grouper(freq='d')).distance.transform('std') </code></pre>
"2021-12-30T12:43:38.113000"
0
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.std.html
pandas.DataFrame.std# pandas.DataFrame.std# DataFrame.std(axis=None, skipna=True, level=None, ddof=1, numeric_only=None, **kwargs)[source]# Return sample standard deviation over requested axis. Normalized by N-1 by default. This can be changed using the ddof argument. Parameters axis{index (0), columns (1)}For Series this parameter is unused and defaults to 0. skipnabool, default TrueExclude NA/null values. If an entire row/column is NA, the result will be NA. levelint or level name, default NoneIf the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series. Deprecated since version 1.3.0: The level keyword is deprecated. Use groupby instead. ddofint, default 1Delta Degrees of Freedom. The divisor used in calculations is N - ddof, Why is it not working? Because axis=1 is for std per columns, but you count Series, df_stats.distance, there is no columns so error raised. If use std of column, output is scalar: print (df_stats.distance.std()) df_stats['std'] = df_stats.distance.std() If need processing per multiple columns then axis=1 count std per rows: df_stats['std'] = df_stats[['distance','a1/a2','mean_distance']].std(axis=1) If need std per some datetimes, e.g. days: df_stats['std'] = df_stats.groupby(pd.Grouper(freq='d')).distance.transform('std') where N represents the number of elements. numeric_onlybool, default NoneInclude only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series. Deprecated since version 1.5.0: Specifying numeric_only=None is deprecated. The default value will be False in a future version of pandas. Returns Series or DataFrame (if level specified) Notes To have the same behaviour as numpy.std, use ddof=0 (instead of the default ddof=1) Examples >>> df = pd.DataFrame({'person_id': [0, 1, 2, 3], ... 'age': [21, 25, 62, 43], ... 'height': [1.61, 1.87, 1.49, 2.01]} ... ).set_index('person_id') >>> df age height person_id 0 21 1.61 1 25 1.87 2 62 1.49 3 43 2.01 The standard deviation of the columns can be found as follows: >>> df.std() age 18.786076 height 0.237417 Alternatively, ddof=0 can be set to normalize by N instead of N-1: >>> df.std(ddof=0) age 16.269219 height 0.205609
790
1,325
Value error when calculating standard deviation on dataframe I need the standard deviation of column distance for each row in my dataframe. Dataframe df_stats: a1/a2 distance mean_distance date_time 2021-10-29T21:00:00+00:00 105.007574 -2.530492 0.234318 2021-10-29T22:00:00+00:00 104.459527 -5.232787 0.012537 2021-10-29T23:00:00+00:00 104.648467 1.807101 0.093432 2021-10-30T00:00:00+00:00 104.758201 1.048046 0.164502 2021-10-30T01:00:00+00:00 104.820132 0.591004 0.246095 2021-10-30T02:00:00+00:00 104.474062 -3.307024 -0.194917 2021-10-30T03:00:00+00:00 104.284146 -1.819483 -0.231843 2021-10-30T04:00:00+00:00 104.072383 -2.032697 -0.249918 2021-10-30T05:00:00+00:00 103.690546 -3.675699 -0.484996 2021-10-30T06:00:00+00:00 103.755979 0.630837 -0.823674 2021-10-30T07:00:00+00:00 102.721667 -10.018720 -1.181811 2021-10-30T08:00:00+00:00 102.998153 2.687995 -1.015365 2021-10-30T09:00:00+00:00 103.236159 2.308109 -0.876012 2021-10-30T10:00:00+00:00 103.471932 2.281216 -0.930593 2021-10-30T11:00:00+00:00 103.376928 -0.918579 -1.142659 2021-10-30T12:00:00+00:00 103.587805 2.037809 -1.110613 2021-10-30T13:00:00+00:00 104.359756 7.424508 -0.468987 2021-10-30T14:00:00+00:00 104.612493 2.418853 -0.383811 2021-10-30T15:00:00+00:00 104.607392 -0.048755 -0.562828 2021-10-30T16:00:00+00:00 104.846049 2.278849 -0.203495 2021-10-30T17:00:00+00:00 104.997437 1.442872 -0.004827 Trying to do it this way: df_stats['std'] = df_stats.distance.std(axis=1) But I get this error: No axis named 1 for object type <class 'pandas.core.series.Series'> Why is it not working?
Why is it not working? Because axis=1 is for std per columns, but you count Series, df_stats.distance, there is no columns so error raised. If use std of column, output is scalar: print (df_stats.distance.std()) df_stats['std'] = df_stats.distance.std() If need processing per multiple columns then axis=1 count std per rows: df_stats['std'] = df_stats[['distance','a1/a2','mean_distance']].std(axis=1) If need std per some datetimes, e.g. days: df_stats['std'] = df_stats.groupby(pd.Grouper(freq='d')).distance.transform('std')
66,921,728
Getting specific values from a range of time in Pandas
<p><a href="https://i.stack.imgur.com/TngnA.png" rel="nofollow noreferrer">DataFrame</a></p> <p>Hi. My problem is: Given a range of 1 minute in that DataFrame, ask and bid will change, sometimes no but sometimes more often. I need to get the first value, max, min and last and create a new dataframe of 1 minute interval. If there is a change in bid and ask, I will get the mean. Let me show what did i do:</p> <pre><code>df['Date'] = pd.to_datetime(df['Date'], format='%Y.%m.%d', errors='coerce') df['Time'] = pd.to_datetime(df['Time'], format='%H:%M:%S.%f', errors='coerce') days = df['Date'].unique() hour = np.arange(0,24) minute = np.arange(0,60) open_list = [] for d in days: for h in hour: for m in minute: open = 0 l = len(df[(df['Date'] == d) &amp; (df['Time'].dt.hour == h) &amp; (df['Time'].dt.minute == m)][['Bid','Ask']]) if l != 0: open = df[(df['Date'] == d) &amp; (df['Time'].dt.hour == h) &amp; (df['Time'].dt.minute == m)][['Bid','Ask']].iloc[0].values else: continue if len(open) == 2: open_list.append((open.sum() / 2)) else: open_list.append(open) </code></pre> <p>As you can see, it would take a life time for me. I would appreciate some help.</p>
66,921,867
"2021-04-02T15:37:31.530000"
1
null
0
59
python|pandas
<p>Since I haven't your data I can try to explain the method I'd use:</p> <ul> <li>Create a column with <code>df[&quot;dt&quot;] = pd.to_datetime(df['Date'] + ' ' + df['Time'])</code></li> <li>Then I'd use <a href="https://pandas.pydata.org/docs/reference/api/pandas.Series.resample.html" rel="nofollow noreferrer"><code>resample</code></a><code>('1Min').agg(['mean','min', 'max', 'last'])</code></li> </ul>
"2021-04-02T15:49:46.700000"
0
https://pandas.pydata.org/docs/user_guide/timeseries.html
Time series / date functionality# Time series / date functionality# pandas contains extensive capabilities and features for working with time series data for all domains. Using the NumPy datetime64 and timedelta64 dtypes, pandas has consolidated a large number of features from other Python libraries like scikits.timeseries as well as created a tremendous amount of new functionality for manipulating time series data. For example, pandas supports: Parsing time series information from various sources and formats In [1]: import datetime In [2]: dti = pd.to_datetime( ...: ["1/1/2018", np.datetime64("2018-01-01"), datetime.datetime(2018, 1, 1)] ...: ) ...: In [3]: dti Out[3]: DatetimeIndex(['2018-01-01', '2018-01-01', '2018-01-01'], dtype='datetime64[ns]', freq=None) Generate sequences of fixed-frequency dates and time spans In [4]: dti = pd.date_range("2018-01-01", periods=3, freq="H") In [5]: dti Out[5]: DatetimeIndex(['2018-01-01 00:00:00', '2018-01-01 01:00:00', '2018-01-01 02:00:00'], dtype='datetime64[ns]', freq='H') Since I haven't your data I can try to explain the method I'd use: Create a column with df["dt"] = pd.to_datetime(df['Date'] + ' ' + df['Time']) Then I'd use resample('1Min').agg(['mean','min', 'max', 'last']) Manipulating and converting date times with timezone information In [6]: dti = dti.tz_localize("UTC") In [7]: dti Out[7]: DatetimeIndex(['2018-01-01 00:00:00+00:00', '2018-01-01 01:00:00+00:00', '2018-01-01 02:00:00+00:00'], dtype='datetime64[ns, UTC]', freq='H') In [8]: dti.tz_convert("US/Pacific") Out[8]: DatetimeIndex(['2017-12-31 16:00:00-08:00', '2017-12-31 17:00:00-08:00', '2017-12-31 18:00:00-08:00'], dtype='datetime64[ns, US/Pacific]', freq='H') Resampling or converting a time series to a particular frequency In [9]: idx = pd.date_range("2018-01-01", periods=5, freq="H") In [10]: ts = pd.Series(range(len(idx)), index=idx) In [11]: ts Out[11]: 2018-01-01 00:00:00 0 2018-01-01 01:00:00 1 2018-01-01 02:00:00 2 2018-01-01 03:00:00 3 2018-01-01 04:00:00 4 Freq: H, dtype: int64 In [12]: ts.resample("2H").mean() Out[12]: 2018-01-01 00:00:00 0.5 2018-01-01 02:00:00 2.5 2018-01-01 04:00:00 4.0 Freq: 2H, dtype: float64 Performing date and time arithmetic with absolute or relative time increments In [13]: friday = pd.Timestamp("2018-01-05") In [14]: friday.day_name() Out[14]: 'Friday' # Add 1 day In [15]: saturday = friday + pd.Timedelta("1 day") In [16]: saturday.day_name() Out[16]: 'Saturday' # Add 1 business day (Friday --> Monday) In [17]: monday = friday + pd.offsets.BDay() In [18]: monday.day_name() Out[18]: 'Monday' pandas provides a relatively compact and self-contained set of tools for performing the above tasks and more. Overview# pandas captures 4 general time related concepts: Date times: A specific date and time with timezone support. Similar to datetime.datetime from the standard library. Time deltas: An absolute time duration. Similar to datetime.timedelta from the standard library. Time spans: A span of time defined by a point in time and its associated frequency. Date offsets: A relative time duration that respects calendar arithmetic. Similar to dateutil.relativedelta.relativedelta from the dateutil package. Concept Scalar Class Array Class pandas Data Type Primary Creation Method Date times Timestamp DatetimeIndex datetime64[ns] or datetime64[ns, tz] to_datetime or date_range Time deltas Timedelta TimedeltaIndex timedelta64[ns] to_timedelta or timedelta_range Time spans Period PeriodIndex period[freq] Period or period_range Date offsets DateOffset None None DateOffset For time series data, it’s conventional to represent the time component in the index of a Series or DataFrame so manipulations can be performed with respect to the time element. In [19]: pd.Series(range(3), index=pd.date_range("2000", freq="D", periods=3)) Out[19]: 2000-01-01 0 2000-01-02 1 2000-01-03 2 Freq: D, dtype: int64 However, Series and DataFrame can directly also support the time component as data itself. In [20]: pd.Series(pd.date_range("2000", freq="D", periods=3)) Out[20]: 0 2000-01-01 1 2000-01-02 2 2000-01-03 dtype: datetime64[ns] Series and DataFrame have extended data type support and functionality for datetime, timedelta and Period data when passed into those constructors. DateOffset data however will be stored as object data. In [21]: pd.Series(pd.period_range("1/1/2011", freq="M", periods=3)) Out[21]: 0 2011-01 1 2011-02 2 2011-03 dtype: period[M] In [22]: pd.Series([pd.DateOffset(1), pd.DateOffset(2)]) Out[22]: 0 <DateOffset> 1 <2 * DateOffsets> dtype: object In [23]: pd.Series(pd.date_range("1/1/2011", freq="M", periods=3)) Out[23]: 0 2011-01-31 1 2011-02-28 2 2011-03-31 dtype: datetime64[ns] Lastly, pandas represents null date times, time deltas, and time spans as NaT which is useful for representing missing or null date like values and behaves similar as np.nan does for float data. In [24]: pd.Timestamp(pd.NaT) Out[24]: NaT In [25]: pd.Timedelta(pd.NaT) Out[25]: NaT In [26]: pd.Period(pd.NaT) Out[26]: NaT # Equality acts as np.nan would In [27]: pd.NaT == pd.NaT Out[27]: False Timestamps vs. time spans# Timestamped data is the most basic type of time series data that associates values with points in time. For pandas objects it means using the points in time. In [28]: pd.Timestamp(datetime.datetime(2012, 5, 1)) Out[28]: Timestamp('2012-05-01 00:00:00') In [29]: pd.Timestamp("2012-05-01") Out[29]: Timestamp('2012-05-01 00:00:00') In [30]: pd.Timestamp(2012, 5, 1) Out[30]: Timestamp('2012-05-01 00:00:00') However, in many cases it is more natural to associate things like change variables with a time span instead. The span represented by Period can be specified explicitly, or inferred from datetime string format. For example: In [31]: pd.Period("2011-01") Out[31]: Period('2011-01', 'M') In [32]: pd.Period("2012-05", freq="D") Out[32]: Period('2012-05-01', 'D') Timestamp and Period can serve as an index. Lists of Timestamp and Period are automatically coerced to DatetimeIndex and PeriodIndex respectively. In [33]: dates = [ ....: pd.Timestamp("2012-05-01"), ....: pd.Timestamp("2012-05-02"), ....: pd.Timestamp("2012-05-03"), ....: ] ....: In [34]: ts = pd.Series(np.random.randn(3), dates) In [35]: type(ts.index) Out[35]: pandas.core.indexes.datetimes.DatetimeIndex In [36]: ts.index Out[36]: DatetimeIndex(['2012-05-01', '2012-05-02', '2012-05-03'], dtype='datetime64[ns]', freq=None) In [37]: ts Out[37]: 2012-05-01 0.469112 2012-05-02 -0.282863 2012-05-03 -1.509059 dtype: float64 In [38]: periods = [pd.Period("2012-01"), pd.Period("2012-02"), pd.Period("2012-03")] In [39]: ts = pd.Series(np.random.randn(3), periods) In [40]: type(ts.index) Out[40]: pandas.core.indexes.period.PeriodIndex In [41]: ts.index Out[41]: PeriodIndex(['2012-01', '2012-02', '2012-03'], dtype='period[M]') In [42]: ts Out[42]: 2012-01 -1.135632 2012-02 1.212112 2012-03 -0.173215 Freq: M, dtype: float64 pandas allows you to capture both representations and convert between them. Under the hood, pandas represents timestamps using instances of Timestamp and sequences of timestamps using instances of DatetimeIndex. For regular time spans, pandas uses Period objects for scalar values and PeriodIndex for sequences of spans. Better support for irregular intervals with arbitrary start and end points are forth-coming in future releases. Converting to timestamps# To convert a Series or list-like object of date-like objects e.g. strings, epochs, or a mixture, you can use the to_datetime function. When passed a Series, this returns a Series (with the same index), while a list-like is converted to a DatetimeIndex: In [43]: pd.to_datetime(pd.Series(["Jul 31, 2009", "2010-01-10", None])) Out[43]: 0 2009-07-31 1 2010-01-10 2 NaT dtype: datetime64[ns] In [44]: pd.to_datetime(["2005/11/23", "2010.12.31"]) Out[44]: DatetimeIndex(['2005-11-23', '2010-12-31'], dtype='datetime64[ns]', freq=None) If you use dates which start with the day first (i.e. European style), you can pass the dayfirst flag: In [45]: pd.to_datetime(["04-01-2012 10:00"], dayfirst=True) Out[45]: DatetimeIndex(['2012-01-04 10:00:00'], dtype='datetime64[ns]', freq=None) In [46]: pd.to_datetime(["14-01-2012", "01-14-2012"], dayfirst=True) Out[46]: DatetimeIndex(['2012-01-14', '2012-01-14'], dtype='datetime64[ns]', freq=None) Warning You see in the above example that dayfirst isn’t strict. If a date can’t be parsed with the day being first it will be parsed as if dayfirst were False, and in the case of parsing delimited date strings (e.g. 31-12-2012) then a warning will also be raised. If you pass a single string to to_datetime, it returns a single Timestamp. Timestamp can also accept string input, but it doesn’t accept string parsing options like dayfirst or format, so use to_datetime if these are required. In [47]: pd.to_datetime("2010/11/12") Out[47]: Timestamp('2010-11-12 00:00:00') In [48]: pd.Timestamp("2010/11/12") Out[48]: Timestamp('2010-11-12 00:00:00') You can also use the DatetimeIndex constructor directly: In [49]: pd.DatetimeIndex(["2018-01-01", "2018-01-03", "2018-01-05"]) Out[49]: DatetimeIndex(['2018-01-01', '2018-01-03', '2018-01-05'], dtype='datetime64[ns]', freq=None) The string ‘infer’ can be passed in order to set the frequency of the index as the inferred frequency upon creation: In [50]: pd.DatetimeIndex(["2018-01-01", "2018-01-03", "2018-01-05"], freq="infer") Out[50]: DatetimeIndex(['2018-01-01', '2018-01-03', '2018-01-05'], dtype='datetime64[ns]', freq='2D') Providing a format argument# In addition to the required datetime string, a format argument can be passed to ensure specific parsing. This could also potentially speed up the conversion considerably. In [51]: pd.to_datetime("2010/11/12", format="%Y/%m/%d") Out[51]: Timestamp('2010-11-12 00:00:00') In [52]: pd.to_datetime("12-11-2010 00:00", format="%d-%m-%Y %H:%M") Out[52]: Timestamp('2010-11-12 00:00:00') For more information on the choices available when specifying the format option, see the Python datetime documentation. Assembling datetime from multiple DataFrame columns# You can also pass a DataFrame of integer or string columns to assemble into a Series of Timestamps. In [53]: df = pd.DataFrame( ....: {"year": [2015, 2016], "month": [2, 3], "day": [4, 5], "hour": [2, 3]} ....: ) ....: In [54]: pd.to_datetime(df) Out[54]: 0 2015-02-04 02:00:00 1 2016-03-05 03:00:00 dtype: datetime64[ns] You can pass only the columns that you need to assemble. In [55]: pd.to_datetime(df[["year", "month", "day"]]) Out[55]: 0 2015-02-04 1 2016-03-05 dtype: datetime64[ns] pd.to_datetime looks for standard designations of the datetime component in the column names, including: required: year, month, day optional: hour, minute, second, millisecond, microsecond, nanosecond Invalid data# The default behavior, errors='raise', is to raise when unparsable: In [2]: pd.to_datetime(['2009/07/31', 'asd'], errors='raise') ValueError: Unknown string format Pass errors='ignore' to return the original input when unparsable: In [56]: pd.to_datetime(["2009/07/31", "asd"], errors="ignore") Out[56]: Index(['2009/07/31', 'asd'], dtype='object') Pass errors='coerce' to convert unparsable data to NaT (not a time): In [57]: pd.to_datetime(["2009/07/31", "asd"], errors="coerce") Out[57]: DatetimeIndex(['2009-07-31', 'NaT'], dtype='datetime64[ns]', freq=None) Epoch timestamps# pandas supports converting integer or float epoch times to Timestamp and DatetimeIndex. The default unit is nanoseconds, since that is how Timestamp objects are stored internally. However, epochs are often stored in another unit which can be specified. These are computed from the starting point specified by the origin parameter. In [58]: pd.to_datetime( ....: [1349720105, 1349806505, 1349892905, 1349979305, 1350065705], unit="s" ....: ) ....: Out[58]: DatetimeIndex(['2012-10-08 18:15:05', '2012-10-09 18:15:05', '2012-10-10 18:15:05', '2012-10-11 18:15:05', '2012-10-12 18:15:05'], dtype='datetime64[ns]', freq=None) In [59]: pd.to_datetime( ....: [1349720105100, 1349720105200, 1349720105300, 1349720105400, 1349720105500], ....: unit="ms", ....: ) ....: Out[59]: DatetimeIndex(['2012-10-08 18:15:05.100000', '2012-10-08 18:15:05.200000', '2012-10-08 18:15:05.300000', '2012-10-08 18:15:05.400000', '2012-10-08 18:15:05.500000'], dtype='datetime64[ns]', freq=None) Note The unit parameter does not use the same strings as the format parameter that was discussed above). The available units are listed on the documentation for pandas.to_datetime(). Changed in version 1.0.0. Constructing a Timestamp or DatetimeIndex with an epoch timestamp with the tz argument specified will raise a ValueError. If you have epochs in wall time in another timezone, you can read the epochs as timezone-naive timestamps and then localize to the appropriate timezone: In [60]: pd.Timestamp(1262347200000000000).tz_localize("US/Pacific") Out[60]: Timestamp('2010-01-01 12:00:00-0800', tz='US/Pacific') In [61]: pd.DatetimeIndex([1262347200000000000]).tz_localize("US/Pacific") Out[61]: DatetimeIndex(['2010-01-01 12:00:00-08:00'], dtype='datetime64[ns, US/Pacific]', freq=None) Note Epoch times will be rounded to the nearest nanosecond. Warning Conversion of float epoch times can lead to inaccurate and unexpected results. Python floats have about 15 digits precision in decimal. Rounding during conversion from float to high precision Timestamp is unavoidable. The only way to achieve exact precision is to use a fixed-width types (e.g. an int64). In [62]: pd.to_datetime([1490195805.433, 1490195805.433502912], unit="s") Out[62]: DatetimeIndex(['2017-03-22 15:16:45.433000088', '2017-03-22 15:16:45.433502913'], dtype='datetime64[ns]', freq=None) In [63]: pd.to_datetime(1490195805433502912, unit="ns") Out[63]: Timestamp('2017-03-22 15:16:45.433502912') See also Using the origin parameter From timestamps to epoch# To invert the operation from above, namely, to convert from a Timestamp to a ‘unix’ epoch: In [64]: stamps = pd.date_range("2012-10-08 18:15:05", periods=4, freq="D") In [65]: stamps Out[65]: DatetimeIndex(['2012-10-08 18:15:05', '2012-10-09 18:15:05', '2012-10-10 18:15:05', '2012-10-11 18:15:05'], dtype='datetime64[ns]', freq='D') We subtract the epoch (midnight at January 1, 1970 UTC) and then floor divide by the “unit” (1 second). In [66]: (stamps - pd.Timestamp("1970-01-01")) // pd.Timedelta("1s") Out[66]: Int64Index([1349720105, 1349806505, 1349892905, 1349979305], dtype='int64') Using the origin parameter# Using the origin parameter, one can specify an alternative starting point for creation of a DatetimeIndex. For example, to use 1960-01-01 as the starting date: In [67]: pd.to_datetime([1, 2, 3], unit="D", origin=pd.Timestamp("1960-01-01")) Out[67]: DatetimeIndex(['1960-01-02', '1960-01-03', '1960-01-04'], dtype='datetime64[ns]', freq=None) The default is set at origin='unix', which defaults to 1970-01-01 00:00:00. Commonly called ‘unix epoch’ or POSIX time. In [68]: pd.to_datetime([1, 2, 3], unit="D") Out[68]: DatetimeIndex(['1970-01-02', '1970-01-03', '1970-01-04'], dtype='datetime64[ns]', freq=None) Generating ranges of timestamps# To generate an index with timestamps, you can use either the DatetimeIndex or Index constructor and pass in a list of datetime objects: In [69]: dates = [ ....: datetime.datetime(2012, 5, 1), ....: datetime.datetime(2012, 5, 2), ....: datetime.datetime(2012, 5, 3), ....: ] ....: # Note the frequency information In [70]: index = pd.DatetimeIndex(dates) In [71]: index Out[71]: DatetimeIndex(['2012-05-01', '2012-05-02', '2012-05-03'], dtype='datetime64[ns]', freq=None) # Automatically converted to DatetimeIndex In [72]: index = pd.Index(dates) In [73]: index Out[73]: DatetimeIndex(['2012-05-01', '2012-05-02', '2012-05-03'], dtype='datetime64[ns]', freq=None) In practice this becomes very cumbersome because we often need a very long index with a large number of timestamps. If we need timestamps on a regular frequency, we can use the date_range() and bdate_range() functions to create a DatetimeIndex. The default frequency for date_range is a calendar day while the default for bdate_range is a business day: In [74]: start = datetime.datetime(2011, 1, 1) In [75]: end = datetime.datetime(2012, 1, 1) In [76]: index = pd.date_range(start, end) In [77]: index Out[77]: DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03', '2011-01-04', '2011-01-05', '2011-01-06', '2011-01-07', '2011-01-08', '2011-01-09', '2011-01-10', ... '2011-12-23', '2011-12-24', '2011-12-25', '2011-12-26', '2011-12-27', '2011-12-28', '2011-12-29', '2011-12-30', '2011-12-31', '2012-01-01'], dtype='datetime64[ns]', length=366, freq='D') In [78]: index = pd.bdate_range(start, end) In [79]: index Out[79]: DatetimeIndex(['2011-01-03', '2011-01-04', '2011-01-05', '2011-01-06', '2011-01-07', '2011-01-10', '2011-01-11', '2011-01-12', '2011-01-13', '2011-01-14', ... '2011-12-19', '2011-12-20', '2011-12-21', '2011-12-22', '2011-12-23', '2011-12-26', '2011-12-27', '2011-12-28', '2011-12-29', '2011-12-30'], dtype='datetime64[ns]', length=260, freq='B') Convenience functions like date_range and bdate_range can utilize a variety of frequency aliases: In [80]: pd.date_range(start, periods=1000, freq="M") Out[80]: DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-30', '2011-05-31', '2011-06-30', '2011-07-31', '2011-08-31', '2011-09-30', '2011-10-31', ... '2093-07-31', '2093-08-31', '2093-09-30', '2093-10-31', '2093-11-30', '2093-12-31', '2094-01-31', '2094-02-28', '2094-03-31', '2094-04-30'], dtype='datetime64[ns]', length=1000, freq='M') In [81]: pd.bdate_range(start, periods=250, freq="BQS") Out[81]: DatetimeIndex(['2011-01-03', '2011-04-01', '2011-07-01', '2011-10-03', '2012-01-02', '2012-04-02', '2012-07-02', '2012-10-01', '2013-01-01', '2013-04-01', ... '2071-01-01', '2071-04-01', '2071-07-01', '2071-10-01', '2072-01-01', '2072-04-01', '2072-07-01', '2072-10-03', '2073-01-02', '2073-04-03'], dtype='datetime64[ns]', length=250, freq='BQS-JAN') date_range and bdate_range make it easy to generate a range of dates using various combinations of parameters like start, end, periods, and freq. The start and end dates are strictly inclusive, so dates outside of those specified will not be generated: In [82]: pd.date_range(start, end, freq="BM") Out[82]: DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-29', '2011-05-31', '2011-06-30', '2011-07-29', '2011-08-31', '2011-09-30', '2011-10-31', '2011-11-30', '2011-12-30'], dtype='datetime64[ns]', freq='BM') In [83]: pd.date_range(start, end, freq="W") Out[83]: DatetimeIndex(['2011-01-02', '2011-01-09', '2011-01-16', '2011-01-23', '2011-01-30', '2011-02-06', '2011-02-13', '2011-02-20', '2011-02-27', '2011-03-06', '2011-03-13', '2011-03-20', '2011-03-27', '2011-04-03', '2011-04-10', '2011-04-17', '2011-04-24', '2011-05-01', '2011-05-08', '2011-05-15', '2011-05-22', '2011-05-29', '2011-06-05', '2011-06-12', '2011-06-19', '2011-06-26', '2011-07-03', '2011-07-10', '2011-07-17', '2011-07-24', '2011-07-31', '2011-08-07', '2011-08-14', '2011-08-21', '2011-08-28', '2011-09-04', '2011-09-11', '2011-09-18', '2011-09-25', '2011-10-02', '2011-10-09', '2011-10-16', '2011-10-23', '2011-10-30', '2011-11-06', '2011-11-13', '2011-11-20', '2011-11-27', '2011-12-04', '2011-12-11', '2011-12-18', '2011-12-25', '2012-01-01'], dtype='datetime64[ns]', freq='W-SUN') In [84]: pd.bdate_range(end=end, periods=20) Out[84]: DatetimeIndex(['2011-12-05', '2011-12-06', '2011-12-07', '2011-12-08', '2011-12-09', '2011-12-12', '2011-12-13', '2011-12-14', '2011-12-15', '2011-12-16', '2011-12-19', '2011-12-20', '2011-12-21', '2011-12-22', '2011-12-23', '2011-12-26', '2011-12-27', '2011-12-28', '2011-12-29', '2011-12-30'], dtype='datetime64[ns]', freq='B') In [85]: pd.bdate_range(start=start, periods=20) Out[85]: DatetimeIndex(['2011-01-03', '2011-01-04', '2011-01-05', '2011-01-06', '2011-01-07', '2011-01-10', '2011-01-11', '2011-01-12', '2011-01-13', '2011-01-14', '2011-01-17', '2011-01-18', '2011-01-19', '2011-01-20', '2011-01-21', '2011-01-24', '2011-01-25', '2011-01-26', '2011-01-27', '2011-01-28'], dtype='datetime64[ns]', freq='B') Specifying start, end, and periods will generate a range of evenly spaced dates from start to end inclusively, with periods number of elements in the resulting DatetimeIndex: In [86]: pd.date_range("2018-01-01", "2018-01-05", periods=5) Out[86]: DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04', '2018-01-05'], dtype='datetime64[ns]', freq=None) In [87]: pd.date_range("2018-01-01", "2018-01-05", periods=10) Out[87]: DatetimeIndex(['2018-01-01 00:00:00', '2018-01-01 10:40:00', '2018-01-01 21:20:00', '2018-01-02 08:00:00', '2018-01-02 18:40:00', '2018-01-03 05:20:00', '2018-01-03 16:00:00', '2018-01-04 02:40:00', '2018-01-04 13:20:00', '2018-01-05 00:00:00'], dtype='datetime64[ns]', freq=None) Custom frequency ranges# bdate_range can also generate a range of custom frequency dates by using the weekmask and holidays parameters. These parameters will only be used if a custom frequency string is passed. In [88]: weekmask = "Mon Wed Fri" In [89]: holidays = [datetime.datetime(2011, 1, 5), datetime.datetime(2011, 3, 14)] In [90]: pd.bdate_range(start, end, freq="C", weekmask=weekmask, holidays=holidays) Out[90]: DatetimeIndex(['2011-01-03', '2011-01-07', '2011-01-10', '2011-01-12', '2011-01-14', '2011-01-17', '2011-01-19', '2011-01-21', '2011-01-24', '2011-01-26', ... '2011-12-09', '2011-12-12', '2011-12-14', '2011-12-16', '2011-12-19', '2011-12-21', '2011-12-23', '2011-12-26', '2011-12-28', '2011-12-30'], dtype='datetime64[ns]', length=154, freq='C') In [91]: pd.bdate_range(start, end, freq="CBMS", weekmask=weekmask) Out[91]: DatetimeIndex(['2011-01-03', '2011-02-02', '2011-03-02', '2011-04-01', '2011-05-02', '2011-06-01', '2011-07-01', '2011-08-01', '2011-09-02', '2011-10-03', '2011-11-02', '2011-12-02'], dtype='datetime64[ns]', freq='CBMS') See also Custom business days Timestamp limitations# Since pandas represents timestamps in nanosecond resolution, the time span that can be represented using a 64-bit integer is limited to approximately 584 years: In [92]: pd.Timestamp.min Out[92]: Timestamp('1677-09-21 00:12:43.145224193') In [93]: pd.Timestamp.max Out[93]: Timestamp('2262-04-11 23:47:16.854775807') See also Representing out-of-bounds spans Indexing# One of the main uses for DatetimeIndex is as an index for pandas objects. The DatetimeIndex class contains many time series related optimizations: A large range of dates for various offsets are pre-computed and cached under the hood in order to make generating subsequent date ranges very fast (just have to grab a slice). Fast shifting using the shift method on pandas objects. Unioning of overlapping DatetimeIndex objects with the same frequency is very fast (important for fast data alignment). Quick access to date fields via properties such as year, month, etc. Regularization functions like snap and very fast asof logic. DatetimeIndex objects have all the basic functionality of regular Index objects, and a smorgasbord of advanced time series specific methods for easy frequency processing. See also Reindexing methods Note While pandas does not force you to have a sorted date index, some of these methods may have unexpected or incorrect behavior if the dates are unsorted. DatetimeIndex can be used like a regular index and offers all of its intelligent functionality like selection, slicing, etc. In [94]: rng = pd.date_range(start, end, freq="BM") In [95]: ts = pd.Series(np.random.randn(len(rng)), index=rng) In [96]: ts.index Out[96]: DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-29', '2011-05-31', '2011-06-30', '2011-07-29', '2011-08-31', '2011-09-30', '2011-10-31', '2011-11-30', '2011-12-30'], dtype='datetime64[ns]', freq='BM') In [97]: ts[:5].index Out[97]: DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-29', '2011-05-31'], dtype='datetime64[ns]', freq='BM') In [98]: ts[::2].index Out[98]: DatetimeIndex(['2011-01-31', '2011-03-31', '2011-05-31', '2011-07-29', '2011-09-30', '2011-11-30'], dtype='datetime64[ns]', freq='2BM') Partial string indexing# Dates and strings that parse to timestamps can be passed as indexing parameters: In [99]: ts["1/31/2011"] Out[99]: 0.11920871129693428 In [100]: ts[datetime.datetime(2011, 12, 25):] Out[100]: 2011-12-30 0.56702 Freq: BM, dtype: float64 In [101]: ts["10/31/2011":"12/31/2011"] Out[101]: 2011-10-31 0.271860 2011-11-30 -0.424972 2011-12-30 0.567020 Freq: BM, dtype: float64 To provide convenience for accessing longer time series, you can also pass in the year or year and month as strings: In [102]: ts["2011"] Out[102]: 2011-01-31 0.119209 2011-02-28 -1.044236 2011-03-31 -0.861849 2011-04-29 -2.104569 2011-05-31 -0.494929 2011-06-30 1.071804 2011-07-29 0.721555 2011-08-31 -0.706771 2011-09-30 -1.039575 2011-10-31 0.271860 2011-11-30 -0.424972 2011-12-30 0.567020 Freq: BM, dtype: float64 In [103]: ts["2011-6"] Out[103]: 2011-06-30 1.071804 Freq: BM, dtype: float64 This type of slicing will work on a DataFrame with a DatetimeIndex as well. Since the partial string selection is a form of label slicing, the endpoints will be included. This would include matching times on an included date: Warning Indexing DataFrame rows with a single string with getitem (e.g. frame[dtstring]) is deprecated starting with pandas 1.2.0 (given the ambiguity whether it is indexing the rows or selecting a column) and will be removed in a future version. The equivalent with .loc (e.g. frame.loc[dtstring]) is still supported. In [104]: dft = pd.DataFrame( .....: np.random.randn(100000, 1), .....: columns=["A"], .....: index=pd.date_range("20130101", periods=100000, freq="T"), .....: ) .....: In [105]: dft Out[105]: A 2013-01-01 00:00:00 0.276232 2013-01-01 00:01:00 -1.087401 2013-01-01 00:02:00 -0.673690 2013-01-01 00:03:00 0.113648 2013-01-01 00:04:00 -1.478427 ... ... 2013-03-11 10:35:00 -0.747967 2013-03-11 10:36:00 -0.034523 2013-03-11 10:37:00 -0.201754 2013-03-11 10:38:00 -1.509067 2013-03-11 10:39:00 -1.693043 [100000 rows x 1 columns] In [106]: dft.loc["2013"] Out[106]: A 2013-01-01 00:00:00 0.276232 2013-01-01 00:01:00 -1.087401 2013-01-01 00:02:00 -0.673690 2013-01-01 00:03:00 0.113648 2013-01-01 00:04:00 -1.478427 ... ... 2013-03-11 10:35:00 -0.747967 2013-03-11 10:36:00 -0.034523 2013-03-11 10:37:00 -0.201754 2013-03-11 10:38:00 -1.509067 2013-03-11 10:39:00 -1.693043 [100000 rows x 1 columns] This starts on the very first time in the month, and includes the last date and time for the month: In [107]: dft["2013-1":"2013-2"] Out[107]: A 2013-01-01 00:00:00 0.276232 2013-01-01 00:01:00 -1.087401 2013-01-01 00:02:00 -0.673690 2013-01-01 00:03:00 0.113648 2013-01-01 00:04:00 -1.478427 ... ... 2013-02-28 23:55:00 0.850929 2013-02-28 23:56:00 0.976712 2013-02-28 23:57:00 -2.693884 2013-02-28 23:58:00 -1.575535 2013-02-28 23:59:00 -1.573517 [84960 rows x 1 columns] This specifies a stop time that includes all of the times on the last day: In [108]: dft["2013-1":"2013-2-28"] Out[108]: A 2013-01-01 00:00:00 0.276232 2013-01-01 00:01:00 -1.087401 2013-01-01 00:02:00 -0.673690 2013-01-01 00:03:00 0.113648 2013-01-01 00:04:00 -1.478427 ... ... 2013-02-28 23:55:00 0.850929 2013-02-28 23:56:00 0.976712 2013-02-28 23:57:00 -2.693884 2013-02-28 23:58:00 -1.575535 2013-02-28 23:59:00 -1.573517 [84960 rows x 1 columns] This specifies an exact stop time (and is not the same as the above): In [109]: dft["2013-1":"2013-2-28 00:00:00"] Out[109]: A 2013-01-01 00:00:00 0.276232 2013-01-01 00:01:00 -1.087401 2013-01-01 00:02:00 -0.673690 2013-01-01 00:03:00 0.113648 2013-01-01 00:04:00 -1.478427 ... ... 2013-02-27 23:56:00 1.197749 2013-02-27 23:57:00 0.720521 2013-02-27 23:58:00 -0.072718 2013-02-27 23:59:00 -0.681192 2013-02-28 00:00:00 -0.557501 [83521 rows x 1 columns] We are stopping on the included end-point as it is part of the index: In [110]: dft["2013-1-15":"2013-1-15 12:30:00"] Out[110]: A 2013-01-15 00:00:00 -0.984810 2013-01-15 00:01:00 0.941451 2013-01-15 00:02:00 1.559365 2013-01-15 00:03:00 1.034374 2013-01-15 00:04:00 -1.480656 ... ... 2013-01-15 12:26:00 0.371454 2013-01-15 12:27:00 -0.930806 2013-01-15 12:28:00 -0.069177 2013-01-15 12:29:00 0.066510 2013-01-15 12:30:00 -0.003945 [751 rows x 1 columns] DatetimeIndex partial string indexing also works on a DataFrame with a MultiIndex: In [111]: dft2 = pd.DataFrame( .....: np.random.randn(20, 1), .....: columns=["A"], .....: index=pd.MultiIndex.from_product( .....: [pd.date_range("20130101", periods=10, freq="12H"), ["a", "b"]] .....: ), .....: ) .....: In [112]: dft2 Out[112]: A 2013-01-01 00:00:00 a -0.298694 b 0.823553 2013-01-01 12:00:00 a 0.943285 b -1.479399 2013-01-02 00:00:00 a -1.643342 ... ... 2013-01-04 12:00:00 b 0.069036 2013-01-05 00:00:00 a 0.122297 b 1.422060 2013-01-05 12:00:00 a 0.370079 b 1.016331 [20 rows x 1 columns] In [113]: dft2.loc["2013-01-05"] Out[113]: A 2013-01-05 00:00:00 a 0.122297 b 1.422060 2013-01-05 12:00:00 a 0.370079 b 1.016331 In [114]: idx = pd.IndexSlice In [115]: dft2 = dft2.swaplevel(0, 1).sort_index() In [116]: dft2.loc[idx[:, "2013-01-05"], :] Out[116]: A a 2013-01-05 00:00:00 0.122297 2013-01-05 12:00:00 0.370079 b 2013-01-05 00:00:00 1.422060 2013-01-05 12:00:00 1.016331 New in version 0.25.0. Slicing with string indexing also honors UTC offset. In [117]: df = pd.DataFrame([0], index=pd.DatetimeIndex(["2019-01-01"], tz="US/Pacific")) In [118]: df Out[118]: 0 2019-01-01 00:00:00-08:00 0 In [119]: df["2019-01-01 12:00:00+04:00":"2019-01-01 13:00:00+04:00"] Out[119]: 0 2019-01-01 00:00:00-08:00 0 Slice vs. exact match# The same string used as an indexing parameter can be treated either as a slice or as an exact match depending on the resolution of the index. If the string is less accurate than the index, it will be treated as a slice, otherwise as an exact match. Consider a Series object with a minute resolution index: In [120]: series_minute = pd.Series( .....: [1, 2, 3], .....: pd.DatetimeIndex( .....: ["2011-12-31 23:59:00", "2012-01-01 00:00:00", "2012-01-01 00:02:00"] .....: ), .....: ) .....: In [121]: series_minute.index.resolution Out[121]: 'minute' A timestamp string less accurate than a minute gives a Series object. In [122]: series_minute["2011-12-31 23"] Out[122]: 2011-12-31 23:59:00 1 dtype: int64 A timestamp string with minute resolution (or more accurate), gives a scalar instead, i.e. it is not casted to a slice. In [123]: series_minute["2011-12-31 23:59"] Out[123]: 1 In [124]: series_minute["2011-12-31 23:59:00"] Out[124]: 1 If index resolution is second, then the minute-accurate timestamp gives a Series. In [125]: series_second = pd.Series( .....: [1, 2, 3], .....: pd.DatetimeIndex( .....: ["2011-12-31 23:59:59", "2012-01-01 00:00:00", "2012-01-01 00:00:01"] .....: ), .....: ) .....: In [126]: series_second.index.resolution Out[126]: 'second' In [127]: series_second["2011-12-31 23:59"] Out[127]: 2011-12-31 23:59:59 1 dtype: int64 If the timestamp string is treated as a slice, it can be used to index DataFrame with .loc[] as well. In [128]: dft_minute = pd.DataFrame( .....: {"a": [1, 2, 3], "b": [4, 5, 6]}, index=series_minute.index .....: ) .....: In [129]: dft_minute.loc["2011-12-31 23"] Out[129]: a b 2011-12-31 23:59:00 1 4 Warning However, if the string is treated as an exact match, the selection in DataFrame’s [] will be column-wise and not row-wise, see Indexing Basics. For example dft_minute['2011-12-31 23:59'] will raise KeyError as '2012-12-31 23:59' has the same resolution as the index and there is no column with such name: To always have unambiguous selection, whether the row is treated as a slice or a single selection, use .loc. In [130]: dft_minute.loc["2011-12-31 23:59"] Out[130]: a 1 b 4 Name: 2011-12-31 23:59:00, dtype: int64 Note also that DatetimeIndex resolution cannot be less precise than day. In [131]: series_monthly = pd.Series( .....: [1, 2, 3], pd.DatetimeIndex(["2011-12", "2012-01", "2012-02"]) .....: ) .....: In [132]: series_monthly.index.resolution Out[132]: 'day' In [133]: series_monthly["2011-12"] # returns Series Out[133]: 2011-12-01 1 dtype: int64 Exact indexing# As discussed in previous section, indexing a DatetimeIndex with a partial string depends on the “accuracy” of the period, in other words how specific the interval is in relation to the resolution of the index. In contrast, indexing with Timestamp or datetime objects is exact, because the objects have exact meaning. These also follow the semantics of including both endpoints. These Timestamp and datetime objects have exact hours, minutes, and seconds, even though they were not explicitly specified (they are 0). In [134]: dft[datetime.datetime(2013, 1, 1): datetime.datetime(2013, 2, 28)] Out[134]: A 2013-01-01 00:00:00 0.276232 2013-01-01 00:01:00 -1.087401 2013-01-01 00:02:00 -0.673690 2013-01-01 00:03:00 0.113648 2013-01-01 00:04:00 -1.478427 ... ... 2013-02-27 23:56:00 1.197749 2013-02-27 23:57:00 0.720521 2013-02-27 23:58:00 -0.072718 2013-02-27 23:59:00 -0.681192 2013-02-28 00:00:00 -0.557501 [83521 rows x 1 columns] With no defaults. In [135]: dft[ .....: datetime.datetime(2013, 1, 1, 10, 12, 0): datetime.datetime( .....: 2013, 2, 28, 10, 12, 0 .....: ) .....: ] .....: Out[135]: A 2013-01-01 10:12:00 0.565375 2013-01-01 10:13:00 0.068184 2013-01-01 10:14:00 0.788871 2013-01-01 10:15:00 -0.280343 2013-01-01 10:16:00 0.931536 ... ... 2013-02-28 10:08:00 0.148098 2013-02-28 10:09:00 -0.388138 2013-02-28 10:10:00 0.139348 2013-02-28 10:11:00 0.085288 2013-02-28 10:12:00 0.950146 [83521 rows x 1 columns] Truncating & fancy indexing# A truncate() convenience function is provided that is similar to slicing. Note that truncate assumes a 0 value for any unspecified date component in a DatetimeIndex in contrast to slicing which returns any partially matching dates: In [136]: rng2 = pd.date_range("2011-01-01", "2012-01-01", freq="W") In [137]: ts2 = pd.Series(np.random.randn(len(rng2)), index=rng2) In [138]: ts2.truncate(before="2011-11", after="2011-12") Out[138]: 2011-11-06 0.437823 2011-11-13 -0.293083 2011-11-20 -0.059881 2011-11-27 1.252450 Freq: W-SUN, dtype: float64 In [139]: ts2["2011-11":"2011-12"] Out[139]: 2011-11-06 0.437823 2011-11-13 -0.293083 2011-11-20 -0.059881 2011-11-27 1.252450 2011-12-04 0.046611 2011-12-11 0.059478 2011-12-18 -0.286539 2011-12-25 0.841669 Freq: W-SUN, dtype: float64 Even complicated fancy indexing that breaks the DatetimeIndex frequency regularity will result in a DatetimeIndex, although frequency is lost: In [140]: ts2[[0, 2, 6]].index Out[140]: DatetimeIndex(['2011-01-02', '2011-01-16', '2011-02-13'], dtype='datetime64[ns]', freq=None) Time/date components# There are several time/date properties that one can access from Timestamp or a collection of timestamps like a DatetimeIndex. Property Description year The year of the datetime month The month of the datetime day The days of the datetime hour The hour of the datetime minute The minutes of the datetime second The seconds of the datetime microsecond The microseconds of the datetime nanosecond The nanoseconds of the datetime date Returns datetime.date (does not contain timezone information) time Returns datetime.time (does not contain timezone information) timetz Returns datetime.time as local time with timezone information dayofyear The ordinal day of year day_of_year The ordinal day of year weekofyear The week ordinal of the year week The week ordinal of the year dayofweek The number of the day of the week with Monday=0, Sunday=6 day_of_week The number of the day of the week with Monday=0, Sunday=6 weekday The number of the day of the week with Monday=0, Sunday=6 quarter Quarter of the date: Jan-Mar = 1, Apr-Jun = 2, etc. days_in_month The number of days in the month of the datetime is_month_start Logical indicating if first day of month (defined by frequency) is_month_end Logical indicating if last day of month (defined by frequency) is_quarter_start Logical indicating if first day of quarter (defined by frequency) is_quarter_end Logical indicating if last day of quarter (defined by frequency) is_year_start Logical indicating if first day of year (defined by frequency) is_year_end Logical indicating if last day of year (defined by frequency) is_leap_year Logical indicating if the date belongs to a leap year Furthermore, if you have a Series with datetimelike values, then you can access these properties via the .dt accessor, as detailed in the section on .dt accessors. New in version 1.1.0. You may obtain the year, week and day components of the ISO year from the ISO 8601 standard: In [141]: idx = pd.date_range(start="2019-12-29", freq="D", periods=4) In [142]: idx.isocalendar() Out[142]: year week day 2019-12-29 2019 52 7 2019-12-30 2020 1 1 2019-12-31 2020 1 2 2020-01-01 2020 1 3 In [143]: idx.to_series().dt.isocalendar() Out[143]: year week day 2019-12-29 2019 52 7 2019-12-30 2020 1 1 2019-12-31 2020 1 2 2020-01-01 2020 1 3 DateOffset objects# In the preceding examples, frequency strings (e.g. 'D') were used to specify a frequency that defined: how the date times in DatetimeIndex were spaced when using date_range() the frequency of a Period or PeriodIndex These frequency strings map to a DateOffset object and its subclasses. A DateOffset is similar to a Timedelta that represents a duration of time but follows specific calendar duration rules. For example, a Timedelta day will always increment datetimes by 24 hours, while a DateOffset day will increment datetimes to the same time the next day whether a day represents 23, 24 or 25 hours due to daylight savings time. However, all DateOffset subclasses that are an hour or smaller (Hour, Minute, Second, Milli, Micro, Nano) behave like Timedelta and respect absolute time. The basic DateOffset acts similar to dateutil.relativedelta (relativedelta documentation) that shifts a date time by the corresponding calendar duration specified. The arithmetic operator (+) can be used to perform the shift. # This particular day contains a day light savings time transition In [144]: ts = pd.Timestamp("2016-10-30 00:00:00", tz="Europe/Helsinki") # Respects absolute time In [145]: ts + pd.Timedelta(days=1) Out[145]: Timestamp('2016-10-30 23:00:00+0200', tz='Europe/Helsinki') # Respects calendar time In [146]: ts + pd.DateOffset(days=1) Out[146]: Timestamp('2016-10-31 00:00:00+0200', tz='Europe/Helsinki') In [147]: friday = pd.Timestamp("2018-01-05") In [148]: friday.day_name() Out[148]: 'Friday' # Add 2 business days (Friday --> Tuesday) In [149]: two_business_days = 2 * pd.offsets.BDay() In [150]: friday + two_business_days Out[150]: Timestamp('2018-01-09 00:00:00') In [151]: (friday + two_business_days).day_name() Out[151]: 'Tuesday' Most DateOffsets have associated frequencies strings, or offset aliases, that can be passed into freq keyword arguments. The available date offsets and associated frequency strings can be found below: Date Offset Frequency String Description DateOffset None Generic offset class, defaults to absolute 24 hours BDay or BusinessDay 'B' business day (weekday) CDay or CustomBusinessDay 'C' custom business day Week 'W' one week, optionally anchored on a day of the week WeekOfMonth 'WOM' the x-th day of the y-th week of each month LastWeekOfMonth 'LWOM' the x-th day of the last week of each month MonthEnd 'M' calendar month end MonthBegin 'MS' calendar month begin BMonthEnd or BusinessMonthEnd 'BM' business month end BMonthBegin or BusinessMonthBegin 'BMS' business month begin CBMonthEnd or CustomBusinessMonthEnd 'CBM' custom business month end CBMonthBegin or CustomBusinessMonthBegin 'CBMS' custom business month begin SemiMonthEnd 'SM' 15th (or other day_of_month) and calendar month end SemiMonthBegin 'SMS' 15th (or other day_of_month) and calendar month begin QuarterEnd 'Q' calendar quarter end QuarterBegin 'QS' calendar quarter begin BQuarterEnd 'BQ business quarter end BQuarterBegin 'BQS' business quarter begin FY5253Quarter 'REQ' retail (aka 52-53 week) quarter YearEnd 'A' calendar year end YearBegin 'AS' or 'BYS' calendar year begin BYearEnd 'BA' business year end BYearBegin 'BAS' business year begin FY5253 'RE' retail (aka 52-53 week) year Easter None Easter holiday BusinessHour 'BH' business hour CustomBusinessHour 'CBH' custom business hour Day 'D' one absolute day Hour 'H' one hour Minute 'T' or 'min' one minute Second 'S' one second Milli 'L' or 'ms' one millisecond Micro 'U' or 'us' one microsecond Nano 'N' one nanosecond DateOffsets additionally have rollforward() and rollback() methods for moving a date forward or backward respectively to a valid offset date relative to the offset. For example, business offsets will roll dates that land on the weekends (Saturday and Sunday) forward to Monday since business offsets operate on the weekdays. In [152]: ts = pd.Timestamp("2018-01-06 00:00:00") In [153]: ts.day_name() Out[153]: 'Saturday' # BusinessHour's valid offset dates are Monday through Friday In [154]: offset = pd.offsets.BusinessHour(start="09:00") # Bring the date to the closest offset date (Monday) In [155]: offset.rollforward(ts) Out[155]: Timestamp('2018-01-08 09:00:00') # Date is brought to the closest offset date first and then the hour is added In [156]: ts + offset Out[156]: Timestamp('2018-01-08 10:00:00') These operations preserve time (hour, minute, etc) information by default. To reset time to midnight, use normalize() before or after applying the operation (depending on whether you want the time information included in the operation). In [157]: ts = pd.Timestamp("2014-01-01 09:00") In [158]: day = pd.offsets.Day() In [159]: day + ts Out[159]: Timestamp('2014-01-02 09:00:00') In [160]: (day + ts).normalize() Out[160]: Timestamp('2014-01-02 00:00:00') In [161]: ts = pd.Timestamp("2014-01-01 22:00") In [162]: hour = pd.offsets.Hour() In [163]: hour + ts Out[163]: Timestamp('2014-01-01 23:00:00') In [164]: (hour + ts).normalize() Out[164]: Timestamp('2014-01-01 00:00:00') In [165]: (hour + pd.Timestamp("2014-01-01 23:30")).normalize() Out[165]: Timestamp('2014-01-02 00:00:00') Parametric offsets# Some of the offsets can be “parameterized” when created to result in different behaviors. For example, the Week offset for generating weekly data accepts a weekday parameter which results in the generated dates always lying on a particular day of the week: In [166]: d = datetime.datetime(2008, 8, 18, 9, 0) In [167]: d Out[167]: datetime.datetime(2008, 8, 18, 9, 0) In [168]: d + pd.offsets.Week() Out[168]: Timestamp('2008-08-25 09:00:00') In [169]: d + pd.offsets.Week(weekday=4) Out[169]: Timestamp('2008-08-22 09:00:00') In [170]: (d + pd.offsets.Week(weekday=4)).weekday() Out[170]: 4 In [171]: d - pd.offsets.Week() Out[171]: Timestamp('2008-08-11 09:00:00') The normalize option will be effective for addition and subtraction. In [172]: d + pd.offsets.Week(normalize=True) Out[172]: Timestamp('2008-08-25 00:00:00') In [173]: d - pd.offsets.Week(normalize=True) Out[173]: Timestamp('2008-08-11 00:00:00') Another example is parameterizing YearEnd with the specific ending month: In [174]: d + pd.offsets.YearEnd() Out[174]: Timestamp('2008-12-31 09:00:00') In [175]: d + pd.offsets.YearEnd(month=6) Out[175]: Timestamp('2009-06-30 09:00:00') Using offsets with Series / DatetimeIndex# Offsets can be used with either a Series or DatetimeIndex to apply the offset to each element. In [176]: rng = pd.date_range("2012-01-01", "2012-01-03") In [177]: s = pd.Series(rng) In [178]: rng Out[178]: DatetimeIndex(['2012-01-01', '2012-01-02', '2012-01-03'], dtype='datetime64[ns]', freq='D') In [179]: rng + pd.DateOffset(months=2) Out[179]: DatetimeIndex(['2012-03-01', '2012-03-02', '2012-03-03'], dtype='datetime64[ns]', freq=None) In [180]: s + pd.DateOffset(months=2) Out[180]: 0 2012-03-01 1 2012-03-02 2 2012-03-03 dtype: datetime64[ns] In [181]: s - pd.DateOffset(months=2) Out[181]: 0 2011-11-01 1 2011-11-02 2 2011-11-03 dtype: datetime64[ns] If the offset class maps directly to a Timedelta (Day, Hour, Minute, Second, Micro, Milli, Nano) it can be used exactly like a Timedelta - see the Timedelta section for more examples. In [182]: s - pd.offsets.Day(2) Out[182]: 0 2011-12-30 1 2011-12-31 2 2012-01-01 dtype: datetime64[ns] In [183]: td = s - pd.Series(pd.date_range("2011-12-29", "2011-12-31")) In [184]: td Out[184]: 0 3 days 1 3 days 2 3 days dtype: timedelta64[ns] In [185]: td + pd.offsets.Minute(15) Out[185]: 0 3 days 00:15:00 1 3 days 00:15:00 2 3 days 00:15:00 dtype: timedelta64[ns] Note that some offsets (such as BQuarterEnd) do not have a vectorized implementation. They can still be used but may calculate significantly slower and will show a PerformanceWarning In [186]: rng + pd.offsets.BQuarterEnd() Out[186]: DatetimeIndex(['2012-03-30', '2012-03-30', '2012-03-30'], dtype='datetime64[ns]', freq=None) Custom business days# The CDay or CustomBusinessDay class provides a parametric BusinessDay class which can be used to create customized business day calendars which account for local holidays and local weekend conventions. As an interesting example, let’s look at Egypt where a Friday-Saturday weekend is observed. In [187]: weekmask_egypt = "Sun Mon Tue Wed Thu" # They also observe International Workers' Day so let's # add that for a couple of years In [188]: holidays = [ .....: "2012-05-01", .....: datetime.datetime(2013, 5, 1), .....: np.datetime64("2014-05-01"), .....: ] .....: In [189]: bday_egypt = pd.offsets.CustomBusinessDay( .....: holidays=holidays, .....: weekmask=weekmask_egypt, .....: ) .....: In [190]: dt = datetime.datetime(2013, 4, 30) In [191]: dt + 2 * bday_egypt Out[191]: Timestamp('2013-05-05 00:00:00') Let’s map to the weekday names: In [192]: dts = pd.date_range(dt, periods=5, freq=bday_egypt) In [193]: pd.Series(dts.weekday, dts).map(pd.Series("Mon Tue Wed Thu Fri Sat Sun".split())) Out[193]: 2013-04-30 Tue 2013-05-02 Thu 2013-05-05 Sun 2013-05-06 Mon 2013-05-07 Tue Freq: C, dtype: object Holiday calendars can be used to provide the list of holidays. See the holiday calendar section for more information. In [194]: from pandas.tseries.holiday import USFederalHolidayCalendar In [195]: bday_us = pd.offsets.CustomBusinessDay(calendar=USFederalHolidayCalendar()) # Friday before MLK Day In [196]: dt = datetime.datetime(2014, 1, 17) # Tuesday after MLK Day (Monday is skipped because it's a holiday) In [197]: dt + bday_us Out[197]: Timestamp('2014-01-21 00:00:00') Monthly offsets that respect a certain holiday calendar can be defined in the usual way. In [198]: bmth_us = pd.offsets.CustomBusinessMonthBegin(calendar=USFederalHolidayCalendar()) # Skip new years In [199]: dt = datetime.datetime(2013, 12, 17) In [200]: dt + bmth_us Out[200]: Timestamp('2014-01-02 00:00:00') # Define date index with custom offset In [201]: pd.date_range(start="20100101", end="20120101", freq=bmth_us) Out[201]: DatetimeIndex(['2010-01-04', '2010-02-01', '2010-03-01', '2010-04-01', '2010-05-03', '2010-06-01', '2010-07-01', '2010-08-02', '2010-09-01', '2010-10-01', '2010-11-01', '2010-12-01', '2011-01-03', '2011-02-01', '2011-03-01', '2011-04-01', '2011-05-02', '2011-06-01', '2011-07-01', '2011-08-01', '2011-09-01', '2011-10-03', '2011-11-01', '2011-12-01'], dtype='datetime64[ns]', freq='CBMS') Note The frequency string ‘C’ is used to indicate that a CustomBusinessDay DateOffset is used, it is important to note that since CustomBusinessDay is a parameterised type, instances of CustomBusinessDay may differ and this is not detectable from the ‘C’ frequency string. The user therefore needs to ensure that the ‘C’ frequency string is used consistently within the user’s application. Business hour# The BusinessHour class provides a business hour representation on BusinessDay, allowing to use specific start and end times. By default, BusinessHour uses 9:00 - 17:00 as business hours. Adding BusinessHour will increment Timestamp by hourly frequency. If target Timestamp is out of business hours, move to the next business hour then increment it. If the result exceeds the business hours end, the remaining hours are added to the next business day. In [202]: bh = pd.offsets.BusinessHour() In [203]: bh Out[203]: <BusinessHour: BH=09:00-17:00> # 2014-08-01 is Friday In [204]: pd.Timestamp("2014-08-01 10:00").weekday() Out[204]: 4 In [205]: pd.Timestamp("2014-08-01 10:00") + bh Out[205]: Timestamp('2014-08-01 11:00:00') # Below example is the same as: pd.Timestamp('2014-08-01 09:00') + bh In [206]: pd.Timestamp("2014-08-01 08:00") + bh Out[206]: Timestamp('2014-08-01 10:00:00') # If the results is on the end time, move to the next business day In [207]: pd.Timestamp("2014-08-01 16:00") + bh Out[207]: Timestamp('2014-08-04 09:00:00') # Remainings are added to the next day In [208]: pd.Timestamp("2014-08-01 16:30") + bh Out[208]: Timestamp('2014-08-04 09:30:00') # Adding 2 business hours In [209]: pd.Timestamp("2014-08-01 10:00") + pd.offsets.BusinessHour(2) Out[209]: Timestamp('2014-08-01 12:00:00') # Subtracting 3 business hours In [210]: pd.Timestamp("2014-08-01 10:00") + pd.offsets.BusinessHour(-3) Out[210]: Timestamp('2014-07-31 15:00:00') You can also specify start and end time by keywords. The argument must be a str with an hour:minute representation or a datetime.time instance. Specifying seconds, microseconds and nanoseconds as business hour results in ValueError. In [211]: bh = pd.offsets.BusinessHour(start="11:00", end=datetime.time(20, 0)) In [212]: bh Out[212]: <BusinessHour: BH=11:00-20:00> In [213]: pd.Timestamp("2014-08-01 13:00") + bh Out[213]: Timestamp('2014-08-01 14:00:00') In [214]: pd.Timestamp("2014-08-01 09:00") + bh Out[214]: Timestamp('2014-08-01 12:00:00') In [215]: pd.Timestamp("2014-08-01 18:00") + bh Out[215]: Timestamp('2014-08-01 19:00:00') Passing start time later than end represents midnight business hour. In this case, business hour exceeds midnight and overlap to the next day. Valid business hours are distinguished by whether it started from valid BusinessDay. In [216]: bh = pd.offsets.BusinessHour(start="17:00", end="09:00") In [217]: bh Out[217]: <BusinessHour: BH=17:00-09:00> In [218]: pd.Timestamp("2014-08-01 17:00") + bh Out[218]: Timestamp('2014-08-01 18:00:00') In [219]: pd.Timestamp("2014-08-01 23:00") + bh Out[219]: Timestamp('2014-08-02 00:00:00') # Although 2014-08-02 is Saturday, # it is valid because it starts from 08-01 (Friday). In [220]: pd.Timestamp("2014-08-02 04:00") + bh Out[220]: Timestamp('2014-08-02 05:00:00') # Although 2014-08-04 is Monday, # it is out of business hours because it starts from 08-03 (Sunday). In [221]: pd.Timestamp("2014-08-04 04:00") + bh Out[221]: Timestamp('2014-08-04 18:00:00') Applying BusinessHour.rollforward and rollback to out of business hours results in the next business hour start or previous day’s end. Different from other offsets, BusinessHour.rollforward may output different results from apply by definition. This is because one day’s business hour end is equal to next day’s business hour start. For example, under the default business hours (9:00 - 17:00), there is no gap (0 minutes) between 2014-08-01 17:00 and 2014-08-04 09:00. # This adjusts a Timestamp to business hour edge In [222]: pd.offsets.BusinessHour().rollback(pd.Timestamp("2014-08-02 15:00")) Out[222]: Timestamp('2014-08-01 17:00:00') In [223]: pd.offsets.BusinessHour().rollforward(pd.Timestamp("2014-08-02 15:00")) Out[223]: Timestamp('2014-08-04 09:00:00') # It is the same as BusinessHour() + pd.Timestamp('2014-08-01 17:00'). # And it is the same as BusinessHour() + pd.Timestamp('2014-08-04 09:00') In [224]: pd.offsets.BusinessHour() + pd.Timestamp("2014-08-02 15:00") Out[224]: Timestamp('2014-08-04 10:00:00') # BusinessDay results (for reference) In [225]: pd.offsets.BusinessHour().rollforward(pd.Timestamp("2014-08-02")) Out[225]: Timestamp('2014-08-04 09:00:00') # It is the same as BusinessDay() + pd.Timestamp('2014-08-01') # The result is the same as rollworward because BusinessDay never overlap. In [226]: pd.offsets.BusinessHour() + pd.Timestamp("2014-08-02") Out[226]: Timestamp('2014-08-04 10:00:00') BusinessHour regards Saturday and Sunday as holidays. To use arbitrary holidays, you can use CustomBusinessHour offset, as explained in the following subsection. Custom business hour# The CustomBusinessHour is a mixture of BusinessHour and CustomBusinessDay which allows you to specify arbitrary holidays. CustomBusinessHour works as the same as BusinessHour except that it skips specified custom holidays. In [227]: from pandas.tseries.holiday import USFederalHolidayCalendar In [228]: bhour_us = pd.offsets.CustomBusinessHour(calendar=USFederalHolidayCalendar()) # Friday before MLK Day In [229]: dt = datetime.datetime(2014, 1, 17, 15) In [230]: dt + bhour_us Out[230]: Timestamp('2014-01-17 16:00:00') # Tuesday after MLK Day (Monday is skipped because it's a holiday) In [231]: dt + bhour_us * 2 Out[231]: Timestamp('2014-01-21 09:00:00') You can use keyword arguments supported by either BusinessHour and CustomBusinessDay. In [232]: bhour_mon = pd.offsets.CustomBusinessHour(start="10:00", weekmask="Tue Wed Thu Fri") # Monday is skipped because it's a holiday, business hour starts from 10:00 In [233]: dt + bhour_mon * 2 Out[233]: Timestamp('2014-01-21 10:00:00') Offset aliases# A number of string aliases are given to useful common time series frequencies. We will refer to these aliases as offset aliases. Alias Description B business day frequency C custom business day frequency D calendar day frequency W weekly frequency M month end frequency SM semi-month end frequency (15th and end of month) BM business month end frequency CBM custom business month end frequency MS month start frequency SMS semi-month start frequency (1st and 15th) BMS business month start frequency CBMS custom business month start frequency Q quarter end frequency BQ business quarter end frequency QS quarter start frequency BQS business quarter start frequency A, Y year end frequency BA, BY business year end frequency AS, YS year start frequency BAS, BYS business year start frequency BH business hour frequency H hourly frequency T, min minutely frequency S secondly frequency L, ms milliseconds U, us microseconds N nanoseconds Note When using the offset aliases above, it should be noted that functions such as date_range(), bdate_range(), will only return timestamps that are in the interval defined by start_date and end_date. If the start_date does not correspond to the frequency, the returned timestamps will start at the next valid timestamp, same for end_date, the returned timestamps will stop at the previous valid timestamp. For example, for the offset MS, if the start_date is not the first of the month, the returned timestamps will start with the first day of the next month. If end_date is not the first day of a month, the last returned timestamp will be the first day of the corresponding month. In [234]: dates_lst_1 = pd.date_range("2020-01-06", "2020-04-03", freq="MS") In [235]: dates_lst_1 Out[235]: DatetimeIndex(['2020-02-01', '2020-03-01', '2020-04-01'], dtype='datetime64[ns]', freq='MS') In [236]: dates_lst_2 = pd.date_range("2020-01-01", "2020-04-01", freq="MS") In [237]: dates_lst_2 Out[237]: DatetimeIndex(['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01'], dtype='datetime64[ns]', freq='MS') We can see in the above example date_range() and bdate_range() will only return the valid timestamps between the start_date and end_date. If these are not valid timestamps for the given frequency it will roll to the next value for start_date (respectively previous for the end_date) Combining aliases# As we have seen previously, the alias and the offset instance are fungible in most functions: In [238]: pd.date_range(start, periods=5, freq="B") Out[238]: DatetimeIndex(['2011-01-03', '2011-01-04', '2011-01-05', '2011-01-06', '2011-01-07'], dtype='datetime64[ns]', freq='B') In [239]: pd.date_range(start, periods=5, freq=pd.offsets.BDay()) Out[239]: DatetimeIndex(['2011-01-03', '2011-01-04', '2011-01-05', '2011-01-06', '2011-01-07'], dtype='datetime64[ns]', freq='B') You can combine together day and intraday offsets: In [240]: pd.date_range(start, periods=10, freq="2h20min") Out[240]: DatetimeIndex(['2011-01-01 00:00:00', '2011-01-01 02:20:00', '2011-01-01 04:40:00', '2011-01-01 07:00:00', '2011-01-01 09:20:00', '2011-01-01 11:40:00', '2011-01-01 14:00:00', '2011-01-01 16:20:00', '2011-01-01 18:40:00', '2011-01-01 21:00:00'], dtype='datetime64[ns]', freq='140T') In [241]: pd.date_range(start, periods=10, freq="1D10U") Out[241]: DatetimeIndex([ '2011-01-01 00:00:00', '2011-01-02 00:00:00.000010', '2011-01-03 00:00:00.000020', '2011-01-04 00:00:00.000030', '2011-01-05 00:00:00.000040', '2011-01-06 00:00:00.000050', '2011-01-07 00:00:00.000060', '2011-01-08 00:00:00.000070', '2011-01-09 00:00:00.000080', '2011-01-10 00:00:00.000090'], dtype='datetime64[ns]', freq='86400000010U') Anchored offsets# For some frequencies you can specify an anchoring suffix: Alias Description W-SUN weekly frequency (Sundays). Same as ‘W’ W-MON weekly frequency (Mondays) W-TUE weekly frequency (Tuesdays) W-WED weekly frequency (Wednesdays) W-THU weekly frequency (Thursdays) W-FRI weekly frequency (Fridays) W-SAT weekly frequency (Saturdays) (B)Q(S)-DEC quarterly frequency, year ends in December. Same as ‘Q’ (B)Q(S)-JAN quarterly frequency, year ends in January (B)Q(S)-FEB quarterly frequency, year ends in February (B)Q(S)-MAR quarterly frequency, year ends in March (B)Q(S)-APR quarterly frequency, year ends in April (B)Q(S)-MAY quarterly frequency, year ends in May (B)Q(S)-JUN quarterly frequency, year ends in June (B)Q(S)-JUL quarterly frequency, year ends in July (B)Q(S)-AUG quarterly frequency, year ends in August (B)Q(S)-SEP quarterly frequency, year ends in September (B)Q(S)-OCT quarterly frequency, year ends in October (B)Q(S)-NOV quarterly frequency, year ends in November (B)A(S)-DEC annual frequency, anchored end of December. Same as ‘A’ (B)A(S)-JAN annual frequency, anchored end of January (B)A(S)-FEB annual frequency, anchored end of February (B)A(S)-MAR annual frequency, anchored end of March (B)A(S)-APR annual frequency, anchored end of April (B)A(S)-MAY annual frequency, anchored end of May (B)A(S)-JUN annual frequency, anchored end of June (B)A(S)-JUL annual frequency, anchored end of July (B)A(S)-AUG annual frequency, anchored end of August (B)A(S)-SEP annual frequency, anchored end of September (B)A(S)-OCT annual frequency, anchored end of October (B)A(S)-NOV annual frequency, anchored end of November These can be used as arguments to date_range, bdate_range, constructors for DatetimeIndex, as well as various other timeseries-related functions in pandas. Anchored offset semantics# For those offsets that are anchored to the start or end of specific frequency (MonthEnd, MonthBegin, WeekEnd, etc), the following rules apply to rolling forward and backwards. When n is not 0, if the given date is not on an anchor point, it snapped to the next(previous) anchor point, and moved |n|-1 additional steps forwards or backwards. In [242]: pd.Timestamp("2014-01-02") + pd.offsets.MonthBegin(n=1) Out[242]: Timestamp('2014-02-01 00:00:00') In [243]: pd.Timestamp("2014-01-02") + pd.offsets.MonthEnd(n=1) Out[243]: Timestamp('2014-01-31 00:00:00') In [244]: pd.Timestamp("2014-01-02") - pd.offsets.MonthBegin(n=1) Out[244]: Timestamp('2014-01-01 00:00:00') In [245]: pd.Timestamp("2014-01-02") - pd.offsets.MonthEnd(n=1) Out[245]: Timestamp('2013-12-31 00:00:00') In [246]: pd.Timestamp("2014-01-02") + pd.offsets.MonthBegin(n=4) Out[246]: Timestamp('2014-05-01 00:00:00') In [247]: pd.Timestamp("2014-01-02") - pd.offsets.MonthBegin(n=4) Out[247]: Timestamp('2013-10-01 00:00:00') If the given date is on an anchor point, it is moved |n| points forwards or backwards. In [248]: pd.Timestamp("2014-01-01") + pd.offsets.MonthBegin(n=1) Out[248]: Timestamp('2014-02-01 00:00:00') In [249]: pd.Timestamp("2014-01-31") + pd.offsets.MonthEnd(n=1) Out[249]: Timestamp('2014-02-28 00:00:00') In [250]: pd.Timestamp("2014-01-01") - pd.offsets.MonthBegin(n=1) Out[250]: Timestamp('2013-12-01 00:00:00') In [251]: pd.Timestamp("2014-01-31") - pd.offsets.MonthEnd(n=1) Out[251]: Timestamp('2013-12-31 00:00:00') In [252]: pd.Timestamp("2014-01-01") + pd.offsets.MonthBegin(n=4) Out[252]: Timestamp('2014-05-01 00:00:00') In [253]: pd.Timestamp("2014-01-31") - pd.offsets.MonthBegin(n=4) Out[253]: Timestamp('2013-10-01 00:00:00') For the case when n=0, the date is not moved if on an anchor point, otherwise it is rolled forward to the next anchor point. In [254]: pd.Timestamp("2014-01-02") + pd.offsets.MonthBegin(n=0) Out[254]: Timestamp('2014-02-01 00:00:00') In [255]: pd.Timestamp("2014-01-02") + pd.offsets.MonthEnd(n=0) Out[255]: Timestamp('2014-01-31 00:00:00') In [256]: pd.Timestamp("2014-01-01") + pd.offsets.MonthBegin(n=0) Out[256]: Timestamp('2014-01-01 00:00:00') In [257]: pd.Timestamp("2014-01-31") + pd.offsets.MonthEnd(n=0) Out[257]: Timestamp('2014-01-31 00:00:00') Holidays / holiday calendars# Holidays and calendars provide a simple way to define holiday rules to be used with CustomBusinessDay or in other analysis that requires a predefined set of holidays. The AbstractHolidayCalendar class provides all the necessary methods to return a list of holidays and only rules need to be defined in a specific holiday calendar class. Furthermore, the start_date and end_date class attributes determine over what date range holidays are generated. These should be overwritten on the AbstractHolidayCalendar class to have the range apply to all calendar subclasses. USFederalHolidayCalendar is the only calendar that exists and primarily serves as an example for developing other calendars. For holidays that occur on fixed dates (e.g., US Memorial Day or July 4th) an observance rule determines when that holiday is observed if it falls on a weekend or some other non-observed day. Defined observance rules are: Rule Description nearest_workday move Saturday to Friday and Sunday to Monday sunday_to_monday move Sunday to following Monday next_monday_or_tuesday move Saturday to Monday and Sunday/Monday to Tuesday previous_friday move Saturday and Sunday to previous Friday” next_monday move Saturday and Sunday to following Monday An example of how holidays and holiday calendars are defined: In [258]: from pandas.tseries.holiday import ( .....: Holiday, .....: USMemorialDay, .....: AbstractHolidayCalendar, .....: nearest_workday, .....: MO, .....: ) .....: In [259]: class ExampleCalendar(AbstractHolidayCalendar): .....: rules = [ .....: USMemorialDay, .....: Holiday("July 4th", month=7, day=4, observance=nearest_workday), .....: Holiday( .....: "Columbus Day", .....: month=10, .....: day=1, .....: offset=pd.DateOffset(weekday=MO(2)), .....: ), .....: ] .....: In [260]: cal = ExampleCalendar() In [261]: cal.holidays(datetime.datetime(2012, 1, 1), datetime.datetime(2012, 12, 31)) Out[261]: DatetimeIndex(['2012-05-28', '2012-07-04', '2012-10-08'], dtype='datetime64[ns]', freq=None) hint weekday=MO(2) is same as 2 * Week(weekday=2) Using this calendar, creating an index or doing offset arithmetic skips weekends and holidays (i.e., Memorial Day/July 4th). For example, the below defines a custom business day offset using the ExampleCalendar. Like any other offset, it can be used to create a DatetimeIndex or added to datetime or Timestamp objects. In [262]: pd.date_range( .....: start="7/1/2012", end="7/10/2012", freq=pd.offsets.CDay(calendar=cal) .....: ).to_pydatetime() .....: Out[262]: array([datetime.datetime(2012, 7, 2, 0, 0), datetime.datetime(2012, 7, 3, 0, 0), datetime.datetime(2012, 7, 5, 0, 0), datetime.datetime(2012, 7, 6, 0, 0), datetime.datetime(2012, 7, 9, 0, 0), datetime.datetime(2012, 7, 10, 0, 0)], dtype=object) In [263]: offset = pd.offsets.CustomBusinessDay(calendar=cal) In [264]: datetime.datetime(2012, 5, 25) + offset Out[264]: Timestamp('2012-05-29 00:00:00') In [265]: datetime.datetime(2012, 7, 3) + offset Out[265]: Timestamp('2012-07-05 00:00:00') In [266]: datetime.datetime(2012, 7, 3) + 2 * offset Out[266]: Timestamp('2012-07-06 00:00:00') In [267]: datetime.datetime(2012, 7, 6) + offset Out[267]: Timestamp('2012-07-09 00:00:00') Ranges are defined by the start_date and end_date class attributes of AbstractHolidayCalendar. The defaults are shown below. In [268]: AbstractHolidayCalendar.start_date Out[268]: Timestamp('1970-01-01 00:00:00') In [269]: AbstractHolidayCalendar.end_date Out[269]: Timestamp('2200-12-31 00:00:00') These dates can be overwritten by setting the attributes as datetime/Timestamp/string. In [270]: AbstractHolidayCalendar.start_date = datetime.datetime(2012, 1, 1) In [271]: AbstractHolidayCalendar.end_date = datetime.datetime(2012, 12, 31) In [272]: cal.holidays() Out[272]: DatetimeIndex(['2012-05-28', '2012-07-04', '2012-10-08'], dtype='datetime64[ns]', freq=None) Every calendar class is accessible by name using the get_calendar function which returns a holiday class instance. Any imported calendar class will automatically be available by this function. Also, HolidayCalendarFactory provides an easy interface to create calendars that are combinations of calendars or calendars with additional rules. In [273]: from pandas.tseries.holiday import get_calendar, HolidayCalendarFactory, USLaborDay In [274]: cal = get_calendar("ExampleCalendar") In [275]: cal.rules Out[275]: [Holiday: Memorial Day (month=5, day=31, offset=<DateOffset: weekday=MO(-1)>), Holiday: July 4th (month=7, day=4, observance=<function nearest_workday at 0x7f1e67138ee0>), Holiday: Columbus Day (month=10, day=1, offset=<DateOffset: weekday=MO(+2)>)] In [276]: new_cal = HolidayCalendarFactory("NewExampleCalendar", cal, USLaborDay) In [277]: new_cal.rules Out[277]: [Holiday: Labor Day (month=9, day=1, offset=<DateOffset: weekday=MO(+1)>), Holiday: Memorial Day (month=5, day=31, offset=<DateOffset: weekday=MO(-1)>), Holiday: July 4th (month=7, day=4, observance=<function nearest_workday at 0x7f1e67138ee0>), Holiday: Columbus Day (month=10, day=1, offset=<DateOffset: weekday=MO(+2)>)] Time Series-related instance methods# Shifting / lagging# One may want to shift or lag the values in a time series back and forward in time. The method for this is shift(), which is available on all of the pandas objects. In [278]: ts = pd.Series(range(len(rng)), index=rng) In [279]: ts = ts[:5] In [280]: ts.shift(1) Out[280]: 2012-01-01 NaN 2012-01-02 0.0 2012-01-03 1.0 Freq: D, dtype: float64 The shift method accepts an freq argument which can accept a DateOffset class or other timedelta-like object or also an offset alias. When freq is specified, shift method changes all the dates in the index rather than changing the alignment of the data and the index: In [281]: ts.shift(5, freq="D") Out[281]: 2012-01-06 0 2012-01-07 1 2012-01-08 2 Freq: D, dtype: int64 In [282]: ts.shift(5, freq=pd.offsets.BDay()) Out[282]: 2012-01-06 0 2012-01-09 1 2012-01-10 2 dtype: int64 In [283]: ts.shift(5, freq="BM") Out[283]: 2012-05-31 0 2012-05-31 1 2012-05-31 2 dtype: int64 Note that with when freq is specified, the leading entry is no longer NaN because the data is not being realigned. Frequency conversion# The primary function for changing frequencies is the asfreq() method. For a DatetimeIndex, this is basically just a thin, but convenient wrapper around reindex() which generates a date_range and calls reindex. In [284]: dr = pd.date_range("1/1/2010", periods=3, freq=3 * pd.offsets.BDay()) In [285]: ts = pd.Series(np.random.randn(3), index=dr) In [286]: ts Out[286]: 2010-01-01 1.494522 2010-01-06 -0.778425 2010-01-11 -0.253355 Freq: 3B, dtype: float64 In [287]: ts.asfreq(pd.offsets.BDay()) Out[287]: 2010-01-01 1.494522 2010-01-04 NaN 2010-01-05 NaN 2010-01-06 -0.778425 2010-01-07 NaN 2010-01-08 NaN 2010-01-11 -0.253355 Freq: B, dtype: float64 asfreq provides a further convenience so you can specify an interpolation method for any gaps that may appear after the frequency conversion. In [288]: ts.asfreq(pd.offsets.BDay(), method="pad") Out[288]: 2010-01-01 1.494522 2010-01-04 1.494522 2010-01-05 1.494522 2010-01-06 -0.778425 2010-01-07 -0.778425 2010-01-08 -0.778425 2010-01-11 -0.253355 Freq: B, dtype: float64 Filling forward / backward# Related to asfreq and reindex is fillna(), which is documented in the missing data section. Converting to Python datetimes# DatetimeIndex can be converted to an array of Python native datetime.datetime objects using the to_pydatetime method. Resampling# pandas has a simple, powerful, and efficient functionality for performing resampling operations during frequency conversion (e.g., converting secondly data into 5-minutely data). This is extremely common in, but not limited to, financial applications. resample() is a time-based groupby, followed by a reduction method on each of its groups. See some cookbook examples for some advanced strategies. The resample() method can be used directly from DataFrameGroupBy objects, see the groupby docs. Basics# In [289]: rng = pd.date_range("1/1/2012", periods=100, freq="S") In [290]: ts = pd.Series(np.random.randint(0, 500, len(rng)), index=rng) In [291]: ts.resample("5Min").sum() Out[291]: 2012-01-01 25103 Freq: 5T, dtype: int64 The resample function is very flexible and allows you to specify many different parameters to control the frequency conversion and resampling operation. Any function available via dispatching is available as a method of the returned object, including sum, mean, std, sem, max, min, median, first, last, ohlc: In [292]: ts.resample("5Min").mean() Out[292]: 2012-01-01 251.03 Freq: 5T, dtype: float64 In [293]: ts.resample("5Min").ohlc() Out[293]: open high low close 2012-01-01 308 460 9 205 In [294]: ts.resample("5Min").max() Out[294]: 2012-01-01 460 Freq: 5T, dtype: int64 For downsampling, closed can be set to ‘left’ or ‘right’ to specify which end of the interval is closed: In [295]: ts.resample("5Min", closed="right").mean() Out[295]: 2011-12-31 23:55:00 308.000000 2012-01-01 00:00:00 250.454545 Freq: 5T, dtype: float64 In [296]: ts.resample("5Min", closed="left").mean() Out[296]: 2012-01-01 251.03 Freq: 5T, dtype: float64 Parameters like label are used to manipulate the resulting labels. label specifies whether the result is labeled with the beginning or the end of the interval. In [297]: ts.resample("5Min").mean() # by default label='left' Out[297]: 2012-01-01 251.03 Freq: 5T, dtype: float64 In [298]: ts.resample("5Min", label="left").mean() Out[298]: 2012-01-01 251.03 Freq: 5T, dtype: float64 Warning The default values for label and closed is ‘left’ for all frequency offsets except for ‘M’, ‘A’, ‘Q’, ‘BM’, ‘BA’, ‘BQ’, and ‘W’ which all have a default of ‘right’. This might unintendedly lead to looking ahead, where the value for a later time is pulled back to a previous time as in the following example with the BusinessDay frequency: In [299]: s = pd.date_range("2000-01-01", "2000-01-05").to_series() In [300]: s.iloc[2] = pd.NaT In [301]: s.dt.day_name() Out[301]: 2000-01-01 Saturday 2000-01-02 Sunday 2000-01-03 NaN 2000-01-04 Tuesday 2000-01-05 Wednesday Freq: D, dtype: object # default: label='left', closed='left' In [302]: s.resample("B").last().dt.day_name() Out[302]: 1999-12-31 Sunday 2000-01-03 NaN 2000-01-04 Tuesday 2000-01-05 Wednesday Freq: B, dtype: object Notice how the value for Sunday got pulled back to the previous Friday. To get the behavior where the value for Sunday is pushed to Monday, use instead In [303]: s.resample("B", label="right", closed="right").last().dt.day_name() Out[303]: 2000-01-03 Sunday 2000-01-04 Tuesday 2000-01-05 Wednesday Freq: B, dtype: object The axis parameter can be set to 0 or 1 and allows you to resample the specified axis for a DataFrame. kind can be set to ‘timestamp’ or ‘period’ to convert the resulting index to/from timestamp and time span representations. By default resample retains the input representation. convention can be set to ‘start’ or ‘end’ when resampling period data (detail below). It specifies how low frequency periods are converted to higher frequency periods. Upsampling# For upsampling, you can specify a way to upsample and the limit parameter to interpolate over the gaps that are created: # from secondly to every 250 milliseconds In [304]: ts[:2].resample("250L").asfreq() Out[304]: 2012-01-01 00:00:00.000 308.0 2012-01-01 00:00:00.250 NaN 2012-01-01 00:00:00.500 NaN 2012-01-01 00:00:00.750 NaN 2012-01-01 00:00:01.000 204.0 Freq: 250L, dtype: float64 In [305]: ts[:2].resample("250L").ffill() Out[305]: 2012-01-01 00:00:00.000 308 2012-01-01 00:00:00.250 308 2012-01-01 00:00:00.500 308 2012-01-01 00:00:00.750 308 2012-01-01 00:00:01.000 204 Freq: 250L, dtype: int64 In [306]: ts[:2].resample("250L").ffill(limit=2) Out[306]: 2012-01-01 00:00:00.000 308.0 2012-01-01 00:00:00.250 308.0 2012-01-01 00:00:00.500 308.0 2012-01-01 00:00:00.750 NaN 2012-01-01 00:00:01.000 204.0 Freq: 250L, dtype: float64 Sparse resampling# Sparse timeseries are the ones where you have a lot fewer points relative to the amount of time you are looking to resample. Naively upsampling a sparse series can potentially generate lots of intermediate values. When you don’t want to use a method to fill these values, e.g. fill_method is None, then intermediate values will be filled with NaN. Since resample is a time-based groupby, the following is a method to efficiently resample only the groups that are not all NaN. In [307]: rng = pd.date_range("2014-1-1", periods=100, freq="D") + pd.Timedelta("1s") In [308]: ts = pd.Series(range(100), index=rng) If we want to resample to the full range of the series: In [309]: ts.resample("3T").sum() Out[309]: 2014-01-01 00:00:00 0 2014-01-01 00:03:00 0 2014-01-01 00:06:00 0 2014-01-01 00:09:00 0 2014-01-01 00:12:00 0 .. 2014-04-09 23:48:00 0 2014-04-09 23:51:00 0 2014-04-09 23:54:00 0 2014-04-09 23:57:00 0 2014-04-10 00:00:00 99 Freq: 3T, Length: 47521, dtype: int64 We can instead only resample those groups where we have points as follows: In [310]: from functools import partial In [311]: from pandas.tseries.frequencies import to_offset In [312]: def round(t, freq): .....: freq = to_offset(freq) .....: return pd.Timestamp((t.value // freq.delta.value) * freq.delta.value) .....: In [313]: ts.groupby(partial(round, freq="3T")).sum() Out[313]: 2014-01-01 0 2014-01-02 1 2014-01-03 2 2014-01-04 3 2014-01-05 4 .. 2014-04-06 95 2014-04-07 96 2014-04-08 97 2014-04-09 98 2014-04-10 99 Length: 100, dtype: int64 Aggregation# Similar to the aggregating API, groupby API, and the window API, a Resampler can be selectively resampled. Resampling a DataFrame, the default will be to act on all columns with the same function. In [314]: df = pd.DataFrame( .....: np.random.randn(1000, 3), .....: index=pd.date_range("1/1/2012", freq="S", periods=1000), .....: columns=["A", "B", "C"], .....: ) .....: In [315]: r = df.resample("3T") In [316]: r.mean() Out[316]: A B C 2012-01-01 00:00:00 -0.033823 -0.121514 -0.081447 2012-01-01 00:03:00 0.056909 0.146731 -0.024320 2012-01-01 00:06:00 -0.058837 0.047046 -0.052021 2012-01-01 00:09:00 0.063123 -0.026158 -0.066533 2012-01-01 00:12:00 0.186340 -0.003144 0.074752 2012-01-01 00:15:00 -0.085954 -0.016287 -0.050046 We can select a specific column or columns using standard getitem. In [317]: r["A"].mean() Out[317]: 2012-01-01 00:00:00 -0.033823 2012-01-01 00:03:00 0.056909 2012-01-01 00:06:00 -0.058837 2012-01-01 00:09:00 0.063123 2012-01-01 00:12:00 0.186340 2012-01-01 00:15:00 -0.085954 Freq: 3T, Name: A, dtype: float64 In [318]: r[["A", "B"]].mean() Out[318]: A B 2012-01-01 00:00:00 -0.033823 -0.121514 2012-01-01 00:03:00 0.056909 0.146731 2012-01-01 00:06:00 -0.058837 0.047046 2012-01-01 00:09:00 0.063123 -0.026158 2012-01-01 00:12:00 0.186340 -0.003144 2012-01-01 00:15:00 -0.085954 -0.016287 You can pass a list or dict of functions to do aggregation with, outputting a DataFrame: In [319]: r["A"].agg([np.sum, np.mean, np.std]) Out[319]: sum mean std 2012-01-01 00:00:00 -6.088060 -0.033823 1.043263 2012-01-01 00:03:00 10.243678 0.056909 1.058534 2012-01-01 00:06:00 -10.590584 -0.058837 0.949264 2012-01-01 00:09:00 11.362228 0.063123 1.028096 2012-01-01 00:12:00 33.541257 0.186340 0.884586 2012-01-01 00:15:00 -8.595393 -0.085954 1.035476 On a resampled DataFrame, you can pass a list of functions to apply to each column, which produces an aggregated result with a hierarchical index: In [320]: r.agg([np.sum, np.mean]) Out[320]: A ... C sum mean ... sum mean 2012-01-01 00:00:00 -6.088060 -0.033823 ... -14.660515 -0.081447 2012-01-01 00:03:00 10.243678 0.056909 ... -4.377642 -0.024320 2012-01-01 00:06:00 -10.590584 -0.058837 ... -9.363825 -0.052021 2012-01-01 00:09:00 11.362228 0.063123 ... -11.975895 -0.066533 2012-01-01 00:12:00 33.541257 0.186340 ... 13.455299 0.074752 2012-01-01 00:15:00 -8.595393 -0.085954 ... -5.004580 -0.050046 [6 rows x 6 columns] By passing a dict to aggregate you can apply a different aggregation to the columns of a DataFrame: In [321]: r.agg({"A": np.sum, "B": lambda x: np.std(x, ddof=1)}) Out[321]: A B 2012-01-01 00:00:00 -6.088060 1.001294 2012-01-01 00:03:00 10.243678 1.074597 2012-01-01 00:06:00 -10.590584 0.987309 2012-01-01 00:09:00 11.362228 0.944953 2012-01-01 00:12:00 33.541257 1.095025 2012-01-01 00:15:00 -8.595393 1.035312 The function names can also be strings. In order for a string to be valid it must be implemented on the resampled object: In [322]: r.agg({"A": "sum", "B": "std"}) Out[322]: A B 2012-01-01 00:00:00 -6.088060 1.001294 2012-01-01 00:03:00 10.243678 1.074597 2012-01-01 00:06:00 -10.590584 0.987309 2012-01-01 00:09:00 11.362228 0.944953 2012-01-01 00:12:00 33.541257 1.095025 2012-01-01 00:15:00 -8.595393 1.035312 Furthermore, you can also specify multiple aggregation functions for each column separately. In [323]: r.agg({"A": ["sum", "std"], "B": ["mean", "std"]}) Out[323]: A B sum std mean std 2012-01-01 00:00:00 -6.088060 1.043263 -0.121514 1.001294 2012-01-01 00:03:00 10.243678 1.058534 0.146731 1.074597 2012-01-01 00:06:00 -10.590584 0.949264 0.047046 0.987309 2012-01-01 00:09:00 11.362228 1.028096 -0.026158 0.944953 2012-01-01 00:12:00 33.541257 0.884586 -0.003144 1.095025 2012-01-01 00:15:00 -8.595393 1.035476 -0.016287 1.035312 If a DataFrame does not have a datetimelike index, but instead you want to resample based on datetimelike column in the frame, it can passed to the on keyword. In [324]: df = pd.DataFrame( .....: {"date": pd.date_range("2015-01-01", freq="W", periods=5), "a": np.arange(5)}, .....: index=pd.MultiIndex.from_arrays( .....: [[1, 2, 3, 4, 5], pd.date_range("2015-01-01", freq="W", periods=5)], .....: names=["v", "d"], .....: ), .....: ) .....: In [325]: df Out[325]: date a v d 1 2015-01-04 2015-01-04 0 2 2015-01-11 2015-01-11 1 3 2015-01-18 2015-01-18 2 4 2015-01-25 2015-01-25 3 5 2015-02-01 2015-02-01 4 In [326]: df.resample("M", on="date")[["a"]].sum() Out[326]: a date 2015-01-31 6 2015-02-28 4 Similarly, if you instead want to resample by a datetimelike level of MultiIndex, its name or location can be passed to the level keyword. In [327]: df.resample("M", level="d")[["a"]].sum() Out[327]: a d 2015-01-31 6 2015-02-28 4 Iterating through groups# With the Resampler object in hand, iterating through the grouped data is very natural and functions similarly to itertools.groupby(): In [328]: small = pd.Series( .....: range(6), .....: index=pd.to_datetime( .....: [ .....: "2017-01-01T00:00:00", .....: "2017-01-01T00:30:00", .....: "2017-01-01T00:31:00", .....: "2017-01-01T01:00:00", .....: "2017-01-01T03:00:00", .....: "2017-01-01T03:05:00", .....: ] .....: ), .....: ) .....: In [329]: resampled = small.resample("H") In [330]: for name, group in resampled: .....: print("Group: ", name) .....: print("-" * 27) .....: print(group, end="\n\n") .....: Group: 2017-01-01 00:00:00 --------------------------- 2017-01-01 00:00:00 0 2017-01-01 00:30:00 1 2017-01-01 00:31:00 2 dtype: int64 Group: 2017-01-01 01:00:00 --------------------------- 2017-01-01 01:00:00 3 dtype: int64 Group: 2017-01-01 02:00:00 --------------------------- Series([], dtype: int64) Group: 2017-01-01 03:00:00 --------------------------- 2017-01-01 03:00:00 4 2017-01-01 03:05:00 5 dtype: int64 See Iterating through groups or Resampler.__iter__ for more. Use origin or offset to adjust the start of the bins# New in version 1.1.0. The bins of the grouping are adjusted based on the beginning of the day of the time series starting point. This works well with frequencies that are multiples of a day (like 30D) or that divide a day evenly (like 90s or 1min). This can create inconsistencies with some frequencies that do not meet this criteria. To change this behavior you can specify a fixed Timestamp with the argument origin. For example: In [331]: start, end = "2000-10-01 23:30:00", "2000-10-02 00:30:00" In [332]: middle = "2000-10-02 00:00:00" In [333]: rng = pd.date_range(start, end, freq="7min") In [334]: ts = pd.Series(np.arange(len(rng)) * 3, index=rng) In [335]: ts Out[335]: 2000-10-01 23:30:00 0 2000-10-01 23:37:00 3 2000-10-01 23:44:00 6 2000-10-01 23:51:00 9 2000-10-01 23:58:00 12 2000-10-02 00:05:00 15 2000-10-02 00:12:00 18 2000-10-02 00:19:00 21 2000-10-02 00:26:00 24 Freq: 7T, dtype: int64 Here we can see that, when using origin with its default value ('start_day'), the result after '2000-10-02 00:00:00' are not identical depending on the start of time series: In [336]: ts.resample("17min", origin="start_day").sum() Out[336]: 2000-10-01 23:14:00 0 2000-10-01 23:31:00 9 2000-10-01 23:48:00 21 2000-10-02 00:05:00 54 2000-10-02 00:22:00 24 Freq: 17T, dtype: int64 In [337]: ts[middle:end].resample("17min", origin="start_day").sum() Out[337]: 2000-10-02 00:00:00 33 2000-10-02 00:17:00 45 Freq: 17T, dtype: int64 Here we can see that, when setting origin to 'epoch', the result after '2000-10-02 00:00:00' are identical depending on the start of time series: In [338]: ts.resample("17min", origin="epoch").sum() Out[338]: 2000-10-01 23:18:00 0 2000-10-01 23:35:00 18 2000-10-01 23:52:00 27 2000-10-02 00:09:00 39 2000-10-02 00:26:00 24 Freq: 17T, dtype: int64 In [339]: ts[middle:end].resample("17min", origin="epoch").sum() Out[339]: 2000-10-01 23:52:00 15 2000-10-02 00:09:00 39 2000-10-02 00:26:00 24 Freq: 17T, dtype: int64 If needed you can use a custom timestamp for origin: In [340]: ts.resample("17min", origin="2001-01-01").sum() Out[340]: 2000-10-01 23:30:00 9 2000-10-01 23:47:00 21 2000-10-02 00:04:00 54 2000-10-02 00:21:00 24 Freq: 17T, dtype: int64 In [341]: ts[middle:end].resample("17min", origin=pd.Timestamp("2001-01-01")).sum() Out[341]: 2000-10-02 00:04:00 54 2000-10-02 00:21:00 24 Freq: 17T, dtype: int64 If needed you can just adjust the bins with an offset Timedelta that would be added to the default origin. Those two examples are equivalent for this time series: In [342]: ts.resample("17min", origin="start").sum() Out[342]: 2000-10-01 23:30:00 9 2000-10-01 23:47:00 21 2000-10-02 00:04:00 54 2000-10-02 00:21:00 24 Freq: 17T, dtype: int64 In [343]: ts.resample("17min", offset="23h30min").sum() Out[343]: 2000-10-01 23:30:00 9 2000-10-01 23:47:00 21 2000-10-02 00:04:00 54 2000-10-02 00:21:00 24 Freq: 17T, dtype: int64 Note the use of 'start' for origin on the last example. In that case, origin will be set to the first value of the timeseries. Backward resample# New in version 1.3.0. Instead of adjusting the beginning of bins, sometimes we need to fix the end of the bins to make a backward resample with a given freq. The backward resample sets closed to 'right' by default since the last value should be considered as the edge point for the last bin. We can set origin to 'end'. The value for a specific Timestamp index stands for the resample result from the current Timestamp minus freq to the current Timestamp with a right close. In [344]: ts.resample('17min', origin='end').sum() Out[344]: 2000-10-01 23:35:00 0 2000-10-01 23:52:00 18 2000-10-02 00:09:00 27 2000-10-02 00:26:00 63 Freq: 17T, dtype: int64 Besides, in contrast with the 'start_day' option, end_day is supported. This will set the origin as the ceiling midnight of the largest Timestamp. In [345]: ts.resample('17min', origin='end_day').sum() Out[345]: 2000-10-01 23:38:00 3 2000-10-01 23:55:00 15 2000-10-02 00:12:00 45 2000-10-02 00:29:00 45 Freq: 17T, dtype: int64 The above result uses 2000-10-02 00:29:00 as the last bin’s right edge since the following computation. In [346]: ceil_mid = rng.max().ceil('D') In [347]: freq = pd.offsets.Minute(17) In [348]: bin_res = ceil_mid - freq * ((ceil_mid - rng.max()) // freq) In [349]: bin_res Out[349]: Timestamp('2000-10-02 00:29:00') Time span representation# Regular intervals of time are represented by Period objects in pandas while sequences of Period objects are collected in a PeriodIndex, which can be created with the convenience function period_range. Period# A Period represents a span of time (e.g., a day, a month, a quarter, etc). You can specify the span via freq keyword using a frequency alias like below. Because freq represents a span of Period, it cannot be negative like “-3D”. In [350]: pd.Period("2012", freq="A-DEC") Out[350]: Period('2012', 'A-DEC') In [351]: pd.Period("2012-1-1", freq="D") Out[351]: Period('2012-01-01', 'D') In [352]: pd.Period("2012-1-1 19:00", freq="H") Out[352]: Period('2012-01-01 19:00', 'H') In [353]: pd.Period("2012-1-1 19:00", freq="5H") Out[353]: Period('2012-01-01 19:00', '5H') Adding and subtracting integers from periods shifts the period by its own frequency. Arithmetic is not allowed between Period with different freq (span). In [354]: p = pd.Period("2012", freq="A-DEC") In [355]: p + 1 Out[355]: Period('2013', 'A-DEC') In [356]: p - 3 Out[356]: Period('2009', 'A-DEC') In [357]: p = pd.Period("2012-01", freq="2M") In [358]: p + 2 Out[358]: Period('2012-05', '2M') In [359]: p - 1 Out[359]: Period('2011-11', '2M') In [360]: p == pd.Period("2012-01", freq="3M") Out[360]: False If Period freq is daily or higher (D, H, T, S, L, U, N), offsets and timedelta-like can be added if the result can have the same freq. Otherwise, ValueError will be raised. In [361]: p = pd.Period("2014-07-01 09:00", freq="H") In [362]: p + pd.offsets.Hour(2) Out[362]: Period('2014-07-01 11:00', 'H') In [363]: p + datetime.timedelta(minutes=120) Out[363]: Period('2014-07-01 11:00', 'H') In [364]: p + np.timedelta64(7200, "s") Out[364]: Period('2014-07-01 11:00', 'H') In [1]: p + pd.offsets.Minute(5) Traceback ... ValueError: Input has different freq from Period(freq=H) If Period has other frequencies, only the same offsets can be added. Otherwise, ValueError will be raised. In [365]: p = pd.Period("2014-07", freq="M") In [366]: p + pd.offsets.MonthEnd(3) Out[366]: Period('2014-10', 'M') In [1]: p + pd.offsets.MonthBegin(3) Traceback ... ValueError: Input has different freq from Period(freq=M) Taking the difference of Period instances with the same frequency will return the number of frequency units between them: In [367]: pd.Period("2012", freq="A-DEC") - pd.Period("2002", freq="A-DEC") Out[367]: <10 * YearEnds: month=12> PeriodIndex and period_range# Regular sequences of Period objects can be collected in a PeriodIndex, which can be constructed using the period_range convenience function: In [368]: prng = pd.period_range("1/1/2011", "1/1/2012", freq="M") In [369]: prng Out[369]: PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04', '2011-05', '2011-06', '2011-07', '2011-08', '2011-09', '2011-10', '2011-11', '2011-12', '2012-01'], dtype='period[M]') The PeriodIndex constructor can also be used directly: In [370]: pd.PeriodIndex(["2011-1", "2011-2", "2011-3"], freq="M") Out[370]: PeriodIndex(['2011-01', '2011-02', '2011-03'], dtype='period[M]') Passing multiplied frequency outputs a sequence of Period which has multiplied span. In [371]: pd.period_range(start="2014-01", freq="3M", periods=4) Out[371]: PeriodIndex(['2014-01', '2014-04', '2014-07', '2014-10'], dtype='period[3M]') If start or end are Period objects, they will be used as anchor endpoints for a PeriodIndex with frequency matching that of the PeriodIndex constructor. In [372]: pd.period_range( .....: start=pd.Period("2017Q1", freq="Q"), end=pd.Period("2017Q2", freq="Q"), freq="M" .....: ) .....: Out[372]: PeriodIndex(['2017-03', '2017-04', '2017-05', '2017-06'], dtype='period[M]') Just like DatetimeIndex, a PeriodIndex can also be used to index pandas objects: In [373]: ps = pd.Series(np.random.randn(len(prng)), prng) In [374]: ps Out[374]: 2011-01 -2.916901 2011-02 0.514474 2011-03 1.346470 2011-04 0.816397 2011-05 2.258648 2011-06 0.494789 2011-07 0.301239 2011-08 0.464776 2011-09 -1.393581 2011-10 0.056780 2011-11 0.197035 2011-12 2.261385 2012-01 -0.329583 Freq: M, dtype: float64 PeriodIndex supports addition and subtraction with the same rule as Period. In [375]: idx = pd.period_range("2014-07-01 09:00", periods=5, freq="H") In [376]: idx Out[376]: PeriodIndex(['2014-07-01 09:00', '2014-07-01 10:00', '2014-07-01 11:00', '2014-07-01 12:00', '2014-07-01 13:00'], dtype='period[H]') In [377]: idx + pd.offsets.Hour(2) Out[377]: PeriodIndex(['2014-07-01 11:00', '2014-07-01 12:00', '2014-07-01 13:00', '2014-07-01 14:00', '2014-07-01 15:00'], dtype='period[H]') In [378]: idx = pd.period_range("2014-07", periods=5, freq="M") In [379]: idx Out[379]: PeriodIndex(['2014-07', '2014-08', '2014-09', '2014-10', '2014-11'], dtype='period[M]') In [380]: idx + pd.offsets.MonthEnd(3) Out[380]: PeriodIndex(['2014-10', '2014-11', '2014-12', '2015-01', '2015-02'], dtype='period[M]') PeriodIndex has its own dtype named period, refer to Period Dtypes. Period dtypes# PeriodIndex has a custom period dtype. This is a pandas extension dtype similar to the timezone aware dtype (datetime64[ns, tz]). The period dtype holds the freq attribute and is represented with period[freq] like period[D] or period[M], using frequency strings. In [381]: pi = pd.period_range("2016-01-01", periods=3, freq="M") In [382]: pi Out[382]: PeriodIndex(['2016-01', '2016-02', '2016-03'], dtype='period[M]') In [383]: pi.dtype Out[383]: period[M] The period dtype can be used in .astype(...). It allows one to change the freq of a PeriodIndex like .asfreq() and convert a DatetimeIndex to PeriodIndex like to_period(): # change monthly freq to daily freq In [384]: pi.astype("period[D]") Out[384]: PeriodIndex(['2016-01-31', '2016-02-29', '2016-03-31'], dtype='period[D]') # convert to DatetimeIndex In [385]: pi.astype("datetime64[ns]") Out[385]: DatetimeIndex(['2016-01-01', '2016-02-01', '2016-03-01'], dtype='datetime64[ns]', freq='MS') # convert to PeriodIndex In [386]: dti = pd.date_range("2011-01-01", freq="M", periods=3) In [387]: dti Out[387]: DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31'], dtype='datetime64[ns]', freq='M') In [388]: dti.astype("period[M]") Out[388]: PeriodIndex(['2011-01', '2011-02', '2011-03'], dtype='period[M]') PeriodIndex partial string indexing# PeriodIndex now supports partial string slicing with non-monotonic indexes. New in version 1.1.0. You can pass in dates and strings to Series and DataFrame with PeriodIndex, in the same manner as DatetimeIndex. For details, refer to DatetimeIndex Partial String Indexing. In [389]: ps["2011-01"] Out[389]: -2.9169013294054507 In [390]: ps[datetime.datetime(2011, 12, 25):] Out[390]: 2011-12 2.261385 2012-01 -0.329583 Freq: M, dtype: float64 In [391]: ps["10/31/2011":"12/31/2011"] Out[391]: 2011-10 0.056780 2011-11 0.197035 2011-12 2.261385 Freq: M, dtype: float64 Passing a string representing a lower frequency than PeriodIndex returns partial sliced data. In [392]: ps["2011"] Out[392]: 2011-01 -2.916901 2011-02 0.514474 2011-03 1.346470 2011-04 0.816397 2011-05 2.258648 2011-06 0.494789 2011-07 0.301239 2011-08 0.464776 2011-09 -1.393581 2011-10 0.056780 2011-11 0.197035 2011-12 2.261385 Freq: M, dtype: float64 In [393]: dfp = pd.DataFrame( .....: np.random.randn(600, 1), .....: columns=["A"], .....: index=pd.period_range("2013-01-01 9:00", periods=600, freq="T"), .....: ) .....: In [394]: dfp Out[394]: A 2013-01-01 09:00 -0.538468 2013-01-01 09:01 -1.365819 2013-01-01 09:02 -0.969051 2013-01-01 09:03 -0.331152 2013-01-01 09:04 -0.245334 ... ... 2013-01-01 18:55 0.522460 2013-01-01 18:56 0.118710 2013-01-01 18:57 0.167517 2013-01-01 18:58 0.922883 2013-01-01 18:59 1.721104 [600 rows x 1 columns] In [395]: dfp.loc["2013-01-01 10H"] Out[395]: A 2013-01-01 10:00 -0.308975 2013-01-01 10:01 0.542520 2013-01-01 10:02 1.061068 2013-01-01 10:03 0.754005 2013-01-01 10:04 0.352933 ... ... 2013-01-01 10:55 -0.865621 2013-01-01 10:56 -1.167818 2013-01-01 10:57 -2.081748 2013-01-01 10:58 -0.527146 2013-01-01 10:59 0.802298 [60 rows x 1 columns] As with DatetimeIndex, the endpoints will be included in the result. The example below slices data starting from 10:00 to 11:59. In [396]: dfp["2013-01-01 10H":"2013-01-01 11H"] Out[396]: A 2013-01-01 10:00 -0.308975 2013-01-01 10:01 0.542520 2013-01-01 10:02 1.061068 2013-01-01 10:03 0.754005 2013-01-01 10:04 0.352933 ... ... 2013-01-01 11:55 -0.590204 2013-01-01 11:56 1.539990 2013-01-01 11:57 -1.224826 2013-01-01 11:58 0.578798 2013-01-01 11:59 -0.685496 [120 rows x 1 columns] Frequency conversion and resampling with PeriodIndex# The frequency of Period and PeriodIndex can be converted via the asfreq method. Let’s start with the fiscal year 2011, ending in December: In [397]: p = pd.Period("2011", freq="A-DEC") In [398]: p Out[398]: Period('2011', 'A-DEC') We can convert it to a monthly frequency. Using the how parameter, we can specify whether to return the starting or ending month: In [399]: p.asfreq("M", how="start") Out[399]: Period('2011-01', 'M') In [400]: p.asfreq("M", how="end") Out[400]: Period('2011-12', 'M') The shorthands ‘s’ and ‘e’ are provided for convenience: In [401]: p.asfreq("M", "s") Out[401]: Period('2011-01', 'M') In [402]: p.asfreq("M", "e") Out[402]: Period('2011-12', 'M') Converting to a “super-period” (e.g., annual frequency is a super-period of quarterly frequency) automatically returns the super-period that includes the input period: In [403]: p = pd.Period("2011-12", freq="M") In [404]: p.asfreq("A-NOV") Out[404]: Period('2012', 'A-NOV') Note that since we converted to an annual frequency that ends the year in November, the monthly period of December 2011 is actually in the 2012 A-NOV period. Period conversions with anchored frequencies are particularly useful for working with various quarterly data common to economics, business, and other fields. Many organizations define quarters relative to the month in which their fiscal year starts and ends. Thus, first quarter of 2011 could start in 2010 or a few months into 2011. Via anchored frequencies, pandas works for all quarterly frequencies Q-JAN through Q-DEC. Q-DEC define regular calendar quarters: In [405]: p = pd.Period("2012Q1", freq="Q-DEC") In [406]: p.asfreq("D", "s") Out[406]: Period('2012-01-01', 'D') In [407]: p.asfreq("D", "e") Out[407]: Period('2012-03-31', 'D') Q-MAR defines fiscal year end in March: In [408]: p = pd.Period("2011Q4", freq="Q-MAR") In [409]: p.asfreq("D", "s") Out[409]: Period('2011-01-01', 'D') In [410]: p.asfreq("D", "e") Out[410]: Period('2011-03-31', 'D') Converting between representations# Timestamped data can be converted to PeriodIndex-ed data using to_period and vice-versa using to_timestamp: In [411]: rng = pd.date_range("1/1/2012", periods=5, freq="M") In [412]: ts = pd.Series(np.random.randn(len(rng)), index=rng) In [413]: ts Out[413]: 2012-01-31 1.931253 2012-02-29 -0.184594 2012-03-31 0.249656 2012-04-30 -0.978151 2012-05-31 -0.873389 Freq: M, dtype: float64 In [414]: ps = ts.to_period() In [415]: ps Out[415]: 2012-01 1.931253 2012-02 -0.184594 2012-03 0.249656 2012-04 -0.978151 2012-05 -0.873389 Freq: M, dtype: float64 In [416]: ps.to_timestamp() Out[416]: 2012-01-01 1.931253 2012-02-01 -0.184594 2012-03-01 0.249656 2012-04-01 -0.978151 2012-05-01 -0.873389 Freq: MS, dtype: float64 Remember that ‘s’ and ‘e’ can be used to return the timestamps at the start or end of the period: In [417]: ps.to_timestamp("D", how="s") Out[417]: 2012-01-01 1.931253 2012-02-01 -0.184594 2012-03-01 0.249656 2012-04-01 -0.978151 2012-05-01 -0.873389 Freq: MS, dtype: float64 Converting between period and timestamp enables some convenient arithmetic functions to be used. In the following example, we convert a quarterly frequency with year ending in November to 9am of the end of the month following the quarter end: In [418]: prng = pd.period_range("1990Q1", "2000Q4", freq="Q-NOV") In [419]: ts = pd.Series(np.random.randn(len(prng)), prng) In [420]: ts.index = (prng.asfreq("M", "e") + 1).asfreq("H", "s") + 9 In [421]: ts.head() Out[421]: 1990-03-01 09:00 -0.109291 1990-06-01 09:00 -0.637235 1990-09-01 09:00 -1.735925 1990-12-01 09:00 2.096946 1991-03-01 09:00 -1.039926 Freq: H, dtype: float64 Representing out-of-bounds spans# If you have data that is outside of the Timestamp bounds, see Timestamp limitations, then you can use a PeriodIndex and/or Series of Periods to do computations. In [422]: span = pd.period_range("1215-01-01", "1381-01-01", freq="D") In [423]: span Out[423]: PeriodIndex(['1215-01-01', '1215-01-02', '1215-01-03', '1215-01-04', '1215-01-05', '1215-01-06', '1215-01-07', '1215-01-08', '1215-01-09', '1215-01-10', ... '1380-12-23', '1380-12-24', '1380-12-25', '1380-12-26', '1380-12-27', '1380-12-28', '1380-12-29', '1380-12-30', '1380-12-31', '1381-01-01'], dtype='period[D]', length=60632) To convert from an int64 based YYYYMMDD representation. In [424]: s = pd.Series([20121231, 20141130, 99991231]) In [425]: s Out[425]: 0 20121231 1 20141130 2 99991231 dtype: int64 In [426]: def conv(x): .....: return pd.Period(year=x // 10000, month=x // 100 % 100, day=x % 100, freq="D") .....: In [427]: s.apply(conv) Out[427]: 0 2012-12-31 1 2014-11-30 2 9999-12-31 dtype: period[D] In [428]: s.apply(conv)[2] Out[428]: Period('9999-12-31', 'D') These can easily be converted to a PeriodIndex: In [429]: span = pd.PeriodIndex(s.apply(conv)) In [430]: span Out[430]: PeriodIndex(['2012-12-31', '2014-11-30', '9999-12-31'], dtype='period[D]') Time zone handling# pandas provides rich support for working with timestamps in different time zones using the pytz and dateutil libraries or datetime.timezone objects from the standard library. Working with time zones# By default, pandas objects are time zone unaware: In [431]: rng = pd.date_range("3/6/2012 00:00", periods=15, freq="D") In [432]: rng.tz is None Out[432]: True To localize these dates to a time zone (assign a particular time zone to a naive date), you can use the tz_localize method or the tz keyword argument in date_range(), Timestamp, or DatetimeIndex. You can either pass pytz or dateutil time zone objects or Olson time zone database strings. Olson time zone strings will return pytz time zone objects by default. To return dateutil time zone objects, append dateutil/ before the string. In pytz you can find a list of common (and less common) time zones using from pytz import common_timezones, all_timezones. dateutil uses the OS time zones so there isn’t a fixed list available. For common zones, the names are the same as pytz. In [433]: import dateutil # pytz In [434]: rng_pytz = pd.date_range("3/6/2012 00:00", periods=3, freq="D", tz="Europe/London") In [435]: rng_pytz.tz Out[435]: <DstTzInfo 'Europe/London' LMT-1 day, 23:59:00 STD> # dateutil In [436]: rng_dateutil = pd.date_range("3/6/2012 00:00", periods=3, freq="D") In [437]: rng_dateutil = rng_dateutil.tz_localize("dateutil/Europe/London") In [438]: rng_dateutil.tz Out[438]: tzfile('/usr/share/zoneinfo/Europe/London') # dateutil - utc special case In [439]: rng_utc = pd.date_range( .....: "3/6/2012 00:00", .....: periods=3, .....: freq="D", .....: tz=dateutil.tz.tzutc(), .....: ) .....: In [440]: rng_utc.tz Out[440]: tzutc() New in version 0.25.0. # datetime.timezone In [441]: rng_utc = pd.date_range( .....: "3/6/2012 00:00", .....: periods=3, .....: freq="D", .....: tz=datetime.timezone.utc, .....: ) .....: In [442]: rng_utc.tz Out[442]: datetime.timezone.utc Note that the UTC time zone is a special case in dateutil and should be constructed explicitly as an instance of dateutil.tz.tzutc. You can also construct other time zones objects explicitly first. In [443]: import pytz # pytz In [444]: tz_pytz = pytz.timezone("Europe/London") In [445]: rng_pytz = pd.date_range("3/6/2012 00:00", periods=3, freq="D") In [446]: rng_pytz = rng_pytz.tz_localize(tz_pytz) In [447]: rng_pytz.tz == tz_pytz Out[447]: True # dateutil In [448]: tz_dateutil = dateutil.tz.gettz("Europe/London") In [449]: rng_dateutil = pd.date_range("3/6/2012 00:00", periods=3, freq="D", tz=tz_dateutil) In [450]: rng_dateutil.tz == tz_dateutil Out[450]: True To convert a time zone aware pandas object from one time zone to another, you can use the tz_convert method. In [451]: rng_pytz.tz_convert("US/Eastern") Out[451]: DatetimeIndex(['2012-03-05 19:00:00-05:00', '2012-03-06 19:00:00-05:00', '2012-03-07 19:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq=None) Note When using pytz time zones, DatetimeIndex will construct a different time zone object than a Timestamp for the same time zone input. A DatetimeIndex can hold a collection of Timestamp objects that may have different UTC offsets and cannot be succinctly represented by one pytz time zone instance while one Timestamp represents one point in time with a specific UTC offset. In [452]: dti = pd.date_range("2019-01-01", periods=3, freq="D", tz="US/Pacific") In [453]: dti.tz Out[453]: <DstTzInfo 'US/Pacific' LMT-1 day, 16:07:00 STD> In [454]: ts = pd.Timestamp("2019-01-01", tz="US/Pacific") In [455]: ts.tz Out[455]: <DstTzInfo 'US/Pacific' PST-1 day, 16:00:00 STD> Warning Be wary of conversions between libraries. For some time zones, pytz and dateutil have different definitions of the zone. This is more of a problem for unusual time zones than for ‘standard’ zones like US/Eastern. Warning Be aware that a time zone definition across versions of time zone libraries may not be considered equal. This may cause problems when working with stored data that is localized using one version and operated on with a different version. See here for how to handle such a situation. Warning For pytz time zones, it is incorrect to pass a time zone object directly into the datetime.datetime constructor (e.g., datetime.datetime(2011, 1, 1, tzinfo=pytz.timezone('US/Eastern')). Instead, the datetime needs to be localized using the localize method on the pytz time zone object. Warning Be aware that for times in the future, correct conversion between time zones (and UTC) cannot be guaranteed by any time zone library because a timezone’s offset from UTC may be changed by the respective government. Warning If you are using dates beyond 2038-01-18, due to current deficiencies in the underlying libraries caused by the year 2038 problem, daylight saving time (DST) adjustments to timezone aware dates will not be applied. If and when the underlying libraries are fixed, the DST transitions will be applied. For example, for two dates that are in British Summer Time (and so would normally be GMT+1), both the following asserts evaluate as true: In [456]: d_2037 = "2037-03-31T010101" In [457]: d_2038 = "2038-03-31T010101" In [458]: DST = "Europe/London" In [459]: assert pd.Timestamp(d_2037, tz=DST) != pd.Timestamp(d_2037, tz="GMT") In [460]: assert pd.Timestamp(d_2038, tz=DST) == pd.Timestamp(d_2038, tz="GMT") Under the hood, all timestamps are stored in UTC. Values from a time zone aware DatetimeIndex or Timestamp will have their fields (day, hour, minute, etc.) localized to the time zone. However, timestamps with the same UTC value are still considered to be equal even if they are in different time zones: In [461]: rng_eastern = rng_utc.tz_convert("US/Eastern") In [462]: rng_berlin = rng_utc.tz_convert("Europe/Berlin") In [463]: rng_eastern[2] Out[463]: Timestamp('2012-03-07 19:00:00-0500', tz='US/Eastern', freq='D') In [464]: rng_berlin[2] Out[464]: Timestamp('2012-03-08 01:00:00+0100', tz='Europe/Berlin', freq='D') In [465]: rng_eastern[2] == rng_berlin[2] Out[465]: True Operations between Series in different time zones will yield UTC Series, aligning the data on the UTC timestamps: In [466]: ts_utc = pd.Series(range(3), pd.date_range("20130101", periods=3, tz="UTC")) In [467]: eastern = ts_utc.tz_convert("US/Eastern") In [468]: berlin = ts_utc.tz_convert("Europe/Berlin") In [469]: result = eastern + berlin In [470]: result Out[470]: 2013-01-01 00:00:00+00:00 0 2013-01-02 00:00:00+00:00 2 2013-01-03 00:00:00+00:00 4 Freq: D, dtype: int64 In [471]: result.index Out[471]: DatetimeIndex(['2013-01-01 00:00:00+00:00', '2013-01-02 00:00:00+00:00', '2013-01-03 00:00:00+00:00'], dtype='datetime64[ns, UTC]', freq='D') To remove time zone information, use tz_localize(None) or tz_convert(None). tz_localize(None) will remove the time zone yielding the local time representation. tz_convert(None) will remove the time zone after converting to UTC time. In [472]: didx = pd.date_range(start="2014-08-01 09:00", freq="H", periods=3, tz="US/Eastern") In [473]: didx Out[473]: DatetimeIndex(['2014-08-01 09:00:00-04:00', '2014-08-01 10:00:00-04:00', '2014-08-01 11:00:00-04:00'], dtype='datetime64[ns, US/Eastern]', freq='H') In [474]: didx.tz_localize(None) Out[474]: DatetimeIndex(['2014-08-01 09:00:00', '2014-08-01 10:00:00', '2014-08-01 11:00:00'], dtype='datetime64[ns]', freq=None) In [475]: didx.tz_convert(None) Out[475]: DatetimeIndex(['2014-08-01 13:00:00', '2014-08-01 14:00:00', '2014-08-01 15:00:00'], dtype='datetime64[ns]', freq='H') # tz_convert(None) is identical to tz_convert('UTC').tz_localize(None) In [476]: didx.tz_convert("UTC").tz_localize(None) Out[476]: DatetimeIndex(['2014-08-01 13:00:00', '2014-08-01 14:00:00', '2014-08-01 15:00:00'], dtype='datetime64[ns]', freq=None) Fold# New in version 1.1.0. For ambiguous times, pandas supports explicitly specifying the keyword-only fold argument. Due to daylight saving time, one wall clock time can occur twice when shifting from summer to winter time; fold describes whether the datetime-like corresponds to the first (0) or the second time (1) the wall clock hits the ambiguous time. Fold is supported only for constructing from naive datetime.datetime (see datetime documentation for details) or from Timestamp or for constructing from components (see below). Only dateutil timezones are supported (see dateutil documentation for dateutil methods that deal with ambiguous datetimes) as pytz timezones do not support fold (see pytz documentation for details on how pytz deals with ambiguous datetimes). To localize an ambiguous datetime with pytz, please use Timestamp.tz_localize(). In general, we recommend to rely on Timestamp.tz_localize() when localizing ambiguous datetimes if you need direct control over how they are handled. In [477]: pd.Timestamp( .....: datetime.datetime(2019, 10, 27, 1, 30, 0, 0), .....: tz="dateutil/Europe/London", .....: fold=0, .....: ) .....: Out[477]: Timestamp('2019-10-27 01:30:00+0100', tz='dateutil//usr/share/zoneinfo/Europe/London') In [478]: pd.Timestamp( .....: year=2019, .....: month=10, .....: day=27, .....: hour=1, .....: minute=30, .....: tz="dateutil/Europe/London", .....: fold=1, .....: ) .....: Out[478]: Timestamp('2019-10-27 01:30:00+0000', tz='dateutil//usr/share/zoneinfo/Europe/London') Ambiguous times when localizing# tz_localize may not be able to determine the UTC offset of a timestamp because daylight savings time (DST) in a local time zone causes some times to occur twice within one day (“clocks fall back”). The following options are available: 'raise': Raises a pytz.AmbiguousTimeError (the default behavior) 'infer': Attempt to determine the correct offset base on the monotonicity of the timestamps 'NaT': Replaces ambiguous times with NaT bool: True represents a DST time, False represents non-DST time. An array-like of bool values is supported for a sequence of times. In [479]: rng_hourly = pd.DatetimeIndex( .....: ["11/06/2011 00:00", "11/06/2011 01:00", "11/06/2011 01:00", "11/06/2011 02:00"] .....: ) .....: This will fail as there are ambiguous times ('11/06/2011 01:00') In [2]: rng_hourly.tz_localize('US/Eastern') AmbiguousTimeError: Cannot infer dst time from Timestamp('2011-11-06 01:00:00'), try using the 'ambiguous' argument Handle these ambiguous times by specifying the following. In [480]: rng_hourly.tz_localize("US/Eastern", ambiguous="infer") Out[480]: DatetimeIndex(['2011-11-06 00:00:00-04:00', '2011-11-06 01:00:00-04:00', '2011-11-06 01:00:00-05:00', '2011-11-06 02:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq=None) In [481]: rng_hourly.tz_localize("US/Eastern", ambiguous="NaT") Out[481]: DatetimeIndex(['2011-11-06 00:00:00-04:00', 'NaT', 'NaT', '2011-11-06 02:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq=None) In [482]: rng_hourly.tz_localize("US/Eastern", ambiguous=[True, True, False, False]) Out[482]: DatetimeIndex(['2011-11-06 00:00:00-04:00', '2011-11-06 01:00:00-04:00', '2011-11-06 01:00:00-05:00', '2011-11-06 02:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq=None) Nonexistent times when localizing# A DST transition may also shift the local time ahead by 1 hour creating nonexistent local times (“clocks spring forward”). The behavior of localizing a timeseries with nonexistent times can be controlled by the nonexistent argument. The following options are available: 'raise': Raises a pytz.NonExistentTimeError (the default behavior) 'NaT': Replaces nonexistent times with NaT 'shift_forward': Shifts nonexistent times forward to the closest real time 'shift_backward': Shifts nonexistent times backward to the closest real time timedelta object: Shifts nonexistent times by the timedelta duration In [483]: dti = pd.date_range(start="2015-03-29 02:30:00", periods=3, freq="H") # 2:30 is a nonexistent time Localization of nonexistent times will raise an error by default. In [2]: dti.tz_localize('Europe/Warsaw') NonExistentTimeError: 2015-03-29 02:30:00 Transform nonexistent times to NaT or shift the times. In [484]: dti Out[484]: DatetimeIndex(['2015-03-29 02:30:00', '2015-03-29 03:30:00', '2015-03-29 04:30:00'], dtype='datetime64[ns]', freq='H') In [485]: dti.tz_localize("Europe/Warsaw", nonexistent="shift_forward") Out[485]: DatetimeIndex(['2015-03-29 03:00:00+02:00', '2015-03-29 03:30:00+02:00', '2015-03-29 04:30:00+02:00'], dtype='datetime64[ns, Europe/Warsaw]', freq=None) In [486]: dti.tz_localize("Europe/Warsaw", nonexistent="shift_backward") Out[486]: DatetimeIndex(['2015-03-29 01:59:59.999999999+01:00', '2015-03-29 03:30:00+02:00', '2015-03-29 04:30:00+02:00'], dtype='datetime64[ns, Europe/Warsaw]', freq=None) In [487]: dti.tz_localize("Europe/Warsaw", nonexistent=pd.Timedelta(1, unit="H")) Out[487]: DatetimeIndex(['2015-03-29 03:30:00+02:00', '2015-03-29 03:30:00+02:00', '2015-03-29 04:30:00+02:00'], dtype='datetime64[ns, Europe/Warsaw]', freq=None) In [488]: dti.tz_localize("Europe/Warsaw", nonexistent="NaT") Out[488]: DatetimeIndex(['NaT', '2015-03-29 03:30:00+02:00', '2015-03-29 04:30:00+02:00'], dtype='datetime64[ns, Europe/Warsaw]', freq=None) Time zone Series operations# A Series with time zone naive values is represented with a dtype of datetime64[ns]. In [489]: s_naive = pd.Series(pd.date_range("20130101", periods=3)) In [490]: s_naive Out[490]: 0 2013-01-01 1 2013-01-02 2 2013-01-03 dtype: datetime64[ns] A Series with a time zone aware values is represented with a dtype of datetime64[ns, tz] where tz is the time zone In [491]: s_aware = pd.Series(pd.date_range("20130101", periods=3, tz="US/Eastern")) In [492]: s_aware Out[492]: 0 2013-01-01 00:00:00-05:00 1 2013-01-02 00:00:00-05:00 2 2013-01-03 00:00:00-05:00 dtype: datetime64[ns, US/Eastern] Both of these Series time zone information can be manipulated via the .dt accessor, see the dt accessor section. For example, to localize and convert a naive stamp to time zone aware. In [493]: s_naive.dt.tz_localize("UTC").dt.tz_convert("US/Eastern") Out[493]: 0 2012-12-31 19:00:00-05:00 1 2013-01-01 19:00:00-05:00 2 2013-01-02 19:00:00-05:00 dtype: datetime64[ns, US/Eastern] Time zone information can also be manipulated using the astype method. This method can convert between different timezone-aware dtypes. # convert to a new time zone In [494]: s_aware.astype("datetime64[ns, CET]") Out[494]: 0 2013-01-01 06:00:00+01:00 1 2013-01-02 06:00:00+01:00 2 2013-01-03 06:00:00+01:00 dtype: datetime64[ns, CET] Note Using Series.to_numpy() on a Series, returns a NumPy array of the data. NumPy does not currently support time zones (even though it is printing in the local time zone!), therefore an object array of Timestamps is returned for time zone aware data: In [495]: s_naive.to_numpy() Out[495]: array(['2013-01-01T00:00:00.000000000', '2013-01-02T00:00:00.000000000', '2013-01-03T00:00:00.000000000'], dtype='datetime64[ns]') In [496]: s_aware.to_numpy() Out[496]: array([Timestamp('2013-01-01 00:00:00-0500', tz='US/Eastern'), Timestamp('2013-01-02 00:00:00-0500', tz='US/Eastern'), Timestamp('2013-01-03 00:00:00-0500', tz='US/Eastern')], dtype=object) By converting to an object array of Timestamps, it preserves the time zone information. For example, when converting back to a Series: In [497]: pd.Series(s_aware.to_numpy()) Out[497]: 0 2013-01-01 00:00:00-05:00 1 2013-01-02 00:00:00-05:00 2 2013-01-03 00:00:00-05:00 dtype: datetime64[ns, US/Eastern] However, if you want an actual NumPy datetime64[ns] array (with the values converted to UTC) instead of an array of objects, you can specify the dtype argument: In [498]: s_aware.to_numpy(dtype="datetime64[ns]") Out[498]: array(['2013-01-01T05:00:00.000000000', '2013-01-02T05:00:00.000000000', '2013-01-03T05:00:00.000000000'], dtype='datetime64[ns]')
1,084
1,295
Getting specific values from a range of time in Pandas DataFrame Hi. My problem is: Given a range of 1 minute in that DataFrame, ask and bid will change, sometimes no but sometimes more often. I need to get the first value, max, min and last and create a new dataframe of 1 minute interval. If there is a change in bid and ask, I will get the mean. Let me show what did i do: df['Date'] = pd.to_datetime(df['Date'], format='%Y.%m.%d', errors='coerce') df['Time'] = pd.to_datetime(df['Time'], format='%H:%M:%S.%f', errors='coerce') days = df['Date'].unique() hour = np.arange(0,24) minute = np.arange(0,60) open_list = [] for d in days: for h in hour: for m in minute: open = 0 l = len(df[(df['Date'] == d) & (df['Time'].dt.hour == h) & (df['Time'].dt.minute == m)][['Bid','Ask']]) if l != 0: open = df[(df['Date'] == d) & (df['Time'].dt.hour == h) & (df['Time'].dt.minute == m)][['Bid','Ask']].iloc[0].values else: continue if len(open) == 2: open_list.append((open.sum() / 2)) else: open_list.append(open) As you can see, it would take a life time for me. I would appreciate some help.
Since I haven't your data I can try to explain the method I'd use: Create a column with df["dt"] = pd.to_datetime(df['Date'] + ' ' + df['Time']) Then I'd use resample('1Min').agg(['mean','min', 'max', 'last'])
68,051,132
Error when combing two columns: ValueError: Columns must be same lenght as key
<p>I have a large dataframe and am working with pandas. The dataframe has many columns and I am trying to combine a few of them. In the example below I want to combine the year value of the calv1 and calv2 columns seperatly with the herd column and get an integer values -&gt; herd+calv1 = HC1 &amp; herd+calv2 = HC2</p> <pre><code> #datetime #datetime #int #New columns I am trying to create id calv1 calv2 herd HC1 HC2 1 2006-08-29 2007-08-29 1222 12222006 12222006 2 NaT NaT 1222 12220000 12220000 3 2006-08-29 NaT 1222 12222006 12220000 4 2006-08-29 2007-08-29 2334 13342006 13342007 5 2006-08-29 2013-08-29 1333 13332006 13342013 6 2006-08-29 NaT 1333 13332006 13340000 </code></pre> <p>This what I've tried:</p> <pre><code>df[['HC1','HC2']] =((df[['herd','herd'] ].apply( lambda s: s * 10000 )) + (df[['calv1','calv2'] ].apply( lambda x: x.dt.strftime('%Y').replace('NaT', '0').astype(int) )) ) </code></pre> <p>Multiply the herd by *10000 and adding the year.</p> <p>But when I try I get the error marked in the first line of the code:</p> <pre><code>ValueError: Columns must be same length as key </code></pre> <p>Can somebody help me see what going wrong? Am I not doing the apply and lambda correctly ? If I exclude the first part of the equation and only find the year it works but when I add the first part it doesn't.</p> <p>Also if somebody has an Idea how I could do this and leave blank values where there are no observations I would love to see it:</p> <pre><code> #datetime #datetime #int #New columns I am trying to create id calv1 calv2 herd HC1 HC2 1 2006-08-29 2007-08-29 1222 12222006 12222006 2 NaT NaT 1222 0 0 3 2006-08-29 NaT 1222 12222006 0 4 2006-08-29 2007-08-29 2334 13342006 13342007 5 2006-08-29 2013-08-29 1333 13332006 13342013 6 2006-08-29 NaT 1333 13332006 0 </code></pre>
68,051,172
"2021-06-19T22:11:23.553000"
1
null
0
60
python|pandas
<p>Let's try with <a href="https://pandas.pydata.org/docs/reference/api/pandas.Series.dt.year.html#pandas-series-dt-year" rel="nofollow noreferrer"><code>dt.year</code></a> and math operations followed by <a href="https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.fillna.html#pandas-dataframe-fillna" rel="nofollow noreferrer"><code>fillna</code></a> to replace <code>NaN</code> with <code>0</code>:</p> <pre><code>s = df['herd'] * 10000 df['HC1'] = s + df['calv1'].dt.year df['HC2'] = s + df['calv2'].dt.year df[['HC1', 'HC2']] = df[['HC1', 'HC2']].fillna(0, downcast='infer') </code></pre> <p><code>df</code>:</p> <pre class="lang-none prettyprint-override"><code> id calv1 calv2 herd HC1 HC2 0 1 2006-08-29 2007-08-29 1222 12222006 12222007 1 2 NaT NaT 1222 0 0 2 3 2006-08-29 NaT 1222 12222006 0 3 4 2006-08-29 2007-08-29 2334 23342006 23342007 4 5 2006-08-29 2013-08-29 1333 13332006 13332013 5 6 2006-08-29 NaT 1333 13332006 0 </code></pre> <p>For the first output with <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.fillna.html#pandas-series-fillna" rel="nofollow noreferrer"><code>Series.fillna</code></a> to fill <code>NaT</code> with <code>2000</code>:</p> <pre><code>s = df['herd'] * 10000 df['HC1'] = s + df['calv1'].dt.year.fillna(2000, downcast='infer') df['HC2'] = s + df['calv2'].dt.year.fillna(2000, downcast='infer') </code></pre> <p><code>df</code>:</p> <pre class="lang-none prettyprint-override"><code> id calv1 calv2 herd HC1 HC2 0 1 2006-08-29 2007-08-29 1222 12222006 12222007 1 2 NaT NaT 1222 12222000 12222000 2 3 2006-08-29 NaT 1222 12222006 12222000 3 4 2006-08-29 2007-08-29 2334 23342006 23342007 4 5 2006-08-29 2013-08-29 1333 13332006 13332013 5 6 2006-08-29 NaT 1333 13332006 13332000 </code></pre> <hr /> <p>DataFrame Constructor and Imports:</p> <pre><code>import pandas as pd df = pd.DataFrame({ 'id': [1, 2, 3, 4, 5, 6], 'calv1': ['2006-08-29', None, '2006-08-29', '2006-08-29', '2006-08-29', '2006-08-29'], 'calv2': ['2007-08-29', None, None, '2007-08-29', '2013-08-29', None], 'herd': [1222, 1222, 1222, 2334, 1333, 1333] }) df['calv1'] = pd.to_datetime(df['calv1']) df['calv2'] = pd.to_datetime(df['calv2']) </code></pre>
"2021-06-19T22:18:22.867000"
0
https://pandas.pydata.org/docs/dev/user_guide/merging.html
Let's try with dt.year and math operations followed by fillna to replace NaN with 0: s = df['herd'] * 10000 df['HC1'] = s + df['calv1'].dt.year df['HC2'] = s + df['calv2'].dt.year df[['HC1', 'HC2']] = df[['HC1', 'HC2']].fillna(0, downcast='infer') df: id calv1 calv2 herd HC1 HC2 0 1 2006-08-29 2007-08-29 1222 12222006 12222007 1 2 NaT NaT 1222 0 0 2 3 2006-08-29 NaT 1222 12222006 0 3 4 2006-08-29 2007-08-29 2334 23342006 23342007 4 5 2006-08-29 2013-08-29 1333 13332006 13332013 5 6 2006-08-29 NaT 1333 13332006 0 For the first output with Series.fillna to fill NaT with 2000: s = df['herd'] * 10000 df['HC1'] = s + df['calv1'].dt.year.fillna(2000, downcast='infer') df['HC2'] = s + df['calv2'].dt.year.fillna(2000, downcast='infer') df: id calv1 calv2 herd HC1 HC2 0 1 2006-08-29 2007-08-29 1222 12222006 12222007 1 2 NaT NaT 1222 12222000 12222000 2 3 2006-08-29 NaT 1222 12222006 12222000 3 4 2006-08-29 2007-08-29 2334 23342006 23342007 4 5 2006-08-29 2013-08-29 1333 13332006 13332013 5 6 2006-08-29 NaT 1333 13332006 13332000 DataFrame Constructor and Imports: import pandas as pd df = pd.DataFrame({ 'id': [1, 2, 3, 4, 5, 6], 'calv1': ['2006-08-29', None, '2006-08-29', '2006-08-29', '2006-08-29', '2006-08-29'], 'calv2': ['2007-08-29', None, None, '2007-08-29', '2013-08-29', None], 'herd': [1222, 1222, 1222, 2334, 1333, 1333] }) df['calv1'] = pd.to_datetime(df['calv1']) df['calv2'] = pd.to_datetime(df['calv2'])
0
1,658
Error when combing two columns: ValueError: Columns must be same lenght as key I have a large dataframe and am working with pandas. The dataframe has many columns and I am trying to combine a few of them. In the example below I want to combine the year value of the calv1 and calv2 columns seperatly with the herd column and get an integer values -> herd+calv1 = HC1 & herd+calv2 = HC2 #datetime #datetime #int #New columns I am trying to create id calv1 calv2 herd HC1 HC2 1 2006-08-29 2007-08-29 1222 12222006 12222006 2 NaT NaT 1222 12220000 12220000 3 2006-08-29 NaT 1222 12222006 12220000 4 2006-08-29 2007-08-29 2334 13342006 13342007 5 2006-08-29 2013-08-29 1333 13332006 13342013 6 2006-08-29 NaT 1333 13332006 13340000 This what I've tried: df[['HC1','HC2']] =((df[['herd','herd'] ].apply( lambda s: s * 10000 )) + (df[['calv1','calv2'] ].apply( lambda x: x.dt.strftime('%Y').replace('NaT', '0').astype(int) )) ) Multiply the herd by *10000 and adding the year. But when I try I get the error marked in the first line of the code: ValueError: Columns must be same length as key Can somebody help me see what going wrong? Am I not doing the apply and lambda correctly ? If I exclude the first part of the equation and only find the year it works but when I add the first part it doesn't. Also if somebody has an Idea how I could do this and leave blank values where there are no observations I would love to see it: #datetime #datetime #int #New columns I am trying to create id calv1 calv2 herd HC1 HC2 1 2006-08-29 2007-08-29 1222 12222006 12222006 2 NaT NaT 1222 0 0 3 2006-08-29 NaT 1222 12222006 0 4 2006-08-29 2007-08-29 2334 13342006 13342007 5 2006-08-29 2013-08-29 1333 13332006 13342013 6 2006-08-29 NaT 1333 13332006 0
Let's try with dt.year and math operations followed by fillna to replace NaN with 0: s = df['herd'] * 10000 df['HC1'] = s + df['calv1'].dt.year df['HC2'] = s + df['calv2'].dt.year df[['HC1', 'HC2']] = df[['HC1', 'HC2']].fillna(0, downcast='infer') df: id calv1 calv2 herd HC1 HC2 0 1 2006-08-29 2007-08-29 1222 12222006 12222007 1 2 NaT NaT 1222 0 0 2 3 2006-08-29 NaT 1222 12222006 0 3 4 2006-08-29 2007-08-29 2334 23342006 23342007 4 5 2006-08-29 2013-08-29 1333 13332006 13332013 5 6 2006-08-29 NaT 1333 13332006 0 For the first output with Series.fillna to fill NaT with 2000: s = df['herd'] * 10000 df['HC1'] = s + df['calv1'].dt.year.fillna(2000, downcast='infer') df['HC2'] = s + df['calv2'].dt.year.fillna(2000, downcast='infer') df: id calv1 calv2 herd HC1 HC2 0 1 2006-08-29 2007-08-29 1222 12222006 12222007 1 2 NaT NaT 1222 12222000 12222000 2 3 2006-08-29 NaT 1222 12222006 12222000 3 4 2006-08-29 2007-08-29 2334 23342006 23342007 4 5 2006-08-29 2013-08-29 1333 13332006 13332013 5 6 2006-08-29 NaT 1333 13332006 13332000 DataFrame Constructor and Imports: import pandas as pd df = pd.DataFrame({ 'id': [1, 2, 3, 4, 5, 6], 'calv1': ['2006-08-29', None, '2006-08-29', '2006-08-29', '2006-08-29', '2006-08-29'], 'calv2': ['2007-08-29', None, None, '2007-08-29', '2013-08-29', None], 'herd': [1222, 1222, 1222, 2334, 1333, 1333] }) df['calv1'] = pd.to_datetime(df['calv1']) df['calv2'] = pd.to_datetime(df['calv2'])
70,104,554
Merge Pandas Dataframes based on substring or partial match in another Dataframe
<p>I have two sample data frames:</p> <pre><code>df1 = pd.DataFrame({'Model': ['0RW52HC5KDD13R', '0RW52HC5KDD13U','JJS42HC5JSSAYR']}) df2 = pd.DataFrame({'Group_Var': ['0RW52HC5K', '0RW52HC5K','JJS42HC5J']}) </code></pre> <p>Using this will result result in an empty dataframe.</p> <pre><code>df3 = df1.merge(df2, left_on='Model', right_on='Group_Var') </code></pre> <p>How could I go about using a merge to use a substring / partial match from <code>df2['Group_Var']</code> in <code>df1['Model']</code>? Perhaps using the <code>str.contains()</code> method as part of the merge?</p> <p>Just for context my expected output would be something like this:</p> <pre><code>Group_Var Model 0RW52HC5K 0RW52HC5KDD13R 0RW52HC5K 0RW52HC5KDD13U JJS42HC5J JJS42HC5JSSAYR </code></pre>
70,104,588
"2021-11-25T00:34:13.173000"
1
null
-1
65
python|pandas
<p>Use <code>pd.concat</code> with <code>axis=1</code>:</p> <pre class="lang-py prettyprint-override"><code>df3 = pd.concat([df1, df2], axis=1) </code></pre> <p>Output:</p> <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; df3 Model Group_Var 0 0RW52HC5KDD13R 0RW52HC5K 1 0RW52HC5KDD13U 0RW52HC5K 2 JJS42HC5JSSAYR JJS42HC5J </code></pre>
"2021-11-25T00:41:00.657000"
0
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.isin.html
pandas.DataFrame.isin# pandas.DataFrame.isin# DataFrame.isin(values)[source]# Whether each element in the DataFrame is contained in values. Parameters valuesiterable, Series, DataFrame or dictThe result will only be true at a location if all the labels match. If values is a Series, that’s the index. If values is a dict, the keys must be the column names, which must match. If values is a DataFrame, then both the index and column labels must match. Returns DataFrameDataFrame of booleans showing whether each element in the DataFrame is contained in values. Use pd.concat with axis=1: df3 = pd.concat([df1, df2], axis=1) Output: >>> df3 Model Group_Var 0 0RW52HC5KDD13R 0RW52HC5K 1 0RW52HC5KDD13U 0RW52HC5K 2 JJS42HC5JSSAYR JJS42HC5J See also DataFrame.eqEquality test for DataFrame. Series.isinEquivalent method on Series. Series.str.containsTest if pattern or regex is contained within a string of a Series or Index. Examples >>> df = pd.DataFrame({'num_legs': [2, 4], 'num_wings': [2, 0]}, ... index=['falcon', 'dog']) >>> df num_legs num_wings falcon 2 2 dog 4 0 When values is a list check whether every value in the DataFrame is present in the list (which animals have 0 or 2 legs or wings) >>> df.isin([0, 2]) num_legs num_wings falcon True True dog False True To check if values is not in the DataFrame, use the ~ operator: >>> ~df.isin([0, 2]) num_legs num_wings falcon False False dog True False When values is a dict, we can pass values to check for each column separately: >>> df.isin({'num_wings': [0, 3]}) num_legs num_wings falcon False False dog False True When values is a Series or DataFrame the index and column must match. Note that ‘falcon’ does not match based on the number of legs in other. >>> other = pd.DataFrame({'num_legs': [8, 3], 'num_wings': [0, 2]}, ... index=['spider', 'falcon']) >>> df.isin(other) num_legs num_wings falcon False True dog False False
570
766
Merge Pandas Dataframes based on substring or partial match in another Dataframe I have two sample data frames: df1 = pd.DataFrame({'Model': ['0RW52HC5KDD13R', '0RW52HC5KDD13U','JJS42HC5JSSAYR']}) df2 = pd.DataFrame({'Group_Var': ['0RW52HC5K', '0RW52HC5K','JJS42HC5J']}) Using this will result result in an empty dataframe. df3 = df1.merge(df2, left_on='Model', right_on='Group_Var') How could I go about using a merge to use a substring / partial match from df2['Group_Var'] in df1['Model']? Perhaps using the str.contains() method as part of the merge? Just for context my expected output would be something like this: Group_Var Model 0RW52HC5K 0RW52HC5KDD13R 0RW52HC5K 0RW52HC5KDD13U JJS42HC5J JJS42HC5JSSAYR
Use pd.concat with axis=1: df3 = pd.concat([df1, df2], axis=1) Output: >>> df3 Model Group_Var 0 0RW52HC5KDD13R 0RW52HC5K 1 0RW52HC5KDD13U 0RW52HC5K 2 JJS42HC5JSSAYR JJS42HC5J
69,434,826
Read a file from a folder and extract a specific key from the file and save as in CSV file
<p>I'm new to Python and the task I am performing is to extract a specific key value from a list of <code>.iris</code> ( which contains the list of nested dictionary format) files in a specific directory.</p> <p>I wanted to extract the specific value and save it as a new <code>.csv</code> file and repeat it for all other files.</p> <p>Below is my sample of <code>.iris</code> file from which I should extract only for the these keys <code>('uid','enabled','login','name')</code>.</p> <pre class="lang-json prettyprint-override"><code>{&quot;streamType&quot;:&quot;user&quot;, &quot;uid&quot;:17182, &quot;enabled&quot;:true, &quot;login&quot;:&quot;xyz&quot;, &quot;name&quot;:&quot;abcdef&quot;, &quot;comment&quot;:&quot;&quot;, &quot;authSms&quot;:&quot;&quot;, &quot;email&quot;:&quot;&quot;, &quot;phone&quot;:&quot;&quot;, &quot;location&quot;:&quot;&quot;, &quot;extraLdapOu&quot;:&quot;&quot;, &quot;mand&quot;:997, &quot;global&quot;:{ &quot;userAccount&quot;:&quot;View&quot;, &quot;uid&quot;:&quot;&quot;, &quot;retention&quot;:&quot;No&quot;, &quot;enabled&quot;:&quot;&quot;, &quot;messages&quot;:&quot;Change&quot;}, &quot;grants&quot;:[{&quot;mand&quot;:997,&quot;role&quot;:1051,&quot;passOnToSubMand&quot;:true}], </code></pre> <p>I am trying to convert the <code>.iris</code> file to <code>.json</code> and reading the files one by, but unfortunately, I am not getting the exact output as desired.</p> <p>Please, could anyone help me?</p> <hr /> <p>My code <em>(added from comments)</em>:</p> <pre class="lang-py prettyprint-override"><code>import os import csv path = '' os.chdir(path) # Read iris File def read_iris_file(file_path): with open(file_path, 'r') as f: print(f.read()) # iterate through all files for file in os.listdir(): # Check whether file is in iris format or not if file.endswith(&quot;.iris&quot;): file_path = f&quot;{path}\{file}&quot; # call read iris file function print(read_iris_file(file_path)) </code></pre>
69,436,442
"2021-10-04T10:57:36.467000"
2
1
0
140
python|pandas
<p>Your files contain data in JSON format, so we can use built-in <a href="https://docs.python.org/3/library/json.html" rel="nofollow noreferrer"><code>json</code></a> module to parse it. To iterate over files with certain extension you can use <a href="https://docs.python.org/3/library/pathlib.html#pathlib.Path.glob" rel="nofollow noreferrer"><code>pathlib.glob()</code></a> with next pattern <code>&quot;*.iris&quot;</code>. Then we can use <a href="https://docs.python.org/3/library/csv.html#csv.DictWriter" rel="nofollow noreferrer"><code>csv.DictWriter()</code></a> and pass <code>&quot;ignore&quot;</code> to <code>extrasaction</code> argument which will make <code>DictWriter</code> ignore keys which we don't need and write only those which we passed to <code>fieldnames</code> argument.</p> <p><strong>Code:</strong></p> <pre class="lang-py prettyprint-override"><code>import csv import json from pathlib import Path path = Path(r&quot;path/to/folder&quot;) keys = &quot;uid&quot;, &quot;enabled&quot;, &quot;login&quot;, &quot;name&quot; with open(path / &quot;result.csv&quot;, &quot;w&quot;, newline=&quot;&quot;) as out_f: writer = csv.DictWriter(out_f, fieldnames=keys, extrasaction='ignore') writer.writeheader() for file in path.glob(&quot;*.iris&quot;): with open(file) as inp_f: data = json.load(inp_f) writer.writerow(data) </code></pre>
"2021-10-04T13:03:27.423000"
0
https://pandas.pydata.org/docs/user_guide/io.html
IO tools (text, CSV, HDF5, …)# IO tools (text, CSV, HDF5, …)# The pandas I/O API is a set of top level reader functions accessed like pandas.read_csv() that generally return a pandas object. The corresponding Your files contain data in JSON format, so we can use built-in json module to parse it. To iterate over files with certain extension you can use pathlib.glob() with next pattern "*.iris". Then we can use csv.DictWriter() and pass "ignore" to extrasaction argument which will make DictWriter ignore keys which we don't need and write only those which we passed to fieldnames argument. Code: import csv import json from pathlib import Path path = Path(r"path/to/folder") keys = "uid", "enabled", "login", "name" with open(path / "result.csv", "w", newline="") as out_f: writer = csv.DictWriter(out_f, fieldnames=keys, extrasaction='ignore') writer.writeheader() for file in path.glob("*.iris"): with open(file) as inp_f: data = json.load(inp_f) writer.writerow(data) writer functions are object methods that are accessed like DataFrame.to_csv(). Below is a table containing available readers and writers. Format Type Data Description Reader Writer text CSV read_csv to_csv text Fixed-Width Text File read_fwf text JSON read_json to_json text HTML read_html to_html text LaTeX Styler.to_latex text XML read_xml to_xml text Local clipboard read_clipboard to_clipboard binary MS Excel read_excel to_excel binary OpenDocument read_excel binary HDF5 Format read_hdf to_hdf binary Feather Format read_feather to_feather binary Parquet Format read_parquet to_parquet binary ORC Format read_orc to_orc binary Stata read_stata to_stata binary SAS read_sas binary SPSS read_spss binary Python Pickle Format read_pickle to_pickle SQL SQL read_sql to_sql SQL Google BigQuery read_gbq to_gbq Here is an informal performance comparison for some of these IO methods. Note For examples that use the StringIO class, make sure you import it with from io import StringIO for Python 3. CSV & text files# The workhorse function for reading text files (a.k.a. flat files) is read_csv(). See the cookbook for some advanced strategies. Parsing options# read_csv() accepts the following common arguments: Basic# filepath_or_buffervariousEither a path to a file (a str, pathlib.Path, or py:py._path.local.LocalPath), URL (including http, ftp, and S3 locations), or any object with a read() method (such as an open file or StringIO). sepstr, defaults to ',' for read_csv(), \t for read_table()Delimiter to use. If sep is None, the C engine cannot automatically detect the separator, but the Python parsing engine can, meaning the latter will be used and automatically detect the separator by Python’s builtin sniffer tool, csv.Sniffer. In addition, separators longer than 1 character and different from '\s+' will be interpreted as regular expressions and will also force the use of the Python parsing engine. Note that regex delimiters are prone to ignoring quoted data. Regex example: '\\r\\t'. delimiterstr, default NoneAlternative argument name for sep. delim_whitespaceboolean, default FalseSpecifies whether or not whitespace (e.g. ' ' or '\t') will be used as the delimiter. Equivalent to setting sep='\s+'. If this option is set to True, nothing should be passed in for the delimiter parameter. Column and index locations and names# headerint or list of ints, default 'infer'Row number(s) to use as the column names, and the start of the data. Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first line of the file, if column names are passed explicitly then the behavior is identical to header=None. Explicitly pass header=0 to be able to replace existing names. The header can be a list of ints that specify row locations for a MultiIndex on the columns e.g. [0,1,3]. Intervening rows that are not specified will be skipped (e.g. 2 in this example is skipped). Note that this parameter ignores commented lines and empty lines if skip_blank_lines=True, so header=0 denotes the first line of data rather than the first line of the file. namesarray-like, default NoneList of column names to use. If file contains no header row, then you should explicitly pass header=None. Duplicates in this list are not allowed. index_colint, str, sequence of int / str, or False, optional, default NoneColumn(s) to use as the row labels of the DataFrame, either given as string name or column index. If a sequence of int / str is given, a MultiIndex is used. Note index_col=False can be used to force pandas to not use the first column as the index, e.g. when you have a malformed file with delimiters at the end of each line. The default value of None instructs pandas to guess. If the number of fields in the column header row is equal to the number of fields in the body of the data file, then a default index is used. If it is larger, then the first columns are used as index so that the remaining number of fields in the body are equal to the number of fields in the header. The first row after the header is used to determine the number of columns, which will go into the index. If the subsequent rows contain less columns than the first row, they are filled with NaN. This can be avoided through usecols. This ensures that the columns are taken as is and the trailing data are ignored. usecolslist-like or callable, default NoneReturn a subset of the columns. If list-like, all elements must either be positional (i.e. integer indices into the document columns) or strings that correspond to column names provided either by the user in names or inferred from the document header row(s). If names are given, the document header row(s) are not taken into account. For example, a valid list-like usecols parameter would be [0, 1, 2] or ['foo', 'bar', 'baz']. Element order is ignored, so usecols=[0, 1] is the same as [1, 0]. To instantiate a DataFrame from data with element order preserved use pd.read_csv(data, usecols=['foo', 'bar'])[['foo', 'bar']] for columns in ['foo', 'bar'] order or pd.read_csv(data, usecols=['foo', 'bar'])[['bar', 'foo']] for ['bar', 'foo'] order. If callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True: In [1]: import pandas as pd In [2]: from io import StringIO In [3]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [4]: pd.read_csv(StringIO(data)) Out[4]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [5]: pd.read_csv(StringIO(data), usecols=lambda x: x.upper() in ["COL1", "COL3"]) Out[5]: col1 col3 0 a 1 1 a 2 2 c 3 Using this parameter results in much faster parsing time and lower memory usage when using the c engine. The Python engine loads the data first before deciding which columns to drop. squeezeboolean, default FalseIf the parsed data only contains one column then return a Series. Deprecated since version 1.4.0: Append .squeeze("columns") to the call to {func_name} to squeeze the data. prefixstr, default NonePrefix to add to column numbers when no header, e.g. ‘X’ for X0, X1, … Deprecated since version 1.4.0: Use a list comprehension on the DataFrame’s columns after calling read_csv. In [6]: data = "col1,col2,col3\na,b,1" In [7]: df = pd.read_csv(StringIO(data)) In [8]: df.columns = [f"pre_{col}" for col in df.columns] In [9]: df Out[9]: pre_col1 pre_col2 pre_col3 0 a b 1 mangle_dupe_colsboolean, default TrueDuplicate columns will be specified as ‘X’, ‘X.1’…’X.N’, rather than ‘X’…’X’. Passing in False will cause data to be overwritten if there are duplicate names in the columns. Deprecated since version 1.5.0: The argument was never implemented, and a new argument where the renaming pattern can be specified will be added instead. General parsing configuration# dtypeType name or dict of column -> type, default NoneData type for data or columns. E.g. {'a': np.float64, 'b': np.int32, 'c': 'Int64'} Use str or object together with suitable na_values settings to preserve and not interpret dtype. If converters are specified, they will be applied INSTEAD of dtype conversion. New in version 1.5.0: Support for defaultdict was added. Specify a defaultdict as input where the default determines the dtype of the columns which are not explicitly listed. engine{'c', 'python', 'pyarrow'}Parser engine to use. The C and pyarrow engines are faster, while the python engine is currently more feature-complete. Multithreading is currently only supported by the pyarrow engine. New in version 1.4.0: The “pyarrow” engine was added as an experimental engine, and some features are unsupported, or may not work correctly, with this engine. convertersdict, default NoneDict of functions for converting values in certain columns. Keys can either be integers or column labels. true_valueslist, default NoneValues to consider as True. false_valueslist, default NoneValues to consider as False. skipinitialspaceboolean, default FalseSkip spaces after delimiter. skiprowslist-like or integer, default NoneLine numbers to skip (0-indexed) or number of lines to skip (int) at the start of the file. If callable, the callable function will be evaluated against the row indices, returning True if the row should be skipped and False otherwise: In [10]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [11]: pd.read_csv(StringIO(data)) Out[11]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [12]: pd.read_csv(StringIO(data), skiprows=lambda x: x % 2 != 0) Out[12]: col1 col2 col3 0 a b 2 skipfooterint, default 0Number of lines at bottom of file to skip (unsupported with engine=’c’). nrowsint, default NoneNumber of rows of file to read. Useful for reading pieces of large files. low_memoryboolean, default TrueInternally process the file in chunks, resulting in lower memory use while parsing, but possibly mixed type inference. To ensure no mixed types either set False, or specify the type with the dtype parameter. Note that the entire file is read into a single DataFrame regardless, use the chunksize or iterator parameter to return the data in chunks. (Only valid with C parser) memory_mapboolean, default FalseIf a filepath is provided for filepath_or_buffer, map the file object directly onto memory and access the data directly from there. Using this option can improve performance because there is no longer any I/O overhead. NA and missing data handling# na_valuesscalar, str, list-like, or dict, default NoneAdditional strings to recognize as NA/NaN. If dict passed, specific per-column NA values. See na values const below for a list of the values interpreted as NaN by default. keep_default_naboolean, default TrueWhether or not to include the default NaN values when parsing the data. Depending on whether na_values is passed in, the behavior is as follows: If keep_default_na is True, and na_values are specified, na_values is appended to the default NaN values used for parsing. If keep_default_na is True, and na_values are not specified, only the default NaN values are used for parsing. If keep_default_na is False, and na_values are specified, only the NaN values specified na_values are used for parsing. If keep_default_na is False, and na_values are not specified, no strings will be parsed as NaN. Note that if na_filter is passed in as False, the keep_default_na and na_values parameters will be ignored. na_filterboolean, default TrueDetect missing value markers (empty strings and the value of na_values). In data without any NAs, passing na_filter=False can improve the performance of reading a large file. verboseboolean, default FalseIndicate number of NA values placed in non-numeric columns. skip_blank_linesboolean, default TrueIf True, skip over blank lines rather than interpreting as NaN values. Datetime handling# parse_datesboolean or list of ints or names or list of lists or dict, default False. If True -> try parsing the index. If [1, 2, 3] -> try parsing columns 1, 2, 3 each as a separate date column. If [[1, 3]] -> combine columns 1 and 3 and parse as a single date column. If {'foo': [1, 3]} -> parse columns 1, 3 as date and call result ‘foo’. Note A fast-path exists for iso8601-formatted dates. infer_datetime_formatboolean, default FalseIf True and parse_dates is enabled for a column, attempt to infer the datetime format to speed up the processing. keep_date_colboolean, default FalseIf True and parse_dates specifies combining multiple columns then keep the original columns. date_parserfunction, default NoneFunction to use for converting a sequence of string columns to an array of datetime instances. The default uses dateutil.parser.parser to do the conversion. pandas will try to call date_parser in three different ways, advancing to the next if an exception occurs: 1) Pass one or more arrays (as defined by parse_dates) as arguments; 2) concatenate (row-wise) the string values from the columns defined by parse_dates into a single array and pass that; and 3) call date_parser once for each row using one or more strings (corresponding to the columns defined by parse_dates) as arguments. dayfirstboolean, default FalseDD/MM format dates, international and European format. cache_datesboolean, default TrueIf True, use a cache of unique, converted dates to apply the datetime conversion. May produce significant speed-up when parsing duplicate date strings, especially ones with timezone offsets. New in version 0.25.0. Iteration# iteratorboolean, default FalseReturn TextFileReader object for iteration or getting chunks with get_chunk(). chunksizeint, default NoneReturn TextFileReader object for iteration. See iterating and chunking below. Quoting, compression, and file format# compression{'infer', 'gzip', 'bz2', 'zip', 'xz', 'zstd', None, dict}, default 'infer'For on-the-fly decompression of on-disk data. If ‘infer’, then use gzip, bz2, zip, xz, or zstandard if filepath_or_buffer is path-like ending in ‘.gz’, ‘.bz2’, ‘.zip’, ‘.xz’, ‘.zst’, respectively, and no decompression otherwise. If using ‘zip’, the ZIP file must contain only one data file to be read in. Set to None for no decompression. Can also be a dict with key 'method' set to one of {'zip', 'gzip', 'bz2', 'zstd'} and other key-value pairs are forwarded to zipfile.ZipFile, gzip.GzipFile, bz2.BZ2File, or zstandard.ZstdDecompressor. As an example, the following could be passed for faster compression and to create a reproducible gzip archive: compression={'method': 'gzip', 'compresslevel': 1, 'mtime': 1}. Changed in version 1.1.0: dict option extended to support gzip and bz2. Changed in version 1.2.0: Previous versions forwarded dict entries for ‘gzip’ to gzip.open. thousandsstr, default NoneThousands separator. decimalstr, default '.'Character to recognize as decimal point. E.g. use ',' for European data. float_precisionstring, default NoneSpecifies which converter the C engine should use for floating-point values. The options are None for the ordinary converter, high for the high-precision converter, and round_trip for the round-trip converter. lineterminatorstr (length 1), default NoneCharacter to break file into lines. Only valid with C parser. quotecharstr (length 1)The character used to denote the start and end of a quoted item. Quoted items can include the delimiter and it will be ignored. quotingint or csv.QUOTE_* instance, default 0Control field quoting behavior per csv.QUOTE_* constants. Use one of QUOTE_MINIMAL (0), QUOTE_ALL (1), QUOTE_NONNUMERIC (2) or QUOTE_NONE (3). doublequoteboolean, default TrueWhen quotechar is specified and quoting is not QUOTE_NONE, indicate whether or not to interpret two consecutive quotechar elements inside a field as a single quotechar element. escapecharstr (length 1), default NoneOne-character string used to escape delimiter when quoting is QUOTE_NONE. commentstr, default NoneIndicates remainder of line should not be parsed. If found at the beginning of a line, the line will be ignored altogether. This parameter must be a single character. Like empty lines (as long as skip_blank_lines=True), fully commented lines are ignored by the parameter header but not by skiprows. For example, if comment='#', parsing ‘#empty\na,b,c\n1,2,3’ with header=0 will result in ‘a,b,c’ being treated as the header. encodingstr, default NoneEncoding to use for UTF when reading/writing (e.g. 'utf-8'). List of Python standard encodings. dialectstr or csv.Dialect instance, default NoneIf provided, this parameter will override values (default or not) for the following parameters: delimiter, doublequote, escapechar, skipinitialspace, quotechar, and quoting. If it is necessary to override values, a ParserWarning will be issued. See csv.Dialect documentation for more details. Error handling# error_bad_linesboolean, optional, default NoneLines with too many fields (e.g. a csv line with too many commas) will by default cause an exception to be raised, and no DataFrame will be returned. If False, then these “bad lines” will dropped from the DataFrame that is returned. See bad lines below. Deprecated since version 1.3.0: The on_bad_lines parameter should be used instead to specify behavior upon encountering a bad line instead. warn_bad_linesboolean, optional, default NoneIf error_bad_lines is False, and warn_bad_lines is True, a warning for each “bad line” will be output. Deprecated since version 1.3.0: The on_bad_lines parameter should be used instead to specify behavior upon encountering a bad line instead. on_bad_lines(‘error’, ‘warn’, ‘skip’), default ‘error’Specifies what to do upon encountering a bad line (a line with too many fields). Allowed values are : ‘error’, raise an ParserError when a bad line is encountered. ‘warn’, print a warning when a bad line is encountered and skip that line. ‘skip’, skip bad lines without raising or warning when they are encountered. New in version 1.3.0. Specifying column data types# You can indicate the data type for the whole DataFrame or individual columns: In [13]: import numpy as np In [14]: data = "a,b,c,d\n1,2,3,4\n5,6,7,8\n9,10,11" In [15]: print(data) a,b,c,d 1,2,3,4 5,6,7,8 9,10,11 In [16]: df = pd.read_csv(StringIO(data), dtype=object) In [17]: df Out[17]: a b c d 0 1 2 3 4 1 5 6 7 8 2 9 10 11 NaN In [18]: df["a"][0] Out[18]: '1' In [19]: df = pd.read_csv(StringIO(data), dtype={"b": object, "c": np.float64, "d": "Int64"}) In [20]: df.dtypes Out[20]: a int64 b object c float64 d Int64 dtype: object Fortunately, pandas offers more than one way to ensure that your column(s) contain only one dtype. If you’re unfamiliar with these concepts, you can see here to learn more about dtypes, and here to learn more about object conversion in pandas. For instance, you can use the converters argument of read_csv(): In [21]: data = "col_1\n1\n2\n'A'\n4.22" In [22]: df = pd.read_csv(StringIO(data), converters={"col_1": str}) In [23]: df Out[23]: col_1 0 1 1 2 2 'A' 3 4.22 In [24]: df["col_1"].apply(type).value_counts() Out[24]: <class 'str'> 4 Name: col_1, dtype: int64 Or you can use the to_numeric() function to coerce the dtypes after reading in the data, In [25]: df2 = pd.read_csv(StringIO(data)) In [26]: df2["col_1"] = pd.to_numeric(df2["col_1"], errors="coerce") In [27]: df2 Out[27]: col_1 0 1.00 1 2.00 2 NaN 3 4.22 In [28]: df2["col_1"].apply(type).value_counts() Out[28]: <class 'float'> 4 Name: col_1, dtype: int64 which will convert all valid parsing to floats, leaving the invalid parsing as NaN. Ultimately, how you deal with reading in columns containing mixed dtypes depends on your specific needs. In the case above, if you wanted to NaN out the data anomalies, then to_numeric() is probably your best option. However, if you wanted for all the data to be coerced, no matter the type, then using the converters argument of read_csv() would certainly be worth trying. Note In some cases, reading in abnormal data with columns containing mixed dtypes will result in an inconsistent dataset. If you rely on pandas to infer the dtypes of your columns, the parsing engine will go and infer the dtypes for different chunks of the data, rather than the whole dataset at once. Consequently, you can end up with column(s) with mixed dtypes. For example, In [29]: col_1 = list(range(500000)) + ["a", "b"] + list(range(500000)) In [30]: df = pd.DataFrame({"col_1": col_1}) In [31]: df.to_csv("foo.csv") In [32]: mixed_df = pd.read_csv("foo.csv") In [33]: mixed_df["col_1"].apply(type).value_counts() Out[33]: <class 'int'> 737858 <class 'str'> 262144 Name: col_1, dtype: int64 In [34]: mixed_df["col_1"].dtype Out[34]: dtype('O') will result with mixed_df containing an int dtype for certain chunks of the column, and str for others due to the mixed dtypes from the data that was read in. It is important to note that the overall column will be marked with a dtype of object, which is used for columns with mixed dtypes. Specifying categorical dtype# Categorical columns can be parsed directly by specifying dtype='category' or dtype=CategoricalDtype(categories, ordered). In [35]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [36]: pd.read_csv(StringIO(data)) Out[36]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [37]: pd.read_csv(StringIO(data)).dtypes Out[37]: col1 object col2 object col3 int64 dtype: object In [38]: pd.read_csv(StringIO(data), dtype="category").dtypes Out[38]: col1 category col2 category col3 category dtype: object Individual columns can be parsed as a Categorical using a dict specification: In [39]: pd.read_csv(StringIO(data), dtype={"col1": "category"}).dtypes Out[39]: col1 category col2 object col3 int64 dtype: object Specifying dtype='category' will result in an unordered Categorical whose categories are the unique values observed in the data. For more control on the categories and order, create a CategoricalDtype ahead of time, and pass that for that column’s dtype. In [40]: from pandas.api.types import CategoricalDtype In [41]: dtype = CategoricalDtype(["d", "c", "b", "a"], ordered=True) In [42]: pd.read_csv(StringIO(data), dtype={"col1": dtype}).dtypes Out[42]: col1 category col2 object col3 int64 dtype: object When using dtype=CategoricalDtype, “unexpected” values outside of dtype.categories are treated as missing values. In [43]: dtype = CategoricalDtype(["a", "b", "d"]) # No 'c' In [44]: pd.read_csv(StringIO(data), dtype={"col1": dtype}).col1 Out[44]: 0 a 1 a 2 NaN Name: col1, dtype: category Categories (3, object): ['a', 'b', 'd'] This matches the behavior of Categorical.set_categories(). Note With dtype='category', the resulting categories will always be parsed as strings (object dtype). If the categories are numeric they can be converted using the to_numeric() function, or as appropriate, another converter such as to_datetime(). When dtype is a CategoricalDtype with homogeneous categories ( all numeric, all datetimes, etc.), the conversion is done automatically. In [45]: df = pd.read_csv(StringIO(data), dtype="category") In [46]: df.dtypes Out[46]: col1 category col2 category col3 category dtype: object In [47]: df["col3"] Out[47]: 0 1 1 2 2 3 Name: col3, dtype: category Categories (3, object): ['1', '2', '3'] In [48]: new_categories = pd.to_numeric(df["col3"].cat.categories) In [49]: df["col3"] = df["col3"].cat.rename_categories(new_categories) In [50]: df["col3"] Out[50]: 0 1 1 2 2 3 Name: col3, dtype: category Categories (3, int64): [1, 2, 3] Naming and using columns# Handling column names# A file may or may not have a header row. pandas assumes the first row should be used as the column names: In [51]: data = "a,b,c\n1,2,3\n4,5,6\n7,8,9" In [52]: print(data) a,b,c 1,2,3 4,5,6 7,8,9 In [53]: pd.read_csv(StringIO(data)) Out[53]: a b c 0 1 2 3 1 4 5 6 2 7 8 9 By specifying the names argument in conjunction with header you can indicate other names to use and whether or not to throw away the header row (if any): In [54]: print(data) a,b,c 1,2,3 4,5,6 7,8,9 In [55]: pd.read_csv(StringIO(data), names=["foo", "bar", "baz"], header=0) Out[55]: foo bar baz 0 1 2 3 1 4 5 6 2 7 8 9 In [56]: pd.read_csv(StringIO(data), names=["foo", "bar", "baz"], header=None) Out[56]: foo bar baz 0 a b c 1 1 2 3 2 4 5 6 3 7 8 9 If the header is in a row other than the first, pass the row number to header. This will skip the preceding rows: In [57]: data = "skip this skip it\na,b,c\n1,2,3\n4,5,6\n7,8,9" In [58]: pd.read_csv(StringIO(data), header=1) Out[58]: a b c 0 1 2 3 1 4 5 6 2 7 8 9 Note Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first non-blank line of the file, if column names are passed explicitly then the behavior is identical to header=None. Duplicate names parsing# Deprecated since version 1.5.0: mangle_dupe_cols was never implemented, and a new argument where the renaming pattern can be specified will be added instead. If the file or header contains duplicate names, pandas will by default distinguish between them so as to prevent overwriting data: In [59]: data = "a,b,a\n0,1,2\n3,4,5" In [60]: pd.read_csv(StringIO(data)) Out[60]: a b a.1 0 0 1 2 1 3 4 5 There is no more duplicate data because mangle_dupe_cols=True by default, which modifies a series of duplicate columns ‘X’, …, ‘X’ to become ‘X’, ‘X.1’, …, ‘X.N’. Filtering columns (usecols)# The usecols argument allows you to select any subset of the columns in a file, either using the column names, position numbers or a callable: In [61]: data = "a,b,c,d\n1,2,3,foo\n4,5,6,bar\n7,8,9,baz" In [62]: pd.read_csv(StringIO(data)) Out[62]: a b c d 0 1 2 3 foo 1 4 5 6 bar 2 7 8 9 baz In [63]: pd.read_csv(StringIO(data), usecols=["b", "d"]) Out[63]: b d 0 2 foo 1 5 bar 2 8 baz In [64]: pd.read_csv(StringIO(data), usecols=[0, 2, 3]) Out[64]: a c d 0 1 3 foo 1 4 6 bar 2 7 9 baz In [65]: pd.read_csv(StringIO(data), usecols=lambda x: x.upper() in ["A", "C"]) Out[65]: a c 0 1 3 1 4 6 2 7 9 The usecols argument can also be used to specify which columns not to use in the final result: In [66]: pd.read_csv(StringIO(data), usecols=lambda x: x not in ["a", "c"]) Out[66]: b d 0 2 foo 1 5 bar 2 8 baz In this case, the callable is specifying that we exclude the “a” and “c” columns from the output. Comments and empty lines# Ignoring line comments and empty lines# If the comment parameter is specified, then completely commented lines will be ignored. By default, completely blank lines will be ignored as well. In [67]: data = "\na,b,c\n \n# commented line\n1,2,3\n\n4,5,6" In [68]: print(data) a,b,c # commented line 1,2,3 4,5,6 In [69]: pd.read_csv(StringIO(data), comment="#") Out[69]: a b c 0 1 2 3 1 4 5 6 If skip_blank_lines=False, then read_csv will not ignore blank lines: In [70]: data = "a,b,c\n\n1,2,3\n\n\n4,5,6" In [71]: pd.read_csv(StringIO(data), skip_blank_lines=False) Out[71]: a b c 0 NaN NaN NaN 1 1.0 2.0 3.0 2 NaN NaN NaN 3 NaN NaN NaN 4 4.0 5.0 6.0 Warning The presence of ignored lines might create ambiguities involving line numbers; the parameter header uses row numbers (ignoring commented/empty lines), while skiprows uses line numbers (including commented/empty lines): In [72]: data = "#comment\na,b,c\nA,B,C\n1,2,3" In [73]: pd.read_csv(StringIO(data), comment="#", header=1) Out[73]: A B C 0 1 2 3 In [74]: data = "A,B,C\n#comment\na,b,c\n1,2,3" In [75]: pd.read_csv(StringIO(data), comment="#", skiprows=2) Out[75]: a b c 0 1 2 3 If both header and skiprows are specified, header will be relative to the end of skiprows. For example: In [76]: data = ( ....: "# empty\n" ....: "# second empty line\n" ....: "# third emptyline\n" ....: "X,Y,Z\n" ....: "1,2,3\n" ....: "A,B,C\n" ....: "1,2.,4.\n" ....: "5.,NaN,10.0\n" ....: ) ....: In [77]: print(data) # empty # second empty line # third emptyline X,Y,Z 1,2,3 A,B,C 1,2.,4. 5.,NaN,10.0 In [78]: pd.read_csv(StringIO(data), comment="#", skiprows=4, header=1) Out[78]: A B C 0 1.0 2.0 4.0 1 5.0 NaN 10.0 Comments# Sometimes comments or meta data may be included in a file: In [79]: print(open("tmp.csv").read()) ID,level,category Patient1,123000,x # really unpleasant Patient2,23000,y # wouldn't take his medicine Patient3,1234018,z # awesome By default, the parser includes the comments in the output: In [80]: df = pd.read_csv("tmp.csv") In [81]: df Out[81]: ID level category 0 Patient1 123000 x # really unpleasant 1 Patient2 23000 y # wouldn't take his medicine 2 Patient3 1234018 z # awesome We can suppress the comments using the comment keyword: In [82]: df = pd.read_csv("tmp.csv", comment="#") In [83]: df Out[83]: ID level category 0 Patient1 123000 x 1 Patient2 23000 y 2 Patient3 1234018 z Dealing with Unicode data# The encoding argument should be used for encoded unicode data, which will result in byte strings being decoded to unicode in the result: In [84]: from io import BytesIO In [85]: data = b"word,length\n" b"Tr\xc3\xa4umen,7\n" b"Gr\xc3\xbc\xc3\x9fe,5" In [86]: data = data.decode("utf8").encode("latin-1") In [87]: df = pd.read_csv(BytesIO(data), encoding="latin-1") In [88]: df Out[88]: word length 0 Träumen 7 1 Grüße 5 In [89]: df["word"][1] Out[89]: 'Grüße' Some formats which encode all characters as multiple bytes, like UTF-16, won’t parse correctly at all without specifying the encoding. Full list of Python standard encodings. Index columns and trailing delimiters# If a file has one more column of data than the number of column names, the first column will be used as the DataFrame’s row names: In [90]: data = "a,b,c\n4,apple,bat,5.7\n8,orange,cow,10" In [91]: pd.read_csv(StringIO(data)) Out[91]: a b c 4 apple bat 5.7 8 orange cow 10.0 In [92]: data = "index,a,b,c\n4,apple,bat,5.7\n8,orange,cow,10" In [93]: pd.read_csv(StringIO(data), index_col=0) Out[93]: a b c index 4 apple bat 5.7 8 orange cow 10.0 Ordinarily, you can achieve this behavior using the index_col option. There are some exception cases when a file has been prepared with delimiters at the end of each data line, confusing the parser. To explicitly disable the index column inference and discard the last column, pass index_col=False: In [94]: data = "a,b,c\n4,apple,bat,\n8,orange,cow," In [95]: print(data) a,b,c 4,apple,bat, 8,orange,cow, In [96]: pd.read_csv(StringIO(data)) Out[96]: a b c 4 apple bat NaN 8 orange cow NaN In [97]: pd.read_csv(StringIO(data), index_col=False) Out[97]: a b c 0 4 apple bat 1 8 orange cow If a subset of data is being parsed using the usecols option, the index_col specification is based on that subset, not the original data. In [98]: data = "a,b,c\n4,apple,bat,\n8,orange,cow," In [99]: print(data) a,b,c 4,apple,bat, 8,orange,cow, In [100]: pd.read_csv(StringIO(data), usecols=["b", "c"]) Out[100]: b c 4 bat NaN 8 cow NaN In [101]: pd.read_csv(StringIO(data), usecols=["b", "c"], index_col=0) Out[101]: b c 4 bat NaN 8 cow NaN Date Handling# Specifying date columns# To better facilitate working with datetime data, read_csv() uses the keyword arguments parse_dates and date_parser to allow users to specify a variety of columns and date/time formats to turn the input text data into datetime objects. The simplest case is to just pass in parse_dates=True: In [102]: with open("foo.csv", mode="w") as f: .....: f.write("date,A,B,C\n20090101,a,1,2\n20090102,b,3,4\n20090103,c,4,5") .....: # Use a column as an index, and parse it as dates. In [103]: df = pd.read_csv("foo.csv", index_col=0, parse_dates=True) In [104]: df Out[104]: A B C date 2009-01-01 a 1 2 2009-01-02 b 3 4 2009-01-03 c 4 5 # These are Python datetime objects In [105]: df.index Out[105]: DatetimeIndex(['2009-01-01', '2009-01-02', '2009-01-03'], dtype='datetime64[ns]', name='date', freq=None) It is often the case that we may want to store date and time data separately, or store various date fields separately. the parse_dates keyword can be used to specify a combination of columns to parse the dates and/or times from. You can specify a list of column lists to parse_dates, the resulting date columns will be prepended to the output (so as to not affect the existing column order) and the new column names will be the concatenation of the component column names: In [106]: data = ( .....: "KORD,19990127, 19:00:00, 18:56:00, 0.8100\n" .....: "KORD,19990127, 20:00:00, 19:56:00, 0.0100\n" .....: "KORD,19990127, 21:00:00, 20:56:00, -0.5900\n" .....: "KORD,19990127, 21:00:00, 21:18:00, -0.9900\n" .....: "KORD,19990127, 22:00:00, 21:56:00, -0.5900\n" .....: "KORD,19990127, 23:00:00, 22:56:00, -0.5900" .....: ) .....: In [107]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [108]: df = pd.read_csv("tmp.csv", header=None, parse_dates=[[1, 2], [1, 3]]) In [109]: df Out[109]: 1_2 1_3 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 By default the parser removes the component date columns, but you can choose to retain them via the keep_date_col keyword: In [110]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=[[1, 2], [1, 3]], keep_date_col=True .....: ) .....: In [111]: df Out[111]: 1_2 1_3 0 ... 2 3 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD ... 19:00:00 18:56:00 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD ... 20:00:00 19:56:00 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD ... 21:00:00 20:56:00 -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD ... 21:00:00 21:18:00 -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD ... 22:00:00 21:56:00 -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD ... 23:00:00 22:56:00 -0.59 [6 rows x 7 columns] Note that if you wish to combine multiple columns into a single date column, a nested list must be used. In other words, parse_dates=[1, 2] indicates that the second and third columns should each be parsed as separate date columns while parse_dates=[[1, 2]] means the two columns should be parsed into a single column. You can also use a dict to specify custom name columns: In [112]: date_spec = {"nominal": [1, 2], "actual": [1, 3]} In [113]: df = pd.read_csv("tmp.csv", header=None, parse_dates=date_spec) In [114]: df Out[114]: nominal actual 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 It is important to remember that if multiple text columns are to be parsed into a single date column, then a new column is prepended to the data. The index_col specification is based off of this new set of columns rather than the original data columns: In [115]: date_spec = {"nominal": [1, 2], "actual": [1, 3]} In [116]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=date_spec, index_col=0 .....: ) # index is the nominal column .....: In [117]: df Out[117]: actual 0 4 nominal 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 Note If a column or index contains an unparsable date, the entire column or index will be returned unaltered as an object data type. For non-standard datetime parsing, use to_datetime() after pd.read_csv. Note read_csv has a fast_path for parsing datetime strings in iso8601 format, e.g “2000-01-01T00:01:02+00:00” and similar variations. If you can arrange for your data to store datetimes in this format, load times will be significantly faster, ~20x has been observed. Date parsing functions# Finally, the parser allows you to specify a custom date_parser function to take full advantage of the flexibility of the date parsing API: In [118]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=date_spec, date_parser=pd.to_datetime .....: ) .....: In [119]: df Out[119]: nominal actual 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 pandas will try to call the date_parser function in three different ways. If an exception is raised, the next one is tried: date_parser is first called with one or more arrays as arguments, as defined using parse_dates (e.g., date_parser(['2013', '2013'], ['1', '2'])). If #1 fails, date_parser is called with all the columns concatenated row-wise into a single array (e.g., date_parser(['2013 1', '2013 2'])). Note that performance-wise, you should try these methods of parsing dates in order: Try to infer the format using infer_datetime_format=True (see section below). If you know the format, use pd.to_datetime(): date_parser=lambda x: pd.to_datetime(x, format=...). If you have a really non-standard format, use a custom date_parser function. For optimal performance, this should be vectorized, i.e., it should accept arrays as arguments. Parsing a CSV with mixed timezones# pandas cannot natively represent a column or index with mixed timezones. If your CSV file contains columns with a mixture of timezones, the default result will be an object-dtype column with strings, even with parse_dates. In [120]: content = """\ .....: a .....: 2000-01-01T00:00:00+05:00 .....: 2000-01-01T00:00:00+06:00""" .....: In [121]: df = pd.read_csv(StringIO(content), parse_dates=["a"]) In [122]: df["a"] Out[122]: 0 2000-01-01 00:00:00+05:00 1 2000-01-01 00:00:00+06:00 Name: a, dtype: object To parse the mixed-timezone values as a datetime column, pass a partially-applied to_datetime() with utc=True as the date_parser. In [123]: df = pd.read_csv( .....: StringIO(content), .....: parse_dates=["a"], .....: date_parser=lambda col: pd.to_datetime(col, utc=True), .....: ) .....: In [124]: df["a"] Out[124]: 0 1999-12-31 19:00:00+00:00 1 1999-12-31 18:00:00+00:00 Name: a, dtype: datetime64[ns, UTC] Inferring datetime format# If you have parse_dates enabled for some or all of your columns, and your datetime strings are all formatted the same way, you may get a large speed up by setting infer_datetime_format=True. If set, pandas will attempt to guess the format of your datetime strings, and then use a faster means of parsing the strings. 5-10x parsing speeds have been observed. pandas will fallback to the usual parsing if either the format cannot be guessed or the format that was guessed cannot properly parse the entire column of strings. So in general, infer_datetime_format should not have any negative consequences if enabled. Here are some examples of datetime strings that can be guessed (All representing December 30th, 2011 at 00:00:00): “20111230” “2011/12/30” “20111230 00:00:00” “12/30/2011 00:00:00” “30/Dec/2011 00:00:00” “30/December/2011 00:00:00” Note that infer_datetime_format is sensitive to dayfirst. With dayfirst=True, it will guess “01/12/2011” to be December 1st. With dayfirst=False (default) it will guess “01/12/2011” to be January 12th. # Try to infer the format for the index column In [125]: df = pd.read_csv( .....: "foo.csv", .....: index_col=0, .....: parse_dates=True, .....: infer_datetime_format=True, .....: ) .....: In [126]: df Out[126]: A B C date 2009-01-01 a 1 2 2009-01-02 b 3 4 2009-01-03 c 4 5 International date formats# While US date formats tend to be MM/DD/YYYY, many international formats use DD/MM/YYYY instead. For convenience, a dayfirst keyword is provided: In [127]: data = "date,value,cat\n1/6/2000,5,a\n2/6/2000,10,b\n3/6/2000,15,c" In [128]: print(data) date,value,cat 1/6/2000,5,a 2/6/2000,10,b 3/6/2000,15,c In [129]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [130]: pd.read_csv("tmp.csv", parse_dates=[0]) Out[130]: date value cat 0 2000-01-06 5 a 1 2000-02-06 10 b 2 2000-03-06 15 c In [131]: pd.read_csv("tmp.csv", dayfirst=True, parse_dates=[0]) Out[131]: date value cat 0 2000-06-01 5 a 1 2000-06-02 10 b 2 2000-06-03 15 c Writing CSVs to binary file objects# New in version 1.2.0. df.to_csv(..., mode="wb") allows writing a CSV to a file object opened binary mode. In most cases, it is not necessary to specify mode as Pandas will auto-detect whether the file object is opened in text or binary mode. In [132]: import io In [133]: data = pd.DataFrame([0, 1, 2]) In [134]: buffer = io.BytesIO() In [135]: data.to_csv(buffer, encoding="utf-8", compression="gzip") Specifying method for floating-point conversion# The parameter float_precision can be specified in order to use a specific floating-point converter during parsing with the C engine. The options are the ordinary converter, the high-precision converter, and the round-trip converter (which is guaranteed to round-trip values after writing to a file). For example: In [136]: val = "0.3066101993807095471566981359501369297504425048828125" In [137]: data = "a,b,c\n1,2,{0}".format(val) In [138]: abs( .....: pd.read_csv( .....: StringIO(data), .....: engine="c", .....: float_precision=None, .....: )["c"][0] - float(val) .....: ) .....: Out[138]: 5.551115123125783e-17 In [139]: abs( .....: pd.read_csv( .....: StringIO(data), .....: engine="c", .....: float_precision="high", .....: )["c"][0] - float(val) .....: ) .....: Out[139]: 5.551115123125783e-17 In [140]: abs( .....: pd.read_csv(StringIO(data), engine="c", float_precision="round_trip")["c"][0] .....: - float(val) .....: ) .....: Out[140]: 0.0 Thousand separators# For large numbers that have been written with a thousands separator, you can set the thousands keyword to a string of length 1 so that integers will be parsed correctly: By default, numbers with a thousands separator will be parsed as strings: In [141]: data = ( .....: "ID|level|category\n" .....: "Patient1|123,000|x\n" .....: "Patient2|23,000|y\n" .....: "Patient3|1,234,018|z" .....: ) .....: In [142]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [143]: df = pd.read_csv("tmp.csv", sep="|") In [144]: df Out[144]: ID level category 0 Patient1 123,000 x 1 Patient2 23,000 y 2 Patient3 1,234,018 z In [145]: df.level.dtype Out[145]: dtype('O') The thousands keyword allows integers to be parsed correctly: In [146]: df = pd.read_csv("tmp.csv", sep="|", thousands=",") In [147]: df Out[147]: ID level category 0 Patient1 123000 x 1 Patient2 23000 y 2 Patient3 1234018 z In [148]: df.level.dtype Out[148]: dtype('int64') NA values# To control which values are parsed as missing values (which are signified by NaN), specify a string in na_values. If you specify a list of strings, then all values in it are considered to be missing values. If you specify a number (a float, like 5.0 or an integer like 5), the corresponding equivalent values will also imply a missing value (in this case effectively [5.0, 5] are recognized as NaN). To completely override the default values that are recognized as missing, specify keep_default_na=False. The default NaN recognized values are ['-1.#IND', '1.#QNAN', '1.#IND', '-1.#QNAN', '#N/A N/A', '#N/A', 'N/A', 'n/a', 'NA', '<NA>', '#NA', 'NULL', 'null', 'NaN', '-NaN', 'nan', '-nan', '']. Let us consider some examples: pd.read_csv("path_to_file.csv", na_values=[5]) In the example above 5 and 5.0 will be recognized as NaN, in addition to the defaults. A string will first be interpreted as a numerical 5, then as a NaN. pd.read_csv("path_to_file.csv", keep_default_na=False, na_values=[""]) Above, only an empty field will be recognized as NaN. pd.read_csv("path_to_file.csv", keep_default_na=False, na_values=["NA", "0"]) Above, both NA and 0 as strings are NaN. pd.read_csv("path_to_file.csv", na_values=["Nope"]) The default values, in addition to the string "Nope" are recognized as NaN. Infinity# inf like values will be parsed as np.inf (positive infinity), and -inf as -np.inf (negative infinity). These will ignore the case of the value, meaning Inf, will also be parsed as np.inf. Returning Series# Using the squeeze keyword, the parser will return output with a single column as a Series: Deprecated since version 1.4.0: Users should append .squeeze("columns") to the DataFrame returned by read_csv instead. In [149]: data = "level\nPatient1,123000\nPatient2,23000\nPatient3,1234018" In [150]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [151]: print(open("tmp.csv").read()) level Patient1,123000 Patient2,23000 Patient3,1234018 In [152]: output = pd.read_csv("tmp.csv", squeeze=True) In [153]: output Out[153]: Patient1 123000 Patient2 23000 Patient3 1234018 Name: level, dtype: int64 In [154]: type(output) Out[154]: pandas.core.series.Series Boolean values# The common values True, False, TRUE, and FALSE are all recognized as boolean. Occasionally you might want to recognize other values as being boolean. To do this, use the true_values and false_values options as follows: In [155]: data = "a,b,c\n1,Yes,2\n3,No,4" In [156]: print(data) a,b,c 1,Yes,2 3,No,4 In [157]: pd.read_csv(StringIO(data)) Out[157]: a b c 0 1 Yes 2 1 3 No 4 In [158]: pd.read_csv(StringIO(data), true_values=["Yes"], false_values=["No"]) Out[158]: a b c 0 1 True 2 1 3 False 4 Handling “bad” lines# Some files may have malformed lines with too few fields or too many. Lines with too few fields will have NA values filled in the trailing fields. Lines with too many fields will raise an error by default: In [159]: data = "a,b,c\n1,2,3\n4,5,6,7\n8,9,10" In [160]: pd.read_csv(StringIO(data)) --------------------------------------------------------------------------- ParserError Traceback (most recent call last) Cell In[160], line 1 ----> 1 pd.read_csv(StringIO(data)) File ~/work/pandas/pandas/pandas/util/_decorators.py:211, in deprecate_kwarg.<locals>._deprecate_kwarg.<locals>.wrapper(*args, **kwargs) 209 else: 210 kwargs[new_arg_name] = new_arg_value --> 211 return func(*args, **kwargs) File ~/work/pandas/pandas/pandas/util/_decorators.py:331, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs) 325 if len(args) > num_allow_args: 326 warnings.warn( 327 msg.format(arguments=_format_argument_list(allow_args)), 328 FutureWarning, 329 stacklevel=find_stack_level(), 330 ) --> 331 return func(*args, **kwargs) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:950, in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, error_bad_lines, warn_bad_lines, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options) 935 kwds_defaults = _refine_defaults_read( 936 dialect, 937 delimiter, (...) 946 defaults={"delimiter": ","}, 947 ) 948 kwds.update(kwds_defaults) --> 950 return _read(filepath_or_buffer, kwds) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:611, in _read(filepath_or_buffer, kwds) 608 return parser 610 with parser: --> 611 return parser.read(nrows) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:1778, in TextFileReader.read(self, nrows) 1771 nrows = validate_integer("nrows", nrows) 1772 try: 1773 # error: "ParserBase" has no attribute "read" 1774 ( 1775 index, 1776 columns, 1777 col_dict, -> 1778 ) = self._engine.read( # type: ignore[attr-defined] 1779 nrows 1780 ) 1781 except Exception: 1782 self.close() File ~/work/pandas/pandas/pandas/io/parsers/c_parser_wrapper.py:230, in CParserWrapper.read(self, nrows) 228 try: 229 if self.low_memory: --> 230 chunks = self._reader.read_low_memory(nrows) 231 # destructive to chunks 232 data = _concatenate_chunks(chunks) File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:808, in pandas._libs.parsers.TextReader.read_low_memory() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:866, in pandas._libs.parsers.TextReader._read_rows() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:852, in pandas._libs.parsers.TextReader._tokenize_rows() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:1973, in pandas._libs.parsers.raise_parser_error() ParserError: Error tokenizing data. C error: Expected 3 fields in line 3, saw 4 You can elect to skip bad lines: In [29]: pd.read_csv(StringIO(data), on_bad_lines="warn") Skipping line 3: expected 3 fields, saw 4 Out[29]: a b c 0 1 2 3 1 8 9 10 Or pass a callable function to handle the bad line if engine="python". The bad line will be a list of strings that was split by the sep: In [29]: external_list = [] In [30]: def bad_lines_func(line): ...: external_list.append(line) ...: return line[-3:] In [31]: pd.read_csv(StringIO(data), on_bad_lines=bad_lines_func, engine="python") Out[31]: a b c 0 1 2 3 1 5 6 7 2 8 9 10 In [32]: external_list Out[32]: [4, 5, 6, 7] .. versionadded:: 1.4.0 You can also use the usecols parameter to eliminate extraneous column data that appear in some lines but not others: In [33]: pd.read_csv(StringIO(data), usecols=[0, 1, 2]) Out[33]: a b c 0 1 2 3 1 4 5 6 2 8 9 10 In case you want to keep all data including the lines with too many fields, you can specify a sufficient number of names. This ensures that lines with not enough fields are filled with NaN. In [34]: pd.read_csv(StringIO(data), names=['a', 'b', 'c', 'd']) Out[34]: a b c d 0 1 2 3 NaN 1 4 5 6 7 2 8 9 10 NaN Dialect# The dialect keyword gives greater flexibility in specifying the file format. By default it uses the Excel dialect but you can specify either the dialect name or a csv.Dialect instance. Suppose you had data with unenclosed quotes: In [161]: data = "label1,label2,label3\n" 'index1,"a,c,e\n' "index2,b,d,f" In [162]: print(data) label1,label2,label3 index1,"a,c,e index2,b,d,f By default, read_csv uses the Excel dialect and treats the double quote as the quote character, which causes it to fail when it finds a newline before it finds the closing double quote. We can get around this using dialect: In [163]: import csv In [164]: dia = csv.excel() In [165]: dia.quoting = csv.QUOTE_NONE In [166]: pd.read_csv(StringIO(data), dialect=dia) Out[166]: label1 label2 label3 index1 "a c e index2 b d f All of the dialect options can be specified separately by keyword arguments: In [167]: data = "a,b,c~1,2,3~4,5,6" In [168]: pd.read_csv(StringIO(data), lineterminator="~") Out[168]: a b c 0 1 2 3 1 4 5 6 Another common dialect option is skipinitialspace, to skip any whitespace after a delimiter: In [169]: data = "a, b, c\n1, 2, 3\n4, 5, 6" In [170]: print(data) a, b, c 1, 2, 3 4, 5, 6 In [171]: pd.read_csv(StringIO(data), skipinitialspace=True) Out[171]: a b c 0 1 2 3 1 4 5 6 The parsers make every attempt to “do the right thing” and not be fragile. Type inference is a pretty big deal. If a column can be coerced to integer dtype without altering the contents, the parser will do so. Any non-numeric columns will come through as object dtype as with the rest of pandas objects. Quoting and Escape Characters# Quotes (and other escape characters) in embedded fields can be handled in any number of ways. One way is to use backslashes; to properly parse this data, you should pass the escapechar option: In [172]: data = 'a,b\n"hello, \\"Bob\\", nice to see you",5' In [173]: print(data) a,b "hello, \"Bob\", nice to see you",5 In [174]: pd.read_csv(StringIO(data), escapechar="\\") Out[174]: a b 0 hello, "Bob", nice to see you 5 Files with fixed width columns# While read_csv() reads delimited data, the read_fwf() function works with data files that have known and fixed column widths. The function parameters to read_fwf are largely the same as read_csv with two extra parameters, and a different usage of the delimiter parameter: colspecs: A list of pairs (tuples) giving the extents of the fixed-width fields of each line as half-open intervals (i.e., [from, to[ ). String value ‘infer’ can be used to instruct the parser to try detecting the column specifications from the first 100 rows of the data. Default behavior, if not specified, is to infer. widths: A list of field widths which can be used instead of ‘colspecs’ if the intervals are contiguous. delimiter: Characters to consider as filler characters in the fixed-width file. Can be used to specify the filler character of the fields if it is not spaces (e.g., ‘~’). Consider a typical fixed-width data file: In [175]: data1 = ( .....: "id8141 360.242940 149.910199 11950.7\n" .....: "id1594 444.953632 166.985655 11788.4\n" .....: "id1849 364.136849 183.628767 11806.2\n" .....: "id1230 413.836124 184.375703 11916.8\n" .....: "id1948 502.953953 173.237159 12468.3" .....: ) .....: In [176]: with open("bar.csv", "w") as f: .....: f.write(data1) .....: In order to parse this file into a DataFrame, we simply need to supply the column specifications to the read_fwf function along with the file name: # Column specifications are a list of half-intervals In [177]: colspecs = [(0, 6), (8, 20), (21, 33), (34, 43)] In [178]: df = pd.read_fwf("bar.csv", colspecs=colspecs, header=None, index_col=0) In [179]: df Out[179]: 1 2 3 0 id8141 360.242940 149.910199 11950.7 id1594 444.953632 166.985655 11788.4 id1849 364.136849 183.628767 11806.2 id1230 413.836124 184.375703 11916.8 id1948 502.953953 173.237159 12468.3 Note how the parser automatically picks column names X.<column number> when header=None argument is specified. Alternatively, you can supply just the column widths for contiguous columns: # Widths are a list of integers In [180]: widths = [6, 14, 13, 10] In [181]: df = pd.read_fwf("bar.csv", widths=widths, header=None) In [182]: df Out[182]: 0 1 2 3 0 id8141 360.242940 149.910199 11950.7 1 id1594 444.953632 166.985655 11788.4 2 id1849 364.136849 183.628767 11806.2 3 id1230 413.836124 184.375703 11916.8 4 id1948 502.953953 173.237159 12468.3 The parser will take care of extra white spaces around the columns so it’s ok to have extra separation between the columns in the file. By default, read_fwf will try to infer the file’s colspecs by using the first 100 rows of the file. It can do it only in cases when the columns are aligned and correctly separated by the provided delimiter (default delimiter is whitespace). In [183]: df = pd.read_fwf("bar.csv", header=None, index_col=0) In [184]: df Out[184]: 1 2 3 0 id8141 360.242940 149.910199 11950.7 id1594 444.953632 166.985655 11788.4 id1849 364.136849 183.628767 11806.2 id1230 413.836124 184.375703 11916.8 id1948 502.953953 173.237159 12468.3 read_fwf supports the dtype parameter for specifying the types of parsed columns to be different from the inferred type. In [185]: pd.read_fwf("bar.csv", header=None, index_col=0).dtypes Out[185]: 1 float64 2 float64 3 float64 dtype: object In [186]: pd.read_fwf("bar.csv", header=None, dtype={2: "object"}).dtypes Out[186]: 0 object 1 float64 2 object 3 float64 dtype: object Indexes# Files with an “implicit” index column# Consider a file with one less entry in the header than the number of data column: In [187]: data = "A,B,C\n20090101,a,1,2\n20090102,b,3,4\n20090103,c,4,5" In [188]: print(data) A,B,C 20090101,a,1,2 20090102,b,3,4 20090103,c,4,5 In [189]: with open("foo.csv", "w") as f: .....: f.write(data) .....: In this special case, read_csv assumes that the first column is to be used as the index of the DataFrame: In [190]: pd.read_csv("foo.csv") Out[190]: A B C 20090101 a 1 2 20090102 b 3 4 20090103 c 4 5 Note that the dates weren’t automatically parsed. In that case you would need to do as before: In [191]: df = pd.read_csv("foo.csv", parse_dates=True) In [192]: df.index Out[192]: DatetimeIndex(['2009-01-01', '2009-01-02', '2009-01-03'], dtype='datetime64[ns]', freq=None) Reading an index with a MultiIndex# Suppose you have data indexed by two columns: In [193]: data = 'year,indiv,zit,xit\n1977,"A",1.2,.6\n1977,"B",1.5,.5' In [194]: print(data) year,indiv,zit,xit 1977,"A",1.2,.6 1977,"B",1.5,.5 In [195]: with open("mindex_ex.csv", mode="w") as f: .....: f.write(data) .....: The index_col argument to read_csv can take a list of column numbers to turn multiple columns into a MultiIndex for the index of the returned object: In [196]: df = pd.read_csv("mindex_ex.csv", index_col=[0, 1]) In [197]: df Out[197]: zit xit year indiv 1977 A 1.2 0.6 B 1.5 0.5 In [198]: df.loc[1977] Out[198]: zit xit indiv A 1.2 0.6 B 1.5 0.5 Reading columns with a MultiIndex# By specifying list of row locations for the header argument, you can read in a MultiIndex for the columns. Specifying non-consecutive rows will skip the intervening rows. In [199]: from pandas._testing import makeCustomDataframe as mkdf In [200]: df = mkdf(5, 3, r_idx_nlevels=2, c_idx_nlevels=4) In [201]: df.to_csv("mi.csv") In [202]: print(open("mi.csv").read()) C0,,C_l0_g0,C_l0_g1,C_l0_g2 C1,,C_l1_g0,C_l1_g1,C_l1_g2 C2,,C_l2_g0,C_l2_g1,C_l2_g2 C3,,C_l3_g0,C_l3_g1,C_l3_g2 R0,R1,,, R_l0_g0,R_l1_g0,R0C0,R0C1,R0C2 R_l0_g1,R_l1_g1,R1C0,R1C1,R1C2 R_l0_g2,R_l1_g2,R2C0,R2C1,R2C2 R_l0_g3,R_l1_g3,R3C0,R3C1,R3C2 R_l0_g4,R_l1_g4,R4C0,R4C1,R4C2 In [203]: pd.read_csv("mi.csv", header=[0, 1, 2, 3], index_col=[0, 1]) Out[203]: C0 C_l0_g0 C_l0_g1 C_l0_g2 C1 C_l1_g0 C_l1_g1 C_l1_g2 C2 C_l2_g0 C_l2_g1 C_l2_g2 C3 C_l3_g0 C_l3_g1 C_l3_g2 R0 R1 R_l0_g0 R_l1_g0 R0C0 R0C1 R0C2 R_l0_g1 R_l1_g1 R1C0 R1C1 R1C2 R_l0_g2 R_l1_g2 R2C0 R2C1 R2C2 R_l0_g3 R_l1_g3 R3C0 R3C1 R3C2 R_l0_g4 R_l1_g4 R4C0 R4C1 R4C2 read_csv is also able to interpret a more common format of multi-columns indices. In [204]: data = ",a,a,a,b,c,c\n,q,r,s,t,u,v\none,1,2,3,4,5,6\ntwo,7,8,9,10,11,12" In [205]: print(data) ,a,a,a,b,c,c ,q,r,s,t,u,v one,1,2,3,4,5,6 two,7,8,9,10,11,12 In [206]: with open("mi2.csv", "w") as fh: .....: fh.write(data) .....: In [207]: pd.read_csv("mi2.csv", header=[0, 1], index_col=0) Out[207]: a b c q r s t u v one 1 2 3 4 5 6 two 7 8 9 10 11 12 Note If an index_col is not specified (e.g. you don’t have an index, or wrote it with df.to_csv(..., index=False), then any names on the columns index will be lost. Automatically “sniffing” the delimiter# read_csv is capable of inferring delimited (not necessarily comma-separated) files, as pandas uses the csv.Sniffer class of the csv module. For this, you have to specify sep=None. In [208]: df = pd.DataFrame(np.random.randn(10, 4)) In [209]: df.to_csv("tmp.csv", sep="|") In [210]: df.to_csv("tmp2.csv", sep=":") In [211]: pd.read_csv("tmp2.csv", sep=None, engine="python") Out[211]: Unnamed: 0 0 1 2 3 0 0 0.469112 -0.282863 -1.509059 -1.135632 1 1 1.212112 -0.173215 0.119209 -1.044236 2 2 -0.861849 -2.104569 -0.494929 1.071804 3 3 0.721555 -0.706771 -1.039575 0.271860 4 4 -0.424972 0.567020 0.276232 -1.087401 5 5 -0.673690 0.113648 -1.478427 0.524988 6 6 0.404705 0.577046 -1.715002 -1.039268 7 7 -0.370647 -1.157892 -1.344312 0.844885 8 8 1.075770 -0.109050 1.643563 -1.469388 9 9 0.357021 -0.674600 -1.776904 -0.968914 Reading multiple files to create a single DataFrame# It’s best to use concat() to combine multiple files. See the cookbook for an example. Iterating through files chunk by chunk# Suppose you wish to iterate through a (potentially very large) file lazily rather than reading the entire file into memory, such as the following: In [212]: df = pd.DataFrame(np.random.randn(10, 4)) In [213]: df.to_csv("tmp.csv", sep="|") In [214]: table = pd.read_csv("tmp.csv", sep="|") In [215]: table Out[215]: Unnamed: 0 0 1 2 3 0 0 -1.294524 0.413738 0.276662 -0.472035 1 1 -0.013960 -0.362543 -0.006154 -0.923061 2 2 0.895717 0.805244 -1.206412 2.565646 3 3 1.431256 1.340309 -1.170299 -0.226169 4 4 0.410835 0.813850 0.132003 -0.827317 5 5 -0.076467 -1.187678 1.130127 -1.436737 6 6 -1.413681 1.607920 1.024180 0.569605 7 7 0.875906 -2.211372 0.974466 -2.006747 8 8 -0.410001 -0.078638 0.545952 -1.219217 9 9 -1.226825 0.769804 -1.281247 -0.727707 By specifying a chunksize to read_csv, the return value will be an iterable object of type TextFileReader: In [216]: with pd.read_csv("tmp.csv", sep="|", chunksize=4) as reader: .....: reader .....: for chunk in reader: .....: print(chunk) .....: Unnamed: 0 0 1 2 3 0 0 -1.294524 0.413738 0.276662 -0.472035 1 1 -0.013960 -0.362543 -0.006154 -0.923061 2 2 0.895717 0.805244 -1.206412 2.565646 3 3 1.431256 1.340309 -1.170299 -0.226169 Unnamed: 0 0 1 2 3 4 4 0.410835 0.813850 0.132003 -0.827317 5 5 -0.076467 -1.187678 1.130127 -1.436737 6 6 -1.413681 1.607920 1.024180 0.569605 7 7 0.875906 -2.211372 0.974466 -2.006747 Unnamed: 0 0 1 2 3 8 8 -0.410001 -0.078638 0.545952 -1.219217 9 9 -1.226825 0.769804 -1.281247 -0.727707 Changed in version 1.2: read_csv/json/sas return a context-manager when iterating through a file. Specifying iterator=True will also return the TextFileReader object: In [217]: with pd.read_csv("tmp.csv", sep="|", iterator=True) as reader: .....: reader.get_chunk(5) .....: Specifying the parser engine# Pandas currently supports three engines, the C engine, the python engine, and an experimental pyarrow engine (requires the pyarrow package). In general, the pyarrow engine is fastest on larger workloads and is equivalent in speed to the C engine on most other workloads. The python engine tends to be slower than the pyarrow and C engines on most workloads. However, the pyarrow engine is much less robust than the C engine, which lacks a few features compared to the Python engine. Where possible, pandas uses the C parser (specified as engine='c'), but it may fall back to Python if C-unsupported options are specified. Currently, options unsupported by the C and pyarrow engines include: sep other than a single character (e.g. regex separators) skipfooter sep=None with delim_whitespace=False Specifying any of the above options will produce a ParserWarning unless the python engine is selected explicitly using engine='python'. Options that are unsupported by the pyarrow engine which are not covered by the list above include: float_precision chunksize comment nrows thousands memory_map dialect warn_bad_lines error_bad_lines on_bad_lines delim_whitespace quoting lineterminator converters decimal iterator dayfirst infer_datetime_format verbose skipinitialspace low_memory Specifying these options with engine='pyarrow' will raise a ValueError. Reading/writing remote files# You can pass in a URL to read or write remote files to many of pandas’ IO functions - the following example shows reading a CSV file: df = pd.read_csv("https://download.bls.gov/pub/time.series/cu/cu.item", sep="\t") New in version 1.3.0. A custom header can be sent alongside HTTP(s) requests by passing a dictionary of header key value mappings to the storage_options keyword argument as shown below: headers = {"User-Agent": "pandas"} df = pd.read_csv( "https://download.bls.gov/pub/time.series/cu/cu.item", sep="\t", storage_options=headers ) All URLs which are not local files or HTTP(s) are handled by fsspec, if installed, and its various filesystem implementations (including Amazon S3, Google Cloud, SSH, FTP, webHDFS…). Some of these implementations will require additional packages to be installed, for example S3 URLs require the s3fs library: df = pd.read_json("s3://pandas-test/adatafile.json") When dealing with remote storage systems, you might need extra configuration with environment variables or config files in special locations. For example, to access data in your S3 bucket, you will need to define credentials in one of the several ways listed in the S3Fs documentation. The same is true for several of the storage backends, and you should follow the links at fsimpl1 for implementations built into fsspec and fsimpl2 for those not included in the main fsspec distribution. You can also pass parameters directly to the backend driver. For example, if you do not have S3 credentials, you can still access public data by specifying an anonymous connection, such as New in version 1.2.0. pd.read_csv( "s3://ncei-wcsd-archive/data/processed/SH1305/18kHz/SaKe2013" "-D20130523-T080854_to_SaKe2013-D20130523-T085643.csv", storage_options={"anon": True}, ) fsspec also allows complex URLs, for accessing data in compressed archives, local caching of files, and more. To locally cache the above example, you would modify the call to pd.read_csv( "simplecache::s3://ncei-wcsd-archive/data/processed/SH1305/18kHz/" "SaKe2013-D20130523-T080854_to_SaKe2013-D20130523-T085643.csv", storage_options={"s3": {"anon": True}}, ) where we specify that the “anon” parameter is meant for the “s3” part of the implementation, not to the caching implementation. Note that this caches to a temporary directory for the duration of the session only, but you can also specify a permanent store. Writing out data# Writing to CSV format# The Series and DataFrame objects have an instance method to_csv which allows storing the contents of the object as a comma-separated-values file. The function takes a number of arguments. Only the first is required. path_or_buf: A string path to the file to write or a file object. If a file object it must be opened with newline='' sep : Field delimiter for the output file (default “,”) na_rep: A string representation of a missing value (default ‘’) float_format: Format string for floating point numbers columns: Columns to write (default None) header: Whether to write out the column names (default True) index: whether to write row (index) names (default True) index_label: Column label(s) for index column(s) if desired. If None (default), and header and index are True, then the index names are used. (A sequence should be given if the DataFrame uses MultiIndex). mode : Python write mode, default ‘w’ encoding: a string representing the encoding to use if the contents are non-ASCII, for Python versions prior to 3 lineterminator: Character sequence denoting line end (default os.linesep) quoting: Set quoting rules as in csv module (default csv.QUOTE_MINIMAL). Note that if you have set a float_format then floats are converted to strings and csv.QUOTE_NONNUMERIC will treat them as non-numeric quotechar: Character used to quote fields (default ‘”’) doublequote: Control quoting of quotechar in fields (default True) escapechar: Character used to escape sep and quotechar when appropriate (default None) chunksize: Number of rows to write at a time date_format: Format string for datetime objects Writing a formatted string# The DataFrame object has an instance method to_string which allows control over the string representation of the object. All arguments are optional: buf default None, for example a StringIO object columns default None, which columns to write col_space default None, minimum width of each column. na_rep default NaN, representation of NA value formatters default None, a dictionary (by column) of functions each of which takes a single argument and returns a formatted string float_format default None, a function which takes a single (float) argument and returns a formatted string; to be applied to floats in the DataFrame. sparsify default True, set to False for a DataFrame with a hierarchical index to print every MultiIndex key at each row. index_names default True, will print the names of the indices index default True, will print the index (ie, row labels) header default True, will print the column labels justify default left, will print column headers left- or right-justified The Series object also has a to_string method, but with only the buf, na_rep, float_format arguments. There is also a length argument which, if set to True, will additionally output the length of the Series. JSON# Read and write JSON format files and strings. Writing JSON# A Series or DataFrame can be converted to a valid JSON string. Use to_json with optional parameters: path_or_buf : the pathname or buffer to write the output This can be None in which case a JSON string is returned orient : Series: default is index allowed values are {split, records, index} DataFrame: default is columns allowed values are {split, records, index, columns, values, table} The format of the JSON string split dict like {index -> [index], columns -> [columns], data -> [values]} records list like [{column -> value}, … , {column -> value}] index dict like {index -> {column -> value}} columns dict like {column -> {index -> value}} values just the values array table adhering to the JSON Table Schema date_format : string, type of date conversion, ‘epoch’ for timestamp, ‘iso’ for ISO8601. double_precision : The number of decimal places to use when encoding floating point values, default 10. force_ascii : force encoded string to be ASCII, default True. date_unit : The time unit to encode to, governs timestamp and ISO8601 precision. One of ‘s’, ‘ms’, ‘us’ or ‘ns’ for seconds, milliseconds, microseconds and nanoseconds respectively. Default ‘ms’. default_handler : The handler to call if an object cannot otherwise be converted to a suitable format for JSON. Takes a single argument, which is the object to convert, and returns a serializable object. lines : If records orient, then will write each record per line as json. Note NaN’s, NaT’s and None will be converted to null and datetime objects will be converted based on the date_format and date_unit parameters. In [218]: dfj = pd.DataFrame(np.random.randn(5, 2), columns=list("AB")) In [219]: json = dfj.to_json() In [220]: json Out[220]: '{"A":{"0":-0.1213062281,"1":0.6957746499,"2":0.9597255933,"3":-0.6199759194,"4":-0.7323393705},"B":{"0":-0.0978826728,"1":0.3417343559,"2":-1.1103361029,"3":0.1497483186,"4":0.6877383895}}' Orient options# There are a number of different options for the format of the resulting JSON file / string. Consider the following DataFrame and Series: In [221]: dfjo = pd.DataFrame( .....: dict(A=range(1, 4), B=range(4, 7), C=range(7, 10)), .....: columns=list("ABC"), .....: index=list("xyz"), .....: ) .....: In [222]: dfjo Out[222]: A B C x 1 4 7 y 2 5 8 z 3 6 9 In [223]: sjo = pd.Series(dict(x=15, y=16, z=17), name="D") In [224]: sjo Out[224]: x 15 y 16 z 17 Name: D, dtype: int64 Column oriented (the default for DataFrame) serializes the data as nested JSON objects with column labels acting as the primary index: In [225]: dfjo.to_json(orient="columns") Out[225]: '{"A":{"x":1,"y":2,"z":3},"B":{"x":4,"y":5,"z":6},"C":{"x":7,"y":8,"z":9}}' # Not available for Series Index oriented (the default for Series) similar to column oriented but the index labels are now primary: In [226]: dfjo.to_json(orient="index") Out[226]: '{"x":{"A":1,"B":4,"C":7},"y":{"A":2,"B":5,"C":8},"z":{"A":3,"B":6,"C":9}}' In [227]: sjo.to_json(orient="index") Out[227]: '{"x":15,"y":16,"z":17}' Record oriented serializes the data to a JSON array of column -> value records, index labels are not included. This is useful for passing DataFrame data to plotting libraries, for example the JavaScript library d3.js: In [228]: dfjo.to_json(orient="records") Out[228]: '[{"A":1,"B":4,"C":7},{"A":2,"B":5,"C":8},{"A":3,"B":6,"C":9}]' In [229]: sjo.to_json(orient="records") Out[229]: '[15,16,17]' Value oriented is a bare-bones option which serializes to nested JSON arrays of values only, column and index labels are not included: In [230]: dfjo.to_json(orient="values") Out[230]: '[[1,4,7],[2,5,8],[3,6,9]]' # Not available for Series Split oriented serializes to a JSON object containing separate entries for values, index and columns. Name is also included for Series: In [231]: dfjo.to_json(orient="split") Out[231]: '{"columns":["A","B","C"],"index":["x","y","z"],"data":[[1,4,7],[2,5,8],[3,6,9]]}' In [232]: sjo.to_json(orient="split") Out[232]: '{"name":"D","index":["x","y","z"],"data":[15,16,17]}' Table oriented serializes to the JSON Table Schema, allowing for the preservation of metadata including but not limited to dtypes and index names. Note Any orient option that encodes to a JSON object will not preserve the ordering of index and column labels during round-trip serialization. If you wish to preserve label ordering use the split option as it uses ordered containers. Date handling# Writing in ISO date format: In [233]: dfd = pd.DataFrame(np.random.randn(5, 2), columns=list("AB")) In [234]: dfd["date"] = pd.Timestamp("20130101") In [235]: dfd = dfd.sort_index(axis=1, ascending=False) In [236]: json = dfd.to_json(date_format="iso") In [237]: json Out[237]: '{"date":{"0":"2013-01-01T00:00:00.000","1":"2013-01-01T00:00:00.000","2":"2013-01-01T00:00:00.000","3":"2013-01-01T00:00:00.000","4":"2013-01-01T00:00:00.000"},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Writing in ISO date format, with microseconds: In [238]: json = dfd.to_json(date_format="iso", date_unit="us") In [239]: json Out[239]: '{"date":{"0":"2013-01-01T00:00:00.000000","1":"2013-01-01T00:00:00.000000","2":"2013-01-01T00:00:00.000000","3":"2013-01-01T00:00:00.000000","4":"2013-01-01T00:00:00.000000"},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Epoch timestamps, in seconds: In [240]: json = dfd.to_json(date_format="epoch", date_unit="s") In [241]: json Out[241]: '{"date":{"0":1356998400,"1":1356998400,"2":1356998400,"3":1356998400,"4":1356998400},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Writing to a file, with a date index and a date column: In [242]: dfj2 = dfj.copy() In [243]: dfj2["date"] = pd.Timestamp("20130101") In [244]: dfj2["ints"] = list(range(5)) In [245]: dfj2["bools"] = True In [246]: dfj2.index = pd.date_range("20130101", periods=5) In [247]: dfj2.to_json("test.json") In [248]: with open("test.json") as fh: .....: print(fh.read()) .....: {"A":{"1356998400000":-0.1213062281,"1357084800000":0.6957746499,"1357171200000":0.9597255933,"1357257600000":-0.6199759194,"1357344000000":-0.7323393705},"B":{"1356998400000":-0.0978826728,"1357084800000":0.3417343559,"1357171200000":-1.1103361029,"1357257600000":0.1497483186,"1357344000000":0.6877383895},"date":{"1356998400000":1356998400000,"1357084800000":1356998400000,"1357171200000":1356998400000,"1357257600000":1356998400000,"1357344000000":1356998400000},"ints":{"1356998400000":0,"1357084800000":1,"1357171200000":2,"1357257600000":3,"1357344000000":4},"bools":{"1356998400000":true,"1357084800000":true,"1357171200000":true,"1357257600000":true,"1357344000000":true}} Fallback behavior# If the JSON serializer cannot handle the container contents directly it will fall back in the following manner: if the dtype is unsupported (e.g. np.complex_) then the default_handler, if provided, will be called for each value, otherwise an exception is raised. if an object is unsupported it will attempt the following: check if the object has defined a toDict method and call it. A toDict method should return a dict which will then be JSON serialized. invoke the default_handler if one was provided. convert the object to a dict by traversing its contents. However this will often fail with an OverflowError or give unexpected results. In general the best approach for unsupported objects or dtypes is to provide a default_handler. For example: >>> DataFrame([1.0, 2.0, complex(1.0, 2.0)]).to_json() # raises RuntimeError: Unhandled numpy dtype 15 can be dealt with by specifying a simple default_handler: In [249]: pd.DataFrame([1.0, 2.0, complex(1.0, 2.0)]).to_json(default_handler=str) Out[249]: '{"0":{"0":"(1+0j)","1":"(2+0j)","2":"(1+2j)"}}' Reading JSON# Reading a JSON string to pandas object can take a number of parameters. The parser will try to parse a DataFrame if typ is not supplied or is None. To explicitly force Series parsing, pass typ=series filepath_or_buffer : a VALID JSON string or file handle / StringIO. The string could be a URL. Valid URL schemes include http, ftp, S3, and file. For file URLs, a host is expected. For instance, a local file could be file ://localhost/path/to/table.json typ : type of object to recover (series or frame), default ‘frame’ orient : Series : default is index allowed values are {split, records, index} DataFrame default is columns allowed values are {split, records, index, columns, values, table} The format of the JSON string split dict like {index -> [index], columns -> [columns], data -> [values]} records list like [{column -> value}, … , {column -> value}] index dict like {index -> {column -> value}} columns dict like {column -> {index -> value}} values just the values array table adhering to the JSON Table Schema dtype : if True, infer dtypes, if a dict of column to dtype, then use those, if False, then don’t infer dtypes at all, default is True, apply only to the data. convert_axes : boolean, try to convert the axes to the proper dtypes, default is True convert_dates : a list of columns to parse for dates; If True, then try to parse date-like columns, default is True. keep_default_dates : boolean, default True. If parsing dates, then parse the default date-like columns. numpy : direct decoding to NumPy arrays. default is False; Supports numeric data only, although labels may be non-numeric. Also note that the JSON ordering MUST be the same for each term if numpy=True. precise_float : boolean, default False. Set to enable usage of higher precision (strtod) function when decoding string to double values. Default (False) is to use fast but less precise builtin functionality. date_unit : string, the timestamp unit to detect if converting dates. Default None. By default the timestamp precision will be detected, if this is not desired then pass one of ‘s’, ‘ms’, ‘us’ or ‘ns’ to force timestamp precision to seconds, milliseconds, microseconds or nanoseconds respectively. lines : reads file as one json object per line. encoding : The encoding to use to decode py3 bytes. chunksize : when used in combination with lines=True, return a JsonReader which reads in chunksize lines per iteration. The parser will raise one of ValueError/TypeError/AssertionError if the JSON is not parseable. If a non-default orient was used when encoding to JSON be sure to pass the same option here so that decoding produces sensible results, see Orient Options for an overview. Data conversion# The default of convert_axes=True, dtype=True, and convert_dates=True will try to parse the axes, and all of the data into appropriate types, including dates. If you need to override specific dtypes, pass a dict to dtype. convert_axes should only be set to False if you need to preserve string-like numbers (e.g. ‘1’, ‘2’) in an axes. Note Large integer values may be converted to dates if convert_dates=True and the data and / or column labels appear ‘date-like’. The exact threshold depends on the date_unit specified. ‘date-like’ means that the column label meets one of the following criteria: it ends with '_at' it ends with '_time' it begins with 'timestamp' it is 'modified' it is 'date' Warning When reading JSON data, automatic coercing into dtypes has some quirks: an index can be reconstructed in a different order from serialization, that is, the returned order is not guaranteed to be the same as before serialization a column that was float data will be converted to integer if it can be done safely, e.g. a column of 1. bool columns will be converted to integer on reconstruction Thus there are times where you may want to specify specific dtypes via the dtype keyword argument. Reading from a JSON string: In [250]: pd.read_json(json) Out[250]: date B A 0 2013-01-01 0.403310 0.176444 1 2013-01-01 0.301624 -0.154951 2 2013-01-01 -1.369849 -2.179861 3 2013-01-01 1.462696 -0.954208 4 2013-01-01 -0.826591 -1.743161 Reading from a file: In [251]: pd.read_json("test.json") Out[251]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True Don’t convert any data (but still convert axes and dates): In [252]: pd.read_json("test.json", dtype=object).dtypes Out[252]: A object B object date object ints object bools object dtype: object Specify dtypes for conversion: In [253]: pd.read_json("test.json", dtype={"A": "float32", "bools": "int8"}).dtypes Out[253]: A float32 B float64 date datetime64[ns] ints int64 bools int8 dtype: object Preserve string indices: In [254]: si = pd.DataFrame( .....: np.zeros((4, 4)), columns=list(range(4)), index=[str(i) for i in range(4)] .....: ) .....: In [255]: si Out[255]: 0 1 2 3 0 0.0 0.0 0.0 0.0 1 0.0 0.0 0.0 0.0 2 0.0 0.0 0.0 0.0 3 0.0 0.0 0.0 0.0 In [256]: si.index Out[256]: Index(['0', '1', '2', '3'], dtype='object') In [257]: si.columns Out[257]: Int64Index([0, 1, 2, 3], dtype='int64') In [258]: json = si.to_json() In [259]: sij = pd.read_json(json, convert_axes=False) In [260]: sij Out[260]: 0 1 2 3 0 0 0 0 0 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 In [261]: sij.index Out[261]: Index(['0', '1', '2', '3'], dtype='object') In [262]: sij.columns Out[262]: Index(['0', '1', '2', '3'], dtype='object') Dates written in nanoseconds need to be read back in nanoseconds: In [263]: json = dfj2.to_json(date_unit="ns") # Try to parse timestamps as milliseconds -> Won't Work In [264]: dfju = pd.read_json(json, date_unit="ms") In [265]: dfju Out[265]: A B date ints bools 1356998400000000000 -0.121306 -0.097883 1356998400000000000 0 True 1357084800000000000 0.695775 0.341734 1356998400000000000 1 True 1357171200000000000 0.959726 -1.110336 1356998400000000000 2 True 1357257600000000000 -0.619976 0.149748 1356998400000000000 3 True 1357344000000000000 -0.732339 0.687738 1356998400000000000 4 True # Let pandas detect the correct precision In [266]: dfju = pd.read_json(json) In [267]: dfju Out[267]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True # Or specify that all timestamps are in nanoseconds In [268]: dfju = pd.read_json(json, date_unit="ns") In [269]: dfju Out[269]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True The Numpy parameter# Note This param has been deprecated as of version 1.0.0 and will raise a FutureWarning. This supports numeric data only. Index and columns labels may be non-numeric, e.g. strings, dates etc. If numpy=True is passed to read_json an attempt will be made to sniff an appropriate dtype during deserialization and to subsequently decode directly to NumPy arrays, bypassing the need for intermediate Python objects. This can provide speedups if you are deserialising a large amount of numeric data: In [270]: randfloats = np.random.uniform(-100, 1000, 10000) In [271]: randfloats.shape = (1000, 10) In [272]: dffloats = pd.DataFrame(randfloats, columns=list("ABCDEFGHIJ")) In [273]: jsonfloats = dffloats.to_json() In [274]: %timeit pd.read_json(jsonfloats) 7.91 ms +- 77.3 us per loop (mean +- std. dev. of 7 runs, 100 loops each) In [275]: %timeit pd.read_json(jsonfloats, numpy=True) 5.71 ms +- 333 us per loop (mean +- std. dev. of 7 runs, 100 loops each) The speedup is less noticeable for smaller datasets: In [276]: jsonfloats = dffloats.head(100).to_json() In [277]: %timeit pd.read_json(jsonfloats) 4.46 ms +- 25.9 us per loop (mean +- std. dev. of 7 runs, 100 loops each) In [278]: %timeit pd.read_json(jsonfloats, numpy=True) 4.09 ms +- 32.3 us per loop (mean +- std. dev. of 7 runs, 100 loops each) Warning Direct NumPy decoding makes a number of assumptions and may fail or produce unexpected output if these assumptions are not satisfied: data is numeric. data is uniform. The dtype is sniffed from the first value decoded. A ValueError may be raised, or incorrect output may be produced if this condition is not satisfied. labels are ordered. Labels are only read from the first container, it is assumed that each subsequent row / column has been encoded in the same order. This should be satisfied if the data was encoded using to_json but may not be the case if the JSON is from another source. Normalization# pandas provides a utility function to take a dict or list of dicts and normalize this semi-structured data into a flat table. In [279]: data = [ .....: {"id": 1, "name": {"first": "Coleen", "last": "Volk"}}, .....: {"name": {"given": "Mark", "family": "Regner"}}, .....: {"id": 2, "name": "Faye Raker"}, .....: ] .....: In [280]: pd.json_normalize(data) Out[280]: id name.first name.last name.given name.family name 0 1.0 Coleen Volk NaN NaN NaN 1 NaN NaN NaN Mark Regner NaN 2 2.0 NaN NaN NaN NaN Faye Raker In [281]: data = [ .....: { .....: "state": "Florida", .....: "shortname": "FL", .....: "info": {"governor": "Rick Scott"}, .....: "county": [ .....: {"name": "Dade", "population": 12345}, .....: {"name": "Broward", "population": 40000}, .....: {"name": "Palm Beach", "population": 60000}, .....: ], .....: }, .....: { .....: "state": "Ohio", .....: "shortname": "OH", .....: "info": {"governor": "John Kasich"}, .....: "county": [ .....: {"name": "Summit", "population": 1234}, .....: {"name": "Cuyahoga", "population": 1337}, .....: ], .....: }, .....: ] .....: In [282]: pd.json_normalize(data, "county", ["state", "shortname", ["info", "governor"]]) Out[282]: name population state shortname info.governor 0 Dade 12345 Florida FL Rick Scott 1 Broward 40000 Florida FL Rick Scott 2 Palm Beach 60000 Florida FL Rick Scott 3 Summit 1234 Ohio OH John Kasich 4 Cuyahoga 1337 Ohio OH John Kasich The max_level parameter provides more control over which level to end normalization. With max_level=1 the following snippet normalizes until 1st nesting level of the provided dict. In [283]: data = [ .....: { .....: "CreatedBy": {"Name": "User001"}, .....: "Lookup": { .....: "TextField": "Some text", .....: "UserField": {"Id": "ID001", "Name": "Name001"}, .....: }, .....: "Image": {"a": "b"}, .....: } .....: ] .....: In [284]: pd.json_normalize(data, max_level=1) Out[284]: CreatedBy.Name Lookup.TextField Lookup.UserField Image.a 0 User001 Some text {'Id': 'ID001', 'Name': 'Name001'} b Line delimited json# pandas is able to read and write line-delimited json files that are common in data processing pipelines using Hadoop or Spark. For line-delimited json files, pandas can also return an iterator which reads in chunksize lines at a time. This can be useful for large files or to read from a stream. In [285]: jsonl = """ .....: {"a": 1, "b": 2} .....: {"a": 3, "b": 4} .....: """ .....: In [286]: df = pd.read_json(jsonl, lines=True) In [287]: df Out[287]: a b 0 1 2 1 3 4 In [288]: df.to_json(orient="records", lines=True) Out[288]: '{"a":1,"b":2}\n{"a":3,"b":4}\n' # reader is an iterator that returns ``chunksize`` lines each iteration In [289]: with pd.read_json(StringIO(jsonl), lines=True, chunksize=1) as reader: .....: reader .....: for chunk in reader: .....: print(chunk) .....: Empty DataFrame Columns: [] Index: [] a b 0 1 2 a b 1 3 4 Table schema# Table Schema is a spec for describing tabular datasets as a JSON object. The JSON includes information on the field names, types, and other attributes. You can use the orient table to build a JSON string with two fields, schema and data. In [290]: df = pd.DataFrame( .....: { .....: "A": [1, 2, 3], .....: "B": ["a", "b", "c"], .....: "C": pd.date_range("2016-01-01", freq="d", periods=3), .....: }, .....: index=pd.Index(range(3), name="idx"), .....: ) .....: In [291]: df Out[291]: A B C idx 0 1 a 2016-01-01 1 2 b 2016-01-02 2 3 c 2016-01-03 In [292]: df.to_json(orient="table", date_format="iso") Out[292]: '{"schema":{"fields":[{"name":"idx","type":"integer"},{"name":"A","type":"integer"},{"name":"B","type":"string"},{"name":"C","type":"datetime"}],"primaryKey":["idx"],"pandas_version":"1.4.0"},"data":[{"idx":0,"A":1,"B":"a","C":"2016-01-01T00:00:00.000"},{"idx":1,"A":2,"B":"b","C":"2016-01-02T00:00:00.000"},{"idx":2,"A":3,"B":"c","C":"2016-01-03T00:00:00.000"}]}' The schema field contains the fields key, which itself contains a list of column name to type pairs, including the Index or MultiIndex (see below for a list of types). The schema field also contains a primaryKey field if the (Multi)index is unique. The second field, data, contains the serialized data with the records orient. The index is included, and any datetimes are ISO 8601 formatted, as required by the Table Schema spec. The full list of types supported are described in the Table Schema spec. This table shows the mapping from pandas types: pandas type Table Schema type int64 integer float64 number bool boolean datetime64[ns] datetime timedelta64[ns] duration categorical any object str A few notes on the generated table schema: The schema object contains a pandas_version field. This contains the version of pandas’ dialect of the schema, and will be incremented with each revision. All dates are converted to UTC when serializing. Even timezone naive values, which are treated as UTC with an offset of 0. In [293]: from pandas.io.json import build_table_schema In [294]: s = pd.Series(pd.date_range("2016", periods=4)) In [295]: build_table_schema(s) Out[295]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'datetime'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} datetimes with a timezone (before serializing), include an additional field tz with the time zone name (e.g. 'US/Central'). In [296]: s_tz = pd.Series(pd.date_range("2016", periods=12, tz="US/Central")) In [297]: build_table_schema(s_tz) Out[297]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'datetime', 'tz': 'US/Central'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} Periods are converted to timestamps before serialization, and so have the same behavior of being converted to UTC. In addition, periods will contain and additional field freq with the period’s frequency, e.g. 'A-DEC'. In [298]: s_per = pd.Series(1, index=pd.period_range("2016", freq="A-DEC", periods=4)) In [299]: build_table_schema(s_per) Out[299]: {'fields': [{'name': 'index', 'type': 'datetime', 'freq': 'A-DEC'}, {'name': 'values', 'type': 'integer'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} Categoricals use the any type and an enum constraint listing the set of possible values. Additionally, an ordered field is included: In [300]: s_cat = pd.Series(pd.Categorical(["a", "b", "a"])) In [301]: build_table_schema(s_cat) Out[301]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'any', 'constraints': {'enum': ['a', 'b']}, 'ordered': False}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} A primaryKey field, containing an array of labels, is included if the index is unique: In [302]: s_dupe = pd.Series([1, 2], index=[1, 1]) In [303]: build_table_schema(s_dupe) Out[303]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'integer'}], 'pandas_version': '1.4.0'} The primaryKey behavior is the same with MultiIndexes, but in this case the primaryKey is an array: In [304]: s_multi = pd.Series(1, index=pd.MultiIndex.from_product([("a", "b"), (0, 1)])) In [305]: build_table_schema(s_multi) Out[305]: {'fields': [{'name': 'level_0', 'type': 'string'}, {'name': 'level_1', 'type': 'integer'}, {'name': 'values', 'type': 'integer'}], 'primaryKey': FrozenList(['level_0', 'level_1']), 'pandas_version': '1.4.0'} The default naming roughly follows these rules: For series, the object.name is used. If that’s none, then the name is values For DataFrames, the stringified version of the column name is used For Index (not MultiIndex), index.name is used, with a fallback to index if that is None. For MultiIndex, mi.names is used. If any level has no name, then level_<i> is used. read_json also accepts orient='table' as an argument. This allows for the preservation of metadata such as dtypes and index names in a round-trippable manner. In [306]: df = pd.DataFrame( .....: { .....: "foo": [1, 2, 3, 4], .....: "bar": ["a", "b", "c", "d"], .....: "baz": pd.date_range("2018-01-01", freq="d", periods=4), .....: "qux": pd.Categorical(["a", "b", "c", "c"]), .....: }, .....: index=pd.Index(range(4), name="idx"), .....: ) .....: In [307]: df Out[307]: foo bar baz qux idx 0 1 a 2018-01-01 a 1 2 b 2018-01-02 b 2 3 c 2018-01-03 c 3 4 d 2018-01-04 c In [308]: df.dtypes Out[308]: foo int64 bar object baz datetime64[ns] qux category dtype: object In [309]: df.to_json("test.json", orient="table") In [310]: new_df = pd.read_json("test.json", orient="table") In [311]: new_df Out[311]: foo bar baz qux idx 0 1 a 2018-01-01 a 1 2 b 2018-01-02 b 2 3 c 2018-01-03 c 3 4 d 2018-01-04 c In [312]: new_df.dtypes Out[312]: foo int64 bar object baz datetime64[ns] qux category dtype: object Please note that the literal string ‘index’ as the name of an Index is not round-trippable, nor are any names beginning with 'level_' within a MultiIndex. These are used by default in DataFrame.to_json() to indicate missing values and the subsequent read cannot distinguish the intent. In [313]: df.index.name = "index" In [314]: df.to_json("test.json", orient="table") In [315]: new_df = pd.read_json("test.json", orient="table") In [316]: print(new_df.index.name) None When using orient='table' along with user-defined ExtensionArray, the generated schema will contain an additional extDtype key in the respective fields element. This extra key is not standard but does enable JSON roundtrips for extension types (e.g. read_json(df.to_json(orient="table"), orient="table")). The extDtype key carries the name of the extension, if you have properly registered the ExtensionDtype, pandas will use said name to perform a lookup into the registry and re-convert the serialized data into your custom dtype. HTML# Reading HTML content# Warning We highly encourage you to read the HTML Table Parsing gotchas below regarding the issues surrounding the BeautifulSoup4/html5lib/lxml parsers. The top-level read_html() function can accept an HTML string/file/URL and will parse HTML tables into list of pandas DataFrames. Let’s look at a few examples. Note read_html returns a list of DataFrame objects, even if there is only a single table contained in the HTML content. Read a URL with no options: In [320]: "https://www.fdic.gov/resources/resolutions/bank-failures/failed-bank-list" In [321]: pd.read_html(url) Out[321]: [ Bank NameBank CityCity StateSt ... Acquiring InstitutionAI Closing DateClosing FundFund 0 Almena State Bank Almena KS ... Equity Bank October 23, 2020 10538 1 First City Bank of Florida Fort Walton Beach FL ... United Fidelity Bank, fsb October 16, 2020 10537 2 The First State Bank Barboursville WV ... MVB Bank, Inc. April 3, 2020 10536 3 Ericson State Bank Ericson NE ... Farmers and Merchants Bank February 14, 2020 10535 4 City National Bank of New Jersey Newark NJ ... Industrial Bank November 1, 2019 10534 .. ... ... ... ... ... ... ... 558 Superior Bank, FSB Hinsdale IL ... Superior Federal, FSB July 27, 2001 6004 559 Malta National Bank Malta OH ... North Valley Bank May 3, 2001 4648 560 First Alliance Bank & Trust Co. Manchester NH ... Southern New Hampshire Bank & Trust February 2, 2001 4647 561 National State Bank of Metropolis Metropolis IL ... Banterra Bank of Marion December 14, 2000 4646 562 Bank of Honolulu Honolulu HI ... Bank of the Orient October 13, 2000 4645 [563 rows x 7 columns]] Note The data from the above URL changes every Monday so the resulting data above may be slightly different. Read in the content of the file from the above URL and pass it to read_html as a string: In [317]: html_str = """ .....: <table> .....: <tr> .....: <th>A</th> .....: <th colspan="1">B</th> .....: <th rowspan="1">C</th> .....: </tr> .....: <tr> .....: <td>a</td> .....: <td>b</td> .....: <td>c</td> .....: </tr> .....: </table> .....: """ .....: In [318]: with open("tmp.html", "w") as f: .....: f.write(html_str) .....: In [319]: df = pd.read_html("tmp.html") In [320]: df[0] Out[320]: A B C 0 a b c You can even pass in an instance of StringIO if you so desire: In [321]: dfs = pd.read_html(StringIO(html_str)) In [322]: dfs[0] Out[322]: A B C 0 a b c Note The following examples are not run by the IPython evaluator due to the fact that having so many network-accessing functions slows down the documentation build. If you spot an error or an example that doesn’t run, please do not hesitate to report it over on pandas GitHub issues page. Read a URL and match a table that contains specific text: match = "Metcalf Bank" df_list = pd.read_html(url, match=match) Specify a header row (by default <th> or <td> elements located within a <thead> are used to form the column index, if multiple rows are contained within <thead> then a MultiIndex is created); if specified, the header row is taken from the data minus the parsed header elements (<th> elements). dfs = pd.read_html(url, header=0) Specify an index column: dfs = pd.read_html(url, index_col=0) Specify a number of rows to skip: dfs = pd.read_html(url, skiprows=0) Specify a number of rows to skip using a list (range works as well): dfs = pd.read_html(url, skiprows=range(2)) Specify an HTML attribute: dfs1 = pd.read_html(url, attrs={"id": "table"}) dfs2 = pd.read_html(url, attrs={"class": "sortable"}) print(np.array_equal(dfs1[0], dfs2[0])) # Should be True Specify values that should be converted to NaN: dfs = pd.read_html(url, na_values=["No Acquirer"]) Specify whether to keep the default set of NaN values: dfs = pd.read_html(url, keep_default_na=False) Specify converters for columns. This is useful for numerical text data that has leading zeros. By default columns that are numerical are cast to numeric types and the leading zeros are lost. To avoid this, we can convert these columns to strings. url_mcc = "https://en.wikipedia.org/wiki/Mobile_country_code" dfs = pd.read_html( url_mcc, match="Telekom Albania", header=0, converters={"MNC": str}, ) Use some combination of the above: dfs = pd.read_html(url, match="Metcalf Bank", index_col=0) Read in pandas to_html output (with some loss of floating point precision): df = pd.DataFrame(np.random.randn(2, 2)) s = df.to_html(float_format="{0:.40g}".format) dfin = pd.read_html(s, index_col=0) The lxml backend will raise an error on a failed parse if that is the only parser you provide. If you only have a single parser you can provide just a string, but it is considered good practice to pass a list with one string if, for example, the function expects a sequence of strings. You may use: dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor=["lxml"]) Or you could pass flavor='lxml' without a list: dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor="lxml") However, if you have bs4 and html5lib installed and pass None or ['lxml', 'bs4'] then the parse will most likely succeed. Note that as soon as a parse succeeds, the function will return. dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor=["lxml", "bs4"]) Links can be extracted from cells along with the text using extract_links="all". In [323]: html_table = """ .....: <table> .....: <tr> .....: <th>GitHub</th> .....: </tr> .....: <tr> .....: <td><a href="https://github.com/pandas-dev/pandas">pandas</a></td> .....: </tr> .....: </table> .....: """ .....: In [324]: df = pd.read_html( .....: html_table, .....: extract_links="all" .....: )[0] .....: In [325]: df Out[325]: (GitHub, None) 0 (pandas, https://github.com/pandas-dev/pandas) In [326]: df[("GitHub", None)] Out[326]: 0 (pandas, https://github.com/pandas-dev/pandas) Name: (GitHub, None), dtype: object In [327]: df[("GitHub", None)].str[1] Out[327]: 0 https://github.com/pandas-dev/pandas Name: (GitHub, None), dtype: object New in version 1.5.0. Writing to HTML files# DataFrame objects have an instance method to_html which renders the contents of the DataFrame as an HTML table. The function arguments are as in the method to_string described above. Note Not all of the possible options for DataFrame.to_html are shown here for brevity’s sake. See to_html() for the full set of options. Note In an HTML-rendering supported environment like a Jupyter Notebook, display(HTML(...))` will render the raw HTML into the environment. In [328]: from IPython.display import display, HTML In [329]: df = pd.DataFrame(np.random.randn(2, 2)) In [330]: df Out[330]: 0 1 0 0.070319 1.773907 1 0.253908 0.414581 In [331]: html = df.to_html() In [332]: print(html) # raw html <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <th>1</th> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> In [333]: display(HTML(html)) <IPython.core.display.HTML object> The columns argument will limit the columns shown: In [334]: html = df.to_html(columns=[0]) In [335]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> </tr> <tr> <th>1</th> <td>0.253908</td> </tr> </tbody> </table> In [336]: display(HTML(html)) <IPython.core.display.HTML object> float_format takes a Python callable to control the precision of floating point values: In [337]: html = df.to_html(float_format="{0:.10f}".format) In [338]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.0703192665</td> <td>1.7739074228</td> </tr> <tr> <th>1</th> <td>0.2539083433</td> <td>0.4145805920</td> </tr> </tbody> </table> In [339]: display(HTML(html)) <IPython.core.display.HTML object> bold_rows will make the row labels bold by default, but you can turn that off: In [340]: html = df.to_html(bold_rows=False) In [341]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <td>1</td> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> In [342]: display(HTML(html)) <IPython.core.display.HTML object> The classes argument provides the ability to give the resulting HTML table CSS classes. Note that these classes are appended to the existing 'dataframe' class. In [343]: print(df.to_html(classes=["awesome_table_class", "even_more_awesome_class"])) <table border="1" class="dataframe awesome_table_class even_more_awesome_class"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <th>1</th> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> The render_links argument provides the ability to add hyperlinks to cells that contain URLs. In [344]: url_df = pd.DataFrame( .....: { .....: "name": ["Python", "pandas"], .....: "url": ["https://www.python.org/", "https://pandas.pydata.org"], .....: } .....: ) .....: In [345]: html = url_df.to_html(render_links=True) In [346]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>name</th> <th>url</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>Python</td> <td><a href="https://www.python.org/" target="_blank">https://www.python.org/</a></td> </tr> <tr> <th>1</th> <td>pandas</td> <td><a href="https://pandas.pydata.org" target="_blank">https://pandas.pydata.org</a></td> </tr> </tbody> </table> In [347]: display(HTML(html)) <IPython.core.display.HTML object> Finally, the escape argument allows you to control whether the “<”, “>” and “&” characters escaped in the resulting HTML (by default it is True). So to get the HTML without escaped characters pass escape=False In [348]: df = pd.DataFrame({"a": list("&<>"), "b": np.random.randn(3)}) Escaped: In [349]: html = df.to_html() In [350]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>a</th> <th>b</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>&amp;</td> <td>0.842321</td> </tr> <tr> <th>1</th> <td>&lt;</td> <td>0.211337</td> </tr> <tr> <th>2</th> <td>&gt;</td> <td>-1.055427</td> </tr> </tbody> </table> In [351]: display(HTML(html)) <IPython.core.display.HTML object> Not escaped: In [352]: html = df.to_html(escape=False) In [353]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>a</th> <th>b</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>&</td> <td>0.842321</td> </tr> <tr> <th>1</th> <td><</td> <td>0.211337</td> </tr> <tr> <th>2</th> <td>></td> <td>-1.055427</td> </tr> </tbody> </table> In [354]: display(HTML(html)) <IPython.core.display.HTML object> Note Some browsers may not show a difference in the rendering of the previous two HTML tables. HTML Table Parsing Gotchas# There are some versioning issues surrounding the libraries that are used to parse HTML tables in the top-level pandas io function read_html. Issues with lxml Benefits lxml is very fast. lxml requires Cython to install correctly. Drawbacks lxml does not make any guarantees about the results of its parse unless it is given strictly valid markup. In light of the above, we have chosen to allow you, the user, to use the lxml backend, but this backend will use html5lib if lxml fails to parse It is therefore highly recommended that you install both BeautifulSoup4 and html5lib, so that you will still get a valid result (provided everything else is valid) even if lxml fails. Issues with BeautifulSoup4 using lxml as a backend The above issues hold here as well since BeautifulSoup4 is essentially just a wrapper around a parser backend. Issues with BeautifulSoup4 using html5lib as a backend Benefits html5lib is far more lenient than lxml and consequently deals with real-life markup in a much saner way rather than just, e.g., dropping an element without notifying you. html5lib generates valid HTML5 markup from invalid markup automatically. This is extremely important for parsing HTML tables, since it guarantees a valid document. However, that does NOT mean that it is “correct”, since the process of fixing markup does not have a single definition. html5lib is pure Python and requires no additional build steps beyond its own installation. Drawbacks The biggest drawback to using html5lib is that it is slow as molasses. However consider the fact that many tables on the web are not big enough for the parsing algorithm runtime to matter. It is more likely that the bottleneck will be in the process of reading the raw text from the URL over the web, i.e., IO (input-output). For very large tables, this might not be true. LaTeX# New in version 1.3.0. Currently there are no methods to read from LaTeX, only output methods. Writing to LaTeX files# Note DataFrame and Styler objects currently have a to_latex method. We recommend using the Styler.to_latex() method over DataFrame.to_latex() due to the former’s greater flexibility with conditional styling, and the latter’s possible future deprecation. Review the documentation for Styler.to_latex, which gives examples of conditional styling and explains the operation of its keyword arguments. For simple application the following pattern is sufficient. In [355]: df = pd.DataFrame([[1, 2], [3, 4]], index=["a", "b"], columns=["c", "d"]) In [356]: print(df.style.to_latex()) \begin{tabular}{lrr} & c & d \\ a & 1 & 2 \\ b & 3 & 4 \\ \end{tabular} To format values before output, chain the Styler.format method. In [357]: print(df.style.format("€ {}").to_latex()) \begin{tabular}{lrr} & c & d \\ a & € 1 & € 2 \\ b & € 3 & € 4 \\ \end{tabular} XML# Reading XML# New in version 1.3.0. The top-level read_xml() function can accept an XML string/file/URL and will parse nodes and attributes into a pandas DataFrame. Note Since there is no standard XML structure where design types can vary in many ways, read_xml works best with flatter, shallow versions. If an XML document is deeply nested, use the stylesheet feature to transform XML into a flatter version. Let’s look at a few examples. Read an XML string: In [358]: xml = """<?xml version="1.0" encoding="UTF-8"?> .....: <bookstore> .....: <book category="cooking"> .....: <title lang="en">Everyday Italian</title> .....: <author>Giada De Laurentiis</author> .....: <year>2005</year> .....: <price>30.00</price> .....: </book> .....: <book category="children"> .....: <title lang="en">Harry Potter</title> .....: <author>J K. Rowling</author> .....: <year>2005</year> .....: <price>29.99</price> .....: </book> .....: <book category="web"> .....: <title lang="en">Learning XML</title> .....: <author>Erik T. Ray</author> .....: <year>2003</year> .....: <price>39.95</price> .....: </book> .....: </bookstore>""" .....: In [359]: df = pd.read_xml(xml) In [360]: df Out[360]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Read a URL with no options: In [361]: df = pd.read_xml("https://www.w3schools.com/xml/books.xml") In [362]: df Out[362]: category title author year price cover 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 None 1 children Harry Potter J K. Rowling 2005 29.99 None 2 web XQuery Kick Start Vaidyanathan Nagarajan 2003 49.99 None 3 web Learning XML Erik T. Ray 2003 39.95 paperback Read in the content of the “books.xml” file and pass it to read_xml as a string: In [363]: file_path = "books.xml" In [364]: with open(file_path, "w") as f: .....: f.write(xml) .....: In [365]: with open(file_path, "r") as f: .....: df = pd.read_xml(f.read()) .....: In [366]: df Out[366]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Read in the content of the “books.xml” as instance of StringIO or BytesIO and pass it to read_xml: In [367]: with open(file_path, "r") as f: .....: sio = StringIO(f.read()) .....: In [368]: df = pd.read_xml(sio) In [369]: df Out[369]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 In [370]: with open(file_path, "rb") as f: .....: bio = BytesIO(f.read()) .....: In [371]: df = pd.read_xml(bio) In [372]: df Out[372]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Even read XML from AWS S3 buckets such as NIH NCBI PMC Article Datasets providing Biomedical and Life Science Jorurnals: In [373]: df = pd.read_xml( .....: "s3://pmc-oa-opendata/oa_comm/xml/all/PMC1236943.xml", .....: xpath=".//journal-meta", .....: ) .....: In [374]: df Out[374]: journal-id journal-title issn publisher 0 Cardiovasc Ultrasound Cardiovascular Ultrasound 1476-7120 NaN With lxml as default parser, you access the full-featured XML library that extends Python’s ElementTree API. One powerful tool is ability to query nodes selectively or conditionally with more expressive XPath: In [375]: df = pd.read_xml(file_path, xpath="//book[year=2005]") In [376]: df Out[376]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 Specify only elements or only attributes to parse: In [377]: df = pd.read_xml(file_path, elems_only=True) In [378]: df Out[378]: title author year price 0 Everyday Italian Giada De Laurentiis 2005 30.00 1 Harry Potter J K. Rowling 2005 29.99 2 Learning XML Erik T. Ray 2003 39.95 In [379]: df = pd.read_xml(file_path, attrs_only=True) In [380]: df Out[380]: category 0 cooking 1 children 2 web XML documents can have namespaces with prefixes and default namespaces without prefixes both of which are denoted with a special attribute xmlns. In order to parse by node under a namespace context, xpath must reference a prefix. For example, below XML contains a namespace with prefix, doc, and URI at https://example.com. In order to parse doc:row nodes, namespaces must be used. In [381]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <doc:data xmlns:doc="https://example.com"> .....: <doc:row> .....: <doc:shape>square</doc:shape> .....: <doc:degrees>360</doc:degrees> .....: <doc:sides>4.0</doc:sides> .....: </doc:row> .....: <doc:row> .....: <doc:shape>circle</doc:shape> .....: <doc:degrees>360</doc:degrees> .....: <doc:sides/> .....: </doc:row> .....: <doc:row> .....: <doc:shape>triangle</doc:shape> .....: <doc:degrees>180</doc:degrees> .....: <doc:sides>3.0</doc:sides> .....: </doc:row> .....: </doc:data>""" .....: In [382]: df = pd.read_xml(xml, .....: xpath="//doc:row", .....: namespaces={"doc": "https://example.com"}) .....: In [383]: df Out[383]: shape degrees sides 0 square 360 4.0 1 circle 360 NaN 2 triangle 180 3.0 Similarly, an XML document can have a default namespace without prefix. Failing to assign a temporary prefix will return no nodes and raise a ValueError. But assigning any temporary name to correct URI allows parsing by nodes. In [384]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <data xmlns="https://example.com"> .....: <row> .....: <shape>square</shape> .....: <degrees>360</degrees> .....: <sides>4.0</sides> .....: </row> .....: <row> .....: <shape>circle</shape> .....: <degrees>360</degrees> .....: <sides/> .....: </row> .....: <row> .....: <shape>triangle</shape> .....: <degrees>180</degrees> .....: <sides>3.0</sides> .....: </row> .....: </data>""" .....: In [385]: df = pd.read_xml(xml, .....: xpath="//pandas:row", .....: namespaces={"pandas": "https://example.com"}) .....: In [386]: df Out[386]: shape degrees sides 0 square 360 4.0 1 circle 360 NaN 2 triangle 180 3.0 However, if XPath does not reference node names such as default, /*, then namespaces is not required. With lxml as parser, you can flatten nested XML documents with an XSLT script which also can be string/file/URL types. As background, XSLT is a special-purpose language written in a special XML file that can transform original XML documents into other XML, HTML, even text (CSV, JSON, etc.) using an XSLT processor. For example, consider this somewhat nested structure of Chicago “L” Rides where station and rides elements encapsulate data in their own sections. With below XSLT, lxml can transform original nested document into a flatter output (as shown below for demonstration) for easier parse into DataFrame: In [387]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <response> .....: <row> .....: <station id="40850" name="Library"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>864.2</avg_weekday_rides> .....: <avg_saturday_rides>534</avg_saturday_rides> .....: <avg_sunday_holiday_rides>417.2</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: <row> .....: <station id="41700" name="Washington/Wabash"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>2707.4</avg_weekday_rides> .....: <avg_saturday_rides>1909.8</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1438.6</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: <row> .....: <station id="40380" name="Clark/Lake"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>2949.6</avg_weekday_rides> .....: <avg_saturday_rides>1657</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1453.8</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: </response>""" .....: In [388]: xsl = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> .....: <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/> .....: <xsl:strip-space elements="*"/> .....: <xsl:template match="/response"> .....: <xsl:copy> .....: <xsl:apply-templates select="row"/> .....: </xsl:copy> .....: </xsl:template> .....: <xsl:template match="row"> .....: <xsl:copy> .....: <station_id><xsl:value-of select="station/@id"/></station_id> .....: <station_name><xsl:value-of select="station/@name"/></station_name> .....: <xsl:copy-of select="month|rides/*"/> .....: </xsl:copy> .....: </xsl:template> .....: </xsl:stylesheet>""" .....: In [389]: output = """<?xml version='1.0' encoding='utf-8'?> .....: <response> .....: <row> .....: <station_id>40850</station_id> .....: <station_name>Library</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>864.2</avg_weekday_rides> .....: <avg_saturday_rides>534</avg_saturday_rides> .....: <avg_sunday_holiday_rides>417.2</avg_sunday_holiday_rides> .....: </row> .....: <row> .....: <station_id>41700</station_id> .....: <station_name>Washington/Wabash</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>2707.4</avg_weekday_rides> .....: <avg_saturday_rides>1909.8</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1438.6</avg_sunday_holiday_rides> .....: </row> .....: <row> .....: <station_id>40380</station_id> .....: <station_name>Clark/Lake</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>2949.6</avg_weekday_rides> .....: <avg_saturday_rides>1657</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1453.8</avg_sunday_holiday_rides> .....: </row> .....: </response>""" .....: In [390]: df = pd.read_xml(xml, stylesheet=xsl) In [391]: df Out[391]: station_id station_name ... avg_saturday_rides avg_sunday_holiday_rides 0 40850 Library ... 534.0 417.2 1 41700 Washington/Wabash ... 1909.8 1438.6 2 40380 Clark/Lake ... 1657.0 1453.8 [3 rows x 6 columns] For very large XML files that can range in hundreds of megabytes to gigabytes, pandas.read_xml() supports parsing such sizeable files using lxml’s iterparse and etree’s iterparse which are memory-efficient methods to iterate through an XML tree and extract specific elements and attributes. without holding entire tree in memory. New in version 1.5.0. To use this feature, you must pass a physical XML file path into read_xml and use the iterparse argument. Files should not be compressed or point to online sources but stored on local disk. Also, iterparse should be a dictionary where the key is the repeating nodes in document (which become the rows) and the value is a list of any element or attribute that is a descendant (i.e., child, grandchild) of repeating node. Since XPath is not used in this method, descendants do not need to share same relationship with one another. Below shows example of reading in Wikipedia’s very large (12 GB+) latest article data dump. In [1]: df = pd.read_xml( ... "/path/to/downloaded/enwikisource-latest-pages-articles.xml", ... iterparse = {"page": ["title", "ns", "id"]} ... ) ... df Out[2]: title ns id 0 Gettysburg Address 0 21450 1 Main Page 0 42950 2 Declaration by United Nations 0 8435 3 Constitution of the United States of America 0 8435 4 Declaration of Independence (Israel) 0 17858 ... ... ... ... 3578760 Page:Black cat 1897 07 v2 n10.pdf/17 104 219649 3578761 Page:Black cat 1897 07 v2 n10.pdf/43 104 219649 3578762 Page:Black cat 1897 07 v2 n10.pdf/44 104 219649 3578763 The History of Tom Jones, a Foundling/Book IX 0 12084291 3578764 Page:Shakespeare of Stratford (1926) Yale.djvu/91 104 21450 [3578765 rows x 3 columns] Writing XML# New in version 1.3.0. DataFrame objects have an instance method to_xml which renders the contents of the DataFrame as an XML document. Note This method does not support special properties of XML including DTD, CData, XSD schemas, processing instructions, comments, and others. Only namespaces at the root level is supported. However, stylesheet allows design changes after initial output. Let’s look at a few examples. Write an XML without options: In [392]: geom_df = pd.DataFrame( .....: { .....: "shape": ["square", "circle", "triangle"], .....: "degrees": [360, 360, 180], .....: "sides": [4, np.nan, 3], .....: } .....: ) .....: In [393]: print(geom_df.to_xml()) <?xml version='1.0' encoding='utf-8'?> <data> <row> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </row> <row> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </row> <row> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Write an XML with new root and row name: In [394]: print(geom_df.to_xml(root_name="geometry", row_name="objects")) <?xml version='1.0' encoding='utf-8'?> <geometry> <objects> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </objects> <objects> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </objects> <objects> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </objects> </geometry> Write an attribute-centric XML: In [395]: print(geom_df.to_xml(attr_cols=geom_df.columns.tolist())) <?xml version='1.0' encoding='utf-8'?> <data> <row index="0" shape="square" degrees="360" sides="4.0"/> <row index="1" shape="circle" degrees="360"/> <row index="2" shape="triangle" degrees="180" sides="3.0"/> </data> Write a mix of elements and attributes: In [396]: print( .....: geom_df.to_xml( .....: index=False, .....: attr_cols=['shape'], .....: elem_cols=['degrees', 'sides']) .....: ) .....: <?xml version='1.0' encoding='utf-8'?> <data> <row shape="square"> <degrees>360</degrees> <sides>4.0</sides> </row> <row shape="circle"> <degrees>360</degrees> <sides/> </row> <row shape="triangle"> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Any DataFrames with hierarchical columns will be flattened for XML element names with levels delimited by underscores: In [397]: ext_geom_df = pd.DataFrame( .....: { .....: "type": ["polygon", "other", "polygon"], .....: "shape": ["square", "circle", "triangle"], .....: "degrees": [360, 360, 180], .....: "sides": [4, np.nan, 3], .....: } .....: ) .....: In [398]: pvt_df = ext_geom_df.pivot_table(index='shape', .....: columns='type', .....: values=['degrees', 'sides'], .....: aggfunc='sum') .....: In [399]: pvt_df Out[399]: degrees sides type other polygon other polygon shape circle 360.0 NaN 0.0 NaN square NaN 360.0 NaN 4.0 triangle NaN 180.0 NaN 3.0 In [400]: print(pvt_df.to_xml()) <?xml version='1.0' encoding='utf-8'?> <data> <row> <shape>circle</shape> <degrees_other>360.0</degrees_other> <degrees_polygon/> <sides_other>0.0</sides_other> <sides_polygon/> </row> <row> <shape>square</shape> <degrees_other/> <degrees_polygon>360.0</degrees_polygon> <sides_other/> <sides_polygon>4.0</sides_polygon> </row> <row> <shape>triangle</shape> <degrees_other/> <degrees_polygon>180.0</degrees_polygon> <sides_other/> <sides_polygon>3.0</sides_polygon> </row> </data> Write an XML with default namespace: In [401]: print(geom_df.to_xml(namespaces={"": "https://example.com"})) <?xml version='1.0' encoding='utf-8'?> <data xmlns="https://example.com"> <row> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </row> <row> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </row> <row> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Write an XML with namespace prefix: In [402]: print( .....: geom_df.to_xml(namespaces={"doc": "https://example.com"}, .....: prefix="doc") .....: ) .....: <?xml version='1.0' encoding='utf-8'?> <doc:data xmlns:doc="https://example.com"> <doc:row> <doc:index>0</doc:index> <doc:shape>square</doc:shape> <doc:degrees>360</doc:degrees> <doc:sides>4.0</doc:sides> </doc:row> <doc:row> <doc:index>1</doc:index> <doc:shape>circle</doc:shape> <doc:degrees>360</doc:degrees> <doc:sides/> </doc:row> <doc:row> <doc:index>2</doc:index> <doc:shape>triangle</doc:shape> <doc:degrees>180</doc:degrees> <doc:sides>3.0</doc:sides> </doc:row> </doc:data> Write an XML without declaration or pretty print: In [403]: print( .....: geom_df.to_xml(xml_declaration=False, .....: pretty_print=False) .....: ) .....: <data><row><index>0</index><shape>square</shape><degrees>360</degrees><sides>4.0</sides></row><row><index>1</index><shape>circle</shape><degrees>360</degrees><sides/></row><row><index>2</index><shape>triangle</shape><degrees>180</degrees><sides>3.0</sides></row></data> Write an XML and transform with stylesheet: In [404]: xsl = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> .....: <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/> .....: <xsl:strip-space elements="*"/> .....: <xsl:template match="/data"> .....: <geometry> .....: <xsl:apply-templates select="row"/> .....: </geometry> .....: </xsl:template> .....: <xsl:template match="row"> .....: <object index="{index}"> .....: <xsl:if test="shape!='circle'"> .....: <xsl:attribute name="type">polygon</xsl:attribute> .....: </xsl:if> .....: <xsl:copy-of select="shape"/> .....: <property> .....: <xsl:copy-of select="degrees|sides"/> .....: </property> .....: </object> .....: </xsl:template> .....: </xsl:stylesheet>""" .....: In [405]: print(geom_df.to_xml(stylesheet=xsl)) <?xml version="1.0"?> <geometry> <object index="0" type="polygon"> <shape>square</shape> <property> <degrees>360</degrees> <sides>4.0</sides> </property> </object> <object index="1"> <shape>circle</shape> <property> <degrees>360</degrees> <sides/> </property> </object> <object index="2" type="polygon"> <shape>triangle</shape> <property> <degrees>180</degrees> <sides>3.0</sides> </property> </object> </geometry> XML Final Notes# All XML documents adhere to W3C specifications. Both etree and lxml parsers will fail to parse any markup document that is not well-formed or follows XML syntax rules. Do be aware HTML is not an XML document unless it follows XHTML specs. However, other popular markup types including KML, XAML, RSS, MusicML, MathML are compliant XML schemas. For above reason, if your application builds XML prior to pandas operations, use appropriate DOM libraries like etree and lxml to build the necessary document and not by string concatenation or regex adjustments. Always remember XML is a special text file with markup rules. With very large XML files (several hundred MBs to GBs), XPath and XSLT can become memory-intensive operations. Be sure to have enough available RAM for reading and writing to large XML files (roughly about 5 times the size of text). Because XSLT is a programming language, use it with caution since such scripts can pose a security risk in your environment and can run large or infinite recursive operations. Always test scripts on small fragments before full run. The etree parser supports all functionality of both read_xml and to_xml except for complex XPath and any XSLT. Though limited in features, etree is still a reliable and capable parser and tree builder. Its performance may trail lxml to a certain degree for larger files but relatively unnoticeable on small to medium size files. Excel files# The read_excel() method can read Excel 2007+ (.xlsx) files using the openpyxl Python module. Excel 2003 (.xls) files can be read using xlrd. Binary Excel (.xlsb) files can be read using pyxlsb. The to_excel() instance method is used for saving a DataFrame to Excel. Generally the semantics are similar to working with csv data. See the cookbook for some advanced strategies. Warning The xlwt package for writing old-style .xls excel files is no longer maintained. The xlrd package is now only for reading old-style .xls files. Before pandas 1.3.0, the default argument engine=None to read_excel() would result in using the xlrd engine in many cases, including new Excel 2007+ (.xlsx) files. pandas will now default to using the openpyxl engine. It is strongly encouraged to install openpyxl to read Excel 2007+ (.xlsx) files. Please do not report issues when using ``xlrd`` to read ``.xlsx`` files. This is no longer supported, switch to using openpyxl instead. Attempting to use the xlwt engine will raise a FutureWarning unless the option io.excel.xls.writer is set to "xlwt". While this option is now deprecated and will also raise a FutureWarning, it can be globally set and the warning suppressed. Users are recommended to write .xlsx files using the openpyxl engine instead. Reading Excel files# In the most basic use-case, read_excel takes a path to an Excel file, and the sheet_name indicating which sheet to parse. # Returns a DataFrame pd.read_excel("path_to_file.xls", sheet_name="Sheet1") ExcelFile class# To facilitate working with multiple sheets from the same file, the ExcelFile class can be used to wrap the file and can be passed into read_excel There will be a performance benefit for reading multiple sheets as the file is read into memory only once. xlsx = pd.ExcelFile("path_to_file.xls") df = pd.read_excel(xlsx, "Sheet1") The ExcelFile class can also be used as a context manager. with pd.ExcelFile("path_to_file.xls") as xls: df1 = pd.read_excel(xls, "Sheet1") df2 = pd.read_excel(xls, "Sheet2") The sheet_names property will generate a list of the sheet names in the file. The primary use-case for an ExcelFile is parsing multiple sheets with different parameters: data = {} # For when Sheet1's format differs from Sheet2 with pd.ExcelFile("path_to_file.xls") as xls: data["Sheet1"] = pd.read_excel(xls, "Sheet1", index_col=None, na_values=["NA"]) data["Sheet2"] = pd.read_excel(xls, "Sheet2", index_col=1) Note that if the same parsing parameters are used for all sheets, a list of sheet names can simply be passed to read_excel with no loss in performance. # using the ExcelFile class data = {} with pd.ExcelFile("path_to_file.xls") as xls: data["Sheet1"] = pd.read_excel(xls, "Sheet1", index_col=None, na_values=["NA"]) data["Sheet2"] = pd.read_excel(xls, "Sheet2", index_col=None, na_values=["NA"]) # equivalent using the read_excel function data = pd.read_excel( "path_to_file.xls", ["Sheet1", "Sheet2"], index_col=None, na_values=["NA"] ) ExcelFile can also be called with a xlrd.book.Book object as a parameter. This allows the user to control how the excel file is read. For example, sheets can be loaded on demand by calling xlrd.open_workbook() with on_demand=True. import xlrd xlrd_book = xlrd.open_workbook("path_to_file.xls", on_demand=True) with pd.ExcelFile(xlrd_book) as xls: df1 = pd.read_excel(xls, "Sheet1") df2 = pd.read_excel(xls, "Sheet2") Specifying sheets# Note The second argument is sheet_name, not to be confused with ExcelFile.sheet_names. Note An ExcelFile’s attribute sheet_names provides access to a list of sheets. The arguments sheet_name allows specifying the sheet or sheets to read. The default value for sheet_name is 0, indicating to read the first sheet Pass a string to refer to the name of a particular sheet in the workbook. Pass an integer to refer to the index of a sheet. Indices follow Python convention, beginning at 0. Pass a list of either strings or integers, to return a dictionary of specified sheets. Pass a None to return a dictionary of all available sheets. # Returns a DataFrame pd.read_excel("path_to_file.xls", "Sheet1", index_col=None, na_values=["NA"]) Using the sheet index: # Returns a DataFrame pd.read_excel("path_to_file.xls", 0, index_col=None, na_values=["NA"]) Using all default values: # Returns a DataFrame pd.read_excel("path_to_file.xls") Using None to get all sheets: # Returns a dictionary of DataFrames pd.read_excel("path_to_file.xls", sheet_name=None) Using a list to get multiple sheets: # Returns the 1st and 4th sheet, as a dictionary of DataFrames. pd.read_excel("path_to_file.xls", sheet_name=["Sheet1", 3]) read_excel can read more than one sheet, by setting sheet_name to either a list of sheet names, a list of sheet positions, or None to read all sheets. Sheets can be specified by sheet index or sheet name, using an integer or string, respectively. Reading a MultiIndex# read_excel can read a MultiIndex index, by passing a list of columns to index_col and a MultiIndex column by passing a list of rows to header. If either the index or columns have serialized level names those will be read in as well by specifying the rows/columns that make up the levels. For example, to read in a MultiIndex index without names: In [406]: df = pd.DataFrame( .....: {"a": [1, 2, 3, 4], "b": [5, 6, 7, 8]}, .....: index=pd.MultiIndex.from_product([["a", "b"], ["c", "d"]]), .....: ) .....: In [407]: df.to_excel("path_to_file.xlsx") In [408]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1]) In [409]: df Out[409]: a b a c 1 5 d 2 6 b c 3 7 d 4 8 If the index has level names, they will parsed as well, using the same parameters. In [410]: df.index = df.index.set_names(["lvl1", "lvl2"]) In [411]: df.to_excel("path_to_file.xlsx") In [412]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1]) In [413]: df Out[413]: a b lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 If the source file has both MultiIndex index and columns, lists specifying each should be passed to index_col and header: In [414]: df.columns = pd.MultiIndex.from_product([["a"], ["b", "d"]], names=["c1", "c2"]) In [415]: df.to_excel("path_to_file.xlsx") In [416]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1], header=[0, 1]) In [417]: df Out[417]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 Missing values in columns specified in index_col will be forward filled to allow roundtripping with to_excel for merged_cells=True. To avoid forward filling the missing values use set_index after reading the data instead of index_col. Parsing specific columns# It is often the case that users will insert columns to do temporary computations in Excel and you may not want to read in those columns. read_excel takes a usecols keyword to allow you to specify a subset of columns to parse. Changed in version 1.0.0. Passing in an integer for usecols will no longer work. Please pass in a list of ints from 0 to usecols inclusive instead. You can specify a comma-delimited set of Excel columns and ranges as a string: pd.read_excel("path_to_file.xls", "Sheet1", usecols="A,C:E") If usecols is a list of integers, then it is assumed to be the file column indices to be parsed. pd.read_excel("path_to_file.xls", "Sheet1", usecols=[0, 2, 3]) Element order is ignored, so usecols=[0, 1] is the same as [1, 0]. If usecols is a list of strings, it is assumed that each string corresponds to a column name provided either by the user in names or inferred from the document header row(s). Those strings define which columns will be parsed: pd.read_excel("path_to_file.xls", "Sheet1", usecols=["foo", "bar"]) Element order is ignored, so usecols=['baz', 'joe'] is the same as ['joe', 'baz']. If usecols is callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True. pd.read_excel("path_to_file.xls", "Sheet1", usecols=lambda x: x.isalpha()) Parsing dates# Datetime-like values are normally automatically converted to the appropriate dtype when reading the excel file. But if you have a column of strings that look like dates (but are not actually formatted as dates in excel), you can use the parse_dates keyword to parse those strings to datetimes: pd.read_excel("path_to_file.xls", "Sheet1", parse_dates=["date_strings"]) Cell converters# It is possible to transform the contents of Excel cells via the converters option. For instance, to convert a column to boolean: pd.read_excel("path_to_file.xls", "Sheet1", converters={"MyBools": bool}) This options handles missing values and treats exceptions in the converters as missing data. Transformations are applied cell by cell rather than to the column as a whole, so the array dtype is not guaranteed. For instance, a column of integers with missing values cannot be transformed to an array with integer dtype, because NaN is strictly a float. You can manually mask missing data to recover integer dtype: def cfun(x): return int(x) if x else -1 pd.read_excel("path_to_file.xls", "Sheet1", converters={"MyInts": cfun}) Dtype specifications# As an alternative to converters, the type for an entire column can be specified using the dtype keyword, which takes a dictionary mapping column names to types. To interpret data with no type inference, use the type str or object. pd.read_excel("path_to_file.xls", dtype={"MyInts": "int64", "MyText": str}) Writing Excel files# Writing Excel files to disk# To write a DataFrame object to a sheet of an Excel file, you can use the to_excel instance method. The arguments are largely the same as to_csv described above, the first argument being the name of the excel file, and the optional second argument the name of the sheet to which the DataFrame should be written. For example: df.to_excel("path_to_file.xlsx", sheet_name="Sheet1") Files with a .xls extension will be written using xlwt and those with a .xlsx extension will be written using xlsxwriter (if available) or openpyxl. The DataFrame will be written in a way that tries to mimic the REPL output. The index_label will be placed in the second row instead of the first. You can place it in the first row by setting the merge_cells option in to_excel() to False: df.to_excel("path_to_file.xlsx", index_label="label", merge_cells=False) In order to write separate DataFrames to separate sheets in a single Excel file, one can pass an ExcelWriter. with pd.ExcelWriter("path_to_file.xlsx") as writer: df1.to_excel(writer, sheet_name="Sheet1") df2.to_excel(writer, sheet_name="Sheet2") Writing Excel files to memory# pandas supports writing Excel files to buffer-like objects such as StringIO or BytesIO using ExcelWriter. from io import BytesIO bio = BytesIO() # By setting the 'engine' in the ExcelWriter constructor. writer = pd.ExcelWriter(bio, engine="xlsxwriter") df.to_excel(writer, sheet_name="Sheet1") # Save the workbook writer.save() # Seek to the beginning and read to copy the workbook to a variable in memory bio.seek(0) workbook = bio.read() Note engine is optional but recommended. Setting the engine determines the version of workbook produced. Setting engine='xlrd' will produce an Excel 2003-format workbook (xls). Using either 'openpyxl' or 'xlsxwriter' will produce an Excel 2007-format workbook (xlsx). If omitted, an Excel 2007-formatted workbook is produced. Excel writer engines# Deprecated since version 1.2.0: As the xlwt package is no longer maintained, the xlwt engine will be removed from a future version of pandas. This is the only engine in pandas that supports writing to .xls files. pandas chooses an Excel writer via two methods: the engine keyword argument the filename extension (via the default specified in config options) By default, pandas uses the XlsxWriter for .xlsx, openpyxl for .xlsm, and xlwt for .xls files. If you have multiple engines installed, you can set the default engine through setting the config options io.excel.xlsx.writer and io.excel.xls.writer. pandas will fall back on openpyxl for .xlsx files if Xlsxwriter is not available. To specify which writer you want to use, you can pass an engine keyword argument to to_excel and to ExcelWriter. The built-in engines are: openpyxl: version 2.4 or higher is required xlsxwriter xlwt # By setting the 'engine' in the DataFrame 'to_excel()' methods. df.to_excel("path_to_file.xlsx", sheet_name="Sheet1", engine="xlsxwriter") # By setting the 'engine' in the ExcelWriter constructor. writer = pd.ExcelWriter("path_to_file.xlsx", engine="xlsxwriter") # Or via pandas configuration. from pandas import options # noqa: E402 options.io.excel.xlsx.writer = "xlsxwriter" df.to_excel("path_to_file.xlsx", sheet_name="Sheet1") Style and formatting# The look and feel of Excel worksheets created from pandas can be modified using the following parameters on the DataFrame’s to_excel method. float_format : Format string for floating point numbers (default None). freeze_panes : A tuple of two integers representing the bottommost row and rightmost column to freeze. Each of these parameters is one-based, so (1, 1) will freeze the first row and first column (default None). Using the Xlsxwriter engine provides many options for controlling the format of an Excel worksheet created with the to_excel method. Excellent examples can be found in the Xlsxwriter documentation here: https://xlsxwriter.readthedocs.io/working_with_pandas.html OpenDocument Spreadsheets# New in version 0.25. The read_excel() method can also read OpenDocument spreadsheets using the odfpy module. The semantics and features for reading OpenDocument spreadsheets match what can be done for Excel files using engine='odf'. # Returns a DataFrame pd.read_excel("path_to_file.ods", engine="odf") Note Currently pandas only supports reading OpenDocument spreadsheets. Writing is not implemented. Binary Excel (.xlsb) files# New in version 1.0.0. The read_excel() method can also read binary Excel files using the pyxlsb module. The semantics and features for reading binary Excel files mostly match what can be done for Excel files using engine='pyxlsb'. pyxlsb does not recognize datetime types in files and will return floats instead. # Returns a DataFrame pd.read_excel("path_to_file.xlsb", engine="pyxlsb") Note Currently pandas only supports reading binary Excel files. Writing is not implemented. Clipboard# A handy way to grab data is to use the read_clipboard() method, which takes the contents of the clipboard buffer and passes them to the read_csv method. For instance, you can copy the following text to the clipboard (CTRL-C on many operating systems): A B C x 1 4 p y 2 5 q z 3 6 r And then import the data directly to a DataFrame by calling: >>> clipdf = pd.read_clipboard() >>> clipdf A B C x 1 4 p y 2 5 q z 3 6 r The to_clipboard method can be used to write the contents of a DataFrame to the clipboard. Following which you can paste the clipboard contents into other applications (CTRL-V on many operating systems). Here we illustrate writing a DataFrame into clipboard and reading it back. >>> df = pd.DataFrame( ... {"A": [1, 2, 3], "B": [4, 5, 6], "C": ["p", "q", "r"]}, index=["x", "y", "z"] ... ) >>> df A B C x 1 4 p y 2 5 q z 3 6 r >>> df.to_clipboard() >>> pd.read_clipboard() A B C x 1 4 p y 2 5 q z 3 6 r We can see that we got the same content back, which we had earlier written to the clipboard. Note You may need to install xclip or xsel (with PyQt5, PyQt4 or qtpy) on Linux to use these methods. Pickling# All pandas objects are equipped with to_pickle methods which use Python’s cPickle module to save data structures to disk using the pickle format. In [418]: df Out[418]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 In [419]: df.to_pickle("foo.pkl") The read_pickle function in the pandas namespace can be used to load any pickled pandas object (or any other pickled object) from file: In [420]: pd.read_pickle("foo.pkl") Out[420]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 Warning Loading pickled data received from untrusted sources can be unsafe. See: https://docs.python.org/3/library/pickle.html Warning read_pickle() is only guaranteed backwards compatible back to pandas version 0.20.3 Compressed pickle files# read_pickle(), DataFrame.to_pickle() and Series.to_pickle() can read and write compressed pickle files. The compression types of gzip, bz2, xz, zstd are supported for reading and writing. The zip file format only supports reading and must contain only one data file to be read. The compression type can be an explicit parameter or be inferred from the file extension. If ‘infer’, then use gzip, bz2, zip, xz, zstd if filename ends in '.gz', '.bz2', '.zip', '.xz', or '.zst', respectively. The compression parameter can also be a dict in order to pass options to the compression protocol. It must have a 'method' key set to the name of the compression protocol, which must be one of {'zip', 'gzip', 'bz2', 'xz', 'zstd'}. All other key-value pairs are passed to the underlying compression library. In [421]: df = pd.DataFrame( .....: { .....: "A": np.random.randn(1000), .....: "B": "foo", .....: "C": pd.date_range("20130101", periods=1000, freq="s"), .....: } .....: ) .....: In [422]: df Out[422]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] Using an explicit compression type: In [423]: df.to_pickle("data.pkl.compress", compression="gzip") In [424]: rt = pd.read_pickle("data.pkl.compress", compression="gzip") In [425]: rt Out[425]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] Inferring compression type from the extension: In [426]: df.to_pickle("data.pkl.xz", compression="infer") In [427]: rt = pd.read_pickle("data.pkl.xz", compression="infer") In [428]: rt Out[428]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] The default is to ‘infer’: In [429]: df.to_pickle("data.pkl.gz") In [430]: rt = pd.read_pickle("data.pkl.gz") In [431]: rt Out[431]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] In [432]: df["A"].to_pickle("s1.pkl.bz2") In [433]: rt = pd.read_pickle("s1.pkl.bz2") In [434]: rt Out[434]: 0 -0.828876 1 -0.110383 2 2.357598 3 -1.620073 4 0.440903 ... 995 -1.177365 996 1.236988 997 0.743946 998 -0.533097 999 -0.140850 Name: A, Length: 1000, dtype: float64 Passing options to the compression protocol in order to speed up compression: In [435]: df.to_pickle("data.pkl.gz", compression={"method": "gzip", "compresslevel": 1}) msgpack# pandas support for msgpack has been removed in version 1.0.0. It is recommended to use pickle instead. Alternatively, you can also the Arrow IPC serialization format for on-the-wire transmission of pandas objects. For documentation on pyarrow, see here. HDF5 (PyTables)# HDFStore is a dict-like object which reads and writes pandas using the high performance HDF5 format using the excellent PyTables library. See the cookbook for some advanced strategies Warning pandas uses PyTables for reading and writing HDF5 files, which allows serializing object-dtype data with pickle. Loading pickled data received from untrusted sources can be unsafe. See: https://docs.python.org/3/library/pickle.html for more. In [436]: store = pd.HDFStore("store.h5") In [437]: print(store) <class 'pandas.io.pytables.HDFStore'> File path: store.h5 Objects can be written to the file just like adding key-value pairs to a dict: In [438]: index = pd.date_range("1/1/2000", periods=8) In [439]: s = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"]) In [440]: df = pd.DataFrame(np.random.randn(8, 3), index=index, columns=["A", "B", "C"]) # store.put('s', s) is an equivalent method In [441]: store["s"] = s In [442]: store["df"] = df In [443]: store Out[443]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 In a current or later Python session, you can retrieve stored objects: # store.get('df') is an equivalent method In [444]: store["df"] Out[444]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 # dotted (attribute) access provides get as well In [445]: store.df Out[445]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Deletion of the object specified by the key: # store.remove('df') is an equivalent method In [446]: del store["df"] In [447]: store Out[447]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 Closing a Store and using a context manager: In [448]: store.close() In [449]: store Out[449]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 In [450]: store.is_open Out[450]: False # Working with, and automatically closing the store using a context manager In [451]: with pd.HDFStore("store.h5") as store: .....: store.keys() .....: Read/write API# HDFStore supports a top-level API using read_hdf for reading and to_hdf for writing, similar to how read_csv and to_csv work. In [452]: df_tl = pd.DataFrame({"A": list(range(5)), "B": list(range(5))}) In [453]: df_tl.to_hdf("store_tl.h5", "table", append=True) In [454]: pd.read_hdf("store_tl.h5", "table", where=["index>2"]) Out[454]: A B 3 3 3 4 4 4 HDFStore will by default not drop rows that are all missing. This behavior can be changed by setting dropna=True. In [455]: df_with_missing = pd.DataFrame( .....: { .....: "col1": [0, np.nan, 2], .....: "col2": [1, np.nan, np.nan], .....: } .....: ) .....: In [456]: df_with_missing Out[456]: col1 col2 0 0.0 1.0 1 NaN NaN 2 2.0 NaN In [457]: df_with_missing.to_hdf("file.h5", "df_with_missing", format="table", mode="w") In [458]: pd.read_hdf("file.h5", "df_with_missing") Out[458]: col1 col2 0 0.0 1.0 1 NaN NaN 2 2.0 NaN In [459]: df_with_missing.to_hdf( .....: "file.h5", "df_with_missing", format="table", mode="w", dropna=True .....: ) .....: In [460]: pd.read_hdf("file.h5", "df_with_missing") Out[460]: col1 col2 0 0.0 1.0 2 2.0 NaN Fixed format# The examples above show storing using put, which write the HDF5 to PyTables in a fixed array format, called the fixed format. These types of stores are not appendable once written (though you can simply remove them and rewrite). Nor are they queryable; they must be retrieved in their entirety. They also do not support dataframes with non-unique column names. The fixed format stores offer very fast writing and slightly faster reading than table stores. This format is specified by default when using put or to_hdf or by format='fixed' or format='f'. Warning A fixed format will raise a TypeError if you try to retrieve using a where: >>> pd.DataFrame(np.random.randn(10, 2)).to_hdf("test_fixed.h5", "df") >>> pd.read_hdf("test_fixed.h5", "df", where="index>5") TypeError: cannot pass a where specification when reading a fixed format. this store must be selected in its entirety Table format# HDFStore supports another PyTables format on disk, the table format. Conceptually a table is shaped very much like a DataFrame, with rows and columns. A table may be appended to in the same or other sessions. In addition, delete and query type operations are supported. This format is specified by format='table' or format='t' to append or put or to_hdf. This format can be set as an option as well pd.set_option('io.hdf.default_format','table') to enable put/append/to_hdf to by default store in the table format. In [461]: store = pd.HDFStore("store.h5") In [462]: df1 = df[0:4] In [463]: df2 = df[4:] # append data (creates a table automatically) In [464]: store.append("df", df1) In [465]: store.append("df", df2) In [466]: store Out[466]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # select the entire object In [467]: store.select("df") Out[467]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 # the type of stored data In [468]: store.root.df._v_attrs.pandas_type Out[468]: 'frame_table' Note You can also create a table by passing format='table' or format='t' to a put operation. Hierarchical keys# Keys to a store can be specified as a string. These can be in a hierarchical path-name like format (e.g. foo/bar/bah), which will generate a hierarchy of sub-stores (or Groups in PyTables parlance). Keys can be specified without the leading ‘/’ and are always absolute (e.g. ‘foo’ refers to ‘/foo’). Removal operations can remove everything in the sub-store and below, so be careful. In [469]: store.put("foo/bar/bah", df) In [470]: store.append("food/orange", df) In [471]: store.append("food/apple", df) In [472]: store Out[472]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # a list of keys are returned In [473]: store.keys() Out[473]: ['/df', '/food/apple', '/food/orange', '/foo/bar/bah'] # remove all nodes under this level In [474]: store.remove("food") In [475]: store Out[475]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 You can walk through the group hierarchy using the walk method which will yield a tuple for each group key along with the relative keys of its contents. In [476]: for (path, subgroups, subkeys) in store.walk(): .....: for subgroup in subgroups: .....: print("GROUP: {}/{}".format(path, subgroup)) .....: for subkey in subkeys: .....: key = "/".join([path, subkey]) .....: print("KEY: {}".format(key)) .....: print(store.get(key)) .....: GROUP: /foo KEY: /df A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 GROUP: /foo/bar KEY: /foo/bar/bah A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Warning Hierarchical keys cannot be retrieved as dotted (attribute) access as described above for items stored under the root node. In [8]: store.foo.bar.bah AttributeError: 'HDFStore' object has no attribute 'foo' # you can directly access the actual PyTables node but using the root node In [9]: store.root.foo.bar.bah Out[9]: /foo/bar/bah (Group) '' children := ['block0_items' (Array), 'block0_values' (Array), 'axis0' (Array), 'axis1' (Array)] Instead, use explicit string based keys: In [477]: store["foo/bar/bah"] Out[477]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Storing types# Storing mixed types in a table# Storing mixed-dtype data is supported. Strings are stored as a fixed-width using the maximum size of the appended column. Subsequent attempts at appending longer strings will raise a ValueError. Passing min_itemsize={`values`: size} as a parameter to append will set a larger minimum for the string columns. Storing floats, strings, ints, bools, datetime64 are currently supported. For string columns, passing nan_rep = 'nan' to append will change the default nan representation on disk (which converts to/from np.nan), this defaults to nan. In [478]: df_mixed = pd.DataFrame( .....: { .....: "A": np.random.randn(8), .....: "B": np.random.randn(8), .....: "C": np.array(np.random.randn(8), dtype="float32"), .....: "string": "string", .....: "int": 1, .....: "bool": True, .....: "datetime64": pd.Timestamp("20010102"), .....: }, .....: index=list(range(8)), .....: ) .....: In [479]: df_mixed.loc[df_mixed.index[3:5], ["A", "B", "string", "datetime64"]] = np.nan In [480]: store.append("df_mixed", df_mixed, min_itemsize={"values": 50}) In [481]: df_mixed1 = store.select("df_mixed") In [482]: df_mixed1 Out[482]: A B C string int bool datetime64 0 1.778161 -0.898283 -0.263043 string 1 True 2001-01-02 1 -0.913867 -0.218499 -0.639244 string 1 True 2001-01-02 2 -0.030004 1.408028 -0.866305 string 1 True 2001-01-02 3 NaN NaN -0.225250 NaN 1 True NaT 4 NaN NaN -0.890978 NaN 1 True NaT 5 0.081323 0.520995 -0.553839 string 1 True 2001-01-02 6 -0.268494 0.620028 -2.762875 string 1 True 2001-01-02 7 0.168016 0.159416 -1.244763 string 1 True 2001-01-02 In [483]: df_mixed1.dtypes.value_counts() Out[483]: float64 2 float32 1 object 1 int64 1 bool 1 datetime64[ns] 1 dtype: int64 # we have provided a minimum string column size In [484]: store.root.df_mixed.table Out[484]: /df_mixed/table (Table(8,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(2,), dflt=0.0, pos=1), "values_block_1": Float32Col(shape=(1,), dflt=0.0, pos=2), "values_block_2": StringCol(itemsize=50, shape=(1,), dflt=b'', pos=3), "values_block_3": Int64Col(shape=(1,), dflt=0, pos=4), "values_block_4": BoolCol(shape=(1,), dflt=False, pos=5), "values_block_5": Int64Col(shape=(1,), dflt=0, pos=6)} byteorder := 'little' chunkshape := (689,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False} Storing MultiIndex DataFrames# Storing MultiIndex DataFrames as tables is very similar to storing/selecting from homogeneous index DataFrames. In [485]: index = pd.MultiIndex( .....: levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], .....: codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], .....: names=["foo", "bar"], .....: ) .....: In [486]: df_mi = pd.DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"]) In [487]: df_mi Out[487]: A B C foo bar foo one -1.280289 0.692545 -0.536722 two 1.005707 0.296917 0.139796 three -1.083889 0.811865 1.648435 bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 baz two 0.183573 0.145277 0.308146 three -1.043530 -0.708145 1.430905 qux one -0.850136 0.813949 1.508891 two -1.556154 0.187597 1.176488 three -1.246093 -0.002726 -0.444249 In [488]: store.append("df_mi", df_mi) In [489]: store.select("df_mi") Out[489]: A B C foo bar foo one -1.280289 0.692545 -0.536722 two 1.005707 0.296917 0.139796 three -1.083889 0.811865 1.648435 bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 baz two 0.183573 0.145277 0.308146 three -1.043530 -0.708145 1.430905 qux one -0.850136 0.813949 1.508891 two -1.556154 0.187597 1.176488 three -1.246093 -0.002726 -0.444249 # the levels are automatically included as data columns In [490]: store.select("df_mi", "foo=bar") Out[490]: A B C foo bar bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 Note The index keyword is reserved and cannot be use as a level name. Querying# Querying a table# select and delete operations have an optional criterion that can be specified to select/delete only a subset of the data. This allows one to have a very large on-disk table and retrieve only a portion of the data. A query is specified using the Term class under the hood, as a boolean expression. index and columns are supported indexers of DataFrames. if data_columns are specified, these can be used as additional indexers. level name in a MultiIndex, with default name level_0, level_1, … if not provided. Valid comparison operators are: =, ==, !=, >, >=, <, <= Valid boolean expressions are combined with: | : or & : and ( and ) : for grouping These rules are similar to how boolean expressions are used in pandas for indexing. Note = will be automatically expanded to the comparison operator == ~ is the not operator, but can only be used in very limited circumstances If a list/tuple of expressions is passed they will be combined via & The following are valid expressions: 'index >= date' "columns = ['A', 'D']" "columns in ['A', 'D']" 'columns = A' 'columns == A' "~(columns = ['A', 'B'])" 'index > df.index[3] & string = "bar"' '(index > df.index[3] & index <= df.index[6]) | string = "bar"' "ts >= Timestamp('2012-02-01')" "major_axis>=20130101" The indexers are on the left-hand side of the sub-expression: columns, major_axis, ts The right-hand side of the sub-expression (after a comparison operator) can be: functions that will be evaluated, e.g. Timestamp('2012-02-01') strings, e.g. "bar" date-like, e.g. 20130101, or "20130101" lists, e.g. "['A', 'B']" variables that are defined in the local names space, e.g. date Note Passing a string to a query by interpolating it into the query expression is not recommended. Simply assign the string of interest to a variable and use that variable in an expression. For example, do this string = "HolyMoly'" store.select("df", "index == string") instead of this string = "HolyMoly'" store.select('df', f'index == {string}') The latter will not work and will raise a SyntaxError.Note that there’s a single quote followed by a double quote in the string variable. If you must interpolate, use the '%r' format specifier store.select("df", "index == %r" % string) which will quote string. Here are some examples: In [491]: dfq = pd.DataFrame( .....: np.random.randn(10, 4), .....: columns=list("ABCD"), .....: index=pd.date_range("20130101", periods=10), .....: ) .....: In [492]: store.append("dfq", dfq, format="table", data_columns=True) Use boolean expressions, with in-line function evaluation. In [493]: store.select("dfq", "index>pd.Timestamp('20130104') & columns=['A', 'B']") Out[493]: A B 2013-01-05 1.366810 1.073372 2013-01-06 2.119746 -2.628174 2013-01-07 0.337920 -0.634027 2013-01-08 1.053434 1.109090 2013-01-09 -0.772942 -0.269415 2013-01-10 0.048562 -0.285920 Use inline column reference. In [494]: store.select("dfq", where="A>0 or C>0") Out[494]: A B C D 2013-01-01 0.856838 1.491776 0.001283 0.701816 2013-01-02 -1.097917 0.102588 0.661740 0.443531 2013-01-03 0.559313 -0.459055 -1.222598 -0.455304 2013-01-05 1.366810 1.073372 -0.994957 0.755314 2013-01-06 2.119746 -2.628174 -0.089460 -0.133636 2013-01-07 0.337920 -0.634027 0.421107 0.604303 2013-01-08 1.053434 1.109090 -0.367891 -0.846206 2013-01-10 0.048562 -0.285920 1.334100 0.194462 The columns keyword can be supplied to select a list of columns to be returned, this is equivalent to passing a 'columns=list_of_columns_to_filter': In [495]: store.select("df", "columns=['A', 'B']") Out[495]: A B 2000-01-01 -0.398501 -0.677311 2000-01-02 -1.167564 -0.593353 2000-01-03 -0.131959 0.089012 2000-01-04 0.169405 -1.358046 2000-01-05 0.492195 0.076693 2000-01-06 -0.285283 -1.210529 2000-01-07 0.941577 -0.342447 2000-01-08 0.052607 2.093214 start and stop parameters can be specified to limit the total search space. These are in terms of the total number of rows in a table. Note select will raise a ValueError if the query expression has an unknown variable reference. Usually this means that you are trying to select on a column that is not a data_column. select will raise a SyntaxError if the query expression is not valid. Query timedelta64[ns]# You can store and query using the timedelta64[ns] type. Terms can be specified in the format: <float>(<unit>), where float may be signed (and fractional), and unit can be D,s,ms,us,ns for the timedelta. Here’s an example: In [496]: from datetime import timedelta In [497]: dftd = pd.DataFrame( .....: { .....: "A": pd.Timestamp("20130101"), .....: "B": [ .....: pd.Timestamp("20130101") + timedelta(days=i, seconds=10) .....: for i in range(10) .....: ], .....: } .....: ) .....: In [498]: dftd["C"] = dftd["A"] - dftd["B"] In [499]: dftd Out[499]: A B C 0 2013-01-01 2013-01-01 00:00:10 -1 days +23:59:50 1 2013-01-01 2013-01-02 00:00:10 -2 days +23:59:50 2 2013-01-01 2013-01-03 00:00:10 -3 days +23:59:50 3 2013-01-01 2013-01-04 00:00:10 -4 days +23:59:50 4 2013-01-01 2013-01-05 00:00:10 -5 days +23:59:50 5 2013-01-01 2013-01-06 00:00:10 -6 days +23:59:50 6 2013-01-01 2013-01-07 00:00:10 -7 days +23:59:50 7 2013-01-01 2013-01-08 00:00:10 -8 days +23:59:50 8 2013-01-01 2013-01-09 00:00:10 -9 days +23:59:50 9 2013-01-01 2013-01-10 00:00:10 -10 days +23:59:50 In [500]: store.append("dftd", dftd, data_columns=True) In [501]: store.select("dftd", "C<'-3.5D'") Out[501]: A B C 4 2013-01-01 2013-01-05 00:00:10 -5 days +23:59:50 5 2013-01-01 2013-01-06 00:00:10 -6 days +23:59:50 6 2013-01-01 2013-01-07 00:00:10 -7 days +23:59:50 7 2013-01-01 2013-01-08 00:00:10 -8 days +23:59:50 8 2013-01-01 2013-01-09 00:00:10 -9 days +23:59:50 9 2013-01-01 2013-01-10 00:00:10 -10 days +23:59:50 Query MultiIndex# Selecting from a MultiIndex can be achieved by using the name of the level. In [502]: df_mi.index.names Out[502]: FrozenList(['foo', 'bar']) In [503]: store.select("df_mi", "foo=baz and bar=two") Out[503]: A B C foo bar baz two 0.183573 0.145277 0.308146 If the MultiIndex levels names are None, the levels are automatically made available via the level_n keyword with n the level of the MultiIndex you want to select from. In [504]: index = pd.MultiIndex( .....: levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], .....: codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], .....: ) .....: In [505]: df_mi_2 = pd.DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"]) In [506]: df_mi_2 Out[506]: A B C foo one -0.646538 1.210676 -0.315409 two 1.528366 0.376542 0.174490 three 1.247943 -0.742283 0.710400 bar one 0.434128 -1.246384 1.139595 two 1.388668 -0.413554 -0.666287 baz two 0.010150 -0.163820 -0.115305 three 0.216467 0.633720 0.473945 qux one -0.155446 1.287082 0.320201 two -1.256989 0.874920 0.765944 three 0.025557 -0.729782 -0.127439 In [507]: store.append("df_mi_2", df_mi_2) # the levels are automatically included as data columns with keyword level_n In [508]: store.select("df_mi_2", "level_0=foo and level_1=two") Out[508]: A B C foo two 1.528366 0.376542 0.17449 Indexing# You can create/modify an index for a table with create_table_index after data is already in the table (after and append/put operation). Creating a table index is highly encouraged. This will speed your queries a great deal when you use a select with the indexed dimension as the where. Note Indexes are automagically created on the indexables and any data columns you specify. This behavior can be turned off by passing index=False to append. # we have automagically already created an index (in the first section) In [509]: i = store.root.df.table.cols.index.index In [510]: i.optlevel, i.kind Out[510]: (6, 'medium') # change an index by passing new parameters In [511]: store.create_table_index("df", optlevel=9, kind="full") In [512]: i = store.root.df.table.cols.index.index In [513]: i.optlevel, i.kind Out[513]: (9, 'full') Oftentimes when appending large amounts of data to a store, it is useful to turn off index creation for each append, then recreate at the end. In [514]: df_1 = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [515]: df_2 = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [516]: st = pd.HDFStore("appends.h5", mode="w") In [517]: st.append("df", df_1, data_columns=["B"], index=False) In [518]: st.append("df", df_2, data_columns=["B"], index=False) In [519]: st.get_storer("df").table Out[519]: /df/table (Table(20,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2)} byteorder := 'little' chunkshape := (2730,) Then create the index when finished appending. In [520]: st.create_table_index("df", columns=["B"], optlevel=9, kind="full") In [521]: st.get_storer("df").table Out[521]: /df/table (Table(20,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2)} byteorder := 'little' chunkshape := (2730,) autoindex := True colindexes := { "B": Index(9, fullshuffle, zlib(1)).is_csi=True} In [522]: st.close() See here for how to create a completely-sorted-index (CSI) on an existing store. Query via data columns# You can designate (and index) certain columns that you want to be able to perform queries (other than the indexable columns, which you can always query). For instance say you want to perform this common operation, on-disk, and return just the frame that matches this query. You can specify data_columns = True to force all columns to be data_columns. In [523]: df_dc = df.copy() In [524]: df_dc["string"] = "foo" In [525]: df_dc.loc[df_dc.index[4:6], "string"] = np.nan In [526]: df_dc.loc[df_dc.index[7:9], "string"] = "bar" In [527]: df_dc["string2"] = "cool" In [528]: df_dc.loc[df_dc.index[1:3], ["B", "C"]] = 1.0 In [529]: df_dc Out[529]: A B C string string2 2000-01-01 -0.398501 -0.677311 -0.874991 foo cool 2000-01-02 -1.167564 1.000000 1.000000 foo cool 2000-01-03 -0.131959 1.000000 1.000000 foo cool 2000-01-04 0.169405 -1.358046 -0.105563 foo cool 2000-01-05 0.492195 0.076693 0.213685 NaN cool 2000-01-06 -0.285283 -1.210529 -1.408386 NaN cool 2000-01-07 0.941577 -0.342447 0.222031 foo cool 2000-01-08 0.052607 2.093214 1.064908 bar cool # on-disk operations In [530]: store.append("df_dc", df_dc, data_columns=["B", "C", "string", "string2"]) In [531]: store.select("df_dc", where="B > 0") Out[531]: A B C string string2 2000-01-02 -1.167564 1.000000 1.000000 foo cool 2000-01-03 -0.131959 1.000000 1.000000 foo cool 2000-01-05 0.492195 0.076693 0.213685 NaN cool 2000-01-08 0.052607 2.093214 1.064908 bar cool # getting creative In [532]: store.select("df_dc", "B > 0 & C > 0 & string == foo") Out[532]: A B C string string2 2000-01-02 -1.167564 1.0 1.0 foo cool 2000-01-03 -0.131959 1.0 1.0 foo cool # this is in-memory version of this type of selection In [533]: df_dc[(df_dc.B > 0) & (df_dc.C > 0) & (df_dc.string == "foo")] Out[533]: A B C string string2 2000-01-02 -1.167564 1.0 1.0 foo cool 2000-01-03 -0.131959 1.0 1.0 foo cool # we have automagically created this index and the B/C/string/string2 # columns are stored separately as ``PyTables`` columns In [534]: store.root.df_dc.table Out[534]: /df_dc/table (Table(8,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2), "C": Float64Col(shape=(), dflt=0.0, pos=3), "string": StringCol(itemsize=3, shape=(), dflt=b'', pos=4), "string2": StringCol(itemsize=4, shape=(), dflt=b'', pos=5)} byteorder := 'little' chunkshape := (1680,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False, "B": Index(6, mediumshuffle, zlib(1)).is_csi=False, "C": Index(6, mediumshuffle, zlib(1)).is_csi=False, "string": Index(6, mediumshuffle, zlib(1)).is_csi=False, "string2": Index(6, mediumshuffle, zlib(1)).is_csi=False} There is some performance degradation by making lots of columns into data columns, so it is up to the user to designate these. In addition, you cannot change data columns (nor indexables) after the first append/put operation (Of course you can simply read in the data and create a new table!). Iterator# You can pass iterator=True or chunksize=number_in_a_chunk to select and select_as_multiple to return an iterator on the results. The default is 50,000 rows returned in a chunk. In [535]: for df in store.select("df", chunksize=3): .....: print(df) .....: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 A B C 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 A B C 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Note You can also use the iterator with read_hdf which will open, then automatically close the store when finished iterating. for df in pd.read_hdf("store.h5", "df", chunksize=3): print(df) Note, that the chunksize keyword applies to the source rows. So if you are doing a query, then the chunksize will subdivide the total rows in the table and the query applied, returning an iterator on potentially unequal sized chunks. Here is a recipe for generating a query and using it to create equal sized return chunks. In [536]: dfeq = pd.DataFrame({"number": np.arange(1, 11)}) In [537]: dfeq Out[537]: number 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 In [538]: store.append("dfeq", dfeq, data_columns=["number"]) In [539]: def chunks(l, n): .....: return [l[i: i + n] for i in range(0, len(l), n)] .....: In [540]: evens = [2, 4, 6, 8, 10] In [541]: coordinates = store.select_as_coordinates("dfeq", "number=evens") In [542]: for c in chunks(coordinates, 2): .....: print(store.select("dfeq", where=c)) .....: number 1 2 3 4 number 5 6 7 8 number 9 10 Advanced queries# Select a single column# To retrieve a single indexable or data column, use the method select_column. This will, for example, enable you to get the index very quickly. These return a Series of the result, indexed by the row number. These do not currently accept the where selector. In [543]: store.select_column("df_dc", "index") Out[543]: 0 2000-01-01 1 2000-01-02 2 2000-01-03 3 2000-01-04 4 2000-01-05 5 2000-01-06 6 2000-01-07 7 2000-01-08 Name: index, dtype: datetime64[ns] In [544]: store.select_column("df_dc", "string") Out[544]: 0 foo 1 foo 2 foo 3 foo 4 NaN 5 NaN 6 foo 7 bar Name: string, dtype: object Selecting coordinates# Sometimes you want to get the coordinates (a.k.a the index locations) of your query. This returns an Int64Index of the resulting locations. These coordinates can also be passed to subsequent where operations. In [545]: df_coord = pd.DataFrame( .....: np.random.randn(1000, 2), index=pd.date_range("20000101", periods=1000) .....: ) .....: In [546]: store.append("df_coord", df_coord) In [547]: c = store.select_as_coordinates("df_coord", "index > 20020101") In [548]: c Out[548]: Int64Index([732, 733, 734, 735, 736, 737, 738, 739, 740, 741, ... 990, 991, 992, 993, 994, 995, 996, 997, 998, 999], dtype='int64', length=268) In [549]: store.select("df_coord", where=c) Out[549]: 0 1 2002-01-02 0.009035 0.921784 2002-01-03 -1.476563 -1.376375 2002-01-04 1.266731 2.173681 2002-01-05 0.147621 0.616468 2002-01-06 0.008611 2.136001 ... ... ... 2002-09-22 0.781169 -0.791687 2002-09-23 -0.764810 -2.000933 2002-09-24 -0.345662 0.393915 2002-09-25 -0.116661 0.834638 2002-09-26 -1.341780 0.686366 [268 rows x 2 columns] Selecting using a where mask# Sometime your query can involve creating a list of rows to select. Usually this mask would be a resulting index from an indexing operation. This example selects the months of a datetimeindex which are 5. In [550]: df_mask = pd.DataFrame( .....: np.random.randn(1000, 2), index=pd.date_range("20000101", periods=1000) .....: ) .....: In [551]: store.append("df_mask", df_mask) In [552]: c = store.select_column("df_mask", "index") In [553]: where = c[pd.DatetimeIndex(c).month == 5].index In [554]: store.select("df_mask", where=where) Out[554]: 0 1 2000-05-01 -0.386742 -0.977433 2000-05-02 -0.228819 0.471671 2000-05-03 0.337307 1.840494 2000-05-04 0.050249 0.307149 2000-05-05 -0.802947 -0.946730 ... ... ... 2002-05-27 1.605281 1.741415 2002-05-28 -0.804450 -0.715040 2002-05-29 -0.874851 0.037178 2002-05-30 -0.161167 -1.294944 2002-05-31 -0.258463 -0.731969 [93 rows x 2 columns] Storer object# If you want to inspect the stored object, retrieve via get_storer. You could use this programmatically to say get the number of rows in an object. In [555]: store.get_storer("df_dc").nrows Out[555]: 8 Multiple table queries# The methods append_to_multiple and select_as_multiple can perform appending/selecting from multiple tables at once. The idea is to have one table (call it the selector table) that you index most/all of the columns, and perform your queries. The other table(s) are data tables with an index matching the selector table’s index. You can then perform a very fast query on the selector table, yet get lots of data back. This method is similar to having a very wide table, but enables more efficient queries. The append_to_multiple method splits a given single DataFrame into multiple tables according to d, a dictionary that maps the table names to a list of ‘columns’ you want in that table. If None is used in place of a list, that table will have the remaining unspecified columns of the given DataFrame. The argument selector defines which table is the selector table (which you can make queries from). The argument dropna will drop rows from the input DataFrame to ensure tables are synchronized. This means that if a row for one of the tables being written to is entirely np.NaN, that row will be dropped from all tables. If dropna is False, THE USER IS RESPONSIBLE FOR SYNCHRONIZING THE TABLES. Remember that entirely np.Nan rows are not written to the HDFStore, so if you choose to call dropna=False, some tables may have more rows than others, and therefore select_as_multiple may not work or it may return unexpected results. In [556]: df_mt = pd.DataFrame( .....: np.random.randn(8, 6), .....: index=pd.date_range("1/1/2000", periods=8), .....: columns=["A", "B", "C", "D", "E", "F"], .....: ) .....: In [557]: df_mt["foo"] = "bar" In [558]: df_mt.loc[df_mt.index[1], ("A", "B")] = np.nan # you can also create the tables individually In [559]: store.append_to_multiple( .....: {"df1_mt": ["A", "B"], "df2_mt": None}, df_mt, selector="df1_mt" .....: ) .....: In [560]: store Out[560]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # individual tables were created In [561]: store.select("df1_mt") Out[561]: A B 2000-01-01 0.079529 -1.459471 2000-01-02 NaN NaN 2000-01-03 -0.423113 2.314361 2000-01-04 0.756744 -0.792372 2000-01-05 -0.184971 0.170852 2000-01-06 0.678830 0.633974 2000-01-07 0.034973 0.974369 2000-01-08 -2.110103 0.243062 In [562]: store.select("df2_mt") Out[562]: C D E F foo 2000-01-01 -0.596306 -0.910022 -1.057072 -0.864360 bar 2000-01-02 0.477849 0.283128 -2.045700 -0.338206 bar 2000-01-03 -0.033100 -0.965461 -0.001079 -0.351689 bar 2000-01-04 -0.513555 -1.484776 -0.796280 -0.182321 bar 2000-01-05 -0.872407 -1.751515 0.934334 0.938818 bar 2000-01-06 -1.398256 1.347142 -0.029520 0.082738 bar 2000-01-07 -0.755544 0.380786 -1.634116 1.293610 bar 2000-01-08 1.453064 0.500558 -0.574475 0.694324 bar # as a multiple In [563]: store.select_as_multiple( .....: ["df1_mt", "df2_mt"], .....: where=["A>0", "B>0"], .....: selector="df1_mt", .....: ) .....: Out[563]: A B C D E F foo 2000-01-06 0.678830 0.633974 -1.398256 1.347142 -0.029520 0.082738 bar 2000-01-07 0.034973 0.974369 -0.755544 0.380786 -1.634116 1.293610 bar Delete from a table# You can delete from a table selectively by specifying a where. In deleting rows, it is important to understand the PyTables deletes rows by erasing the rows, then moving the following data. Thus deleting can potentially be a very expensive operation depending on the orientation of your data. To get optimal performance, it’s worthwhile to have the dimension you are deleting be the first of the indexables. Data is ordered (on the disk) in terms of the indexables. Here’s a simple use case. You store panel-type data, with dates in the major_axis and ids in the minor_axis. The data is then interleaved like this: date_1 id_1 id_2 . id_n date_2 id_1 . id_n It should be clear that a delete operation on the major_axis will be fairly quick, as one chunk is removed, then the following data moved. On the other hand a delete operation on the minor_axis will be very expensive. In this case it would almost certainly be faster to rewrite the table using a where that selects all but the missing data. Warning Please note that HDF5 DOES NOT RECLAIM SPACE in the h5 files automatically. Thus, repeatedly deleting (or removing nodes) and adding again, WILL TEND TO INCREASE THE FILE SIZE. To repack and clean the file, use ptrepack. Notes & caveats# Compression# PyTables allows the stored data to be compressed. This applies to all kinds of stores, not just tables. Two parameters are used to control compression: complevel and complib. complevel specifies if and how hard data is to be compressed. complevel=0 and complevel=None disables compression and 0<complevel<10 enables compression. complib specifies which compression library to use. If nothing is specified the default library zlib is used. A compression library usually optimizes for either good compression rates or speed and the results will depend on the type of data. Which type of compression to choose depends on your specific needs and data. The list of supported compression libraries: zlib: The default compression library. A classic in terms of compression, achieves good compression rates but is somewhat slow. lzo: Fast compression and decompression. bzip2: Good compression rates. blosc: Fast compression and decompression. Support for alternative blosc compressors: blosc:blosclz This is the default compressor for blosc blosc:lz4: A compact, very popular and fast compressor. blosc:lz4hc: A tweaked version of LZ4, produces better compression ratios at the expense of speed. blosc:snappy: A popular compressor used in many places. blosc:zlib: A classic; somewhat slower than the previous ones, but achieving better compression ratios. blosc:zstd: An extremely well balanced codec; it provides the best compression ratios among the others above, and at reasonably fast speed. If complib is defined as something other than the listed libraries a ValueError exception is issued. Note If the library specified with the complib option is missing on your platform, compression defaults to zlib without further ado. Enable compression for all objects within the file: store_compressed = pd.HDFStore( "store_compressed.h5", complevel=9, complib="blosc:blosclz" ) Or on-the-fly compression (this only applies to tables) in stores where compression is not enabled: store.append("df", df, complib="zlib", complevel=5) ptrepack# PyTables offers better write performance when tables are compressed after they are written, as opposed to turning on compression at the very beginning. You can use the supplied PyTables utility ptrepack. In addition, ptrepack can change compression levels after the fact. ptrepack --chunkshape=auto --propindexes --complevel=9 --complib=blosc in.h5 out.h5 Furthermore ptrepack in.h5 out.h5 will repack the file to allow you to reuse previously deleted space. Alternatively, one can simply remove the file and write again, or use the copy method. Caveats# Warning HDFStore is not-threadsafe for writing. The underlying PyTables only supports concurrent reads (via threading or processes). If you need reading and writing at the same time, you need to serialize these operations in a single thread in a single process. You will corrupt your data otherwise. See the (GH2397) for more information. If you use locks to manage write access between multiple processes, you may want to use fsync() before releasing write locks. For convenience you can use store.flush(fsync=True) to do this for you. Once a table is created columns (DataFrame) are fixed; only exactly the same columns can be appended Be aware that timezones (e.g., pytz.timezone('US/Eastern')) are not necessarily equal across timezone versions. So if data is localized to a specific timezone in the HDFStore using one version of a timezone library and that data is updated with another version, the data will be converted to UTC since these timezones are not considered equal. Either use the same version of timezone library or use tz_convert with the updated timezone definition. Warning PyTables will show a NaturalNameWarning if a column name cannot be used as an attribute selector. Natural identifiers contain only letters, numbers, and underscores, and may not begin with a number. Other identifiers cannot be used in a where clause and are generally a bad idea. DataTypes# HDFStore will map an object dtype to the PyTables underlying dtype. This means the following types are known to work: Type Represents missing values floating : float64, float32, float16 np.nan integer : int64, int32, int8, uint64,uint32, uint8 boolean datetime64[ns] NaT timedelta64[ns] NaT categorical : see the section below object : strings np.nan unicode columns are not supported, and WILL FAIL. Categorical data# You can write data that contains category dtypes to a HDFStore. Queries work the same as if it was an object array. However, the category dtyped data is stored in a more efficient manner. In [564]: dfcat = pd.DataFrame( .....: {"A": pd.Series(list("aabbcdba")).astype("category"), "B": np.random.randn(8)} .....: ) .....: In [565]: dfcat Out[565]: A B 0 a -1.608059 1 a 0.851060 2 b -0.736931 3 b 0.003538 4 c -1.422611 5 d 2.060901 6 b 0.993899 7 a -1.371768 In [566]: dfcat.dtypes Out[566]: A category B float64 dtype: object In [567]: cstore = pd.HDFStore("cats.h5", mode="w") In [568]: cstore.append("dfcat", dfcat, format="table", data_columns=["A"]) In [569]: result = cstore.select("dfcat", where="A in ['b', 'c']") In [570]: result Out[570]: A B 2 b -0.736931 3 b 0.003538 4 c -1.422611 6 b 0.993899 In [571]: result.dtypes Out[571]: A category B float64 dtype: object String columns# min_itemsize The underlying implementation of HDFStore uses a fixed column width (itemsize) for string columns. A string column itemsize is calculated as the maximum of the length of data (for that column) that is passed to the HDFStore, in the first append. Subsequent appends, may introduce a string for a column larger than the column can hold, an Exception will be raised (otherwise you could have a silent truncation of these columns, leading to loss of information). In the future we may relax this and allow a user-specified truncation to occur. Pass min_itemsize on the first table creation to a-priori specify the minimum length of a particular string column. min_itemsize can be an integer, or a dict mapping a column name to an integer. You can pass values as a key to allow all indexables or data_columns to have this min_itemsize. Passing a min_itemsize dict will cause all passed columns to be created as data_columns automatically. Note If you are not passing any data_columns, then the min_itemsize will be the maximum of the length of any string passed In [572]: dfs = pd.DataFrame({"A": "foo", "B": "bar"}, index=list(range(5))) In [573]: dfs Out[573]: A B 0 foo bar 1 foo bar 2 foo bar 3 foo bar 4 foo bar # A and B have a size of 30 In [574]: store.append("dfs", dfs, min_itemsize=30) In [575]: store.get_storer("dfs").table Out[575]: /dfs/table (Table(5,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": StringCol(itemsize=30, shape=(2,), dflt=b'', pos=1)} byteorder := 'little' chunkshape := (963,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False} # A is created as a data_column with a size of 30 # B is size is calculated In [576]: store.append("dfs2", dfs, min_itemsize={"A": 30}) In [577]: store.get_storer("dfs2").table Out[577]: /dfs2/table (Table(5,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": StringCol(itemsize=3, shape=(1,), dflt=b'', pos=1), "A": StringCol(itemsize=30, shape=(), dflt=b'', pos=2)} byteorder := 'little' chunkshape := (1598,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False, "A": Index(6, mediumshuffle, zlib(1)).is_csi=False} nan_rep String columns will serialize a np.nan (a missing value) with the nan_rep string representation. This defaults to the string value nan. You could inadvertently turn an actual nan value into a missing value. In [578]: dfss = pd.DataFrame({"A": ["foo", "bar", "nan"]}) In [579]: dfss Out[579]: A 0 foo 1 bar 2 nan In [580]: store.append("dfss", dfss) In [581]: store.select("dfss") Out[581]: A 0 foo 1 bar 2 NaN # here you need to specify a different nan rep In [582]: store.append("dfss2", dfss, nan_rep="_nan_") In [583]: store.select("dfss2") Out[583]: A 0 foo 1 bar 2 nan External compatibility# HDFStore writes table format objects in specific formats suitable for producing loss-less round trips to pandas objects. For external compatibility, HDFStore can read native PyTables format tables. It is possible to write an HDFStore object that can easily be imported into R using the rhdf5 library (Package website). Create a table format store like this: In [584]: df_for_r = pd.DataFrame( .....: { .....: "first": np.random.rand(100), .....: "second": np.random.rand(100), .....: "class": np.random.randint(0, 2, (100,)), .....: }, .....: index=range(100), .....: ) .....: In [585]: df_for_r.head() Out[585]: first second class 0 0.013480 0.504941 0 1 0.690984 0.898188 1 2 0.510113 0.618748 1 3 0.357698 0.004972 0 4 0.451658 0.012065 1 In [586]: store_export = pd.HDFStore("export.h5") In [587]: store_export.append("df_for_r", df_for_r, data_columns=df_dc.columns) In [588]: store_export Out[588]: <class 'pandas.io.pytables.HDFStore'> File path: export.h5 In R this file can be read into a data.frame object using the rhdf5 library. The following example function reads the corresponding column names and data values from the values and assembles them into a data.frame: # Load values and column names for all datasets from corresponding nodes and # insert them into one data.frame object. library(rhdf5) loadhdf5data <- function(h5File) { listing <- h5ls(h5File) # Find all data nodes, values are stored in *_values and corresponding column # titles in *_items data_nodes <- grep("_values", listing$name) name_nodes <- grep("_items", listing$name) data_paths = paste(listing$group[data_nodes], listing$name[data_nodes], sep = "/") name_paths = paste(listing$group[name_nodes], listing$name[name_nodes], sep = "/") columns = list() for (idx in seq(data_paths)) { # NOTE: matrices returned by h5read have to be transposed to obtain # required Fortran order! data <- data.frame(t(h5read(h5File, data_paths[idx]))) names <- t(h5read(h5File, name_paths[idx])) entry <- data.frame(data) colnames(entry) <- names columns <- append(columns, entry) } data <- data.frame(columns) return(data) } Now you can import the DataFrame into R: > data = loadhdf5data("transfer.hdf5") > head(data) first second class 1 0.4170220047 0.3266449 0 2 0.7203244934 0.5270581 0 3 0.0001143748 0.8859421 1 4 0.3023325726 0.3572698 1 5 0.1467558908 0.9085352 1 6 0.0923385948 0.6233601 1 Note The R function lists the entire HDF5 file’s contents and assembles the data.frame object from all matching nodes, so use this only as a starting point if you have stored multiple DataFrame objects to a single HDF5 file. Performance# tables format come with a writing performance penalty as compared to fixed stores. The benefit is the ability to append/delete and query (potentially very large amounts of data). Write times are generally longer as compared with regular stores. Query times can be quite fast, especially on an indexed axis. You can pass chunksize=<int> to append, specifying the write chunksize (default is 50000). This will significantly lower your memory usage on writing. You can pass expectedrows=<int> to the first append, to set the TOTAL number of rows that PyTables will expect. This will optimize read/write performance. Duplicate rows can be written to tables, but are filtered out in selection (with the last items being selected; thus a table is unique on major, minor pairs) A PerformanceWarning will be raised if you are attempting to store types that will be pickled by PyTables (rather than stored as endemic types). See Here for more information and some solutions. Feather# Feather provides binary columnar serialization for data frames. It is designed to make reading and writing data frames efficient, and to make sharing data across data analysis languages easy. Feather is designed to faithfully serialize and de-serialize DataFrames, supporting all of the pandas dtypes, including extension dtypes such as categorical and datetime with tz. Several caveats: The format will NOT write an Index, or MultiIndex for the DataFrame and will raise an error if a non-default one is provided. You can .reset_index() to store the index or .reset_index(drop=True) to ignore it. Duplicate column names and non-string columns names are not supported Actual Python objects in object dtype columns are not supported. These will raise a helpful error message on an attempt at serialization. See the Full Documentation. In [589]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(3, 6).astype("u1"), .....: "d": np.arange(4.0, 7.0, dtype="float64"), .....: "e": [True, False, True], .....: "f": pd.Categorical(list("abc")), .....: "g": pd.date_range("20130101", periods=3), .....: "h": pd.date_range("20130101", periods=3, tz="US/Eastern"), .....: "i": pd.date_range("20130101", periods=3, freq="ns"), .....: } .....: ) .....: In [590]: df Out[590]: a b c ... g h i 0 a 1 3 ... 2013-01-01 2013-01-01 00:00:00-05:00 2013-01-01 00:00:00.000000000 1 b 2 4 ... 2013-01-02 2013-01-02 00:00:00-05:00 2013-01-01 00:00:00.000000001 2 c 3 5 ... 2013-01-03 2013-01-03 00:00:00-05:00 2013-01-01 00:00:00.000000002 [3 rows x 9 columns] In [591]: df.dtypes Out[591]: a object b int64 c uint8 d float64 e bool f category g datetime64[ns] h datetime64[ns, US/Eastern] i datetime64[ns] dtype: object Write to a feather file. In [592]: df.to_feather("example.feather") Read from a feather file. In [593]: result = pd.read_feather("example.feather") In [594]: result Out[594]: a b c ... g h i 0 a 1 3 ... 2013-01-01 2013-01-01 00:00:00-05:00 2013-01-01 00:00:00.000000000 1 b 2 4 ... 2013-01-02 2013-01-02 00:00:00-05:00 2013-01-01 00:00:00.000000001 2 c 3 5 ... 2013-01-03 2013-01-03 00:00:00-05:00 2013-01-01 00:00:00.000000002 [3 rows x 9 columns] # we preserve dtypes In [595]: result.dtypes Out[595]: a object b int64 c uint8 d float64 e bool f category g datetime64[ns] h datetime64[ns, US/Eastern] i datetime64[ns] dtype: object Parquet# Apache Parquet provides a partitioned binary columnar serialization for data frames. It is designed to make reading and writing data frames efficient, and to make sharing data across data analysis languages easy. Parquet can use a variety of compression techniques to shrink the file size as much as possible while still maintaining good read performance. Parquet is designed to faithfully serialize and de-serialize DataFrame s, supporting all of the pandas dtypes, including extension dtypes such as datetime with tz. Several caveats. Duplicate column names and non-string columns names are not supported. The pyarrow engine always writes the index to the output, but fastparquet only writes non-default indexes. This extra column can cause problems for non-pandas consumers that are not expecting it. You can force including or omitting indexes with the index argument, regardless of the underlying engine. Index level names, if specified, must be strings. In the pyarrow engine, categorical dtypes for non-string types can be serialized to parquet, but will de-serialize as their primitive dtype. The pyarrow engine preserves the ordered flag of categorical dtypes with string types. fastparquet does not preserve the ordered flag. Non supported types include Interval and actual Python object types. These will raise a helpful error message on an attempt at serialization. Period type is supported with pyarrow >= 0.16.0. The pyarrow engine preserves extension data types such as the nullable integer and string data type (requiring pyarrow >= 0.16.0, and requiring the extension type to implement the needed protocols, see the extension types documentation). You can specify an engine to direct the serialization. This can be one of pyarrow, or fastparquet, or auto. If the engine is NOT specified, then the pd.options.io.parquet.engine option is checked; if this is also auto, then pyarrow is tried, and falling back to fastparquet. See the documentation for pyarrow and fastparquet. Note These engines are very similar and should read/write nearly identical parquet format files. pyarrow>=8.0.0 supports timedelta data, fastparquet>=0.1.4 supports timezone aware datetimes. These libraries differ by having different underlying dependencies (fastparquet by using numba, while pyarrow uses a c-library). In [596]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(3, 6).astype("u1"), .....: "d": np.arange(4.0, 7.0, dtype="float64"), .....: "e": [True, False, True], .....: "f": pd.date_range("20130101", periods=3), .....: "g": pd.date_range("20130101", periods=3, tz="US/Eastern"), .....: "h": pd.Categorical(list("abc")), .....: "i": pd.Categorical(list("abc"), ordered=True), .....: } .....: ) .....: In [597]: df Out[597]: a b c d e f g h i 0 a 1 3 4.0 True 2013-01-01 2013-01-01 00:00:00-05:00 a a 1 b 2 4 5.0 False 2013-01-02 2013-01-02 00:00:00-05:00 b b 2 c 3 5 6.0 True 2013-01-03 2013-01-03 00:00:00-05:00 c c In [598]: df.dtypes Out[598]: a object b int64 c uint8 d float64 e bool f datetime64[ns] g datetime64[ns, US/Eastern] h category i category dtype: object Write to a parquet file. In [599]: df.to_parquet("example_pa.parquet", engine="pyarrow") In [600]: df.to_parquet("example_fp.parquet", engine="fastparquet") Read from a parquet file. In [601]: result = pd.read_parquet("example_fp.parquet", engine="fastparquet") In [602]: result = pd.read_parquet("example_pa.parquet", engine="pyarrow") In [603]: result.dtypes Out[603]: a object b int64 c uint8 d float64 e bool f datetime64[ns] g datetime64[ns, US/Eastern] h category i category dtype: object Read only certain columns of a parquet file. In [604]: result = pd.read_parquet( .....: "example_fp.parquet", .....: engine="fastparquet", .....: columns=["a", "b"], .....: ) .....: In [605]: result = pd.read_parquet( .....: "example_pa.parquet", .....: engine="pyarrow", .....: columns=["a", "b"], .....: ) .....: In [606]: result.dtypes Out[606]: a object b int64 dtype: object Handling indexes# Serializing a DataFrame to parquet may include the implicit index as one or more columns in the output file. Thus, this code: In [607]: df = pd.DataFrame({"a": [1, 2], "b": [3, 4]}) In [608]: df.to_parquet("test.parquet", engine="pyarrow") creates a parquet file with three columns if you use pyarrow for serialization: a, b, and __index_level_0__. If you’re using fastparquet, the index may or may not be written to the file. This unexpected extra column causes some databases like Amazon Redshift to reject the file, because that column doesn’t exist in the target table. If you want to omit a dataframe’s indexes when writing, pass index=False to to_parquet(): In [609]: df.to_parquet("test.parquet", index=False) This creates a parquet file with just the two expected columns, a and b. If your DataFrame has a custom index, you won’t get it back when you load this file into a DataFrame. Passing index=True will always write the index, even if that’s not the underlying engine’s default behavior. Partitioning Parquet files# Parquet supports partitioning of data based on the values of one or more columns. In [610]: df = pd.DataFrame({"a": [0, 0, 1, 1], "b": [0, 1, 0, 1]}) In [611]: df.to_parquet(path="test", engine="pyarrow", partition_cols=["a"], compression=None) The path specifies the parent directory to which data will be saved. The partition_cols are the column names by which the dataset will be partitioned. Columns are partitioned in the order they are given. The partition splits are determined by the unique values in the partition columns. The above example creates a partitioned dataset that may look like: test ├── a=0 │ ├── 0bac803e32dc42ae83fddfd029cbdebc.parquet │ └── ... └── a=1 ├── e6ab24a4f45147b49b54a662f0c412a3.parquet └── ... ORC# New in version 1.0.0. Similar to the parquet format, the ORC Format is a binary columnar serialization for data frames. It is designed to make reading data frames efficient. pandas provides both the reader and the writer for the ORC format, read_orc() and to_orc(). This requires the pyarrow library. Warning It is highly recommended to install pyarrow using conda due to some issues occurred by pyarrow. to_orc() requires pyarrow>=7.0.0. read_orc() and to_orc() are not supported on Windows yet, you can find valid environments on install optional dependencies. For supported dtypes please refer to supported ORC features in Arrow. Currently timezones in datetime columns are not preserved when a dataframe is converted into ORC files. In [612]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(4.0, 7.0, dtype="float64"), .....: "d": [True, False, True], .....: "e": pd.date_range("20130101", periods=3), .....: } .....: ) .....: In [613]: df Out[613]: a b c d e 0 a 1 4.0 True 2013-01-01 1 b 2 5.0 False 2013-01-02 2 c 3 6.0 True 2013-01-03 In [614]: df.dtypes Out[614]: a object b int64 c float64 d bool e datetime64[ns] dtype: object Write to an orc file. In [615]: df.to_orc("example_pa.orc", engine="pyarrow") Read from an orc file. In [616]: result = pd.read_orc("example_pa.orc") In [617]: result.dtypes Out[617]: a object b int64 c float64 d bool e datetime64[ns] dtype: object Read only certain columns of an orc file. In [618]: result = pd.read_orc( .....: "example_pa.orc", .....: columns=["a", "b"], .....: ) .....: In [619]: result.dtypes Out[619]: a object b int64 dtype: object SQL queries# The pandas.io.sql module provides a collection of query wrappers to both facilitate data retrieval and to reduce dependency on DB-specific API. Database abstraction is provided by SQLAlchemy if installed. In addition you will need a driver library for your database. Examples of such drivers are psycopg2 for PostgreSQL or pymysql for MySQL. For SQLite this is included in Python’s standard library by default. You can find an overview of supported drivers for each SQL dialect in the SQLAlchemy docs. If SQLAlchemy is not installed, a fallback is only provided for sqlite (and for mysql for backwards compatibility, but this is deprecated and will be removed in a future version). This mode requires a Python database adapter which respect the Python DB-API. See also some cookbook examples for some advanced strategies. The key functions are: read_sql_table(table_name, con[, schema, ...]) Read SQL database table into a DataFrame. read_sql_query(sql, con[, index_col, ...]) Read SQL query into a DataFrame. read_sql(sql, con[, index_col, ...]) Read SQL query or database table into a DataFrame. DataFrame.to_sql(name, con[, schema, ...]) Write records stored in a DataFrame to a SQL database. Note The function read_sql() is a convenience wrapper around read_sql_table() and read_sql_query() (and for backward compatibility) and will delegate to specific function depending on the provided input (database table name or sql query). Table names do not need to be quoted if they have special characters. In the following example, we use the SQlite SQL database engine. You can use a temporary SQLite database where data are stored in “memory”. To connect with SQLAlchemy you use the create_engine() function to create an engine object from database URI. You only need to create the engine once per database you are connecting to. For more information on create_engine() and the URI formatting, see the examples below and the SQLAlchemy documentation In [620]: from sqlalchemy import create_engine # Create your engine. In [621]: engine = create_engine("sqlite:///:memory:") If you want to manage your own connections you can pass one of those instead. The example below opens a connection to the database using a Python context manager that automatically closes the connection after the block has completed. See the SQLAlchemy docs for an explanation of how the database connection is handled. with engine.connect() as conn, conn.begin(): data = pd.read_sql_table("data", conn) Warning When you open a connection to a database you are also responsible for closing it. Side effects of leaving a connection open may include locking the database or other breaking behaviour. Writing DataFrames# Assuming the following data is in a DataFrame data, we can insert it into the database using to_sql(). id Date Col_1 Col_2 Col_3 26 2012-10-18 X 25.7 True 42 2012-10-19 Y -12.4 False 63 2012-10-20 Z 5.73 True In [622]: import datetime In [623]: c = ["id", "Date", "Col_1", "Col_2", "Col_3"] In [624]: d = [ .....: (26, datetime.datetime(2010, 10, 18), "X", 27.5, True), .....: (42, datetime.datetime(2010, 10, 19), "Y", -12.5, False), .....: (63, datetime.datetime(2010, 10, 20), "Z", 5.73, True), .....: ] .....: In [625]: data = pd.DataFrame(d, columns=c) In [626]: data Out[626]: id Date Col_1 Col_2 Col_3 0 26 2010-10-18 X 27.50 True 1 42 2010-10-19 Y -12.50 False 2 63 2010-10-20 Z 5.73 True In [627]: data.to_sql("data", engine) Out[627]: 3 With some databases, writing large DataFrames can result in errors due to packet size limitations being exceeded. This can be avoided by setting the chunksize parameter when calling to_sql. For example, the following writes data to the database in batches of 1000 rows at a time: In [628]: data.to_sql("data_chunked", engine, chunksize=1000) Out[628]: 3 SQL data types# to_sql() will try to map your data to an appropriate SQL data type based on the dtype of the data. When you have columns of dtype object, pandas will try to infer the data type. You can always override the default type by specifying the desired SQL type of any of the columns by using the dtype argument. This argument needs a dictionary mapping column names to SQLAlchemy types (or strings for the sqlite3 fallback mode). For example, specifying to use the sqlalchemy String type instead of the default Text type for string columns: In [629]: from sqlalchemy.types import String In [630]: data.to_sql("data_dtype", engine, dtype={"Col_1": String}) Out[630]: 3 Note Due to the limited support for timedelta’s in the different database flavors, columns with type timedelta64 will be written as integer values as nanoseconds to the database and a warning will be raised. Note Columns of category dtype will be converted to the dense representation as you would get with np.asarray(categorical) (e.g. for string categories this gives an array of strings). Because of this, reading the database table back in does not generate a categorical. Datetime data types# Using SQLAlchemy, to_sql() is capable of writing datetime data that is timezone naive or timezone aware. However, the resulting data stored in the database ultimately depends on the supported data type for datetime data of the database system being used. The following table lists supported data types for datetime data for some common databases. Other database dialects may have different data types for datetime data. Database SQL Datetime Types Timezone Support SQLite TEXT No MySQL TIMESTAMP or DATETIME No PostgreSQL TIMESTAMP or TIMESTAMP WITH TIME ZONE Yes When writing timezone aware data to databases that do not support timezones, the data will be written as timezone naive timestamps that are in local time with respect to the timezone. read_sql_table() is also capable of reading datetime data that is timezone aware or naive. When reading TIMESTAMP WITH TIME ZONE types, pandas will convert the data to UTC. Insertion method# The parameter method controls the SQL insertion clause used. Possible values are: None: Uses standard SQL INSERT clause (one per row). 'multi': Pass multiple values in a single INSERT clause. It uses a special SQL syntax not supported by all backends. This usually provides better performance for analytic databases like Presto and Redshift, but has worse performance for traditional SQL backend if the table contains many columns. For more information check the SQLAlchemy documentation. callable with signature (pd_table, conn, keys, data_iter): This can be used to implement a more performant insertion method based on specific backend dialect features. Example of a callable using PostgreSQL COPY clause: # Alternative to_sql() *method* for DBs that support COPY FROM import csv from io import StringIO def psql_insert_copy(table, conn, keys, data_iter): """ Execute SQL statement inserting data Parameters ---------- table : pandas.io.sql.SQLTable conn : sqlalchemy.engine.Engine or sqlalchemy.engine.Connection keys : list of str Column names data_iter : Iterable that iterates the values to be inserted """ # gets a DBAPI connection that can provide a cursor dbapi_conn = conn.connection with dbapi_conn.cursor() as cur: s_buf = StringIO() writer = csv.writer(s_buf) writer.writerows(data_iter) s_buf.seek(0) columns = ', '.join(['"{}"'.format(k) for k in keys]) if table.schema: table_name = '{}.{}'.format(table.schema, table.name) else: table_name = table.name sql = 'COPY {} ({}) FROM STDIN WITH CSV'.format( table_name, columns) cur.copy_expert(sql=sql, file=s_buf) Reading tables# read_sql_table() will read a database table given the table name and optionally a subset of columns to read. Note In order to use read_sql_table(), you must have the SQLAlchemy optional dependency installed. In [631]: pd.read_sql_table("data", engine) Out[631]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 X 27.50 True 1 1 42 2010-10-19 Y -12.50 False 2 2 63 2010-10-20 Z 5.73 True Note Note that pandas infers column dtypes from query outputs, and not by looking up data types in the physical database schema. For example, assume userid is an integer column in a table. Then, intuitively, select userid ... will return integer-valued series, while select cast(userid as text) ... will return object-valued (str) series. Accordingly, if the query output is empty, then all resulting columns will be returned as object-valued (since they are most general). If you foresee that your query will sometimes generate an empty result, you may want to explicitly typecast afterwards to ensure dtype integrity. You can also specify the name of the column as the DataFrame index, and specify a subset of columns to be read. In [632]: pd.read_sql_table("data", engine, index_col="id") Out[632]: index Date Col_1 Col_2 Col_3 id 26 0 2010-10-18 X 27.50 True 42 1 2010-10-19 Y -12.50 False 63 2 2010-10-20 Z 5.73 True In [633]: pd.read_sql_table("data", engine, columns=["Col_1", "Col_2"]) Out[633]: Col_1 Col_2 0 X 27.50 1 Y -12.50 2 Z 5.73 And you can explicitly force columns to be parsed as dates: In [634]: pd.read_sql_table("data", engine, parse_dates=["Date"]) Out[634]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 X 27.50 True 1 1 42 2010-10-19 Y -12.50 False 2 2 63 2010-10-20 Z 5.73 True If needed you can explicitly specify a format string, or a dict of arguments to pass to pandas.to_datetime(): pd.read_sql_table("data", engine, parse_dates={"Date": "%Y-%m-%d"}) pd.read_sql_table( "data", engine, parse_dates={"Date": {"format": "%Y-%m-%d %H:%M:%S"}}, ) You can check if a table exists using has_table() Schema support# Reading from and writing to different schema’s is supported through the schema keyword in the read_sql_table() and to_sql() functions. Note however that this depends on the database flavor (sqlite does not have schema’s). For example: df.to_sql("table", engine, schema="other_schema") pd.read_sql_table("table", engine, schema="other_schema") Querying# You can query using raw SQL in the read_sql_query() function. In this case you must use the SQL variant appropriate for your database. When using SQLAlchemy, you can also pass SQLAlchemy Expression language constructs, which are database-agnostic. In [635]: pd.read_sql_query("SELECT * FROM data", engine) Out[635]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 00:00:00.000000 X 27.50 1 1 1 42 2010-10-19 00:00:00.000000 Y -12.50 0 2 2 63 2010-10-20 00:00:00.000000 Z 5.73 1 Of course, you can specify a more “complex” query. In [636]: pd.read_sql_query("SELECT id, Col_1, Col_2 FROM data WHERE id = 42;", engine) Out[636]: id Col_1 Col_2 0 42 Y -12.5 The read_sql_query() function supports a chunksize argument. Specifying this will return an iterator through chunks of the query result: In [637]: df = pd.DataFrame(np.random.randn(20, 3), columns=list("abc")) In [638]: df.to_sql("data_chunks", engine, index=False) Out[638]: 20 In [639]: for chunk in pd.read_sql_query("SELECT * FROM data_chunks", engine, chunksize=5): .....: print(chunk) .....: a b c 0 0.070470 0.901320 0.937577 1 0.295770 1.420548 -0.005283 2 -1.518598 -0.730065 0.226497 3 -2.061465 0.632115 0.853619 4 2.719155 0.139018 0.214557 a b c 0 -1.538924 -0.366973 -0.748801 1 -0.478137 -1.559153 -3.097759 2 -2.320335 -0.221090 0.119763 3 0.608228 1.064810 -0.780506 4 -2.736887 0.143539 1.170191 a b c 0 -1.573076 0.075792 -1.722223 1 -0.774650 0.803627 0.221665 2 0.584637 0.147264 1.057825 3 -0.284136 0.912395 1.552808 4 0.189376 -0.109830 0.539341 a b c 0 0.592591 -0.155407 -1.356475 1 0.833837 1.524249 1.606722 2 -0.029487 -0.051359 1.700152 3 0.921484 -0.926347 0.979818 4 0.182380 -0.186376 0.049820 You can also run a plain query without creating a DataFrame with execute(). This is useful for queries that don’t return values, such as INSERT. This is functionally equivalent to calling execute on the SQLAlchemy engine or db connection object. Again, you must use the SQL syntax variant appropriate for your database. from pandas.io import sql sql.execute("SELECT * FROM table_name", engine) sql.execute( "INSERT INTO table_name VALUES(?, ?, ?)", engine, params=[("id", 1, 12.2, True)] ) Engine connection examples# To connect with SQLAlchemy you use the create_engine() function to create an engine object from database URI. You only need to create the engine once per database you are connecting to. from sqlalchemy import create_engine engine = create_engine("postgresql://scott:[email protected]:5432/mydatabase") engine = create_engine("mysql+mysqldb://scott:[email protected]/foo") engine = create_engine("oracle://scott:[email protected]:1521/sidname") engine = create_engine("mssql+pyodbc://mydsn") # sqlite://<nohostname>/<path> # where <path> is relative: engine = create_engine("sqlite:///foo.db") # or absolute, starting with a slash: engine = create_engine("sqlite:////absolute/path/to/foo.db") For more information see the examples the SQLAlchemy documentation Advanced SQLAlchemy queries# You can use SQLAlchemy constructs to describe your query. Use sqlalchemy.text() to specify query parameters in a backend-neutral way In [640]: import sqlalchemy as sa In [641]: pd.read_sql( .....: sa.text("SELECT * FROM data where Col_1=:col1"), engine, params={"col1": "X"} .....: ) .....: Out[641]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 00:00:00.000000 X 27.5 1 If you have an SQLAlchemy description of your database you can express where conditions using SQLAlchemy expressions In [642]: metadata = sa.MetaData() In [643]: data_table = sa.Table( .....: "data", .....: metadata, .....: sa.Column("index", sa.Integer), .....: sa.Column("Date", sa.DateTime), .....: sa.Column("Col_1", sa.String), .....: sa.Column("Col_2", sa.Float), .....: sa.Column("Col_3", sa.Boolean), .....: ) .....: In [644]: pd.read_sql(sa.select([data_table]).where(data_table.c.Col_3 is True), engine) Out[644]: Empty DataFrame Columns: [index, Date, Col_1, Col_2, Col_3] Index: [] You can combine SQLAlchemy expressions with parameters passed to read_sql() using sqlalchemy.bindparam() In [645]: import datetime as dt In [646]: expr = sa.select([data_table]).where(data_table.c.Date > sa.bindparam("date")) In [647]: pd.read_sql(expr, engine, params={"date": dt.datetime(2010, 10, 18)}) Out[647]: index Date Col_1 Col_2 Col_3 0 1 2010-10-19 Y -12.50 False 1 2 2010-10-20 Z 5.73 True Sqlite fallback# The use of sqlite is supported without using SQLAlchemy. This mode requires a Python database adapter which respect the Python DB-API. You can create connections like so: import sqlite3 con = sqlite3.connect(":memory:") And then issue the following queries: data.to_sql("data", con) pd.read_sql_query("SELECT * FROM data", con) Google BigQuery# Warning Starting in 0.20.0, pandas has split off Google BigQuery support into the separate package pandas-gbq. You can pip install pandas-gbq to get it. The pandas-gbq package provides functionality to read/write from Google BigQuery. pandas integrates with this external package. if pandas-gbq is installed, you can use the pandas methods pd.read_gbq and DataFrame.to_gbq, which will call the respective functions from pandas-gbq. Full documentation can be found here. Stata format# Writing to stata format# The method to_stata() will write a DataFrame into a .dta file. The format version of this file is always 115 (Stata 12). In [648]: df = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [649]: df.to_stata("stata.dta") Stata data files have limited data type support; only strings with 244 or fewer characters, int8, int16, int32, float32 and float64 can be stored in .dta files. Additionally, Stata reserves certain values to represent missing data. Exporting a non-missing value that is outside of the permitted range in Stata for a particular data type will retype the variable to the next larger size. For example, int8 values are restricted to lie between -127 and 100 in Stata, and so variables with values above 100 will trigger a conversion to int16. nan values in floating points data types are stored as the basic missing data type (. in Stata). Note It is not possible to export missing data values for integer data types. The Stata writer gracefully handles other data types including int64, bool, uint8, uint16, uint32 by casting to the smallest supported type that can represent the data. For example, data with a type of uint8 will be cast to int8 if all values are less than 100 (the upper bound for non-missing int8 data in Stata), or, if values are outside of this range, the variable is cast to int16. Warning Conversion from int64 to float64 may result in a loss of precision if int64 values are larger than 2**53. Warning StataWriter and to_stata() only support fixed width strings containing up to 244 characters, a limitation imposed by the version 115 dta file format. Attempting to write Stata dta files with strings longer than 244 characters raises a ValueError. Reading from Stata format# The top-level function read_stata will read a dta file and return either a DataFrame or a StataReader that can be used to read the file incrementally. In [650]: pd.read_stata("stata.dta") Out[650]: index A B 0 0 -1.690072 0.405144 1 1 -1.511309 -1.531396 2 2 0.572698 -1.106845 3 3 -1.185859 0.174564 4 4 0.603797 -1.796129 5 5 -0.791679 1.173795 6 6 -0.277710 1.859988 7 7 -0.258413 1.251808 8 8 1.443262 0.441553 9 9 1.168163 -2.054946 Specifying a chunksize yields a StataReader instance that can be used to read chunksize lines from the file at a time. The StataReader object can be used as an iterator. In [651]: with pd.read_stata("stata.dta", chunksize=3) as reader: .....: for df in reader: .....: print(df.shape) .....: (3, 3) (3, 3) (3, 3) (1, 3) For more fine-grained control, use iterator=True and specify chunksize with each call to read(). In [652]: with pd.read_stata("stata.dta", iterator=True) as reader: .....: chunk1 = reader.read(5) .....: chunk2 = reader.read(5) .....: Currently the index is retrieved as a column. The parameter convert_categoricals indicates whether value labels should be read and used to create a Categorical variable from them. Value labels can also be retrieved by the function value_labels, which requires read() to be called before use. The parameter convert_missing indicates whether missing value representations in Stata should be preserved. If False (the default), missing values are represented as np.nan. If True, missing values are represented using StataMissingValue objects, and columns containing missing values will have object data type. Note read_stata() and StataReader support .dta formats 113-115 (Stata 10-12), 117 (Stata 13), and 118 (Stata 14). Note Setting preserve_dtypes=False will upcast to the standard pandas data types: int64 for all integer types and float64 for floating point data. By default, the Stata data types are preserved when importing. Categorical data# Categorical data can be exported to Stata data files as value labeled data. The exported data consists of the underlying category codes as integer data values and the categories as value labels. Stata does not have an explicit equivalent to a Categorical and information about whether the variable is ordered is lost when exporting. Warning Stata only supports string value labels, and so str is called on the categories when exporting data. Exporting Categorical variables with non-string categories produces a warning, and can result a loss of information if the str representations of the categories are not unique. Labeled data can similarly be imported from Stata data files as Categorical variables using the keyword argument convert_categoricals (True by default). The keyword argument order_categoricals (True by default) determines whether imported Categorical variables are ordered. Note When importing categorical data, the values of the variables in the Stata data file are not preserved since Categorical variables always use integer data types between -1 and n-1 where n is the number of categories. If the original values in the Stata data file are required, these can be imported by setting convert_categoricals=False, which will import original data (but not the variable labels). The original values can be matched to the imported categorical data since there is a simple mapping between the original Stata data values and the category codes of imported Categorical variables: missing values are assigned code -1, and the smallest original value is assigned 0, the second smallest is assigned 1 and so on until the largest original value is assigned the code n-1. Note Stata supports partially labeled series. These series have value labels for some but not all data values. Importing a partially labeled series will produce a Categorical with string categories for the values that are labeled and numeric categories for values with no label. SAS formats# The top-level function read_sas() can read (but not write) SAS XPORT (.xpt) and (since v0.18.0) SAS7BDAT (.sas7bdat) format files. SAS files only contain two value types: ASCII text and floating point values (usually 8 bytes but sometimes truncated). For xport files, there is no automatic type conversion to integers, dates, or categoricals. For SAS7BDAT files, the format codes may allow date variables to be automatically converted to dates. By default the whole file is read and returned as a DataFrame. Specify a chunksize or use iterator=True to obtain reader objects (XportReader or SAS7BDATReader) for incrementally reading the file. The reader objects also have attributes that contain additional information about the file and its variables. Read a SAS7BDAT file: df = pd.read_sas("sas_data.sas7bdat") Obtain an iterator and read an XPORT file 100,000 lines at a time: def do_something(chunk): pass with pd.read_sas("sas_xport.xpt", chunk=100000) as rdr: for chunk in rdr: do_something(chunk) The specification for the xport file format is available from the SAS web site. No official documentation is available for the SAS7BDAT format. SPSS formats# New in version 0.25.0. The top-level function read_spss() can read (but not write) SPSS SAV (.sav) and ZSAV (.zsav) format files. SPSS files contain column names. By default the whole file is read, categorical columns are converted into pd.Categorical, and a DataFrame with all columns is returned. Specify the usecols parameter to obtain a subset of columns. Specify convert_categoricals=False to avoid converting categorical columns into pd.Categorical. Read an SPSS file: df = pd.read_spss("spss_data.sav") Extract a subset of columns contained in usecols from an SPSS file and avoid converting categorical columns into pd.Categorical: df = pd.read_spss( "spss_data.sav", usecols=["foo", "bar"], convert_categoricals=False, ) More information about the SAV and ZSAV file formats is available here. Other file formats# pandas itself only supports IO with a limited set of file formats that map cleanly to its tabular data model. For reading and writing other file formats into and from pandas, we recommend these packages from the broader community. netCDF# xarray provides data structures inspired by the pandas DataFrame for working with multi-dimensional datasets, with a focus on the netCDF file format and easy conversion to and from pandas. Performance considerations# This is an informal comparison of various IO methods, using pandas 0.24.2. Timings are machine dependent and small differences should be ignored. In [1]: sz = 1000000 In [2]: df = pd.DataFrame({'A': np.random.randn(sz), 'B': [1] * sz}) In [3]: df.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 1000000 entries, 0 to 999999 Data columns (total 2 columns): A 1000000 non-null float64 B 1000000 non-null int64 dtypes: float64(1), int64(1) memory usage: 15.3 MB The following test functions will be used below to compare the performance of several IO methods: import numpy as np import os sz = 1000000 df = pd.DataFrame({"A": np.random.randn(sz), "B": [1] * sz}) sz = 1000000 np.random.seed(42) df = pd.DataFrame({"A": np.random.randn(sz), "B": [1] * sz}) def test_sql_write(df): if os.path.exists("test.sql"): os.remove("test.sql") sql_db = sqlite3.connect("test.sql") df.to_sql(name="test_table", con=sql_db) sql_db.close() def test_sql_read(): sql_db = sqlite3.connect("test.sql") pd.read_sql_query("select * from test_table", sql_db) sql_db.close() def test_hdf_fixed_write(df): df.to_hdf("test_fixed.hdf", "test", mode="w") def test_hdf_fixed_read(): pd.read_hdf("test_fixed.hdf", "test") def test_hdf_fixed_write_compress(df): df.to_hdf("test_fixed_compress.hdf", "test", mode="w", complib="blosc") def test_hdf_fixed_read_compress(): pd.read_hdf("test_fixed_compress.hdf", "test") def test_hdf_table_write(df): df.to_hdf("test_table.hdf", "test", mode="w", format="table") def test_hdf_table_read(): pd.read_hdf("test_table.hdf", "test") def test_hdf_table_write_compress(df): df.to_hdf( "test_table_compress.hdf", "test", mode="w", complib="blosc", format="table" ) def test_hdf_table_read_compress(): pd.read_hdf("test_table_compress.hdf", "test") def test_csv_write(df): df.to_csv("test.csv", mode="w") def test_csv_read(): pd.read_csv("test.csv", index_col=0) def test_feather_write(df): df.to_feather("test.feather") def test_feather_read(): pd.read_feather("test.feather") def test_pickle_write(df): df.to_pickle("test.pkl") def test_pickle_read(): pd.read_pickle("test.pkl") def test_pickle_write_compress(df): df.to_pickle("test.pkl.compress", compression="xz") def test_pickle_read_compress(): pd.read_pickle("test.pkl.compress", compression="xz") def test_parquet_write(df): df.to_parquet("test.parquet") def test_parquet_read(): pd.read_parquet("test.parquet") When writing, the top three functions in terms of speed are test_feather_write, test_hdf_fixed_write and test_hdf_fixed_write_compress. In [4]: %timeit test_sql_write(df) 3.29 s ± 43.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [5]: %timeit test_hdf_fixed_write(df) 19.4 ms ± 560 µs per loop (mean ± std. dev. of 7 runs, 1 loop each) In [6]: %timeit test_hdf_fixed_write_compress(df) 19.6 ms ± 308 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [7]: %timeit test_hdf_table_write(df) 449 ms ± 5.61 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [8]: %timeit test_hdf_table_write_compress(df) 448 ms ± 11.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [9]: %timeit test_csv_write(df) 3.66 s ± 26.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [10]: %timeit test_feather_write(df) 9.75 ms ± 117 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [11]: %timeit test_pickle_write(df) 30.1 ms ± 229 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [12]: %timeit test_pickle_write_compress(df) 4.29 s ± 15.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [13]: %timeit test_parquet_write(df) 67.6 ms ± 706 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) When reading, the top three functions in terms of speed are test_feather_read, test_pickle_read and test_hdf_fixed_read. In [14]: %timeit test_sql_read() 1.77 s ± 17.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [15]: %timeit test_hdf_fixed_read() 19.4 ms ± 436 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [16]: %timeit test_hdf_fixed_read_compress() 19.5 ms ± 222 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [17]: %timeit test_hdf_table_read() 38.6 ms ± 857 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [18]: %timeit test_hdf_table_read_compress() 38.8 ms ± 1.49 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) In [19]: %timeit test_csv_read() 452 ms ± 9.04 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [20]: %timeit test_feather_read() 12.4 ms ± 99.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [21]: %timeit test_pickle_read() 18.4 ms ± 191 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [22]: %timeit test_pickle_read_compress() 915 ms ± 7.48 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [23]: %timeit test_parquet_read() 24.4 ms ± 146 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) The files test.pkl.compress, test.parquet and test.feather took the least space on disk (in bytes). 29519500 Oct 10 06:45 test.csv 16000248 Oct 10 06:45 test.feather 8281983 Oct 10 06:49 test.parquet 16000857 Oct 10 06:47 test.pkl 7552144 Oct 10 06:48 test.pkl.compress 34816000 Oct 10 06:42 test.sql 24009288 Oct 10 06:43 test_fixed.hdf 24009288 Oct 10 06:43 test_fixed_compress.hdf 24458940 Oct 10 06:44 test_table.hdf 24458940 Oct 10 06:44 test_table_compress.hdf
211
1,017
Read a file from a folder and extract a specific key from the file and save as in CSV file I'm new to Python and the task I am performing is to extract a specific key value from a list of .iris ( which contains the list of nested dictionary format) files in a specific directory. I wanted to extract the specific value and save it as a new .csv file and repeat it for all other files. Below is my sample of .iris file from which I should extract only for the these keys ('uid','enabled','login','name'). {"streamType":"user", "uid":17182, "enabled":true, "login":"xyz", "name":"abcdef", "comment":"", "authSms":"", "email":"", "phone":"", "location":"", "extraLdapOu":"", "mand":997, "global":{ "userAccount":"View", "uid":"", "retention":"No", "enabled":"", "messages":"Change"}, "grants":[{"mand":997,"role":1051,"passOnToSubMand":true}], I am trying to convert the .iris file to .json and reading the files one by, but unfortunately, I am not getting the exact output as desired. Please, could anyone help me? My code (added from comments): import os import csv path = '' os.chdir(path) # Read iris File def read_iris_file(file_path): with open(file_path, 'r') as f: print(f.read()) # iterate through all files for file in os.listdir(): # Check whether file is in iris format or not if file.endswith(".iris"): file_path = f"{path}\{file}" # call read iris file function print(read_iris_file(file_path))
[CLS] Python Read iris File import csv path call read [SEP] _ 2 Col _ 3 0 1 2010 - 10 - 19 Y - 12. 50 False 1 2 2010 - 10 - 20 Z 5. 73 True Sqlite fallback # The use of sqlite is supported without using SQLAlchemy. This mode requires a Python database adapter which respect the Python DB - API. You can create connections like so : import sqlite3 con = sqlite3. connect /
Your files contain data in JSON format, so we can use built-in json module to parse it. To iterate over files with certain extension you can use pathlib.glob() with next pattern "*.iris". Then we can use csv.DictWriter() and pass "ignore" to extrasaction argument which will make DictWriter ignore keys which we don't need and write only those which we passed to fieldnames argument. Code: import csv import json from pathlib import Path path = Path(r"path/to/folder") keys = "uid", "enabled", "login", "name" with open(path / "result.csv", "w", newline="") as out_f: writer = csv.DictWriter(out_f, fieldnames=keys, extrasaction='ignore') writer.writeheader() for file in path.glob("*.iris"): with open(file) as inp_f: data = json.load(inp_f) writer.writerow(data)
60,677,367
Writing excel formulas on pandas DataFrame that depends on Row/Column values
<p>Suppose that I have the following df:</p> <pre><code> Column1 Column2 0 19843 30 1 19842 35 2 19841 40 3 19840 45 4 19839 50 5 19838 55 </code></pre> <p>I need to generate a third column that depends of Column1, Column2 and row numbem, like de following:</p> <pre><code>column3_formula = '"={row_number}*(Column1 + Column2)"' </code></pre> <p>I Know that this is kinda weird but Im trying to abstract and simplify my real problem ( my Df has more than 50 columns and I need to add 30 more columns that are Excel formulas ). I already have a way to "parse" the DF Column Names to "Excel real columns" (A3, or C2...):</p> <pre><code>def excel_column(column_number): letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' quot, rem = divmod(column_number - 1, len(letters)) return ( excel_column(quot) + chr(rem + ord('A')) if column_number != 0 else '' ) attributes_excel_column_mapping = { v: excel_column(k) for k, v in enumerate(['Column1', 'Column2'], 1) } </code></pre> <p>With this structure I can do things like <code>column3_formula.format(**attributes_excel_column_mapping)</code> but how can I feel the row number to insert this formulas like <code>my_df['Column3] = ...</code> ?</p>
60,707,726
"2020-03-13T21:22:01.717000"
2
null
-1
144
python|pandas
<p>What I needed was the "name" attribute of row when I use the apply method. Example:</p> <pre><code> self.data_frame['column'] = self.data_frame.apply( lambda row: formula.format( **column_mapping, row_number=row.name ), axis=1 ) </code></pre> <p>where formula is a string like </p> <pre><code>formula = "=IFERROR({column1}{row_number}&gt;0;1 ..... )" and </code></pre> <p>column_mapping = { v: excel_column(k) for k, v in enumerate(ordered_attributes, 1) }</p> <pre><code> </code></pre>
"2020-03-16T14:24:16.460000"
0
https://pandas.pydata.org/docs/user_guide/io.html
IO tools (text, CSV, HDF5, …)# IO tools (text, CSV, HDF5, …)# The pandas I/O API is a set of top level reader functions accessed like pandas.read_csv() that generally return a pandas object. The corresponding writer functions are object methods that are accessed like DataFrame.to_csv(). Below is a table containing available readers and What I needed was the "name" attribute of row when I use the apply method. Example: self.data_frame['column'] = self.data_frame.apply( lambda row: formula.format( **column_mapping, row_number=row.name ), axis=1 ) where formula is a string like formula = "=IFERROR({column1}{row_number}>0;1 ..... )" and column_mapping = { v: excel_column(k) for k, v in enumerate(ordered_attributes, 1) } writers. Format Type Data Description Reader Writer text CSV read_csv to_csv text Fixed-Width Text File read_fwf text JSON read_json to_json text HTML read_html to_html text LaTeX Styler.to_latex text XML read_xml to_xml text Local clipboard read_clipboard to_clipboard binary MS Excel read_excel to_excel binary OpenDocument read_excel binary HDF5 Format read_hdf to_hdf binary Feather Format read_feather to_feather binary Parquet Format read_parquet to_parquet binary ORC Format read_orc to_orc binary Stata read_stata to_stata binary SAS read_sas binary SPSS read_spss binary Python Pickle Format read_pickle to_pickle SQL SQL read_sql to_sql SQL Google BigQuery read_gbq to_gbq Here is an informal performance comparison for some of these IO methods. Note For examples that use the StringIO class, make sure you import it with from io import StringIO for Python 3. CSV & text files# The workhorse function for reading text files (a.k.a. flat files) is read_csv(). See the cookbook for some advanced strategies. Parsing options# read_csv() accepts the following common arguments: Basic# filepath_or_buffervariousEither a path to a file (a str, pathlib.Path, or py:py._path.local.LocalPath), URL (including http, ftp, and S3 locations), or any object with a read() method (such as an open file or StringIO). sepstr, defaults to ',' for read_csv(), \t for read_table()Delimiter to use. If sep is None, the C engine cannot automatically detect the separator, but the Python parsing engine can, meaning the latter will be used and automatically detect the separator by Python’s builtin sniffer tool, csv.Sniffer. In addition, separators longer than 1 character and different from '\s+' will be interpreted as regular expressions and will also force the use of the Python parsing engine. Note that regex delimiters are prone to ignoring quoted data. Regex example: '\\r\\t'. delimiterstr, default NoneAlternative argument name for sep. delim_whitespaceboolean, default FalseSpecifies whether or not whitespace (e.g. ' ' or '\t') will be used as the delimiter. Equivalent to setting sep='\s+'. If this option is set to True, nothing should be passed in for the delimiter parameter. Column and index locations and names# headerint or list of ints, default 'infer'Row number(s) to use as the column names, and the start of the data. Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first line of the file, if column names are passed explicitly then the behavior is identical to header=None. Explicitly pass header=0 to be able to replace existing names. The header can be a list of ints that specify row locations for a MultiIndex on the columns e.g. [0,1,3]. Intervening rows that are not specified will be skipped (e.g. 2 in this example is skipped). Note that this parameter ignores commented lines and empty lines if skip_blank_lines=True, so header=0 denotes the first line of data rather than the first line of the file. namesarray-like, default NoneList of column names to use. If file contains no header row, then you should explicitly pass header=None. Duplicates in this list are not allowed. index_colint, str, sequence of int / str, or False, optional, default NoneColumn(s) to use as the row labels of the DataFrame, either given as string name or column index. If a sequence of int / str is given, a MultiIndex is used. Note index_col=False can be used to force pandas to not use the first column as the index, e.g. when you have a malformed file with delimiters at the end of each line. The default value of None instructs pandas to guess. If the number of fields in the column header row is equal to the number of fields in the body of the data file, then a default index is used. If it is larger, then the first columns are used as index so that the remaining number of fields in the body are equal to the number of fields in the header. The first row after the header is used to determine the number of columns, which will go into the index. If the subsequent rows contain less columns than the first row, they are filled with NaN. This can be avoided through usecols. This ensures that the columns are taken as is and the trailing data are ignored. usecolslist-like or callable, default NoneReturn a subset of the columns. If list-like, all elements must either be positional (i.e. integer indices into the document columns) or strings that correspond to column names provided either by the user in names or inferred from the document header row(s). If names are given, the document header row(s) are not taken into account. For example, a valid list-like usecols parameter would be [0, 1, 2] or ['foo', 'bar', 'baz']. Element order is ignored, so usecols=[0, 1] is the same as [1, 0]. To instantiate a DataFrame from data with element order preserved use pd.read_csv(data, usecols=['foo', 'bar'])[['foo', 'bar']] for columns in ['foo', 'bar'] order or pd.read_csv(data, usecols=['foo', 'bar'])[['bar', 'foo']] for ['bar', 'foo'] order. If callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True: In [1]: import pandas as pd In [2]: from io import StringIO In [3]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [4]: pd.read_csv(StringIO(data)) Out[4]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [5]: pd.read_csv(StringIO(data), usecols=lambda x: x.upper() in ["COL1", "COL3"]) Out[5]: col1 col3 0 a 1 1 a 2 2 c 3 Using this parameter results in much faster parsing time and lower memory usage when using the c engine. The Python engine loads the data first before deciding which columns to drop. squeezeboolean, default FalseIf the parsed data only contains one column then return a Series. Deprecated since version 1.4.0: Append .squeeze("columns") to the call to {func_name} to squeeze the data. prefixstr, default NonePrefix to add to column numbers when no header, e.g. ‘X’ for X0, X1, … Deprecated since version 1.4.0: Use a list comprehension on the DataFrame’s columns after calling read_csv. In [6]: data = "col1,col2,col3\na,b,1" In [7]: df = pd.read_csv(StringIO(data)) In [8]: df.columns = [f"pre_{col}" for col in df.columns] In [9]: df Out[9]: pre_col1 pre_col2 pre_col3 0 a b 1 mangle_dupe_colsboolean, default TrueDuplicate columns will be specified as ‘X’, ‘X.1’…’X.N’, rather than ‘X’…’X’. Passing in False will cause data to be overwritten if there are duplicate names in the columns. Deprecated since version 1.5.0: The argument was never implemented, and a new argument where the renaming pattern can be specified will be added instead. General parsing configuration# dtypeType name or dict of column -> type, default NoneData type for data or columns. E.g. {'a': np.float64, 'b': np.int32, 'c': 'Int64'} Use str or object together with suitable na_values settings to preserve and not interpret dtype. If converters are specified, they will be applied INSTEAD of dtype conversion. New in version 1.5.0: Support for defaultdict was added. Specify a defaultdict as input where the default determines the dtype of the columns which are not explicitly listed. engine{'c', 'python', 'pyarrow'}Parser engine to use. The C and pyarrow engines are faster, while the python engine is currently more feature-complete. Multithreading is currently only supported by the pyarrow engine. New in version 1.4.0: The “pyarrow” engine was added as an experimental engine, and some features are unsupported, or may not work correctly, with this engine. convertersdict, default NoneDict of functions for converting values in certain columns. Keys can either be integers or column labels. true_valueslist, default NoneValues to consider as True. false_valueslist, default NoneValues to consider as False. skipinitialspaceboolean, default FalseSkip spaces after delimiter. skiprowslist-like or integer, default NoneLine numbers to skip (0-indexed) or number of lines to skip (int) at the start of the file. If callable, the callable function will be evaluated against the row indices, returning True if the row should be skipped and False otherwise: In [10]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [11]: pd.read_csv(StringIO(data)) Out[11]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [12]: pd.read_csv(StringIO(data), skiprows=lambda x: x % 2 != 0) Out[12]: col1 col2 col3 0 a b 2 skipfooterint, default 0Number of lines at bottom of file to skip (unsupported with engine=’c’). nrowsint, default NoneNumber of rows of file to read. Useful for reading pieces of large files. low_memoryboolean, default TrueInternally process the file in chunks, resulting in lower memory use while parsing, but possibly mixed type inference. To ensure no mixed types either set False, or specify the type with the dtype parameter. Note that the entire file is read into a single DataFrame regardless, use the chunksize or iterator parameter to return the data in chunks. (Only valid with C parser) memory_mapboolean, default FalseIf a filepath is provided for filepath_or_buffer, map the file object directly onto memory and access the data directly from there. Using this option can improve performance because there is no longer any I/O overhead. NA and missing data handling# na_valuesscalar, str, list-like, or dict, default NoneAdditional strings to recognize as NA/NaN. If dict passed, specific per-column NA values. See na values const below for a list of the values interpreted as NaN by default. keep_default_naboolean, default TrueWhether or not to include the default NaN values when parsing the data. Depending on whether na_values is passed in, the behavior is as follows: If keep_default_na is True, and na_values are specified, na_values is appended to the default NaN values used for parsing. If keep_default_na is True, and na_values are not specified, only the default NaN values are used for parsing. If keep_default_na is False, and na_values are specified, only the NaN values specified na_values are used for parsing. If keep_default_na is False, and na_values are not specified, no strings will be parsed as NaN. Note that if na_filter is passed in as False, the keep_default_na and na_values parameters will be ignored. na_filterboolean, default TrueDetect missing value markers (empty strings and the value of na_values). In data without any NAs, passing na_filter=False can improve the performance of reading a large file. verboseboolean, default FalseIndicate number of NA values placed in non-numeric columns. skip_blank_linesboolean, default TrueIf True, skip over blank lines rather than interpreting as NaN values. Datetime handling# parse_datesboolean or list of ints or names or list of lists or dict, default False. If True -> try parsing the index. If [1, 2, 3] -> try parsing columns 1, 2, 3 each as a separate date column. If [[1, 3]] -> combine columns 1 and 3 and parse as a single date column. If {'foo': [1, 3]} -> parse columns 1, 3 as date and call result ‘foo’. Note A fast-path exists for iso8601-formatted dates. infer_datetime_formatboolean, default FalseIf True and parse_dates is enabled for a column, attempt to infer the datetime format to speed up the processing. keep_date_colboolean, default FalseIf True and parse_dates specifies combining multiple columns then keep the original columns. date_parserfunction, default NoneFunction to use for converting a sequence of string columns to an array of datetime instances. The default uses dateutil.parser.parser to do the conversion. pandas will try to call date_parser in three different ways, advancing to the next if an exception occurs: 1) Pass one or more arrays (as defined by parse_dates) as arguments; 2) concatenate (row-wise) the string values from the columns defined by parse_dates into a single array and pass that; and 3) call date_parser once for each row using one or more strings (corresponding to the columns defined by parse_dates) as arguments. dayfirstboolean, default FalseDD/MM format dates, international and European format. cache_datesboolean, default TrueIf True, use a cache of unique, converted dates to apply the datetime conversion. May produce significant speed-up when parsing duplicate date strings, especially ones with timezone offsets. New in version 0.25.0. Iteration# iteratorboolean, default FalseReturn TextFileReader object for iteration or getting chunks with get_chunk(). chunksizeint, default NoneReturn TextFileReader object for iteration. See iterating and chunking below. Quoting, compression, and file format# compression{'infer', 'gzip', 'bz2', 'zip', 'xz', 'zstd', None, dict}, default 'infer'For on-the-fly decompression of on-disk data. If ‘infer’, then use gzip, bz2, zip, xz, or zstandard if filepath_or_buffer is path-like ending in ‘.gz’, ‘.bz2’, ‘.zip’, ‘.xz’, ‘.zst’, respectively, and no decompression otherwise. If using ‘zip’, the ZIP file must contain only one data file to be read in. Set to None for no decompression. Can also be a dict with key 'method' set to one of {'zip', 'gzip', 'bz2', 'zstd'} and other key-value pairs are forwarded to zipfile.ZipFile, gzip.GzipFile, bz2.BZ2File, or zstandard.ZstdDecompressor. As an example, the following could be passed for faster compression and to create a reproducible gzip archive: compression={'method': 'gzip', 'compresslevel': 1, 'mtime': 1}. Changed in version 1.1.0: dict option extended to support gzip and bz2. Changed in version 1.2.0: Previous versions forwarded dict entries for ‘gzip’ to gzip.open. thousandsstr, default NoneThousands separator. decimalstr, default '.'Character to recognize as decimal point. E.g. use ',' for European data. float_precisionstring, default NoneSpecifies which converter the C engine should use for floating-point values. The options are None for the ordinary converter, high for the high-precision converter, and round_trip for the round-trip converter. lineterminatorstr (length 1), default NoneCharacter to break file into lines. Only valid with C parser. quotecharstr (length 1)The character used to denote the start and end of a quoted item. Quoted items can include the delimiter and it will be ignored. quotingint or csv.QUOTE_* instance, default 0Control field quoting behavior per csv.QUOTE_* constants. Use one of QUOTE_MINIMAL (0), QUOTE_ALL (1), QUOTE_NONNUMERIC (2) or QUOTE_NONE (3). doublequoteboolean, default TrueWhen quotechar is specified and quoting is not QUOTE_NONE, indicate whether or not to interpret two consecutive quotechar elements inside a field as a single quotechar element. escapecharstr (length 1), default NoneOne-character string used to escape delimiter when quoting is QUOTE_NONE. commentstr, default NoneIndicates remainder of line should not be parsed. If found at the beginning of a line, the line will be ignored altogether. This parameter must be a single character. Like empty lines (as long as skip_blank_lines=True), fully commented lines are ignored by the parameter header but not by skiprows. For example, if comment='#', parsing ‘#empty\na,b,c\n1,2,3’ with header=0 will result in ‘a,b,c’ being treated as the header. encodingstr, default NoneEncoding to use for UTF when reading/writing (e.g. 'utf-8'). List of Python standard encodings. dialectstr or csv.Dialect instance, default NoneIf provided, this parameter will override values (default or not) for the following parameters: delimiter, doublequote, escapechar, skipinitialspace, quotechar, and quoting. If it is necessary to override values, a ParserWarning will be issued. See csv.Dialect documentation for more details. Error handling# error_bad_linesboolean, optional, default NoneLines with too many fields (e.g. a csv line with too many commas) will by default cause an exception to be raised, and no DataFrame will be returned. If False, then these “bad lines” will dropped from the DataFrame that is returned. See bad lines below. Deprecated since version 1.3.0: The on_bad_lines parameter should be used instead to specify behavior upon encountering a bad line instead. warn_bad_linesboolean, optional, default NoneIf error_bad_lines is False, and warn_bad_lines is True, a warning for each “bad line” will be output. Deprecated since version 1.3.0: The on_bad_lines parameter should be used instead to specify behavior upon encountering a bad line instead. on_bad_lines(‘error’, ‘warn’, ‘skip’), default ‘error’Specifies what to do upon encountering a bad line (a line with too many fields). Allowed values are : ‘error’, raise an ParserError when a bad line is encountered. ‘warn’, print a warning when a bad line is encountered and skip that line. ‘skip’, skip bad lines without raising or warning when they are encountered. New in version 1.3.0. Specifying column data types# You can indicate the data type for the whole DataFrame or individual columns: In [13]: import numpy as np In [14]: data = "a,b,c,d\n1,2,3,4\n5,6,7,8\n9,10,11" In [15]: print(data) a,b,c,d 1,2,3,4 5,6,7,8 9,10,11 In [16]: df = pd.read_csv(StringIO(data), dtype=object) In [17]: df Out[17]: a b c d 0 1 2 3 4 1 5 6 7 8 2 9 10 11 NaN In [18]: df["a"][0] Out[18]: '1' In [19]: df = pd.read_csv(StringIO(data), dtype={"b": object, "c": np.float64, "d": "Int64"}) In [20]: df.dtypes Out[20]: a int64 b object c float64 d Int64 dtype: object Fortunately, pandas offers more than one way to ensure that your column(s) contain only one dtype. If you’re unfamiliar with these concepts, you can see here to learn more about dtypes, and here to learn more about object conversion in pandas. For instance, you can use the converters argument of read_csv(): In [21]: data = "col_1\n1\n2\n'A'\n4.22" In [22]: df = pd.read_csv(StringIO(data), converters={"col_1": str}) In [23]: df Out[23]: col_1 0 1 1 2 2 'A' 3 4.22 In [24]: df["col_1"].apply(type).value_counts() Out[24]: <class 'str'> 4 Name: col_1, dtype: int64 Or you can use the to_numeric() function to coerce the dtypes after reading in the data, In [25]: df2 = pd.read_csv(StringIO(data)) In [26]: df2["col_1"] = pd.to_numeric(df2["col_1"], errors="coerce") In [27]: df2 Out[27]: col_1 0 1.00 1 2.00 2 NaN 3 4.22 In [28]: df2["col_1"].apply(type).value_counts() Out[28]: <class 'float'> 4 Name: col_1, dtype: int64 which will convert all valid parsing to floats, leaving the invalid parsing as NaN. Ultimately, how you deal with reading in columns containing mixed dtypes depends on your specific needs. In the case above, if you wanted to NaN out the data anomalies, then to_numeric() is probably your best option. However, if you wanted for all the data to be coerced, no matter the type, then using the converters argument of read_csv() would certainly be worth trying. Note In some cases, reading in abnormal data with columns containing mixed dtypes will result in an inconsistent dataset. If you rely on pandas to infer the dtypes of your columns, the parsing engine will go and infer the dtypes for different chunks of the data, rather than the whole dataset at once. Consequently, you can end up with column(s) with mixed dtypes. For example, In [29]: col_1 = list(range(500000)) + ["a", "b"] + list(range(500000)) In [30]: df = pd.DataFrame({"col_1": col_1}) In [31]: df.to_csv("foo.csv") In [32]: mixed_df = pd.read_csv("foo.csv") In [33]: mixed_df["col_1"].apply(type).value_counts() Out[33]: <class 'int'> 737858 <class 'str'> 262144 Name: col_1, dtype: int64 In [34]: mixed_df["col_1"].dtype Out[34]: dtype('O') will result with mixed_df containing an int dtype for certain chunks of the column, and str for others due to the mixed dtypes from the data that was read in. It is important to note that the overall column will be marked with a dtype of object, which is used for columns with mixed dtypes. Specifying categorical dtype# Categorical columns can be parsed directly by specifying dtype='category' or dtype=CategoricalDtype(categories, ordered). In [35]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [36]: pd.read_csv(StringIO(data)) Out[36]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [37]: pd.read_csv(StringIO(data)).dtypes Out[37]: col1 object col2 object col3 int64 dtype: object In [38]: pd.read_csv(StringIO(data), dtype="category").dtypes Out[38]: col1 category col2 category col3 category dtype: object Individual columns can be parsed as a Categorical using a dict specification: In [39]: pd.read_csv(StringIO(data), dtype={"col1": "category"}).dtypes Out[39]: col1 category col2 object col3 int64 dtype: object Specifying dtype='category' will result in an unordered Categorical whose categories are the unique values observed in the data. For more control on the categories and order, create a CategoricalDtype ahead of time, and pass that for that column’s dtype. In [40]: from pandas.api.types import CategoricalDtype In [41]: dtype = CategoricalDtype(["d", "c", "b", "a"], ordered=True) In [42]: pd.read_csv(StringIO(data), dtype={"col1": dtype}).dtypes Out[42]: col1 category col2 object col3 int64 dtype: object When using dtype=CategoricalDtype, “unexpected” values outside of dtype.categories are treated as missing values. In [43]: dtype = CategoricalDtype(["a", "b", "d"]) # No 'c' In [44]: pd.read_csv(StringIO(data), dtype={"col1": dtype}).col1 Out[44]: 0 a 1 a 2 NaN Name: col1, dtype: category Categories (3, object): ['a', 'b', 'd'] This matches the behavior of Categorical.set_categories(). Note With dtype='category', the resulting categories will always be parsed as strings (object dtype). If the categories are numeric they can be converted using the to_numeric() function, or as appropriate, another converter such as to_datetime(). When dtype is a CategoricalDtype with homogeneous categories ( all numeric, all datetimes, etc.), the conversion is done automatically. In [45]: df = pd.read_csv(StringIO(data), dtype="category") In [46]: df.dtypes Out[46]: col1 category col2 category col3 category dtype: object In [47]: df["col3"] Out[47]: 0 1 1 2 2 3 Name: col3, dtype: category Categories (3, object): ['1', '2', '3'] In [48]: new_categories = pd.to_numeric(df["col3"].cat.categories) In [49]: df["col3"] = df["col3"].cat.rename_categories(new_categories) In [50]: df["col3"] Out[50]: 0 1 1 2 2 3 Name: col3, dtype: category Categories (3, int64): [1, 2, 3] Naming and using columns# Handling column names# A file may or may not have a header row. pandas assumes the first row should be used as the column names: In [51]: data = "a,b,c\n1,2,3\n4,5,6\n7,8,9" In [52]: print(data) a,b,c 1,2,3 4,5,6 7,8,9 In [53]: pd.read_csv(StringIO(data)) Out[53]: a b c 0 1 2 3 1 4 5 6 2 7 8 9 By specifying the names argument in conjunction with header you can indicate other names to use and whether or not to throw away the header row (if any): In [54]: print(data) a,b,c 1,2,3 4,5,6 7,8,9 In [55]: pd.read_csv(StringIO(data), names=["foo", "bar", "baz"], header=0) Out[55]: foo bar baz 0 1 2 3 1 4 5 6 2 7 8 9 In [56]: pd.read_csv(StringIO(data), names=["foo", "bar", "baz"], header=None) Out[56]: foo bar baz 0 a b c 1 1 2 3 2 4 5 6 3 7 8 9 If the header is in a row other than the first, pass the row number to header. This will skip the preceding rows: In [57]: data = "skip this skip it\na,b,c\n1,2,3\n4,5,6\n7,8,9" In [58]: pd.read_csv(StringIO(data), header=1) Out[58]: a b c 0 1 2 3 1 4 5 6 2 7 8 9 Note Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first non-blank line of the file, if column names are passed explicitly then the behavior is identical to header=None. Duplicate names parsing# Deprecated since version 1.5.0: mangle_dupe_cols was never implemented, and a new argument where the renaming pattern can be specified will be added instead. If the file or header contains duplicate names, pandas will by default distinguish between them so as to prevent overwriting data: In [59]: data = "a,b,a\n0,1,2\n3,4,5" In [60]: pd.read_csv(StringIO(data)) Out[60]: a b a.1 0 0 1 2 1 3 4 5 There is no more duplicate data because mangle_dupe_cols=True by default, which modifies a series of duplicate columns ‘X’, …, ‘X’ to become ‘X’, ‘X.1’, …, ‘X.N’. Filtering columns (usecols)# The usecols argument allows you to select any subset of the columns in a file, either using the column names, position numbers or a callable: In [61]: data = "a,b,c,d\n1,2,3,foo\n4,5,6,bar\n7,8,9,baz" In [62]: pd.read_csv(StringIO(data)) Out[62]: a b c d 0 1 2 3 foo 1 4 5 6 bar 2 7 8 9 baz In [63]: pd.read_csv(StringIO(data), usecols=["b", "d"]) Out[63]: b d 0 2 foo 1 5 bar 2 8 baz In [64]: pd.read_csv(StringIO(data), usecols=[0, 2, 3]) Out[64]: a c d 0 1 3 foo 1 4 6 bar 2 7 9 baz In [65]: pd.read_csv(StringIO(data), usecols=lambda x: x.upper() in ["A", "C"]) Out[65]: a c 0 1 3 1 4 6 2 7 9 The usecols argument can also be used to specify which columns not to use in the final result: In [66]: pd.read_csv(StringIO(data), usecols=lambda x: x not in ["a", "c"]) Out[66]: b d 0 2 foo 1 5 bar 2 8 baz In this case, the callable is specifying that we exclude the “a” and “c” columns from the output. Comments and empty lines# Ignoring line comments and empty lines# If the comment parameter is specified, then completely commented lines will be ignored. By default, completely blank lines will be ignored as well. In [67]: data = "\na,b,c\n \n# commented line\n1,2,3\n\n4,5,6" In [68]: print(data) a,b,c # commented line 1,2,3 4,5,6 In [69]: pd.read_csv(StringIO(data), comment="#") Out[69]: a b c 0 1 2 3 1 4 5 6 If skip_blank_lines=False, then read_csv will not ignore blank lines: In [70]: data = "a,b,c\n\n1,2,3\n\n\n4,5,6" In [71]: pd.read_csv(StringIO(data), skip_blank_lines=False) Out[71]: a b c 0 NaN NaN NaN 1 1.0 2.0 3.0 2 NaN NaN NaN 3 NaN NaN NaN 4 4.0 5.0 6.0 Warning The presence of ignored lines might create ambiguities involving line numbers; the parameter header uses row numbers (ignoring commented/empty lines), while skiprows uses line numbers (including commented/empty lines): In [72]: data = "#comment\na,b,c\nA,B,C\n1,2,3" In [73]: pd.read_csv(StringIO(data), comment="#", header=1) Out[73]: A B C 0 1 2 3 In [74]: data = "A,B,C\n#comment\na,b,c\n1,2,3" In [75]: pd.read_csv(StringIO(data), comment="#", skiprows=2) Out[75]: a b c 0 1 2 3 If both header and skiprows are specified, header will be relative to the end of skiprows. For example: In [76]: data = ( ....: "# empty\n" ....: "# second empty line\n" ....: "# third emptyline\n" ....: "X,Y,Z\n" ....: "1,2,3\n" ....: "A,B,C\n" ....: "1,2.,4.\n" ....: "5.,NaN,10.0\n" ....: ) ....: In [77]: print(data) # empty # second empty line # third emptyline X,Y,Z 1,2,3 A,B,C 1,2.,4. 5.,NaN,10.0 In [78]: pd.read_csv(StringIO(data), comment="#", skiprows=4, header=1) Out[78]: A B C 0 1.0 2.0 4.0 1 5.0 NaN 10.0 Comments# Sometimes comments or meta data may be included in a file: In [79]: print(open("tmp.csv").read()) ID,level,category Patient1,123000,x # really unpleasant Patient2,23000,y # wouldn't take his medicine Patient3,1234018,z # awesome By default, the parser includes the comments in the output: In [80]: df = pd.read_csv("tmp.csv") In [81]: df Out[81]: ID level category 0 Patient1 123000 x # really unpleasant 1 Patient2 23000 y # wouldn't take his medicine 2 Patient3 1234018 z # awesome We can suppress the comments using the comment keyword: In [82]: df = pd.read_csv("tmp.csv", comment="#") In [83]: df Out[83]: ID level category 0 Patient1 123000 x 1 Patient2 23000 y 2 Patient3 1234018 z Dealing with Unicode data# The encoding argument should be used for encoded unicode data, which will result in byte strings being decoded to unicode in the result: In [84]: from io import BytesIO In [85]: data = b"word,length\n" b"Tr\xc3\xa4umen,7\n" b"Gr\xc3\xbc\xc3\x9fe,5" In [86]: data = data.decode("utf8").encode("latin-1") In [87]: df = pd.read_csv(BytesIO(data), encoding="latin-1") In [88]: df Out[88]: word length 0 Träumen 7 1 Grüße 5 In [89]: df["word"][1] Out[89]: 'Grüße' Some formats which encode all characters as multiple bytes, like UTF-16, won’t parse correctly at all without specifying the encoding. Full list of Python standard encodings. Index columns and trailing delimiters# If a file has one more column of data than the number of column names, the first column will be used as the DataFrame’s row names: In [90]: data = "a,b,c\n4,apple,bat,5.7\n8,orange,cow,10" In [91]: pd.read_csv(StringIO(data)) Out[91]: a b c 4 apple bat 5.7 8 orange cow 10.0 In [92]: data = "index,a,b,c\n4,apple,bat,5.7\n8,orange,cow,10" In [93]: pd.read_csv(StringIO(data), index_col=0) Out[93]: a b c index 4 apple bat 5.7 8 orange cow 10.0 Ordinarily, you can achieve this behavior using the index_col option. There are some exception cases when a file has been prepared with delimiters at the end of each data line, confusing the parser. To explicitly disable the index column inference and discard the last column, pass index_col=False: In [94]: data = "a,b,c\n4,apple,bat,\n8,orange,cow," In [95]: print(data) a,b,c 4,apple,bat, 8,orange,cow, In [96]: pd.read_csv(StringIO(data)) Out[96]: a b c 4 apple bat NaN 8 orange cow NaN In [97]: pd.read_csv(StringIO(data), index_col=False) Out[97]: a b c 0 4 apple bat 1 8 orange cow If a subset of data is being parsed using the usecols option, the index_col specification is based on that subset, not the original data. In [98]: data = "a,b,c\n4,apple,bat,\n8,orange,cow," In [99]: print(data) a,b,c 4,apple,bat, 8,orange,cow, In [100]: pd.read_csv(StringIO(data), usecols=["b", "c"]) Out[100]: b c 4 bat NaN 8 cow NaN In [101]: pd.read_csv(StringIO(data), usecols=["b", "c"], index_col=0) Out[101]: b c 4 bat NaN 8 cow NaN Date Handling# Specifying date columns# To better facilitate working with datetime data, read_csv() uses the keyword arguments parse_dates and date_parser to allow users to specify a variety of columns and date/time formats to turn the input text data into datetime objects. The simplest case is to just pass in parse_dates=True: In [102]: with open("foo.csv", mode="w") as f: .....: f.write("date,A,B,C\n20090101,a,1,2\n20090102,b,3,4\n20090103,c,4,5") .....: # Use a column as an index, and parse it as dates. In [103]: df = pd.read_csv("foo.csv", index_col=0, parse_dates=True) In [104]: df Out[104]: A B C date 2009-01-01 a 1 2 2009-01-02 b 3 4 2009-01-03 c 4 5 # These are Python datetime objects In [105]: df.index Out[105]: DatetimeIndex(['2009-01-01', '2009-01-02', '2009-01-03'], dtype='datetime64[ns]', name='date', freq=None) It is often the case that we may want to store date and time data separately, or store various date fields separately. the parse_dates keyword can be used to specify a combination of columns to parse the dates and/or times from. You can specify a list of column lists to parse_dates, the resulting date columns will be prepended to the output (so as to not affect the existing column order) and the new column names will be the concatenation of the component column names: In [106]: data = ( .....: "KORD,19990127, 19:00:00, 18:56:00, 0.8100\n" .....: "KORD,19990127, 20:00:00, 19:56:00, 0.0100\n" .....: "KORD,19990127, 21:00:00, 20:56:00, -0.5900\n" .....: "KORD,19990127, 21:00:00, 21:18:00, -0.9900\n" .....: "KORD,19990127, 22:00:00, 21:56:00, -0.5900\n" .....: "KORD,19990127, 23:00:00, 22:56:00, -0.5900" .....: ) .....: In [107]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [108]: df = pd.read_csv("tmp.csv", header=None, parse_dates=[[1, 2], [1, 3]]) In [109]: df Out[109]: 1_2 1_3 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 By default the parser removes the component date columns, but you can choose to retain them via the keep_date_col keyword: In [110]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=[[1, 2], [1, 3]], keep_date_col=True .....: ) .....: In [111]: df Out[111]: 1_2 1_3 0 ... 2 3 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD ... 19:00:00 18:56:00 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD ... 20:00:00 19:56:00 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD ... 21:00:00 20:56:00 -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD ... 21:00:00 21:18:00 -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD ... 22:00:00 21:56:00 -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD ... 23:00:00 22:56:00 -0.59 [6 rows x 7 columns] Note that if you wish to combine multiple columns into a single date column, a nested list must be used. In other words, parse_dates=[1, 2] indicates that the second and third columns should each be parsed as separate date columns while parse_dates=[[1, 2]] means the two columns should be parsed into a single column. You can also use a dict to specify custom name columns: In [112]: date_spec = {"nominal": [1, 2], "actual": [1, 3]} In [113]: df = pd.read_csv("tmp.csv", header=None, parse_dates=date_spec) In [114]: df Out[114]: nominal actual 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 It is important to remember that if multiple text columns are to be parsed into a single date column, then a new column is prepended to the data. The index_col specification is based off of this new set of columns rather than the original data columns: In [115]: date_spec = {"nominal": [1, 2], "actual": [1, 3]} In [116]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=date_spec, index_col=0 .....: ) # index is the nominal column .....: In [117]: df Out[117]: actual 0 4 nominal 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 Note If a column or index contains an unparsable date, the entire column or index will be returned unaltered as an object data type. For non-standard datetime parsing, use to_datetime() after pd.read_csv. Note read_csv has a fast_path for parsing datetime strings in iso8601 format, e.g “2000-01-01T00:01:02+00:00” and similar variations. If you can arrange for your data to store datetimes in this format, load times will be significantly faster, ~20x has been observed. Date parsing functions# Finally, the parser allows you to specify a custom date_parser function to take full advantage of the flexibility of the date parsing API: In [118]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=date_spec, date_parser=pd.to_datetime .....: ) .....: In [119]: df Out[119]: nominal actual 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 pandas will try to call the date_parser function in three different ways. If an exception is raised, the next one is tried: date_parser is first called with one or more arrays as arguments, as defined using parse_dates (e.g., date_parser(['2013', '2013'], ['1', '2'])). If #1 fails, date_parser is called with all the columns concatenated row-wise into a single array (e.g., date_parser(['2013 1', '2013 2'])). Note that performance-wise, you should try these methods of parsing dates in order: Try to infer the format using infer_datetime_format=True (see section below). If you know the format, use pd.to_datetime(): date_parser=lambda x: pd.to_datetime(x, format=...). If you have a really non-standard format, use a custom date_parser function. For optimal performance, this should be vectorized, i.e., it should accept arrays as arguments. Parsing a CSV with mixed timezones# pandas cannot natively represent a column or index with mixed timezones. If your CSV file contains columns with a mixture of timezones, the default result will be an object-dtype column with strings, even with parse_dates. In [120]: content = """\ .....: a .....: 2000-01-01T00:00:00+05:00 .....: 2000-01-01T00:00:00+06:00""" .....: In [121]: df = pd.read_csv(StringIO(content), parse_dates=["a"]) In [122]: df["a"] Out[122]: 0 2000-01-01 00:00:00+05:00 1 2000-01-01 00:00:00+06:00 Name: a, dtype: object To parse the mixed-timezone values as a datetime column, pass a partially-applied to_datetime() with utc=True as the date_parser. In [123]: df = pd.read_csv( .....: StringIO(content), .....: parse_dates=["a"], .....: date_parser=lambda col: pd.to_datetime(col, utc=True), .....: ) .....: In [124]: df["a"] Out[124]: 0 1999-12-31 19:00:00+00:00 1 1999-12-31 18:00:00+00:00 Name: a, dtype: datetime64[ns, UTC] Inferring datetime format# If you have parse_dates enabled for some or all of your columns, and your datetime strings are all formatted the same way, you may get a large speed up by setting infer_datetime_format=True. If set, pandas will attempt to guess the format of your datetime strings, and then use a faster means of parsing the strings. 5-10x parsing speeds have been observed. pandas will fallback to the usual parsing if either the format cannot be guessed or the format that was guessed cannot properly parse the entire column of strings. So in general, infer_datetime_format should not have any negative consequences if enabled. Here are some examples of datetime strings that can be guessed (All representing December 30th, 2011 at 00:00:00): “20111230” “2011/12/30” “20111230 00:00:00” “12/30/2011 00:00:00” “30/Dec/2011 00:00:00” “30/December/2011 00:00:00” Note that infer_datetime_format is sensitive to dayfirst. With dayfirst=True, it will guess “01/12/2011” to be December 1st. With dayfirst=False (default) it will guess “01/12/2011” to be January 12th. # Try to infer the format for the index column In [125]: df = pd.read_csv( .....: "foo.csv", .....: index_col=0, .....: parse_dates=True, .....: infer_datetime_format=True, .....: ) .....: In [126]: df Out[126]: A B C date 2009-01-01 a 1 2 2009-01-02 b 3 4 2009-01-03 c 4 5 International date formats# While US date formats tend to be MM/DD/YYYY, many international formats use DD/MM/YYYY instead. For convenience, a dayfirst keyword is provided: In [127]: data = "date,value,cat\n1/6/2000,5,a\n2/6/2000,10,b\n3/6/2000,15,c" In [128]: print(data) date,value,cat 1/6/2000,5,a 2/6/2000,10,b 3/6/2000,15,c In [129]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [130]: pd.read_csv("tmp.csv", parse_dates=[0]) Out[130]: date value cat 0 2000-01-06 5 a 1 2000-02-06 10 b 2 2000-03-06 15 c In [131]: pd.read_csv("tmp.csv", dayfirst=True, parse_dates=[0]) Out[131]: date value cat 0 2000-06-01 5 a 1 2000-06-02 10 b 2 2000-06-03 15 c Writing CSVs to binary file objects# New in version 1.2.0. df.to_csv(..., mode="wb") allows writing a CSV to a file object opened binary mode. In most cases, it is not necessary to specify mode as Pandas will auto-detect whether the file object is opened in text or binary mode. In [132]: import io In [133]: data = pd.DataFrame([0, 1, 2]) In [134]: buffer = io.BytesIO() In [135]: data.to_csv(buffer, encoding="utf-8", compression="gzip") Specifying method for floating-point conversion# The parameter float_precision can be specified in order to use a specific floating-point converter during parsing with the C engine. The options are the ordinary converter, the high-precision converter, and the round-trip converter (which is guaranteed to round-trip values after writing to a file). For example: In [136]: val = "0.3066101993807095471566981359501369297504425048828125" In [137]: data = "a,b,c\n1,2,{0}".format(val) In [138]: abs( .....: pd.read_csv( .....: StringIO(data), .....: engine="c", .....: float_precision=None, .....: )["c"][0] - float(val) .....: ) .....: Out[138]: 5.551115123125783e-17 In [139]: abs( .....: pd.read_csv( .....: StringIO(data), .....: engine="c", .....: float_precision="high", .....: )["c"][0] - float(val) .....: ) .....: Out[139]: 5.551115123125783e-17 In [140]: abs( .....: pd.read_csv(StringIO(data), engine="c", float_precision="round_trip")["c"][0] .....: - float(val) .....: ) .....: Out[140]: 0.0 Thousand separators# For large numbers that have been written with a thousands separator, you can set the thousands keyword to a string of length 1 so that integers will be parsed correctly: By default, numbers with a thousands separator will be parsed as strings: In [141]: data = ( .....: "ID|level|category\n" .....: "Patient1|123,000|x\n" .....: "Patient2|23,000|y\n" .....: "Patient3|1,234,018|z" .....: ) .....: In [142]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [143]: df = pd.read_csv("tmp.csv", sep="|") In [144]: df Out[144]: ID level category 0 Patient1 123,000 x 1 Patient2 23,000 y 2 Patient3 1,234,018 z In [145]: df.level.dtype Out[145]: dtype('O') The thousands keyword allows integers to be parsed correctly: In [146]: df = pd.read_csv("tmp.csv", sep="|", thousands=",") In [147]: df Out[147]: ID level category 0 Patient1 123000 x 1 Patient2 23000 y 2 Patient3 1234018 z In [148]: df.level.dtype Out[148]: dtype('int64') NA values# To control which values are parsed as missing values (which are signified by NaN), specify a string in na_values. If you specify a list of strings, then all values in it are considered to be missing values. If you specify a number (a float, like 5.0 or an integer like 5), the corresponding equivalent values will also imply a missing value (in this case effectively [5.0, 5] are recognized as NaN). To completely override the default values that are recognized as missing, specify keep_default_na=False. The default NaN recognized values are ['-1.#IND', '1.#QNAN', '1.#IND', '-1.#QNAN', '#N/A N/A', '#N/A', 'N/A', 'n/a', 'NA', '<NA>', '#NA', 'NULL', 'null', 'NaN', '-NaN', 'nan', '-nan', '']. Let us consider some examples: pd.read_csv("path_to_file.csv", na_values=[5]) In the example above 5 and 5.0 will be recognized as NaN, in addition to the defaults. A string will first be interpreted as a numerical 5, then as a NaN. pd.read_csv("path_to_file.csv", keep_default_na=False, na_values=[""]) Above, only an empty field will be recognized as NaN. pd.read_csv("path_to_file.csv", keep_default_na=False, na_values=["NA", "0"]) Above, both NA and 0 as strings are NaN. pd.read_csv("path_to_file.csv", na_values=["Nope"]) The default values, in addition to the string "Nope" are recognized as NaN. Infinity# inf like values will be parsed as np.inf (positive infinity), and -inf as -np.inf (negative infinity). These will ignore the case of the value, meaning Inf, will also be parsed as np.inf. Returning Series# Using the squeeze keyword, the parser will return output with a single column as a Series: Deprecated since version 1.4.0: Users should append .squeeze("columns") to the DataFrame returned by read_csv instead. In [149]: data = "level\nPatient1,123000\nPatient2,23000\nPatient3,1234018" In [150]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [151]: print(open("tmp.csv").read()) level Patient1,123000 Patient2,23000 Patient3,1234018 In [152]: output = pd.read_csv("tmp.csv", squeeze=True) In [153]: output Out[153]: Patient1 123000 Patient2 23000 Patient3 1234018 Name: level, dtype: int64 In [154]: type(output) Out[154]: pandas.core.series.Series Boolean values# The common values True, False, TRUE, and FALSE are all recognized as boolean. Occasionally you might want to recognize other values as being boolean. To do this, use the true_values and false_values options as follows: In [155]: data = "a,b,c\n1,Yes,2\n3,No,4" In [156]: print(data) a,b,c 1,Yes,2 3,No,4 In [157]: pd.read_csv(StringIO(data)) Out[157]: a b c 0 1 Yes 2 1 3 No 4 In [158]: pd.read_csv(StringIO(data), true_values=["Yes"], false_values=["No"]) Out[158]: a b c 0 1 True 2 1 3 False 4 Handling “bad” lines# Some files may have malformed lines with too few fields or too many. Lines with too few fields will have NA values filled in the trailing fields. Lines with too many fields will raise an error by default: In [159]: data = "a,b,c\n1,2,3\n4,5,6,7\n8,9,10" In [160]: pd.read_csv(StringIO(data)) --------------------------------------------------------------------------- ParserError Traceback (most recent call last) Cell In[160], line 1 ----> 1 pd.read_csv(StringIO(data)) File ~/work/pandas/pandas/pandas/util/_decorators.py:211, in deprecate_kwarg.<locals>._deprecate_kwarg.<locals>.wrapper(*args, **kwargs) 209 else: 210 kwargs[new_arg_name] = new_arg_value --> 211 return func(*args, **kwargs) File ~/work/pandas/pandas/pandas/util/_decorators.py:331, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs) 325 if len(args) > num_allow_args: 326 warnings.warn( 327 msg.format(arguments=_format_argument_list(allow_args)), 328 FutureWarning, 329 stacklevel=find_stack_level(), 330 ) --> 331 return func(*args, **kwargs) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:950, in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, error_bad_lines, warn_bad_lines, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options) 935 kwds_defaults = _refine_defaults_read( 936 dialect, 937 delimiter, (...) 946 defaults={"delimiter": ","}, 947 ) 948 kwds.update(kwds_defaults) --> 950 return _read(filepath_or_buffer, kwds) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:611, in _read(filepath_or_buffer, kwds) 608 return parser 610 with parser: --> 611 return parser.read(nrows) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:1778, in TextFileReader.read(self, nrows) 1771 nrows = validate_integer("nrows", nrows) 1772 try: 1773 # error: "ParserBase" has no attribute "read" 1774 ( 1775 index, 1776 columns, 1777 col_dict, -> 1778 ) = self._engine.read( # type: ignore[attr-defined] 1779 nrows 1780 ) 1781 except Exception: 1782 self.close() File ~/work/pandas/pandas/pandas/io/parsers/c_parser_wrapper.py:230, in CParserWrapper.read(self, nrows) 228 try: 229 if self.low_memory: --> 230 chunks = self._reader.read_low_memory(nrows) 231 # destructive to chunks 232 data = _concatenate_chunks(chunks) File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:808, in pandas._libs.parsers.TextReader.read_low_memory() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:866, in pandas._libs.parsers.TextReader._read_rows() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:852, in pandas._libs.parsers.TextReader._tokenize_rows() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:1973, in pandas._libs.parsers.raise_parser_error() ParserError: Error tokenizing data. C error: Expected 3 fields in line 3, saw 4 You can elect to skip bad lines: In [29]: pd.read_csv(StringIO(data), on_bad_lines="warn") Skipping line 3: expected 3 fields, saw 4 Out[29]: a b c 0 1 2 3 1 8 9 10 Or pass a callable function to handle the bad line if engine="python". The bad line will be a list of strings that was split by the sep: In [29]: external_list = [] In [30]: def bad_lines_func(line): ...: external_list.append(line) ...: return line[-3:] In [31]: pd.read_csv(StringIO(data), on_bad_lines=bad_lines_func, engine="python") Out[31]: a b c 0 1 2 3 1 5 6 7 2 8 9 10 In [32]: external_list Out[32]: [4, 5, 6, 7] .. versionadded:: 1.4.0 You can also use the usecols parameter to eliminate extraneous column data that appear in some lines but not others: In [33]: pd.read_csv(StringIO(data), usecols=[0, 1, 2]) Out[33]: a b c 0 1 2 3 1 4 5 6 2 8 9 10 In case you want to keep all data including the lines with too many fields, you can specify a sufficient number of names. This ensures that lines with not enough fields are filled with NaN. In [34]: pd.read_csv(StringIO(data), names=['a', 'b', 'c', 'd']) Out[34]: a b c d 0 1 2 3 NaN 1 4 5 6 7 2 8 9 10 NaN Dialect# The dialect keyword gives greater flexibility in specifying the file format. By default it uses the Excel dialect but you can specify either the dialect name or a csv.Dialect instance. Suppose you had data with unenclosed quotes: In [161]: data = "label1,label2,label3\n" 'index1,"a,c,e\n' "index2,b,d,f" In [162]: print(data) label1,label2,label3 index1,"a,c,e index2,b,d,f By default, read_csv uses the Excel dialect and treats the double quote as the quote character, which causes it to fail when it finds a newline before it finds the closing double quote. We can get around this using dialect: In [163]: import csv In [164]: dia = csv.excel() In [165]: dia.quoting = csv.QUOTE_NONE In [166]: pd.read_csv(StringIO(data), dialect=dia) Out[166]: label1 label2 label3 index1 "a c e index2 b d f All of the dialect options can be specified separately by keyword arguments: In [167]: data = "a,b,c~1,2,3~4,5,6" In [168]: pd.read_csv(StringIO(data), lineterminator="~") Out[168]: a b c 0 1 2 3 1 4 5 6 Another common dialect option is skipinitialspace, to skip any whitespace after a delimiter: In [169]: data = "a, b, c\n1, 2, 3\n4, 5, 6" In [170]: print(data) a, b, c 1, 2, 3 4, 5, 6 In [171]: pd.read_csv(StringIO(data), skipinitialspace=True) Out[171]: a b c 0 1 2 3 1 4 5 6 The parsers make every attempt to “do the right thing” and not be fragile. Type inference is a pretty big deal. If a column can be coerced to integer dtype without altering the contents, the parser will do so. Any non-numeric columns will come through as object dtype as with the rest of pandas objects. Quoting and Escape Characters# Quotes (and other escape characters) in embedded fields can be handled in any number of ways. One way is to use backslashes; to properly parse this data, you should pass the escapechar option: In [172]: data = 'a,b\n"hello, \\"Bob\\", nice to see you",5' In [173]: print(data) a,b "hello, \"Bob\", nice to see you",5 In [174]: pd.read_csv(StringIO(data), escapechar="\\") Out[174]: a b 0 hello, "Bob", nice to see you 5 Files with fixed width columns# While read_csv() reads delimited data, the read_fwf() function works with data files that have known and fixed column widths. The function parameters to read_fwf are largely the same as read_csv with two extra parameters, and a different usage of the delimiter parameter: colspecs: A list of pairs (tuples) giving the extents of the fixed-width fields of each line as half-open intervals (i.e., [from, to[ ). String value ‘infer’ can be used to instruct the parser to try detecting the column specifications from the first 100 rows of the data. Default behavior, if not specified, is to infer. widths: A list of field widths which can be used instead of ‘colspecs’ if the intervals are contiguous. delimiter: Characters to consider as filler characters in the fixed-width file. Can be used to specify the filler character of the fields if it is not spaces (e.g., ‘~’). Consider a typical fixed-width data file: In [175]: data1 = ( .....: "id8141 360.242940 149.910199 11950.7\n" .....: "id1594 444.953632 166.985655 11788.4\n" .....: "id1849 364.136849 183.628767 11806.2\n" .....: "id1230 413.836124 184.375703 11916.8\n" .....: "id1948 502.953953 173.237159 12468.3" .....: ) .....: In [176]: with open("bar.csv", "w") as f: .....: f.write(data1) .....: In order to parse this file into a DataFrame, we simply need to supply the column specifications to the read_fwf function along with the file name: # Column specifications are a list of half-intervals In [177]: colspecs = [(0, 6), (8, 20), (21, 33), (34, 43)] In [178]: df = pd.read_fwf("bar.csv", colspecs=colspecs, header=None, index_col=0) In [179]: df Out[179]: 1 2 3 0 id8141 360.242940 149.910199 11950.7 id1594 444.953632 166.985655 11788.4 id1849 364.136849 183.628767 11806.2 id1230 413.836124 184.375703 11916.8 id1948 502.953953 173.237159 12468.3 Note how the parser automatically picks column names X.<column number> when header=None argument is specified. Alternatively, you can supply just the column widths for contiguous columns: # Widths are a list of integers In [180]: widths = [6, 14, 13, 10] In [181]: df = pd.read_fwf("bar.csv", widths=widths, header=None) In [182]: df Out[182]: 0 1 2 3 0 id8141 360.242940 149.910199 11950.7 1 id1594 444.953632 166.985655 11788.4 2 id1849 364.136849 183.628767 11806.2 3 id1230 413.836124 184.375703 11916.8 4 id1948 502.953953 173.237159 12468.3 The parser will take care of extra white spaces around the columns so it’s ok to have extra separation between the columns in the file. By default, read_fwf will try to infer the file’s colspecs by using the first 100 rows of the file. It can do it only in cases when the columns are aligned and correctly separated by the provided delimiter (default delimiter is whitespace). In [183]: df = pd.read_fwf("bar.csv", header=None, index_col=0) In [184]: df Out[184]: 1 2 3 0 id8141 360.242940 149.910199 11950.7 id1594 444.953632 166.985655 11788.4 id1849 364.136849 183.628767 11806.2 id1230 413.836124 184.375703 11916.8 id1948 502.953953 173.237159 12468.3 read_fwf supports the dtype parameter for specifying the types of parsed columns to be different from the inferred type. In [185]: pd.read_fwf("bar.csv", header=None, index_col=0).dtypes Out[185]: 1 float64 2 float64 3 float64 dtype: object In [186]: pd.read_fwf("bar.csv", header=None, dtype={2: "object"}).dtypes Out[186]: 0 object 1 float64 2 object 3 float64 dtype: object Indexes# Files with an “implicit” index column# Consider a file with one less entry in the header than the number of data column: In [187]: data = "A,B,C\n20090101,a,1,2\n20090102,b,3,4\n20090103,c,4,5" In [188]: print(data) A,B,C 20090101,a,1,2 20090102,b,3,4 20090103,c,4,5 In [189]: with open("foo.csv", "w") as f: .....: f.write(data) .....: In this special case, read_csv assumes that the first column is to be used as the index of the DataFrame: In [190]: pd.read_csv("foo.csv") Out[190]: A B C 20090101 a 1 2 20090102 b 3 4 20090103 c 4 5 Note that the dates weren’t automatically parsed. In that case you would need to do as before: In [191]: df = pd.read_csv("foo.csv", parse_dates=True) In [192]: df.index Out[192]: DatetimeIndex(['2009-01-01', '2009-01-02', '2009-01-03'], dtype='datetime64[ns]', freq=None) Reading an index with a MultiIndex# Suppose you have data indexed by two columns: In [193]: data = 'year,indiv,zit,xit\n1977,"A",1.2,.6\n1977,"B",1.5,.5' In [194]: print(data) year,indiv,zit,xit 1977,"A",1.2,.6 1977,"B",1.5,.5 In [195]: with open("mindex_ex.csv", mode="w") as f: .....: f.write(data) .....: The index_col argument to read_csv can take a list of column numbers to turn multiple columns into a MultiIndex for the index of the returned object: In [196]: df = pd.read_csv("mindex_ex.csv", index_col=[0, 1]) In [197]: df Out[197]: zit xit year indiv 1977 A 1.2 0.6 B 1.5 0.5 In [198]: df.loc[1977] Out[198]: zit xit indiv A 1.2 0.6 B 1.5 0.5 Reading columns with a MultiIndex# By specifying list of row locations for the header argument, you can read in a MultiIndex for the columns. Specifying non-consecutive rows will skip the intervening rows. In [199]: from pandas._testing import makeCustomDataframe as mkdf In [200]: df = mkdf(5, 3, r_idx_nlevels=2, c_idx_nlevels=4) In [201]: df.to_csv("mi.csv") In [202]: print(open("mi.csv").read()) C0,,C_l0_g0,C_l0_g1,C_l0_g2 C1,,C_l1_g0,C_l1_g1,C_l1_g2 C2,,C_l2_g0,C_l2_g1,C_l2_g2 C3,,C_l3_g0,C_l3_g1,C_l3_g2 R0,R1,,, R_l0_g0,R_l1_g0,R0C0,R0C1,R0C2 R_l0_g1,R_l1_g1,R1C0,R1C1,R1C2 R_l0_g2,R_l1_g2,R2C0,R2C1,R2C2 R_l0_g3,R_l1_g3,R3C0,R3C1,R3C2 R_l0_g4,R_l1_g4,R4C0,R4C1,R4C2 In [203]: pd.read_csv("mi.csv", header=[0, 1, 2, 3], index_col=[0, 1]) Out[203]: C0 C_l0_g0 C_l0_g1 C_l0_g2 C1 C_l1_g0 C_l1_g1 C_l1_g2 C2 C_l2_g0 C_l2_g1 C_l2_g2 C3 C_l3_g0 C_l3_g1 C_l3_g2 R0 R1 R_l0_g0 R_l1_g0 R0C0 R0C1 R0C2 R_l0_g1 R_l1_g1 R1C0 R1C1 R1C2 R_l0_g2 R_l1_g2 R2C0 R2C1 R2C2 R_l0_g3 R_l1_g3 R3C0 R3C1 R3C2 R_l0_g4 R_l1_g4 R4C0 R4C1 R4C2 read_csv is also able to interpret a more common format of multi-columns indices. In [204]: data = ",a,a,a,b,c,c\n,q,r,s,t,u,v\none,1,2,3,4,5,6\ntwo,7,8,9,10,11,12" In [205]: print(data) ,a,a,a,b,c,c ,q,r,s,t,u,v one,1,2,3,4,5,6 two,7,8,9,10,11,12 In [206]: with open("mi2.csv", "w") as fh: .....: fh.write(data) .....: In [207]: pd.read_csv("mi2.csv", header=[0, 1], index_col=0) Out[207]: a b c q r s t u v one 1 2 3 4 5 6 two 7 8 9 10 11 12 Note If an index_col is not specified (e.g. you don’t have an index, or wrote it with df.to_csv(..., index=False), then any names on the columns index will be lost. Automatically “sniffing” the delimiter# read_csv is capable of inferring delimited (not necessarily comma-separated) files, as pandas uses the csv.Sniffer class of the csv module. For this, you have to specify sep=None. In [208]: df = pd.DataFrame(np.random.randn(10, 4)) In [209]: df.to_csv("tmp.csv", sep="|") In [210]: df.to_csv("tmp2.csv", sep=":") In [211]: pd.read_csv("tmp2.csv", sep=None, engine="python") Out[211]: Unnamed: 0 0 1 2 3 0 0 0.469112 -0.282863 -1.509059 -1.135632 1 1 1.212112 -0.173215 0.119209 -1.044236 2 2 -0.861849 -2.104569 -0.494929 1.071804 3 3 0.721555 -0.706771 -1.039575 0.271860 4 4 -0.424972 0.567020 0.276232 -1.087401 5 5 -0.673690 0.113648 -1.478427 0.524988 6 6 0.404705 0.577046 -1.715002 -1.039268 7 7 -0.370647 -1.157892 -1.344312 0.844885 8 8 1.075770 -0.109050 1.643563 -1.469388 9 9 0.357021 -0.674600 -1.776904 -0.968914 Reading multiple files to create a single DataFrame# It’s best to use concat() to combine multiple files. See the cookbook for an example. Iterating through files chunk by chunk# Suppose you wish to iterate through a (potentially very large) file lazily rather than reading the entire file into memory, such as the following: In [212]: df = pd.DataFrame(np.random.randn(10, 4)) In [213]: df.to_csv("tmp.csv", sep="|") In [214]: table = pd.read_csv("tmp.csv", sep="|") In [215]: table Out[215]: Unnamed: 0 0 1 2 3 0 0 -1.294524 0.413738 0.276662 -0.472035 1 1 -0.013960 -0.362543 -0.006154 -0.923061 2 2 0.895717 0.805244 -1.206412 2.565646 3 3 1.431256 1.340309 -1.170299 -0.226169 4 4 0.410835 0.813850 0.132003 -0.827317 5 5 -0.076467 -1.187678 1.130127 -1.436737 6 6 -1.413681 1.607920 1.024180 0.569605 7 7 0.875906 -2.211372 0.974466 -2.006747 8 8 -0.410001 -0.078638 0.545952 -1.219217 9 9 -1.226825 0.769804 -1.281247 -0.727707 By specifying a chunksize to read_csv, the return value will be an iterable object of type TextFileReader: In [216]: with pd.read_csv("tmp.csv", sep="|", chunksize=4) as reader: .....: reader .....: for chunk in reader: .....: print(chunk) .....: Unnamed: 0 0 1 2 3 0 0 -1.294524 0.413738 0.276662 -0.472035 1 1 -0.013960 -0.362543 -0.006154 -0.923061 2 2 0.895717 0.805244 -1.206412 2.565646 3 3 1.431256 1.340309 -1.170299 -0.226169 Unnamed: 0 0 1 2 3 4 4 0.410835 0.813850 0.132003 -0.827317 5 5 -0.076467 -1.187678 1.130127 -1.436737 6 6 -1.413681 1.607920 1.024180 0.569605 7 7 0.875906 -2.211372 0.974466 -2.006747 Unnamed: 0 0 1 2 3 8 8 -0.410001 -0.078638 0.545952 -1.219217 9 9 -1.226825 0.769804 -1.281247 -0.727707 Changed in version 1.2: read_csv/json/sas return a context-manager when iterating through a file. Specifying iterator=True will also return the TextFileReader object: In [217]: with pd.read_csv("tmp.csv", sep="|", iterator=True) as reader: .....: reader.get_chunk(5) .....: Specifying the parser engine# Pandas currently supports three engines, the C engine, the python engine, and an experimental pyarrow engine (requires the pyarrow package). In general, the pyarrow engine is fastest on larger workloads and is equivalent in speed to the C engine on most other workloads. The python engine tends to be slower than the pyarrow and C engines on most workloads. However, the pyarrow engine is much less robust than the C engine, which lacks a few features compared to the Python engine. Where possible, pandas uses the C parser (specified as engine='c'), but it may fall back to Python if C-unsupported options are specified. Currently, options unsupported by the C and pyarrow engines include: sep other than a single character (e.g. regex separators) skipfooter sep=None with delim_whitespace=False Specifying any of the above options will produce a ParserWarning unless the python engine is selected explicitly using engine='python'. Options that are unsupported by the pyarrow engine which are not covered by the list above include: float_precision chunksize comment nrows thousands memory_map dialect warn_bad_lines error_bad_lines on_bad_lines delim_whitespace quoting lineterminator converters decimal iterator dayfirst infer_datetime_format verbose skipinitialspace low_memory Specifying these options with engine='pyarrow' will raise a ValueError. Reading/writing remote files# You can pass in a URL to read or write remote files to many of pandas’ IO functions - the following example shows reading a CSV file: df = pd.read_csv("https://download.bls.gov/pub/time.series/cu/cu.item", sep="\t") New in version 1.3.0. A custom header can be sent alongside HTTP(s) requests by passing a dictionary of header key value mappings to the storage_options keyword argument as shown below: headers = {"User-Agent": "pandas"} df = pd.read_csv( "https://download.bls.gov/pub/time.series/cu/cu.item", sep="\t", storage_options=headers ) All URLs which are not local files or HTTP(s) are handled by fsspec, if installed, and its various filesystem implementations (including Amazon S3, Google Cloud, SSH, FTP, webHDFS…). Some of these implementations will require additional packages to be installed, for example S3 URLs require the s3fs library: df = pd.read_json("s3://pandas-test/adatafile.json") When dealing with remote storage systems, you might need extra configuration with environment variables or config files in special locations. For example, to access data in your S3 bucket, you will need to define credentials in one of the several ways listed in the S3Fs documentation. The same is true for several of the storage backends, and you should follow the links at fsimpl1 for implementations built into fsspec and fsimpl2 for those not included in the main fsspec distribution. You can also pass parameters directly to the backend driver. For example, if you do not have S3 credentials, you can still access public data by specifying an anonymous connection, such as New in version 1.2.0. pd.read_csv( "s3://ncei-wcsd-archive/data/processed/SH1305/18kHz/SaKe2013" "-D20130523-T080854_to_SaKe2013-D20130523-T085643.csv", storage_options={"anon": True}, ) fsspec also allows complex URLs, for accessing data in compressed archives, local caching of files, and more. To locally cache the above example, you would modify the call to pd.read_csv( "simplecache::s3://ncei-wcsd-archive/data/processed/SH1305/18kHz/" "SaKe2013-D20130523-T080854_to_SaKe2013-D20130523-T085643.csv", storage_options={"s3": {"anon": True}}, ) where we specify that the “anon” parameter is meant for the “s3” part of the implementation, not to the caching implementation. Note that this caches to a temporary directory for the duration of the session only, but you can also specify a permanent store. Writing out data# Writing to CSV format# The Series and DataFrame objects have an instance method to_csv which allows storing the contents of the object as a comma-separated-values file. The function takes a number of arguments. Only the first is required. path_or_buf: A string path to the file to write or a file object. If a file object it must be opened with newline='' sep : Field delimiter for the output file (default “,”) na_rep: A string representation of a missing value (default ‘’) float_format: Format string for floating point numbers columns: Columns to write (default None) header: Whether to write out the column names (default True) index: whether to write row (index) names (default True) index_label: Column label(s) for index column(s) if desired. If None (default), and header and index are True, then the index names are used. (A sequence should be given if the DataFrame uses MultiIndex). mode : Python write mode, default ‘w’ encoding: a string representing the encoding to use if the contents are non-ASCII, for Python versions prior to 3 lineterminator: Character sequence denoting line end (default os.linesep) quoting: Set quoting rules as in csv module (default csv.QUOTE_MINIMAL). Note that if you have set a float_format then floats are converted to strings and csv.QUOTE_NONNUMERIC will treat them as non-numeric quotechar: Character used to quote fields (default ‘”’) doublequote: Control quoting of quotechar in fields (default True) escapechar: Character used to escape sep and quotechar when appropriate (default None) chunksize: Number of rows to write at a time date_format: Format string for datetime objects Writing a formatted string# The DataFrame object has an instance method to_string which allows control over the string representation of the object. All arguments are optional: buf default None, for example a StringIO object columns default None, which columns to write col_space default None, minimum width of each column. na_rep default NaN, representation of NA value formatters default None, a dictionary (by column) of functions each of which takes a single argument and returns a formatted string float_format default None, a function which takes a single (float) argument and returns a formatted string; to be applied to floats in the DataFrame. sparsify default True, set to False for a DataFrame with a hierarchical index to print every MultiIndex key at each row. index_names default True, will print the names of the indices index default True, will print the index (ie, row labels) header default True, will print the column labels justify default left, will print column headers left- or right-justified The Series object also has a to_string method, but with only the buf, na_rep, float_format arguments. There is also a length argument which, if set to True, will additionally output the length of the Series. JSON# Read and write JSON format files and strings. Writing JSON# A Series or DataFrame can be converted to a valid JSON string. Use to_json with optional parameters: path_or_buf : the pathname or buffer to write the output This can be None in which case a JSON string is returned orient : Series: default is index allowed values are {split, records, index} DataFrame: default is columns allowed values are {split, records, index, columns, values, table} The format of the JSON string split dict like {index -> [index], columns -> [columns], data -> [values]} records list like [{column -> value}, … , {column -> value}] index dict like {index -> {column -> value}} columns dict like {column -> {index -> value}} values just the values array table adhering to the JSON Table Schema date_format : string, type of date conversion, ‘epoch’ for timestamp, ‘iso’ for ISO8601. double_precision : The number of decimal places to use when encoding floating point values, default 10. force_ascii : force encoded string to be ASCII, default True. date_unit : The time unit to encode to, governs timestamp and ISO8601 precision. One of ‘s’, ‘ms’, ‘us’ or ‘ns’ for seconds, milliseconds, microseconds and nanoseconds respectively. Default ‘ms’. default_handler : The handler to call if an object cannot otherwise be converted to a suitable format for JSON. Takes a single argument, which is the object to convert, and returns a serializable object. lines : If records orient, then will write each record per line as json. Note NaN’s, NaT’s and None will be converted to null and datetime objects will be converted based on the date_format and date_unit parameters. In [218]: dfj = pd.DataFrame(np.random.randn(5, 2), columns=list("AB")) In [219]: json = dfj.to_json() In [220]: json Out[220]: '{"A":{"0":-0.1213062281,"1":0.6957746499,"2":0.9597255933,"3":-0.6199759194,"4":-0.7323393705},"B":{"0":-0.0978826728,"1":0.3417343559,"2":-1.1103361029,"3":0.1497483186,"4":0.6877383895}}' Orient options# There are a number of different options for the format of the resulting JSON file / string. Consider the following DataFrame and Series: In [221]: dfjo = pd.DataFrame( .....: dict(A=range(1, 4), B=range(4, 7), C=range(7, 10)), .....: columns=list("ABC"), .....: index=list("xyz"), .....: ) .....: In [222]: dfjo Out[222]: A B C x 1 4 7 y 2 5 8 z 3 6 9 In [223]: sjo = pd.Series(dict(x=15, y=16, z=17), name="D") In [224]: sjo Out[224]: x 15 y 16 z 17 Name: D, dtype: int64 Column oriented (the default for DataFrame) serializes the data as nested JSON objects with column labels acting as the primary index: In [225]: dfjo.to_json(orient="columns") Out[225]: '{"A":{"x":1,"y":2,"z":3},"B":{"x":4,"y":5,"z":6},"C":{"x":7,"y":8,"z":9}}' # Not available for Series Index oriented (the default for Series) similar to column oriented but the index labels are now primary: In [226]: dfjo.to_json(orient="index") Out[226]: '{"x":{"A":1,"B":4,"C":7},"y":{"A":2,"B":5,"C":8},"z":{"A":3,"B":6,"C":9}}' In [227]: sjo.to_json(orient="index") Out[227]: '{"x":15,"y":16,"z":17}' Record oriented serializes the data to a JSON array of column -> value records, index labels are not included. This is useful for passing DataFrame data to plotting libraries, for example the JavaScript library d3.js: In [228]: dfjo.to_json(orient="records") Out[228]: '[{"A":1,"B":4,"C":7},{"A":2,"B":5,"C":8},{"A":3,"B":6,"C":9}]' In [229]: sjo.to_json(orient="records") Out[229]: '[15,16,17]' Value oriented is a bare-bones option which serializes to nested JSON arrays of values only, column and index labels are not included: In [230]: dfjo.to_json(orient="values") Out[230]: '[[1,4,7],[2,5,8],[3,6,9]]' # Not available for Series Split oriented serializes to a JSON object containing separate entries for values, index and columns. Name is also included for Series: In [231]: dfjo.to_json(orient="split") Out[231]: '{"columns":["A","B","C"],"index":["x","y","z"],"data":[[1,4,7],[2,5,8],[3,6,9]]}' In [232]: sjo.to_json(orient="split") Out[232]: '{"name":"D","index":["x","y","z"],"data":[15,16,17]}' Table oriented serializes to the JSON Table Schema, allowing for the preservation of metadata including but not limited to dtypes and index names. Note Any orient option that encodes to a JSON object will not preserve the ordering of index and column labels during round-trip serialization. If you wish to preserve label ordering use the split option as it uses ordered containers. Date handling# Writing in ISO date format: In [233]: dfd = pd.DataFrame(np.random.randn(5, 2), columns=list("AB")) In [234]: dfd["date"] = pd.Timestamp("20130101") In [235]: dfd = dfd.sort_index(axis=1, ascending=False) In [236]: json = dfd.to_json(date_format="iso") In [237]: json Out[237]: '{"date":{"0":"2013-01-01T00:00:00.000","1":"2013-01-01T00:00:00.000","2":"2013-01-01T00:00:00.000","3":"2013-01-01T00:00:00.000","4":"2013-01-01T00:00:00.000"},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Writing in ISO date format, with microseconds: In [238]: json = dfd.to_json(date_format="iso", date_unit="us") In [239]: json Out[239]: '{"date":{"0":"2013-01-01T00:00:00.000000","1":"2013-01-01T00:00:00.000000","2":"2013-01-01T00:00:00.000000","3":"2013-01-01T00:00:00.000000","4":"2013-01-01T00:00:00.000000"},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Epoch timestamps, in seconds: In [240]: json = dfd.to_json(date_format="epoch", date_unit="s") In [241]: json Out[241]: '{"date":{"0":1356998400,"1":1356998400,"2":1356998400,"3":1356998400,"4":1356998400},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Writing to a file, with a date index and a date column: In [242]: dfj2 = dfj.copy() In [243]: dfj2["date"] = pd.Timestamp("20130101") In [244]: dfj2["ints"] = list(range(5)) In [245]: dfj2["bools"] = True In [246]: dfj2.index = pd.date_range("20130101", periods=5) In [247]: dfj2.to_json("test.json") In [248]: with open("test.json") as fh: .....: print(fh.read()) .....: {"A":{"1356998400000":-0.1213062281,"1357084800000":0.6957746499,"1357171200000":0.9597255933,"1357257600000":-0.6199759194,"1357344000000":-0.7323393705},"B":{"1356998400000":-0.0978826728,"1357084800000":0.3417343559,"1357171200000":-1.1103361029,"1357257600000":0.1497483186,"1357344000000":0.6877383895},"date":{"1356998400000":1356998400000,"1357084800000":1356998400000,"1357171200000":1356998400000,"1357257600000":1356998400000,"1357344000000":1356998400000},"ints":{"1356998400000":0,"1357084800000":1,"1357171200000":2,"1357257600000":3,"1357344000000":4},"bools":{"1356998400000":true,"1357084800000":true,"1357171200000":true,"1357257600000":true,"1357344000000":true}} Fallback behavior# If the JSON serializer cannot handle the container contents directly it will fall back in the following manner: if the dtype is unsupported (e.g. np.complex_) then the default_handler, if provided, will be called for each value, otherwise an exception is raised. if an object is unsupported it will attempt the following: check if the object has defined a toDict method and call it. A toDict method should return a dict which will then be JSON serialized. invoke the default_handler if one was provided. convert the object to a dict by traversing its contents. However this will often fail with an OverflowError or give unexpected results. In general the best approach for unsupported objects or dtypes is to provide a default_handler. For example: >>> DataFrame([1.0, 2.0, complex(1.0, 2.0)]).to_json() # raises RuntimeError: Unhandled numpy dtype 15 can be dealt with by specifying a simple default_handler: In [249]: pd.DataFrame([1.0, 2.0, complex(1.0, 2.0)]).to_json(default_handler=str) Out[249]: '{"0":{"0":"(1+0j)","1":"(2+0j)","2":"(1+2j)"}}' Reading JSON# Reading a JSON string to pandas object can take a number of parameters. The parser will try to parse a DataFrame if typ is not supplied or is None. To explicitly force Series parsing, pass typ=series filepath_or_buffer : a VALID JSON string or file handle / StringIO. The string could be a URL. Valid URL schemes include http, ftp, S3, and file. For file URLs, a host is expected. For instance, a local file could be file ://localhost/path/to/table.json typ : type of object to recover (series or frame), default ‘frame’ orient : Series : default is index allowed values are {split, records, index} DataFrame default is columns allowed values are {split, records, index, columns, values, table} The format of the JSON string split dict like {index -> [index], columns -> [columns], data -> [values]} records list like [{column -> value}, … , {column -> value}] index dict like {index -> {column -> value}} columns dict like {column -> {index -> value}} values just the values array table adhering to the JSON Table Schema dtype : if True, infer dtypes, if a dict of column to dtype, then use those, if False, then don’t infer dtypes at all, default is True, apply only to the data. convert_axes : boolean, try to convert the axes to the proper dtypes, default is True convert_dates : a list of columns to parse for dates; If True, then try to parse date-like columns, default is True. keep_default_dates : boolean, default True. If parsing dates, then parse the default date-like columns. numpy : direct decoding to NumPy arrays. default is False; Supports numeric data only, although labels may be non-numeric. Also note that the JSON ordering MUST be the same for each term if numpy=True. precise_float : boolean, default False. Set to enable usage of higher precision (strtod) function when decoding string to double values. Default (False) is to use fast but less precise builtin functionality. date_unit : string, the timestamp unit to detect if converting dates. Default None. By default the timestamp precision will be detected, if this is not desired then pass one of ‘s’, ‘ms’, ‘us’ or ‘ns’ to force timestamp precision to seconds, milliseconds, microseconds or nanoseconds respectively. lines : reads file as one json object per line. encoding : The encoding to use to decode py3 bytes. chunksize : when used in combination with lines=True, return a JsonReader which reads in chunksize lines per iteration. The parser will raise one of ValueError/TypeError/AssertionError if the JSON is not parseable. If a non-default orient was used when encoding to JSON be sure to pass the same option here so that decoding produces sensible results, see Orient Options for an overview. Data conversion# The default of convert_axes=True, dtype=True, and convert_dates=True will try to parse the axes, and all of the data into appropriate types, including dates. If you need to override specific dtypes, pass a dict to dtype. convert_axes should only be set to False if you need to preserve string-like numbers (e.g. ‘1’, ‘2’) in an axes. Note Large integer values may be converted to dates if convert_dates=True and the data and / or column labels appear ‘date-like’. The exact threshold depends on the date_unit specified. ‘date-like’ means that the column label meets one of the following criteria: it ends with '_at' it ends with '_time' it begins with 'timestamp' it is 'modified' it is 'date' Warning When reading JSON data, automatic coercing into dtypes has some quirks: an index can be reconstructed in a different order from serialization, that is, the returned order is not guaranteed to be the same as before serialization a column that was float data will be converted to integer if it can be done safely, e.g. a column of 1. bool columns will be converted to integer on reconstruction Thus there are times where you may want to specify specific dtypes via the dtype keyword argument. Reading from a JSON string: In [250]: pd.read_json(json) Out[250]: date B A 0 2013-01-01 0.403310 0.176444 1 2013-01-01 0.301624 -0.154951 2 2013-01-01 -1.369849 -2.179861 3 2013-01-01 1.462696 -0.954208 4 2013-01-01 -0.826591 -1.743161 Reading from a file: In [251]: pd.read_json("test.json") Out[251]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True Don’t convert any data (but still convert axes and dates): In [252]: pd.read_json("test.json", dtype=object).dtypes Out[252]: A object B object date object ints object bools object dtype: object Specify dtypes for conversion: In [253]: pd.read_json("test.json", dtype={"A": "float32", "bools": "int8"}).dtypes Out[253]: A float32 B float64 date datetime64[ns] ints int64 bools int8 dtype: object Preserve string indices: In [254]: si = pd.DataFrame( .....: np.zeros((4, 4)), columns=list(range(4)), index=[str(i) for i in range(4)] .....: ) .....: In [255]: si Out[255]: 0 1 2 3 0 0.0 0.0 0.0 0.0 1 0.0 0.0 0.0 0.0 2 0.0 0.0 0.0 0.0 3 0.0 0.0 0.0 0.0 In [256]: si.index Out[256]: Index(['0', '1', '2', '3'], dtype='object') In [257]: si.columns Out[257]: Int64Index([0, 1, 2, 3], dtype='int64') In [258]: json = si.to_json() In [259]: sij = pd.read_json(json, convert_axes=False) In [260]: sij Out[260]: 0 1 2 3 0 0 0 0 0 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 In [261]: sij.index Out[261]: Index(['0', '1', '2', '3'], dtype='object') In [262]: sij.columns Out[262]: Index(['0', '1', '2', '3'], dtype='object') Dates written in nanoseconds need to be read back in nanoseconds: In [263]: json = dfj2.to_json(date_unit="ns") # Try to parse timestamps as milliseconds -> Won't Work In [264]: dfju = pd.read_json(json, date_unit="ms") In [265]: dfju Out[265]: A B date ints bools 1356998400000000000 -0.121306 -0.097883 1356998400000000000 0 True 1357084800000000000 0.695775 0.341734 1356998400000000000 1 True 1357171200000000000 0.959726 -1.110336 1356998400000000000 2 True 1357257600000000000 -0.619976 0.149748 1356998400000000000 3 True 1357344000000000000 -0.732339 0.687738 1356998400000000000 4 True # Let pandas detect the correct precision In [266]: dfju = pd.read_json(json) In [267]: dfju Out[267]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True # Or specify that all timestamps are in nanoseconds In [268]: dfju = pd.read_json(json, date_unit="ns") In [269]: dfju Out[269]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True The Numpy parameter# Note This param has been deprecated as of version 1.0.0 and will raise a FutureWarning. This supports numeric data only. Index and columns labels may be non-numeric, e.g. strings, dates etc. If numpy=True is passed to read_json an attempt will be made to sniff an appropriate dtype during deserialization and to subsequently decode directly to NumPy arrays, bypassing the need for intermediate Python objects. This can provide speedups if you are deserialising a large amount of numeric data: In [270]: randfloats = np.random.uniform(-100, 1000, 10000) In [271]: randfloats.shape = (1000, 10) In [272]: dffloats = pd.DataFrame(randfloats, columns=list("ABCDEFGHIJ")) In [273]: jsonfloats = dffloats.to_json() In [274]: %timeit pd.read_json(jsonfloats) 7.91 ms +- 77.3 us per loop (mean +- std. dev. of 7 runs, 100 loops each) In [275]: %timeit pd.read_json(jsonfloats, numpy=True) 5.71 ms +- 333 us per loop (mean +- std. dev. of 7 runs, 100 loops each) The speedup is less noticeable for smaller datasets: In [276]: jsonfloats = dffloats.head(100).to_json() In [277]: %timeit pd.read_json(jsonfloats) 4.46 ms +- 25.9 us per loop (mean +- std. dev. of 7 runs, 100 loops each) In [278]: %timeit pd.read_json(jsonfloats, numpy=True) 4.09 ms +- 32.3 us per loop (mean +- std. dev. of 7 runs, 100 loops each) Warning Direct NumPy decoding makes a number of assumptions and may fail or produce unexpected output if these assumptions are not satisfied: data is numeric. data is uniform. The dtype is sniffed from the first value decoded. A ValueError may be raised, or incorrect output may be produced if this condition is not satisfied. labels are ordered. Labels are only read from the first container, it is assumed that each subsequent row / column has been encoded in the same order. This should be satisfied if the data was encoded using to_json but may not be the case if the JSON is from another source. Normalization# pandas provides a utility function to take a dict or list of dicts and normalize this semi-structured data into a flat table. In [279]: data = [ .....: {"id": 1, "name": {"first": "Coleen", "last": "Volk"}}, .....: {"name": {"given": "Mark", "family": "Regner"}}, .....: {"id": 2, "name": "Faye Raker"}, .....: ] .....: In [280]: pd.json_normalize(data) Out[280]: id name.first name.last name.given name.family name 0 1.0 Coleen Volk NaN NaN NaN 1 NaN NaN NaN Mark Regner NaN 2 2.0 NaN NaN NaN NaN Faye Raker In [281]: data = [ .....: { .....: "state": "Florida", .....: "shortname": "FL", .....: "info": {"governor": "Rick Scott"}, .....: "county": [ .....: {"name": "Dade", "population": 12345}, .....: {"name": "Broward", "population": 40000}, .....: {"name": "Palm Beach", "population": 60000}, .....: ], .....: }, .....: { .....: "state": "Ohio", .....: "shortname": "OH", .....: "info": {"governor": "John Kasich"}, .....: "county": [ .....: {"name": "Summit", "population": 1234}, .....: {"name": "Cuyahoga", "population": 1337}, .....: ], .....: }, .....: ] .....: In [282]: pd.json_normalize(data, "county", ["state", "shortname", ["info", "governor"]]) Out[282]: name population state shortname info.governor 0 Dade 12345 Florida FL Rick Scott 1 Broward 40000 Florida FL Rick Scott 2 Palm Beach 60000 Florida FL Rick Scott 3 Summit 1234 Ohio OH John Kasich 4 Cuyahoga 1337 Ohio OH John Kasich The max_level parameter provides more control over which level to end normalization. With max_level=1 the following snippet normalizes until 1st nesting level of the provided dict. In [283]: data = [ .....: { .....: "CreatedBy": {"Name": "User001"}, .....: "Lookup": { .....: "TextField": "Some text", .....: "UserField": {"Id": "ID001", "Name": "Name001"}, .....: }, .....: "Image": {"a": "b"}, .....: } .....: ] .....: In [284]: pd.json_normalize(data, max_level=1) Out[284]: CreatedBy.Name Lookup.TextField Lookup.UserField Image.a 0 User001 Some text {'Id': 'ID001', 'Name': 'Name001'} b Line delimited json# pandas is able to read and write line-delimited json files that are common in data processing pipelines using Hadoop or Spark. For line-delimited json files, pandas can also return an iterator which reads in chunksize lines at a time. This can be useful for large files or to read from a stream. In [285]: jsonl = """ .....: {"a": 1, "b": 2} .....: {"a": 3, "b": 4} .....: """ .....: In [286]: df = pd.read_json(jsonl, lines=True) In [287]: df Out[287]: a b 0 1 2 1 3 4 In [288]: df.to_json(orient="records", lines=True) Out[288]: '{"a":1,"b":2}\n{"a":3,"b":4}\n' # reader is an iterator that returns ``chunksize`` lines each iteration In [289]: with pd.read_json(StringIO(jsonl), lines=True, chunksize=1) as reader: .....: reader .....: for chunk in reader: .....: print(chunk) .....: Empty DataFrame Columns: [] Index: [] a b 0 1 2 a b 1 3 4 Table schema# Table Schema is a spec for describing tabular datasets as a JSON object. The JSON includes information on the field names, types, and other attributes. You can use the orient table to build a JSON string with two fields, schema and data. In [290]: df = pd.DataFrame( .....: { .....: "A": [1, 2, 3], .....: "B": ["a", "b", "c"], .....: "C": pd.date_range("2016-01-01", freq="d", periods=3), .....: }, .....: index=pd.Index(range(3), name="idx"), .....: ) .....: In [291]: df Out[291]: A B C idx 0 1 a 2016-01-01 1 2 b 2016-01-02 2 3 c 2016-01-03 In [292]: df.to_json(orient="table", date_format="iso") Out[292]: '{"schema":{"fields":[{"name":"idx","type":"integer"},{"name":"A","type":"integer"},{"name":"B","type":"string"},{"name":"C","type":"datetime"}],"primaryKey":["idx"],"pandas_version":"1.4.0"},"data":[{"idx":0,"A":1,"B":"a","C":"2016-01-01T00:00:00.000"},{"idx":1,"A":2,"B":"b","C":"2016-01-02T00:00:00.000"},{"idx":2,"A":3,"B":"c","C":"2016-01-03T00:00:00.000"}]}' The schema field contains the fields key, which itself contains a list of column name to type pairs, including the Index or MultiIndex (see below for a list of types). The schema field also contains a primaryKey field if the (Multi)index is unique. The second field, data, contains the serialized data with the records orient. The index is included, and any datetimes are ISO 8601 formatted, as required by the Table Schema spec. The full list of types supported are described in the Table Schema spec. This table shows the mapping from pandas types: pandas type Table Schema type int64 integer float64 number bool boolean datetime64[ns] datetime timedelta64[ns] duration categorical any object str A few notes on the generated table schema: The schema object contains a pandas_version field. This contains the version of pandas’ dialect of the schema, and will be incremented with each revision. All dates are converted to UTC when serializing. Even timezone naive values, which are treated as UTC with an offset of 0. In [293]: from pandas.io.json import build_table_schema In [294]: s = pd.Series(pd.date_range("2016", periods=4)) In [295]: build_table_schema(s) Out[295]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'datetime'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} datetimes with a timezone (before serializing), include an additional field tz with the time zone name (e.g. 'US/Central'). In [296]: s_tz = pd.Series(pd.date_range("2016", periods=12, tz="US/Central")) In [297]: build_table_schema(s_tz) Out[297]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'datetime', 'tz': 'US/Central'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} Periods are converted to timestamps before serialization, and so have the same behavior of being converted to UTC. In addition, periods will contain and additional field freq with the period’s frequency, e.g. 'A-DEC'. In [298]: s_per = pd.Series(1, index=pd.period_range("2016", freq="A-DEC", periods=4)) In [299]: build_table_schema(s_per) Out[299]: {'fields': [{'name': 'index', 'type': 'datetime', 'freq': 'A-DEC'}, {'name': 'values', 'type': 'integer'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} Categoricals use the any type and an enum constraint listing the set of possible values. Additionally, an ordered field is included: In [300]: s_cat = pd.Series(pd.Categorical(["a", "b", "a"])) In [301]: build_table_schema(s_cat) Out[301]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'any', 'constraints': {'enum': ['a', 'b']}, 'ordered': False}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} A primaryKey field, containing an array of labels, is included if the index is unique: In [302]: s_dupe = pd.Series([1, 2], index=[1, 1]) In [303]: build_table_schema(s_dupe) Out[303]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'integer'}], 'pandas_version': '1.4.0'} The primaryKey behavior is the same with MultiIndexes, but in this case the primaryKey is an array: In [304]: s_multi = pd.Series(1, index=pd.MultiIndex.from_product([("a", "b"), (0, 1)])) In [305]: build_table_schema(s_multi) Out[305]: {'fields': [{'name': 'level_0', 'type': 'string'}, {'name': 'level_1', 'type': 'integer'}, {'name': 'values', 'type': 'integer'}], 'primaryKey': FrozenList(['level_0', 'level_1']), 'pandas_version': '1.4.0'} The default naming roughly follows these rules: For series, the object.name is used. If that’s none, then the name is values For DataFrames, the stringified version of the column name is used For Index (not MultiIndex), index.name is used, with a fallback to index if that is None. For MultiIndex, mi.names is used. If any level has no name, then level_<i> is used. read_json also accepts orient='table' as an argument. This allows for the preservation of metadata such as dtypes and index names in a round-trippable manner. In [306]: df = pd.DataFrame( .....: { .....: "foo": [1, 2, 3, 4], .....: "bar": ["a", "b", "c", "d"], .....: "baz": pd.date_range("2018-01-01", freq="d", periods=4), .....: "qux": pd.Categorical(["a", "b", "c", "c"]), .....: }, .....: index=pd.Index(range(4), name="idx"), .....: ) .....: In [307]: df Out[307]: foo bar baz qux idx 0 1 a 2018-01-01 a 1 2 b 2018-01-02 b 2 3 c 2018-01-03 c 3 4 d 2018-01-04 c In [308]: df.dtypes Out[308]: foo int64 bar object baz datetime64[ns] qux category dtype: object In [309]: df.to_json("test.json", orient="table") In [310]: new_df = pd.read_json("test.json", orient="table") In [311]: new_df Out[311]: foo bar baz qux idx 0 1 a 2018-01-01 a 1 2 b 2018-01-02 b 2 3 c 2018-01-03 c 3 4 d 2018-01-04 c In [312]: new_df.dtypes Out[312]: foo int64 bar object baz datetime64[ns] qux category dtype: object Please note that the literal string ‘index’ as the name of an Index is not round-trippable, nor are any names beginning with 'level_' within a MultiIndex. These are used by default in DataFrame.to_json() to indicate missing values and the subsequent read cannot distinguish the intent. In [313]: df.index.name = "index" In [314]: df.to_json("test.json", orient="table") In [315]: new_df = pd.read_json("test.json", orient="table") In [316]: print(new_df.index.name) None When using orient='table' along with user-defined ExtensionArray, the generated schema will contain an additional extDtype key in the respective fields element. This extra key is not standard but does enable JSON roundtrips for extension types (e.g. read_json(df.to_json(orient="table"), orient="table")). The extDtype key carries the name of the extension, if you have properly registered the ExtensionDtype, pandas will use said name to perform a lookup into the registry and re-convert the serialized data into your custom dtype. HTML# Reading HTML content# Warning We highly encourage you to read the HTML Table Parsing gotchas below regarding the issues surrounding the BeautifulSoup4/html5lib/lxml parsers. The top-level read_html() function can accept an HTML string/file/URL and will parse HTML tables into list of pandas DataFrames. Let’s look at a few examples. Note read_html returns a list of DataFrame objects, even if there is only a single table contained in the HTML content. Read a URL with no options: In [320]: "https://www.fdic.gov/resources/resolutions/bank-failures/failed-bank-list" In [321]: pd.read_html(url) Out[321]: [ Bank NameBank CityCity StateSt ... Acquiring InstitutionAI Closing DateClosing FundFund 0 Almena State Bank Almena KS ... Equity Bank October 23, 2020 10538 1 First City Bank of Florida Fort Walton Beach FL ... United Fidelity Bank, fsb October 16, 2020 10537 2 The First State Bank Barboursville WV ... MVB Bank, Inc. April 3, 2020 10536 3 Ericson State Bank Ericson NE ... Farmers and Merchants Bank February 14, 2020 10535 4 City National Bank of New Jersey Newark NJ ... Industrial Bank November 1, 2019 10534 .. ... ... ... ... ... ... ... 558 Superior Bank, FSB Hinsdale IL ... Superior Federal, FSB July 27, 2001 6004 559 Malta National Bank Malta OH ... North Valley Bank May 3, 2001 4648 560 First Alliance Bank & Trust Co. Manchester NH ... Southern New Hampshire Bank & Trust February 2, 2001 4647 561 National State Bank of Metropolis Metropolis IL ... Banterra Bank of Marion December 14, 2000 4646 562 Bank of Honolulu Honolulu HI ... Bank of the Orient October 13, 2000 4645 [563 rows x 7 columns]] Note The data from the above URL changes every Monday so the resulting data above may be slightly different. Read in the content of the file from the above URL and pass it to read_html as a string: In [317]: html_str = """ .....: <table> .....: <tr> .....: <th>A</th> .....: <th colspan="1">B</th> .....: <th rowspan="1">C</th> .....: </tr> .....: <tr> .....: <td>a</td> .....: <td>b</td> .....: <td>c</td> .....: </tr> .....: </table> .....: """ .....: In [318]: with open("tmp.html", "w") as f: .....: f.write(html_str) .....: In [319]: df = pd.read_html("tmp.html") In [320]: df[0] Out[320]: A B C 0 a b c You can even pass in an instance of StringIO if you so desire: In [321]: dfs = pd.read_html(StringIO(html_str)) In [322]: dfs[0] Out[322]: A B C 0 a b c Note The following examples are not run by the IPython evaluator due to the fact that having so many network-accessing functions slows down the documentation build. If you spot an error or an example that doesn’t run, please do not hesitate to report it over on pandas GitHub issues page. Read a URL and match a table that contains specific text: match = "Metcalf Bank" df_list = pd.read_html(url, match=match) Specify a header row (by default <th> or <td> elements located within a <thead> are used to form the column index, if multiple rows are contained within <thead> then a MultiIndex is created); if specified, the header row is taken from the data minus the parsed header elements (<th> elements). dfs = pd.read_html(url, header=0) Specify an index column: dfs = pd.read_html(url, index_col=0) Specify a number of rows to skip: dfs = pd.read_html(url, skiprows=0) Specify a number of rows to skip using a list (range works as well): dfs = pd.read_html(url, skiprows=range(2)) Specify an HTML attribute: dfs1 = pd.read_html(url, attrs={"id": "table"}) dfs2 = pd.read_html(url, attrs={"class": "sortable"}) print(np.array_equal(dfs1[0], dfs2[0])) # Should be True Specify values that should be converted to NaN: dfs = pd.read_html(url, na_values=["No Acquirer"]) Specify whether to keep the default set of NaN values: dfs = pd.read_html(url, keep_default_na=False) Specify converters for columns. This is useful for numerical text data that has leading zeros. By default columns that are numerical are cast to numeric types and the leading zeros are lost. To avoid this, we can convert these columns to strings. url_mcc = "https://en.wikipedia.org/wiki/Mobile_country_code" dfs = pd.read_html( url_mcc, match="Telekom Albania", header=0, converters={"MNC": str}, ) Use some combination of the above: dfs = pd.read_html(url, match="Metcalf Bank", index_col=0) Read in pandas to_html output (with some loss of floating point precision): df = pd.DataFrame(np.random.randn(2, 2)) s = df.to_html(float_format="{0:.40g}".format) dfin = pd.read_html(s, index_col=0) The lxml backend will raise an error on a failed parse if that is the only parser you provide. If you only have a single parser you can provide just a string, but it is considered good practice to pass a list with one string if, for example, the function expects a sequence of strings. You may use: dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor=["lxml"]) Or you could pass flavor='lxml' without a list: dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor="lxml") However, if you have bs4 and html5lib installed and pass None or ['lxml', 'bs4'] then the parse will most likely succeed. Note that as soon as a parse succeeds, the function will return. dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor=["lxml", "bs4"]) Links can be extracted from cells along with the text using extract_links="all". In [323]: html_table = """ .....: <table> .....: <tr> .....: <th>GitHub</th> .....: </tr> .....: <tr> .....: <td><a href="https://github.com/pandas-dev/pandas">pandas</a></td> .....: </tr> .....: </table> .....: """ .....: In [324]: df = pd.read_html( .....: html_table, .....: extract_links="all" .....: )[0] .....: In [325]: df Out[325]: (GitHub, None) 0 (pandas, https://github.com/pandas-dev/pandas) In [326]: df[("GitHub", None)] Out[326]: 0 (pandas, https://github.com/pandas-dev/pandas) Name: (GitHub, None), dtype: object In [327]: df[("GitHub", None)].str[1] Out[327]: 0 https://github.com/pandas-dev/pandas Name: (GitHub, None), dtype: object New in version 1.5.0. Writing to HTML files# DataFrame objects have an instance method to_html which renders the contents of the DataFrame as an HTML table. The function arguments are as in the method to_string described above. Note Not all of the possible options for DataFrame.to_html are shown here for brevity’s sake. See to_html() for the full set of options. Note In an HTML-rendering supported environment like a Jupyter Notebook, display(HTML(...))` will render the raw HTML into the environment. In [328]: from IPython.display import display, HTML In [329]: df = pd.DataFrame(np.random.randn(2, 2)) In [330]: df Out[330]: 0 1 0 0.070319 1.773907 1 0.253908 0.414581 In [331]: html = df.to_html() In [332]: print(html) # raw html <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <th>1</th> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> In [333]: display(HTML(html)) <IPython.core.display.HTML object> The columns argument will limit the columns shown: In [334]: html = df.to_html(columns=[0]) In [335]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> </tr> <tr> <th>1</th> <td>0.253908</td> </tr> </tbody> </table> In [336]: display(HTML(html)) <IPython.core.display.HTML object> float_format takes a Python callable to control the precision of floating point values: In [337]: html = df.to_html(float_format="{0:.10f}".format) In [338]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.0703192665</td> <td>1.7739074228</td> </tr> <tr> <th>1</th> <td>0.2539083433</td> <td>0.4145805920</td> </tr> </tbody> </table> In [339]: display(HTML(html)) <IPython.core.display.HTML object> bold_rows will make the row labels bold by default, but you can turn that off: In [340]: html = df.to_html(bold_rows=False) In [341]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <td>1</td> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> In [342]: display(HTML(html)) <IPython.core.display.HTML object> The classes argument provides the ability to give the resulting HTML table CSS classes. Note that these classes are appended to the existing 'dataframe' class. In [343]: print(df.to_html(classes=["awesome_table_class", "even_more_awesome_class"])) <table border="1" class="dataframe awesome_table_class even_more_awesome_class"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <th>1</th> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> The render_links argument provides the ability to add hyperlinks to cells that contain URLs. In [344]: url_df = pd.DataFrame( .....: { .....: "name": ["Python", "pandas"], .....: "url": ["https://www.python.org/", "https://pandas.pydata.org"], .....: } .....: ) .....: In [345]: html = url_df.to_html(render_links=True) In [346]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>name</th> <th>url</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>Python</td> <td><a href="https://www.python.org/" target="_blank">https://www.python.org/</a></td> </tr> <tr> <th>1</th> <td>pandas</td> <td><a href="https://pandas.pydata.org" target="_blank">https://pandas.pydata.org</a></td> </tr> </tbody> </table> In [347]: display(HTML(html)) <IPython.core.display.HTML object> Finally, the escape argument allows you to control whether the “<”, “>” and “&” characters escaped in the resulting HTML (by default it is True). So to get the HTML without escaped characters pass escape=False In [348]: df = pd.DataFrame({"a": list("&<>"), "b": np.random.randn(3)}) Escaped: In [349]: html = df.to_html() In [350]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>a</th> <th>b</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>&amp;</td> <td>0.842321</td> </tr> <tr> <th>1</th> <td>&lt;</td> <td>0.211337</td> </tr> <tr> <th>2</th> <td>&gt;</td> <td>-1.055427</td> </tr> </tbody> </table> In [351]: display(HTML(html)) <IPython.core.display.HTML object> Not escaped: In [352]: html = df.to_html(escape=False) In [353]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>a</th> <th>b</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>&</td> <td>0.842321</td> </tr> <tr> <th>1</th> <td><</td> <td>0.211337</td> </tr> <tr> <th>2</th> <td>></td> <td>-1.055427</td> </tr> </tbody> </table> In [354]: display(HTML(html)) <IPython.core.display.HTML object> Note Some browsers may not show a difference in the rendering of the previous two HTML tables. HTML Table Parsing Gotchas# There are some versioning issues surrounding the libraries that are used to parse HTML tables in the top-level pandas io function read_html. Issues with lxml Benefits lxml is very fast. lxml requires Cython to install correctly. Drawbacks lxml does not make any guarantees about the results of its parse unless it is given strictly valid markup. In light of the above, we have chosen to allow you, the user, to use the lxml backend, but this backend will use html5lib if lxml fails to parse It is therefore highly recommended that you install both BeautifulSoup4 and html5lib, so that you will still get a valid result (provided everything else is valid) even if lxml fails. Issues with BeautifulSoup4 using lxml as a backend The above issues hold here as well since BeautifulSoup4 is essentially just a wrapper around a parser backend. Issues with BeautifulSoup4 using html5lib as a backend Benefits html5lib is far more lenient than lxml and consequently deals with real-life markup in a much saner way rather than just, e.g., dropping an element without notifying you. html5lib generates valid HTML5 markup from invalid markup automatically. This is extremely important for parsing HTML tables, since it guarantees a valid document. However, that does NOT mean that it is “correct”, since the process of fixing markup does not have a single definition. html5lib is pure Python and requires no additional build steps beyond its own installation. Drawbacks The biggest drawback to using html5lib is that it is slow as molasses. However consider the fact that many tables on the web are not big enough for the parsing algorithm runtime to matter. It is more likely that the bottleneck will be in the process of reading the raw text from the URL over the web, i.e., IO (input-output). For very large tables, this might not be true. LaTeX# New in version 1.3.0. Currently there are no methods to read from LaTeX, only output methods. Writing to LaTeX files# Note DataFrame and Styler objects currently have a to_latex method. We recommend using the Styler.to_latex() method over DataFrame.to_latex() due to the former’s greater flexibility with conditional styling, and the latter’s possible future deprecation. Review the documentation for Styler.to_latex, which gives examples of conditional styling and explains the operation of its keyword arguments. For simple application the following pattern is sufficient. In [355]: df = pd.DataFrame([[1, 2], [3, 4]], index=["a", "b"], columns=["c", "d"]) In [356]: print(df.style.to_latex()) \begin{tabular}{lrr} & c & d \\ a & 1 & 2 \\ b & 3 & 4 \\ \end{tabular} To format values before output, chain the Styler.format method. In [357]: print(df.style.format("€ {}").to_latex()) \begin{tabular}{lrr} & c & d \\ a & € 1 & € 2 \\ b & € 3 & € 4 \\ \end{tabular} XML# Reading XML# New in version 1.3.0. The top-level read_xml() function can accept an XML string/file/URL and will parse nodes and attributes into a pandas DataFrame. Note Since there is no standard XML structure where design types can vary in many ways, read_xml works best with flatter, shallow versions. If an XML document is deeply nested, use the stylesheet feature to transform XML into a flatter version. Let’s look at a few examples. Read an XML string: In [358]: xml = """<?xml version="1.0" encoding="UTF-8"?> .....: <bookstore> .....: <book category="cooking"> .....: <title lang="en">Everyday Italian</title> .....: <author>Giada De Laurentiis</author> .....: <year>2005</year> .....: <price>30.00</price> .....: </book> .....: <book category="children"> .....: <title lang="en">Harry Potter</title> .....: <author>J K. Rowling</author> .....: <year>2005</year> .....: <price>29.99</price> .....: </book> .....: <book category="web"> .....: <title lang="en">Learning XML</title> .....: <author>Erik T. Ray</author> .....: <year>2003</year> .....: <price>39.95</price> .....: </book> .....: </bookstore>""" .....: In [359]: df = pd.read_xml(xml) In [360]: df Out[360]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Read a URL with no options: In [361]: df = pd.read_xml("https://www.w3schools.com/xml/books.xml") In [362]: df Out[362]: category title author year price cover 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 None 1 children Harry Potter J K. Rowling 2005 29.99 None 2 web XQuery Kick Start Vaidyanathan Nagarajan 2003 49.99 None 3 web Learning XML Erik T. Ray 2003 39.95 paperback Read in the content of the “books.xml” file and pass it to read_xml as a string: In [363]: file_path = "books.xml" In [364]: with open(file_path, "w") as f: .....: f.write(xml) .....: In [365]: with open(file_path, "r") as f: .....: df = pd.read_xml(f.read()) .....: In [366]: df Out[366]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Read in the content of the “books.xml” as instance of StringIO or BytesIO and pass it to read_xml: In [367]: with open(file_path, "r") as f: .....: sio = StringIO(f.read()) .....: In [368]: df = pd.read_xml(sio) In [369]: df Out[369]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 In [370]: with open(file_path, "rb") as f: .....: bio = BytesIO(f.read()) .....: In [371]: df = pd.read_xml(bio) In [372]: df Out[372]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Even read XML from AWS S3 buckets such as NIH NCBI PMC Article Datasets providing Biomedical and Life Science Jorurnals: In [373]: df = pd.read_xml( .....: "s3://pmc-oa-opendata/oa_comm/xml/all/PMC1236943.xml", .....: xpath=".//journal-meta", .....: ) .....: In [374]: df Out[374]: journal-id journal-title issn publisher 0 Cardiovasc Ultrasound Cardiovascular Ultrasound 1476-7120 NaN With lxml as default parser, you access the full-featured XML library that extends Python’s ElementTree API. One powerful tool is ability to query nodes selectively or conditionally with more expressive XPath: In [375]: df = pd.read_xml(file_path, xpath="//book[year=2005]") In [376]: df Out[376]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 Specify only elements or only attributes to parse: In [377]: df = pd.read_xml(file_path, elems_only=True) In [378]: df Out[378]: title author year price 0 Everyday Italian Giada De Laurentiis 2005 30.00 1 Harry Potter J K. Rowling 2005 29.99 2 Learning XML Erik T. Ray 2003 39.95 In [379]: df = pd.read_xml(file_path, attrs_only=True) In [380]: df Out[380]: category 0 cooking 1 children 2 web XML documents can have namespaces with prefixes and default namespaces without prefixes both of which are denoted with a special attribute xmlns. In order to parse by node under a namespace context, xpath must reference a prefix. For example, below XML contains a namespace with prefix, doc, and URI at https://example.com. In order to parse doc:row nodes, namespaces must be used. In [381]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <doc:data xmlns:doc="https://example.com"> .....: <doc:row> .....: <doc:shape>square</doc:shape> .....: <doc:degrees>360</doc:degrees> .....: <doc:sides>4.0</doc:sides> .....: </doc:row> .....: <doc:row> .....: <doc:shape>circle</doc:shape> .....: <doc:degrees>360</doc:degrees> .....: <doc:sides/> .....: </doc:row> .....: <doc:row> .....: <doc:shape>triangle</doc:shape> .....: <doc:degrees>180</doc:degrees> .....: <doc:sides>3.0</doc:sides> .....: </doc:row> .....: </doc:data>""" .....: In [382]: df = pd.read_xml(xml, .....: xpath="//doc:row", .....: namespaces={"doc": "https://example.com"}) .....: In [383]: df Out[383]: shape degrees sides 0 square 360 4.0 1 circle 360 NaN 2 triangle 180 3.0 Similarly, an XML document can have a default namespace without prefix. Failing to assign a temporary prefix will return no nodes and raise a ValueError. But assigning any temporary name to correct URI allows parsing by nodes. In [384]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <data xmlns="https://example.com"> .....: <row> .....: <shape>square</shape> .....: <degrees>360</degrees> .....: <sides>4.0</sides> .....: </row> .....: <row> .....: <shape>circle</shape> .....: <degrees>360</degrees> .....: <sides/> .....: </row> .....: <row> .....: <shape>triangle</shape> .....: <degrees>180</degrees> .....: <sides>3.0</sides> .....: </row> .....: </data>""" .....: In [385]: df = pd.read_xml(xml, .....: xpath="//pandas:row", .....: namespaces={"pandas": "https://example.com"}) .....: In [386]: df Out[386]: shape degrees sides 0 square 360 4.0 1 circle 360 NaN 2 triangle 180 3.0 However, if XPath does not reference node names such as default, /*, then namespaces is not required. With lxml as parser, you can flatten nested XML documents with an XSLT script which also can be string/file/URL types. As background, XSLT is a special-purpose language written in a special XML file that can transform original XML documents into other XML, HTML, even text (CSV, JSON, etc.) using an XSLT processor. For example, consider this somewhat nested structure of Chicago “L” Rides where station and rides elements encapsulate data in their own sections. With below XSLT, lxml can transform original nested document into a flatter output (as shown below for demonstration) for easier parse into DataFrame: In [387]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <response> .....: <row> .....: <station id="40850" name="Library"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>864.2</avg_weekday_rides> .....: <avg_saturday_rides>534</avg_saturday_rides> .....: <avg_sunday_holiday_rides>417.2</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: <row> .....: <station id="41700" name="Washington/Wabash"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>2707.4</avg_weekday_rides> .....: <avg_saturday_rides>1909.8</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1438.6</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: <row> .....: <station id="40380" name="Clark/Lake"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>2949.6</avg_weekday_rides> .....: <avg_saturday_rides>1657</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1453.8</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: </response>""" .....: In [388]: xsl = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> .....: <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/> .....: <xsl:strip-space elements="*"/> .....: <xsl:template match="/response"> .....: <xsl:copy> .....: <xsl:apply-templates select="row"/> .....: </xsl:copy> .....: </xsl:template> .....: <xsl:template match="row"> .....: <xsl:copy> .....: <station_id><xsl:value-of select="station/@id"/></station_id> .....: <station_name><xsl:value-of select="station/@name"/></station_name> .....: <xsl:copy-of select="month|rides/*"/> .....: </xsl:copy> .....: </xsl:template> .....: </xsl:stylesheet>""" .....: In [389]: output = """<?xml version='1.0' encoding='utf-8'?> .....: <response> .....: <row> .....: <station_id>40850</station_id> .....: <station_name>Library</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>864.2</avg_weekday_rides> .....: <avg_saturday_rides>534</avg_saturday_rides> .....: <avg_sunday_holiday_rides>417.2</avg_sunday_holiday_rides> .....: </row> .....: <row> .....: <station_id>41700</station_id> .....: <station_name>Washington/Wabash</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>2707.4</avg_weekday_rides> .....: <avg_saturday_rides>1909.8</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1438.6</avg_sunday_holiday_rides> .....: </row> .....: <row> .....: <station_id>40380</station_id> .....: <station_name>Clark/Lake</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>2949.6</avg_weekday_rides> .....: <avg_saturday_rides>1657</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1453.8</avg_sunday_holiday_rides> .....: </row> .....: </response>""" .....: In [390]: df = pd.read_xml(xml, stylesheet=xsl) In [391]: df Out[391]: station_id station_name ... avg_saturday_rides avg_sunday_holiday_rides 0 40850 Library ... 534.0 417.2 1 41700 Washington/Wabash ... 1909.8 1438.6 2 40380 Clark/Lake ... 1657.0 1453.8 [3 rows x 6 columns] For very large XML files that can range in hundreds of megabytes to gigabytes, pandas.read_xml() supports parsing such sizeable files using lxml’s iterparse and etree’s iterparse which are memory-efficient methods to iterate through an XML tree and extract specific elements and attributes. without holding entire tree in memory. New in version 1.5.0. To use this feature, you must pass a physical XML file path into read_xml and use the iterparse argument. Files should not be compressed or point to online sources but stored on local disk. Also, iterparse should be a dictionary where the key is the repeating nodes in document (which become the rows) and the value is a list of any element or attribute that is a descendant (i.e., child, grandchild) of repeating node. Since XPath is not used in this method, descendants do not need to share same relationship with one another. Below shows example of reading in Wikipedia’s very large (12 GB+) latest article data dump. In [1]: df = pd.read_xml( ... "/path/to/downloaded/enwikisource-latest-pages-articles.xml", ... iterparse = {"page": ["title", "ns", "id"]} ... ) ... df Out[2]: title ns id 0 Gettysburg Address 0 21450 1 Main Page 0 42950 2 Declaration by United Nations 0 8435 3 Constitution of the United States of America 0 8435 4 Declaration of Independence (Israel) 0 17858 ... ... ... ... 3578760 Page:Black cat 1897 07 v2 n10.pdf/17 104 219649 3578761 Page:Black cat 1897 07 v2 n10.pdf/43 104 219649 3578762 Page:Black cat 1897 07 v2 n10.pdf/44 104 219649 3578763 The History of Tom Jones, a Foundling/Book IX 0 12084291 3578764 Page:Shakespeare of Stratford (1926) Yale.djvu/91 104 21450 [3578765 rows x 3 columns] Writing XML# New in version 1.3.0. DataFrame objects have an instance method to_xml which renders the contents of the DataFrame as an XML document. Note This method does not support special properties of XML including DTD, CData, XSD schemas, processing instructions, comments, and others. Only namespaces at the root level is supported. However, stylesheet allows design changes after initial output. Let’s look at a few examples. Write an XML without options: In [392]: geom_df = pd.DataFrame( .....: { .....: "shape": ["square", "circle", "triangle"], .....: "degrees": [360, 360, 180], .....: "sides": [4, np.nan, 3], .....: } .....: ) .....: In [393]: print(geom_df.to_xml()) <?xml version='1.0' encoding='utf-8'?> <data> <row> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </row> <row> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </row> <row> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Write an XML with new root and row name: In [394]: print(geom_df.to_xml(root_name="geometry", row_name="objects")) <?xml version='1.0' encoding='utf-8'?> <geometry> <objects> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </objects> <objects> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </objects> <objects> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </objects> </geometry> Write an attribute-centric XML: In [395]: print(geom_df.to_xml(attr_cols=geom_df.columns.tolist())) <?xml version='1.0' encoding='utf-8'?> <data> <row index="0" shape="square" degrees="360" sides="4.0"/> <row index="1" shape="circle" degrees="360"/> <row index="2" shape="triangle" degrees="180" sides="3.0"/> </data> Write a mix of elements and attributes: In [396]: print( .....: geom_df.to_xml( .....: index=False, .....: attr_cols=['shape'], .....: elem_cols=['degrees', 'sides']) .....: ) .....: <?xml version='1.0' encoding='utf-8'?> <data> <row shape="square"> <degrees>360</degrees> <sides>4.0</sides> </row> <row shape="circle"> <degrees>360</degrees> <sides/> </row> <row shape="triangle"> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Any DataFrames with hierarchical columns will be flattened for XML element names with levels delimited by underscores: In [397]: ext_geom_df = pd.DataFrame( .....: { .....: "type": ["polygon", "other", "polygon"], .....: "shape": ["square", "circle", "triangle"], .....: "degrees": [360, 360, 180], .....: "sides": [4, np.nan, 3], .....: } .....: ) .....: In [398]: pvt_df = ext_geom_df.pivot_table(index='shape', .....: columns='type', .....: values=['degrees', 'sides'], .....: aggfunc='sum') .....: In [399]: pvt_df Out[399]: degrees sides type other polygon other polygon shape circle 360.0 NaN 0.0 NaN square NaN 360.0 NaN 4.0 triangle NaN 180.0 NaN 3.0 In [400]: print(pvt_df.to_xml()) <?xml version='1.0' encoding='utf-8'?> <data> <row> <shape>circle</shape> <degrees_other>360.0</degrees_other> <degrees_polygon/> <sides_other>0.0</sides_other> <sides_polygon/> </row> <row> <shape>square</shape> <degrees_other/> <degrees_polygon>360.0</degrees_polygon> <sides_other/> <sides_polygon>4.0</sides_polygon> </row> <row> <shape>triangle</shape> <degrees_other/> <degrees_polygon>180.0</degrees_polygon> <sides_other/> <sides_polygon>3.0</sides_polygon> </row> </data> Write an XML with default namespace: In [401]: print(geom_df.to_xml(namespaces={"": "https://example.com"})) <?xml version='1.0' encoding='utf-8'?> <data xmlns="https://example.com"> <row> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </row> <row> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </row> <row> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Write an XML with namespace prefix: In [402]: print( .....: geom_df.to_xml(namespaces={"doc": "https://example.com"}, .....: prefix="doc") .....: ) .....: <?xml version='1.0' encoding='utf-8'?> <doc:data xmlns:doc="https://example.com"> <doc:row> <doc:index>0</doc:index> <doc:shape>square</doc:shape> <doc:degrees>360</doc:degrees> <doc:sides>4.0</doc:sides> </doc:row> <doc:row> <doc:index>1</doc:index> <doc:shape>circle</doc:shape> <doc:degrees>360</doc:degrees> <doc:sides/> </doc:row> <doc:row> <doc:index>2</doc:index> <doc:shape>triangle</doc:shape> <doc:degrees>180</doc:degrees> <doc:sides>3.0</doc:sides> </doc:row> </doc:data> Write an XML without declaration or pretty print: In [403]: print( .....: geom_df.to_xml(xml_declaration=False, .....: pretty_print=False) .....: ) .....: <data><row><index>0</index><shape>square</shape><degrees>360</degrees><sides>4.0</sides></row><row><index>1</index><shape>circle</shape><degrees>360</degrees><sides/></row><row><index>2</index><shape>triangle</shape><degrees>180</degrees><sides>3.0</sides></row></data> Write an XML and transform with stylesheet: In [404]: xsl = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> .....: <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/> .....: <xsl:strip-space elements="*"/> .....: <xsl:template match="/data"> .....: <geometry> .....: <xsl:apply-templates select="row"/> .....: </geometry> .....: </xsl:template> .....: <xsl:template match="row"> .....: <object index="{index}"> .....: <xsl:if test="shape!='circle'"> .....: <xsl:attribute name="type">polygon</xsl:attribute> .....: </xsl:if> .....: <xsl:copy-of select="shape"/> .....: <property> .....: <xsl:copy-of select="degrees|sides"/> .....: </property> .....: </object> .....: </xsl:template> .....: </xsl:stylesheet>""" .....: In [405]: print(geom_df.to_xml(stylesheet=xsl)) <?xml version="1.0"?> <geometry> <object index="0" type="polygon"> <shape>square</shape> <property> <degrees>360</degrees> <sides>4.0</sides> </property> </object> <object index="1"> <shape>circle</shape> <property> <degrees>360</degrees> <sides/> </property> </object> <object index="2" type="polygon"> <shape>triangle</shape> <property> <degrees>180</degrees> <sides>3.0</sides> </property> </object> </geometry> XML Final Notes# All XML documents adhere to W3C specifications. Both etree and lxml parsers will fail to parse any markup document that is not well-formed or follows XML syntax rules. Do be aware HTML is not an XML document unless it follows XHTML specs. However, other popular markup types including KML, XAML, RSS, MusicML, MathML are compliant XML schemas. For above reason, if your application builds XML prior to pandas operations, use appropriate DOM libraries like etree and lxml to build the necessary document and not by string concatenation or regex adjustments. Always remember XML is a special text file with markup rules. With very large XML files (several hundred MBs to GBs), XPath and XSLT can become memory-intensive operations. Be sure to have enough available RAM for reading and writing to large XML files (roughly about 5 times the size of text). Because XSLT is a programming language, use it with caution since such scripts can pose a security risk in your environment and can run large or infinite recursive operations. Always test scripts on small fragments before full run. The etree parser supports all functionality of both read_xml and to_xml except for complex XPath and any XSLT. Though limited in features, etree is still a reliable and capable parser and tree builder. Its performance may trail lxml to a certain degree for larger files but relatively unnoticeable on small to medium size files. Excel files# The read_excel() method can read Excel 2007+ (.xlsx) files using the openpyxl Python module. Excel 2003 (.xls) files can be read using xlrd. Binary Excel (.xlsb) files can be read using pyxlsb. The to_excel() instance method is used for saving a DataFrame to Excel. Generally the semantics are similar to working with csv data. See the cookbook for some advanced strategies. Warning The xlwt package for writing old-style .xls excel files is no longer maintained. The xlrd package is now only for reading old-style .xls files. Before pandas 1.3.0, the default argument engine=None to read_excel() would result in using the xlrd engine in many cases, including new Excel 2007+ (.xlsx) files. pandas will now default to using the openpyxl engine. It is strongly encouraged to install openpyxl to read Excel 2007+ (.xlsx) files. Please do not report issues when using ``xlrd`` to read ``.xlsx`` files. This is no longer supported, switch to using openpyxl instead. Attempting to use the xlwt engine will raise a FutureWarning unless the option io.excel.xls.writer is set to "xlwt". While this option is now deprecated and will also raise a FutureWarning, it can be globally set and the warning suppressed. Users are recommended to write .xlsx files using the openpyxl engine instead. Reading Excel files# In the most basic use-case, read_excel takes a path to an Excel file, and the sheet_name indicating which sheet to parse. # Returns a DataFrame pd.read_excel("path_to_file.xls", sheet_name="Sheet1") ExcelFile class# To facilitate working with multiple sheets from the same file, the ExcelFile class can be used to wrap the file and can be passed into read_excel There will be a performance benefit for reading multiple sheets as the file is read into memory only once. xlsx = pd.ExcelFile("path_to_file.xls") df = pd.read_excel(xlsx, "Sheet1") The ExcelFile class can also be used as a context manager. with pd.ExcelFile("path_to_file.xls") as xls: df1 = pd.read_excel(xls, "Sheet1") df2 = pd.read_excel(xls, "Sheet2") The sheet_names property will generate a list of the sheet names in the file. The primary use-case for an ExcelFile is parsing multiple sheets with different parameters: data = {} # For when Sheet1's format differs from Sheet2 with pd.ExcelFile("path_to_file.xls") as xls: data["Sheet1"] = pd.read_excel(xls, "Sheet1", index_col=None, na_values=["NA"]) data["Sheet2"] = pd.read_excel(xls, "Sheet2", index_col=1) Note that if the same parsing parameters are used for all sheets, a list of sheet names can simply be passed to read_excel with no loss in performance. # using the ExcelFile class data = {} with pd.ExcelFile("path_to_file.xls") as xls: data["Sheet1"] = pd.read_excel(xls, "Sheet1", index_col=None, na_values=["NA"]) data["Sheet2"] = pd.read_excel(xls, "Sheet2", index_col=None, na_values=["NA"]) # equivalent using the read_excel function data = pd.read_excel( "path_to_file.xls", ["Sheet1", "Sheet2"], index_col=None, na_values=["NA"] ) ExcelFile can also be called with a xlrd.book.Book object as a parameter. This allows the user to control how the excel file is read. For example, sheets can be loaded on demand by calling xlrd.open_workbook() with on_demand=True. import xlrd xlrd_book = xlrd.open_workbook("path_to_file.xls", on_demand=True) with pd.ExcelFile(xlrd_book) as xls: df1 = pd.read_excel(xls, "Sheet1") df2 = pd.read_excel(xls, "Sheet2") Specifying sheets# Note The second argument is sheet_name, not to be confused with ExcelFile.sheet_names. Note An ExcelFile’s attribute sheet_names provides access to a list of sheets. The arguments sheet_name allows specifying the sheet or sheets to read. The default value for sheet_name is 0, indicating to read the first sheet Pass a string to refer to the name of a particular sheet in the workbook. Pass an integer to refer to the index of a sheet. Indices follow Python convention, beginning at 0. Pass a list of either strings or integers, to return a dictionary of specified sheets. Pass a None to return a dictionary of all available sheets. # Returns a DataFrame pd.read_excel("path_to_file.xls", "Sheet1", index_col=None, na_values=["NA"]) Using the sheet index: # Returns a DataFrame pd.read_excel("path_to_file.xls", 0, index_col=None, na_values=["NA"]) Using all default values: # Returns a DataFrame pd.read_excel("path_to_file.xls") Using None to get all sheets: # Returns a dictionary of DataFrames pd.read_excel("path_to_file.xls", sheet_name=None) Using a list to get multiple sheets: # Returns the 1st and 4th sheet, as a dictionary of DataFrames. pd.read_excel("path_to_file.xls", sheet_name=["Sheet1", 3]) read_excel can read more than one sheet, by setting sheet_name to either a list of sheet names, a list of sheet positions, or None to read all sheets. Sheets can be specified by sheet index or sheet name, using an integer or string, respectively. Reading a MultiIndex# read_excel can read a MultiIndex index, by passing a list of columns to index_col and a MultiIndex column by passing a list of rows to header. If either the index or columns have serialized level names those will be read in as well by specifying the rows/columns that make up the levels. For example, to read in a MultiIndex index without names: In [406]: df = pd.DataFrame( .....: {"a": [1, 2, 3, 4], "b": [5, 6, 7, 8]}, .....: index=pd.MultiIndex.from_product([["a", "b"], ["c", "d"]]), .....: ) .....: In [407]: df.to_excel("path_to_file.xlsx") In [408]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1]) In [409]: df Out[409]: a b a c 1 5 d 2 6 b c 3 7 d 4 8 If the index has level names, they will parsed as well, using the same parameters. In [410]: df.index = df.index.set_names(["lvl1", "lvl2"]) In [411]: df.to_excel("path_to_file.xlsx") In [412]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1]) In [413]: df Out[413]: a b lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 If the source file has both MultiIndex index and columns, lists specifying each should be passed to index_col and header: In [414]: df.columns = pd.MultiIndex.from_product([["a"], ["b", "d"]], names=["c1", "c2"]) In [415]: df.to_excel("path_to_file.xlsx") In [416]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1], header=[0, 1]) In [417]: df Out[417]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 Missing values in columns specified in index_col will be forward filled to allow roundtripping with to_excel for merged_cells=True. To avoid forward filling the missing values use set_index after reading the data instead of index_col. Parsing specific columns# It is often the case that users will insert columns to do temporary computations in Excel and you may not want to read in those columns. read_excel takes a usecols keyword to allow you to specify a subset of columns to parse. Changed in version 1.0.0. Passing in an integer for usecols will no longer work. Please pass in a list of ints from 0 to usecols inclusive instead. You can specify a comma-delimited set of Excel columns and ranges as a string: pd.read_excel("path_to_file.xls", "Sheet1", usecols="A,C:E") If usecols is a list of integers, then it is assumed to be the file column indices to be parsed. pd.read_excel("path_to_file.xls", "Sheet1", usecols=[0, 2, 3]) Element order is ignored, so usecols=[0, 1] is the same as [1, 0]. If usecols is a list of strings, it is assumed that each string corresponds to a column name provided either by the user in names or inferred from the document header row(s). Those strings define which columns will be parsed: pd.read_excel("path_to_file.xls", "Sheet1", usecols=["foo", "bar"]) Element order is ignored, so usecols=['baz', 'joe'] is the same as ['joe', 'baz']. If usecols is callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True. pd.read_excel("path_to_file.xls", "Sheet1", usecols=lambda x: x.isalpha()) Parsing dates# Datetime-like values are normally automatically converted to the appropriate dtype when reading the excel file. But if you have a column of strings that look like dates (but are not actually formatted as dates in excel), you can use the parse_dates keyword to parse those strings to datetimes: pd.read_excel("path_to_file.xls", "Sheet1", parse_dates=["date_strings"]) Cell converters# It is possible to transform the contents of Excel cells via the converters option. For instance, to convert a column to boolean: pd.read_excel("path_to_file.xls", "Sheet1", converters={"MyBools": bool}) This options handles missing values and treats exceptions in the converters as missing data. Transformations are applied cell by cell rather than to the column as a whole, so the array dtype is not guaranteed. For instance, a column of integers with missing values cannot be transformed to an array with integer dtype, because NaN is strictly a float. You can manually mask missing data to recover integer dtype: def cfun(x): return int(x) if x else -1 pd.read_excel("path_to_file.xls", "Sheet1", converters={"MyInts": cfun}) Dtype specifications# As an alternative to converters, the type for an entire column can be specified using the dtype keyword, which takes a dictionary mapping column names to types. To interpret data with no type inference, use the type str or object. pd.read_excel("path_to_file.xls", dtype={"MyInts": "int64", "MyText": str}) Writing Excel files# Writing Excel files to disk# To write a DataFrame object to a sheet of an Excel file, you can use the to_excel instance method. The arguments are largely the same as to_csv described above, the first argument being the name of the excel file, and the optional second argument the name of the sheet to which the DataFrame should be written. For example: df.to_excel("path_to_file.xlsx", sheet_name="Sheet1") Files with a .xls extension will be written using xlwt and those with a .xlsx extension will be written using xlsxwriter (if available) or openpyxl. The DataFrame will be written in a way that tries to mimic the REPL output. The index_label will be placed in the second row instead of the first. You can place it in the first row by setting the merge_cells option in to_excel() to False: df.to_excel("path_to_file.xlsx", index_label="label", merge_cells=False) In order to write separate DataFrames to separate sheets in a single Excel file, one can pass an ExcelWriter. with pd.ExcelWriter("path_to_file.xlsx") as writer: df1.to_excel(writer, sheet_name="Sheet1") df2.to_excel(writer, sheet_name="Sheet2") Writing Excel files to memory# pandas supports writing Excel files to buffer-like objects such as StringIO or BytesIO using ExcelWriter. from io import BytesIO bio = BytesIO() # By setting the 'engine' in the ExcelWriter constructor. writer = pd.ExcelWriter(bio, engine="xlsxwriter") df.to_excel(writer, sheet_name="Sheet1") # Save the workbook writer.save() # Seek to the beginning and read to copy the workbook to a variable in memory bio.seek(0) workbook = bio.read() Note engine is optional but recommended. Setting the engine determines the version of workbook produced. Setting engine='xlrd' will produce an Excel 2003-format workbook (xls). Using either 'openpyxl' or 'xlsxwriter' will produce an Excel 2007-format workbook (xlsx). If omitted, an Excel 2007-formatted workbook is produced. Excel writer engines# Deprecated since version 1.2.0: As the xlwt package is no longer maintained, the xlwt engine will be removed from a future version of pandas. This is the only engine in pandas that supports writing to .xls files. pandas chooses an Excel writer via two methods: the engine keyword argument the filename extension (via the default specified in config options) By default, pandas uses the XlsxWriter for .xlsx, openpyxl for .xlsm, and xlwt for .xls files. If you have multiple engines installed, you can set the default engine through setting the config options io.excel.xlsx.writer and io.excel.xls.writer. pandas will fall back on openpyxl for .xlsx files if Xlsxwriter is not available. To specify which writer you want to use, you can pass an engine keyword argument to to_excel and to ExcelWriter. The built-in engines are: openpyxl: version 2.4 or higher is required xlsxwriter xlwt # By setting the 'engine' in the DataFrame 'to_excel()' methods. df.to_excel("path_to_file.xlsx", sheet_name="Sheet1", engine="xlsxwriter") # By setting the 'engine' in the ExcelWriter constructor. writer = pd.ExcelWriter("path_to_file.xlsx", engine="xlsxwriter") # Or via pandas configuration. from pandas import options # noqa: E402 options.io.excel.xlsx.writer = "xlsxwriter" df.to_excel("path_to_file.xlsx", sheet_name="Sheet1") Style and formatting# The look and feel of Excel worksheets created from pandas can be modified using the following parameters on the DataFrame’s to_excel method. float_format : Format string for floating point numbers (default None). freeze_panes : A tuple of two integers representing the bottommost row and rightmost column to freeze. Each of these parameters is one-based, so (1, 1) will freeze the first row and first column (default None). Using the Xlsxwriter engine provides many options for controlling the format of an Excel worksheet created with the to_excel method. Excellent examples can be found in the Xlsxwriter documentation here: https://xlsxwriter.readthedocs.io/working_with_pandas.html OpenDocument Spreadsheets# New in version 0.25. The read_excel() method can also read OpenDocument spreadsheets using the odfpy module. The semantics and features for reading OpenDocument spreadsheets match what can be done for Excel files using engine='odf'. # Returns a DataFrame pd.read_excel("path_to_file.ods", engine="odf") Note Currently pandas only supports reading OpenDocument spreadsheets. Writing is not implemented. Binary Excel (.xlsb) files# New in version 1.0.0. The read_excel() method can also read binary Excel files using the pyxlsb module. The semantics and features for reading binary Excel files mostly match what can be done for Excel files using engine='pyxlsb'. pyxlsb does not recognize datetime types in files and will return floats instead. # Returns a DataFrame pd.read_excel("path_to_file.xlsb", engine="pyxlsb") Note Currently pandas only supports reading binary Excel files. Writing is not implemented. Clipboard# A handy way to grab data is to use the read_clipboard() method, which takes the contents of the clipboard buffer and passes them to the read_csv method. For instance, you can copy the following text to the clipboard (CTRL-C on many operating systems): A B C x 1 4 p y 2 5 q z 3 6 r And then import the data directly to a DataFrame by calling: >>> clipdf = pd.read_clipboard() >>> clipdf A B C x 1 4 p y 2 5 q z 3 6 r The to_clipboard method can be used to write the contents of a DataFrame to the clipboard. Following which you can paste the clipboard contents into other applications (CTRL-V on many operating systems). Here we illustrate writing a DataFrame into clipboard and reading it back. >>> df = pd.DataFrame( ... {"A": [1, 2, 3], "B": [4, 5, 6], "C": ["p", "q", "r"]}, index=["x", "y", "z"] ... ) >>> df A B C x 1 4 p y 2 5 q z 3 6 r >>> df.to_clipboard() >>> pd.read_clipboard() A B C x 1 4 p y 2 5 q z 3 6 r We can see that we got the same content back, which we had earlier written to the clipboard. Note You may need to install xclip or xsel (with PyQt5, PyQt4 or qtpy) on Linux to use these methods. Pickling# All pandas objects are equipped with to_pickle methods which use Python’s cPickle module to save data structures to disk using the pickle format. In [418]: df Out[418]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 In [419]: df.to_pickle("foo.pkl") The read_pickle function in the pandas namespace can be used to load any pickled pandas object (or any other pickled object) from file: In [420]: pd.read_pickle("foo.pkl") Out[420]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 Warning Loading pickled data received from untrusted sources can be unsafe. See: https://docs.python.org/3/library/pickle.html Warning read_pickle() is only guaranteed backwards compatible back to pandas version 0.20.3 Compressed pickle files# read_pickle(), DataFrame.to_pickle() and Series.to_pickle() can read and write compressed pickle files. The compression types of gzip, bz2, xz, zstd are supported for reading and writing. The zip file format only supports reading and must contain only one data file to be read. The compression type can be an explicit parameter or be inferred from the file extension. If ‘infer’, then use gzip, bz2, zip, xz, zstd if filename ends in '.gz', '.bz2', '.zip', '.xz', or '.zst', respectively. The compression parameter can also be a dict in order to pass options to the compression protocol. It must have a 'method' key set to the name of the compression protocol, which must be one of {'zip', 'gzip', 'bz2', 'xz', 'zstd'}. All other key-value pairs are passed to the underlying compression library. In [421]: df = pd.DataFrame( .....: { .....: "A": np.random.randn(1000), .....: "B": "foo", .....: "C": pd.date_range("20130101", periods=1000, freq="s"), .....: } .....: ) .....: In [422]: df Out[422]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] Using an explicit compression type: In [423]: df.to_pickle("data.pkl.compress", compression="gzip") In [424]: rt = pd.read_pickle("data.pkl.compress", compression="gzip") In [425]: rt Out[425]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] Inferring compression type from the extension: In [426]: df.to_pickle("data.pkl.xz", compression="infer") In [427]: rt = pd.read_pickle("data.pkl.xz", compression="infer") In [428]: rt Out[428]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] The default is to ‘infer’: In [429]: df.to_pickle("data.pkl.gz") In [430]: rt = pd.read_pickle("data.pkl.gz") In [431]: rt Out[431]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] In [432]: df["A"].to_pickle("s1.pkl.bz2") In [433]: rt = pd.read_pickle("s1.pkl.bz2") In [434]: rt Out[434]: 0 -0.828876 1 -0.110383 2 2.357598 3 -1.620073 4 0.440903 ... 995 -1.177365 996 1.236988 997 0.743946 998 -0.533097 999 -0.140850 Name: A, Length: 1000, dtype: float64 Passing options to the compression protocol in order to speed up compression: In [435]: df.to_pickle("data.pkl.gz", compression={"method": "gzip", "compresslevel": 1}) msgpack# pandas support for msgpack has been removed in version 1.0.0. It is recommended to use pickle instead. Alternatively, you can also the Arrow IPC serialization format for on-the-wire transmission of pandas objects. For documentation on pyarrow, see here. HDF5 (PyTables)# HDFStore is a dict-like object which reads and writes pandas using the high performance HDF5 format using the excellent PyTables library. See the cookbook for some advanced strategies Warning pandas uses PyTables for reading and writing HDF5 files, which allows serializing object-dtype data with pickle. Loading pickled data received from untrusted sources can be unsafe. See: https://docs.python.org/3/library/pickle.html for more. In [436]: store = pd.HDFStore("store.h5") In [437]: print(store) <class 'pandas.io.pytables.HDFStore'> File path: store.h5 Objects can be written to the file just like adding key-value pairs to a dict: In [438]: index = pd.date_range("1/1/2000", periods=8) In [439]: s = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"]) In [440]: df = pd.DataFrame(np.random.randn(8, 3), index=index, columns=["A", "B", "C"]) # store.put('s', s) is an equivalent method In [441]: store["s"] = s In [442]: store["df"] = df In [443]: store Out[443]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 In a current or later Python session, you can retrieve stored objects: # store.get('df') is an equivalent method In [444]: store["df"] Out[444]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 # dotted (attribute) access provides get as well In [445]: store.df Out[445]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Deletion of the object specified by the key: # store.remove('df') is an equivalent method In [446]: del store["df"] In [447]: store Out[447]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 Closing a Store and using a context manager: In [448]: store.close() In [449]: store Out[449]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 In [450]: store.is_open Out[450]: False # Working with, and automatically closing the store using a context manager In [451]: with pd.HDFStore("store.h5") as store: .....: store.keys() .....: Read/write API# HDFStore supports a top-level API using read_hdf for reading and to_hdf for writing, similar to how read_csv and to_csv work. In [452]: df_tl = pd.DataFrame({"A": list(range(5)), "B": list(range(5))}) In [453]: df_tl.to_hdf("store_tl.h5", "table", append=True) In [454]: pd.read_hdf("store_tl.h5", "table", where=["index>2"]) Out[454]: A B 3 3 3 4 4 4 HDFStore will by default not drop rows that are all missing. This behavior can be changed by setting dropna=True. In [455]: df_with_missing = pd.DataFrame( .....: { .....: "col1": [0, np.nan, 2], .....: "col2": [1, np.nan, np.nan], .....: } .....: ) .....: In [456]: df_with_missing Out[456]: col1 col2 0 0.0 1.0 1 NaN NaN 2 2.0 NaN In [457]: df_with_missing.to_hdf("file.h5", "df_with_missing", format="table", mode="w") In [458]: pd.read_hdf("file.h5", "df_with_missing") Out[458]: col1 col2 0 0.0 1.0 1 NaN NaN 2 2.0 NaN In [459]: df_with_missing.to_hdf( .....: "file.h5", "df_with_missing", format="table", mode="w", dropna=True .....: ) .....: In [460]: pd.read_hdf("file.h5", "df_with_missing") Out[460]: col1 col2 0 0.0 1.0 2 2.0 NaN Fixed format# The examples above show storing using put, which write the HDF5 to PyTables in a fixed array format, called the fixed format. These types of stores are not appendable once written (though you can simply remove them and rewrite). Nor are they queryable; they must be retrieved in their entirety. They also do not support dataframes with non-unique column names. The fixed format stores offer very fast writing and slightly faster reading than table stores. This format is specified by default when using put or to_hdf or by format='fixed' or format='f'. Warning A fixed format will raise a TypeError if you try to retrieve using a where: >>> pd.DataFrame(np.random.randn(10, 2)).to_hdf("test_fixed.h5", "df") >>> pd.read_hdf("test_fixed.h5", "df", where="index>5") TypeError: cannot pass a where specification when reading a fixed format. this store must be selected in its entirety Table format# HDFStore supports another PyTables format on disk, the table format. Conceptually a table is shaped very much like a DataFrame, with rows and columns. A table may be appended to in the same or other sessions. In addition, delete and query type operations are supported. This format is specified by format='table' or format='t' to append or put or to_hdf. This format can be set as an option as well pd.set_option('io.hdf.default_format','table') to enable put/append/to_hdf to by default store in the table format. In [461]: store = pd.HDFStore("store.h5") In [462]: df1 = df[0:4] In [463]: df2 = df[4:] # append data (creates a table automatically) In [464]: store.append("df", df1) In [465]: store.append("df", df2) In [466]: store Out[466]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # select the entire object In [467]: store.select("df") Out[467]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 # the type of stored data In [468]: store.root.df._v_attrs.pandas_type Out[468]: 'frame_table' Note You can also create a table by passing format='table' or format='t' to a put operation. Hierarchical keys# Keys to a store can be specified as a string. These can be in a hierarchical path-name like format (e.g. foo/bar/bah), which will generate a hierarchy of sub-stores (or Groups in PyTables parlance). Keys can be specified without the leading ‘/’ and are always absolute (e.g. ‘foo’ refers to ‘/foo’). Removal operations can remove everything in the sub-store and below, so be careful. In [469]: store.put("foo/bar/bah", df) In [470]: store.append("food/orange", df) In [471]: store.append("food/apple", df) In [472]: store Out[472]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # a list of keys are returned In [473]: store.keys() Out[473]: ['/df', '/food/apple', '/food/orange', '/foo/bar/bah'] # remove all nodes under this level In [474]: store.remove("food") In [475]: store Out[475]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 You can walk through the group hierarchy using the walk method which will yield a tuple for each group key along with the relative keys of its contents. In [476]: for (path, subgroups, subkeys) in store.walk(): .....: for subgroup in subgroups: .....: print("GROUP: {}/{}".format(path, subgroup)) .....: for subkey in subkeys: .....: key = "/".join([path, subkey]) .....: print("KEY: {}".format(key)) .....: print(store.get(key)) .....: GROUP: /foo KEY: /df A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 GROUP: /foo/bar KEY: /foo/bar/bah A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Warning Hierarchical keys cannot be retrieved as dotted (attribute) access as described above for items stored under the root node. In [8]: store.foo.bar.bah AttributeError: 'HDFStore' object has no attribute 'foo' # you can directly access the actual PyTables node but using the root node In [9]: store.root.foo.bar.bah Out[9]: /foo/bar/bah (Group) '' children := ['block0_items' (Array), 'block0_values' (Array), 'axis0' (Array), 'axis1' (Array)] Instead, use explicit string based keys: In [477]: store["foo/bar/bah"] Out[477]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Storing types# Storing mixed types in a table# Storing mixed-dtype data is supported. Strings are stored as a fixed-width using the maximum size of the appended column. Subsequent attempts at appending longer strings will raise a ValueError. Passing min_itemsize={`values`: size} as a parameter to append will set a larger minimum for the string columns. Storing floats, strings, ints, bools, datetime64 are currently supported. For string columns, passing nan_rep = 'nan' to append will change the default nan representation on disk (which converts to/from np.nan), this defaults to nan. In [478]: df_mixed = pd.DataFrame( .....: { .....: "A": np.random.randn(8), .....: "B": np.random.randn(8), .....: "C": np.array(np.random.randn(8), dtype="float32"), .....: "string": "string", .....: "int": 1, .....: "bool": True, .....: "datetime64": pd.Timestamp("20010102"), .....: }, .....: index=list(range(8)), .....: ) .....: In [479]: df_mixed.loc[df_mixed.index[3:5], ["A", "B", "string", "datetime64"]] = np.nan In [480]: store.append("df_mixed", df_mixed, min_itemsize={"values": 50}) In [481]: df_mixed1 = store.select("df_mixed") In [482]: df_mixed1 Out[482]: A B C string int bool datetime64 0 1.778161 -0.898283 -0.263043 string 1 True 2001-01-02 1 -0.913867 -0.218499 -0.639244 string 1 True 2001-01-02 2 -0.030004 1.408028 -0.866305 string 1 True 2001-01-02 3 NaN NaN -0.225250 NaN 1 True NaT 4 NaN NaN -0.890978 NaN 1 True NaT 5 0.081323 0.520995 -0.553839 string 1 True 2001-01-02 6 -0.268494 0.620028 -2.762875 string 1 True 2001-01-02 7 0.168016 0.159416 -1.244763 string 1 True 2001-01-02 In [483]: df_mixed1.dtypes.value_counts() Out[483]: float64 2 float32 1 object 1 int64 1 bool 1 datetime64[ns] 1 dtype: int64 # we have provided a minimum string column size In [484]: store.root.df_mixed.table Out[484]: /df_mixed/table (Table(8,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(2,), dflt=0.0, pos=1), "values_block_1": Float32Col(shape=(1,), dflt=0.0, pos=2), "values_block_2": StringCol(itemsize=50, shape=(1,), dflt=b'', pos=3), "values_block_3": Int64Col(shape=(1,), dflt=0, pos=4), "values_block_4": BoolCol(shape=(1,), dflt=False, pos=5), "values_block_5": Int64Col(shape=(1,), dflt=0, pos=6)} byteorder := 'little' chunkshape := (689,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False} Storing MultiIndex DataFrames# Storing MultiIndex DataFrames as tables is very similar to storing/selecting from homogeneous index DataFrames. In [485]: index = pd.MultiIndex( .....: levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], .....: codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], .....: names=["foo", "bar"], .....: ) .....: In [486]: df_mi = pd.DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"]) In [487]: df_mi Out[487]: A B C foo bar foo one -1.280289 0.692545 -0.536722 two 1.005707 0.296917 0.139796 three -1.083889 0.811865 1.648435 bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 baz two 0.183573 0.145277 0.308146 three -1.043530 -0.708145 1.430905 qux one -0.850136 0.813949 1.508891 two -1.556154 0.187597 1.176488 three -1.246093 -0.002726 -0.444249 In [488]: store.append("df_mi", df_mi) In [489]: store.select("df_mi") Out[489]: A B C foo bar foo one -1.280289 0.692545 -0.536722 two 1.005707 0.296917 0.139796 three -1.083889 0.811865 1.648435 bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 baz two 0.183573 0.145277 0.308146 three -1.043530 -0.708145 1.430905 qux one -0.850136 0.813949 1.508891 two -1.556154 0.187597 1.176488 three -1.246093 -0.002726 -0.444249 # the levels are automatically included as data columns In [490]: store.select("df_mi", "foo=bar") Out[490]: A B C foo bar bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 Note The index keyword is reserved and cannot be use as a level name. Querying# Querying a table# select and delete operations have an optional criterion that can be specified to select/delete only a subset of the data. This allows one to have a very large on-disk table and retrieve only a portion of the data. A query is specified using the Term class under the hood, as a boolean expression. index and columns are supported indexers of DataFrames. if data_columns are specified, these can be used as additional indexers. level name in a MultiIndex, with default name level_0, level_1, … if not provided. Valid comparison operators are: =, ==, !=, >, >=, <, <= Valid boolean expressions are combined with: | : or & : and ( and ) : for grouping These rules are similar to how boolean expressions are used in pandas for indexing. Note = will be automatically expanded to the comparison operator == ~ is the not operator, but can only be used in very limited circumstances If a list/tuple of expressions is passed they will be combined via & The following are valid expressions: 'index >= date' "columns = ['A', 'D']" "columns in ['A', 'D']" 'columns = A' 'columns == A' "~(columns = ['A', 'B'])" 'index > df.index[3] & string = "bar"' '(index > df.index[3] & index <= df.index[6]) | string = "bar"' "ts >= Timestamp('2012-02-01')" "major_axis>=20130101" The indexers are on the left-hand side of the sub-expression: columns, major_axis, ts The right-hand side of the sub-expression (after a comparison operator) can be: functions that will be evaluated, e.g. Timestamp('2012-02-01') strings, e.g. "bar" date-like, e.g. 20130101, or "20130101" lists, e.g. "['A', 'B']" variables that are defined in the local names space, e.g. date Note Passing a string to a query by interpolating it into the query expression is not recommended. Simply assign the string of interest to a variable and use that variable in an expression. For example, do this string = "HolyMoly'" store.select("df", "index == string") instead of this string = "HolyMoly'" store.select('df', f'index == {string}') The latter will not work and will raise a SyntaxError.Note that there’s a single quote followed by a double quote in the string variable. If you must interpolate, use the '%r' format specifier store.select("df", "index == %r" % string) which will quote string. Here are some examples: In [491]: dfq = pd.DataFrame( .....: np.random.randn(10, 4), .....: columns=list("ABCD"), .....: index=pd.date_range("20130101", periods=10), .....: ) .....: In [492]: store.append("dfq", dfq, format="table", data_columns=True) Use boolean expressions, with in-line function evaluation. In [493]: store.select("dfq", "index>pd.Timestamp('20130104') & columns=['A', 'B']") Out[493]: A B 2013-01-05 1.366810 1.073372 2013-01-06 2.119746 -2.628174 2013-01-07 0.337920 -0.634027 2013-01-08 1.053434 1.109090 2013-01-09 -0.772942 -0.269415 2013-01-10 0.048562 -0.285920 Use inline column reference. In [494]: store.select("dfq", where="A>0 or C>0") Out[494]: A B C D 2013-01-01 0.856838 1.491776 0.001283 0.701816 2013-01-02 -1.097917 0.102588 0.661740 0.443531 2013-01-03 0.559313 -0.459055 -1.222598 -0.455304 2013-01-05 1.366810 1.073372 -0.994957 0.755314 2013-01-06 2.119746 -2.628174 -0.089460 -0.133636 2013-01-07 0.337920 -0.634027 0.421107 0.604303 2013-01-08 1.053434 1.109090 -0.367891 -0.846206 2013-01-10 0.048562 -0.285920 1.334100 0.194462 The columns keyword can be supplied to select a list of columns to be returned, this is equivalent to passing a 'columns=list_of_columns_to_filter': In [495]: store.select("df", "columns=['A', 'B']") Out[495]: A B 2000-01-01 -0.398501 -0.677311 2000-01-02 -1.167564 -0.593353 2000-01-03 -0.131959 0.089012 2000-01-04 0.169405 -1.358046 2000-01-05 0.492195 0.076693 2000-01-06 -0.285283 -1.210529 2000-01-07 0.941577 -0.342447 2000-01-08 0.052607 2.093214 start and stop parameters can be specified to limit the total search space. These are in terms of the total number of rows in a table. Note select will raise a ValueError if the query expression has an unknown variable reference. Usually this means that you are trying to select on a column that is not a data_column. select will raise a SyntaxError if the query expression is not valid. Query timedelta64[ns]# You can store and query using the timedelta64[ns] type. Terms can be specified in the format: <float>(<unit>), where float may be signed (and fractional), and unit can be D,s,ms,us,ns for the timedelta. Here’s an example: In [496]: from datetime import timedelta In [497]: dftd = pd.DataFrame( .....: { .....: "A": pd.Timestamp("20130101"), .....: "B": [ .....: pd.Timestamp("20130101") + timedelta(days=i, seconds=10) .....: for i in range(10) .....: ], .....: } .....: ) .....: In [498]: dftd["C"] = dftd["A"] - dftd["B"] In [499]: dftd Out[499]: A B C 0 2013-01-01 2013-01-01 00:00:10 -1 days +23:59:50 1 2013-01-01 2013-01-02 00:00:10 -2 days +23:59:50 2 2013-01-01 2013-01-03 00:00:10 -3 days +23:59:50 3 2013-01-01 2013-01-04 00:00:10 -4 days +23:59:50 4 2013-01-01 2013-01-05 00:00:10 -5 days +23:59:50 5 2013-01-01 2013-01-06 00:00:10 -6 days +23:59:50 6 2013-01-01 2013-01-07 00:00:10 -7 days +23:59:50 7 2013-01-01 2013-01-08 00:00:10 -8 days +23:59:50 8 2013-01-01 2013-01-09 00:00:10 -9 days +23:59:50 9 2013-01-01 2013-01-10 00:00:10 -10 days +23:59:50 In [500]: store.append("dftd", dftd, data_columns=True) In [501]: store.select("dftd", "C<'-3.5D'") Out[501]: A B C 4 2013-01-01 2013-01-05 00:00:10 -5 days +23:59:50 5 2013-01-01 2013-01-06 00:00:10 -6 days +23:59:50 6 2013-01-01 2013-01-07 00:00:10 -7 days +23:59:50 7 2013-01-01 2013-01-08 00:00:10 -8 days +23:59:50 8 2013-01-01 2013-01-09 00:00:10 -9 days +23:59:50 9 2013-01-01 2013-01-10 00:00:10 -10 days +23:59:50 Query MultiIndex# Selecting from a MultiIndex can be achieved by using the name of the level. In [502]: df_mi.index.names Out[502]: FrozenList(['foo', 'bar']) In [503]: store.select("df_mi", "foo=baz and bar=two") Out[503]: A B C foo bar baz two 0.183573 0.145277 0.308146 If the MultiIndex levels names are None, the levels are automatically made available via the level_n keyword with n the level of the MultiIndex you want to select from. In [504]: index = pd.MultiIndex( .....: levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], .....: codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], .....: ) .....: In [505]: df_mi_2 = pd.DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"]) In [506]: df_mi_2 Out[506]: A B C foo one -0.646538 1.210676 -0.315409 two 1.528366 0.376542 0.174490 three 1.247943 -0.742283 0.710400 bar one 0.434128 -1.246384 1.139595 two 1.388668 -0.413554 -0.666287 baz two 0.010150 -0.163820 -0.115305 three 0.216467 0.633720 0.473945 qux one -0.155446 1.287082 0.320201 two -1.256989 0.874920 0.765944 three 0.025557 -0.729782 -0.127439 In [507]: store.append("df_mi_2", df_mi_2) # the levels are automatically included as data columns with keyword level_n In [508]: store.select("df_mi_2", "level_0=foo and level_1=two") Out[508]: A B C foo two 1.528366 0.376542 0.17449 Indexing# You can create/modify an index for a table with create_table_index after data is already in the table (after and append/put operation). Creating a table index is highly encouraged. This will speed your queries a great deal when you use a select with the indexed dimension as the where. Note Indexes are automagically created on the indexables and any data columns you specify. This behavior can be turned off by passing index=False to append. # we have automagically already created an index (in the first section) In [509]: i = store.root.df.table.cols.index.index In [510]: i.optlevel, i.kind Out[510]: (6, 'medium') # change an index by passing new parameters In [511]: store.create_table_index("df", optlevel=9, kind="full") In [512]: i = store.root.df.table.cols.index.index In [513]: i.optlevel, i.kind Out[513]: (9, 'full') Oftentimes when appending large amounts of data to a store, it is useful to turn off index creation for each append, then recreate at the end. In [514]: df_1 = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [515]: df_2 = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [516]: st = pd.HDFStore("appends.h5", mode="w") In [517]: st.append("df", df_1, data_columns=["B"], index=False) In [518]: st.append("df", df_2, data_columns=["B"], index=False) In [519]: st.get_storer("df").table Out[519]: /df/table (Table(20,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2)} byteorder := 'little' chunkshape := (2730,) Then create the index when finished appending. In [520]: st.create_table_index("df", columns=["B"], optlevel=9, kind="full") In [521]: st.get_storer("df").table Out[521]: /df/table (Table(20,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2)} byteorder := 'little' chunkshape := (2730,) autoindex := True colindexes := { "B": Index(9, fullshuffle, zlib(1)).is_csi=True} In [522]: st.close() See here for how to create a completely-sorted-index (CSI) on an existing store. Query via data columns# You can designate (and index) certain columns that you want to be able to perform queries (other than the indexable columns, which you can always query). For instance say you want to perform this common operation, on-disk, and return just the frame that matches this query. You can specify data_columns = True to force all columns to be data_columns. In [523]: df_dc = df.copy() In [524]: df_dc["string"] = "foo" In [525]: df_dc.loc[df_dc.index[4:6], "string"] = np.nan In [526]: df_dc.loc[df_dc.index[7:9], "string"] = "bar" In [527]: df_dc["string2"] = "cool" In [528]: df_dc.loc[df_dc.index[1:3], ["B", "C"]] = 1.0 In [529]: df_dc Out[529]: A B C string string2 2000-01-01 -0.398501 -0.677311 -0.874991 foo cool 2000-01-02 -1.167564 1.000000 1.000000 foo cool 2000-01-03 -0.131959 1.000000 1.000000 foo cool 2000-01-04 0.169405 -1.358046 -0.105563 foo cool 2000-01-05 0.492195 0.076693 0.213685 NaN cool 2000-01-06 -0.285283 -1.210529 -1.408386 NaN cool 2000-01-07 0.941577 -0.342447 0.222031 foo cool 2000-01-08 0.052607 2.093214 1.064908 bar cool # on-disk operations In [530]: store.append("df_dc", df_dc, data_columns=["B", "C", "string", "string2"]) In [531]: store.select("df_dc", where="B > 0") Out[531]: A B C string string2 2000-01-02 -1.167564 1.000000 1.000000 foo cool 2000-01-03 -0.131959 1.000000 1.000000 foo cool 2000-01-05 0.492195 0.076693 0.213685 NaN cool 2000-01-08 0.052607 2.093214 1.064908 bar cool # getting creative In [532]: store.select("df_dc", "B > 0 & C > 0 & string == foo") Out[532]: A B C string string2 2000-01-02 -1.167564 1.0 1.0 foo cool 2000-01-03 -0.131959 1.0 1.0 foo cool # this is in-memory version of this type of selection In [533]: df_dc[(df_dc.B > 0) & (df_dc.C > 0) & (df_dc.string == "foo")] Out[533]: A B C string string2 2000-01-02 -1.167564 1.0 1.0 foo cool 2000-01-03 -0.131959 1.0 1.0 foo cool # we have automagically created this index and the B/C/string/string2 # columns are stored separately as ``PyTables`` columns In [534]: store.root.df_dc.table Out[534]: /df_dc/table (Table(8,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2), "C": Float64Col(shape=(), dflt=0.0, pos=3), "string": StringCol(itemsize=3, shape=(), dflt=b'', pos=4), "string2": StringCol(itemsize=4, shape=(), dflt=b'', pos=5)} byteorder := 'little' chunkshape := (1680,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False, "B": Index(6, mediumshuffle, zlib(1)).is_csi=False, "C": Index(6, mediumshuffle, zlib(1)).is_csi=False, "string": Index(6, mediumshuffle, zlib(1)).is_csi=False, "string2": Index(6, mediumshuffle, zlib(1)).is_csi=False} There is some performance degradation by making lots of columns into data columns, so it is up to the user to designate these. In addition, you cannot change data columns (nor indexables) after the first append/put operation (Of course you can simply read in the data and create a new table!). Iterator# You can pass iterator=True or chunksize=number_in_a_chunk to select and select_as_multiple to return an iterator on the results. The default is 50,000 rows returned in a chunk. In [535]: for df in store.select("df", chunksize=3): .....: print(df) .....: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 A B C 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 A B C 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Note You can also use the iterator with read_hdf which will open, then automatically close the store when finished iterating. for df in pd.read_hdf("store.h5", "df", chunksize=3): print(df) Note, that the chunksize keyword applies to the source rows. So if you are doing a query, then the chunksize will subdivide the total rows in the table and the query applied, returning an iterator on potentially unequal sized chunks. Here is a recipe for generating a query and using it to create equal sized return chunks. In [536]: dfeq = pd.DataFrame({"number": np.arange(1, 11)}) In [537]: dfeq Out[537]: number 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 In [538]: store.append("dfeq", dfeq, data_columns=["number"]) In [539]: def chunks(l, n): .....: return [l[i: i + n] for i in range(0, len(l), n)] .....: In [540]: evens = [2, 4, 6, 8, 10] In [541]: coordinates = store.select_as_coordinates("dfeq", "number=evens") In [542]: for c in chunks(coordinates, 2): .....: print(store.select("dfeq", where=c)) .....: number 1 2 3 4 number 5 6 7 8 number 9 10 Advanced queries# Select a single column# To retrieve a single indexable or data column, use the method select_column. This will, for example, enable you to get the index very quickly. These return a Series of the result, indexed by the row number. These do not currently accept the where selector. In [543]: store.select_column("df_dc", "index") Out[543]: 0 2000-01-01 1 2000-01-02 2 2000-01-03 3 2000-01-04 4 2000-01-05 5 2000-01-06 6 2000-01-07 7 2000-01-08 Name: index, dtype: datetime64[ns] In [544]: store.select_column("df_dc", "string") Out[544]: 0 foo 1 foo 2 foo 3 foo 4 NaN 5 NaN 6 foo 7 bar Name: string, dtype: object Selecting coordinates# Sometimes you want to get the coordinates (a.k.a the index locations) of your query. This returns an Int64Index of the resulting locations. These coordinates can also be passed to subsequent where operations. In [545]: df_coord = pd.DataFrame( .....: np.random.randn(1000, 2), index=pd.date_range("20000101", periods=1000) .....: ) .....: In [546]: store.append("df_coord", df_coord) In [547]: c = store.select_as_coordinates("df_coord", "index > 20020101") In [548]: c Out[548]: Int64Index([732, 733, 734, 735, 736, 737, 738, 739, 740, 741, ... 990, 991, 992, 993, 994, 995, 996, 997, 998, 999], dtype='int64', length=268) In [549]: store.select("df_coord", where=c) Out[549]: 0 1 2002-01-02 0.009035 0.921784 2002-01-03 -1.476563 -1.376375 2002-01-04 1.266731 2.173681 2002-01-05 0.147621 0.616468 2002-01-06 0.008611 2.136001 ... ... ... 2002-09-22 0.781169 -0.791687 2002-09-23 -0.764810 -2.000933 2002-09-24 -0.345662 0.393915 2002-09-25 -0.116661 0.834638 2002-09-26 -1.341780 0.686366 [268 rows x 2 columns] Selecting using a where mask# Sometime your query can involve creating a list of rows to select. Usually this mask would be a resulting index from an indexing operation. This example selects the months of a datetimeindex which are 5. In [550]: df_mask = pd.DataFrame( .....: np.random.randn(1000, 2), index=pd.date_range("20000101", periods=1000) .....: ) .....: In [551]: store.append("df_mask", df_mask) In [552]: c = store.select_column("df_mask", "index") In [553]: where = c[pd.DatetimeIndex(c).month == 5].index In [554]: store.select("df_mask", where=where) Out[554]: 0 1 2000-05-01 -0.386742 -0.977433 2000-05-02 -0.228819 0.471671 2000-05-03 0.337307 1.840494 2000-05-04 0.050249 0.307149 2000-05-05 -0.802947 -0.946730 ... ... ... 2002-05-27 1.605281 1.741415 2002-05-28 -0.804450 -0.715040 2002-05-29 -0.874851 0.037178 2002-05-30 -0.161167 -1.294944 2002-05-31 -0.258463 -0.731969 [93 rows x 2 columns] Storer object# If you want to inspect the stored object, retrieve via get_storer. You could use this programmatically to say get the number of rows in an object. In [555]: store.get_storer("df_dc").nrows Out[555]: 8 Multiple table queries# The methods append_to_multiple and select_as_multiple can perform appending/selecting from multiple tables at once. The idea is to have one table (call it the selector table) that you index most/all of the columns, and perform your queries. The other table(s) are data tables with an index matching the selector table’s index. You can then perform a very fast query on the selector table, yet get lots of data back. This method is similar to having a very wide table, but enables more efficient queries. The append_to_multiple method splits a given single DataFrame into multiple tables according to d, a dictionary that maps the table names to a list of ‘columns’ you want in that table. If None is used in place of a list, that table will have the remaining unspecified columns of the given DataFrame. The argument selector defines which table is the selector table (which you can make queries from). The argument dropna will drop rows from the input DataFrame to ensure tables are synchronized. This means that if a row for one of the tables being written to is entirely np.NaN, that row will be dropped from all tables. If dropna is False, THE USER IS RESPONSIBLE FOR SYNCHRONIZING THE TABLES. Remember that entirely np.Nan rows are not written to the HDFStore, so if you choose to call dropna=False, some tables may have more rows than others, and therefore select_as_multiple may not work or it may return unexpected results. In [556]: df_mt = pd.DataFrame( .....: np.random.randn(8, 6), .....: index=pd.date_range("1/1/2000", periods=8), .....: columns=["A", "B", "C", "D", "E", "F"], .....: ) .....: In [557]: df_mt["foo"] = "bar" In [558]: df_mt.loc[df_mt.index[1], ("A", "B")] = np.nan # you can also create the tables individually In [559]: store.append_to_multiple( .....: {"df1_mt": ["A", "B"], "df2_mt": None}, df_mt, selector="df1_mt" .....: ) .....: In [560]: store Out[560]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # individual tables were created In [561]: store.select("df1_mt") Out[561]: A B 2000-01-01 0.079529 -1.459471 2000-01-02 NaN NaN 2000-01-03 -0.423113 2.314361 2000-01-04 0.756744 -0.792372 2000-01-05 -0.184971 0.170852 2000-01-06 0.678830 0.633974 2000-01-07 0.034973 0.974369 2000-01-08 -2.110103 0.243062 In [562]: store.select("df2_mt") Out[562]: C D E F foo 2000-01-01 -0.596306 -0.910022 -1.057072 -0.864360 bar 2000-01-02 0.477849 0.283128 -2.045700 -0.338206 bar 2000-01-03 -0.033100 -0.965461 -0.001079 -0.351689 bar 2000-01-04 -0.513555 -1.484776 -0.796280 -0.182321 bar 2000-01-05 -0.872407 -1.751515 0.934334 0.938818 bar 2000-01-06 -1.398256 1.347142 -0.029520 0.082738 bar 2000-01-07 -0.755544 0.380786 -1.634116 1.293610 bar 2000-01-08 1.453064 0.500558 -0.574475 0.694324 bar # as a multiple In [563]: store.select_as_multiple( .....: ["df1_mt", "df2_mt"], .....: where=["A>0", "B>0"], .....: selector="df1_mt", .....: ) .....: Out[563]: A B C D E F foo 2000-01-06 0.678830 0.633974 -1.398256 1.347142 -0.029520 0.082738 bar 2000-01-07 0.034973 0.974369 -0.755544 0.380786 -1.634116 1.293610 bar Delete from a table# You can delete from a table selectively by specifying a where. In deleting rows, it is important to understand the PyTables deletes rows by erasing the rows, then moving the following data. Thus deleting can potentially be a very expensive operation depending on the orientation of your data. To get optimal performance, it’s worthwhile to have the dimension you are deleting be the first of the indexables. Data is ordered (on the disk) in terms of the indexables. Here’s a simple use case. You store panel-type data, with dates in the major_axis and ids in the minor_axis. The data is then interleaved like this: date_1 id_1 id_2 . id_n date_2 id_1 . id_n It should be clear that a delete operation on the major_axis will be fairly quick, as one chunk is removed, then the following data moved. On the other hand a delete operation on the minor_axis will be very expensive. In this case it would almost certainly be faster to rewrite the table using a where that selects all but the missing data. Warning Please note that HDF5 DOES NOT RECLAIM SPACE in the h5 files automatically. Thus, repeatedly deleting (or removing nodes) and adding again, WILL TEND TO INCREASE THE FILE SIZE. To repack and clean the file, use ptrepack. Notes & caveats# Compression# PyTables allows the stored data to be compressed. This applies to all kinds of stores, not just tables. Two parameters are used to control compression: complevel and complib. complevel specifies if and how hard data is to be compressed. complevel=0 and complevel=None disables compression and 0<complevel<10 enables compression. complib specifies which compression library to use. If nothing is specified the default library zlib is used. A compression library usually optimizes for either good compression rates or speed and the results will depend on the type of data. Which type of compression to choose depends on your specific needs and data. The list of supported compression libraries: zlib: The default compression library. A classic in terms of compression, achieves good compression rates but is somewhat slow. lzo: Fast compression and decompression. bzip2: Good compression rates. blosc: Fast compression and decompression. Support for alternative blosc compressors: blosc:blosclz This is the default compressor for blosc blosc:lz4: A compact, very popular and fast compressor. blosc:lz4hc: A tweaked version of LZ4, produces better compression ratios at the expense of speed. blosc:snappy: A popular compressor used in many places. blosc:zlib: A classic; somewhat slower than the previous ones, but achieving better compression ratios. blosc:zstd: An extremely well balanced codec; it provides the best compression ratios among the others above, and at reasonably fast speed. If complib is defined as something other than the listed libraries a ValueError exception is issued. Note If the library specified with the complib option is missing on your platform, compression defaults to zlib without further ado. Enable compression for all objects within the file: store_compressed = pd.HDFStore( "store_compressed.h5", complevel=9, complib="blosc:blosclz" ) Or on-the-fly compression (this only applies to tables) in stores where compression is not enabled: store.append("df", df, complib="zlib", complevel=5) ptrepack# PyTables offers better write performance when tables are compressed after they are written, as opposed to turning on compression at the very beginning. You can use the supplied PyTables utility ptrepack. In addition, ptrepack can change compression levels after the fact. ptrepack --chunkshape=auto --propindexes --complevel=9 --complib=blosc in.h5 out.h5 Furthermore ptrepack in.h5 out.h5 will repack the file to allow you to reuse previously deleted space. Alternatively, one can simply remove the file and write again, or use the copy method. Caveats# Warning HDFStore is not-threadsafe for writing. The underlying PyTables only supports concurrent reads (via threading or processes). If you need reading and writing at the same time, you need to serialize these operations in a single thread in a single process. You will corrupt your data otherwise. See the (GH2397) for more information. If you use locks to manage write access between multiple processes, you may want to use fsync() before releasing write locks. For convenience you can use store.flush(fsync=True) to do this for you. Once a table is created columns (DataFrame) are fixed; only exactly the same columns can be appended Be aware that timezones (e.g., pytz.timezone('US/Eastern')) are not necessarily equal across timezone versions. So if data is localized to a specific timezone in the HDFStore using one version of a timezone library and that data is updated with another version, the data will be converted to UTC since these timezones are not considered equal. Either use the same version of timezone library or use tz_convert with the updated timezone definition. Warning PyTables will show a NaturalNameWarning if a column name cannot be used as an attribute selector. Natural identifiers contain only letters, numbers, and underscores, and may not begin with a number. Other identifiers cannot be used in a where clause and are generally a bad idea. DataTypes# HDFStore will map an object dtype to the PyTables underlying dtype. This means the following types are known to work: Type Represents missing values floating : float64, float32, float16 np.nan integer : int64, int32, int8, uint64,uint32, uint8 boolean datetime64[ns] NaT timedelta64[ns] NaT categorical : see the section below object : strings np.nan unicode columns are not supported, and WILL FAIL. Categorical data# You can write data that contains category dtypes to a HDFStore. Queries work the same as if it was an object array. However, the category dtyped data is stored in a more efficient manner. In [564]: dfcat = pd.DataFrame( .....: {"A": pd.Series(list("aabbcdba")).astype("category"), "B": np.random.randn(8)} .....: ) .....: In [565]: dfcat Out[565]: A B 0 a -1.608059 1 a 0.851060 2 b -0.736931 3 b 0.003538 4 c -1.422611 5 d 2.060901 6 b 0.993899 7 a -1.371768 In [566]: dfcat.dtypes Out[566]: A category B float64 dtype: object In [567]: cstore = pd.HDFStore("cats.h5", mode="w") In [568]: cstore.append("dfcat", dfcat, format="table", data_columns=["A"]) In [569]: result = cstore.select("dfcat", where="A in ['b', 'c']") In [570]: result Out[570]: A B 2 b -0.736931 3 b 0.003538 4 c -1.422611 6 b 0.993899 In [571]: result.dtypes Out[571]: A category B float64 dtype: object String columns# min_itemsize The underlying implementation of HDFStore uses a fixed column width (itemsize) for string columns. A string column itemsize is calculated as the maximum of the length of data (for that column) that is passed to the HDFStore, in the first append. Subsequent appends, may introduce a string for a column larger than the column can hold, an Exception will be raised (otherwise you could have a silent truncation of these columns, leading to loss of information). In the future we may relax this and allow a user-specified truncation to occur. Pass min_itemsize on the first table creation to a-priori specify the minimum length of a particular string column. min_itemsize can be an integer, or a dict mapping a column name to an integer. You can pass values as a key to allow all indexables or data_columns to have this min_itemsize. Passing a min_itemsize dict will cause all passed columns to be created as data_columns automatically. Note If you are not passing any data_columns, then the min_itemsize will be the maximum of the length of any string passed In [572]: dfs = pd.DataFrame({"A": "foo", "B": "bar"}, index=list(range(5))) In [573]: dfs Out[573]: A B 0 foo bar 1 foo bar 2 foo bar 3 foo bar 4 foo bar # A and B have a size of 30 In [574]: store.append("dfs", dfs, min_itemsize=30) In [575]: store.get_storer("dfs").table Out[575]: /dfs/table (Table(5,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": StringCol(itemsize=30, shape=(2,), dflt=b'', pos=1)} byteorder := 'little' chunkshape := (963,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False} # A is created as a data_column with a size of 30 # B is size is calculated In [576]: store.append("dfs2", dfs, min_itemsize={"A": 30}) In [577]: store.get_storer("dfs2").table Out[577]: /dfs2/table (Table(5,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": StringCol(itemsize=3, shape=(1,), dflt=b'', pos=1), "A": StringCol(itemsize=30, shape=(), dflt=b'', pos=2)} byteorder := 'little' chunkshape := (1598,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False, "A": Index(6, mediumshuffle, zlib(1)).is_csi=False} nan_rep String columns will serialize a np.nan (a missing value) with the nan_rep string representation. This defaults to the string value nan. You could inadvertently turn an actual nan value into a missing value. In [578]: dfss = pd.DataFrame({"A": ["foo", "bar", "nan"]}) In [579]: dfss Out[579]: A 0 foo 1 bar 2 nan In [580]: store.append("dfss", dfss) In [581]: store.select("dfss") Out[581]: A 0 foo 1 bar 2 NaN # here you need to specify a different nan rep In [582]: store.append("dfss2", dfss, nan_rep="_nan_") In [583]: store.select("dfss2") Out[583]: A 0 foo 1 bar 2 nan External compatibility# HDFStore writes table format objects in specific formats suitable for producing loss-less round trips to pandas objects. For external compatibility, HDFStore can read native PyTables format tables. It is possible to write an HDFStore object that can easily be imported into R using the rhdf5 library (Package website). Create a table format store like this: In [584]: df_for_r = pd.DataFrame( .....: { .....: "first": np.random.rand(100), .....: "second": np.random.rand(100), .....: "class": np.random.randint(0, 2, (100,)), .....: }, .....: index=range(100), .....: ) .....: In [585]: df_for_r.head() Out[585]: first second class 0 0.013480 0.504941 0 1 0.690984 0.898188 1 2 0.510113 0.618748 1 3 0.357698 0.004972 0 4 0.451658 0.012065 1 In [586]: store_export = pd.HDFStore("export.h5") In [587]: store_export.append("df_for_r", df_for_r, data_columns=df_dc.columns) In [588]: store_export Out[588]: <class 'pandas.io.pytables.HDFStore'> File path: export.h5 In R this file can be read into a data.frame object using the rhdf5 library. The following example function reads the corresponding column names and data values from the values and assembles them into a data.frame: # Load values and column names for all datasets from corresponding nodes and # insert them into one data.frame object. library(rhdf5) loadhdf5data <- function(h5File) { listing <- h5ls(h5File) # Find all data nodes, values are stored in *_values and corresponding column # titles in *_items data_nodes <- grep("_values", listing$name) name_nodes <- grep("_items", listing$name) data_paths = paste(listing$group[data_nodes], listing$name[data_nodes], sep = "/") name_paths = paste(listing$group[name_nodes], listing$name[name_nodes], sep = "/") columns = list() for (idx in seq(data_paths)) { # NOTE: matrices returned by h5read have to be transposed to obtain # required Fortran order! data <- data.frame(t(h5read(h5File, data_paths[idx]))) names <- t(h5read(h5File, name_paths[idx])) entry <- data.frame(data) colnames(entry) <- names columns <- append(columns, entry) } data <- data.frame(columns) return(data) } Now you can import the DataFrame into R: > data = loadhdf5data("transfer.hdf5") > head(data) first second class 1 0.4170220047 0.3266449 0 2 0.7203244934 0.5270581 0 3 0.0001143748 0.8859421 1 4 0.3023325726 0.3572698 1 5 0.1467558908 0.9085352 1 6 0.0923385948 0.6233601 1 Note The R function lists the entire HDF5 file’s contents and assembles the data.frame object from all matching nodes, so use this only as a starting point if you have stored multiple DataFrame objects to a single HDF5 file. Performance# tables format come with a writing performance penalty as compared to fixed stores. The benefit is the ability to append/delete and query (potentially very large amounts of data). Write times are generally longer as compared with regular stores. Query times can be quite fast, especially on an indexed axis. You can pass chunksize=<int> to append, specifying the write chunksize (default is 50000). This will significantly lower your memory usage on writing. You can pass expectedrows=<int> to the first append, to set the TOTAL number of rows that PyTables will expect. This will optimize read/write performance. Duplicate rows can be written to tables, but are filtered out in selection (with the last items being selected; thus a table is unique on major, minor pairs) A PerformanceWarning will be raised if you are attempting to store types that will be pickled by PyTables (rather than stored as endemic types). See Here for more information and some solutions. Feather# Feather provides binary columnar serialization for data frames. It is designed to make reading and writing data frames efficient, and to make sharing data across data analysis languages easy. Feather is designed to faithfully serialize and de-serialize DataFrames, supporting all of the pandas dtypes, including extension dtypes such as categorical and datetime with tz. Several caveats: The format will NOT write an Index, or MultiIndex for the DataFrame and will raise an error if a non-default one is provided. You can .reset_index() to store the index or .reset_index(drop=True) to ignore it. Duplicate column names and non-string columns names are not supported Actual Python objects in object dtype columns are not supported. These will raise a helpful error message on an attempt at serialization. See the Full Documentation. In [589]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(3, 6).astype("u1"), .....: "d": np.arange(4.0, 7.0, dtype="float64"), .....: "e": [True, False, True], .....: "f": pd.Categorical(list("abc")), .....: "g": pd.date_range("20130101", periods=3), .....: "h": pd.date_range("20130101", periods=3, tz="US/Eastern"), .....: "i": pd.date_range("20130101", periods=3, freq="ns"), .....: } .....: ) .....: In [590]: df Out[590]: a b c ... g h i 0 a 1 3 ... 2013-01-01 2013-01-01 00:00:00-05:00 2013-01-01 00:00:00.000000000 1 b 2 4 ... 2013-01-02 2013-01-02 00:00:00-05:00 2013-01-01 00:00:00.000000001 2 c 3 5 ... 2013-01-03 2013-01-03 00:00:00-05:00 2013-01-01 00:00:00.000000002 [3 rows x 9 columns] In [591]: df.dtypes Out[591]: a object b int64 c uint8 d float64 e bool f category g datetime64[ns] h datetime64[ns, US/Eastern] i datetime64[ns] dtype: object Write to a feather file. In [592]: df.to_feather("example.feather") Read from a feather file. In [593]: result = pd.read_feather("example.feather") In [594]: result Out[594]: a b c ... g h i 0 a 1 3 ... 2013-01-01 2013-01-01 00:00:00-05:00 2013-01-01 00:00:00.000000000 1 b 2 4 ... 2013-01-02 2013-01-02 00:00:00-05:00 2013-01-01 00:00:00.000000001 2 c 3 5 ... 2013-01-03 2013-01-03 00:00:00-05:00 2013-01-01 00:00:00.000000002 [3 rows x 9 columns] # we preserve dtypes In [595]: result.dtypes Out[595]: a object b int64 c uint8 d float64 e bool f category g datetime64[ns] h datetime64[ns, US/Eastern] i datetime64[ns] dtype: object Parquet# Apache Parquet provides a partitioned binary columnar serialization for data frames. It is designed to make reading and writing data frames efficient, and to make sharing data across data analysis languages easy. Parquet can use a variety of compression techniques to shrink the file size as much as possible while still maintaining good read performance. Parquet is designed to faithfully serialize and de-serialize DataFrame s, supporting all of the pandas dtypes, including extension dtypes such as datetime with tz. Several caveats. Duplicate column names and non-string columns names are not supported. The pyarrow engine always writes the index to the output, but fastparquet only writes non-default indexes. This extra column can cause problems for non-pandas consumers that are not expecting it. You can force including or omitting indexes with the index argument, regardless of the underlying engine. Index level names, if specified, must be strings. In the pyarrow engine, categorical dtypes for non-string types can be serialized to parquet, but will de-serialize as their primitive dtype. The pyarrow engine preserves the ordered flag of categorical dtypes with string types. fastparquet does not preserve the ordered flag. Non supported types include Interval and actual Python object types. These will raise a helpful error message on an attempt at serialization. Period type is supported with pyarrow >= 0.16.0. The pyarrow engine preserves extension data types such as the nullable integer and string data type (requiring pyarrow >= 0.16.0, and requiring the extension type to implement the needed protocols, see the extension types documentation). You can specify an engine to direct the serialization. This can be one of pyarrow, or fastparquet, or auto. If the engine is NOT specified, then the pd.options.io.parquet.engine option is checked; if this is also auto, then pyarrow is tried, and falling back to fastparquet. See the documentation for pyarrow and fastparquet. Note These engines are very similar and should read/write nearly identical parquet format files. pyarrow>=8.0.0 supports timedelta data, fastparquet>=0.1.4 supports timezone aware datetimes. These libraries differ by having different underlying dependencies (fastparquet by using numba, while pyarrow uses a c-library). In [596]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(3, 6).astype("u1"), .....: "d": np.arange(4.0, 7.0, dtype="float64"), .....: "e": [True, False, True], .....: "f": pd.date_range("20130101", periods=3), .....: "g": pd.date_range("20130101", periods=3, tz="US/Eastern"), .....: "h": pd.Categorical(list("abc")), .....: "i": pd.Categorical(list("abc"), ordered=True), .....: } .....: ) .....: In [597]: df Out[597]: a b c d e f g h i 0 a 1 3 4.0 True 2013-01-01 2013-01-01 00:00:00-05:00 a a 1 b 2 4 5.0 False 2013-01-02 2013-01-02 00:00:00-05:00 b b 2 c 3 5 6.0 True 2013-01-03 2013-01-03 00:00:00-05:00 c c In [598]: df.dtypes Out[598]: a object b int64 c uint8 d float64 e bool f datetime64[ns] g datetime64[ns, US/Eastern] h category i category dtype: object Write to a parquet file. In [599]: df.to_parquet("example_pa.parquet", engine="pyarrow") In [600]: df.to_parquet("example_fp.parquet", engine="fastparquet") Read from a parquet file. In [601]: result = pd.read_parquet("example_fp.parquet", engine="fastparquet") In [602]: result = pd.read_parquet("example_pa.parquet", engine="pyarrow") In [603]: result.dtypes Out[603]: a object b int64 c uint8 d float64 e bool f datetime64[ns] g datetime64[ns, US/Eastern] h category i category dtype: object Read only certain columns of a parquet file. In [604]: result = pd.read_parquet( .....: "example_fp.parquet", .....: engine="fastparquet", .....: columns=["a", "b"], .....: ) .....: In [605]: result = pd.read_parquet( .....: "example_pa.parquet", .....: engine="pyarrow", .....: columns=["a", "b"], .....: ) .....: In [606]: result.dtypes Out[606]: a object b int64 dtype: object Handling indexes# Serializing a DataFrame to parquet may include the implicit index as one or more columns in the output file. Thus, this code: In [607]: df = pd.DataFrame({"a": [1, 2], "b": [3, 4]}) In [608]: df.to_parquet("test.parquet", engine="pyarrow") creates a parquet file with three columns if you use pyarrow for serialization: a, b, and __index_level_0__. If you’re using fastparquet, the index may or may not be written to the file. This unexpected extra column causes some databases like Amazon Redshift to reject the file, because that column doesn’t exist in the target table. If you want to omit a dataframe’s indexes when writing, pass index=False to to_parquet(): In [609]: df.to_parquet("test.parquet", index=False) This creates a parquet file with just the two expected columns, a and b. If your DataFrame has a custom index, you won’t get it back when you load this file into a DataFrame. Passing index=True will always write the index, even if that’s not the underlying engine’s default behavior. Partitioning Parquet files# Parquet supports partitioning of data based on the values of one or more columns. In [610]: df = pd.DataFrame({"a": [0, 0, 1, 1], "b": [0, 1, 0, 1]}) In [611]: df.to_parquet(path="test", engine="pyarrow", partition_cols=["a"], compression=None) The path specifies the parent directory to which data will be saved. The partition_cols are the column names by which the dataset will be partitioned. Columns are partitioned in the order they are given. The partition splits are determined by the unique values in the partition columns. The above example creates a partitioned dataset that may look like: test ├── a=0 │ ├── 0bac803e32dc42ae83fddfd029cbdebc.parquet │ └── ... └── a=1 ├── e6ab24a4f45147b49b54a662f0c412a3.parquet └── ... ORC# New in version 1.0.0. Similar to the parquet format, the ORC Format is a binary columnar serialization for data frames. It is designed to make reading data frames efficient. pandas provides both the reader and the writer for the ORC format, read_orc() and to_orc(). This requires the pyarrow library. Warning It is highly recommended to install pyarrow using conda due to some issues occurred by pyarrow. to_orc() requires pyarrow>=7.0.0. read_orc() and to_orc() are not supported on Windows yet, you can find valid environments on install optional dependencies. For supported dtypes please refer to supported ORC features in Arrow. Currently timezones in datetime columns are not preserved when a dataframe is converted into ORC files. In [612]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(4.0, 7.0, dtype="float64"), .....: "d": [True, False, True], .....: "e": pd.date_range("20130101", periods=3), .....: } .....: ) .....: In [613]: df Out[613]: a b c d e 0 a 1 4.0 True 2013-01-01 1 b 2 5.0 False 2013-01-02 2 c 3 6.0 True 2013-01-03 In [614]: df.dtypes Out[614]: a object b int64 c float64 d bool e datetime64[ns] dtype: object Write to an orc file. In [615]: df.to_orc("example_pa.orc", engine="pyarrow") Read from an orc file. In [616]: result = pd.read_orc("example_pa.orc") In [617]: result.dtypes Out[617]: a object b int64 c float64 d bool e datetime64[ns] dtype: object Read only certain columns of an orc file. In [618]: result = pd.read_orc( .....: "example_pa.orc", .....: columns=["a", "b"], .....: ) .....: In [619]: result.dtypes Out[619]: a object b int64 dtype: object SQL queries# The pandas.io.sql module provides a collection of query wrappers to both facilitate data retrieval and to reduce dependency on DB-specific API. Database abstraction is provided by SQLAlchemy if installed. In addition you will need a driver library for your database. Examples of such drivers are psycopg2 for PostgreSQL or pymysql for MySQL. For SQLite this is included in Python’s standard library by default. You can find an overview of supported drivers for each SQL dialect in the SQLAlchemy docs. If SQLAlchemy is not installed, a fallback is only provided for sqlite (and for mysql for backwards compatibility, but this is deprecated and will be removed in a future version). This mode requires a Python database adapter which respect the Python DB-API. See also some cookbook examples for some advanced strategies. The key functions are: read_sql_table(table_name, con[, schema, ...]) Read SQL database table into a DataFrame. read_sql_query(sql, con[, index_col, ...]) Read SQL query into a DataFrame. read_sql(sql, con[, index_col, ...]) Read SQL query or database table into a DataFrame. DataFrame.to_sql(name, con[, schema, ...]) Write records stored in a DataFrame to a SQL database. Note The function read_sql() is a convenience wrapper around read_sql_table() and read_sql_query() (and for backward compatibility) and will delegate to specific function depending on the provided input (database table name or sql query). Table names do not need to be quoted if they have special characters. In the following example, we use the SQlite SQL database engine. You can use a temporary SQLite database where data are stored in “memory”. To connect with SQLAlchemy you use the create_engine() function to create an engine object from database URI. You only need to create the engine once per database you are connecting to. For more information on create_engine() and the URI formatting, see the examples below and the SQLAlchemy documentation In [620]: from sqlalchemy import create_engine # Create your engine. In [621]: engine = create_engine("sqlite:///:memory:") If you want to manage your own connections you can pass one of those instead. The example below opens a connection to the database using a Python context manager that automatically closes the connection after the block has completed. See the SQLAlchemy docs for an explanation of how the database connection is handled. with engine.connect() as conn, conn.begin(): data = pd.read_sql_table("data", conn) Warning When you open a connection to a database you are also responsible for closing it. Side effects of leaving a connection open may include locking the database or other breaking behaviour. Writing DataFrames# Assuming the following data is in a DataFrame data, we can insert it into the database using to_sql(). id Date Col_1 Col_2 Col_3 26 2012-10-18 X 25.7 True 42 2012-10-19 Y -12.4 False 63 2012-10-20 Z 5.73 True In [622]: import datetime In [623]: c = ["id", "Date", "Col_1", "Col_2", "Col_3"] In [624]: d = [ .....: (26, datetime.datetime(2010, 10, 18), "X", 27.5, True), .....: (42, datetime.datetime(2010, 10, 19), "Y", -12.5, False), .....: (63, datetime.datetime(2010, 10, 20), "Z", 5.73, True), .....: ] .....: In [625]: data = pd.DataFrame(d, columns=c) In [626]: data Out[626]: id Date Col_1 Col_2 Col_3 0 26 2010-10-18 X 27.50 True 1 42 2010-10-19 Y -12.50 False 2 63 2010-10-20 Z 5.73 True In [627]: data.to_sql("data", engine) Out[627]: 3 With some databases, writing large DataFrames can result in errors due to packet size limitations being exceeded. This can be avoided by setting the chunksize parameter when calling to_sql. For example, the following writes data to the database in batches of 1000 rows at a time: In [628]: data.to_sql("data_chunked", engine, chunksize=1000) Out[628]: 3 SQL data types# to_sql() will try to map your data to an appropriate SQL data type based on the dtype of the data. When you have columns of dtype object, pandas will try to infer the data type. You can always override the default type by specifying the desired SQL type of any of the columns by using the dtype argument. This argument needs a dictionary mapping column names to SQLAlchemy types (or strings for the sqlite3 fallback mode). For example, specifying to use the sqlalchemy String type instead of the default Text type for string columns: In [629]: from sqlalchemy.types import String In [630]: data.to_sql("data_dtype", engine, dtype={"Col_1": String}) Out[630]: 3 Note Due to the limited support for timedelta’s in the different database flavors, columns with type timedelta64 will be written as integer values as nanoseconds to the database and a warning will be raised. Note Columns of category dtype will be converted to the dense representation as you would get with np.asarray(categorical) (e.g. for string categories this gives an array of strings). Because of this, reading the database table back in does not generate a categorical. Datetime data types# Using SQLAlchemy, to_sql() is capable of writing datetime data that is timezone naive or timezone aware. However, the resulting data stored in the database ultimately depends on the supported data type for datetime data of the database system being used. The following table lists supported data types for datetime data for some common databases. Other database dialects may have different data types for datetime data. Database SQL Datetime Types Timezone Support SQLite TEXT No MySQL TIMESTAMP or DATETIME No PostgreSQL TIMESTAMP or TIMESTAMP WITH TIME ZONE Yes When writing timezone aware data to databases that do not support timezones, the data will be written as timezone naive timestamps that are in local time with respect to the timezone. read_sql_table() is also capable of reading datetime data that is timezone aware or naive. When reading TIMESTAMP WITH TIME ZONE types, pandas will convert the data to UTC. Insertion method# The parameter method controls the SQL insertion clause used. Possible values are: None: Uses standard SQL INSERT clause (one per row). 'multi': Pass multiple values in a single INSERT clause. It uses a special SQL syntax not supported by all backends. This usually provides better performance for analytic databases like Presto and Redshift, but has worse performance for traditional SQL backend if the table contains many columns. For more information check the SQLAlchemy documentation. callable with signature (pd_table, conn, keys, data_iter): This can be used to implement a more performant insertion method based on specific backend dialect features. Example of a callable using PostgreSQL COPY clause: # Alternative to_sql() *method* for DBs that support COPY FROM import csv from io import StringIO def psql_insert_copy(table, conn, keys, data_iter): """ Execute SQL statement inserting data Parameters ---------- table : pandas.io.sql.SQLTable conn : sqlalchemy.engine.Engine or sqlalchemy.engine.Connection keys : list of str Column names data_iter : Iterable that iterates the values to be inserted """ # gets a DBAPI connection that can provide a cursor dbapi_conn = conn.connection with dbapi_conn.cursor() as cur: s_buf = StringIO() writer = csv.writer(s_buf) writer.writerows(data_iter) s_buf.seek(0) columns = ', '.join(['"{}"'.format(k) for k in keys]) if table.schema: table_name = '{}.{}'.format(table.schema, table.name) else: table_name = table.name sql = 'COPY {} ({}) FROM STDIN WITH CSV'.format( table_name, columns) cur.copy_expert(sql=sql, file=s_buf) Reading tables# read_sql_table() will read a database table given the table name and optionally a subset of columns to read. Note In order to use read_sql_table(), you must have the SQLAlchemy optional dependency installed. In [631]: pd.read_sql_table("data", engine) Out[631]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 X 27.50 True 1 1 42 2010-10-19 Y -12.50 False 2 2 63 2010-10-20 Z 5.73 True Note Note that pandas infers column dtypes from query outputs, and not by looking up data types in the physical database schema. For example, assume userid is an integer column in a table. Then, intuitively, select userid ... will return integer-valued series, while select cast(userid as text) ... will return object-valued (str) series. Accordingly, if the query output is empty, then all resulting columns will be returned as object-valued (since they are most general). If you foresee that your query will sometimes generate an empty result, you may want to explicitly typecast afterwards to ensure dtype integrity. You can also specify the name of the column as the DataFrame index, and specify a subset of columns to be read. In [632]: pd.read_sql_table("data", engine, index_col="id") Out[632]: index Date Col_1 Col_2 Col_3 id 26 0 2010-10-18 X 27.50 True 42 1 2010-10-19 Y -12.50 False 63 2 2010-10-20 Z 5.73 True In [633]: pd.read_sql_table("data", engine, columns=["Col_1", "Col_2"]) Out[633]: Col_1 Col_2 0 X 27.50 1 Y -12.50 2 Z 5.73 And you can explicitly force columns to be parsed as dates: In [634]: pd.read_sql_table("data", engine, parse_dates=["Date"]) Out[634]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 X 27.50 True 1 1 42 2010-10-19 Y -12.50 False 2 2 63 2010-10-20 Z 5.73 True If needed you can explicitly specify a format string, or a dict of arguments to pass to pandas.to_datetime(): pd.read_sql_table("data", engine, parse_dates={"Date": "%Y-%m-%d"}) pd.read_sql_table( "data", engine, parse_dates={"Date": {"format": "%Y-%m-%d %H:%M:%S"}}, ) You can check if a table exists using has_table() Schema support# Reading from and writing to different schema’s is supported through the schema keyword in the read_sql_table() and to_sql() functions. Note however that this depends on the database flavor (sqlite does not have schema’s). For example: df.to_sql("table", engine, schema="other_schema") pd.read_sql_table("table", engine, schema="other_schema") Querying# You can query using raw SQL in the read_sql_query() function. In this case you must use the SQL variant appropriate for your database. When using SQLAlchemy, you can also pass SQLAlchemy Expression language constructs, which are database-agnostic. In [635]: pd.read_sql_query("SELECT * FROM data", engine) Out[635]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 00:00:00.000000 X 27.50 1 1 1 42 2010-10-19 00:00:00.000000 Y -12.50 0 2 2 63 2010-10-20 00:00:00.000000 Z 5.73 1 Of course, you can specify a more “complex” query. In [636]: pd.read_sql_query("SELECT id, Col_1, Col_2 FROM data WHERE id = 42;", engine) Out[636]: id Col_1 Col_2 0 42 Y -12.5 The read_sql_query() function supports a chunksize argument. Specifying this will return an iterator through chunks of the query result: In [637]: df = pd.DataFrame(np.random.randn(20, 3), columns=list("abc")) In [638]: df.to_sql("data_chunks", engine, index=False) Out[638]: 20 In [639]: for chunk in pd.read_sql_query("SELECT * FROM data_chunks", engine, chunksize=5): .....: print(chunk) .....: a b c 0 0.070470 0.901320 0.937577 1 0.295770 1.420548 -0.005283 2 -1.518598 -0.730065 0.226497 3 -2.061465 0.632115 0.853619 4 2.719155 0.139018 0.214557 a b c 0 -1.538924 -0.366973 -0.748801 1 -0.478137 -1.559153 -3.097759 2 -2.320335 -0.221090 0.119763 3 0.608228 1.064810 -0.780506 4 -2.736887 0.143539 1.170191 a b c 0 -1.573076 0.075792 -1.722223 1 -0.774650 0.803627 0.221665 2 0.584637 0.147264 1.057825 3 -0.284136 0.912395 1.552808 4 0.189376 -0.109830 0.539341 a b c 0 0.592591 -0.155407 -1.356475 1 0.833837 1.524249 1.606722 2 -0.029487 -0.051359 1.700152 3 0.921484 -0.926347 0.979818 4 0.182380 -0.186376 0.049820 You can also run a plain query without creating a DataFrame with execute(). This is useful for queries that don’t return values, such as INSERT. This is functionally equivalent to calling execute on the SQLAlchemy engine or db connection object. Again, you must use the SQL syntax variant appropriate for your database. from pandas.io import sql sql.execute("SELECT * FROM table_name", engine) sql.execute( "INSERT INTO table_name VALUES(?, ?, ?)", engine, params=[("id", 1, 12.2, True)] ) Engine connection examples# To connect with SQLAlchemy you use the create_engine() function to create an engine object from database URI. You only need to create the engine once per database you are connecting to. from sqlalchemy import create_engine engine = create_engine("postgresql://scott:[email protected]:5432/mydatabase") engine = create_engine("mysql+mysqldb://scott:[email protected]/foo") engine = create_engine("oracle://scott:[email protected]:1521/sidname") engine = create_engine("mssql+pyodbc://mydsn") # sqlite://<nohostname>/<path> # where <path> is relative: engine = create_engine("sqlite:///foo.db") # or absolute, starting with a slash: engine = create_engine("sqlite:////absolute/path/to/foo.db") For more information see the examples the SQLAlchemy documentation Advanced SQLAlchemy queries# You can use SQLAlchemy constructs to describe your query. Use sqlalchemy.text() to specify query parameters in a backend-neutral way In [640]: import sqlalchemy as sa In [641]: pd.read_sql( .....: sa.text("SELECT * FROM data where Col_1=:col1"), engine, params={"col1": "X"} .....: ) .....: Out[641]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 00:00:00.000000 X 27.5 1 If you have an SQLAlchemy description of your database you can express where conditions using SQLAlchemy expressions In [642]: metadata = sa.MetaData() In [643]: data_table = sa.Table( .....: "data", .....: metadata, .....: sa.Column("index", sa.Integer), .....: sa.Column("Date", sa.DateTime), .....: sa.Column("Col_1", sa.String), .....: sa.Column("Col_2", sa.Float), .....: sa.Column("Col_3", sa.Boolean), .....: ) .....: In [644]: pd.read_sql(sa.select([data_table]).where(data_table.c.Col_3 is True), engine) Out[644]: Empty DataFrame Columns: [index, Date, Col_1, Col_2, Col_3] Index: [] You can combine SQLAlchemy expressions with parameters passed to read_sql() using sqlalchemy.bindparam() In [645]: import datetime as dt In [646]: expr = sa.select([data_table]).where(data_table.c.Date > sa.bindparam("date")) In [647]: pd.read_sql(expr, engine, params={"date": dt.datetime(2010, 10, 18)}) Out[647]: index Date Col_1 Col_2 Col_3 0 1 2010-10-19 Y -12.50 False 1 2 2010-10-20 Z 5.73 True Sqlite fallback# The use of sqlite is supported without using SQLAlchemy. This mode requires a Python database adapter which respect the Python DB-API. You can create connections like so: import sqlite3 con = sqlite3.connect(":memory:") And then issue the following queries: data.to_sql("data", con) pd.read_sql_query("SELECT * FROM data", con) Google BigQuery# Warning Starting in 0.20.0, pandas has split off Google BigQuery support into the separate package pandas-gbq. You can pip install pandas-gbq to get it. The pandas-gbq package provides functionality to read/write from Google BigQuery. pandas integrates with this external package. if pandas-gbq is installed, you can use the pandas methods pd.read_gbq and DataFrame.to_gbq, which will call the respective functions from pandas-gbq. Full documentation can be found here. Stata format# Writing to stata format# The method to_stata() will write a DataFrame into a .dta file. The format version of this file is always 115 (Stata 12). In [648]: df = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [649]: df.to_stata("stata.dta") Stata data files have limited data type support; only strings with 244 or fewer characters, int8, int16, int32, float32 and float64 can be stored in .dta files. Additionally, Stata reserves certain values to represent missing data. Exporting a non-missing value that is outside of the permitted range in Stata for a particular data type will retype the variable to the next larger size. For example, int8 values are restricted to lie between -127 and 100 in Stata, and so variables with values above 100 will trigger a conversion to int16. nan values in floating points data types are stored as the basic missing data type (. in Stata). Note It is not possible to export missing data values for integer data types. The Stata writer gracefully handles other data types including int64, bool, uint8, uint16, uint32 by casting to the smallest supported type that can represent the data. For example, data with a type of uint8 will be cast to int8 if all values are less than 100 (the upper bound for non-missing int8 data in Stata), or, if values are outside of this range, the variable is cast to int16. Warning Conversion from int64 to float64 may result in a loss of precision if int64 values are larger than 2**53. Warning StataWriter and to_stata() only support fixed width strings containing up to 244 characters, a limitation imposed by the version 115 dta file format. Attempting to write Stata dta files with strings longer than 244 characters raises a ValueError. Reading from Stata format# The top-level function read_stata will read a dta file and return either a DataFrame or a StataReader that can be used to read the file incrementally. In [650]: pd.read_stata("stata.dta") Out[650]: index A B 0 0 -1.690072 0.405144 1 1 -1.511309 -1.531396 2 2 0.572698 -1.106845 3 3 -1.185859 0.174564 4 4 0.603797 -1.796129 5 5 -0.791679 1.173795 6 6 -0.277710 1.859988 7 7 -0.258413 1.251808 8 8 1.443262 0.441553 9 9 1.168163 -2.054946 Specifying a chunksize yields a StataReader instance that can be used to read chunksize lines from the file at a time. The StataReader object can be used as an iterator. In [651]: with pd.read_stata("stata.dta", chunksize=3) as reader: .....: for df in reader: .....: print(df.shape) .....: (3, 3) (3, 3) (3, 3) (1, 3) For more fine-grained control, use iterator=True and specify chunksize with each call to read(). In [652]: with pd.read_stata("stata.dta", iterator=True) as reader: .....: chunk1 = reader.read(5) .....: chunk2 = reader.read(5) .....: Currently the index is retrieved as a column. The parameter convert_categoricals indicates whether value labels should be read and used to create a Categorical variable from them. Value labels can also be retrieved by the function value_labels, which requires read() to be called before use. The parameter convert_missing indicates whether missing value representations in Stata should be preserved. If False (the default), missing values are represented as np.nan. If True, missing values are represented using StataMissingValue objects, and columns containing missing values will have object data type. Note read_stata() and StataReader support .dta formats 113-115 (Stata 10-12), 117 (Stata 13), and 118 (Stata 14). Note Setting preserve_dtypes=False will upcast to the standard pandas data types: int64 for all integer types and float64 for floating point data. By default, the Stata data types are preserved when importing. Categorical data# Categorical data can be exported to Stata data files as value labeled data. The exported data consists of the underlying category codes as integer data values and the categories as value labels. Stata does not have an explicit equivalent to a Categorical and information about whether the variable is ordered is lost when exporting. Warning Stata only supports string value labels, and so str is called on the categories when exporting data. Exporting Categorical variables with non-string categories produces a warning, and can result a loss of information if the str representations of the categories are not unique. Labeled data can similarly be imported from Stata data files as Categorical variables using the keyword argument convert_categoricals (True by default). The keyword argument order_categoricals (True by default) determines whether imported Categorical variables are ordered. Note When importing categorical data, the values of the variables in the Stata data file are not preserved since Categorical variables always use integer data types between -1 and n-1 where n is the number of categories. If the original values in the Stata data file are required, these can be imported by setting convert_categoricals=False, which will import original data (but not the variable labels). The original values can be matched to the imported categorical data since there is a simple mapping between the original Stata data values and the category codes of imported Categorical variables: missing values are assigned code -1, and the smallest original value is assigned 0, the second smallest is assigned 1 and so on until the largest original value is assigned the code n-1. Note Stata supports partially labeled series. These series have value labels for some but not all data values. Importing a partially labeled series will produce a Categorical with string categories for the values that are labeled and numeric categories for values with no label. SAS formats# The top-level function read_sas() can read (but not write) SAS XPORT (.xpt) and (since v0.18.0) SAS7BDAT (.sas7bdat) format files. SAS files only contain two value types: ASCII text and floating point values (usually 8 bytes but sometimes truncated). For xport files, there is no automatic type conversion to integers, dates, or categoricals. For SAS7BDAT files, the format codes may allow date variables to be automatically converted to dates. By default the whole file is read and returned as a DataFrame. Specify a chunksize or use iterator=True to obtain reader objects (XportReader or SAS7BDATReader) for incrementally reading the file. The reader objects also have attributes that contain additional information about the file and its variables. Read a SAS7BDAT file: df = pd.read_sas("sas_data.sas7bdat") Obtain an iterator and read an XPORT file 100,000 lines at a time: def do_something(chunk): pass with pd.read_sas("sas_xport.xpt", chunk=100000) as rdr: for chunk in rdr: do_something(chunk) The specification for the xport file format is available from the SAS web site. No official documentation is available for the SAS7BDAT format. SPSS formats# New in version 0.25.0. The top-level function read_spss() can read (but not write) SPSS SAV (.sav) and ZSAV (.zsav) format files. SPSS files contain column names. By default the whole file is read, categorical columns are converted into pd.Categorical, and a DataFrame with all columns is returned. Specify the usecols parameter to obtain a subset of columns. Specify convert_categoricals=False to avoid converting categorical columns into pd.Categorical. Read an SPSS file: df = pd.read_spss("spss_data.sav") Extract a subset of columns contained in usecols from an SPSS file and avoid converting categorical columns into pd.Categorical: df = pd.read_spss( "spss_data.sav", usecols=["foo", "bar"], convert_categoricals=False, ) More information about the SAV and ZSAV file formats is available here. Other file formats# pandas itself only supports IO with a limited set of file formats that map cleanly to its tabular data model. For reading and writing other file formats into and from pandas, we recommend these packages from the broader community. netCDF# xarray provides data structures inspired by the pandas DataFrame for working with multi-dimensional datasets, with a focus on the netCDF file format and easy conversion to and from pandas. Performance considerations# This is an informal comparison of various IO methods, using pandas 0.24.2. Timings are machine dependent and small differences should be ignored. In [1]: sz = 1000000 In [2]: df = pd.DataFrame({'A': np.random.randn(sz), 'B': [1] * sz}) In [3]: df.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 1000000 entries, 0 to 999999 Data columns (total 2 columns): A 1000000 non-null float64 B 1000000 non-null int64 dtypes: float64(1), int64(1) memory usage: 15.3 MB The following test functions will be used below to compare the performance of several IO methods: import numpy as np import os sz = 1000000 df = pd.DataFrame({"A": np.random.randn(sz), "B": [1] * sz}) sz = 1000000 np.random.seed(42) df = pd.DataFrame({"A": np.random.randn(sz), "B": [1] * sz}) def test_sql_write(df): if os.path.exists("test.sql"): os.remove("test.sql") sql_db = sqlite3.connect("test.sql") df.to_sql(name="test_table", con=sql_db) sql_db.close() def test_sql_read(): sql_db = sqlite3.connect("test.sql") pd.read_sql_query("select * from test_table", sql_db) sql_db.close() def test_hdf_fixed_write(df): df.to_hdf("test_fixed.hdf", "test", mode="w") def test_hdf_fixed_read(): pd.read_hdf("test_fixed.hdf", "test") def test_hdf_fixed_write_compress(df): df.to_hdf("test_fixed_compress.hdf", "test", mode="w", complib="blosc") def test_hdf_fixed_read_compress(): pd.read_hdf("test_fixed_compress.hdf", "test") def test_hdf_table_write(df): df.to_hdf("test_table.hdf", "test", mode="w", format="table") def test_hdf_table_read(): pd.read_hdf("test_table.hdf", "test") def test_hdf_table_write_compress(df): df.to_hdf( "test_table_compress.hdf", "test", mode="w", complib="blosc", format="table" ) def test_hdf_table_read_compress(): pd.read_hdf("test_table_compress.hdf", "test") def test_csv_write(df): df.to_csv("test.csv", mode="w") def test_csv_read(): pd.read_csv("test.csv", index_col=0) def test_feather_write(df): df.to_feather("test.feather") def test_feather_read(): pd.read_feather("test.feather") def test_pickle_write(df): df.to_pickle("test.pkl") def test_pickle_read(): pd.read_pickle("test.pkl") def test_pickle_write_compress(df): df.to_pickle("test.pkl.compress", compression="xz") def test_pickle_read_compress(): pd.read_pickle("test.pkl.compress", compression="xz") def test_parquet_write(df): df.to_parquet("test.parquet") def test_parquet_read(): pd.read_parquet("test.parquet") When writing, the top three functions in terms of speed are test_feather_write, test_hdf_fixed_write and test_hdf_fixed_write_compress. In [4]: %timeit test_sql_write(df) 3.29 s ± 43.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [5]: %timeit test_hdf_fixed_write(df) 19.4 ms ± 560 µs per loop (mean ± std. dev. of 7 runs, 1 loop each) In [6]: %timeit test_hdf_fixed_write_compress(df) 19.6 ms ± 308 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [7]: %timeit test_hdf_table_write(df) 449 ms ± 5.61 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [8]: %timeit test_hdf_table_write_compress(df) 448 ms ± 11.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [9]: %timeit test_csv_write(df) 3.66 s ± 26.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [10]: %timeit test_feather_write(df) 9.75 ms ± 117 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [11]: %timeit test_pickle_write(df) 30.1 ms ± 229 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [12]: %timeit test_pickle_write_compress(df) 4.29 s ± 15.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [13]: %timeit test_parquet_write(df) 67.6 ms ± 706 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) When reading, the top three functions in terms of speed are test_feather_read, test_pickle_read and test_hdf_fixed_read. In [14]: %timeit test_sql_read() 1.77 s ± 17.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [15]: %timeit test_hdf_fixed_read() 19.4 ms ± 436 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [16]: %timeit test_hdf_fixed_read_compress() 19.5 ms ± 222 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [17]: %timeit test_hdf_table_read() 38.6 ms ± 857 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [18]: %timeit test_hdf_table_read_compress() 38.8 ms ± 1.49 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) In [19]: %timeit test_csv_read() 452 ms ± 9.04 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [20]: %timeit test_feather_read() 12.4 ms ± 99.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [21]: %timeit test_pickle_read() 18.4 ms ± 191 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [22]: %timeit test_pickle_read_compress() 915 ms ± 7.48 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [23]: %timeit test_parquet_read() 24.4 ms ± 146 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) The files test.pkl.compress, test.parquet and test.feather took the least space on disk (in bytes). 29519500 Oct 10 06:45 test.csv 16000248 Oct 10 06:45 test.feather 8281983 Oct 10 06:49 test.parquet 16000857 Oct 10 06:47 test.pkl 7552144 Oct 10 06:48 test.pkl.compress 34816000 Oct 10 06:42 test.sql 24009288 Oct 10 06:43 test_fixed.hdf 24009288 Oct 10 06:43 test_fixed_compress.hdf 24458940 Oct 10 06:44 test_table.hdf 24458940 Oct 10 06:44 test_table_compress.hdf
340
845
Writing excel formulas on pandas DataFrame that depends on Row/Column values Suppose that I have the following df: Column1 Column2 0 19843 30 1 19842 35 2 19841 40 3 19840 45 4 19839 50 5 19838 55 I need to generate a third column that depends of Column1, Column2 and row numbem, like de following: column3_formula = '"={row_number}*(Column1 + Column2)"' I Know that this is kinda weird but Im trying to abstract and simplify my real problem ( my Df has more than 50 columns and I need to add 30 more columns that are Excel formulas ). I already have a way to "parse" the DF Column Names to "Excel real columns" (A3, or C2...): def excel_column(column_number): letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' quot, rem = divmod(column_number - 1, len(letters)) return ( excel_column(quot) + chr(rem + ord('A')) if column_number != 0 else '' ) attributes_excel_column_mapping = { v: excel_column(k) for k, v in enumerate(['Column1', 'Column2'], 1) } With this structure I can do things like column3_formula.format(**attributes_excel_column_mapping) but how can I feel the row number to insert this formulas like my_df['Column3] = ... ?
What I needed was the "name" attribute of row when I use the apply method. Example: self.data_frame['column'] = self.data_frame.apply( lambda row: formula.format( **column_mapping, row_number=row.name ), axis=1 ) where formula is a string like formula = "=IFERROR({column1}{row_number}>0;1 ..... )" and column_mapping = { v: excel_column(k) for k, v in enumerate(ordered_attributes, 1) }
63,072,954
file written by df.to_csv cannot be read by pd.read_csv
<p>I have a dataframe I read from json, process, and save to csv. The df looks fine in terms of shape and df.head(), tail look fine. I write to a csv then read and get an error -</p> <pre><code>df = get_json(params) df.to_csv(f'fname.csv') testdf = pd.read_csv(f'fname.csv') ParserError: Error tokenizing data. C error: Buffer overflow caught - possible malformed input file. </code></pre> <p>Is there some 'careful write' I should be doing?</p>
63,073,158
"2020-07-24T11:49:13.063000"
2
null
2
706
python|pandas
<p>As per my experience with this issue, there was some problem with the default engine and doing this helped me:</p> <pre class="lang-py prettyprint-override"><code>df.read_csv('input.csv', engine = 'python') </code></pre>
"2020-07-24T12:02:07.680000"
0
https://pandas.pydata.org/docs/user_guide/io.html
IO tools (text, CSV, HDF5, …)# As per my experience with this issue, there was some problem with the default engine and doing this helped me: df.read_csv('input.csv', engine = 'python') IO tools (text, CSV, HDF5, …)# The pandas I/O API is a set of top level reader functions accessed like pandas.read_csv() that generally return a pandas object. The corresponding writer functions are object methods that are accessed like DataFrame.to_csv(). Below is a table containing available readers and writers. Format Type Data Description Reader Writer text CSV read_csv to_csv text Fixed-Width Text File read_fwf text JSON read_json to_json text HTML read_html to_html text LaTeX Styler.to_latex text XML read_xml to_xml text Local clipboard read_clipboard to_clipboard binary MS Excel read_excel to_excel binary OpenDocument read_excel binary HDF5 Format read_hdf to_hdf binary Feather Format read_feather to_feather binary Parquet Format read_parquet to_parquet binary ORC Format read_orc to_orc binary Stata read_stata to_stata binary SAS read_sas binary SPSS read_spss binary Python Pickle Format read_pickle to_pickle SQL SQL read_sql to_sql SQL Google BigQuery read_gbq to_gbq Here is an informal performance comparison for some of these IO methods. Note For examples that use the StringIO class, make sure you import it with from io import StringIO for Python 3. CSV & text files# The workhorse function for reading text files (a.k.a. flat files) is read_csv(). See the cookbook for some advanced strategies. Parsing options# read_csv() accepts the following common arguments: Basic# filepath_or_buffervariousEither a path to a file (a str, pathlib.Path, or py:py._path.local.LocalPath), URL (including http, ftp, and S3 locations), or any object with a read() method (such as an open file or StringIO). sepstr, defaults to ',' for read_csv(), \t for read_table()Delimiter to use. If sep is None, the C engine cannot automatically detect the separator, but the Python parsing engine can, meaning the latter will be used and automatically detect the separator by Python’s builtin sniffer tool, csv.Sniffer. In addition, separators longer than 1 character and different from '\s+' will be interpreted as regular expressions and will also force the use of the Python parsing engine. Note that regex delimiters are prone to ignoring quoted data. Regex example: '\\r\\t'. delimiterstr, default NoneAlternative argument name for sep. delim_whitespaceboolean, default FalseSpecifies whether or not whitespace (e.g. ' ' or '\t') will be used as the delimiter. Equivalent to setting sep='\s+'. If this option is set to True, nothing should be passed in for the delimiter parameter. Column and index locations and names# headerint or list of ints, default 'infer'Row number(s) to use as the column names, and the start of the data. Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first line of the file, if column names are passed explicitly then the behavior is identical to header=None. Explicitly pass header=0 to be able to replace existing names. The header can be a list of ints that specify row locations for a MultiIndex on the columns e.g. [0,1,3]. Intervening rows that are not specified will be skipped (e.g. 2 in this example is skipped). Note that this parameter ignores commented lines and empty lines if skip_blank_lines=True, so header=0 denotes the first line of data rather than the first line of the file. namesarray-like, default NoneList of column names to use. If file contains no header row, then you should explicitly pass header=None. Duplicates in this list are not allowed. index_colint, str, sequence of int / str, or False, optional, default NoneColumn(s) to use as the row labels of the DataFrame, either given as string name or column index. If a sequence of int / str is given, a MultiIndex is used. Note index_col=False can be used to force pandas to not use the first column as the index, e.g. when you have a malformed file with delimiters at the end of each line. The default value of None instructs pandas to guess. If the number of fields in the column header row is equal to the number of fields in the body of the data file, then a default index is used. If it is larger, then the first columns are used as index so that the remaining number of fields in the body are equal to the number of fields in the header. The first row after the header is used to determine the number of columns, which will go into the index. If the subsequent rows contain less columns than the first row, they are filled with NaN. This can be avoided through usecols. This ensures that the columns are taken as is and the trailing data are ignored. usecolslist-like or callable, default NoneReturn a subset of the columns. If list-like, all elements must either be positional (i.e. integer indices into the document columns) or strings that correspond to column names provided either by the user in names or inferred from the document header row(s). If names are given, the document header row(s) are not taken into account. For example, a valid list-like usecols parameter would be [0, 1, 2] or ['foo', 'bar', 'baz']. Element order is ignored, so usecols=[0, 1] is the same as [1, 0]. To instantiate a DataFrame from data with element order preserved use pd.read_csv(data, usecols=['foo', 'bar'])[['foo', 'bar']] for columns in ['foo', 'bar'] order or pd.read_csv(data, usecols=['foo', 'bar'])[['bar', 'foo']] for ['bar', 'foo'] order. If callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True: In [1]: import pandas as pd In [2]: from io import StringIO In [3]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [4]: pd.read_csv(StringIO(data)) Out[4]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [5]: pd.read_csv(StringIO(data), usecols=lambda x: x.upper() in ["COL1", "COL3"]) Out[5]: col1 col3 0 a 1 1 a 2 2 c 3 Using this parameter results in much faster parsing time and lower memory usage when using the c engine. The Python engine loads the data first before deciding which columns to drop. squeezeboolean, default FalseIf the parsed data only contains one column then return a Series. Deprecated since version 1.4.0: Append .squeeze("columns") to the call to {func_name} to squeeze the data. prefixstr, default NonePrefix to add to column numbers when no header, e.g. ‘X’ for X0, X1, … Deprecated since version 1.4.0: Use a list comprehension on the DataFrame’s columns after calling read_csv. In [6]: data = "col1,col2,col3\na,b,1" In [7]: df = pd.read_csv(StringIO(data)) In [8]: df.columns = [f"pre_{col}" for col in df.columns] In [9]: df Out[9]: pre_col1 pre_col2 pre_col3 0 a b 1 mangle_dupe_colsboolean, default TrueDuplicate columns will be specified as ‘X’, ‘X.1’…’X.N’, rather than ‘X’…’X’. Passing in False will cause data to be overwritten if there are duplicate names in the columns. Deprecated since version 1.5.0: The argument was never implemented, and a new argument where the renaming pattern can be specified will be added instead. General parsing configuration# dtypeType name or dict of column -> type, default NoneData type for data or columns. E.g. {'a': np.float64, 'b': np.int32, 'c': 'Int64'} Use str or object together with suitable na_values settings to preserve and not interpret dtype. If converters are specified, they will be applied INSTEAD of dtype conversion. New in version 1.5.0: Support for defaultdict was added. Specify a defaultdict as input where the default determines the dtype of the columns which are not explicitly listed. engine{'c', 'python', 'pyarrow'}Parser engine to use. The C and pyarrow engines are faster, while the python engine is currently more feature-complete. Multithreading is currently only supported by the pyarrow engine. New in version 1.4.0: The “pyarrow” engine was added as an experimental engine, and some features are unsupported, or may not work correctly, with this engine. convertersdict, default NoneDict of functions for converting values in certain columns. Keys can either be integers or column labels. true_valueslist, default NoneValues to consider as True. false_valueslist, default NoneValues to consider as False. skipinitialspaceboolean, default FalseSkip spaces after delimiter. skiprowslist-like or integer, default NoneLine numbers to skip (0-indexed) or number of lines to skip (int) at the start of the file. If callable, the callable function will be evaluated against the row indices, returning True if the row should be skipped and False otherwise: In [10]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [11]: pd.read_csv(StringIO(data)) Out[11]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [12]: pd.read_csv(StringIO(data), skiprows=lambda x: x % 2 != 0) Out[12]: col1 col2 col3 0 a b 2 skipfooterint, default 0Number of lines at bottom of file to skip (unsupported with engine=’c’). nrowsint, default NoneNumber of rows of file to read. Useful for reading pieces of large files. low_memoryboolean, default TrueInternally process the file in chunks, resulting in lower memory use while parsing, but possibly mixed type inference. To ensure no mixed types either set False, or specify the type with the dtype parameter. Note that the entire file is read into a single DataFrame regardless, use the chunksize or iterator parameter to return the data in chunks. (Only valid with C parser) memory_mapboolean, default FalseIf a filepath is provided for filepath_or_buffer, map the file object directly onto memory and access the data directly from there. Using this option can improve performance because there is no longer any I/O overhead. NA and missing data handling# na_valuesscalar, str, list-like, or dict, default NoneAdditional strings to recognize as NA/NaN. If dict passed, specific per-column NA values. See na values const below for a list of the values interpreted as NaN by default. keep_default_naboolean, default TrueWhether or not to include the default NaN values when parsing the data. Depending on whether na_values is passed in, the behavior is as follows: If keep_default_na is True, and na_values are specified, na_values is appended to the default NaN values used for parsing. If keep_default_na is True, and na_values are not specified, only the default NaN values are used for parsing. If keep_default_na is False, and na_values are specified, only the NaN values specified na_values are used for parsing. If keep_default_na is False, and na_values are not specified, no strings will be parsed as NaN. Note that if na_filter is passed in as False, the keep_default_na and na_values parameters will be ignored. na_filterboolean, default TrueDetect missing value markers (empty strings and the value of na_values). In data without any NAs, passing na_filter=False can improve the performance of reading a large file. verboseboolean, default FalseIndicate number of NA values placed in non-numeric columns. skip_blank_linesboolean, default TrueIf True, skip over blank lines rather than interpreting as NaN values. Datetime handling# parse_datesboolean or list of ints or names or list of lists or dict, default False. If True -> try parsing the index. If [1, 2, 3] -> try parsing columns 1, 2, 3 each as a separate date column. If [[1, 3]] -> combine columns 1 and 3 and parse as a single date column. If {'foo': [1, 3]} -> parse columns 1, 3 as date and call result ‘foo’. Note A fast-path exists for iso8601-formatted dates. infer_datetime_formatboolean, default FalseIf True and parse_dates is enabled for a column, attempt to infer the datetime format to speed up the processing. keep_date_colboolean, default FalseIf True and parse_dates specifies combining multiple columns then keep the original columns. date_parserfunction, default NoneFunction to use for converting a sequence of string columns to an array of datetime instances. The default uses dateutil.parser.parser to do the conversion. pandas will try to call date_parser in three different ways, advancing to the next if an exception occurs: 1) Pass one or more arrays (as defined by parse_dates) as arguments; 2) concatenate (row-wise) the string values from the columns defined by parse_dates into a single array and pass that; and 3) call date_parser once for each row using one or more strings (corresponding to the columns defined by parse_dates) as arguments. dayfirstboolean, default FalseDD/MM format dates, international and European format. cache_datesboolean, default TrueIf True, use a cache of unique, converted dates to apply the datetime conversion. May produce significant speed-up when parsing duplicate date strings, especially ones with timezone offsets. New in version 0.25.0. Iteration# iteratorboolean, default FalseReturn TextFileReader object for iteration or getting chunks with get_chunk(). chunksizeint, default NoneReturn TextFileReader object for iteration. See iterating and chunking below. Quoting, compression, and file format# compression{'infer', 'gzip', 'bz2', 'zip', 'xz', 'zstd', None, dict}, default 'infer'For on-the-fly decompression of on-disk data. If ‘infer’, then use gzip, bz2, zip, xz, or zstandard if filepath_or_buffer is path-like ending in ‘.gz’, ‘.bz2’, ‘.zip’, ‘.xz’, ‘.zst’, respectively, and no decompression otherwise. If using ‘zip’, the ZIP file must contain only one data file to be read in. Set to None for no decompression. Can also be a dict with key 'method' set to one of {'zip', 'gzip', 'bz2', 'zstd'} and other key-value pairs are forwarded to zipfile.ZipFile, gzip.GzipFile, bz2.BZ2File, or zstandard.ZstdDecompressor. As an example, the following could be passed for faster compression and to create a reproducible gzip archive: compression={'method': 'gzip', 'compresslevel': 1, 'mtime': 1}. Changed in version 1.1.0: dict option extended to support gzip and bz2. Changed in version 1.2.0: Previous versions forwarded dict entries for ‘gzip’ to gzip.open. thousandsstr, default NoneThousands separator. decimalstr, default '.'Character to recognize as decimal point. E.g. use ',' for European data. float_precisionstring, default NoneSpecifies which converter the C engine should use for floating-point values. The options are None for the ordinary converter, high for the high-precision converter, and round_trip for the round-trip converter. lineterminatorstr (length 1), default NoneCharacter to break file into lines. Only valid with C parser. quotecharstr (length 1)The character used to denote the start and end of a quoted item. Quoted items can include the delimiter and it will be ignored. quotingint or csv.QUOTE_* instance, default 0Control field quoting behavior per csv.QUOTE_* constants. Use one of QUOTE_MINIMAL (0), QUOTE_ALL (1), QUOTE_NONNUMERIC (2) or QUOTE_NONE (3). doublequoteboolean, default TrueWhen quotechar is specified and quoting is not QUOTE_NONE, indicate whether or not to interpret two consecutive quotechar elements inside a field as a single quotechar element. escapecharstr (length 1), default NoneOne-character string used to escape delimiter when quoting is QUOTE_NONE. commentstr, default NoneIndicates remainder of line should not be parsed. If found at the beginning of a line, the line will be ignored altogether. This parameter must be a single character. Like empty lines (as long as skip_blank_lines=True), fully commented lines are ignored by the parameter header but not by skiprows. For example, if comment='#', parsing ‘#empty\na,b,c\n1,2,3’ with header=0 will result in ‘a,b,c’ being treated as the header. encodingstr, default NoneEncoding to use for UTF when reading/writing (e.g. 'utf-8'). List of Python standard encodings. dialectstr or csv.Dialect instance, default NoneIf provided, this parameter will override values (default or not) for the following parameters: delimiter, doublequote, escapechar, skipinitialspace, quotechar, and quoting. If it is necessary to override values, a ParserWarning will be issued. See csv.Dialect documentation for more details. Error handling# error_bad_linesboolean, optional, default NoneLines with too many fields (e.g. a csv line with too many commas) will by default cause an exception to be raised, and no DataFrame will be returned. If False, then these “bad lines” will dropped from the DataFrame that is returned. See bad lines below. Deprecated since version 1.3.0: The on_bad_lines parameter should be used instead to specify behavior upon encountering a bad line instead. warn_bad_linesboolean, optional, default NoneIf error_bad_lines is False, and warn_bad_lines is True, a warning for each “bad line” will be output. Deprecated since version 1.3.0: The on_bad_lines parameter should be used instead to specify behavior upon encountering a bad line instead. on_bad_lines(‘error’, ‘warn’, ‘skip’), default ‘error’Specifies what to do upon encountering a bad line (a line with too many fields). Allowed values are : ‘error’, raise an ParserError when a bad line is encountered. ‘warn’, print a warning when a bad line is encountered and skip that line. ‘skip’, skip bad lines without raising or warning when they are encountered. New in version 1.3.0. Specifying column data types# You can indicate the data type for the whole DataFrame or individual columns: In [13]: import numpy as np In [14]: data = "a,b,c,d\n1,2,3,4\n5,6,7,8\n9,10,11" In [15]: print(data) a,b,c,d 1,2,3,4 5,6,7,8 9,10,11 In [16]: df = pd.read_csv(StringIO(data), dtype=object) In [17]: df Out[17]: a b c d 0 1 2 3 4 1 5 6 7 8 2 9 10 11 NaN In [18]: df["a"][0] Out[18]: '1' In [19]: df = pd.read_csv(StringIO(data), dtype={"b": object, "c": np.float64, "d": "Int64"}) In [20]: df.dtypes Out[20]: a int64 b object c float64 d Int64 dtype: object Fortunately, pandas offers more than one way to ensure that your column(s) contain only one dtype. If you’re unfamiliar with these concepts, you can see here to learn more about dtypes, and here to learn more about object conversion in pandas. For instance, you can use the converters argument of read_csv(): In [21]: data = "col_1\n1\n2\n'A'\n4.22" In [22]: df = pd.read_csv(StringIO(data), converters={"col_1": str}) In [23]: df Out[23]: col_1 0 1 1 2 2 'A' 3 4.22 In [24]: df["col_1"].apply(type).value_counts() Out[24]: <class 'str'> 4 Name: col_1, dtype: int64 Or you can use the to_numeric() function to coerce the dtypes after reading in the data, In [25]: df2 = pd.read_csv(StringIO(data)) In [26]: df2["col_1"] = pd.to_numeric(df2["col_1"], errors="coerce") In [27]: df2 Out[27]: col_1 0 1.00 1 2.00 2 NaN 3 4.22 In [28]: df2["col_1"].apply(type).value_counts() Out[28]: <class 'float'> 4 Name: col_1, dtype: int64 which will convert all valid parsing to floats, leaving the invalid parsing as NaN. Ultimately, how you deal with reading in columns containing mixed dtypes depends on your specific needs. In the case above, if you wanted to NaN out the data anomalies, then to_numeric() is probably your best option. However, if you wanted for all the data to be coerced, no matter the type, then using the converters argument of read_csv() would certainly be worth trying. Note In some cases, reading in abnormal data with columns containing mixed dtypes will result in an inconsistent dataset. If you rely on pandas to infer the dtypes of your columns, the parsing engine will go and infer the dtypes for different chunks of the data, rather than the whole dataset at once. Consequently, you can end up with column(s) with mixed dtypes. For example, In [29]: col_1 = list(range(500000)) + ["a", "b"] + list(range(500000)) In [30]: df = pd.DataFrame({"col_1": col_1}) In [31]: df.to_csv("foo.csv") In [32]: mixed_df = pd.read_csv("foo.csv") In [33]: mixed_df["col_1"].apply(type).value_counts() Out[33]: <class 'int'> 737858 <class 'str'> 262144 Name: col_1, dtype: int64 In [34]: mixed_df["col_1"].dtype Out[34]: dtype('O') will result with mixed_df containing an int dtype for certain chunks of the column, and str for others due to the mixed dtypes from the data that was read in. It is important to note that the overall column will be marked with a dtype of object, which is used for columns with mixed dtypes. Specifying categorical dtype# Categorical columns can be parsed directly by specifying dtype='category' or dtype=CategoricalDtype(categories, ordered). In [35]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [36]: pd.read_csv(StringIO(data)) Out[36]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [37]: pd.read_csv(StringIO(data)).dtypes Out[37]: col1 object col2 object col3 int64 dtype: object In [38]: pd.read_csv(StringIO(data), dtype="category").dtypes Out[38]: col1 category col2 category col3 category dtype: object Individual columns can be parsed as a Categorical using a dict specification: In [39]: pd.read_csv(StringIO(data), dtype={"col1": "category"}).dtypes Out[39]: col1 category col2 object col3 int64 dtype: object Specifying dtype='category' will result in an unordered Categorical whose categories are the unique values observed in the data. For more control on the categories and order, create a CategoricalDtype ahead of time, and pass that for that column’s dtype. In [40]: from pandas.api.types import CategoricalDtype In [41]: dtype = CategoricalDtype(["d", "c", "b", "a"], ordered=True) In [42]: pd.read_csv(StringIO(data), dtype={"col1": dtype}).dtypes Out[42]: col1 category col2 object col3 int64 dtype: object When using dtype=CategoricalDtype, “unexpected” values outside of dtype.categories are treated as missing values. In [43]: dtype = CategoricalDtype(["a", "b", "d"]) # No 'c' In [44]: pd.read_csv(StringIO(data), dtype={"col1": dtype}).col1 Out[44]: 0 a 1 a 2 NaN Name: col1, dtype: category Categories (3, object): ['a', 'b', 'd'] This matches the behavior of Categorical.set_categories(). Note With dtype='category', the resulting categories will always be parsed as strings (object dtype). If the categories are numeric they can be converted using the to_numeric() function, or as appropriate, another converter such as to_datetime(). When dtype is a CategoricalDtype with homogeneous categories ( all numeric, all datetimes, etc.), the conversion is done automatically. In [45]: df = pd.read_csv(StringIO(data), dtype="category") In [46]: df.dtypes Out[46]: col1 category col2 category col3 category dtype: object In [47]: df["col3"] Out[47]: 0 1 1 2 2 3 Name: col3, dtype: category Categories (3, object): ['1', '2', '3'] In [48]: new_categories = pd.to_numeric(df["col3"].cat.categories) In [49]: df["col3"] = df["col3"].cat.rename_categories(new_categories) In [50]: df["col3"] Out[50]: 0 1 1 2 2 3 Name: col3, dtype: category Categories (3, int64): [1, 2, 3] Naming and using columns# Handling column names# A file may or may not have a header row. pandas assumes the first row should be used as the column names: In [51]: data = "a,b,c\n1,2,3\n4,5,6\n7,8,9" In [52]: print(data) a,b,c 1,2,3 4,5,6 7,8,9 In [53]: pd.read_csv(StringIO(data)) Out[53]: a b c 0 1 2 3 1 4 5 6 2 7 8 9 By specifying the names argument in conjunction with header you can indicate other names to use and whether or not to throw away the header row (if any): In [54]: print(data) a,b,c 1,2,3 4,5,6 7,8,9 In [55]: pd.read_csv(StringIO(data), names=["foo", "bar", "baz"], header=0) Out[55]: foo bar baz 0 1 2 3 1 4 5 6 2 7 8 9 In [56]: pd.read_csv(StringIO(data), names=["foo", "bar", "baz"], header=None) Out[56]: foo bar baz 0 a b c 1 1 2 3 2 4 5 6 3 7 8 9 If the header is in a row other than the first, pass the row number to header. This will skip the preceding rows: In [57]: data = "skip this skip it\na,b,c\n1,2,3\n4,5,6\n7,8,9" In [58]: pd.read_csv(StringIO(data), header=1) Out[58]: a b c 0 1 2 3 1 4 5 6 2 7 8 9 Note Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first non-blank line of the file, if column names are passed explicitly then the behavior is identical to header=None. Duplicate names parsing# Deprecated since version 1.5.0: mangle_dupe_cols was never implemented, and a new argument where the renaming pattern can be specified will be added instead. If the file or header contains duplicate names, pandas will by default distinguish between them so as to prevent overwriting data: In [59]: data = "a,b,a\n0,1,2\n3,4,5" In [60]: pd.read_csv(StringIO(data)) Out[60]: a b a.1 0 0 1 2 1 3 4 5 There is no more duplicate data because mangle_dupe_cols=True by default, which modifies a series of duplicate columns ‘X’, …, ‘X’ to become ‘X’, ‘X.1’, …, ‘X.N’. Filtering columns (usecols)# The usecols argument allows you to select any subset of the columns in a file, either using the column names, position numbers or a callable: In [61]: data = "a,b,c,d\n1,2,3,foo\n4,5,6,bar\n7,8,9,baz" In [62]: pd.read_csv(StringIO(data)) Out[62]: a b c d 0 1 2 3 foo 1 4 5 6 bar 2 7 8 9 baz In [63]: pd.read_csv(StringIO(data), usecols=["b", "d"]) Out[63]: b d 0 2 foo 1 5 bar 2 8 baz In [64]: pd.read_csv(StringIO(data), usecols=[0, 2, 3]) Out[64]: a c d 0 1 3 foo 1 4 6 bar 2 7 9 baz In [65]: pd.read_csv(StringIO(data), usecols=lambda x: x.upper() in ["A", "C"]) Out[65]: a c 0 1 3 1 4 6 2 7 9 The usecols argument can also be used to specify which columns not to use in the final result: In [66]: pd.read_csv(StringIO(data), usecols=lambda x: x not in ["a", "c"]) Out[66]: b d 0 2 foo 1 5 bar 2 8 baz In this case, the callable is specifying that we exclude the “a” and “c” columns from the output. Comments and empty lines# Ignoring line comments and empty lines# If the comment parameter is specified, then completely commented lines will be ignored. By default, completely blank lines will be ignored as well. In [67]: data = "\na,b,c\n \n# commented line\n1,2,3\n\n4,5,6" In [68]: print(data) a,b,c # commented line 1,2,3 4,5,6 In [69]: pd.read_csv(StringIO(data), comment="#") Out[69]: a b c 0 1 2 3 1 4 5 6 If skip_blank_lines=False, then read_csv will not ignore blank lines: In [70]: data = "a,b,c\n\n1,2,3\n\n\n4,5,6" In [71]: pd.read_csv(StringIO(data), skip_blank_lines=False) Out[71]: a b c 0 NaN NaN NaN 1 1.0 2.0 3.0 2 NaN NaN NaN 3 NaN NaN NaN 4 4.0 5.0 6.0 Warning The presence of ignored lines might create ambiguities involving line numbers; the parameter header uses row numbers (ignoring commented/empty lines), while skiprows uses line numbers (including commented/empty lines): In [72]: data = "#comment\na,b,c\nA,B,C\n1,2,3" In [73]: pd.read_csv(StringIO(data), comment="#", header=1) Out[73]: A B C 0 1 2 3 In [74]: data = "A,B,C\n#comment\na,b,c\n1,2,3" In [75]: pd.read_csv(StringIO(data), comment="#", skiprows=2) Out[75]: a b c 0 1 2 3 If both header and skiprows are specified, header will be relative to the end of skiprows. For example: In [76]: data = ( ....: "# empty\n" ....: "# second empty line\n" ....: "# third emptyline\n" ....: "X,Y,Z\n" ....: "1,2,3\n" ....: "A,B,C\n" ....: "1,2.,4.\n" ....: "5.,NaN,10.0\n" ....: ) ....: In [77]: print(data) # empty # second empty line # third emptyline X,Y,Z 1,2,3 A,B,C 1,2.,4. 5.,NaN,10.0 In [78]: pd.read_csv(StringIO(data), comment="#", skiprows=4, header=1) Out[78]: A B C 0 1.0 2.0 4.0 1 5.0 NaN 10.0 Comments# Sometimes comments or meta data may be included in a file: In [79]: print(open("tmp.csv").read()) ID,level,category Patient1,123000,x # really unpleasant Patient2,23000,y # wouldn't take his medicine Patient3,1234018,z # awesome By default, the parser includes the comments in the output: In [80]: df = pd.read_csv("tmp.csv") In [81]: df Out[81]: ID level category 0 Patient1 123000 x # really unpleasant 1 Patient2 23000 y # wouldn't take his medicine 2 Patient3 1234018 z # awesome We can suppress the comments using the comment keyword: In [82]: df = pd.read_csv("tmp.csv", comment="#") In [83]: df Out[83]: ID level category 0 Patient1 123000 x 1 Patient2 23000 y 2 Patient3 1234018 z Dealing with Unicode data# The encoding argument should be used for encoded unicode data, which will result in byte strings being decoded to unicode in the result: In [84]: from io import BytesIO In [85]: data = b"word,length\n" b"Tr\xc3\xa4umen,7\n" b"Gr\xc3\xbc\xc3\x9fe,5" In [86]: data = data.decode("utf8").encode("latin-1") In [87]: df = pd.read_csv(BytesIO(data), encoding="latin-1") In [88]: df Out[88]: word length 0 Träumen 7 1 Grüße 5 In [89]: df["word"][1] Out[89]: 'Grüße' Some formats which encode all characters as multiple bytes, like UTF-16, won’t parse correctly at all without specifying the encoding. Full list of Python standard encodings. Index columns and trailing delimiters# If a file has one more column of data than the number of column names, the first column will be used as the DataFrame’s row names: In [90]: data = "a,b,c\n4,apple,bat,5.7\n8,orange,cow,10" In [91]: pd.read_csv(StringIO(data)) Out[91]: a b c 4 apple bat 5.7 8 orange cow 10.0 In [92]: data = "index,a,b,c\n4,apple,bat,5.7\n8,orange,cow,10" In [93]: pd.read_csv(StringIO(data), index_col=0) Out[93]: a b c index 4 apple bat 5.7 8 orange cow 10.0 Ordinarily, you can achieve this behavior using the index_col option. There are some exception cases when a file has been prepared with delimiters at the end of each data line, confusing the parser. To explicitly disable the index column inference and discard the last column, pass index_col=False: In [94]: data = "a,b,c\n4,apple,bat,\n8,orange,cow," In [95]: print(data) a,b,c 4,apple,bat, 8,orange,cow, In [96]: pd.read_csv(StringIO(data)) Out[96]: a b c 4 apple bat NaN 8 orange cow NaN In [97]: pd.read_csv(StringIO(data), index_col=False) Out[97]: a b c 0 4 apple bat 1 8 orange cow If a subset of data is being parsed using the usecols option, the index_col specification is based on that subset, not the original data. In [98]: data = "a,b,c\n4,apple,bat,\n8,orange,cow," In [99]: print(data) a,b,c 4,apple,bat, 8,orange,cow, In [100]: pd.read_csv(StringIO(data), usecols=["b", "c"]) Out[100]: b c 4 bat NaN 8 cow NaN In [101]: pd.read_csv(StringIO(data), usecols=["b", "c"], index_col=0) Out[101]: b c 4 bat NaN 8 cow NaN Date Handling# Specifying date columns# To better facilitate working with datetime data, read_csv() uses the keyword arguments parse_dates and date_parser to allow users to specify a variety of columns and date/time formats to turn the input text data into datetime objects. The simplest case is to just pass in parse_dates=True: In [102]: with open("foo.csv", mode="w") as f: .....: f.write("date,A,B,C\n20090101,a,1,2\n20090102,b,3,4\n20090103,c,4,5") .....: # Use a column as an index, and parse it as dates. In [103]: df = pd.read_csv("foo.csv", index_col=0, parse_dates=True) In [104]: df Out[104]: A B C date 2009-01-01 a 1 2 2009-01-02 b 3 4 2009-01-03 c 4 5 # These are Python datetime objects In [105]: df.index Out[105]: DatetimeIndex(['2009-01-01', '2009-01-02', '2009-01-03'], dtype='datetime64[ns]', name='date', freq=None) It is often the case that we may want to store date and time data separately, or store various date fields separately. the parse_dates keyword can be used to specify a combination of columns to parse the dates and/or times from. You can specify a list of column lists to parse_dates, the resulting date columns will be prepended to the output (so as to not affect the existing column order) and the new column names will be the concatenation of the component column names: In [106]: data = ( .....: "KORD,19990127, 19:00:00, 18:56:00, 0.8100\n" .....: "KORD,19990127, 20:00:00, 19:56:00, 0.0100\n" .....: "KORD,19990127, 21:00:00, 20:56:00, -0.5900\n" .....: "KORD,19990127, 21:00:00, 21:18:00, -0.9900\n" .....: "KORD,19990127, 22:00:00, 21:56:00, -0.5900\n" .....: "KORD,19990127, 23:00:00, 22:56:00, -0.5900" .....: ) .....: In [107]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [108]: df = pd.read_csv("tmp.csv", header=None, parse_dates=[[1, 2], [1, 3]]) In [109]: df Out[109]: 1_2 1_3 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 By default the parser removes the component date columns, but you can choose to retain them via the keep_date_col keyword: In [110]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=[[1, 2], [1, 3]], keep_date_col=True .....: ) .....: In [111]: df Out[111]: 1_2 1_3 0 ... 2 3 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD ... 19:00:00 18:56:00 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD ... 20:00:00 19:56:00 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD ... 21:00:00 20:56:00 -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD ... 21:00:00 21:18:00 -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD ... 22:00:00 21:56:00 -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD ... 23:00:00 22:56:00 -0.59 [6 rows x 7 columns] Note that if you wish to combine multiple columns into a single date column, a nested list must be used. In other words, parse_dates=[1, 2] indicates that the second and third columns should each be parsed as separate date columns while parse_dates=[[1, 2]] means the two columns should be parsed into a single column. You can also use a dict to specify custom name columns: In [112]: date_spec = {"nominal": [1, 2], "actual": [1, 3]} In [113]: df = pd.read_csv("tmp.csv", header=None, parse_dates=date_spec) In [114]: df Out[114]: nominal actual 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 It is important to remember that if multiple text columns are to be parsed into a single date column, then a new column is prepended to the data. The index_col specification is based off of this new set of columns rather than the original data columns: In [115]: date_spec = {"nominal": [1, 2], "actual": [1, 3]} In [116]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=date_spec, index_col=0 .....: ) # index is the nominal column .....: In [117]: df Out[117]: actual 0 4 nominal 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 Note If a column or index contains an unparsable date, the entire column or index will be returned unaltered as an object data type. For non-standard datetime parsing, use to_datetime() after pd.read_csv. Note read_csv has a fast_path for parsing datetime strings in iso8601 format, e.g “2000-01-01T00:01:02+00:00” and similar variations. If you can arrange for your data to store datetimes in this format, load times will be significantly faster, ~20x has been observed. Date parsing functions# Finally, the parser allows you to specify a custom date_parser function to take full advantage of the flexibility of the date parsing API: In [118]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=date_spec, date_parser=pd.to_datetime .....: ) .....: In [119]: df Out[119]: nominal actual 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 pandas will try to call the date_parser function in three different ways. If an exception is raised, the next one is tried: date_parser is first called with one or more arrays as arguments, as defined using parse_dates (e.g., date_parser(['2013', '2013'], ['1', '2'])). If #1 fails, date_parser is called with all the columns concatenated row-wise into a single array (e.g., date_parser(['2013 1', '2013 2'])). Note that performance-wise, you should try these methods of parsing dates in order: Try to infer the format using infer_datetime_format=True (see section below). If you know the format, use pd.to_datetime(): date_parser=lambda x: pd.to_datetime(x, format=...). If you have a really non-standard format, use a custom date_parser function. For optimal performance, this should be vectorized, i.e., it should accept arrays as arguments. Parsing a CSV with mixed timezones# pandas cannot natively represent a column or index with mixed timezones. If your CSV file contains columns with a mixture of timezones, the default result will be an object-dtype column with strings, even with parse_dates. In [120]: content = """\ .....: a .....: 2000-01-01T00:00:00+05:00 .....: 2000-01-01T00:00:00+06:00""" .....: In [121]: df = pd.read_csv(StringIO(content), parse_dates=["a"]) In [122]: df["a"] Out[122]: 0 2000-01-01 00:00:00+05:00 1 2000-01-01 00:00:00+06:00 Name: a, dtype: object To parse the mixed-timezone values as a datetime column, pass a partially-applied to_datetime() with utc=True as the date_parser. In [123]: df = pd.read_csv( .....: StringIO(content), .....: parse_dates=["a"], .....: date_parser=lambda col: pd.to_datetime(col, utc=True), .....: ) .....: In [124]: df["a"] Out[124]: 0 1999-12-31 19:00:00+00:00 1 1999-12-31 18:00:00+00:00 Name: a, dtype: datetime64[ns, UTC] Inferring datetime format# If you have parse_dates enabled for some or all of your columns, and your datetime strings are all formatted the same way, you may get a large speed up by setting infer_datetime_format=True. If set, pandas will attempt to guess the format of your datetime strings, and then use a faster means of parsing the strings. 5-10x parsing speeds have been observed. pandas will fallback to the usual parsing if either the format cannot be guessed or the format that was guessed cannot properly parse the entire column of strings. So in general, infer_datetime_format should not have any negative consequences if enabled. Here are some examples of datetime strings that can be guessed (All representing December 30th, 2011 at 00:00:00): “20111230” “2011/12/30” “20111230 00:00:00” “12/30/2011 00:00:00” “30/Dec/2011 00:00:00” “30/December/2011 00:00:00” Note that infer_datetime_format is sensitive to dayfirst. With dayfirst=True, it will guess “01/12/2011” to be December 1st. With dayfirst=False (default) it will guess “01/12/2011” to be January 12th. # Try to infer the format for the index column In [125]: df = pd.read_csv( .....: "foo.csv", .....: index_col=0, .....: parse_dates=True, .....: infer_datetime_format=True, .....: ) .....: In [126]: df Out[126]: A B C date 2009-01-01 a 1 2 2009-01-02 b 3 4 2009-01-03 c 4 5 International date formats# While US date formats tend to be MM/DD/YYYY, many international formats use DD/MM/YYYY instead. For convenience, a dayfirst keyword is provided: In [127]: data = "date,value,cat\n1/6/2000,5,a\n2/6/2000,10,b\n3/6/2000,15,c" In [128]: print(data) date,value,cat 1/6/2000,5,a 2/6/2000,10,b 3/6/2000,15,c In [129]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [130]: pd.read_csv("tmp.csv", parse_dates=[0]) Out[130]: date value cat 0 2000-01-06 5 a 1 2000-02-06 10 b 2 2000-03-06 15 c In [131]: pd.read_csv("tmp.csv", dayfirst=True, parse_dates=[0]) Out[131]: date value cat 0 2000-06-01 5 a 1 2000-06-02 10 b 2 2000-06-03 15 c Writing CSVs to binary file objects# New in version 1.2.0. df.to_csv(..., mode="wb") allows writing a CSV to a file object opened binary mode. In most cases, it is not necessary to specify mode as Pandas will auto-detect whether the file object is opened in text or binary mode. In [132]: import io In [133]: data = pd.DataFrame([0, 1, 2]) In [134]: buffer = io.BytesIO() In [135]: data.to_csv(buffer, encoding="utf-8", compression="gzip") Specifying method for floating-point conversion# The parameter float_precision can be specified in order to use a specific floating-point converter during parsing with the C engine. The options are the ordinary converter, the high-precision converter, and the round-trip converter (which is guaranteed to round-trip values after writing to a file). For example: In [136]: val = "0.3066101993807095471566981359501369297504425048828125" In [137]: data = "a,b,c\n1,2,{0}".format(val) In [138]: abs( .....: pd.read_csv( .....: StringIO(data), .....: engine="c", .....: float_precision=None, .....: )["c"][0] - float(val) .....: ) .....: Out[138]: 5.551115123125783e-17 In [139]: abs( .....: pd.read_csv( .....: StringIO(data), .....: engine="c", .....: float_precision="high", .....: )["c"][0] - float(val) .....: ) .....: Out[139]: 5.551115123125783e-17 In [140]: abs( .....: pd.read_csv(StringIO(data), engine="c", float_precision="round_trip")["c"][0] .....: - float(val) .....: ) .....: Out[140]: 0.0 Thousand separators# For large numbers that have been written with a thousands separator, you can set the thousands keyword to a string of length 1 so that integers will be parsed correctly: By default, numbers with a thousands separator will be parsed as strings: In [141]: data = ( .....: "ID|level|category\n" .....: "Patient1|123,000|x\n" .....: "Patient2|23,000|y\n" .....: "Patient3|1,234,018|z" .....: ) .....: In [142]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [143]: df = pd.read_csv("tmp.csv", sep="|") In [144]: df Out[144]: ID level category 0 Patient1 123,000 x 1 Patient2 23,000 y 2 Patient3 1,234,018 z In [145]: df.level.dtype Out[145]: dtype('O') The thousands keyword allows integers to be parsed correctly: In [146]: df = pd.read_csv("tmp.csv", sep="|", thousands=",") In [147]: df Out[147]: ID level category 0 Patient1 123000 x 1 Patient2 23000 y 2 Patient3 1234018 z In [148]: df.level.dtype Out[148]: dtype('int64') NA values# To control which values are parsed as missing values (which are signified by NaN), specify a string in na_values. If you specify a list of strings, then all values in it are considered to be missing values. If you specify a number (a float, like 5.0 or an integer like 5), the corresponding equivalent values will also imply a missing value (in this case effectively [5.0, 5] are recognized as NaN). To completely override the default values that are recognized as missing, specify keep_default_na=False. The default NaN recognized values are ['-1.#IND', '1.#QNAN', '1.#IND', '-1.#QNAN', '#N/A N/A', '#N/A', 'N/A', 'n/a', 'NA', '<NA>', '#NA', 'NULL', 'null', 'NaN', '-NaN', 'nan', '-nan', '']. Let us consider some examples: pd.read_csv("path_to_file.csv", na_values=[5]) In the example above 5 and 5.0 will be recognized as NaN, in addition to the defaults. A string will first be interpreted as a numerical 5, then as a NaN. pd.read_csv("path_to_file.csv", keep_default_na=False, na_values=[""]) Above, only an empty field will be recognized as NaN. pd.read_csv("path_to_file.csv", keep_default_na=False, na_values=["NA", "0"]) Above, both NA and 0 as strings are NaN. pd.read_csv("path_to_file.csv", na_values=["Nope"]) The default values, in addition to the string "Nope" are recognized as NaN. Infinity# inf like values will be parsed as np.inf (positive infinity), and -inf as -np.inf (negative infinity). These will ignore the case of the value, meaning Inf, will also be parsed as np.inf. Returning Series# Using the squeeze keyword, the parser will return output with a single column as a Series: Deprecated since version 1.4.0: Users should append .squeeze("columns") to the DataFrame returned by read_csv instead. In [149]: data = "level\nPatient1,123000\nPatient2,23000\nPatient3,1234018" In [150]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [151]: print(open("tmp.csv").read()) level Patient1,123000 Patient2,23000 Patient3,1234018 In [152]: output = pd.read_csv("tmp.csv", squeeze=True) In [153]: output Out[153]: Patient1 123000 Patient2 23000 Patient3 1234018 Name: level, dtype: int64 In [154]: type(output) Out[154]: pandas.core.series.Series Boolean values# The common values True, False, TRUE, and FALSE are all recognized as boolean. Occasionally you might want to recognize other values as being boolean. To do this, use the true_values and false_values options as follows: In [155]: data = "a,b,c\n1,Yes,2\n3,No,4" In [156]: print(data) a,b,c 1,Yes,2 3,No,4 In [157]: pd.read_csv(StringIO(data)) Out[157]: a b c 0 1 Yes 2 1 3 No 4 In [158]: pd.read_csv(StringIO(data), true_values=["Yes"], false_values=["No"]) Out[158]: a b c 0 1 True 2 1 3 False 4 Handling “bad” lines# Some files may have malformed lines with too few fields or too many. Lines with too few fields will have NA values filled in the trailing fields. Lines with too many fields will raise an error by default: In [159]: data = "a,b,c\n1,2,3\n4,5,6,7\n8,9,10" In [160]: pd.read_csv(StringIO(data)) --------------------------------------------------------------------------- ParserError Traceback (most recent call last) Cell In[160], line 1 ----> 1 pd.read_csv(StringIO(data)) File ~/work/pandas/pandas/pandas/util/_decorators.py:211, in deprecate_kwarg.<locals>._deprecate_kwarg.<locals>.wrapper(*args, **kwargs) 209 else: 210 kwargs[new_arg_name] = new_arg_value --> 211 return func(*args, **kwargs) File ~/work/pandas/pandas/pandas/util/_decorators.py:331, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs) 325 if len(args) > num_allow_args: 326 warnings.warn( 327 msg.format(arguments=_format_argument_list(allow_args)), 328 FutureWarning, 329 stacklevel=find_stack_level(), 330 ) --> 331 return func(*args, **kwargs) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:950, in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, error_bad_lines, warn_bad_lines, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options) 935 kwds_defaults = _refine_defaults_read( 936 dialect, 937 delimiter, (...) 946 defaults={"delimiter": ","}, 947 ) 948 kwds.update(kwds_defaults) --> 950 return _read(filepath_or_buffer, kwds) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:611, in _read(filepath_or_buffer, kwds) 608 return parser 610 with parser: --> 611 return parser.read(nrows) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:1778, in TextFileReader.read(self, nrows) 1771 nrows = validate_integer("nrows", nrows) 1772 try: 1773 # error: "ParserBase" has no attribute "read" 1774 ( 1775 index, 1776 columns, 1777 col_dict, -> 1778 ) = self._engine.read( # type: ignore[attr-defined] 1779 nrows 1780 ) 1781 except Exception: 1782 self.close() File ~/work/pandas/pandas/pandas/io/parsers/c_parser_wrapper.py:230, in CParserWrapper.read(self, nrows) 228 try: 229 if self.low_memory: --> 230 chunks = self._reader.read_low_memory(nrows) 231 # destructive to chunks 232 data = _concatenate_chunks(chunks) File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:808, in pandas._libs.parsers.TextReader.read_low_memory() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:866, in pandas._libs.parsers.TextReader._read_rows() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:852, in pandas._libs.parsers.TextReader._tokenize_rows() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:1973, in pandas._libs.parsers.raise_parser_error() ParserError: Error tokenizing data. C error: Expected 3 fields in line 3, saw 4 You can elect to skip bad lines: In [29]: pd.read_csv(StringIO(data), on_bad_lines="warn") Skipping line 3: expected 3 fields, saw 4 Out[29]: a b c 0 1 2 3 1 8 9 10 Or pass a callable function to handle the bad line if engine="python". The bad line will be a list of strings that was split by the sep: In [29]: external_list = [] In [30]: def bad_lines_func(line): ...: external_list.append(line) ...: return line[-3:] In [31]: pd.read_csv(StringIO(data), on_bad_lines=bad_lines_func, engine="python") Out[31]: a b c 0 1 2 3 1 5 6 7 2 8 9 10 In [32]: external_list Out[32]: [4, 5, 6, 7] .. versionadded:: 1.4.0 You can also use the usecols parameter to eliminate extraneous column data that appear in some lines but not others: In [33]: pd.read_csv(StringIO(data), usecols=[0, 1, 2]) Out[33]: a b c 0 1 2 3 1 4 5 6 2 8 9 10 In case you want to keep all data including the lines with too many fields, you can specify a sufficient number of names. This ensures that lines with not enough fields are filled with NaN. In [34]: pd.read_csv(StringIO(data), names=['a', 'b', 'c', 'd']) Out[34]: a b c d 0 1 2 3 NaN 1 4 5 6 7 2 8 9 10 NaN Dialect# The dialect keyword gives greater flexibility in specifying the file format. By default it uses the Excel dialect but you can specify either the dialect name or a csv.Dialect instance. Suppose you had data with unenclosed quotes: In [161]: data = "label1,label2,label3\n" 'index1,"a,c,e\n' "index2,b,d,f" In [162]: print(data) label1,label2,label3 index1,"a,c,e index2,b,d,f By default, read_csv uses the Excel dialect and treats the double quote as the quote character, which causes it to fail when it finds a newline before it finds the closing double quote. We can get around this using dialect: In [163]: import csv In [164]: dia = csv.excel() In [165]: dia.quoting = csv.QUOTE_NONE In [166]: pd.read_csv(StringIO(data), dialect=dia) Out[166]: label1 label2 label3 index1 "a c e index2 b d f All of the dialect options can be specified separately by keyword arguments: In [167]: data = "a,b,c~1,2,3~4,5,6" In [168]: pd.read_csv(StringIO(data), lineterminator="~") Out[168]: a b c 0 1 2 3 1 4 5 6 Another common dialect option is skipinitialspace, to skip any whitespace after a delimiter: In [169]: data = "a, b, c\n1, 2, 3\n4, 5, 6" In [170]: print(data) a, b, c 1, 2, 3 4, 5, 6 In [171]: pd.read_csv(StringIO(data), skipinitialspace=True) Out[171]: a b c 0 1 2 3 1 4 5 6 The parsers make every attempt to “do the right thing” and not be fragile. Type inference is a pretty big deal. If a column can be coerced to integer dtype without altering the contents, the parser will do so. Any non-numeric columns will come through as object dtype as with the rest of pandas objects. Quoting and Escape Characters# Quotes (and other escape characters) in embedded fields can be handled in any number of ways. One way is to use backslashes; to properly parse this data, you should pass the escapechar option: In [172]: data = 'a,b\n"hello, \\"Bob\\", nice to see you",5' In [173]: print(data) a,b "hello, \"Bob\", nice to see you",5 In [174]: pd.read_csv(StringIO(data), escapechar="\\") Out[174]: a b 0 hello, "Bob", nice to see you 5 Files with fixed width columns# While read_csv() reads delimited data, the read_fwf() function works with data files that have known and fixed column widths. The function parameters to read_fwf are largely the same as read_csv with two extra parameters, and a different usage of the delimiter parameter: colspecs: A list of pairs (tuples) giving the extents of the fixed-width fields of each line as half-open intervals (i.e., [from, to[ ). String value ‘infer’ can be used to instruct the parser to try detecting the column specifications from the first 100 rows of the data. Default behavior, if not specified, is to infer. widths: A list of field widths which can be used instead of ‘colspecs’ if the intervals are contiguous. delimiter: Characters to consider as filler characters in the fixed-width file. Can be used to specify the filler character of the fields if it is not spaces (e.g., ‘~’). Consider a typical fixed-width data file: In [175]: data1 = ( .....: "id8141 360.242940 149.910199 11950.7\n" .....: "id1594 444.953632 166.985655 11788.4\n" .....: "id1849 364.136849 183.628767 11806.2\n" .....: "id1230 413.836124 184.375703 11916.8\n" .....: "id1948 502.953953 173.237159 12468.3" .....: ) .....: In [176]: with open("bar.csv", "w") as f: .....: f.write(data1) .....: In order to parse this file into a DataFrame, we simply need to supply the column specifications to the read_fwf function along with the file name: # Column specifications are a list of half-intervals In [177]: colspecs = [(0, 6), (8, 20), (21, 33), (34, 43)] In [178]: df = pd.read_fwf("bar.csv", colspecs=colspecs, header=None, index_col=0) In [179]: df Out[179]: 1 2 3 0 id8141 360.242940 149.910199 11950.7 id1594 444.953632 166.985655 11788.4 id1849 364.136849 183.628767 11806.2 id1230 413.836124 184.375703 11916.8 id1948 502.953953 173.237159 12468.3 Note how the parser automatically picks column names X.<column number> when header=None argument is specified. Alternatively, you can supply just the column widths for contiguous columns: # Widths are a list of integers In [180]: widths = [6, 14, 13, 10] In [181]: df = pd.read_fwf("bar.csv", widths=widths, header=None) In [182]: df Out[182]: 0 1 2 3 0 id8141 360.242940 149.910199 11950.7 1 id1594 444.953632 166.985655 11788.4 2 id1849 364.136849 183.628767 11806.2 3 id1230 413.836124 184.375703 11916.8 4 id1948 502.953953 173.237159 12468.3 The parser will take care of extra white spaces around the columns so it’s ok to have extra separation between the columns in the file. By default, read_fwf will try to infer the file’s colspecs by using the first 100 rows of the file. It can do it only in cases when the columns are aligned and correctly separated by the provided delimiter (default delimiter is whitespace). In [183]: df = pd.read_fwf("bar.csv", header=None, index_col=0) In [184]: df Out[184]: 1 2 3 0 id8141 360.242940 149.910199 11950.7 id1594 444.953632 166.985655 11788.4 id1849 364.136849 183.628767 11806.2 id1230 413.836124 184.375703 11916.8 id1948 502.953953 173.237159 12468.3 read_fwf supports the dtype parameter for specifying the types of parsed columns to be different from the inferred type. In [185]: pd.read_fwf("bar.csv", header=None, index_col=0).dtypes Out[185]: 1 float64 2 float64 3 float64 dtype: object In [186]: pd.read_fwf("bar.csv", header=None, dtype={2: "object"}).dtypes Out[186]: 0 object 1 float64 2 object 3 float64 dtype: object Indexes# Files with an “implicit” index column# Consider a file with one less entry in the header than the number of data column: In [187]: data = "A,B,C\n20090101,a,1,2\n20090102,b,3,4\n20090103,c,4,5" In [188]: print(data) A,B,C 20090101,a,1,2 20090102,b,3,4 20090103,c,4,5 In [189]: with open("foo.csv", "w") as f: .....: f.write(data) .....: In this special case, read_csv assumes that the first column is to be used as the index of the DataFrame: In [190]: pd.read_csv("foo.csv") Out[190]: A B C 20090101 a 1 2 20090102 b 3 4 20090103 c 4 5 Note that the dates weren’t automatically parsed. In that case you would need to do as before: In [191]: df = pd.read_csv("foo.csv", parse_dates=True) In [192]: df.index Out[192]: DatetimeIndex(['2009-01-01', '2009-01-02', '2009-01-03'], dtype='datetime64[ns]', freq=None) Reading an index with a MultiIndex# Suppose you have data indexed by two columns: In [193]: data = 'year,indiv,zit,xit\n1977,"A",1.2,.6\n1977,"B",1.5,.5' In [194]: print(data) year,indiv,zit,xit 1977,"A",1.2,.6 1977,"B",1.5,.5 In [195]: with open("mindex_ex.csv", mode="w") as f: .....: f.write(data) .....: The index_col argument to read_csv can take a list of column numbers to turn multiple columns into a MultiIndex for the index of the returned object: In [196]: df = pd.read_csv("mindex_ex.csv", index_col=[0, 1]) In [197]: df Out[197]: zit xit year indiv 1977 A 1.2 0.6 B 1.5 0.5 In [198]: df.loc[1977] Out[198]: zit xit indiv A 1.2 0.6 B 1.5 0.5 Reading columns with a MultiIndex# By specifying list of row locations for the header argument, you can read in a MultiIndex for the columns. Specifying non-consecutive rows will skip the intervening rows. In [199]: from pandas._testing import makeCustomDataframe as mkdf In [200]: df = mkdf(5, 3, r_idx_nlevels=2, c_idx_nlevels=4) In [201]: df.to_csv("mi.csv") In [202]: print(open("mi.csv").read()) C0,,C_l0_g0,C_l0_g1,C_l0_g2 C1,,C_l1_g0,C_l1_g1,C_l1_g2 C2,,C_l2_g0,C_l2_g1,C_l2_g2 C3,,C_l3_g0,C_l3_g1,C_l3_g2 R0,R1,,, R_l0_g0,R_l1_g0,R0C0,R0C1,R0C2 R_l0_g1,R_l1_g1,R1C0,R1C1,R1C2 R_l0_g2,R_l1_g2,R2C0,R2C1,R2C2 R_l0_g3,R_l1_g3,R3C0,R3C1,R3C2 R_l0_g4,R_l1_g4,R4C0,R4C1,R4C2 In [203]: pd.read_csv("mi.csv", header=[0, 1, 2, 3], index_col=[0, 1]) Out[203]: C0 C_l0_g0 C_l0_g1 C_l0_g2 C1 C_l1_g0 C_l1_g1 C_l1_g2 C2 C_l2_g0 C_l2_g1 C_l2_g2 C3 C_l3_g0 C_l3_g1 C_l3_g2 R0 R1 R_l0_g0 R_l1_g0 R0C0 R0C1 R0C2 R_l0_g1 R_l1_g1 R1C0 R1C1 R1C2 R_l0_g2 R_l1_g2 R2C0 R2C1 R2C2 R_l0_g3 R_l1_g3 R3C0 R3C1 R3C2 R_l0_g4 R_l1_g4 R4C0 R4C1 R4C2 read_csv is also able to interpret a more common format of multi-columns indices. In [204]: data = ",a,a,a,b,c,c\n,q,r,s,t,u,v\none,1,2,3,4,5,6\ntwo,7,8,9,10,11,12" In [205]: print(data) ,a,a,a,b,c,c ,q,r,s,t,u,v one,1,2,3,4,5,6 two,7,8,9,10,11,12 In [206]: with open("mi2.csv", "w") as fh: .....: fh.write(data) .....: In [207]: pd.read_csv("mi2.csv", header=[0, 1], index_col=0) Out[207]: a b c q r s t u v one 1 2 3 4 5 6 two 7 8 9 10 11 12 Note If an index_col is not specified (e.g. you don’t have an index, or wrote it with df.to_csv(..., index=False), then any names on the columns index will be lost. Automatically “sniffing” the delimiter# read_csv is capable of inferring delimited (not necessarily comma-separated) files, as pandas uses the csv.Sniffer class of the csv module. For this, you have to specify sep=None. In [208]: df = pd.DataFrame(np.random.randn(10, 4)) In [209]: df.to_csv("tmp.csv", sep="|") In [210]: df.to_csv("tmp2.csv", sep=":") In [211]: pd.read_csv("tmp2.csv", sep=None, engine="python") Out[211]: Unnamed: 0 0 1 2 3 0 0 0.469112 -0.282863 -1.509059 -1.135632 1 1 1.212112 -0.173215 0.119209 -1.044236 2 2 -0.861849 -2.104569 -0.494929 1.071804 3 3 0.721555 -0.706771 -1.039575 0.271860 4 4 -0.424972 0.567020 0.276232 -1.087401 5 5 -0.673690 0.113648 -1.478427 0.524988 6 6 0.404705 0.577046 -1.715002 -1.039268 7 7 -0.370647 -1.157892 -1.344312 0.844885 8 8 1.075770 -0.109050 1.643563 -1.469388 9 9 0.357021 -0.674600 -1.776904 -0.968914 Reading multiple files to create a single DataFrame# It’s best to use concat() to combine multiple files. See the cookbook for an example. Iterating through files chunk by chunk# Suppose you wish to iterate through a (potentially very large) file lazily rather than reading the entire file into memory, such as the following: In [212]: df = pd.DataFrame(np.random.randn(10, 4)) In [213]: df.to_csv("tmp.csv", sep="|") In [214]: table = pd.read_csv("tmp.csv", sep="|") In [215]: table Out[215]: Unnamed: 0 0 1 2 3 0 0 -1.294524 0.413738 0.276662 -0.472035 1 1 -0.013960 -0.362543 -0.006154 -0.923061 2 2 0.895717 0.805244 -1.206412 2.565646 3 3 1.431256 1.340309 -1.170299 -0.226169 4 4 0.410835 0.813850 0.132003 -0.827317 5 5 -0.076467 -1.187678 1.130127 -1.436737 6 6 -1.413681 1.607920 1.024180 0.569605 7 7 0.875906 -2.211372 0.974466 -2.006747 8 8 -0.410001 -0.078638 0.545952 -1.219217 9 9 -1.226825 0.769804 -1.281247 -0.727707 By specifying a chunksize to read_csv, the return value will be an iterable object of type TextFileReader: In [216]: with pd.read_csv("tmp.csv", sep="|", chunksize=4) as reader: .....: reader .....: for chunk in reader: .....: print(chunk) .....: Unnamed: 0 0 1 2 3 0 0 -1.294524 0.413738 0.276662 -0.472035 1 1 -0.013960 -0.362543 -0.006154 -0.923061 2 2 0.895717 0.805244 -1.206412 2.565646 3 3 1.431256 1.340309 -1.170299 -0.226169 Unnamed: 0 0 1 2 3 4 4 0.410835 0.813850 0.132003 -0.827317 5 5 -0.076467 -1.187678 1.130127 -1.436737 6 6 -1.413681 1.607920 1.024180 0.569605 7 7 0.875906 -2.211372 0.974466 -2.006747 Unnamed: 0 0 1 2 3 8 8 -0.410001 -0.078638 0.545952 -1.219217 9 9 -1.226825 0.769804 -1.281247 -0.727707 Changed in version 1.2: read_csv/json/sas return a context-manager when iterating through a file. Specifying iterator=True will also return the TextFileReader object: In [217]: with pd.read_csv("tmp.csv", sep="|", iterator=True) as reader: .....: reader.get_chunk(5) .....: Specifying the parser engine# Pandas currently supports three engines, the C engine, the python engine, and an experimental pyarrow engine (requires the pyarrow package). In general, the pyarrow engine is fastest on larger workloads and is equivalent in speed to the C engine on most other workloads. The python engine tends to be slower than the pyarrow and C engines on most workloads. However, the pyarrow engine is much less robust than the C engine, which lacks a few features compared to the Python engine. Where possible, pandas uses the C parser (specified as engine='c'), but it may fall back to Python if C-unsupported options are specified. Currently, options unsupported by the C and pyarrow engines include: sep other than a single character (e.g. regex separators) skipfooter sep=None with delim_whitespace=False Specifying any of the above options will produce a ParserWarning unless the python engine is selected explicitly using engine='python'. Options that are unsupported by the pyarrow engine which are not covered by the list above include: float_precision chunksize comment nrows thousands memory_map dialect warn_bad_lines error_bad_lines on_bad_lines delim_whitespace quoting lineterminator converters decimal iterator dayfirst infer_datetime_format verbose skipinitialspace low_memory Specifying these options with engine='pyarrow' will raise a ValueError. Reading/writing remote files# You can pass in a URL to read or write remote files to many of pandas’ IO functions - the following example shows reading a CSV file: df = pd.read_csv("https://download.bls.gov/pub/time.series/cu/cu.item", sep="\t") New in version 1.3.0. A custom header can be sent alongside HTTP(s) requests by passing a dictionary of header key value mappings to the storage_options keyword argument as shown below: headers = {"User-Agent": "pandas"} df = pd.read_csv( "https://download.bls.gov/pub/time.series/cu/cu.item", sep="\t", storage_options=headers ) All URLs which are not local files or HTTP(s) are handled by fsspec, if installed, and its various filesystem implementations (including Amazon S3, Google Cloud, SSH, FTP, webHDFS…). Some of these implementations will require additional packages to be installed, for example S3 URLs require the s3fs library: df = pd.read_json("s3://pandas-test/adatafile.json") When dealing with remote storage systems, you might need extra configuration with environment variables or config files in special locations. For example, to access data in your S3 bucket, you will need to define credentials in one of the several ways listed in the S3Fs documentation. The same is true for several of the storage backends, and you should follow the links at fsimpl1 for implementations built into fsspec and fsimpl2 for those not included in the main fsspec distribution. You can also pass parameters directly to the backend driver. For example, if you do not have S3 credentials, you can still access public data by specifying an anonymous connection, such as New in version 1.2.0. pd.read_csv( "s3://ncei-wcsd-archive/data/processed/SH1305/18kHz/SaKe2013" "-D20130523-T080854_to_SaKe2013-D20130523-T085643.csv", storage_options={"anon": True}, ) fsspec also allows complex URLs, for accessing data in compressed archives, local caching of files, and more. To locally cache the above example, you would modify the call to pd.read_csv( "simplecache::s3://ncei-wcsd-archive/data/processed/SH1305/18kHz/" "SaKe2013-D20130523-T080854_to_SaKe2013-D20130523-T085643.csv", storage_options={"s3": {"anon": True}}, ) where we specify that the “anon” parameter is meant for the “s3” part of the implementation, not to the caching implementation. Note that this caches to a temporary directory for the duration of the session only, but you can also specify a permanent store. Writing out data# Writing to CSV format# The Series and DataFrame objects have an instance method to_csv which allows storing the contents of the object as a comma-separated-values file. The function takes a number of arguments. Only the first is required. path_or_buf: A string path to the file to write or a file object. If a file object it must be opened with newline='' sep : Field delimiter for the output file (default “,”) na_rep: A string representation of a missing value (default ‘’) float_format: Format string for floating point numbers columns: Columns to write (default None) header: Whether to write out the column names (default True) index: whether to write row (index) names (default True) index_label: Column label(s) for index column(s) if desired. If None (default), and header and index are True, then the index names are used. (A sequence should be given if the DataFrame uses MultiIndex). mode : Python write mode, default ‘w’ encoding: a string representing the encoding to use if the contents are non-ASCII, for Python versions prior to 3 lineterminator: Character sequence denoting line end (default os.linesep) quoting: Set quoting rules as in csv module (default csv.QUOTE_MINIMAL). Note that if you have set a float_format then floats are converted to strings and csv.QUOTE_NONNUMERIC will treat them as non-numeric quotechar: Character used to quote fields (default ‘”’) doublequote: Control quoting of quotechar in fields (default True) escapechar: Character used to escape sep and quotechar when appropriate (default None) chunksize: Number of rows to write at a time date_format: Format string for datetime objects Writing a formatted string# The DataFrame object has an instance method to_string which allows control over the string representation of the object. All arguments are optional: buf default None, for example a StringIO object columns default None, which columns to write col_space default None, minimum width of each column. na_rep default NaN, representation of NA value formatters default None, a dictionary (by column) of functions each of which takes a single argument and returns a formatted string float_format default None, a function which takes a single (float) argument and returns a formatted string; to be applied to floats in the DataFrame. sparsify default True, set to False for a DataFrame with a hierarchical index to print every MultiIndex key at each row. index_names default True, will print the names of the indices index default True, will print the index (ie, row labels) header default True, will print the column labels justify default left, will print column headers left- or right-justified The Series object also has a to_string method, but with only the buf, na_rep, float_format arguments. There is also a length argument which, if set to True, will additionally output the length of the Series. JSON# Read and write JSON format files and strings. Writing JSON# A Series or DataFrame can be converted to a valid JSON string. Use to_json with optional parameters: path_or_buf : the pathname or buffer to write the output This can be None in which case a JSON string is returned orient : Series: default is index allowed values are {split, records, index} DataFrame: default is columns allowed values are {split, records, index, columns, values, table} The format of the JSON string split dict like {index -> [index], columns -> [columns], data -> [values]} records list like [{column -> value}, … , {column -> value}] index dict like {index -> {column -> value}} columns dict like {column -> {index -> value}} values just the values array table adhering to the JSON Table Schema date_format : string, type of date conversion, ‘epoch’ for timestamp, ‘iso’ for ISO8601. double_precision : The number of decimal places to use when encoding floating point values, default 10. force_ascii : force encoded string to be ASCII, default True. date_unit : The time unit to encode to, governs timestamp and ISO8601 precision. One of ‘s’, ‘ms’, ‘us’ or ‘ns’ for seconds, milliseconds, microseconds and nanoseconds respectively. Default ‘ms’. default_handler : The handler to call if an object cannot otherwise be converted to a suitable format for JSON. Takes a single argument, which is the object to convert, and returns a serializable object. lines : If records orient, then will write each record per line as json. Note NaN’s, NaT’s and None will be converted to null and datetime objects will be converted based on the date_format and date_unit parameters. In [218]: dfj = pd.DataFrame(np.random.randn(5, 2), columns=list("AB")) In [219]: json = dfj.to_json() In [220]: json Out[220]: '{"A":{"0":-0.1213062281,"1":0.6957746499,"2":0.9597255933,"3":-0.6199759194,"4":-0.7323393705},"B":{"0":-0.0978826728,"1":0.3417343559,"2":-1.1103361029,"3":0.1497483186,"4":0.6877383895}}' Orient options# There are a number of different options for the format of the resulting JSON file / string. Consider the following DataFrame and Series: In [221]: dfjo = pd.DataFrame( .....: dict(A=range(1, 4), B=range(4, 7), C=range(7, 10)), .....: columns=list("ABC"), .....: index=list("xyz"), .....: ) .....: In [222]: dfjo Out[222]: A B C x 1 4 7 y 2 5 8 z 3 6 9 In [223]: sjo = pd.Series(dict(x=15, y=16, z=17), name="D") In [224]: sjo Out[224]: x 15 y 16 z 17 Name: D, dtype: int64 Column oriented (the default for DataFrame) serializes the data as nested JSON objects with column labels acting as the primary index: In [225]: dfjo.to_json(orient="columns") Out[225]: '{"A":{"x":1,"y":2,"z":3},"B":{"x":4,"y":5,"z":6},"C":{"x":7,"y":8,"z":9}}' # Not available for Series Index oriented (the default for Series) similar to column oriented but the index labels are now primary: In [226]: dfjo.to_json(orient="index") Out[226]: '{"x":{"A":1,"B":4,"C":7},"y":{"A":2,"B":5,"C":8},"z":{"A":3,"B":6,"C":9}}' In [227]: sjo.to_json(orient="index") Out[227]: '{"x":15,"y":16,"z":17}' Record oriented serializes the data to a JSON array of column -> value records, index labels are not included. This is useful for passing DataFrame data to plotting libraries, for example the JavaScript library d3.js: In [228]: dfjo.to_json(orient="records") Out[228]: '[{"A":1,"B":4,"C":7},{"A":2,"B":5,"C":8},{"A":3,"B":6,"C":9}]' In [229]: sjo.to_json(orient="records") Out[229]: '[15,16,17]' Value oriented is a bare-bones option which serializes to nested JSON arrays of values only, column and index labels are not included: In [230]: dfjo.to_json(orient="values") Out[230]: '[[1,4,7],[2,5,8],[3,6,9]]' # Not available for Series Split oriented serializes to a JSON object containing separate entries for values, index and columns. Name is also included for Series: In [231]: dfjo.to_json(orient="split") Out[231]: '{"columns":["A","B","C"],"index":["x","y","z"],"data":[[1,4,7],[2,5,8],[3,6,9]]}' In [232]: sjo.to_json(orient="split") Out[232]: '{"name":"D","index":["x","y","z"],"data":[15,16,17]}' Table oriented serializes to the JSON Table Schema, allowing for the preservation of metadata including but not limited to dtypes and index names. Note Any orient option that encodes to a JSON object will not preserve the ordering of index and column labels during round-trip serialization. If you wish to preserve label ordering use the split option as it uses ordered containers. Date handling# Writing in ISO date format: In [233]: dfd = pd.DataFrame(np.random.randn(5, 2), columns=list("AB")) In [234]: dfd["date"] = pd.Timestamp("20130101") In [235]: dfd = dfd.sort_index(axis=1, ascending=False) In [236]: json = dfd.to_json(date_format="iso") In [237]: json Out[237]: '{"date":{"0":"2013-01-01T00:00:00.000","1":"2013-01-01T00:00:00.000","2":"2013-01-01T00:00:00.000","3":"2013-01-01T00:00:00.000","4":"2013-01-01T00:00:00.000"},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Writing in ISO date format, with microseconds: In [238]: json = dfd.to_json(date_format="iso", date_unit="us") In [239]: json Out[239]: '{"date":{"0":"2013-01-01T00:00:00.000000","1":"2013-01-01T00:00:00.000000","2":"2013-01-01T00:00:00.000000","3":"2013-01-01T00:00:00.000000","4":"2013-01-01T00:00:00.000000"},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Epoch timestamps, in seconds: In [240]: json = dfd.to_json(date_format="epoch", date_unit="s") In [241]: json Out[241]: '{"date":{"0":1356998400,"1":1356998400,"2":1356998400,"3":1356998400,"4":1356998400},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Writing to a file, with a date index and a date column: In [242]: dfj2 = dfj.copy() In [243]: dfj2["date"] = pd.Timestamp("20130101") In [244]: dfj2["ints"] = list(range(5)) In [245]: dfj2["bools"] = True In [246]: dfj2.index = pd.date_range("20130101", periods=5) In [247]: dfj2.to_json("test.json") In [248]: with open("test.json") as fh: .....: print(fh.read()) .....: {"A":{"1356998400000":-0.1213062281,"1357084800000":0.6957746499,"1357171200000":0.9597255933,"1357257600000":-0.6199759194,"1357344000000":-0.7323393705},"B":{"1356998400000":-0.0978826728,"1357084800000":0.3417343559,"1357171200000":-1.1103361029,"1357257600000":0.1497483186,"1357344000000":0.6877383895},"date":{"1356998400000":1356998400000,"1357084800000":1356998400000,"1357171200000":1356998400000,"1357257600000":1356998400000,"1357344000000":1356998400000},"ints":{"1356998400000":0,"1357084800000":1,"1357171200000":2,"1357257600000":3,"1357344000000":4},"bools":{"1356998400000":true,"1357084800000":true,"1357171200000":true,"1357257600000":true,"1357344000000":true}} Fallback behavior# If the JSON serializer cannot handle the container contents directly it will fall back in the following manner: if the dtype is unsupported (e.g. np.complex_) then the default_handler, if provided, will be called for each value, otherwise an exception is raised. if an object is unsupported it will attempt the following: check if the object has defined a toDict method and call it. A toDict method should return a dict which will then be JSON serialized. invoke the default_handler if one was provided. convert the object to a dict by traversing its contents. However this will often fail with an OverflowError or give unexpected results. In general the best approach for unsupported objects or dtypes is to provide a default_handler. For example: >>> DataFrame([1.0, 2.0, complex(1.0, 2.0)]).to_json() # raises RuntimeError: Unhandled numpy dtype 15 can be dealt with by specifying a simple default_handler: In [249]: pd.DataFrame([1.0, 2.0, complex(1.0, 2.0)]).to_json(default_handler=str) Out[249]: '{"0":{"0":"(1+0j)","1":"(2+0j)","2":"(1+2j)"}}' Reading JSON# Reading a JSON string to pandas object can take a number of parameters. The parser will try to parse a DataFrame if typ is not supplied or is None. To explicitly force Series parsing, pass typ=series filepath_or_buffer : a VALID JSON string or file handle / StringIO. The string could be a URL. Valid URL schemes include http, ftp, S3, and file. For file URLs, a host is expected. For instance, a local file could be file ://localhost/path/to/table.json typ : type of object to recover (series or frame), default ‘frame’ orient : Series : default is index allowed values are {split, records, index} DataFrame default is columns allowed values are {split, records, index, columns, values, table} The format of the JSON string split dict like {index -> [index], columns -> [columns], data -> [values]} records list like [{column -> value}, … , {column -> value}] index dict like {index -> {column -> value}} columns dict like {column -> {index -> value}} values just the values array table adhering to the JSON Table Schema dtype : if True, infer dtypes, if a dict of column to dtype, then use those, if False, then don’t infer dtypes at all, default is True, apply only to the data. convert_axes : boolean, try to convert the axes to the proper dtypes, default is True convert_dates : a list of columns to parse for dates; If True, then try to parse date-like columns, default is True. keep_default_dates : boolean, default True. If parsing dates, then parse the default date-like columns. numpy : direct decoding to NumPy arrays. default is False; Supports numeric data only, although labels may be non-numeric. Also note that the JSON ordering MUST be the same for each term if numpy=True. precise_float : boolean, default False. Set to enable usage of higher precision (strtod) function when decoding string to double values. Default (False) is to use fast but less precise builtin functionality. date_unit : string, the timestamp unit to detect if converting dates. Default None. By default the timestamp precision will be detected, if this is not desired then pass one of ‘s’, ‘ms’, ‘us’ or ‘ns’ to force timestamp precision to seconds, milliseconds, microseconds or nanoseconds respectively. lines : reads file as one json object per line. encoding : The encoding to use to decode py3 bytes. chunksize : when used in combination with lines=True, return a JsonReader which reads in chunksize lines per iteration. The parser will raise one of ValueError/TypeError/AssertionError if the JSON is not parseable. If a non-default orient was used when encoding to JSON be sure to pass the same option here so that decoding produces sensible results, see Orient Options for an overview. Data conversion# The default of convert_axes=True, dtype=True, and convert_dates=True will try to parse the axes, and all of the data into appropriate types, including dates. If you need to override specific dtypes, pass a dict to dtype. convert_axes should only be set to False if you need to preserve string-like numbers (e.g. ‘1’, ‘2’) in an axes. Note Large integer values may be converted to dates if convert_dates=True and the data and / or column labels appear ‘date-like’. The exact threshold depends on the date_unit specified. ‘date-like’ means that the column label meets one of the following criteria: it ends with '_at' it ends with '_time' it begins with 'timestamp' it is 'modified' it is 'date' Warning When reading JSON data, automatic coercing into dtypes has some quirks: an index can be reconstructed in a different order from serialization, that is, the returned order is not guaranteed to be the same as before serialization a column that was float data will be converted to integer if it can be done safely, e.g. a column of 1. bool columns will be converted to integer on reconstruction Thus there are times where you may want to specify specific dtypes via the dtype keyword argument. Reading from a JSON string: In [250]: pd.read_json(json) Out[250]: date B A 0 2013-01-01 0.403310 0.176444 1 2013-01-01 0.301624 -0.154951 2 2013-01-01 -1.369849 -2.179861 3 2013-01-01 1.462696 -0.954208 4 2013-01-01 -0.826591 -1.743161 Reading from a file: In [251]: pd.read_json("test.json") Out[251]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True Don’t convert any data (but still convert axes and dates): In [252]: pd.read_json("test.json", dtype=object).dtypes Out[252]: A object B object date object ints object bools object dtype: object Specify dtypes for conversion: In [253]: pd.read_json("test.json", dtype={"A": "float32", "bools": "int8"}).dtypes Out[253]: A float32 B float64 date datetime64[ns] ints int64 bools int8 dtype: object Preserve string indices: In [254]: si = pd.DataFrame( .....: np.zeros((4, 4)), columns=list(range(4)), index=[str(i) for i in range(4)] .....: ) .....: In [255]: si Out[255]: 0 1 2 3 0 0.0 0.0 0.0 0.0 1 0.0 0.0 0.0 0.0 2 0.0 0.0 0.0 0.0 3 0.0 0.0 0.0 0.0 In [256]: si.index Out[256]: Index(['0', '1', '2', '3'], dtype='object') In [257]: si.columns Out[257]: Int64Index([0, 1, 2, 3], dtype='int64') In [258]: json = si.to_json() In [259]: sij = pd.read_json(json, convert_axes=False) In [260]: sij Out[260]: 0 1 2 3 0 0 0 0 0 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 In [261]: sij.index Out[261]: Index(['0', '1', '2', '3'], dtype='object') In [262]: sij.columns Out[262]: Index(['0', '1', '2', '3'], dtype='object') Dates written in nanoseconds need to be read back in nanoseconds: In [263]: json = dfj2.to_json(date_unit="ns") # Try to parse timestamps as milliseconds -> Won't Work In [264]: dfju = pd.read_json(json, date_unit="ms") In [265]: dfju Out[265]: A B date ints bools 1356998400000000000 -0.121306 -0.097883 1356998400000000000 0 True 1357084800000000000 0.695775 0.341734 1356998400000000000 1 True 1357171200000000000 0.959726 -1.110336 1356998400000000000 2 True 1357257600000000000 -0.619976 0.149748 1356998400000000000 3 True 1357344000000000000 -0.732339 0.687738 1356998400000000000 4 True # Let pandas detect the correct precision In [266]: dfju = pd.read_json(json) In [267]: dfju Out[267]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True # Or specify that all timestamps are in nanoseconds In [268]: dfju = pd.read_json(json, date_unit="ns") In [269]: dfju Out[269]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True The Numpy parameter# Note This param has been deprecated as of version 1.0.0 and will raise a FutureWarning. This supports numeric data only. Index and columns labels may be non-numeric, e.g. strings, dates etc. If numpy=True is passed to read_json an attempt will be made to sniff an appropriate dtype during deserialization and to subsequently decode directly to NumPy arrays, bypassing the need for intermediate Python objects. This can provide speedups if you are deserialising a large amount of numeric data: In [270]: randfloats = np.random.uniform(-100, 1000, 10000) In [271]: randfloats.shape = (1000, 10) In [272]: dffloats = pd.DataFrame(randfloats, columns=list("ABCDEFGHIJ")) In [273]: jsonfloats = dffloats.to_json() In [274]: %timeit pd.read_json(jsonfloats) 7.91 ms +- 77.3 us per loop (mean +- std. dev. of 7 runs, 100 loops each) In [275]: %timeit pd.read_json(jsonfloats, numpy=True) 5.71 ms +- 333 us per loop (mean +- std. dev. of 7 runs, 100 loops each) The speedup is less noticeable for smaller datasets: In [276]: jsonfloats = dffloats.head(100).to_json() In [277]: %timeit pd.read_json(jsonfloats) 4.46 ms +- 25.9 us per loop (mean +- std. dev. of 7 runs, 100 loops each) In [278]: %timeit pd.read_json(jsonfloats, numpy=True) 4.09 ms +- 32.3 us per loop (mean +- std. dev. of 7 runs, 100 loops each) Warning Direct NumPy decoding makes a number of assumptions and may fail or produce unexpected output if these assumptions are not satisfied: data is numeric. data is uniform. The dtype is sniffed from the first value decoded. A ValueError may be raised, or incorrect output may be produced if this condition is not satisfied. labels are ordered. Labels are only read from the first container, it is assumed that each subsequent row / column has been encoded in the same order. This should be satisfied if the data was encoded using to_json but may not be the case if the JSON is from another source. Normalization# pandas provides a utility function to take a dict or list of dicts and normalize this semi-structured data into a flat table. In [279]: data = [ .....: {"id": 1, "name": {"first": "Coleen", "last": "Volk"}}, .....: {"name": {"given": "Mark", "family": "Regner"}}, .....: {"id": 2, "name": "Faye Raker"}, .....: ] .....: In [280]: pd.json_normalize(data) Out[280]: id name.first name.last name.given name.family name 0 1.0 Coleen Volk NaN NaN NaN 1 NaN NaN NaN Mark Regner NaN 2 2.0 NaN NaN NaN NaN Faye Raker In [281]: data = [ .....: { .....: "state": "Florida", .....: "shortname": "FL", .....: "info": {"governor": "Rick Scott"}, .....: "county": [ .....: {"name": "Dade", "population": 12345}, .....: {"name": "Broward", "population": 40000}, .....: {"name": "Palm Beach", "population": 60000}, .....: ], .....: }, .....: { .....: "state": "Ohio", .....: "shortname": "OH", .....: "info": {"governor": "John Kasich"}, .....: "county": [ .....: {"name": "Summit", "population": 1234}, .....: {"name": "Cuyahoga", "population": 1337}, .....: ], .....: }, .....: ] .....: In [282]: pd.json_normalize(data, "county", ["state", "shortname", ["info", "governor"]]) Out[282]: name population state shortname info.governor 0 Dade 12345 Florida FL Rick Scott 1 Broward 40000 Florida FL Rick Scott 2 Palm Beach 60000 Florida FL Rick Scott 3 Summit 1234 Ohio OH John Kasich 4 Cuyahoga 1337 Ohio OH John Kasich The max_level parameter provides more control over which level to end normalization. With max_level=1 the following snippet normalizes until 1st nesting level of the provided dict. In [283]: data = [ .....: { .....: "CreatedBy": {"Name": "User001"}, .....: "Lookup": { .....: "TextField": "Some text", .....: "UserField": {"Id": "ID001", "Name": "Name001"}, .....: }, .....: "Image": {"a": "b"}, .....: } .....: ] .....: In [284]: pd.json_normalize(data, max_level=1) Out[284]: CreatedBy.Name Lookup.TextField Lookup.UserField Image.a 0 User001 Some text {'Id': 'ID001', 'Name': 'Name001'} b Line delimited json# pandas is able to read and write line-delimited json files that are common in data processing pipelines using Hadoop or Spark. For line-delimited json files, pandas can also return an iterator which reads in chunksize lines at a time. This can be useful for large files or to read from a stream. In [285]: jsonl = """ .....: {"a": 1, "b": 2} .....: {"a": 3, "b": 4} .....: """ .....: In [286]: df = pd.read_json(jsonl, lines=True) In [287]: df Out[287]: a b 0 1 2 1 3 4 In [288]: df.to_json(orient="records", lines=True) Out[288]: '{"a":1,"b":2}\n{"a":3,"b":4}\n' # reader is an iterator that returns ``chunksize`` lines each iteration In [289]: with pd.read_json(StringIO(jsonl), lines=True, chunksize=1) as reader: .....: reader .....: for chunk in reader: .....: print(chunk) .....: Empty DataFrame Columns: [] Index: [] a b 0 1 2 a b 1 3 4 Table schema# Table Schema is a spec for describing tabular datasets as a JSON object. The JSON includes information on the field names, types, and other attributes. You can use the orient table to build a JSON string with two fields, schema and data. In [290]: df = pd.DataFrame( .....: { .....: "A": [1, 2, 3], .....: "B": ["a", "b", "c"], .....: "C": pd.date_range("2016-01-01", freq="d", periods=3), .....: }, .....: index=pd.Index(range(3), name="idx"), .....: ) .....: In [291]: df Out[291]: A B C idx 0 1 a 2016-01-01 1 2 b 2016-01-02 2 3 c 2016-01-03 In [292]: df.to_json(orient="table", date_format="iso") Out[292]: '{"schema":{"fields":[{"name":"idx","type":"integer"},{"name":"A","type":"integer"},{"name":"B","type":"string"},{"name":"C","type":"datetime"}],"primaryKey":["idx"],"pandas_version":"1.4.0"},"data":[{"idx":0,"A":1,"B":"a","C":"2016-01-01T00:00:00.000"},{"idx":1,"A":2,"B":"b","C":"2016-01-02T00:00:00.000"},{"idx":2,"A":3,"B":"c","C":"2016-01-03T00:00:00.000"}]}' The schema field contains the fields key, which itself contains a list of column name to type pairs, including the Index or MultiIndex (see below for a list of types). The schema field also contains a primaryKey field if the (Multi)index is unique. The second field, data, contains the serialized data with the records orient. The index is included, and any datetimes are ISO 8601 formatted, as required by the Table Schema spec. The full list of types supported are described in the Table Schema spec. This table shows the mapping from pandas types: pandas type Table Schema type int64 integer float64 number bool boolean datetime64[ns] datetime timedelta64[ns] duration categorical any object str A few notes on the generated table schema: The schema object contains a pandas_version field. This contains the version of pandas’ dialect of the schema, and will be incremented with each revision. All dates are converted to UTC when serializing. Even timezone naive values, which are treated as UTC with an offset of 0. In [293]: from pandas.io.json import build_table_schema In [294]: s = pd.Series(pd.date_range("2016", periods=4)) In [295]: build_table_schema(s) Out[295]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'datetime'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} datetimes with a timezone (before serializing), include an additional field tz with the time zone name (e.g. 'US/Central'). In [296]: s_tz = pd.Series(pd.date_range("2016", periods=12, tz="US/Central")) In [297]: build_table_schema(s_tz) Out[297]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'datetime', 'tz': 'US/Central'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} Periods are converted to timestamps before serialization, and so have the same behavior of being converted to UTC. In addition, periods will contain and additional field freq with the period’s frequency, e.g. 'A-DEC'. In [298]: s_per = pd.Series(1, index=pd.period_range("2016", freq="A-DEC", periods=4)) In [299]: build_table_schema(s_per) Out[299]: {'fields': [{'name': 'index', 'type': 'datetime', 'freq': 'A-DEC'}, {'name': 'values', 'type': 'integer'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} Categoricals use the any type and an enum constraint listing the set of possible values. Additionally, an ordered field is included: In [300]: s_cat = pd.Series(pd.Categorical(["a", "b", "a"])) In [301]: build_table_schema(s_cat) Out[301]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'any', 'constraints': {'enum': ['a', 'b']}, 'ordered': False}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} A primaryKey field, containing an array of labels, is included if the index is unique: In [302]: s_dupe = pd.Series([1, 2], index=[1, 1]) In [303]: build_table_schema(s_dupe) Out[303]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'integer'}], 'pandas_version': '1.4.0'} The primaryKey behavior is the same with MultiIndexes, but in this case the primaryKey is an array: In [304]: s_multi = pd.Series(1, index=pd.MultiIndex.from_product([("a", "b"), (0, 1)])) In [305]: build_table_schema(s_multi) Out[305]: {'fields': [{'name': 'level_0', 'type': 'string'}, {'name': 'level_1', 'type': 'integer'}, {'name': 'values', 'type': 'integer'}], 'primaryKey': FrozenList(['level_0', 'level_1']), 'pandas_version': '1.4.0'} The default naming roughly follows these rules: For series, the object.name is used. If that’s none, then the name is values For DataFrames, the stringified version of the column name is used For Index (not MultiIndex), index.name is used, with a fallback to index if that is None. For MultiIndex, mi.names is used. If any level has no name, then level_<i> is used. read_json also accepts orient='table' as an argument. This allows for the preservation of metadata such as dtypes and index names in a round-trippable manner. In [306]: df = pd.DataFrame( .....: { .....: "foo": [1, 2, 3, 4], .....: "bar": ["a", "b", "c", "d"], .....: "baz": pd.date_range("2018-01-01", freq="d", periods=4), .....: "qux": pd.Categorical(["a", "b", "c", "c"]), .....: }, .....: index=pd.Index(range(4), name="idx"), .....: ) .....: In [307]: df Out[307]: foo bar baz qux idx 0 1 a 2018-01-01 a 1 2 b 2018-01-02 b 2 3 c 2018-01-03 c 3 4 d 2018-01-04 c In [308]: df.dtypes Out[308]: foo int64 bar object baz datetime64[ns] qux category dtype: object In [309]: df.to_json("test.json", orient="table") In [310]: new_df = pd.read_json("test.json", orient="table") In [311]: new_df Out[311]: foo bar baz qux idx 0 1 a 2018-01-01 a 1 2 b 2018-01-02 b 2 3 c 2018-01-03 c 3 4 d 2018-01-04 c In [312]: new_df.dtypes Out[312]: foo int64 bar object baz datetime64[ns] qux category dtype: object Please note that the literal string ‘index’ as the name of an Index is not round-trippable, nor are any names beginning with 'level_' within a MultiIndex. These are used by default in DataFrame.to_json() to indicate missing values and the subsequent read cannot distinguish the intent. In [313]: df.index.name = "index" In [314]: df.to_json("test.json", orient="table") In [315]: new_df = pd.read_json("test.json", orient="table") In [316]: print(new_df.index.name) None When using orient='table' along with user-defined ExtensionArray, the generated schema will contain an additional extDtype key in the respective fields element. This extra key is not standard but does enable JSON roundtrips for extension types (e.g. read_json(df.to_json(orient="table"), orient="table")). The extDtype key carries the name of the extension, if you have properly registered the ExtensionDtype, pandas will use said name to perform a lookup into the registry and re-convert the serialized data into your custom dtype. HTML# Reading HTML content# Warning We highly encourage you to read the HTML Table Parsing gotchas below regarding the issues surrounding the BeautifulSoup4/html5lib/lxml parsers. The top-level read_html() function can accept an HTML string/file/URL and will parse HTML tables into list of pandas DataFrames. Let’s look at a few examples. Note read_html returns a list of DataFrame objects, even if there is only a single table contained in the HTML content. Read a URL with no options: In [320]: "https://www.fdic.gov/resources/resolutions/bank-failures/failed-bank-list" In [321]: pd.read_html(url) Out[321]: [ Bank NameBank CityCity StateSt ... Acquiring InstitutionAI Closing DateClosing FundFund 0 Almena State Bank Almena KS ... Equity Bank October 23, 2020 10538 1 First City Bank of Florida Fort Walton Beach FL ... United Fidelity Bank, fsb October 16, 2020 10537 2 The First State Bank Barboursville WV ... MVB Bank, Inc. April 3, 2020 10536 3 Ericson State Bank Ericson NE ... Farmers and Merchants Bank February 14, 2020 10535 4 City National Bank of New Jersey Newark NJ ... Industrial Bank November 1, 2019 10534 .. ... ... ... ... ... ... ... 558 Superior Bank, FSB Hinsdale IL ... Superior Federal, FSB July 27, 2001 6004 559 Malta National Bank Malta OH ... North Valley Bank May 3, 2001 4648 560 First Alliance Bank & Trust Co. Manchester NH ... Southern New Hampshire Bank & Trust February 2, 2001 4647 561 National State Bank of Metropolis Metropolis IL ... Banterra Bank of Marion December 14, 2000 4646 562 Bank of Honolulu Honolulu HI ... Bank of the Orient October 13, 2000 4645 [563 rows x 7 columns]] Note The data from the above URL changes every Monday so the resulting data above may be slightly different. Read in the content of the file from the above URL and pass it to read_html as a string: In [317]: html_str = """ .....: <table> .....: <tr> .....: <th>A</th> .....: <th colspan="1">B</th> .....: <th rowspan="1">C</th> .....: </tr> .....: <tr> .....: <td>a</td> .....: <td>b</td> .....: <td>c</td> .....: </tr> .....: </table> .....: """ .....: In [318]: with open("tmp.html", "w") as f: .....: f.write(html_str) .....: In [319]: df = pd.read_html("tmp.html") In [320]: df[0] Out[320]: A B C 0 a b c You can even pass in an instance of StringIO if you so desire: In [321]: dfs = pd.read_html(StringIO(html_str)) In [322]: dfs[0] Out[322]: A B C 0 a b c Note The following examples are not run by the IPython evaluator due to the fact that having so many network-accessing functions slows down the documentation build. If you spot an error or an example that doesn’t run, please do not hesitate to report it over on pandas GitHub issues page. Read a URL and match a table that contains specific text: match = "Metcalf Bank" df_list = pd.read_html(url, match=match) Specify a header row (by default <th> or <td> elements located within a <thead> are used to form the column index, if multiple rows are contained within <thead> then a MultiIndex is created); if specified, the header row is taken from the data minus the parsed header elements (<th> elements). dfs = pd.read_html(url, header=0) Specify an index column: dfs = pd.read_html(url, index_col=0) Specify a number of rows to skip: dfs = pd.read_html(url, skiprows=0) Specify a number of rows to skip using a list (range works as well): dfs = pd.read_html(url, skiprows=range(2)) Specify an HTML attribute: dfs1 = pd.read_html(url, attrs={"id": "table"}) dfs2 = pd.read_html(url, attrs={"class": "sortable"}) print(np.array_equal(dfs1[0], dfs2[0])) # Should be True Specify values that should be converted to NaN: dfs = pd.read_html(url, na_values=["No Acquirer"]) Specify whether to keep the default set of NaN values: dfs = pd.read_html(url, keep_default_na=False) Specify converters for columns. This is useful for numerical text data that has leading zeros. By default columns that are numerical are cast to numeric types and the leading zeros are lost. To avoid this, we can convert these columns to strings. url_mcc = "https://en.wikipedia.org/wiki/Mobile_country_code" dfs = pd.read_html( url_mcc, match="Telekom Albania", header=0, converters={"MNC": str}, ) Use some combination of the above: dfs = pd.read_html(url, match="Metcalf Bank", index_col=0) Read in pandas to_html output (with some loss of floating point precision): df = pd.DataFrame(np.random.randn(2, 2)) s = df.to_html(float_format="{0:.40g}".format) dfin = pd.read_html(s, index_col=0) The lxml backend will raise an error on a failed parse if that is the only parser you provide. If you only have a single parser you can provide just a string, but it is considered good practice to pass a list with one string if, for example, the function expects a sequence of strings. You may use: dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor=["lxml"]) Or you could pass flavor='lxml' without a list: dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor="lxml") However, if you have bs4 and html5lib installed and pass None or ['lxml', 'bs4'] then the parse will most likely succeed. Note that as soon as a parse succeeds, the function will return. dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor=["lxml", "bs4"]) Links can be extracted from cells along with the text using extract_links="all". In [323]: html_table = """ .....: <table> .....: <tr> .....: <th>GitHub</th> .....: </tr> .....: <tr> .....: <td><a href="https://github.com/pandas-dev/pandas">pandas</a></td> .....: </tr> .....: </table> .....: """ .....: In [324]: df = pd.read_html( .....: html_table, .....: extract_links="all" .....: )[0] .....: In [325]: df Out[325]: (GitHub, None) 0 (pandas, https://github.com/pandas-dev/pandas) In [326]: df[("GitHub", None)] Out[326]: 0 (pandas, https://github.com/pandas-dev/pandas) Name: (GitHub, None), dtype: object In [327]: df[("GitHub", None)].str[1] Out[327]: 0 https://github.com/pandas-dev/pandas Name: (GitHub, None), dtype: object New in version 1.5.0. Writing to HTML files# DataFrame objects have an instance method to_html which renders the contents of the DataFrame as an HTML table. The function arguments are as in the method to_string described above. Note Not all of the possible options for DataFrame.to_html are shown here for brevity’s sake. See to_html() for the full set of options. Note In an HTML-rendering supported environment like a Jupyter Notebook, display(HTML(...))` will render the raw HTML into the environment. In [328]: from IPython.display import display, HTML In [329]: df = pd.DataFrame(np.random.randn(2, 2)) In [330]: df Out[330]: 0 1 0 0.070319 1.773907 1 0.253908 0.414581 In [331]: html = df.to_html() In [332]: print(html) # raw html <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <th>1</th> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> In [333]: display(HTML(html)) <IPython.core.display.HTML object> The columns argument will limit the columns shown: In [334]: html = df.to_html(columns=[0]) In [335]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> </tr> <tr> <th>1</th> <td>0.253908</td> </tr> </tbody> </table> In [336]: display(HTML(html)) <IPython.core.display.HTML object> float_format takes a Python callable to control the precision of floating point values: In [337]: html = df.to_html(float_format="{0:.10f}".format) In [338]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.0703192665</td> <td>1.7739074228</td> </tr> <tr> <th>1</th> <td>0.2539083433</td> <td>0.4145805920</td> </tr> </tbody> </table> In [339]: display(HTML(html)) <IPython.core.display.HTML object> bold_rows will make the row labels bold by default, but you can turn that off: In [340]: html = df.to_html(bold_rows=False) In [341]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <td>1</td> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> In [342]: display(HTML(html)) <IPython.core.display.HTML object> The classes argument provides the ability to give the resulting HTML table CSS classes. Note that these classes are appended to the existing 'dataframe' class. In [343]: print(df.to_html(classes=["awesome_table_class", "even_more_awesome_class"])) <table border="1" class="dataframe awesome_table_class even_more_awesome_class"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <th>1</th> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> The render_links argument provides the ability to add hyperlinks to cells that contain URLs. In [344]: url_df = pd.DataFrame( .....: { .....: "name": ["Python", "pandas"], .....: "url": ["https://www.python.org/", "https://pandas.pydata.org"], .....: } .....: ) .....: In [345]: html = url_df.to_html(render_links=True) In [346]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>name</th> <th>url</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>Python</td> <td><a href="https://www.python.org/" target="_blank">https://www.python.org/</a></td> </tr> <tr> <th>1</th> <td>pandas</td> <td><a href="https://pandas.pydata.org" target="_blank">https://pandas.pydata.org</a></td> </tr> </tbody> </table> In [347]: display(HTML(html)) <IPython.core.display.HTML object> Finally, the escape argument allows you to control whether the “<”, “>” and “&” characters escaped in the resulting HTML (by default it is True). So to get the HTML without escaped characters pass escape=False In [348]: df = pd.DataFrame({"a": list("&<>"), "b": np.random.randn(3)}) Escaped: In [349]: html = df.to_html() In [350]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>a</th> <th>b</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>&amp;</td> <td>0.842321</td> </tr> <tr> <th>1</th> <td>&lt;</td> <td>0.211337</td> </tr> <tr> <th>2</th> <td>&gt;</td> <td>-1.055427</td> </tr> </tbody> </table> In [351]: display(HTML(html)) <IPython.core.display.HTML object> Not escaped: In [352]: html = df.to_html(escape=False) In [353]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>a</th> <th>b</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>&</td> <td>0.842321</td> </tr> <tr> <th>1</th> <td><</td> <td>0.211337</td> </tr> <tr> <th>2</th> <td>></td> <td>-1.055427</td> </tr> </tbody> </table> In [354]: display(HTML(html)) <IPython.core.display.HTML object> Note Some browsers may not show a difference in the rendering of the previous two HTML tables. HTML Table Parsing Gotchas# There are some versioning issues surrounding the libraries that are used to parse HTML tables in the top-level pandas io function read_html. Issues with lxml Benefits lxml is very fast. lxml requires Cython to install correctly. Drawbacks lxml does not make any guarantees about the results of its parse unless it is given strictly valid markup. In light of the above, we have chosen to allow you, the user, to use the lxml backend, but this backend will use html5lib if lxml fails to parse It is therefore highly recommended that you install both BeautifulSoup4 and html5lib, so that you will still get a valid result (provided everything else is valid) even if lxml fails. Issues with BeautifulSoup4 using lxml as a backend The above issues hold here as well since BeautifulSoup4 is essentially just a wrapper around a parser backend. Issues with BeautifulSoup4 using html5lib as a backend Benefits html5lib is far more lenient than lxml and consequently deals with real-life markup in a much saner way rather than just, e.g., dropping an element without notifying you. html5lib generates valid HTML5 markup from invalid markup automatically. This is extremely important for parsing HTML tables, since it guarantees a valid document. However, that does NOT mean that it is “correct”, since the process of fixing markup does not have a single definition. html5lib is pure Python and requires no additional build steps beyond its own installation. Drawbacks The biggest drawback to using html5lib is that it is slow as molasses. However consider the fact that many tables on the web are not big enough for the parsing algorithm runtime to matter. It is more likely that the bottleneck will be in the process of reading the raw text from the URL over the web, i.e., IO (input-output). For very large tables, this might not be true. LaTeX# New in version 1.3.0. Currently there are no methods to read from LaTeX, only output methods. Writing to LaTeX files# Note DataFrame and Styler objects currently have a to_latex method. We recommend using the Styler.to_latex() method over DataFrame.to_latex() due to the former’s greater flexibility with conditional styling, and the latter’s possible future deprecation. Review the documentation for Styler.to_latex, which gives examples of conditional styling and explains the operation of its keyword arguments. For simple application the following pattern is sufficient. In [355]: df = pd.DataFrame([[1, 2], [3, 4]], index=["a", "b"], columns=["c", "d"]) In [356]: print(df.style.to_latex()) \begin{tabular}{lrr} & c & d \\ a & 1 & 2 \\ b & 3 & 4 \\ \end{tabular} To format values before output, chain the Styler.format method. In [357]: print(df.style.format("€ {}").to_latex()) \begin{tabular}{lrr} & c & d \\ a & € 1 & € 2 \\ b & € 3 & € 4 \\ \end{tabular} XML# Reading XML# New in version 1.3.0. The top-level read_xml() function can accept an XML string/file/URL and will parse nodes and attributes into a pandas DataFrame. Note Since there is no standard XML structure where design types can vary in many ways, read_xml works best with flatter, shallow versions. If an XML document is deeply nested, use the stylesheet feature to transform XML into a flatter version. Let’s look at a few examples. Read an XML string: In [358]: xml = """<?xml version="1.0" encoding="UTF-8"?> .....: <bookstore> .....: <book category="cooking"> .....: <title lang="en">Everyday Italian</title> .....: <author>Giada De Laurentiis</author> .....: <year>2005</year> .....: <price>30.00</price> .....: </book> .....: <book category="children"> .....: <title lang="en">Harry Potter</title> .....: <author>J K. Rowling</author> .....: <year>2005</year> .....: <price>29.99</price> .....: </book> .....: <book category="web"> .....: <title lang="en">Learning XML</title> .....: <author>Erik T. Ray</author> .....: <year>2003</year> .....: <price>39.95</price> .....: </book> .....: </bookstore>""" .....: In [359]: df = pd.read_xml(xml) In [360]: df Out[360]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Read a URL with no options: In [361]: df = pd.read_xml("https://www.w3schools.com/xml/books.xml") In [362]: df Out[362]: category title author year price cover 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 None 1 children Harry Potter J K. Rowling 2005 29.99 None 2 web XQuery Kick Start Vaidyanathan Nagarajan 2003 49.99 None 3 web Learning XML Erik T. Ray 2003 39.95 paperback Read in the content of the “books.xml” file and pass it to read_xml as a string: In [363]: file_path = "books.xml" In [364]: with open(file_path, "w") as f: .....: f.write(xml) .....: In [365]: with open(file_path, "r") as f: .....: df = pd.read_xml(f.read()) .....: In [366]: df Out[366]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Read in the content of the “books.xml” as instance of StringIO or BytesIO and pass it to read_xml: In [367]: with open(file_path, "r") as f: .....: sio = StringIO(f.read()) .....: In [368]: df = pd.read_xml(sio) In [369]: df Out[369]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 In [370]: with open(file_path, "rb") as f: .....: bio = BytesIO(f.read()) .....: In [371]: df = pd.read_xml(bio) In [372]: df Out[372]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Even read XML from AWS S3 buckets such as NIH NCBI PMC Article Datasets providing Biomedical and Life Science Jorurnals: In [373]: df = pd.read_xml( .....: "s3://pmc-oa-opendata/oa_comm/xml/all/PMC1236943.xml", .....: xpath=".//journal-meta", .....: ) .....: In [374]: df Out[374]: journal-id journal-title issn publisher 0 Cardiovasc Ultrasound Cardiovascular Ultrasound 1476-7120 NaN With lxml as default parser, you access the full-featured XML library that extends Python’s ElementTree API. One powerful tool is ability to query nodes selectively or conditionally with more expressive XPath: In [375]: df = pd.read_xml(file_path, xpath="//book[year=2005]") In [376]: df Out[376]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 Specify only elements or only attributes to parse: In [377]: df = pd.read_xml(file_path, elems_only=True) In [378]: df Out[378]: title author year price 0 Everyday Italian Giada De Laurentiis 2005 30.00 1 Harry Potter J K. Rowling 2005 29.99 2 Learning XML Erik T. Ray 2003 39.95 In [379]: df = pd.read_xml(file_path, attrs_only=True) In [380]: df Out[380]: category 0 cooking 1 children 2 web XML documents can have namespaces with prefixes and default namespaces without prefixes both of which are denoted with a special attribute xmlns. In order to parse by node under a namespace context, xpath must reference a prefix. For example, below XML contains a namespace with prefix, doc, and URI at https://example.com. In order to parse doc:row nodes, namespaces must be used. In [381]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <doc:data xmlns:doc="https://example.com"> .....: <doc:row> .....: <doc:shape>square</doc:shape> .....: <doc:degrees>360</doc:degrees> .....: <doc:sides>4.0</doc:sides> .....: </doc:row> .....: <doc:row> .....: <doc:shape>circle</doc:shape> .....: <doc:degrees>360</doc:degrees> .....: <doc:sides/> .....: </doc:row> .....: <doc:row> .....: <doc:shape>triangle</doc:shape> .....: <doc:degrees>180</doc:degrees> .....: <doc:sides>3.0</doc:sides> .....: </doc:row> .....: </doc:data>""" .....: In [382]: df = pd.read_xml(xml, .....: xpath="//doc:row", .....: namespaces={"doc": "https://example.com"}) .....: In [383]: df Out[383]: shape degrees sides 0 square 360 4.0 1 circle 360 NaN 2 triangle 180 3.0 Similarly, an XML document can have a default namespace without prefix. Failing to assign a temporary prefix will return no nodes and raise a ValueError. But assigning any temporary name to correct URI allows parsing by nodes. In [384]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <data xmlns="https://example.com"> .....: <row> .....: <shape>square</shape> .....: <degrees>360</degrees> .....: <sides>4.0</sides> .....: </row> .....: <row> .....: <shape>circle</shape> .....: <degrees>360</degrees> .....: <sides/> .....: </row> .....: <row> .....: <shape>triangle</shape> .....: <degrees>180</degrees> .....: <sides>3.0</sides> .....: </row> .....: </data>""" .....: In [385]: df = pd.read_xml(xml, .....: xpath="//pandas:row", .....: namespaces={"pandas": "https://example.com"}) .....: In [386]: df Out[386]: shape degrees sides 0 square 360 4.0 1 circle 360 NaN 2 triangle 180 3.0 However, if XPath does not reference node names such as default, /*, then namespaces is not required. With lxml as parser, you can flatten nested XML documents with an XSLT script which also can be string/file/URL types. As background, XSLT is a special-purpose language written in a special XML file that can transform original XML documents into other XML, HTML, even text (CSV, JSON, etc.) using an XSLT processor. For example, consider this somewhat nested structure of Chicago “L” Rides where station and rides elements encapsulate data in their own sections. With below XSLT, lxml can transform original nested document into a flatter output (as shown below for demonstration) for easier parse into DataFrame: In [387]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <response> .....: <row> .....: <station id="40850" name="Library"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>864.2</avg_weekday_rides> .....: <avg_saturday_rides>534</avg_saturday_rides> .....: <avg_sunday_holiday_rides>417.2</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: <row> .....: <station id="41700" name="Washington/Wabash"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>2707.4</avg_weekday_rides> .....: <avg_saturday_rides>1909.8</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1438.6</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: <row> .....: <station id="40380" name="Clark/Lake"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>2949.6</avg_weekday_rides> .....: <avg_saturday_rides>1657</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1453.8</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: </response>""" .....: In [388]: xsl = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> .....: <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/> .....: <xsl:strip-space elements="*"/> .....: <xsl:template match="/response"> .....: <xsl:copy> .....: <xsl:apply-templates select="row"/> .....: </xsl:copy> .....: </xsl:template> .....: <xsl:template match="row"> .....: <xsl:copy> .....: <station_id><xsl:value-of select="station/@id"/></station_id> .....: <station_name><xsl:value-of select="station/@name"/></station_name> .....: <xsl:copy-of select="month|rides/*"/> .....: </xsl:copy> .....: </xsl:template> .....: </xsl:stylesheet>""" .....: In [389]: output = """<?xml version='1.0' encoding='utf-8'?> .....: <response> .....: <row> .....: <station_id>40850</station_id> .....: <station_name>Library</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>864.2</avg_weekday_rides> .....: <avg_saturday_rides>534</avg_saturday_rides> .....: <avg_sunday_holiday_rides>417.2</avg_sunday_holiday_rides> .....: </row> .....: <row> .....: <station_id>41700</station_id> .....: <station_name>Washington/Wabash</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>2707.4</avg_weekday_rides> .....: <avg_saturday_rides>1909.8</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1438.6</avg_sunday_holiday_rides> .....: </row> .....: <row> .....: <station_id>40380</station_id> .....: <station_name>Clark/Lake</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>2949.6</avg_weekday_rides> .....: <avg_saturday_rides>1657</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1453.8</avg_sunday_holiday_rides> .....: </row> .....: </response>""" .....: In [390]: df = pd.read_xml(xml, stylesheet=xsl) In [391]: df Out[391]: station_id station_name ... avg_saturday_rides avg_sunday_holiday_rides 0 40850 Library ... 534.0 417.2 1 41700 Washington/Wabash ... 1909.8 1438.6 2 40380 Clark/Lake ... 1657.0 1453.8 [3 rows x 6 columns] For very large XML files that can range in hundreds of megabytes to gigabytes, pandas.read_xml() supports parsing such sizeable files using lxml’s iterparse and etree’s iterparse which are memory-efficient methods to iterate through an XML tree and extract specific elements and attributes. without holding entire tree in memory. New in version 1.5.0. To use this feature, you must pass a physical XML file path into read_xml and use the iterparse argument. Files should not be compressed or point to online sources but stored on local disk. Also, iterparse should be a dictionary where the key is the repeating nodes in document (which become the rows) and the value is a list of any element or attribute that is a descendant (i.e., child, grandchild) of repeating node. Since XPath is not used in this method, descendants do not need to share same relationship with one another. Below shows example of reading in Wikipedia’s very large (12 GB+) latest article data dump. In [1]: df = pd.read_xml( ... "/path/to/downloaded/enwikisource-latest-pages-articles.xml", ... iterparse = {"page": ["title", "ns", "id"]} ... ) ... df Out[2]: title ns id 0 Gettysburg Address 0 21450 1 Main Page 0 42950 2 Declaration by United Nations 0 8435 3 Constitution of the United States of America 0 8435 4 Declaration of Independence (Israel) 0 17858 ... ... ... ... 3578760 Page:Black cat 1897 07 v2 n10.pdf/17 104 219649 3578761 Page:Black cat 1897 07 v2 n10.pdf/43 104 219649 3578762 Page:Black cat 1897 07 v2 n10.pdf/44 104 219649 3578763 The History of Tom Jones, a Foundling/Book IX 0 12084291 3578764 Page:Shakespeare of Stratford (1926) Yale.djvu/91 104 21450 [3578765 rows x 3 columns] Writing XML# New in version 1.3.0. DataFrame objects have an instance method to_xml which renders the contents of the DataFrame as an XML document. Note This method does not support special properties of XML including DTD, CData, XSD schemas, processing instructions, comments, and others. Only namespaces at the root level is supported. However, stylesheet allows design changes after initial output. Let’s look at a few examples. Write an XML without options: In [392]: geom_df = pd.DataFrame( .....: { .....: "shape": ["square", "circle", "triangle"], .....: "degrees": [360, 360, 180], .....: "sides": [4, np.nan, 3], .....: } .....: ) .....: In [393]: print(geom_df.to_xml()) <?xml version='1.0' encoding='utf-8'?> <data> <row> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </row> <row> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </row> <row> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Write an XML with new root and row name: In [394]: print(geom_df.to_xml(root_name="geometry", row_name="objects")) <?xml version='1.0' encoding='utf-8'?> <geometry> <objects> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </objects> <objects> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </objects> <objects> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </objects> </geometry> Write an attribute-centric XML: In [395]: print(geom_df.to_xml(attr_cols=geom_df.columns.tolist())) <?xml version='1.0' encoding='utf-8'?> <data> <row index="0" shape="square" degrees="360" sides="4.0"/> <row index="1" shape="circle" degrees="360"/> <row index="2" shape="triangle" degrees="180" sides="3.0"/> </data> Write a mix of elements and attributes: In [396]: print( .....: geom_df.to_xml( .....: index=False, .....: attr_cols=['shape'], .....: elem_cols=['degrees', 'sides']) .....: ) .....: <?xml version='1.0' encoding='utf-8'?> <data> <row shape="square"> <degrees>360</degrees> <sides>4.0</sides> </row> <row shape="circle"> <degrees>360</degrees> <sides/> </row> <row shape="triangle"> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Any DataFrames with hierarchical columns will be flattened for XML element names with levels delimited by underscores: In [397]: ext_geom_df = pd.DataFrame( .....: { .....: "type": ["polygon", "other", "polygon"], .....: "shape": ["square", "circle", "triangle"], .....: "degrees": [360, 360, 180], .....: "sides": [4, np.nan, 3], .....: } .....: ) .....: In [398]: pvt_df = ext_geom_df.pivot_table(index='shape', .....: columns='type', .....: values=['degrees', 'sides'], .....: aggfunc='sum') .....: In [399]: pvt_df Out[399]: degrees sides type other polygon other polygon shape circle 360.0 NaN 0.0 NaN square NaN 360.0 NaN 4.0 triangle NaN 180.0 NaN 3.0 In [400]: print(pvt_df.to_xml()) <?xml version='1.0' encoding='utf-8'?> <data> <row> <shape>circle</shape> <degrees_other>360.0</degrees_other> <degrees_polygon/> <sides_other>0.0</sides_other> <sides_polygon/> </row> <row> <shape>square</shape> <degrees_other/> <degrees_polygon>360.0</degrees_polygon> <sides_other/> <sides_polygon>4.0</sides_polygon> </row> <row> <shape>triangle</shape> <degrees_other/> <degrees_polygon>180.0</degrees_polygon> <sides_other/> <sides_polygon>3.0</sides_polygon> </row> </data> Write an XML with default namespace: In [401]: print(geom_df.to_xml(namespaces={"": "https://example.com"})) <?xml version='1.0' encoding='utf-8'?> <data xmlns="https://example.com"> <row> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </row> <row> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </row> <row> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Write an XML with namespace prefix: In [402]: print( .....: geom_df.to_xml(namespaces={"doc": "https://example.com"}, .....: prefix="doc") .....: ) .....: <?xml version='1.0' encoding='utf-8'?> <doc:data xmlns:doc="https://example.com"> <doc:row> <doc:index>0</doc:index> <doc:shape>square</doc:shape> <doc:degrees>360</doc:degrees> <doc:sides>4.0</doc:sides> </doc:row> <doc:row> <doc:index>1</doc:index> <doc:shape>circle</doc:shape> <doc:degrees>360</doc:degrees> <doc:sides/> </doc:row> <doc:row> <doc:index>2</doc:index> <doc:shape>triangle</doc:shape> <doc:degrees>180</doc:degrees> <doc:sides>3.0</doc:sides> </doc:row> </doc:data> Write an XML without declaration or pretty print: In [403]: print( .....: geom_df.to_xml(xml_declaration=False, .....: pretty_print=False) .....: ) .....: <data><row><index>0</index><shape>square</shape><degrees>360</degrees><sides>4.0</sides></row><row><index>1</index><shape>circle</shape><degrees>360</degrees><sides/></row><row><index>2</index><shape>triangle</shape><degrees>180</degrees><sides>3.0</sides></row></data> Write an XML and transform with stylesheet: In [404]: xsl = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> .....: <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/> .....: <xsl:strip-space elements="*"/> .....: <xsl:template match="/data"> .....: <geometry> .....: <xsl:apply-templates select="row"/> .....: </geometry> .....: </xsl:template> .....: <xsl:template match="row"> .....: <object index="{index}"> .....: <xsl:if test="shape!='circle'"> .....: <xsl:attribute name="type">polygon</xsl:attribute> .....: </xsl:if> .....: <xsl:copy-of select="shape"/> .....: <property> .....: <xsl:copy-of select="degrees|sides"/> .....: </property> .....: </object> .....: </xsl:template> .....: </xsl:stylesheet>""" .....: In [405]: print(geom_df.to_xml(stylesheet=xsl)) <?xml version="1.0"?> <geometry> <object index="0" type="polygon"> <shape>square</shape> <property> <degrees>360</degrees> <sides>4.0</sides> </property> </object> <object index="1"> <shape>circle</shape> <property> <degrees>360</degrees> <sides/> </property> </object> <object index="2" type="polygon"> <shape>triangle</shape> <property> <degrees>180</degrees> <sides>3.0</sides> </property> </object> </geometry> XML Final Notes# All XML documents adhere to W3C specifications. Both etree and lxml parsers will fail to parse any markup document that is not well-formed or follows XML syntax rules. Do be aware HTML is not an XML document unless it follows XHTML specs. However, other popular markup types including KML, XAML, RSS, MusicML, MathML are compliant XML schemas. For above reason, if your application builds XML prior to pandas operations, use appropriate DOM libraries like etree and lxml to build the necessary document and not by string concatenation or regex adjustments. Always remember XML is a special text file with markup rules. With very large XML files (several hundred MBs to GBs), XPath and XSLT can become memory-intensive operations. Be sure to have enough available RAM for reading and writing to large XML files (roughly about 5 times the size of text). Because XSLT is a programming language, use it with caution since such scripts can pose a security risk in your environment and can run large or infinite recursive operations. Always test scripts on small fragments before full run. The etree parser supports all functionality of both read_xml and to_xml except for complex XPath and any XSLT. Though limited in features, etree is still a reliable and capable parser and tree builder. Its performance may trail lxml to a certain degree for larger files but relatively unnoticeable on small to medium size files. Excel files# The read_excel() method can read Excel 2007+ (.xlsx) files using the openpyxl Python module. Excel 2003 (.xls) files can be read using xlrd. Binary Excel (.xlsb) files can be read using pyxlsb. The to_excel() instance method is used for saving a DataFrame to Excel. Generally the semantics are similar to working with csv data. See the cookbook for some advanced strategies. Warning The xlwt package for writing old-style .xls excel files is no longer maintained. The xlrd package is now only for reading old-style .xls files. Before pandas 1.3.0, the default argument engine=None to read_excel() would result in using the xlrd engine in many cases, including new Excel 2007+ (.xlsx) files. pandas will now default to using the openpyxl engine. It is strongly encouraged to install openpyxl to read Excel 2007+ (.xlsx) files. Please do not report issues when using ``xlrd`` to read ``.xlsx`` files. This is no longer supported, switch to using openpyxl instead. Attempting to use the xlwt engine will raise a FutureWarning unless the option io.excel.xls.writer is set to "xlwt". While this option is now deprecated and will also raise a FutureWarning, it can be globally set and the warning suppressed. Users are recommended to write .xlsx files using the openpyxl engine instead. Reading Excel files# In the most basic use-case, read_excel takes a path to an Excel file, and the sheet_name indicating which sheet to parse. # Returns a DataFrame pd.read_excel("path_to_file.xls", sheet_name="Sheet1") ExcelFile class# To facilitate working with multiple sheets from the same file, the ExcelFile class can be used to wrap the file and can be passed into read_excel There will be a performance benefit for reading multiple sheets as the file is read into memory only once. xlsx = pd.ExcelFile("path_to_file.xls") df = pd.read_excel(xlsx, "Sheet1") The ExcelFile class can also be used as a context manager. with pd.ExcelFile("path_to_file.xls") as xls: df1 = pd.read_excel(xls, "Sheet1") df2 = pd.read_excel(xls, "Sheet2") The sheet_names property will generate a list of the sheet names in the file. The primary use-case for an ExcelFile is parsing multiple sheets with different parameters: data = {} # For when Sheet1's format differs from Sheet2 with pd.ExcelFile("path_to_file.xls") as xls: data["Sheet1"] = pd.read_excel(xls, "Sheet1", index_col=None, na_values=["NA"]) data["Sheet2"] = pd.read_excel(xls, "Sheet2", index_col=1) Note that if the same parsing parameters are used for all sheets, a list of sheet names can simply be passed to read_excel with no loss in performance. # using the ExcelFile class data = {} with pd.ExcelFile("path_to_file.xls") as xls: data["Sheet1"] = pd.read_excel(xls, "Sheet1", index_col=None, na_values=["NA"]) data["Sheet2"] = pd.read_excel(xls, "Sheet2", index_col=None, na_values=["NA"]) # equivalent using the read_excel function data = pd.read_excel( "path_to_file.xls", ["Sheet1", "Sheet2"], index_col=None, na_values=["NA"] ) ExcelFile can also be called with a xlrd.book.Book object as a parameter. This allows the user to control how the excel file is read. For example, sheets can be loaded on demand by calling xlrd.open_workbook() with on_demand=True. import xlrd xlrd_book = xlrd.open_workbook("path_to_file.xls", on_demand=True) with pd.ExcelFile(xlrd_book) as xls: df1 = pd.read_excel(xls, "Sheet1") df2 = pd.read_excel(xls, "Sheet2") Specifying sheets# Note The second argument is sheet_name, not to be confused with ExcelFile.sheet_names. Note An ExcelFile’s attribute sheet_names provides access to a list of sheets. The arguments sheet_name allows specifying the sheet or sheets to read. The default value for sheet_name is 0, indicating to read the first sheet Pass a string to refer to the name of a particular sheet in the workbook. Pass an integer to refer to the index of a sheet. Indices follow Python convention, beginning at 0. Pass a list of either strings or integers, to return a dictionary of specified sheets. Pass a None to return a dictionary of all available sheets. # Returns a DataFrame pd.read_excel("path_to_file.xls", "Sheet1", index_col=None, na_values=["NA"]) Using the sheet index: # Returns a DataFrame pd.read_excel("path_to_file.xls", 0, index_col=None, na_values=["NA"]) Using all default values: # Returns a DataFrame pd.read_excel("path_to_file.xls") Using None to get all sheets: # Returns a dictionary of DataFrames pd.read_excel("path_to_file.xls", sheet_name=None) Using a list to get multiple sheets: # Returns the 1st and 4th sheet, as a dictionary of DataFrames. pd.read_excel("path_to_file.xls", sheet_name=["Sheet1", 3]) read_excel can read more than one sheet, by setting sheet_name to either a list of sheet names, a list of sheet positions, or None to read all sheets. Sheets can be specified by sheet index or sheet name, using an integer or string, respectively. Reading a MultiIndex# read_excel can read a MultiIndex index, by passing a list of columns to index_col and a MultiIndex column by passing a list of rows to header. If either the index or columns have serialized level names those will be read in as well by specifying the rows/columns that make up the levels. For example, to read in a MultiIndex index without names: In [406]: df = pd.DataFrame( .....: {"a": [1, 2, 3, 4], "b": [5, 6, 7, 8]}, .....: index=pd.MultiIndex.from_product([["a", "b"], ["c", "d"]]), .....: ) .....: In [407]: df.to_excel("path_to_file.xlsx") In [408]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1]) In [409]: df Out[409]: a b a c 1 5 d 2 6 b c 3 7 d 4 8 If the index has level names, they will parsed as well, using the same parameters. In [410]: df.index = df.index.set_names(["lvl1", "lvl2"]) In [411]: df.to_excel("path_to_file.xlsx") In [412]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1]) In [413]: df Out[413]: a b lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 If the source file has both MultiIndex index and columns, lists specifying each should be passed to index_col and header: In [414]: df.columns = pd.MultiIndex.from_product([["a"], ["b", "d"]], names=["c1", "c2"]) In [415]: df.to_excel("path_to_file.xlsx") In [416]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1], header=[0, 1]) In [417]: df Out[417]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 Missing values in columns specified in index_col will be forward filled to allow roundtripping with to_excel for merged_cells=True. To avoid forward filling the missing values use set_index after reading the data instead of index_col. Parsing specific columns# It is often the case that users will insert columns to do temporary computations in Excel and you may not want to read in those columns. read_excel takes a usecols keyword to allow you to specify a subset of columns to parse. Changed in version 1.0.0. Passing in an integer for usecols will no longer work. Please pass in a list of ints from 0 to usecols inclusive instead. You can specify a comma-delimited set of Excel columns and ranges as a string: pd.read_excel("path_to_file.xls", "Sheet1", usecols="A,C:E") If usecols is a list of integers, then it is assumed to be the file column indices to be parsed. pd.read_excel("path_to_file.xls", "Sheet1", usecols=[0, 2, 3]) Element order is ignored, so usecols=[0, 1] is the same as [1, 0]. If usecols is a list of strings, it is assumed that each string corresponds to a column name provided either by the user in names or inferred from the document header row(s). Those strings define which columns will be parsed: pd.read_excel("path_to_file.xls", "Sheet1", usecols=["foo", "bar"]) Element order is ignored, so usecols=['baz', 'joe'] is the same as ['joe', 'baz']. If usecols is callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True. pd.read_excel("path_to_file.xls", "Sheet1", usecols=lambda x: x.isalpha()) Parsing dates# Datetime-like values are normally automatically converted to the appropriate dtype when reading the excel file. But if you have a column of strings that look like dates (but are not actually formatted as dates in excel), you can use the parse_dates keyword to parse those strings to datetimes: pd.read_excel("path_to_file.xls", "Sheet1", parse_dates=["date_strings"]) Cell converters# It is possible to transform the contents of Excel cells via the converters option. For instance, to convert a column to boolean: pd.read_excel("path_to_file.xls", "Sheet1", converters={"MyBools": bool}) This options handles missing values and treats exceptions in the converters as missing data. Transformations are applied cell by cell rather than to the column as a whole, so the array dtype is not guaranteed. For instance, a column of integers with missing values cannot be transformed to an array with integer dtype, because NaN is strictly a float. You can manually mask missing data to recover integer dtype: def cfun(x): return int(x) if x else -1 pd.read_excel("path_to_file.xls", "Sheet1", converters={"MyInts": cfun}) Dtype specifications# As an alternative to converters, the type for an entire column can be specified using the dtype keyword, which takes a dictionary mapping column names to types. To interpret data with no type inference, use the type str or object. pd.read_excel("path_to_file.xls", dtype={"MyInts": "int64", "MyText": str}) Writing Excel files# Writing Excel files to disk# To write a DataFrame object to a sheet of an Excel file, you can use the to_excel instance method. The arguments are largely the same as to_csv described above, the first argument being the name of the excel file, and the optional second argument the name of the sheet to which the DataFrame should be written. For example: df.to_excel("path_to_file.xlsx", sheet_name="Sheet1") Files with a .xls extension will be written using xlwt and those with a .xlsx extension will be written using xlsxwriter (if available) or openpyxl. The DataFrame will be written in a way that tries to mimic the REPL output. The index_label will be placed in the second row instead of the first. You can place it in the first row by setting the merge_cells option in to_excel() to False: df.to_excel("path_to_file.xlsx", index_label="label", merge_cells=False) In order to write separate DataFrames to separate sheets in a single Excel file, one can pass an ExcelWriter. with pd.ExcelWriter("path_to_file.xlsx") as writer: df1.to_excel(writer, sheet_name="Sheet1") df2.to_excel(writer, sheet_name="Sheet2") Writing Excel files to memory# pandas supports writing Excel files to buffer-like objects such as StringIO or BytesIO using ExcelWriter. from io import BytesIO bio = BytesIO() # By setting the 'engine' in the ExcelWriter constructor. writer = pd.ExcelWriter(bio, engine="xlsxwriter") df.to_excel(writer, sheet_name="Sheet1") # Save the workbook writer.save() # Seek to the beginning and read to copy the workbook to a variable in memory bio.seek(0) workbook = bio.read() Note engine is optional but recommended. Setting the engine determines the version of workbook produced. Setting engine='xlrd' will produce an Excel 2003-format workbook (xls). Using either 'openpyxl' or 'xlsxwriter' will produce an Excel 2007-format workbook (xlsx). If omitted, an Excel 2007-formatted workbook is produced. Excel writer engines# Deprecated since version 1.2.0: As the xlwt package is no longer maintained, the xlwt engine will be removed from a future version of pandas. This is the only engine in pandas that supports writing to .xls files. pandas chooses an Excel writer via two methods: the engine keyword argument the filename extension (via the default specified in config options) By default, pandas uses the XlsxWriter for .xlsx, openpyxl for .xlsm, and xlwt for .xls files. If you have multiple engines installed, you can set the default engine through setting the config options io.excel.xlsx.writer and io.excel.xls.writer. pandas will fall back on openpyxl for .xlsx files if Xlsxwriter is not available. To specify which writer you want to use, you can pass an engine keyword argument to to_excel and to ExcelWriter. The built-in engines are: openpyxl: version 2.4 or higher is required xlsxwriter xlwt # By setting the 'engine' in the DataFrame 'to_excel()' methods. df.to_excel("path_to_file.xlsx", sheet_name="Sheet1", engine="xlsxwriter") # By setting the 'engine' in the ExcelWriter constructor. writer = pd.ExcelWriter("path_to_file.xlsx", engine="xlsxwriter") # Or via pandas configuration. from pandas import options # noqa: E402 options.io.excel.xlsx.writer = "xlsxwriter" df.to_excel("path_to_file.xlsx", sheet_name="Sheet1") Style and formatting# The look and feel of Excel worksheets created from pandas can be modified using the following parameters on the DataFrame’s to_excel method. float_format : Format string for floating point numbers (default None). freeze_panes : A tuple of two integers representing the bottommost row and rightmost column to freeze. Each of these parameters is one-based, so (1, 1) will freeze the first row and first column (default None). Using the Xlsxwriter engine provides many options for controlling the format of an Excel worksheet created with the to_excel method. Excellent examples can be found in the Xlsxwriter documentation here: https://xlsxwriter.readthedocs.io/working_with_pandas.html OpenDocument Spreadsheets# New in version 0.25. The read_excel() method can also read OpenDocument spreadsheets using the odfpy module. The semantics and features for reading OpenDocument spreadsheets match what can be done for Excel files using engine='odf'. # Returns a DataFrame pd.read_excel("path_to_file.ods", engine="odf") Note Currently pandas only supports reading OpenDocument spreadsheets. Writing is not implemented. Binary Excel (.xlsb) files# New in version 1.0.0. The read_excel() method can also read binary Excel files using the pyxlsb module. The semantics and features for reading binary Excel files mostly match what can be done for Excel files using engine='pyxlsb'. pyxlsb does not recognize datetime types in files and will return floats instead. # Returns a DataFrame pd.read_excel("path_to_file.xlsb", engine="pyxlsb") Note Currently pandas only supports reading binary Excel files. Writing is not implemented. Clipboard# A handy way to grab data is to use the read_clipboard() method, which takes the contents of the clipboard buffer and passes them to the read_csv method. For instance, you can copy the following text to the clipboard (CTRL-C on many operating systems): A B C x 1 4 p y 2 5 q z 3 6 r And then import the data directly to a DataFrame by calling: >>> clipdf = pd.read_clipboard() >>> clipdf A B C x 1 4 p y 2 5 q z 3 6 r The to_clipboard method can be used to write the contents of a DataFrame to the clipboard. Following which you can paste the clipboard contents into other applications (CTRL-V on many operating systems). Here we illustrate writing a DataFrame into clipboard and reading it back. >>> df = pd.DataFrame( ... {"A": [1, 2, 3], "B": [4, 5, 6], "C": ["p", "q", "r"]}, index=["x", "y", "z"] ... ) >>> df A B C x 1 4 p y 2 5 q z 3 6 r >>> df.to_clipboard() >>> pd.read_clipboard() A B C x 1 4 p y 2 5 q z 3 6 r We can see that we got the same content back, which we had earlier written to the clipboard. Note You may need to install xclip or xsel (with PyQt5, PyQt4 or qtpy) on Linux to use these methods. Pickling# All pandas objects are equipped with to_pickle methods which use Python’s cPickle module to save data structures to disk using the pickle format. In [418]: df Out[418]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 In [419]: df.to_pickle("foo.pkl") The read_pickle function in the pandas namespace can be used to load any pickled pandas object (or any other pickled object) from file: In [420]: pd.read_pickle("foo.pkl") Out[420]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 Warning Loading pickled data received from untrusted sources can be unsafe. See: https://docs.python.org/3/library/pickle.html Warning read_pickle() is only guaranteed backwards compatible back to pandas version 0.20.3 Compressed pickle files# read_pickle(), DataFrame.to_pickle() and Series.to_pickle() can read and write compressed pickle files. The compression types of gzip, bz2, xz, zstd are supported for reading and writing. The zip file format only supports reading and must contain only one data file to be read. The compression type can be an explicit parameter or be inferred from the file extension. If ‘infer’, then use gzip, bz2, zip, xz, zstd if filename ends in '.gz', '.bz2', '.zip', '.xz', or '.zst', respectively. The compression parameter can also be a dict in order to pass options to the compression protocol. It must have a 'method' key set to the name of the compression protocol, which must be one of {'zip', 'gzip', 'bz2', 'xz', 'zstd'}. All other key-value pairs are passed to the underlying compression library. In [421]: df = pd.DataFrame( .....: { .....: "A": np.random.randn(1000), .....: "B": "foo", .....: "C": pd.date_range("20130101", periods=1000, freq="s"), .....: } .....: ) .....: In [422]: df Out[422]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] Using an explicit compression type: In [423]: df.to_pickle("data.pkl.compress", compression="gzip") In [424]: rt = pd.read_pickle("data.pkl.compress", compression="gzip") In [425]: rt Out[425]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] Inferring compression type from the extension: In [426]: df.to_pickle("data.pkl.xz", compression="infer") In [427]: rt = pd.read_pickle("data.pkl.xz", compression="infer") In [428]: rt Out[428]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] The default is to ‘infer’: In [429]: df.to_pickle("data.pkl.gz") In [430]: rt = pd.read_pickle("data.pkl.gz") In [431]: rt Out[431]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] In [432]: df["A"].to_pickle("s1.pkl.bz2") In [433]: rt = pd.read_pickle("s1.pkl.bz2") In [434]: rt Out[434]: 0 -0.828876 1 -0.110383 2 2.357598 3 -1.620073 4 0.440903 ... 995 -1.177365 996 1.236988 997 0.743946 998 -0.533097 999 -0.140850 Name: A, Length: 1000, dtype: float64 Passing options to the compression protocol in order to speed up compression: In [435]: df.to_pickle("data.pkl.gz", compression={"method": "gzip", "compresslevel": 1}) msgpack# pandas support for msgpack has been removed in version 1.0.0. It is recommended to use pickle instead. Alternatively, you can also the Arrow IPC serialization format for on-the-wire transmission of pandas objects. For documentation on pyarrow, see here. HDF5 (PyTables)# HDFStore is a dict-like object which reads and writes pandas using the high performance HDF5 format using the excellent PyTables library. See the cookbook for some advanced strategies Warning pandas uses PyTables for reading and writing HDF5 files, which allows serializing object-dtype data with pickle. Loading pickled data received from untrusted sources can be unsafe. See: https://docs.python.org/3/library/pickle.html for more. In [436]: store = pd.HDFStore("store.h5") In [437]: print(store) <class 'pandas.io.pytables.HDFStore'> File path: store.h5 Objects can be written to the file just like adding key-value pairs to a dict: In [438]: index = pd.date_range("1/1/2000", periods=8) In [439]: s = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"]) In [440]: df = pd.DataFrame(np.random.randn(8, 3), index=index, columns=["A", "B", "C"]) # store.put('s', s) is an equivalent method In [441]: store["s"] = s In [442]: store["df"] = df In [443]: store Out[443]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 In a current or later Python session, you can retrieve stored objects: # store.get('df') is an equivalent method In [444]: store["df"] Out[444]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 # dotted (attribute) access provides get as well In [445]: store.df Out[445]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Deletion of the object specified by the key: # store.remove('df') is an equivalent method In [446]: del store["df"] In [447]: store Out[447]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 Closing a Store and using a context manager: In [448]: store.close() In [449]: store Out[449]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 In [450]: store.is_open Out[450]: False # Working with, and automatically closing the store using a context manager In [451]: with pd.HDFStore("store.h5") as store: .....: store.keys() .....: Read/write API# HDFStore supports a top-level API using read_hdf for reading and to_hdf for writing, similar to how read_csv and to_csv work. In [452]: df_tl = pd.DataFrame({"A": list(range(5)), "B": list(range(5))}) In [453]: df_tl.to_hdf("store_tl.h5", "table", append=True) In [454]: pd.read_hdf("store_tl.h5", "table", where=["index>2"]) Out[454]: A B 3 3 3 4 4 4 HDFStore will by default not drop rows that are all missing. This behavior can be changed by setting dropna=True. In [455]: df_with_missing = pd.DataFrame( .....: { .....: "col1": [0, np.nan, 2], .....: "col2": [1, np.nan, np.nan], .....: } .....: ) .....: In [456]: df_with_missing Out[456]: col1 col2 0 0.0 1.0 1 NaN NaN 2 2.0 NaN In [457]: df_with_missing.to_hdf("file.h5", "df_with_missing", format="table", mode="w") In [458]: pd.read_hdf("file.h5", "df_with_missing") Out[458]: col1 col2 0 0.0 1.0 1 NaN NaN 2 2.0 NaN In [459]: df_with_missing.to_hdf( .....: "file.h5", "df_with_missing", format="table", mode="w", dropna=True .....: ) .....: In [460]: pd.read_hdf("file.h5", "df_with_missing") Out[460]: col1 col2 0 0.0 1.0 2 2.0 NaN Fixed format# The examples above show storing using put, which write the HDF5 to PyTables in a fixed array format, called the fixed format. These types of stores are not appendable once written (though you can simply remove them and rewrite). Nor are they queryable; they must be retrieved in their entirety. They also do not support dataframes with non-unique column names. The fixed format stores offer very fast writing and slightly faster reading than table stores. This format is specified by default when using put or to_hdf or by format='fixed' or format='f'. Warning A fixed format will raise a TypeError if you try to retrieve using a where: >>> pd.DataFrame(np.random.randn(10, 2)).to_hdf("test_fixed.h5", "df") >>> pd.read_hdf("test_fixed.h5", "df", where="index>5") TypeError: cannot pass a where specification when reading a fixed format. this store must be selected in its entirety Table format# HDFStore supports another PyTables format on disk, the table format. Conceptually a table is shaped very much like a DataFrame, with rows and columns. A table may be appended to in the same or other sessions. In addition, delete and query type operations are supported. This format is specified by format='table' or format='t' to append or put or to_hdf. This format can be set as an option as well pd.set_option('io.hdf.default_format','table') to enable put/append/to_hdf to by default store in the table format. In [461]: store = pd.HDFStore("store.h5") In [462]: df1 = df[0:4] In [463]: df2 = df[4:] # append data (creates a table automatically) In [464]: store.append("df", df1) In [465]: store.append("df", df2) In [466]: store Out[466]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # select the entire object In [467]: store.select("df") Out[467]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 # the type of stored data In [468]: store.root.df._v_attrs.pandas_type Out[468]: 'frame_table' Note You can also create a table by passing format='table' or format='t' to a put operation. Hierarchical keys# Keys to a store can be specified as a string. These can be in a hierarchical path-name like format (e.g. foo/bar/bah), which will generate a hierarchy of sub-stores (or Groups in PyTables parlance). Keys can be specified without the leading ‘/’ and are always absolute (e.g. ‘foo’ refers to ‘/foo’). Removal operations can remove everything in the sub-store and below, so be careful. In [469]: store.put("foo/bar/bah", df) In [470]: store.append("food/orange", df) In [471]: store.append("food/apple", df) In [472]: store Out[472]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # a list of keys are returned In [473]: store.keys() Out[473]: ['/df', '/food/apple', '/food/orange', '/foo/bar/bah'] # remove all nodes under this level In [474]: store.remove("food") In [475]: store Out[475]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 You can walk through the group hierarchy using the walk method which will yield a tuple for each group key along with the relative keys of its contents. In [476]: for (path, subgroups, subkeys) in store.walk(): .....: for subgroup in subgroups: .....: print("GROUP: {}/{}".format(path, subgroup)) .....: for subkey in subkeys: .....: key = "/".join([path, subkey]) .....: print("KEY: {}".format(key)) .....: print(store.get(key)) .....: GROUP: /foo KEY: /df A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 GROUP: /foo/bar KEY: /foo/bar/bah A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Warning Hierarchical keys cannot be retrieved as dotted (attribute) access as described above for items stored under the root node. In [8]: store.foo.bar.bah AttributeError: 'HDFStore' object has no attribute 'foo' # you can directly access the actual PyTables node but using the root node In [9]: store.root.foo.bar.bah Out[9]: /foo/bar/bah (Group) '' children := ['block0_items' (Array), 'block0_values' (Array), 'axis0' (Array), 'axis1' (Array)] Instead, use explicit string based keys: In [477]: store["foo/bar/bah"] Out[477]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Storing types# Storing mixed types in a table# Storing mixed-dtype data is supported. Strings are stored as a fixed-width using the maximum size of the appended column. Subsequent attempts at appending longer strings will raise a ValueError. Passing min_itemsize={`values`: size} as a parameter to append will set a larger minimum for the string columns. Storing floats, strings, ints, bools, datetime64 are currently supported. For string columns, passing nan_rep = 'nan' to append will change the default nan representation on disk (which converts to/from np.nan), this defaults to nan. In [478]: df_mixed = pd.DataFrame( .....: { .....: "A": np.random.randn(8), .....: "B": np.random.randn(8), .....: "C": np.array(np.random.randn(8), dtype="float32"), .....: "string": "string", .....: "int": 1, .....: "bool": True, .....: "datetime64": pd.Timestamp("20010102"), .....: }, .....: index=list(range(8)), .....: ) .....: In [479]: df_mixed.loc[df_mixed.index[3:5], ["A", "B", "string", "datetime64"]] = np.nan In [480]: store.append("df_mixed", df_mixed, min_itemsize={"values": 50}) In [481]: df_mixed1 = store.select("df_mixed") In [482]: df_mixed1 Out[482]: A B C string int bool datetime64 0 1.778161 -0.898283 -0.263043 string 1 True 2001-01-02 1 -0.913867 -0.218499 -0.639244 string 1 True 2001-01-02 2 -0.030004 1.408028 -0.866305 string 1 True 2001-01-02 3 NaN NaN -0.225250 NaN 1 True NaT 4 NaN NaN -0.890978 NaN 1 True NaT 5 0.081323 0.520995 -0.553839 string 1 True 2001-01-02 6 -0.268494 0.620028 -2.762875 string 1 True 2001-01-02 7 0.168016 0.159416 -1.244763 string 1 True 2001-01-02 In [483]: df_mixed1.dtypes.value_counts() Out[483]: float64 2 float32 1 object 1 int64 1 bool 1 datetime64[ns] 1 dtype: int64 # we have provided a minimum string column size In [484]: store.root.df_mixed.table Out[484]: /df_mixed/table (Table(8,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(2,), dflt=0.0, pos=1), "values_block_1": Float32Col(shape=(1,), dflt=0.0, pos=2), "values_block_2": StringCol(itemsize=50, shape=(1,), dflt=b'', pos=3), "values_block_3": Int64Col(shape=(1,), dflt=0, pos=4), "values_block_4": BoolCol(shape=(1,), dflt=False, pos=5), "values_block_5": Int64Col(shape=(1,), dflt=0, pos=6)} byteorder := 'little' chunkshape := (689,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False} Storing MultiIndex DataFrames# Storing MultiIndex DataFrames as tables is very similar to storing/selecting from homogeneous index DataFrames. In [485]: index = pd.MultiIndex( .....: levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], .....: codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], .....: names=["foo", "bar"], .....: ) .....: In [486]: df_mi = pd.DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"]) In [487]: df_mi Out[487]: A B C foo bar foo one -1.280289 0.692545 -0.536722 two 1.005707 0.296917 0.139796 three -1.083889 0.811865 1.648435 bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 baz two 0.183573 0.145277 0.308146 three -1.043530 -0.708145 1.430905 qux one -0.850136 0.813949 1.508891 two -1.556154 0.187597 1.176488 three -1.246093 -0.002726 -0.444249 In [488]: store.append("df_mi", df_mi) In [489]: store.select("df_mi") Out[489]: A B C foo bar foo one -1.280289 0.692545 -0.536722 two 1.005707 0.296917 0.139796 three -1.083889 0.811865 1.648435 bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 baz two 0.183573 0.145277 0.308146 three -1.043530 -0.708145 1.430905 qux one -0.850136 0.813949 1.508891 two -1.556154 0.187597 1.176488 three -1.246093 -0.002726 -0.444249 # the levels are automatically included as data columns In [490]: store.select("df_mi", "foo=bar") Out[490]: A B C foo bar bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 Note The index keyword is reserved and cannot be use as a level name. Querying# Querying a table# select and delete operations have an optional criterion that can be specified to select/delete only a subset of the data. This allows one to have a very large on-disk table and retrieve only a portion of the data. A query is specified using the Term class under the hood, as a boolean expression. index and columns are supported indexers of DataFrames. if data_columns are specified, these can be used as additional indexers. level name in a MultiIndex, with default name level_0, level_1, … if not provided. Valid comparison operators are: =, ==, !=, >, >=, <, <= Valid boolean expressions are combined with: | : or & : and ( and ) : for grouping These rules are similar to how boolean expressions are used in pandas for indexing. Note = will be automatically expanded to the comparison operator == ~ is the not operator, but can only be used in very limited circumstances If a list/tuple of expressions is passed they will be combined via & The following are valid expressions: 'index >= date' "columns = ['A', 'D']" "columns in ['A', 'D']" 'columns = A' 'columns == A' "~(columns = ['A', 'B'])" 'index > df.index[3] & string = "bar"' '(index > df.index[3] & index <= df.index[6]) | string = "bar"' "ts >= Timestamp('2012-02-01')" "major_axis>=20130101" The indexers are on the left-hand side of the sub-expression: columns, major_axis, ts The right-hand side of the sub-expression (after a comparison operator) can be: functions that will be evaluated, e.g. Timestamp('2012-02-01') strings, e.g. "bar" date-like, e.g. 20130101, or "20130101" lists, e.g. "['A', 'B']" variables that are defined in the local names space, e.g. date Note Passing a string to a query by interpolating it into the query expression is not recommended. Simply assign the string of interest to a variable and use that variable in an expression. For example, do this string = "HolyMoly'" store.select("df", "index == string") instead of this string = "HolyMoly'" store.select('df', f'index == {string}') The latter will not work and will raise a SyntaxError.Note that there’s a single quote followed by a double quote in the string variable. If you must interpolate, use the '%r' format specifier store.select("df", "index == %r" % string) which will quote string. Here are some examples: In [491]: dfq = pd.DataFrame( .....: np.random.randn(10, 4), .....: columns=list("ABCD"), .....: index=pd.date_range("20130101", periods=10), .....: ) .....: In [492]: store.append("dfq", dfq, format="table", data_columns=True) Use boolean expressions, with in-line function evaluation. In [493]: store.select("dfq", "index>pd.Timestamp('20130104') & columns=['A', 'B']") Out[493]: A B 2013-01-05 1.366810 1.073372 2013-01-06 2.119746 -2.628174 2013-01-07 0.337920 -0.634027 2013-01-08 1.053434 1.109090 2013-01-09 -0.772942 -0.269415 2013-01-10 0.048562 -0.285920 Use inline column reference. In [494]: store.select("dfq", where="A>0 or C>0") Out[494]: A B C D 2013-01-01 0.856838 1.491776 0.001283 0.701816 2013-01-02 -1.097917 0.102588 0.661740 0.443531 2013-01-03 0.559313 -0.459055 -1.222598 -0.455304 2013-01-05 1.366810 1.073372 -0.994957 0.755314 2013-01-06 2.119746 -2.628174 -0.089460 -0.133636 2013-01-07 0.337920 -0.634027 0.421107 0.604303 2013-01-08 1.053434 1.109090 -0.367891 -0.846206 2013-01-10 0.048562 -0.285920 1.334100 0.194462 The columns keyword can be supplied to select a list of columns to be returned, this is equivalent to passing a 'columns=list_of_columns_to_filter': In [495]: store.select("df", "columns=['A', 'B']") Out[495]: A B 2000-01-01 -0.398501 -0.677311 2000-01-02 -1.167564 -0.593353 2000-01-03 -0.131959 0.089012 2000-01-04 0.169405 -1.358046 2000-01-05 0.492195 0.076693 2000-01-06 -0.285283 -1.210529 2000-01-07 0.941577 -0.342447 2000-01-08 0.052607 2.093214 start and stop parameters can be specified to limit the total search space. These are in terms of the total number of rows in a table. Note select will raise a ValueError if the query expression has an unknown variable reference. Usually this means that you are trying to select on a column that is not a data_column. select will raise a SyntaxError if the query expression is not valid. Query timedelta64[ns]# You can store and query using the timedelta64[ns] type. Terms can be specified in the format: <float>(<unit>), where float may be signed (and fractional), and unit can be D,s,ms,us,ns for the timedelta. Here’s an example: In [496]: from datetime import timedelta In [497]: dftd = pd.DataFrame( .....: { .....: "A": pd.Timestamp("20130101"), .....: "B": [ .....: pd.Timestamp("20130101") + timedelta(days=i, seconds=10) .....: for i in range(10) .....: ], .....: } .....: ) .....: In [498]: dftd["C"] = dftd["A"] - dftd["B"] In [499]: dftd Out[499]: A B C 0 2013-01-01 2013-01-01 00:00:10 -1 days +23:59:50 1 2013-01-01 2013-01-02 00:00:10 -2 days +23:59:50 2 2013-01-01 2013-01-03 00:00:10 -3 days +23:59:50 3 2013-01-01 2013-01-04 00:00:10 -4 days +23:59:50 4 2013-01-01 2013-01-05 00:00:10 -5 days +23:59:50 5 2013-01-01 2013-01-06 00:00:10 -6 days +23:59:50 6 2013-01-01 2013-01-07 00:00:10 -7 days +23:59:50 7 2013-01-01 2013-01-08 00:00:10 -8 days +23:59:50 8 2013-01-01 2013-01-09 00:00:10 -9 days +23:59:50 9 2013-01-01 2013-01-10 00:00:10 -10 days +23:59:50 In [500]: store.append("dftd", dftd, data_columns=True) In [501]: store.select("dftd", "C<'-3.5D'") Out[501]: A B C 4 2013-01-01 2013-01-05 00:00:10 -5 days +23:59:50 5 2013-01-01 2013-01-06 00:00:10 -6 days +23:59:50 6 2013-01-01 2013-01-07 00:00:10 -7 days +23:59:50 7 2013-01-01 2013-01-08 00:00:10 -8 days +23:59:50 8 2013-01-01 2013-01-09 00:00:10 -9 days +23:59:50 9 2013-01-01 2013-01-10 00:00:10 -10 days +23:59:50 Query MultiIndex# Selecting from a MultiIndex can be achieved by using the name of the level. In [502]: df_mi.index.names Out[502]: FrozenList(['foo', 'bar']) In [503]: store.select("df_mi", "foo=baz and bar=two") Out[503]: A B C foo bar baz two 0.183573 0.145277 0.308146 If the MultiIndex levels names are None, the levels are automatically made available via the level_n keyword with n the level of the MultiIndex you want to select from. In [504]: index = pd.MultiIndex( .....: levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], .....: codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], .....: ) .....: In [505]: df_mi_2 = pd.DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"]) In [506]: df_mi_2 Out[506]: A B C foo one -0.646538 1.210676 -0.315409 two 1.528366 0.376542 0.174490 three 1.247943 -0.742283 0.710400 bar one 0.434128 -1.246384 1.139595 two 1.388668 -0.413554 -0.666287 baz two 0.010150 -0.163820 -0.115305 three 0.216467 0.633720 0.473945 qux one -0.155446 1.287082 0.320201 two -1.256989 0.874920 0.765944 three 0.025557 -0.729782 -0.127439 In [507]: store.append("df_mi_2", df_mi_2) # the levels are automatically included as data columns with keyword level_n In [508]: store.select("df_mi_2", "level_0=foo and level_1=two") Out[508]: A B C foo two 1.528366 0.376542 0.17449 Indexing# You can create/modify an index for a table with create_table_index after data is already in the table (after and append/put operation). Creating a table index is highly encouraged. This will speed your queries a great deal when you use a select with the indexed dimension as the where. Note Indexes are automagically created on the indexables and any data columns you specify. This behavior can be turned off by passing index=False to append. # we have automagically already created an index (in the first section) In [509]: i = store.root.df.table.cols.index.index In [510]: i.optlevel, i.kind Out[510]: (6, 'medium') # change an index by passing new parameters In [511]: store.create_table_index("df", optlevel=9, kind="full") In [512]: i = store.root.df.table.cols.index.index In [513]: i.optlevel, i.kind Out[513]: (9, 'full') Oftentimes when appending large amounts of data to a store, it is useful to turn off index creation for each append, then recreate at the end. In [514]: df_1 = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [515]: df_2 = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [516]: st = pd.HDFStore("appends.h5", mode="w") In [517]: st.append("df", df_1, data_columns=["B"], index=False) In [518]: st.append("df", df_2, data_columns=["B"], index=False) In [519]: st.get_storer("df").table Out[519]: /df/table (Table(20,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2)} byteorder := 'little' chunkshape := (2730,) Then create the index when finished appending. In [520]: st.create_table_index("df", columns=["B"], optlevel=9, kind="full") In [521]: st.get_storer("df").table Out[521]: /df/table (Table(20,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2)} byteorder := 'little' chunkshape := (2730,) autoindex := True colindexes := { "B": Index(9, fullshuffle, zlib(1)).is_csi=True} In [522]: st.close() See here for how to create a completely-sorted-index (CSI) on an existing store. Query via data columns# You can designate (and index) certain columns that you want to be able to perform queries (other than the indexable columns, which you can always query). For instance say you want to perform this common operation, on-disk, and return just the frame that matches this query. You can specify data_columns = True to force all columns to be data_columns. In [523]: df_dc = df.copy() In [524]: df_dc["string"] = "foo" In [525]: df_dc.loc[df_dc.index[4:6], "string"] = np.nan In [526]: df_dc.loc[df_dc.index[7:9], "string"] = "bar" In [527]: df_dc["string2"] = "cool" In [528]: df_dc.loc[df_dc.index[1:3], ["B", "C"]] = 1.0 In [529]: df_dc Out[529]: A B C string string2 2000-01-01 -0.398501 -0.677311 -0.874991 foo cool 2000-01-02 -1.167564 1.000000 1.000000 foo cool 2000-01-03 -0.131959 1.000000 1.000000 foo cool 2000-01-04 0.169405 -1.358046 -0.105563 foo cool 2000-01-05 0.492195 0.076693 0.213685 NaN cool 2000-01-06 -0.285283 -1.210529 -1.408386 NaN cool 2000-01-07 0.941577 -0.342447 0.222031 foo cool 2000-01-08 0.052607 2.093214 1.064908 bar cool # on-disk operations In [530]: store.append("df_dc", df_dc, data_columns=["B", "C", "string", "string2"]) In [531]: store.select("df_dc", where="B > 0") Out[531]: A B C string string2 2000-01-02 -1.167564 1.000000 1.000000 foo cool 2000-01-03 -0.131959 1.000000 1.000000 foo cool 2000-01-05 0.492195 0.076693 0.213685 NaN cool 2000-01-08 0.052607 2.093214 1.064908 bar cool # getting creative In [532]: store.select("df_dc", "B > 0 & C > 0 & string == foo") Out[532]: A B C string string2 2000-01-02 -1.167564 1.0 1.0 foo cool 2000-01-03 -0.131959 1.0 1.0 foo cool # this is in-memory version of this type of selection In [533]: df_dc[(df_dc.B > 0) & (df_dc.C > 0) & (df_dc.string == "foo")] Out[533]: A B C string string2 2000-01-02 -1.167564 1.0 1.0 foo cool 2000-01-03 -0.131959 1.0 1.0 foo cool # we have automagically created this index and the B/C/string/string2 # columns are stored separately as ``PyTables`` columns In [534]: store.root.df_dc.table Out[534]: /df_dc/table (Table(8,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2), "C": Float64Col(shape=(), dflt=0.0, pos=3), "string": StringCol(itemsize=3, shape=(), dflt=b'', pos=4), "string2": StringCol(itemsize=4, shape=(), dflt=b'', pos=5)} byteorder := 'little' chunkshape := (1680,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False, "B": Index(6, mediumshuffle, zlib(1)).is_csi=False, "C": Index(6, mediumshuffle, zlib(1)).is_csi=False, "string": Index(6, mediumshuffle, zlib(1)).is_csi=False, "string2": Index(6, mediumshuffle, zlib(1)).is_csi=False} There is some performance degradation by making lots of columns into data columns, so it is up to the user to designate these. In addition, you cannot change data columns (nor indexables) after the first append/put operation (Of course you can simply read in the data and create a new table!). Iterator# You can pass iterator=True or chunksize=number_in_a_chunk to select and select_as_multiple to return an iterator on the results. The default is 50,000 rows returned in a chunk. In [535]: for df in store.select("df", chunksize=3): .....: print(df) .....: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 A B C 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 A B C 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Note You can also use the iterator with read_hdf which will open, then automatically close the store when finished iterating. for df in pd.read_hdf("store.h5", "df", chunksize=3): print(df) Note, that the chunksize keyword applies to the source rows. So if you are doing a query, then the chunksize will subdivide the total rows in the table and the query applied, returning an iterator on potentially unequal sized chunks. Here is a recipe for generating a query and using it to create equal sized return chunks. In [536]: dfeq = pd.DataFrame({"number": np.arange(1, 11)}) In [537]: dfeq Out[537]: number 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 In [538]: store.append("dfeq", dfeq, data_columns=["number"]) In [539]: def chunks(l, n): .....: return [l[i: i + n] for i in range(0, len(l), n)] .....: In [540]: evens = [2, 4, 6, 8, 10] In [541]: coordinates = store.select_as_coordinates("dfeq", "number=evens") In [542]: for c in chunks(coordinates, 2): .....: print(store.select("dfeq", where=c)) .....: number 1 2 3 4 number 5 6 7 8 number 9 10 Advanced queries# Select a single column# To retrieve a single indexable or data column, use the method select_column. This will, for example, enable you to get the index very quickly. These return a Series of the result, indexed by the row number. These do not currently accept the where selector. In [543]: store.select_column("df_dc", "index") Out[543]: 0 2000-01-01 1 2000-01-02 2 2000-01-03 3 2000-01-04 4 2000-01-05 5 2000-01-06 6 2000-01-07 7 2000-01-08 Name: index, dtype: datetime64[ns] In [544]: store.select_column("df_dc", "string") Out[544]: 0 foo 1 foo 2 foo 3 foo 4 NaN 5 NaN 6 foo 7 bar Name: string, dtype: object Selecting coordinates# Sometimes you want to get the coordinates (a.k.a the index locations) of your query. This returns an Int64Index of the resulting locations. These coordinates can also be passed to subsequent where operations. In [545]: df_coord = pd.DataFrame( .....: np.random.randn(1000, 2), index=pd.date_range("20000101", periods=1000) .....: ) .....: In [546]: store.append("df_coord", df_coord) In [547]: c = store.select_as_coordinates("df_coord", "index > 20020101") In [548]: c Out[548]: Int64Index([732, 733, 734, 735, 736, 737, 738, 739, 740, 741, ... 990, 991, 992, 993, 994, 995, 996, 997, 998, 999], dtype='int64', length=268) In [549]: store.select("df_coord", where=c) Out[549]: 0 1 2002-01-02 0.009035 0.921784 2002-01-03 -1.476563 -1.376375 2002-01-04 1.266731 2.173681 2002-01-05 0.147621 0.616468 2002-01-06 0.008611 2.136001 ... ... ... 2002-09-22 0.781169 -0.791687 2002-09-23 -0.764810 -2.000933 2002-09-24 -0.345662 0.393915 2002-09-25 -0.116661 0.834638 2002-09-26 -1.341780 0.686366 [268 rows x 2 columns] Selecting using a where mask# Sometime your query can involve creating a list of rows to select. Usually this mask would be a resulting index from an indexing operation. This example selects the months of a datetimeindex which are 5. In [550]: df_mask = pd.DataFrame( .....: np.random.randn(1000, 2), index=pd.date_range("20000101", periods=1000) .....: ) .....: In [551]: store.append("df_mask", df_mask) In [552]: c = store.select_column("df_mask", "index") In [553]: where = c[pd.DatetimeIndex(c).month == 5].index In [554]: store.select("df_mask", where=where) Out[554]: 0 1 2000-05-01 -0.386742 -0.977433 2000-05-02 -0.228819 0.471671 2000-05-03 0.337307 1.840494 2000-05-04 0.050249 0.307149 2000-05-05 -0.802947 -0.946730 ... ... ... 2002-05-27 1.605281 1.741415 2002-05-28 -0.804450 -0.715040 2002-05-29 -0.874851 0.037178 2002-05-30 -0.161167 -1.294944 2002-05-31 -0.258463 -0.731969 [93 rows x 2 columns] Storer object# If you want to inspect the stored object, retrieve via get_storer. You could use this programmatically to say get the number of rows in an object. In [555]: store.get_storer("df_dc").nrows Out[555]: 8 Multiple table queries# The methods append_to_multiple and select_as_multiple can perform appending/selecting from multiple tables at once. The idea is to have one table (call it the selector table) that you index most/all of the columns, and perform your queries. The other table(s) are data tables with an index matching the selector table’s index. You can then perform a very fast query on the selector table, yet get lots of data back. This method is similar to having a very wide table, but enables more efficient queries. The append_to_multiple method splits a given single DataFrame into multiple tables according to d, a dictionary that maps the table names to a list of ‘columns’ you want in that table. If None is used in place of a list, that table will have the remaining unspecified columns of the given DataFrame. The argument selector defines which table is the selector table (which you can make queries from). The argument dropna will drop rows from the input DataFrame to ensure tables are synchronized. This means that if a row for one of the tables being written to is entirely np.NaN, that row will be dropped from all tables. If dropna is False, THE USER IS RESPONSIBLE FOR SYNCHRONIZING THE TABLES. Remember that entirely np.Nan rows are not written to the HDFStore, so if you choose to call dropna=False, some tables may have more rows than others, and therefore select_as_multiple may not work or it may return unexpected results. In [556]: df_mt = pd.DataFrame( .....: np.random.randn(8, 6), .....: index=pd.date_range("1/1/2000", periods=8), .....: columns=["A", "B", "C", "D", "E", "F"], .....: ) .....: In [557]: df_mt["foo"] = "bar" In [558]: df_mt.loc[df_mt.index[1], ("A", "B")] = np.nan # you can also create the tables individually In [559]: store.append_to_multiple( .....: {"df1_mt": ["A", "B"], "df2_mt": None}, df_mt, selector="df1_mt" .....: ) .....: In [560]: store Out[560]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # individual tables were created In [561]: store.select("df1_mt") Out[561]: A B 2000-01-01 0.079529 -1.459471 2000-01-02 NaN NaN 2000-01-03 -0.423113 2.314361 2000-01-04 0.756744 -0.792372 2000-01-05 -0.184971 0.170852 2000-01-06 0.678830 0.633974 2000-01-07 0.034973 0.974369 2000-01-08 -2.110103 0.243062 In [562]: store.select("df2_mt") Out[562]: C D E F foo 2000-01-01 -0.596306 -0.910022 -1.057072 -0.864360 bar 2000-01-02 0.477849 0.283128 -2.045700 -0.338206 bar 2000-01-03 -0.033100 -0.965461 -0.001079 -0.351689 bar 2000-01-04 -0.513555 -1.484776 -0.796280 -0.182321 bar 2000-01-05 -0.872407 -1.751515 0.934334 0.938818 bar 2000-01-06 -1.398256 1.347142 -0.029520 0.082738 bar 2000-01-07 -0.755544 0.380786 -1.634116 1.293610 bar 2000-01-08 1.453064 0.500558 -0.574475 0.694324 bar # as a multiple In [563]: store.select_as_multiple( .....: ["df1_mt", "df2_mt"], .....: where=["A>0", "B>0"], .....: selector="df1_mt", .....: ) .....: Out[563]: A B C D E F foo 2000-01-06 0.678830 0.633974 -1.398256 1.347142 -0.029520 0.082738 bar 2000-01-07 0.034973 0.974369 -0.755544 0.380786 -1.634116 1.293610 bar Delete from a table# You can delete from a table selectively by specifying a where. In deleting rows, it is important to understand the PyTables deletes rows by erasing the rows, then moving the following data. Thus deleting can potentially be a very expensive operation depending on the orientation of your data. To get optimal performance, it’s worthwhile to have the dimension you are deleting be the first of the indexables. Data is ordered (on the disk) in terms of the indexables. Here’s a simple use case. You store panel-type data, with dates in the major_axis and ids in the minor_axis. The data is then interleaved like this: date_1 id_1 id_2 . id_n date_2 id_1 . id_n It should be clear that a delete operation on the major_axis will be fairly quick, as one chunk is removed, then the following data moved. On the other hand a delete operation on the minor_axis will be very expensive. In this case it would almost certainly be faster to rewrite the table using a where that selects all but the missing data. Warning Please note that HDF5 DOES NOT RECLAIM SPACE in the h5 files automatically. Thus, repeatedly deleting (or removing nodes) and adding again, WILL TEND TO INCREASE THE FILE SIZE. To repack and clean the file, use ptrepack. Notes & caveats# Compression# PyTables allows the stored data to be compressed. This applies to all kinds of stores, not just tables. Two parameters are used to control compression: complevel and complib. complevel specifies if and how hard data is to be compressed. complevel=0 and complevel=None disables compression and 0<complevel<10 enables compression. complib specifies which compression library to use. If nothing is specified the default library zlib is used. A compression library usually optimizes for either good compression rates or speed and the results will depend on the type of data. Which type of compression to choose depends on your specific needs and data. The list of supported compression libraries: zlib: The default compression library. A classic in terms of compression, achieves good compression rates but is somewhat slow. lzo: Fast compression and decompression. bzip2: Good compression rates. blosc: Fast compression and decompression. Support for alternative blosc compressors: blosc:blosclz This is the default compressor for blosc blosc:lz4: A compact, very popular and fast compressor. blosc:lz4hc: A tweaked version of LZ4, produces better compression ratios at the expense of speed. blosc:snappy: A popular compressor used in many places. blosc:zlib: A classic; somewhat slower than the previous ones, but achieving better compression ratios. blosc:zstd: An extremely well balanced codec; it provides the best compression ratios among the others above, and at reasonably fast speed. If complib is defined as something other than the listed libraries a ValueError exception is issued. Note If the library specified with the complib option is missing on your platform, compression defaults to zlib without further ado. Enable compression for all objects within the file: store_compressed = pd.HDFStore( "store_compressed.h5", complevel=9, complib="blosc:blosclz" ) Or on-the-fly compression (this only applies to tables) in stores where compression is not enabled: store.append("df", df, complib="zlib", complevel=5) ptrepack# PyTables offers better write performance when tables are compressed after they are written, as opposed to turning on compression at the very beginning. You can use the supplied PyTables utility ptrepack. In addition, ptrepack can change compression levels after the fact. ptrepack --chunkshape=auto --propindexes --complevel=9 --complib=blosc in.h5 out.h5 Furthermore ptrepack in.h5 out.h5 will repack the file to allow you to reuse previously deleted space. Alternatively, one can simply remove the file and write again, or use the copy method. Caveats# Warning HDFStore is not-threadsafe for writing. The underlying PyTables only supports concurrent reads (via threading or processes). If you need reading and writing at the same time, you need to serialize these operations in a single thread in a single process. You will corrupt your data otherwise. See the (GH2397) for more information. If you use locks to manage write access between multiple processes, you may want to use fsync() before releasing write locks. For convenience you can use store.flush(fsync=True) to do this for you. Once a table is created columns (DataFrame) are fixed; only exactly the same columns can be appended Be aware that timezones (e.g., pytz.timezone('US/Eastern')) are not necessarily equal across timezone versions. So if data is localized to a specific timezone in the HDFStore using one version of a timezone library and that data is updated with another version, the data will be converted to UTC since these timezones are not considered equal. Either use the same version of timezone library or use tz_convert with the updated timezone definition. Warning PyTables will show a NaturalNameWarning if a column name cannot be used as an attribute selector. Natural identifiers contain only letters, numbers, and underscores, and may not begin with a number. Other identifiers cannot be used in a where clause and are generally a bad idea. DataTypes# HDFStore will map an object dtype to the PyTables underlying dtype. This means the following types are known to work: Type Represents missing values floating : float64, float32, float16 np.nan integer : int64, int32, int8, uint64,uint32, uint8 boolean datetime64[ns] NaT timedelta64[ns] NaT categorical : see the section below object : strings np.nan unicode columns are not supported, and WILL FAIL. Categorical data# You can write data that contains category dtypes to a HDFStore. Queries work the same as if it was an object array. However, the category dtyped data is stored in a more efficient manner. In [564]: dfcat = pd.DataFrame( .....: {"A": pd.Series(list("aabbcdba")).astype("category"), "B": np.random.randn(8)} .....: ) .....: In [565]: dfcat Out[565]: A B 0 a -1.608059 1 a 0.851060 2 b -0.736931 3 b 0.003538 4 c -1.422611 5 d 2.060901 6 b 0.993899 7 a -1.371768 In [566]: dfcat.dtypes Out[566]: A category B float64 dtype: object In [567]: cstore = pd.HDFStore("cats.h5", mode="w") In [568]: cstore.append("dfcat", dfcat, format="table", data_columns=["A"]) In [569]: result = cstore.select("dfcat", where="A in ['b', 'c']") In [570]: result Out[570]: A B 2 b -0.736931 3 b 0.003538 4 c -1.422611 6 b 0.993899 In [571]: result.dtypes Out[571]: A category B float64 dtype: object String columns# min_itemsize The underlying implementation of HDFStore uses a fixed column width (itemsize) for string columns. A string column itemsize is calculated as the maximum of the length of data (for that column) that is passed to the HDFStore, in the first append. Subsequent appends, may introduce a string for a column larger than the column can hold, an Exception will be raised (otherwise you could have a silent truncation of these columns, leading to loss of information). In the future we may relax this and allow a user-specified truncation to occur. Pass min_itemsize on the first table creation to a-priori specify the minimum length of a particular string column. min_itemsize can be an integer, or a dict mapping a column name to an integer. You can pass values as a key to allow all indexables or data_columns to have this min_itemsize. Passing a min_itemsize dict will cause all passed columns to be created as data_columns automatically. Note If you are not passing any data_columns, then the min_itemsize will be the maximum of the length of any string passed In [572]: dfs = pd.DataFrame({"A": "foo", "B": "bar"}, index=list(range(5))) In [573]: dfs Out[573]: A B 0 foo bar 1 foo bar 2 foo bar 3 foo bar 4 foo bar # A and B have a size of 30 In [574]: store.append("dfs", dfs, min_itemsize=30) In [575]: store.get_storer("dfs").table Out[575]: /dfs/table (Table(5,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": StringCol(itemsize=30, shape=(2,), dflt=b'', pos=1)} byteorder := 'little' chunkshape := (963,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False} # A is created as a data_column with a size of 30 # B is size is calculated In [576]: store.append("dfs2", dfs, min_itemsize={"A": 30}) In [577]: store.get_storer("dfs2").table Out[577]: /dfs2/table (Table(5,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": StringCol(itemsize=3, shape=(1,), dflt=b'', pos=1), "A": StringCol(itemsize=30, shape=(), dflt=b'', pos=2)} byteorder := 'little' chunkshape := (1598,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False, "A": Index(6, mediumshuffle, zlib(1)).is_csi=False} nan_rep String columns will serialize a np.nan (a missing value) with the nan_rep string representation. This defaults to the string value nan. You could inadvertently turn an actual nan value into a missing value. In [578]: dfss = pd.DataFrame({"A": ["foo", "bar", "nan"]}) In [579]: dfss Out[579]: A 0 foo 1 bar 2 nan In [580]: store.append("dfss", dfss) In [581]: store.select("dfss") Out[581]: A 0 foo 1 bar 2 NaN # here you need to specify a different nan rep In [582]: store.append("dfss2", dfss, nan_rep="_nan_") In [583]: store.select("dfss2") Out[583]: A 0 foo 1 bar 2 nan External compatibility# HDFStore writes table format objects in specific formats suitable for producing loss-less round trips to pandas objects. For external compatibility, HDFStore can read native PyTables format tables. It is possible to write an HDFStore object that can easily be imported into R using the rhdf5 library (Package website). Create a table format store like this: In [584]: df_for_r = pd.DataFrame( .....: { .....: "first": np.random.rand(100), .....: "second": np.random.rand(100), .....: "class": np.random.randint(0, 2, (100,)), .....: }, .....: index=range(100), .....: ) .....: In [585]: df_for_r.head() Out[585]: first second class 0 0.013480 0.504941 0 1 0.690984 0.898188 1 2 0.510113 0.618748 1 3 0.357698 0.004972 0 4 0.451658 0.012065 1 In [586]: store_export = pd.HDFStore("export.h5") In [587]: store_export.append("df_for_r", df_for_r, data_columns=df_dc.columns) In [588]: store_export Out[588]: <class 'pandas.io.pytables.HDFStore'> File path: export.h5 In R this file can be read into a data.frame object using the rhdf5 library. The following example function reads the corresponding column names and data values from the values and assembles them into a data.frame: # Load values and column names for all datasets from corresponding nodes and # insert them into one data.frame object. library(rhdf5) loadhdf5data <- function(h5File) { listing <- h5ls(h5File) # Find all data nodes, values are stored in *_values and corresponding column # titles in *_items data_nodes <- grep("_values", listing$name) name_nodes <- grep("_items", listing$name) data_paths = paste(listing$group[data_nodes], listing$name[data_nodes], sep = "/") name_paths = paste(listing$group[name_nodes], listing$name[name_nodes], sep = "/") columns = list() for (idx in seq(data_paths)) { # NOTE: matrices returned by h5read have to be transposed to obtain # required Fortran order! data <- data.frame(t(h5read(h5File, data_paths[idx]))) names <- t(h5read(h5File, name_paths[idx])) entry <- data.frame(data) colnames(entry) <- names columns <- append(columns, entry) } data <- data.frame(columns) return(data) } Now you can import the DataFrame into R: > data = loadhdf5data("transfer.hdf5") > head(data) first second class 1 0.4170220047 0.3266449 0 2 0.7203244934 0.5270581 0 3 0.0001143748 0.8859421 1 4 0.3023325726 0.3572698 1 5 0.1467558908 0.9085352 1 6 0.0923385948 0.6233601 1 Note The R function lists the entire HDF5 file’s contents and assembles the data.frame object from all matching nodes, so use this only as a starting point if you have stored multiple DataFrame objects to a single HDF5 file. Performance# tables format come with a writing performance penalty as compared to fixed stores. The benefit is the ability to append/delete and query (potentially very large amounts of data). Write times are generally longer as compared with regular stores. Query times can be quite fast, especially on an indexed axis. You can pass chunksize=<int> to append, specifying the write chunksize (default is 50000). This will significantly lower your memory usage on writing. You can pass expectedrows=<int> to the first append, to set the TOTAL number of rows that PyTables will expect. This will optimize read/write performance. Duplicate rows can be written to tables, but are filtered out in selection (with the last items being selected; thus a table is unique on major, minor pairs) A PerformanceWarning will be raised if you are attempting to store types that will be pickled by PyTables (rather than stored as endemic types). See Here for more information and some solutions. Feather# Feather provides binary columnar serialization for data frames. It is designed to make reading and writing data frames efficient, and to make sharing data across data analysis languages easy. Feather is designed to faithfully serialize and de-serialize DataFrames, supporting all of the pandas dtypes, including extension dtypes such as categorical and datetime with tz. Several caveats: The format will NOT write an Index, or MultiIndex for the DataFrame and will raise an error if a non-default one is provided. You can .reset_index() to store the index or .reset_index(drop=True) to ignore it. Duplicate column names and non-string columns names are not supported Actual Python objects in object dtype columns are not supported. These will raise a helpful error message on an attempt at serialization. See the Full Documentation. In [589]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(3, 6).astype("u1"), .....: "d": np.arange(4.0, 7.0, dtype="float64"), .....: "e": [True, False, True], .....: "f": pd.Categorical(list("abc")), .....: "g": pd.date_range("20130101", periods=3), .....: "h": pd.date_range("20130101", periods=3, tz="US/Eastern"), .....: "i": pd.date_range("20130101", periods=3, freq="ns"), .....: } .....: ) .....: In [590]: df Out[590]: a b c ... g h i 0 a 1 3 ... 2013-01-01 2013-01-01 00:00:00-05:00 2013-01-01 00:00:00.000000000 1 b 2 4 ... 2013-01-02 2013-01-02 00:00:00-05:00 2013-01-01 00:00:00.000000001 2 c 3 5 ... 2013-01-03 2013-01-03 00:00:00-05:00 2013-01-01 00:00:00.000000002 [3 rows x 9 columns] In [591]: df.dtypes Out[591]: a object b int64 c uint8 d float64 e bool f category g datetime64[ns] h datetime64[ns, US/Eastern] i datetime64[ns] dtype: object Write to a feather file. In [592]: df.to_feather("example.feather") Read from a feather file. In [593]: result = pd.read_feather("example.feather") In [594]: result Out[594]: a b c ... g h i 0 a 1 3 ... 2013-01-01 2013-01-01 00:00:00-05:00 2013-01-01 00:00:00.000000000 1 b 2 4 ... 2013-01-02 2013-01-02 00:00:00-05:00 2013-01-01 00:00:00.000000001 2 c 3 5 ... 2013-01-03 2013-01-03 00:00:00-05:00 2013-01-01 00:00:00.000000002 [3 rows x 9 columns] # we preserve dtypes In [595]: result.dtypes Out[595]: a object b int64 c uint8 d float64 e bool f category g datetime64[ns] h datetime64[ns, US/Eastern] i datetime64[ns] dtype: object Parquet# Apache Parquet provides a partitioned binary columnar serialization for data frames. It is designed to make reading and writing data frames efficient, and to make sharing data across data analysis languages easy. Parquet can use a variety of compression techniques to shrink the file size as much as possible while still maintaining good read performance. Parquet is designed to faithfully serialize and de-serialize DataFrame s, supporting all of the pandas dtypes, including extension dtypes such as datetime with tz. Several caveats. Duplicate column names and non-string columns names are not supported. The pyarrow engine always writes the index to the output, but fastparquet only writes non-default indexes. This extra column can cause problems for non-pandas consumers that are not expecting it. You can force including or omitting indexes with the index argument, regardless of the underlying engine. Index level names, if specified, must be strings. In the pyarrow engine, categorical dtypes for non-string types can be serialized to parquet, but will de-serialize as their primitive dtype. The pyarrow engine preserves the ordered flag of categorical dtypes with string types. fastparquet does not preserve the ordered flag. Non supported types include Interval and actual Python object types. These will raise a helpful error message on an attempt at serialization. Period type is supported with pyarrow >= 0.16.0. The pyarrow engine preserves extension data types such as the nullable integer and string data type (requiring pyarrow >= 0.16.0, and requiring the extension type to implement the needed protocols, see the extension types documentation). You can specify an engine to direct the serialization. This can be one of pyarrow, or fastparquet, or auto. If the engine is NOT specified, then the pd.options.io.parquet.engine option is checked; if this is also auto, then pyarrow is tried, and falling back to fastparquet. See the documentation for pyarrow and fastparquet. Note These engines are very similar and should read/write nearly identical parquet format files. pyarrow>=8.0.0 supports timedelta data, fastparquet>=0.1.4 supports timezone aware datetimes. These libraries differ by having different underlying dependencies (fastparquet by using numba, while pyarrow uses a c-library). In [596]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(3, 6).astype("u1"), .....: "d": np.arange(4.0, 7.0, dtype="float64"), .....: "e": [True, False, True], .....: "f": pd.date_range("20130101", periods=3), .....: "g": pd.date_range("20130101", periods=3, tz="US/Eastern"), .....: "h": pd.Categorical(list("abc")), .....: "i": pd.Categorical(list("abc"), ordered=True), .....: } .....: ) .....: In [597]: df Out[597]: a b c d e f g h i 0 a 1 3 4.0 True 2013-01-01 2013-01-01 00:00:00-05:00 a a 1 b 2 4 5.0 False 2013-01-02 2013-01-02 00:00:00-05:00 b b 2 c 3 5 6.0 True 2013-01-03 2013-01-03 00:00:00-05:00 c c In [598]: df.dtypes Out[598]: a object b int64 c uint8 d float64 e bool f datetime64[ns] g datetime64[ns, US/Eastern] h category i category dtype: object Write to a parquet file. In [599]: df.to_parquet("example_pa.parquet", engine="pyarrow") In [600]: df.to_parquet("example_fp.parquet", engine="fastparquet") Read from a parquet file. In [601]: result = pd.read_parquet("example_fp.parquet", engine="fastparquet") In [602]: result = pd.read_parquet("example_pa.parquet", engine="pyarrow") In [603]: result.dtypes Out[603]: a object b int64 c uint8 d float64 e bool f datetime64[ns] g datetime64[ns, US/Eastern] h category i category dtype: object Read only certain columns of a parquet file. In [604]: result = pd.read_parquet( .....: "example_fp.parquet", .....: engine="fastparquet", .....: columns=["a", "b"], .....: ) .....: In [605]: result = pd.read_parquet( .....: "example_pa.parquet", .....: engine="pyarrow", .....: columns=["a", "b"], .....: ) .....: In [606]: result.dtypes Out[606]: a object b int64 dtype: object Handling indexes# Serializing a DataFrame to parquet may include the implicit index as one or more columns in the output file. Thus, this code: In [607]: df = pd.DataFrame({"a": [1, 2], "b": [3, 4]}) In [608]: df.to_parquet("test.parquet", engine="pyarrow") creates a parquet file with three columns if you use pyarrow for serialization: a, b, and __index_level_0__. If you’re using fastparquet, the index may or may not be written to the file. This unexpected extra column causes some databases like Amazon Redshift to reject the file, because that column doesn’t exist in the target table. If you want to omit a dataframe’s indexes when writing, pass index=False to to_parquet(): In [609]: df.to_parquet("test.parquet", index=False) This creates a parquet file with just the two expected columns, a and b. If your DataFrame has a custom index, you won’t get it back when you load this file into a DataFrame. Passing index=True will always write the index, even if that’s not the underlying engine’s default behavior. Partitioning Parquet files# Parquet supports partitioning of data based on the values of one or more columns. In [610]: df = pd.DataFrame({"a": [0, 0, 1, 1], "b": [0, 1, 0, 1]}) In [611]: df.to_parquet(path="test", engine="pyarrow", partition_cols=["a"], compression=None) The path specifies the parent directory to which data will be saved. The partition_cols are the column names by which the dataset will be partitioned. Columns are partitioned in the order they are given. The partition splits are determined by the unique values in the partition columns. The above example creates a partitioned dataset that may look like: test ├── a=0 │ ├── 0bac803e32dc42ae83fddfd029cbdebc.parquet │ └── ... └── a=1 ├── e6ab24a4f45147b49b54a662f0c412a3.parquet └── ... ORC# New in version 1.0.0. Similar to the parquet format, the ORC Format is a binary columnar serialization for data frames. It is designed to make reading data frames efficient. pandas provides both the reader and the writer for the ORC format, read_orc() and to_orc(). This requires the pyarrow library. Warning It is highly recommended to install pyarrow using conda due to some issues occurred by pyarrow. to_orc() requires pyarrow>=7.0.0. read_orc() and to_orc() are not supported on Windows yet, you can find valid environments on install optional dependencies. For supported dtypes please refer to supported ORC features in Arrow. Currently timezones in datetime columns are not preserved when a dataframe is converted into ORC files. In [612]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(4.0, 7.0, dtype="float64"), .....: "d": [True, False, True], .....: "e": pd.date_range("20130101", periods=3), .....: } .....: ) .....: In [613]: df Out[613]: a b c d e 0 a 1 4.0 True 2013-01-01 1 b 2 5.0 False 2013-01-02 2 c 3 6.0 True 2013-01-03 In [614]: df.dtypes Out[614]: a object b int64 c float64 d bool e datetime64[ns] dtype: object Write to an orc file. In [615]: df.to_orc("example_pa.orc", engine="pyarrow") Read from an orc file. In [616]: result = pd.read_orc("example_pa.orc") In [617]: result.dtypes Out[617]: a object b int64 c float64 d bool e datetime64[ns] dtype: object Read only certain columns of an orc file. In [618]: result = pd.read_orc( .....: "example_pa.orc", .....: columns=["a", "b"], .....: ) .....: In [619]: result.dtypes Out[619]: a object b int64 dtype: object SQL queries# The pandas.io.sql module provides a collection of query wrappers to both facilitate data retrieval and to reduce dependency on DB-specific API. Database abstraction is provided by SQLAlchemy if installed. In addition you will need a driver library for your database. Examples of such drivers are psycopg2 for PostgreSQL or pymysql for MySQL. For SQLite this is included in Python’s standard library by default. You can find an overview of supported drivers for each SQL dialect in the SQLAlchemy docs. If SQLAlchemy is not installed, a fallback is only provided for sqlite (and for mysql for backwards compatibility, but this is deprecated and will be removed in a future version). This mode requires a Python database adapter which respect the Python DB-API. See also some cookbook examples for some advanced strategies. The key functions are: read_sql_table(table_name, con[, schema, ...]) Read SQL database table into a DataFrame. read_sql_query(sql, con[, index_col, ...]) Read SQL query into a DataFrame. read_sql(sql, con[, index_col, ...]) Read SQL query or database table into a DataFrame. DataFrame.to_sql(name, con[, schema, ...]) Write records stored in a DataFrame to a SQL database. Note The function read_sql() is a convenience wrapper around read_sql_table() and read_sql_query() (and for backward compatibility) and will delegate to specific function depending on the provided input (database table name or sql query). Table names do not need to be quoted if they have special characters. In the following example, we use the SQlite SQL database engine. You can use a temporary SQLite database where data are stored in “memory”. To connect with SQLAlchemy you use the create_engine() function to create an engine object from database URI. You only need to create the engine once per database you are connecting to. For more information on create_engine() and the URI formatting, see the examples below and the SQLAlchemy documentation In [620]: from sqlalchemy import create_engine # Create your engine. In [621]: engine = create_engine("sqlite:///:memory:") If you want to manage your own connections you can pass one of those instead. The example below opens a connection to the database using a Python context manager that automatically closes the connection after the block has completed. See the SQLAlchemy docs for an explanation of how the database connection is handled. with engine.connect() as conn, conn.begin(): data = pd.read_sql_table("data", conn) Warning When you open a connection to a database you are also responsible for closing it. Side effects of leaving a connection open may include locking the database or other breaking behaviour. Writing DataFrames# Assuming the following data is in a DataFrame data, we can insert it into the database using to_sql(). id Date Col_1 Col_2 Col_3 26 2012-10-18 X 25.7 True 42 2012-10-19 Y -12.4 False 63 2012-10-20 Z 5.73 True In [622]: import datetime In [623]: c = ["id", "Date", "Col_1", "Col_2", "Col_3"] In [624]: d = [ .....: (26, datetime.datetime(2010, 10, 18), "X", 27.5, True), .....: (42, datetime.datetime(2010, 10, 19), "Y", -12.5, False), .....: (63, datetime.datetime(2010, 10, 20), "Z", 5.73, True), .....: ] .....: In [625]: data = pd.DataFrame(d, columns=c) In [626]: data Out[626]: id Date Col_1 Col_2 Col_3 0 26 2010-10-18 X 27.50 True 1 42 2010-10-19 Y -12.50 False 2 63 2010-10-20 Z 5.73 True In [627]: data.to_sql("data", engine) Out[627]: 3 With some databases, writing large DataFrames can result in errors due to packet size limitations being exceeded. This can be avoided by setting the chunksize parameter when calling to_sql. For example, the following writes data to the database in batches of 1000 rows at a time: In [628]: data.to_sql("data_chunked", engine, chunksize=1000) Out[628]: 3 SQL data types# to_sql() will try to map your data to an appropriate SQL data type based on the dtype of the data. When you have columns of dtype object, pandas will try to infer the data type. You can always override the default type by specifying the desired SQL type of any of the columns by using the dtype argument. This argument needs a dictionary mapping column names to SQLAlchemy types (or strings for the sqlite3 fallback mode). For example, specifying to use the sqlalchemy String type instead of the default Text type for string columns: In [629]: from sqlalchemy.types import String In [630]: data.to_sql("data_dtype", engine, dtype={"Col_1": String}) Out[630]: 3 Note Due to the limited support for timedelta’s in the different database flavors, columns with type timedelta64 will be written as integer values as nanoseconds to the database and a warning will be raised. Note Columns of category dtype will be converted to the dense representation as you would get with np.asarray(categorical) (e.g. for string categories this gives an array of strings). Because of this, reading the database table back in does not generate a categorical. Datetime data types# Using SQLAlchemy, to_sql() is capable of writing datetime data that is timezone naive or timezone aware. However, the resulting data stored in the database ultimately depends on the supported data type for datetime data of the database system being used. The following table lists supported data types for datetime data for some common databases. Other database dialects may have different data types for datetime data. Database SQL Datetime Types Timezone Support SQLite TEXT No MySQL TIMESTAMP or DATETIME No PostgreSQL TIMESTAMP or TIMESTAMP WITH TIME ZONE Yes When writing timezone aware data to databases that do not support timezones, the data will be written as timezone naive timestamps that are in local time with respect to the timezone. read_sql_table() is also capable of reading datetime data that is timezone aware or naive. When reading TIMESTAMP WITH TIME ZONE types, pandas will convert the data to UTC. Insertion method# The parameter method controls the SQL insertion clause used. Possible values are: None: Uses standard SQL INSERT clause (one per row). 'multi': Pass multiple values in a single INSERT clause. It uses a special SQL syntax not supported by all backends. This usually provides better performance for analytic databases like Presto and Redshift, but has worse performance for traditional SQL backend if the table contains many columns. For more information check the SQLAlchemy documentation. callable with signature (pd_table, conn, keys, data_iter): This can be used to implement a more performant insertion method based on specific backend dialect features. Example of a callable using PostgreSQL COPY clause: # Alternative to_sql() *method* for DBs that support COPY FROM import csv from io import StringIO def psql_insert_copy(table, conn, keys, data_iter): """ Execute SQL statement inserting data Parameters ---------- table : pandas.io.sql.SQLTable conn : sqlalchemy.engine.Engine or sqlalchemy.engine.Connection keys : list of str Column names data_iter : Iterable that iterates the values to be inserted """ # gets a DBAPI connection that can provide a cursor dbapi_conn = conn.connection with dbapi_conn.cursor() as cur: s_buf = StringIO() writer = csv.writer(s_buf) writer.writerows(data_iter) s_buf.seek(0) columns = ', '.join(['"{}"'.format(k) for k in keys]) if table.schema: table_name = '{}.{}'.format(table.schema, table.name) else: table_name = table.name sql = 'COPY {} ({}) FROM STDIN WITH CSV'.format( table_name, columns) cur.copy_expert(sql=sql, file=s_buf) Reading tables# read_sql_table() will read a database table given the table name and optionally a subset of columns to read. Note In order to use read_sql_table(), you must have the SQLAlchemy optional dependency installed. In [631]: pd.read_sql_table("data", engine) Out[631]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 X 27.50 True 1 1 42 2010-10-19 Y -12.50 False 2 2 63 2010-10-20 Z 5.73 True Note Note that pandas infers column dtypes from query outputs, and not by looking up data types in the physical database schema. For example, assume userid is an integer column in a table. Then, intuitively, select userid ... will return integer-valued series, while select cast(userid as text) ... will return object-valued (str) series. Accordingly, if the query output is empty, then all resulting columns will be returned as object-valued (since they are most general). If you foresee that your query will sometimes generate an empty result, you may want to explicitly typecast afterwards to ensure dtype integrity. You can also specify the name of the column as the DataFrame index, and specify a subset of columns to be read. In [632]: pd.read_sql_table("data", engine, index_col="id") Out[632]: index Date Col_1 Col_2 Col_3 id 26 0 2010-10-18 X 27.50 True 42 1 2010-10-19 Y -12.50 False 63 2 2010-10-20 Z 5.73 True In [633]: pd.read_sql_table("data", engine, columns=["Col_1", "Col_2"]) Out[633]: Col_1 Col_2 0 X 27.50 1 Y -12.50 2 Z 5.73 And you can explicitly force columns to be parsed as dates: In [634]: pd.read_sql_table("data", engine, parse_dates=["Date"]) Out[634]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 X 27.50 True 1 1 42 2010-10-19 Y -12.50 False 2 2 63 2010-10-20 Z 5.73 True If needed you can explicitly specify a format string, or a dict of arguments to pass to pandas.to_datetime(): pd.read_sql_table("data", engine, parse_dates={"Date": "%Y-%m-%d"}) pd.read_sql_table( "data", engine, parse_dates={"Date": {"format": "%Y-%m-%d %H:%M:%S"}}, ) You can check if a table exists using has_table() Schema support# Reading from and writing to different schema’s is supported through the schema keyword in the read_sql_table() and to_sql() functions. Note however that this depends on the database flavor (sqlite does not have schema’s). For example: df.to_sql("table", engine, schema="other_schema") pd.read_sql_table("table", engine, schema="other_schema") Querying# You can query using raw SQL in the read_sql_query() function. In this case you must use the SQL variant appropriate for your database. When using SQLAlchemy, you can also pass SQLAlchemy Expression language constructs, which are database-agnostic. In [635]: pd.read_sql_query("SELECT * FROM data", engine) Out[635]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 00:00:00.000000 X 27.50 1 1 1 42 2010-10-19 00:00:00.000000 Y -12.50 0 2 2 63 2010-10-20 00:00:00.000000 Z 5.73 1 Of course, you can specify a more “complex” query. In [636]: pd.read_sql_query("SELECT id, Col_1, Col_2 FROM data WHERE id = 42;", engine) Out[636]: id Col_1 Col_2 0 42 Y -12.5 The read_sql_query() function supports a chunksize argument. Specifying this will return an iterator through chunks of the query result: In [637]: df = pd.DataFrame(np.random.randn(20, 3), columns=list("abc")) In [638]: df.to_sql("data_chunks", engine, index=False) Out[638]: 20 In [639]: for chunk in pd.read_sql_query("SELECT * FROM data_chunks", engine, chunksize=5): .....: print(chunk) .....: a b c 0 0.070470 0.901320 0.937577 1 0.295770 1.420548 -0.005283 2 -1.518598 -0.730065 0.226497 3 -2.061465 0.632115 0.853619 4 2.719155 0.139018 0.214557 a b c 0 -1.538924 -0.366973 -0.748801 1 -0.478137 -1.559153 -3.097759 2 -2.320335 -0.221090 0.119763 3 0.608228 1.064810 -0.780506 4 -2.736887 0.143539 1.170191 a b c 0 -1.573076 0.075792 -1.722223 1 -0.774650 0.803627 0.221665 2 0.584637 0.147264 1.057825 3 -0.284136 0.912395 1.552808 4 0.189376 -0.109830 0.539341 a b c 0 0.592591 -0.155407 -1.356475 1 0.833837 1.524249 1.606722 2 -0.029487 -0.051359 1.700152 3 0.921484 -0.926347 0.979818 4 0.182380 -0.186376 0.049820 You can also run a plain query without creating a DataFrame with execute(). This is useful for queries that don’t return values, such as INSERT. This is functionally equivalent to calling execute on the SQLAlchemy engine or db connection object. Again, you must use the SQL syntax variant appropriate for your database. from pandas.io import sql sql.execute("SELECT * FROM table_name", engine) sql.execute( "INSERT INTO table_name VALUES(?, ?, ?)", engine, params=[("id", 1, 12.2, True)] ) Engine connection examples# To connect with SQLAlchemy you use the create_engine() function to create an engine object from database URI. You only need to create the engine once per database you are connecting to. from sqlalchemy import create_engine engine = create_engine("postgresql://scott:[email protected]:5432/mydatabase") engine = create_engine("mysql+mysqldb://scott:[email protected]/foo") engine = create_engine("oracle://scott:[email protected]7.0.0.1:1521/sidname") engine = create_engine("mssql+pyodbc://mydsn") # sqlite://<nohostname>/<path> # where <path> is relative: engine = create_engine("sqlite:///foo.db") # or absolute, starting with a slash: engine = create_engine("sqlite:////absolute/path/to/foo.db") For more information see the examples the SQLAlchemy documentation Advanced SQLAlchemy queries# You can use SQLAlchemy constructs to describe your query. Use sqlalchemy.text() to specify query parameters in a backend-neutral way In [640]: import sqlalchemy as sa In [641]: pd.read_sql( .....: sa.text("SELECT * FROM data where Col_1=:col1"), engine, params={"col1": "X"} .....: ) .....: Out[641]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 00:00:00.000000 X 27.5 1 If you have an SQLAlchemy description of your database you can express where conditions using SQLAlchemy expressions In [642]: metadata = sa.MetaData() In [643]: data_table = sa.Table( .....: "data", .....: metadata, .....: sa.Column("index", sa.Integer), .....: sa.Column("Date", sa.DateTime), .....: sa.Column("Col_1", sa.String), .....: sa.Column("Col_2", sa.Float), .....: sa.Column("Col_3", sa.Boolean), .....: ) .....: In [644]: pd.read_sql(sa.select([data_table]).where(data_table.c.Col_3 is True), engine) Out[644]: Empty DataFrame Columns: [index, Date, Col_1, Col_2, Col_3] Index: [] You can combine SQLAlchemy expressions with parameters passed to read_sql() using sqlalchemy.bindparam() In [645]: import datetime as dt In [646]: expr = sa.select([data_table]).where(data_table.c.Date > sa.bindparam("date")) In [647]: pd.read_sql(expr, engine, params={"date": dt.datetime(2010, 10, 18)}) Out[647]: index Date Col_1 Col_2 Col_3 0 1 2010-10-19 Y -12.50 False 1 2 2010-10-20 Z 5.73 True Sqlite fallback# The use of sqlite is supported without using SQLAlchemy. This mode requires a Python database adapter which respect the Python DB-API. You can create connections like so: import sqlite3 con = sqlite3.connect(":memory:") And then issue the following queries: data.to_sql("data", con) pd.read_sql_query("SELECT * FROM data", con) Google BigQuery# Warning Starting in 0.20.0, pandas has split off Google BigQuery support into the separate package pandas-gbq. You can pip install pandas-gbq to get it. The pandas-gbq package provides functionality to read/write from Google BigQuery. pandas integrates with this external package. if pandas-gbq is installed, you can use the pandas methods pd.read_gbq and DataFrame.to_gbq, which will call the respective functions from pandas-gbq. Full documentation can be found here. Stata format# Writing to stata format# The method to_stata() will write a DataFrame into a .dta file. The format version of this file is always 115 (Stata 12). In [648]: df = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [649]: df.to_stata("stata.dta") Stata data files have limited data type support; only strings with 244 or fewer characters, int8, int16, int32, float32 and float64 can be stored in .dta files. Additionally, Stata reserves certain values to represent missing data. Exporting a non-missing value that is outside of the permitted range in Stata for a particular data type will retype the variable to the next larger size. For example, int8 values are restricted to lie between -127 and 100 in Stata, and so variables with values above 100 will trigger a conversion to int16. nan values in floating points data types are stored as the basic missing data type (. in Stata). Note It is not possible to export missing data values for integer data types. The Stata writer gracefully handles other data types including int64, bool, uint8, uint16, uint32 by casting to the smallest supported type that can represent the data. For example, data with a type of uint8 will be cast to int8 if all values are less than 100 (the upper bound for non-missing int8 data in Stata), or, if values are outside of this range, the variable is cast to int16. Warning Conversion from int64 to float64 may result in a loss of precision if int64 values are larger than 2**53. Warning StataWriter and to_stata() only support fixed width strings containing up to 244 characters, a limitation imposed by the version 115 dta file format. Attempting to write Stata dta files with strings longer than 244 characters raises a ValueError. Reading from Stata format# The top-level function read_stata will read a dta file and return either a DataFrame or a StataReader that can be used to read the file incrementally. In [650]: pd.read_stata("stata.dta") Out[650]: index A B 0 0 -1.690072 0.405144 1 1 -1.511309 -1.531396 2 2 0.572698 -1.106845 3 3 -1.185859 0.174564 4 4 0.603797 -1.796129 5 5 -0.791679 1.173795 6 6 -0.277710 1.859988 7 7 -0.258413 1.251808 8 8 1.443262 0.441553 9 9 1.168163 -2.054946 Specifying a chunksize yields a StataReader instance that can be used to read chunksize lines from the file at a time. The StataReader object can be used as an iterator. In [651]: with pd.read_stata("stata.dta", chunksize=3) as reader: .....: for df in reader: .....: print(df.shape) .....: (3, 3) (3, 3) (3, 3) (1, 3) For more fine-grained control, use iterator=True and specify chunksize with each call to read(). In [652]: with pd.read_stata("stata.dta", iterator=True) as reader: .....: chunk1 = reader.read(5) .....: chunk2 = reader.read(5) .....: Currently the index is retrieved as a column. The parameter convert_categoricals indicates whether value labels should be read and used to create a Categorical variable from them. Value labels can also be retrieved by the function value_labels, which requires read() to be called before use. The parameter convert_missing indicates whether missing value representations in Stata should be preserved. If False (the default), missing values are represented as np.nan. If True, missing values are represented using StataMissingValue objects, and columns containing missing values will have object data type. Note read_stata() and StataReader support .dta formats 113-115 (Stata 10-12), 117 (Stata 13), and 118 (Stata 14). Note Setting preserve_dtypes=False will upcast to the standard pandas data types: int64 for all integer types and float64 for floating point data. By default, the Stata data types are preserved when importing. Categorical data# Categorical data can be exported to Stata data files as value labeled data. The exported data consists of the underlying category codes as integer data values and the categories as value labels. Stata does not have an explicit equivalent to a Categorical and information about whether the variable is ordered is lost when exporting. Warning Stata only supports string value labels, and so str is called on the categories when exporting data. Exporting Categorical variables with non-string categories produces a warning, and can result a loss of information if the str representations of the categories are not unique. Labeled data can similarly be imported from Stata data files as Categorical variables using the keyword argument convert_categoricals (True by default). The keyword argument order_categoricals (True by default) determines whether imported Categorical variables are ordered. Note When importing categorical data, the values of the variables in the Stata data file are not preserved since Categorical variables always use integer data types between -1 and n-1 where n is the number of categories. If the original values in the Stata data file are required, these can be imported by setting convert_categoricals=False, which will import original data (but not the variable labels). The original values can be matched to the imported categorical data since there is a simple mapping between the original Stata data values and the category codes of imported Categorical variables: missing values are assigned code -1, and the smallest original value is assigned 0, the second smallest is assigned 1 and so on until the largest original value is assigned the code n-1. Note Stata supports partially labeled series. These series have value labels for some but not all data values. Importing a partially labeled series will produce a Categorical with string categories for the values that are labeled and numeric categories for values with no label. SAS formats# The top-level function read_sas() can read (but not write) SAS XPORT (.xpt) and (since v0.18.0) SAS7BDAT (.sas7bdat) format files. SAS files only contain two value types: ASCII text and floating point values (usually 8 bytes but sometimes truncated). For xport files, there is no automatic type conversion to integers, dates, or categoricals. For SAS7BDAT files, the format codes may allow date variables to be automatically converted to dates. By default the whole file is read and returned as a DataFrame. Specify a chunksize or use iterator=True to obtain reader objects (XportReader or SAS7BDATReader) for incrementally reading the file. The reader objects also have attributes that contain additional information about the file and its variables. Read a SAS7BDAT file: df = pd.read_sas("sas_data.sas7bdat") Obtain an iterator and read an XPORT file 100,000 lines at a time: def do_something(chunk): pass with pd.read_sas("sas_xport.xpt", chunk=100000) as rdr: for chunk in rdr: do_something(chunk) The specification for the xport file format is available from the SAS web site. No official documentation is available for the SAS7BDAT format. SPSS formats# New in version 0.25.0. The top-level function read_spss() can read (but not write) SPSS SAV (.sav) and ZSAV (.zsav) format files. SPSS files contain column names. By default the whole file is read, categorical columns are converted into pd.Categorical, and a DataFrame with all columns is returned. Specify the usecols parameter to obtain a subset of columns. Specify convert_categoricals=False to avoid converting categorical columns into pd.Categorical. Read an SPSS file: df = pd.read_spss("spss_data.sav") Extract a subset of columns contained in usecols from an SPSS file and avoid converting categorical columns into pd.Categorical: df = pd.read_spss( "spss_data.sav", usecols=["foo", "bar"], convert_categoricals=False, ) More information about the SAV and ZSAV file formats is available here. Other file formats# pandas itself only supports IO with a limited set of file formats that map cleanly to its tabular data model. For reading and writing other file formats into and from pandas, we recommend these packages from the broader community. netCDF# xarray provides data structures inspired by the pandas DataFrame for working with multi-dimensional datasets, with a focus on the netCDF file format and easy conversion to and from pandas. Performance considerations# This is an informal comparison of various IO methods, using pandas 0.24.2. Timings are machine dependent and small differences should be ignored. In [1]: sz = 1000000 In [2]: df = pd.DataFrame({'A': np.random.randn(sz), 'B': [1] * sz}) In [3]: df.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 1000000 entries, 0 to 999999 Data columns (total 2 columns): A 1000000 non-null float64 B 1000000 non-null int64 dtypes: float64(1), int64(1) memory usage: 15.3 MB The following test functions will be used below to compare the performance of several IO methods: import numpy as np import os sz = 1000000 df = pd.DataFrame({"A": np.random.randn(sz), "B": [1] * sz}) sz = 1000000 np.random.seed(42) df = pd.DataFrame({"A": np.random.randn(sz), "B": [1] * sz}) def test_sql_write(df): if os.path.exists("test.sql"): os.remove("test.sql") sql_db = sqlite3.connect("test.sql") df.to_sql(name="test_table", con=sql_db) sql_db.close() def test_sql_read(): sql_db = sqlite3.connect("test.sql") pd.read_sql_query("select * from test_table", sql_db) sql_db.close() def test_hdf_fixed_write(df): df.to_hdf("test_fixed.hdf", "test", mode="w") def test_hdf_fixed_read(): pd.read_hdf("test_fixed.hdf", "test") def test_hdf_fixed_write_compress(df): df.to_hdf("test_fixed_compress.hdf", "test", mode="w", complib="blosc") def test_hdf_fixed_read_compress(): pd.read_hdf("test_fixed_compress.hdf", "test") def test_hdf_table_write(df): df.to_hdf("test_table.hdf", "test", mode="w", format="table") def test_hdf_table_read(): pd.read_hdf("test_table.hdf", "test") def test_hdf_table_write_compress(df): df.to_hdf( "test_table_compress.hdf", "test", mode="w", complib="blosc", format="table" ) def test_hdf_table_read_compress(): pd.read_hdf("test_table_compress.hdf", "test") def test_csv_write(df): df.to_csv("test.csv", mode="w") def test_csv_read(): pd.read_csv("test.csv", index_col=0) def test_feather_write(df): df.to_feather("test.feather") def test_feather_read(): pd.read_feather("test.feather") def test_pickle_write(df): df.to_pickle("test.pkl") def test_pickle_read(): pd.read_pickle("test.pkl") def test_pickle_write_compress(df): df.to_pickle("test.pkl.compress", compression="xz") def test_pickle_read_compress(): pd.read_pickle("test.pkl.compress", compression="xz") def test_parquet_write(df): df.to_parquet("test.parquet") def test_parquet_read(): pd.read_parquet("test.parquet") When writing, the top three functions in terms of speed are test_feather_write, test_hdf_fixed_write and test_hdf_fixed_write_compress. In [4]: %timeit test_sql_write(df) 3.29 s ± 43.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [5]: %timeit test_hdf_fixed_write(df) 19.4 ms ± 560 µs per loop (mean ± std. dev. of 7 runs, 1 loop each) In [6]: %timeit test_hdf_fixed_write_compress(df) 19.6 ms ± 308 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [7]: %timeit test_hdf_table_write(df) 449 ms ± 5.61 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [8]: %timeit test_hdf_table_write_compress(df) 448 ms ± 11.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [9]: %timeit test_csv_write(df) 3.66 s ± 26.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [10]: %timeit test_feather_write(df) 9.75 ms ± 117 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [11]: %timeit test_pickle_write(df) 30.1 ms ± 229 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [12]: %timeit test_pickle_write_compress(df) 4.29 s ± 15.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [13]: %timeit test_parquet_write(df) 67.6 ms ± 706 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) When reading, the top three functions in terms of speed are test_feather_read, test_pickle_read and test_hdf_fixed_read. In [14]: %timeit test_sql_read() 1.77 s ± 17.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [15]: %timeit test_hdf_fixed_read() 19.4 ms ± 436 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [16]: %timeit test_hdf_fixed_read_compress() 19.5 ms ± 222 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [17]: %timeit test_hdf_table_read() 38.6 ms ± 857 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [18]: %timeit test_hdf_table_read_compress() 38.8 ms ± 1.49 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) In [19]: %timeit test_csv_read() 452 ms ± 9.04 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [20]: %timeit test_feather_read() 12.4 ms ± 99.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [21]: %timeit test_pickle_read() 18.4 ms ± 191 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [22]: %timeit test_pickle_read_compress() 915 ms ± 7.48 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [23]: %timeit test_parquet_read() 24.4 ms ± 146 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) The files test.pkl.compress, test.parquet and test.feather took the least space on disk (in bytes). 29519500 Oct 10 06:45 test.csv 16000248 Oct 10 06:45 test.feather 8281983 Oct 10 06:49 test.parquet 16000857 Oct 10 06:47 test.pkl 7552144 Oct 10 06:48 test.pkl.compress 34816000 Oct 10 06:42 test.sql 24009288 Oct 10 06:43 test_fixed.hdf 24009288 Oct 10 06:43 test_fixed_compress.hdf 24458940 Oct 10 06:44 test_table.hdf 24458940 Oct 10 06:44 test_table_compress.hdf
32
187
file written by df.to_csv cannot be read by pd.read_csv I have a dataframe I read from json, process, and save to csv. The df looks fine in terms of shape and df.head(), tail look fine. I write to a csv then read and get an error - df = get_json(params) df.to_csv(f'fname.csv') testdf = pd.read_csv(f'fname.csv') ParserError: Error tokenizing data. C error: Buffer overflow caught - possible malformed input file. Is there some 'careful write' I should be doing?
tmp. csv / writer /
As per my experience with this issue, there was some problem with the default engine and doing this helped me: df.read_csv('input.csv', engine = 'python')
68,070,151
Trying to read data with excel pandas... and getting a consistent error across multiple files
<p>Here is my test code, that gives my error:</p> <pre><code>import pandas file = 'KBART EDINA.xlsx' data = list() with open(file, 'r') as xl_file: df = pandas.read_excel(xl_file) </code></pre> <p>When I run it, I get the following error:</p> <pre><code>❯ python hack.py Traceback (most recent call last): File &quot;hack.py&quot;, line 11, in &lt;module&gt; df = pandas.read_excel(xl_file) File &quot;/CWD/deliberately/obscured/.direnv/python-3.7.6/lib/python3.7/site-packages/pandas/util/_decorators.py&quot;, line 299, in wrapper return func(*args, **kwargs) File &quot;/CWD/deliberately/obscured/.direnv/python-3.7.6/lib/python3.7/site-packages/pandas/io/excel/_base.py&quot;, line 336, in read_excel io = ExcelFile(io, storage_options=storage_options, engine=engine) File &quot;/CWD/deliberately/obscured/.direnv/python-3.7.6/lib/python3.7/site-packages/pandas/io/excel/_base.py&quot;, line 1072, in __init__ content=path_or_buffer, storage_options=storage_options File &quot;/CWD/deliberately/obscured/.direnv/python-3.7.6/lib/python3.7/site-packages/pandas/io/excel/_base.py&quot;, line 954, in inspect_excel_format buf = stream.read(PEEK_SIZE) File &quot;/another/obscured/path/miniconda3/lib/python3.7/codecs.py&quot;, line 322, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb9 in position 16: invalid start byte </code></pre> <p>I've tried with 6 different xls, xlsx, and ods files..... and they all return the same error</p> <p>I have the following (relevant) libraries installed:</p> <pre><code>openpyxl 3.0.7 pandas 1.2.4 xlrd 2.0.1 </code></pre> <p>I know the file(s) are readable (I had a <code>if not os.path.isfile(file): print(&quot;####&quot;)</code> clause to prove that)</p> <p>.... what am I missing?</p>
68,252,043
"2021-06-21T14:54:31.470000"
1
null
0
216
pandas
<p>Error between seat &amp; keyboard:</p> <h2>Wrong:</h2> <pre><code>import pandas file = 'KBART EDINA.xlsx' with open(file, 'r') as xl_file: df = pandas.read_excel(xl_file) </code></pre> <h2>Right</h2> <pre><code>import pandas file = 'KBART EDINA.xlsx' df = pandas.read_excel(file) </code></pre>
"2021-07-05T07:16:07.663000"
0
https://pandas.pydata.org/docs/user_guide/io.html
IO tools (text, CSV, HDF5, …)# IO tools (text, CSV, HDF5, …)# The pandas I/O API is a set of top level reader functions accessed like pandas.read_csv() that generally return a pandas object. The corresponding writer functions are object methods that are accessed like DataFrame.to_csv(). Below is a table containing available readers and writers. Format Type Data Description Reader Writer text CSV read_csv to_csv text Fixed-Width Text File read_fwf text JSON read_json to_json text HTML read_html to_html text LaTeX Styler.to_latex text XML read_xml to_xml text Local clipboard read_clipboard to_clipboard binary MS Excel read_excel to_excel binary OpenDocument read_excel binary HDF5 Format read_hdf to_hdf binary Feather Format read_feather to_feather binary Parquet Format read_parquet to_parquet binary ORC Format read_orc to_orc binary Stata read_stata to_stata Error between seat & keyboard: Wrong: import pandas file = 'KBART EDINA.xlsx' with open(file, 'r') as xl_file: df = pandas.read_excel(xl_file) Right import pandas file = 'KBART EDINA.xlsx' df = pandas.read_excel(file) binary SAS read_sas binary SPSS read_spss binary Python Pickle Format read_pickle to_pickle SQL SQL read_sql to_sql SQL Google BigQuery read_gbq to_gbq Here is an informal performance comparison for some of these IO methods. Note For examples that use the StringIO class, make sure you import it with from io import StringIO for Python 3. CSV & text files# The workhorse function for reading text files (a.k.a. flat files) is read_csv(). See the cookbook for some advanced strategies. Parsing options# read_csv() accepts the following common arguments: Basic# filepath_or_buffervariousEither a path to a file (a str, pathlib.Path, or py:py._path.local.LocalPath), URL (including http, ftp, and S3 locations), or any object with a read() method (such as an open file or StringIO). sepstr, defaults to ',' for read_csv(), \t for read_table()Delimiter to use. If sep is None, the C engine cannot automatically detect the separator, but the Python parsing engine can, meaning the latter will be used and automatically detect the separator by Python’s builtin sniffer tool, csv.Sniffer. In addition, separators longer than 1 character and different from '\s+' will be interpreted as regular expressions and will also force the use of the Python parsing engine. Note that regex delimiters are prone to ignoring quoted data. Regex example: '\\r\\t'. delimiterstr, default NoneAlternative argument name for sep. delim_whitespaceboolean, default FalseSpecifies whether or not whitespace (e.g. ' ' or '\t') will be used as the delimiter. Equivalent to setting sep='\s+'. If this option is set to True, nothing should be passed in for the delimiter parameter. Column and index locations and names# headerint or list of ints, default 'infer'Row number(s) to use as the column names, and the start of the data. Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first line of the file, if column names are passed explicitly then the behavior is identical to header=None. Explicitly pass header=0 to be able to replace existing names. The header can be a list of ints that specify row locations for a MultiIndex on the columns e.g. [0,1,3]. Intervening rows that are not specified will be skipped (e.g. 2 in this example is skipped). Note that this parameter ignores commented lines and empty lines if skip_blank_lines=True, so header=0 denotes the first line of data rather than the first line of the file. namesarray-like, default NoneList of column names to use. If file contains no header row, then you should explicitly pass header=None. Duplicates in this list are not allowed. index_colint, str, sequence of int / str, or False, optional, default NoneColumn(s) to use as the row labels of the DataFrame, either given as string name or column index. If a sequence of int / str is given, a MultiIndex is used. Note index_col=False can be used to force pandas to not use the first column as the index, e.g. when you have a malformed file with delimiters at the end of each line. The default value of None instructs pandas to guess. If the number of fields in the column header row is equal to the number of fields in the body of the data file, then a default index is used. If it is larger, then the first columns are used as index so that the remaining number of fields in the body are equal to the number of fields in the header. The first row after the header is used to determine the number of columns, which will go into the index. If the subsequent rows contain less columns than the first row, they are filled with NaN. This can be avoided through usecols. This ensures that the columns are taken as is and the trailing data are ignored. usecolslist-like or callable, default NoneReturn a subset of the columns. If list-like, all elements must either be positional (i.e. integer indices into the document columns) or strings that correspond to column names provided either by the user in names or inferred from the document header row(s). If names are given, the document header row(s) are not taken into account. For example, a valid list-like usecols parameter would be [0, 1, 2] or ['foo', 'bar', 'baz']. Element order is ignored, so usecols=[0, 1] is the same as [1, 0]. To instantiate a DataFrame from data with element order preserved use pd.read_csv(data, usecols=['foo', 'bar'])[['foo', 'bar']] for columns in ['foo', 'bar'] order or pd.read_csv(data, usecols=['foo', 'bar'])[['bar', 'foo']] for ['bar', 'foo'] order. If callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True: In [1]: import pandas as pd In [2]: from io import StringIO In [3]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [4]: pd.read_csv(StringIO(data)) Out[4]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [5]: pd.read_csv(StringIO(data), usecols=lambda x: x.upper() in ["COL1", "COL3"]) Out[5]: col1 col3 0 a 1 1 a 2 2 c 3 Using this parameter results in much faster parsing time and lower memory usage when using the c engine. The Python engine loads the data first before deciding which columns to drop. squeezeboolean, default FalseIf the parsed data only contains one column then return a Series. Deprecated since version 1.4.0: Append .squeeze("columns") to the call to {func_name} to squeeze the data. prefixstr, default NonePrefix to add to column numbers when no header, e.g. ‘X’ for X0, X1, … Deprecated since version 1.4.0: Use a list comprehension on the DataFrame’s columns after calling read_csv. In [6]: data = "col1,col2,col3\na,b,1" In [7]: df = pd.read_csv(StringIO(data)) In [8]: df.columns = [f"pre_{col}" for col in df.columns] In [9]: df Out[9]: pre_col1 pre_col2 pre_col3 0 a b 1 mangle_dupe_colsboolean, default TrueDuplicate columns will be specified as ‘X’, ‘X.1’…’X.N’, rather than ‘X’…’X’. Passing in False will cause data to be overwritten if there are duplicate names in the columns. Deprecated since version 1.5.0: The argument was never implemented, and a new argument where the renaming pattern can be specified will be added instead. General parsing configuration# dtypeType name or dict of column -> type, default NoneData type for data or columns. E.g. {'a': np.float64, 'b': np.int32, 'c': 'Int64'} Use str or object together with suitable na_values settings to preserve and not interpret dtype. If converters are specified, they will be applied INSTEAD of dtype conversion. New in version 1.5.0: Support for defaultdict was added. Specify a defaultdict as input where the default determines the dtype of the columns which are not explicitly listed. engine{'c', 'python', 'pyarrow'}Parser engine to use. The C and pyarrow engines are faster, while the python engine is currently more feature-complete. Multithreading is currently only supported by the pyarrow engine. New in version 1.4.0: The “pyarrow” engine was added as an experimental engine, and some features are unsupported, or may not work correctly, with this engine. convertersdict, default NoneDict of functions for converting values in certain columns. Keys can either be integers or column labels. true_valueslist, default NoneValues to consider as True. false_valueslist, default NoneValues to consider as False. skipinitialspaceboolean, default FalseSkip spaces after delimiter. skiprowslist-like or integer, default NoneLine numbers to skip (0-indexed) or number of lines to skip (int) at the start of the file. If callable, the callable function will be evaluated against the row indices, returning True if the row should be skipped and False otherwise: In [10]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [11]: pd.read_csv(StringIO(data)) Out[11]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [12]: pd.read_csv(StringIO(data), skiprows=lambda x: x % 2 != 0) Out[12]: col1 col2 col3 0 a b 2 skipfooterint, default 0Number of lines at bottom of file to skip (unsupported with engine=’c’). nrowsint, default NoneNumber of rows of file to read. Useful for reading pieces of large files. low_memoryboolean, default TrueInternally process the file in chunks, resulting in lower memory use while parsing, but possibly mixed type inference. To ensure no mixed types either set False, or specify the type with the dtype parameter. Note that the entire file is read into a single DataFrame regardless, use the chunksize or iterator parameter to return the data in chunks. (Only valid with C parser) memory_mapboolean, default FalseIf a filepath is provided for filepath_or_buffer, map the file object directly onto memory and access the data directly from there. Using this option can improve performance because there is no longer any I/O overhead. NA and missing data handling# na_valuesscalar, str, list-like, or dict, default NoneAdditional strings to recognize as NA/NaN. If dict passed, specific per-column NA values. See na values const below for a list of the values interpreted as NaN by default. keep_default_naboolean, default TrueWhether or not to include the default NaN values when parsing the data. Depending on whether na_values is passed in, the behavior is as follows: If keep_default_na is True, and na_values are specified, na_values is appended to the default NaN values used for parsing. If keep_default_na is True, and na_values are not specified, only the default NaN values are used for parsing. If keep_default_na is False, and na_values are specified, only the NaN values specified na_values are used for parsing. If keep_default_na is False, and na_values are not specified, no strings will be parsed as NaN. Note that if na_filter is passed in as False, the keep_default_na and na_values parameters will be ignored. na_filterboolean, default TrueDetect missing value markers (empty strings and the value of na_values). In data without any NAs, passing na_filter=False can improve the performance of reading a large file. verboseboolean, default FalseIndicate number of NA values placed in non-numeric columns. skip_blank_linesboolean, default TrueIf True, skip over blank lines rather than interpreting as NaN values. Datetime handling# parse_datesboolean or list of ints or names or list of lists or dict, default False. If True -> try parsing the index. If [1, 2, 3] -> try parsing columns 1, 2, 3 each as a separate date column. If [[1, 3]] -> combine columns 1 and 3 and parse as a single date column. If {'foo': [1, 3]} -> parse columns 1, 3 as date and call result ‘foo’. Note A fast-path exists for iso8601-formatted dates. infer_datetime_formatboolean, default FalseIf True and parse_dates is enabled for a column, attempt to infer the datetime format to speed up the processing. keep_date_colboolean, default FalseIf True and parse_dates specifies combining multiple columns then keep the original columns. date_parserfunction, default NoneFunction to use for converting a sequence of string columns to an array of datetime instances. The default uses dateutil.parser.parser to do the conversion. pandas will try to call date_parser in three different ways, advancing to the next if an exception occurs: 1) Pass one or more arrays (as defined by parse_dates) as arguments; 2) concatenate (row-wise) the string values from the columns defined by parse_dates into a single array and pass that; and 3) call date_parser once for each row using one or more strings (corresponding to the columns defined by parse_dates) as arguments. dayfirstboolean, default FalseDD/MM format dates, international and European format. cache_datesboolean, default TrueIf True, use a cache of unique, converted dates to apply the datetime conversion. May produce significant speed-up when parsing duplicate date strings, especially ones with timezone offsets. New in version 0.25.0. Iteration# iteratorboolean, default FalseReturn TextFileReader object for iteration or getting chunks with get_chunk(). chunksizeint, default NoneReturn TextFileReader object for iteration. See iterating and chunking below. Quoting, compression, and file format# compression{'infer', 'gzip', 'bz2', 'zip', 'xz', 'zstd', None, dict}, default 'infer'For on-the-fly decompression of on-disk data. If ‘infer’, then use gzip, bz2, zip, xz, or zstandard if filepath_or_buffer is path-like ending in ‘.gz’, ‘.bz2’, ‘.zip’, ‘.xz’, ‘.zst’, respectively, and no decompression otherwise. If using ‘zip’, the ZIP file must contain only one data file to be read in. Set to None for no decompression. Can also be a dict with key 'method' set to one of {'zip', 'gzip', 'bz2', 'zstd'} and other key-value pairs are forwarded to zipfile.ZipFile, gzip.GzipFile, bz2.BZ2File, or zstandard.ZstdDecompressor. As an example, the following could be passed for faster compression and to create a reproducible gzip archive: compression={'method': 'gzip', 'compresslevel': 1, 'mtime': 1}. Changed in version 1.1.0: dict option extended to support gzip and bz2. Changed in version 1.2.0: Previous versions forwarded dict entries for ‘gzip’ to gzip.open. thousandsstr, default NoneThousands separator. decimalstr, default '.'Character to recognize as decimal point. E.g. use ',' for European data. float_precisionstring, default NoneSpecifies which converter the C engine should use for floating-point values. The options are None for the ordinary converter, high for the high-precision converter, and round_trip for the round-trip converter. lineterminatorstr (length 1), default NoneCharacter to break file into lines. Only valid with C parser. quotecharstr (length 1)The character used to denote the start and end of a quoted item. Quoted items can include the delimiter and it will be ignored. quotingint or csv.QUOTE_* instance, default 0Control field quoting behavior per csv.QUOTE_* constants. Use one of QUOTE_MINIMAL (0), QUOTE_ALL (1), QUOTE_NONNUMERIC (2) or QUOTE_NONE (3). doublequoteboolean, default TrueWhen quotechar is specified and quoting is not QUOTE_NONE, indicate whether or not to interpret two consecutive quotechar elements inside a field as a single quotechar element. escapecharstr (length 1), default NoneOne-character string used to escape delimiter when quoting is QUOTE_NONE. commentstr, default NoneIndicates remainder of line should not be parsed. If found at the beginning of a line, the line will be ignored altogether. This parameter must be a single character. Like empty lines (as long as skip_blank_lines=True), fully commented lines are ignored by the parameter header but not by skiprows. For example, if comment='#', parsing ‘#empty\na,b,c\n1,2,3’ with header=0 will result in ‘a,b,c’ being treated as the header. encodingstr, default NoneEncoding to use for UTF when reading/writing (e.g. 'utf-8'). List of Python standard encodings. dialectstr or csv.Dialect instance, default NoneIf provided, this parameter will override values (default or not) for the following parameters: delimiter, doublequote, escapechar, skipinitialspace, quotechar, and quoting. If it is necessary to override values, a ParserWarning will be issued. See csv.Dialect documentation for more details. Error handling# error_bad_linesboolean, optional, default NoneLines with too many fields (e.g. a csv line with too many commas) will by default cause an exception to be raised, and no DataFrame will be returned. If False, then these “bad lines” will dropped from the DataFrame that is returned. See bad lines below. Deprecated since version 1.3.0: The on_bad_lines parameter should be used instead to specify behavior upon encountering a bad line instead. warn_bad_linesboolean, optional, default NoneIf error_bad_lines is False, and warn_bad_lines is True, a warning for each “bad line” will be output. Deprecated since version 1.3.0: The on_bad_lines parameter should be used instead to specify behavior upon encountering a bad line instead. on_bad_lines(‘error’, ‘warn’, ‘skip’), default ‘error’Specifies what to do upon encountering a bad line (a line with too many fields). Allowed values are : ‘error’, raise an ParserError when a bad line is encountered. ‘warn’, print a warning when a bad line is encountered and skip that line. ‘skip’, skip bad lines without raising or warning when they are encountered. New in version 1.3.0. Specifying column data types# You can indicate the data type for the whole DataFrame or individual columns: In [13]: import numpy as np In [14]: data = "a,b,c,d\n1,2,3,4\n5,6,7,8\n9,10,11" In [15]: print(data) a,b,c,d 1,2,3,4 5,6,7,8 9,10,11 In [16]: df = pd.read_csv(StringIO(data), dtype=object) In [17]: df Out[17]: a b c d 0 1 2 3 4 1 5 6 7 8 2 9 10 11 NaN In [18]: df["a"][0] Out[18]: '1' In [19]: df = pd.read_csv(StringIO(data), dtype={"b": object, "c": np.float64, "d": "Int64"}) In [20]: df.dtypes Out[20]: a int64 b object c float64 d Int64 dtype: object Fortunately, pandas offers more than one way to ensure that your column(s) contain only one dtype. If you’re unfamiliar with these concepts, you can see here to learn more about dtypes, and here to learn more about object conversion in pandas. For instance, you can use the converters argument of read_csv(): In [21]: data = "col_1\n1\n2\n'A'\n4.22" In [22]: df = pd.read_csv(StringIO(data), converters={"col_1": str}) In [23]: df Out[23]: col_1 0 1 1 2 2 'A' 3 4.22 In [24]: df["col_1"].apply(type).value_counts() Out[24]: <class 'str'> 4 Name: col_1, dtype: int64 Or you can use the to_numeric() function to coerce the dtypes after reading in the data, In [25]: df2 = pd.read_csv(StringIO(data)) In [26]: df2["col_1"] = pd.to_numeric(df2["col_1"], errors="coerce") In [27]: df2 Out[27]: col_1 0 1.00 1 2.00 2 NaN 3 4.22 In [28]: df2["col_1"].apply(type).value_counts() Out[28]: <class 'float'> 4 Name: col_1, dtype: int64 which will convert all valid parsing to floats, leaving the invalid parsing as NaN. Ultimately, how you deal with reading in columns containing mixed dtypes depends on your specific needs. In the case above, if you wanted to NaN out the data anomalies, then to_numeric() is probably your best option. However, if you wanted for all the data to be coerced, no matter the type, then using the converters argument of read_csv() would certainly be worth trying. Note In some cases, reading in abnormal data with columns containing mixed dtypes will result in an inconsistent dataset. If you rely on pandas to infer the dtypes of your columns, the parsing engine will go and infer the dtypes for different chunks of the data, rather than the whole dataset at once. Consequently, you can end up with column(s) with mixed dtypes. For example, In [29]: col_1 = list(range(500000)) + ["a", "b"] + list(range(500000)) In [30]: df = pd.DataFrame({"col_1": col_1}) In [31]: df.to_csv("foo.csv") In [32]: mixed_df = pd.read_csv("foo.csv") In [33]: mixed_df["col_1"].apply(type).value_counts() Out[33]: <class 'int'> 737858 <class 'str'> 262144 Name: col_1, dtype: int64 In [34]: mixed_df["col_1"].dtype Out[34]: dtype('O') will result with mixed_df containing an int dtype for certain chunks of the column, and str for others due to the mixed dtypes from the data that was read in. It is important to note that the overall column will be marked with a dtype of object, which is used for columns with mixed dtypes. Specifying categorical dtype# Categorical columns can be parsed directly by specifying dtype='category' or dtype=CategoricalDtype(categories, ordered). In [35]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [36]: pd.read_csv(StringIO(data)) Out[36]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [37]: pd.read_csv(StringIO(data)).dtypes Out[37]: col1 object col2 object col3 int64 dtype: object In [38]: pd.read_csv(StringIO(data), dtype="category").dtypes Out[38]: col1 category col2 category col3 category dtype: object Individual columns can be parsed as a Categorical using a dict specification: In [39]: pd.read_csv(StringIO(data), dtype={"col1": "category"}).dtypes Out[39]: col1 category col2 object col3 int64 dtype: object Specifying dtype='category' will result in an unordered Categorical whose categories are the unique values observed in the data. For more control on the categories and order, create a CategoricalDtype ahead of time, and pass that for that column’s dtype. In [40]: from pandas.api.types import CategoricalDtype In [41]: dtype = CategoricalDtype(["d", "c", "b", "a"], ordered=True) In [42]: pd.read_csv(StringIO(data), dtype={"col1": dtype}).dtypes Out[42]: col1 category col2 object col3 int64 dtype: object When using dtype=CategoricalDtype, “unexpected” values outside of dtype.categories are treated as missing values. In [43]: dtype = CategoricalDtype(["a", "b", "d"]) # No 'c' In [44]: pd.read_csv(StringIO(data), dtype={"col1": dtype}).col1 Out[44]: 0 a 1 a 2 NaN Name: col1, dtype: category Categories (3, object): ['a', 'b', 'd'] This matches the behavior of Categorical.set_categories(). Note With dtype='category', the resulting categories will always be parsed as strings (object dtype). If the categories are numeric they can be converted using the to_numeric() function, or as appropriate, another converter such as to_datetime(). When dtype is a CategoricalDtype with homogeneous categories ( all numeric, all datetimes, etc.), the conversion is done automatically. In [45]: df = pd.read_csv(StringIO(data), dtype="category") In [46]: df.dtypes Out[46]: col1 category col2 category col3 category dtype: object In [47]: df["col3"] Out[47]: 0 1 1 2 2 3 Name: col3, dtype: category Categories (3, object): ['1', '2', '3'] In [48]: new_categories = pd.to_numeric(df["col3"].cat.categories) In [49]: df["col3"] = df["col3"].cat.rename_categories(new_categories) In [50]: df["col3"] Out[50]: 0 1 1 2 2 3 Name: col3, dtype: category Categories (3, int64): [1, 2, 3] Naming and using columns# Handling column names# A file may or may not have a header row. pandas assumes the first row should be used as the column names: In [51]: data = "a,b,c\n1,2,3\n4,5,6\n7,8,9" In [52]: print(data) a,b,c 1,2,3 4,5,6 7,8,9 In [53]: pd.read_csv(StringIO(data)) Out[53]: a b c 0 1 2 3 1 4 5 6 2 7 8 9 By specifying the names argument in conjunction with header you can indicate other names to use and whether or not to throw away the header row (if any): In [54]: print(data) a,b,c 1,2,3 4,5,6 7,8,9 In [55]: pd.read_csv(StringIO(data), names=["foo", "bar", "baz"], header=0) Out[55]: foo bar baz 0 1 2 3 1 4 5 6 2 7 8 9 In [56]: pd.read_csv(StringIO(data), names=["foo", "bar", "baz"], header=None) Out[56]: foo bar baz 0 a b c 1 1 2 3 2 4 5 6 3 7 8 9 If the header is in a row other than the first, pass the row number to header. This will skip the preceding rows: In [57]: data = "skip this skip it\na,b,c\n1,2,3\n4,5,6\n7,8,9" In [58]: pd.read_csv(StringIO(data), header=1) Out[58]: a b c 0 1 2 3 1 4 5 6 2 7 8 9 Note Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first non-blank line of the file, if column names are passed explicitly then the behavior is identical to header=None. Duplicate names parsing# Deprecated since version 1.5.0: mangle_dupe_cols was never implemented, and a new argument where the renaming pattern can be specified will be added instead. If the file or header contains duplicate names, pandas will by default distinguish between them so as to prevent overwriting data: In [59]: data = "a,b,a\n0,1,2\n3,4,5" In [60]: pd.read_csv(StringIO(data)) Out[60]: a b a.1 0 0 1 2 1 3 4 5 There is no more duplicate data because mangle_dupe_cols=True by default, which modifies a series of duplicate columns ‘X’, …, ‘X’ to become ‘X’, ‘X.1’, …, ‘X.N’. Filtering columns (usecols)# The usecols argument allows you to select any subset of the columns in a file, either using the column names, position numbers or a callable: In [61]: data = "a,b,c,d\n1,2,3,foo\n4,5,6,bar\n7,8,9,baz" In [62]: pd.read_csv(StringIO(data)) Out[62]: a b c d 0 1 2 3 foo 1 4 5 6 bar 2 7 8 9 baz In [63]: pd.read_csv(StringIO(data), usecols=["b", "d"]) Out[63]: b d 0 2 foo 1 5 bar 2 8 baz In [64]: pd.read_csv(StringIO(data), usecols=[0, 2, 3]) Out[64]: a c d 0 1 3 foo 1 4 6 bar 2 7 9 baz In [65]: pd.read_csv(StringIO(data), usecols=lambda x: x.upper() in ["A", "C"]) Out[65]: a c 0 1 3 1 4 6 2 7 9 The usecols argument can also be used to specify which columns not to use in the final result: In [66]: pd.read_csv(StringIO(data), usecols=lambda x: x not in ["a", "c"]) Out[66]: b d 0 2 foo 1 5 bar 2 8 baz In this case, the callable is specifying that we exclude the “a” and “c” columns from the output. Comments and empty lines# Ignoring line comments and empty lines# If the comment parameter is specified, then completely commented lines will be ignored. By default, completely blank lines will be ignored as well. In [67]: data = "\na,b,c\n \n# commented line\n1,2,3\n\n4,5,6" In [68]: print(data) a,b,c # commented line 1,2,3 4,5,6 In [69]: pd.read_csv(StringIO(data), comment="#") Out[69]: a b c 0 1 2 3 1 4 5 6 If skip_blank_lines=False, then read_csv will not ignore blank lines: In [70]: data = "a,b,c\n\n1,2,3\n\n\n4,5,6" In [71]: pd.read_csv(StringIO(data), skip_blank_lines=False) Out[71]: a b c 0 NaN NaN NaN 1 1.0 2.0 3.0 2 NaN NaN NaN 3 NaN NaN NaN 4 4.0 5.0 6.0 Warning The presence of ignored lines might create ambiguities involving line numbers; the parameter header uses row numbers (ignoring commented/empty lines), while skiprows uses line numbers (including commented/empty lines): In [72]: data = "#comment\na,b,c\nA,B,C\n1,2,3" In [73]: pd.read_csv(StringIO(data), comment="#", header=1) Out[73]: A B C 0 1 2 3 In [74]: data = "A,B,C\n#comment\na,b,c\n1,2,3" In [75]: pd.read_csv(StringIO(data), comment="#", skiprows=2) Out[75]: a b c 0 1 2 3 If both header and skiprows are specified, header will be relative to the end of skiprows. For example: In [76]: data = ( ....: "# empty\n" ....: "# second empty line\n" ....: "# third emptyline\n" ....: "X,Y,Z\n" ....: "1,2,3\n" ....: "A,B,C\n" ....: "1,2.,4.\n" ....: "5.,NaN,10.0\n" ....: ) ....: In [77]: print(data) # empty # second empty line # third emptyline X,Y,Z 1,2,3 A,B,C 1,2.,4. 5.,NaN,10.0 In [78]: pd.read_csv(StringIO(data), comment="#", skiprows=4, header=1) Out[78]: A B C 0 1.0 2.0 4.0 1 5.0 NaN 10.0 Comments# Sometimes comments or meta data may be included in a file: In [79]: print(open("tmp.csv").read()) ID,level,category Patient1,123000,x # really unpleasant Patient2,23000,y # wouldn't take his medicine Patient3,1234018,z # awesome By default, the parser includes the comments in the output: In [80]: df = pd.read_csv("tmp.csv") In [81]: df Out[81]: ID level category 0 Patient1 123000 x # really unpleasant 1 Patient2 23000 y # wouldn't take his medicine 2 Patient3 1234018 z # awesome We can suppress the comments using the comment keyword: In [82]: df = pd.read_csv("tmp.csv", comment="#") In [83]: df Out[83]: ID level category 0 Patient1 123000 x 1 Patient2 23000 y 2 Patient3 1234018 z Dealing with Unicode data# The encoding argument should be used for encoded unicode data, which will result in byte strings being decoded to unicode in the result: In [84]: from io import BytesIO In [85]: data = b"word,length\n" b"Tr\xc3\xa4umen,7\n" b"Gr\xc3\xbc\xc3\x9fe,5" In [86]: data = data.decode("utf8").encode("latin-1") In [87]: df = pd.read_csv(BytesIO(data), encoding="latin-1") In [88]: df Out[88]: word length 0 Träumen 7 1 Grüße 5 In [89]: df["word"][1] Out[89]: 'Grüße' Some formats which encode all characters as multiple bytes, like UTF-16, won’t parse correctly at all without specifying the encoding. Full list of Python standard encodings. Index columns and trailing delimiters# If a file has one more column of data than the number of column names, the first column will be used as the DataFrame’s row names: In [90]: data = "a,b,c\n4,apple,bat,5.7\n8,orange,cow,10" In [91]: pd.read_csv(StringIO(data)) Out[91]: a b c 4 apple bat 5.7 8 orange cow 10.0 In [92]: data = "index,a,b,c\n4,apple,bat,5.7\n8,orange,cow,10" In [93]: pd.read_csv(StringIO(data), index_col=0) Out[93]: a b c index 4 apple bat 5.7 8 orange cow 10.0 Ordinarily, you can achieve this behavior using the index_col option. There are some exception cases when a file has been prepared with delimiters at the end of each data line, confusing the parser. To explicitly disable the index column inference and discard the last column, pass index_col=False: In [94]: data = "a,b,c\n4,apple,bat,\n8,orange,cow," In [95]: print(data) a,b,c 4,apple,bat, 8,orange,cow, In [96]: pd.read_csv(StringIO(data)) Out[96]: a b c 4 apple bat NaN 8 orange cow NaN In [97]: pd.read_csv(StringIO(data), index_col=False) Out[97]: a b c 0 4 apple bat 1 8 orange cow If a subset of data is being parsed using the usecols option, the index_col specification is based on that subset, not the original data. In [98]: data = "a,b,c\n4,apple,bat,\n8,orange,cow," In [99]: print(data) a,b,c 4,apple,bat, 8,orange,cow, In [100]: pd.read_csv(StringIO(data), usecols=["b", "c"]) Out[100]: b c 4 bat NaN 8 cow NaN In [101]: pd.read_csv(StringIO(data), usecols=["b", "c"], index_col=0) Out[101]: b c 4 bat NaN 8 cow NaN Date Handling# Specifying date columns# To better facilitate working with datetime data, read_csv() uses the keyword arguments parse_dates and date_parser to allow users to specify a variety of columns and date/time formats to turn the input text data into datetime objects. The simplest case is to just pass in parse_dates=True: In [102]: with open("foo.csv", mode="w") as f: .....: f.write("date,A,B,C\n20090101,a,1,2\n20090102,b,3,4\n20090103,c,4,5") .....: # Use a column as an index, and parse it as dates. In [103]: df = pd.read_csv("foo.csv", index_col=0, parse_dates=True) In [104]: df Out[104]: A B C date 2009-01-01 a 1 2 2009-01-02 b 3 4 2009-01-03 c 4 5 # These are Python datetime objects In [105]: df.index Out[105]: DatetimeIndex(['2009-01-01', '2009-01-02', '2009-01-03'], dtype='datetime64[ns]', name='date', freq=None) It is often the case that we may want to store date and time data separately, or store various date fields separately. the parse_dates keyword can be used to specify a combination of columns to parse the dates and/or times from. You can specify a list of column lists to parse_dates, the resulting date columns will be prepended to the output (so as to not affect the existing column order) and the new column names will be the concatenation of the component column names: In [106]: data = ( .....: "KORD,19990127, 19:00:00, 18:56:00, 0.8100\n" .....: "KORD,19990127, 20:00:00, 19:56:00, 0.0100\n" .....: "KORD,19990127, 21:00:00, 20:56:00, -0.5900\n" .....: "KORD,19990127, 21:00:00, 21:18:00, -0.9900\n" .....: "KORD,19990127, 22:00:00, 21:56:00, -0.5900\n" .....: "KORD,19990127, 23:00:00, 22:56:00, -0.5900" .....: ) .....: In [107]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [108]: df = pd.read_csv("tmp.csv", header=None, parse_dates=[[1, 2], [1, 3]]) In [109]: df Out[109]: 1_2 1_3 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 By default the parser removes the component date columns, but you can choose to retain them via the keep_date_col keyword: In [110]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=[[1, 2], [1, 3]], keep_date_col=True .....: ) .....: In [111]: df Out[111]: 1_2 1_3 0 ... 2 3 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD ... 19:00:00 18:56:00 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD ... 20:00:00 19:56:00 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD ... 21:00:00 20:56:00 -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD ... 21:00:00 21:18:00 -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD ... 22:00:00 21:56:00 -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD ... 23:00:00 22:56:00 -0.59 [6 rows x 7 columns] Note that if you wish to combine multiple columns into a single date column, a nested list must be used. In other words, parse_dates=[1, 2] indicates that the second and third columns should each be parsed as separate date columns while parse_dates=[[1, 2]] means the two columns should be parsed into a single column. You can also use a dict to specify custom name columns: In [112]: date_spec = {"nominal": [1, 2], "actual": [1, 3]} In [113]: df = pd.read_csv("tmp.csv", header=None, parse_dates=date_spec) In [114]: df Out[114]: nominal actual 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 It is important to remember that if multiple text columns are to be parsed into a single date column, then a new column is prepended to the data. The index_col specification is based off of this new set of columns rather than the original data columns: In [115]: date_spec = {"nominal": [1, 2], "actual": [1, 3]} In [116]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=date_spec, index_col=0 .....: ) # index is the nominal column .....: In [117]: df Out[117]: actual 0 4 nominal 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 Note If a column or index contains an unparsable date, the entire column or index will be returned unaltered as an object data type. For non-standard datetime parsing, use to_datetime() after pd.read_csv. Note read_csv has a fast_path for parsing datetime strings in iso8601 format, e.g “2000-01-01T00:01:02+00:00” and similar variations. If you can arrange for your data to store datetimes in this format, load times will be significantly faster, ~20x has been observed. Date parsing functions# Finally, the parser allows you to specify a custom date_parser function to take full advantage of the flexibility of the date parsing API: In [118]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=date_spec, date_parser=pd.to_datetime .....: ) .....: In [119]: df Out[119]: nominal actual 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 pandas will try to call the date_parser function in three different ways. If an exception is raised, the next one is tried: date_parser is first called with one or more arrays as arguments, as defined using parse_dates (e.g., date_parser(['2013', '2013'], ['1', '2'])). If #1 fails, date_parser is called with all the columns concatenated row-wise into a single array (e.g., date_parser(['2013 1', '2013 2'])). Note that performance-wise, you should try these methods of parsing dates in order: Try to infer the format using infer_datetime_format=True (see section below). If you know the format, use pd.to_datetime(): date_parser=lambda x: pd.to_datetime(x, format=...). If you have a really non-standard format, use a custom date_parser function. For optimal performance, this should be vectorized, i.e., it should accept arrays as arguments. Parsing a CSV with mixed timezones# pandas cannot natively represent a column or index with mixed timezones. If your CSV file contains columns with a mixture of timezones, the default result will be an object-dtype column with strings, even with parse_dates. In [120]: content = """\ .....: a .....: 2000-01-01T00:00:00+05:00 .....: 2000-01-01T00:00:00+06:00""" .....: In [121]: df = pd.read_csv(StringIO(content), parse_dates=["a"]) In [122]: df["a"] Out[122]: 0 2000-01-01 00:00:00+05:00 1 2000-01-01 00:00:00+06:00 Name: a, dtype: object To parse the mixed-timezone values as a datetime column, pass a partially-applied to_datetime() with utc=True as the date_parser. In [123]: df = pd.read_csv( .....: StringIO(content), .....: parse_dates=["a"], .....: date_parser=lambda col: pd.to_datetime(col, utc=True), .....: ) .....: In [124]: df["a"] Out[124]: 0 1999-12-31 19:00:00+00:00 1 1999-12-31 18:00:00+00:00 Name: a, dtype: datetime64[ns, UTC] Inferring datetime format# If you have parse_dates enabled for some or all of your columns, and your datetime strings are all formatted the same way, you may get a large speed up by setting infer_datetime_format=True. If set, pandas will attempt to guess the format of your datetime strings, and then use a faster means of parsing the strings. 5-10x parsing speeds have been observed. pandas will fallback to the usual parsing if either the format cannot be guessed or the format that was guessed cannot properly parse the entire column of strings. So in general, infer_datetime_format should not have any negative consequences if enabled. Here are some examples of datetime strings that can be guessed (All representing December 30th, 2011 at 00:00:00): “20111230” “2011/12/30” “20111230 00:00:00” “12/30/2011 00:00:00” “30/Dec/2011 00:00:00” “30/December/2011 00:00:00” Note that infer_datetime_format is sensitive to dayfirst. With dayfirst=True, it will guess “01/12/2011” to be December 1st. With dayfirst=False (default) it will guess “01/12/2011” to be January 12th. # Try to infer the format for the index column In [125]: df = pd.read_csv( .....: "foo.csv", .....: index_col=0, .....: parse_dates=True, .....: infer_datetime_format=True, .....: ) .....: In [126]: df Out[126]: A B C date 2009-01-01 a 1 2 2009-01-02 b 3 4 2009-01-03 c 4 5 International date formats# While US date formats tend to be MM/DD/YYYY, many international formats use DD/MM/YYYY instead. For convenience, a dayfirst keyword is provided: In [127]: data = "date,value,cat\n1/6/2000,5,a\n2/6/2000,10,b\n3/6/2000,15,c" In [128]: print(data) date,value,cat 1/6/2000,5,a 2/6/2000,10,b 3/6/2000,15,c In [129]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [130]: pd.read_csv("tmp.csv", parse_dates=[0]) Out[130]: date value cat 0 2000-01-06 5 a 1 2000-02-06 10 b 2 2000-03-06 15 c In [131]: pd.read_csv("tmp.csv", dayfirst=True, parse_dates=[0]) Out[131]: date value cat 0 2000-06-01 5 a 1 2000-06-02 10 b 2 2000-06-03 15 c Writing CSVs to binary file objects# New in version 1.2.0. df.to_csv(..., mode="wb") allows writing a CSV to a file object opened binary mode. In most cases, it is not necessary to specify mode as Pandas will auto-detect whether the file object is opened in text or binary mode. In [132]: import io In [133]: data = pd.DataFrame([0, 1, 2]) In [134]: buffer = io.BytesIO() In [135]: data.to_csv(buffer, encoding="utf-8", compression="gzip") Specifying method for floating-point conversion# The parameter float_precision can be specified in order to use a specific floating-point converter during parsing with the C engine. The options are the ordinary converter, the high-precision converter, and the round-trip converter (which is guaranteed to round-trip values after writing to a file). For example: In [136]: val = "0.3066101993807095471566981359501369297504425048828125" In [137]: data = "a,b,c\n1,2,{0}".format(val) In [138]: abs( .....: pd.read_csv( .....: StringIO(data), .....: engine="c", .....: float_precision=None, .....: )["c"][0] - float(val) .....: ) .....: Out[138]: 5.551115123125783e-17 In [139]: abs( .....: pd.read_csv( .....: StringIO(data), .....: engine="c", .....: float_precision="high", .....: )["c"][0] - float(val) .....: ) .....: Out[139]: 5.551115123125783e-17 In [140]: abs( .....: pd.read_csv(StringIO(data), engine="c", float_precision="round_trip")["c"][0] .....: - float(val) .....: ) .....: Out[140]: 0.0 Thousand separators# For large numbers that have been written with a thousands separator, you can set the thousands keyword to a string of length 1 so that integers will be parsed correctly: By default, numbers with a thousands separator will be parsed as strings: In [141]: data = ( .....: "ID|level|category\n" .....: "Patient1|123,000|x\n" .....: "Patient2|23,000|y\n" .....: "Patient3|1,234,018|z" .....: ) .....: In [142]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [143]: df = pd.read_csv("tmp.csv", sep="|") In [144]: df Out[144]: ID level category 0 Patient1 123,000 x 1 Patient2 23,000 y 2 Patient3 1,234,018 z In [145]: df.level.dtype Out[145]: dtype('O') The thousands keyword allows integers to be parsed correctly: In [146]: df = pd.read_csv("tmp.csv", sep="|", thousands=",") In [147]: df Out[147]: ID level category 0 Patient1 123000 x 1 Patient2 23000 y 2 Patient3 1234018 z In [148]: df.level.dtype Out[148]: dtype('int64') NA values# To control which values are parsed as missing values (which are signified by NaN), specify a string in na_values. If you specify a list of strings, then all values in it are considered to be missing values. If you specify a number (a float, like 5.0 or an integer like 5), the corresponding equivalent values will also imply a missing value (in this case effectively [5.0, 5] are recognized as NaN). To completely override the default values that are recognized as missing, specify keep_default_na=False. The default NaN recognized values are ['-1.#IND', '1.#QNAN', '1.#IND', '-1.#QNAN', '#N/A N/A', '#N/A', 'N/A', 'n/a', 'NA', '<NA>', '#NA', 'NULL', 'null', 'NaN', '-NaN', 'nan', '-nan', '']. Let us consider some examples: pd.read_csv("path_to_file.csv", na_values=[5]) In the example above 5 and 5.0 will be recognized as NaN, in addition to the defaults. A string will first be interpreted as a numerical 5, then as a NaN. pd.read_csv("path_to_file.csv", keep_default_na=False, na_values=[""]) Above, only an empty field will be recognized as NaN. pd.read_csv("path_to_file.csv", keep_default_na=False, na_values=["NA", "0"]) Above, both NA and 0 as strings are NaN. pd.read_csv("path_to_file.csv", na_values=["Nope"]) The default values, in addition to the string "Nope" are recognized as NaN. Infinity# inf like values will be parsed as np.inf (positive infinity), and -inf as -np.inf (negative infinity). These will ignore the case of the value, meaning Inf, will also be parsed as np.inf. Returning Series# Using the squeeze keyword, the parser will return output with a single column as a Series: Deprecated since version 1.4.0: Users should append .squeeze("columns") to the DataFrame returned by read_csv instead. In [149]: data = "level\nPatient1,123000\nPatient2,23000\nPatient3,1234018" In [150]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [151]: print(open("tmp.csv").read()) level Patient1,123000 Patient2,23000 Patient3,1234018 In [152]: output = pd.read_csv("tmp.csv", squeeze=True) In [153]: output Out[153]: Patient1 123000 Patient2 23000 Patient3 1234018 Name: level, dtype: int64 In [154]: type(output) Out[154]: pandas.core.series.Series Boolean values# The common values True, False, TRUE, and FALSE are all recognized as boolean. Occasionally you might want to recognize other values as being boolean. To do this, use the true_values and false_values options as follows: In [155]: data = "a,b,c\n1,Yes,2\n3,No,4" In [156]: print(data) a,b,c 1,Yes,2 3,No,4 In [157]: pd.read_csv(StringIO(data)) Out[157]: a b c 0 1 Yes 2 1 3 No 4 In [158]: pd.read_csv(StringIO(data), true_values=["Yes"], false_values=["No"]) Out[158]: a b c 0 1 True 2 1 3 False 4 Handling “bad” lines# Some files may have malformed lines with too few fields or too many. Lines with too few fields will have NA values filled in the trailing fields. Lines with too many fields will raise an error by default: In [159]: data = "a,b,c\n1,2,3\n4,5,6,7\n8,9,10" In [160]: pd.read_csv(StringIO(data)) --------------------------------------------------------------------------- ParserError Traceback (most recent call last) Cell In[160], line 1 ----> 1 pd.read_csv(StringIO(data)) File ~/work/pandas/pandas/pandas/util/_decorators.py:211, in deprecate_kwarg.<locals>._deprecate_kwarg.<locals>.wrapper(*args, **kwargs) 209 else: 210 kwargs[new_arg_name] = new_arg_value --> 211 return func(*args, **kwargs) File ~/work/pandas/pandas/pandas/util/_decorators.py:331, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs) 325 if len(args) > num_allow_args: 326 warnings.warn( 327 msg.format(arguments=_format_argument_list(allow_args)), 328 FutureWarning, 329 stacklevel=find_stack_level(), 330 ) --> 331 return func(*args, **kwargs) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:950, in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, error_bad_lines, warn_bad_lines, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options) 935 kwds_defaults = _refine_defaults_read( 936 dialect, 937 delimiter, (...) 946 defaults={"delimiter": ","}, 947 ) 948 kwds.update(kwds_defaults) --> 950 return _read(filepath_or_buffer, kwds) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:611, in _read(filepath_or_buffer, kwds) 608 return parser 610 with parser: --> 611 return parser.read(nrows) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:1778, in TextFileReader.read(self, nrows) 1771 nrows = validate_integer("nrows", nrows) 1772 try: 1773 # error: "ParserBase" has no attribute "read" 1774 ( 1775 index, 1776 columns, 1777 col_dict, -> 1778 ) = self._engine.read( # type: ignore[attr-defined] 1779 nrows 1780 ) 1781 except Exception: 1782 self.close() File ~/work/pandas/pandas/pandas/io/parsers/c_parser_wrapper.py:230, in CParserWrapper.read(self, nrows) 228 try: 229 if self.low_memory: --> 230 chunks = self._reader.read_low_memory(nrows) 231 # destructive to chunks 232 data = _concatenate_chunks(chunks) File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:808, in pandas._libs.parsers.TextReader.read_low_memory() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:866, in pandas._libs.parsers.TextReader._read_rows() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:852, in pandas._libs.parsers.TextReader._tokenize_rows() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:1973, in pandas._libs.parsers.raise_parser_error() ParserError: Error tokenizing data. C error: Expected 3 fields in line 3, saw 4 You can elect to skip bad lines: In [29]: pd.read_csv(StringIO(data), on_bad_lines="warn") Skipping line 3: expected 3 fields, saw 4 Out[29]: a b c 0 1 2 3 1 8 9 10 Or pass a callable function to handle the bad line if engine="python". The bad line will be a list of strings that was split by the sep: In [29]: external_list = [] In [30]: def bad_lines_func(line): ...: external_list.append(line) ...: return line[-3:] In [31]: pd.read_csv(StringIO(data), on_bad_lines=bad_lines_func, engine="python") Out[31]: a b c 0 1 2 3 1 5 6 7 2 8 9 10 In [32]: external_list Out[32]: [4, 5, 6, 7] .. versionadded:: 1.4.0 You can also use the usecols parameter to eliminate extraneous column data that appear in some lines but not others: In [33]: pd.read_csv(StringIO(data), usecols=[0, 1, 2]) Out[33]: a b c 0 1 2 3 1 4 5 6 2 8 9 10 In case you want to keep all data including the lines with too many fields, you can specify a sufficient number of names. This ensures that lines with not enough fields are filled with NaN. In [34]: pd.read_csv(StringIO(data), names=['a', 'b', 'c', 'd']) Out[34]: a b c d 0 1 2 3 NaN 1 4 5 6 7 2 8 9 10 NaN Dialect# The dialect keyword gives greater flexibility in specifying the file format. By default it uses the Excel dialect but you can specify either the dialect name or a csv.Dialect instance. Suppose you had data with unenclosed quotes: In [161]: data = "label1,label2,label3\n" 'index1,"a,c,e\n' "index2,b,d,f" In [162]: print(data) label1,label2,label3 index1,"a,c,e index2,b,d,f By default, read_csv uses the Excel dialect and treats the double quote as the quote character, which causes it to fail when it finds a newline before it finds the closing double quote. We can get around this using dialect: In [163]: import csv In [164]: dia = csv.excel() In [165]: dia.quoting = csv.QUOTE_NONE In [166]: pd.read_csv(StringIO(data), dialect=dia) Out[166]: label1 label2 label3 index1 "a c e index2 b d f All of the dialect options can be specified separately by keyword arguments: In [167]: data = "a,b,c~1,2,3~4,5,6" In [168]: pd.read_csv(StringIO(data), lineterminator="~") Out[168]: a b c 0 1 2 3 1 4 5 6 Another common dialect option is skipinitialspace, to skip any whitespace after a delimiter: In [169]: data = "a, b, c\n1, 2, 3\n4, 5, 6" In [170]: print(data) a, b, c 1, 2, 3 4, 5, 6 In [171]: pd.read_csv(StringIO(data), skipinitialspace=True) Out[171]: a b c 0 1 2 3 1 4 5 6 The parsers make every attempt to “do the right thing” and not be fragile. Type inference is a pretty big deal. If a column can be coerced to integer dtype without altering the contents, the parser will do so. Any non-numeric columns will come through as object dtype as with the rest of pandas objects. Quoting and Escape Characters# Quotes (and other escape characters) in embedded fields can be handled in any number of ways. One way is to use backslashes; to properly parse this data, you should pass the escapechar option: In [172]: data = 'a,b\n"hello, \\"Bob\\", nice to see you",5' In [173]: print(data) a,b "hello, \"Bob\", nice to see you",5 In [174]: pd.read_csv(StringIO(data), escapechar="\\") Out[174]: a b 0 hello, "Bob", nice to see you 5 Files with fixed width columns# While read_csv() reads delimited data, the read_fwf() function works with data files that have known and fixed column widths. The function parameters to read_fwf are largely the same as read_csv with two extra parameters, and a different usage of the delimiter parameter: colspecs: A list of pairs (tuples) giving the extents of the fixed-width fields of each line as half-open intervals (i.e., [from, to[ ). String value ‘infer’ can be used to instruct the parser to try detecting the column specifications from the first 100 rows of the data. Default behavior, if not specified, is to infer. widths: A list of field widths which can be used instead of ‘colspecs’ if the intervals are contiguous. delimiter: Characters to consider as filler characters in the fixed-width file. Can be used to specify the filler character of the fields if it is not spaces (e.g., ‘~’). Consider a typical fixed-width data file: In [175]: data1 = ( .....: "id8141 360.242940 149.910199 11950.7\n" .....: "id1594 444.953632 166.985655 11788.4\n" .....: "id1849 364.136849 183.628767 11806.2\n" .....: "id1230 413.836124 184.375703 11916.8\n" .....: "id1948 502.953953 173.237159 12468.3" .....: ) .....: In [176]: with open("bar.csv", "w") as f: .....: f.write(data1) .....: In order to parse this file into a DataFrame, we simply need to supply the column specifications to the read_fwf function along with the file name: # Column specifications are a list of half-intervals In [177]: colspecs = [(0, 6), (8, 20), (21, 33), (34, 43)] In [178]: df = pd.read_fwf("bar.csv", colspecs=colspecs, header=None, index_col=0) In [179]: df Out[179]: 1 2 3 0 id8141 360.242940 149.910199 11950.7 id1594 444.953632 166.985655 11788.4 id1849 364.136849 183.628767 11806.2 id1230 413.836124 184.375703 11916.8 id1948 502.953953 173.237159 12468.3 Note how the parser automatically picks column names X.<column number> when header=None argument is specified. Alternatively, you can supply just the column widths for contiguous columns: # Widths are a list of integers In [180]: widths = [6, 14, 13, 10] In [181]: df = pd.read_fwf("bar.csv", widths=widths, header=None) In [182]: df Out[182]: 0 1 2 3 0 id8141 360.242940 149.910199 11950.7 1 id1594 444.953632 166.985655 11788.4 2 id1849 364.136849 183.628767 11806.2 3 id1230 413.836124 184.375703 11916.8 4 id1948 502.953953 173.237159 12468.3 The parser will take care of extra white spaces around the columns so it’s ok to have extra separation between the columns in the file. By default, read_fwf will try to infer the file’s colspecs by using the first 100 rows of the file. It can do it only in cases when the columns are aligned and correctly separated by the provided delimiter (default delimiter is whitespace). In [183]: df = pd.read_fwf("bar.csv", header=None, index_col=0) In [184]: df Out[184]: 1 2 3 0 id8141 360.242940 149.910199 11950.7 id1594 444.953632 166.985655 11788.4 id1849 364.136849 183.628767 11806.2 id1230 413.836124 184.375703 11916.8 id1948 502.953953 173.237159 12468.3 read_fwf supports the dtype parameter for specifying the types of parsed columns to be different from the inferred type. In [185]: pd.read_fwf("bar.csv", header=None, index_col=0).dtypes Out[185]: 1 float64 2 float64 3 float64 dtype: object In [186]: pd.read_fwf("bar.csv", header=None, dtype={2: "object"}).dtypes Out[186]: 0 object 1 float64 2 object 3 float64 dtype: object Indexes# Files with an “implicit” index column# Consider a file with one less entry in the header than the number of data column: In [187]: data = "A,B,C\n20090101,a,1,2\n20090102,b,3,4\n20090103,c,4,5" In [188]: print(data) A,B,C 20090101,a,1,2 20090102,b,3,4 20090103,c,4,5 In [189]: with open("foo.csv", "w") as f: .....: f.write(data) .....: In this special case, read_csv assumes that the first column is to be used as the index of the DataFrame: In [190]: pd.read_csv("foo.csv") Out[190]: A B C 20090101 a 1 2 20090102 b 3 4 20090103 c 4 5 Note that the dates weren’t automatically parsed. In that case you would need to do as before: In [191]: df = pd.read_csv("foo.csv", parse_dates=True) In [192]: df.index Out[192]: DatetimeIndex(['2009-01-01', '2009-01-02', '2009-01-03'], dtype='datetime64[ns]', freq=None) Reading an index with a MultiIndex# Suppose you have data indexed by two columns: In [193]: data = 'year,indiv,zit,xit\n1977,"A",1.2,.6\n1977,"B",1.5,.5' In [194]: print(data) year,indiv,zit,xit 1977,"A",1.2,.6 1977,"B",1.5,.5 In [195]: with open("mindex_ex.csv", mode="w") as f: .....: f.write(data) .....: The index_col argument to read_csv can take a list of column numbers to turn multiple columns into a MultiIndex for the index of the returned object: In [196]: df = pd.read_csv("mindex_ex.csv", index_col=[0, 1]) In [197]: df Out[197]: zit xit year indiv 1977 A 1.2 0.6 B 1.5 0.5 In [198]: df.loc[1977] Out[198]: zit xit indiv A 1.2 0.6 B 1.5 0.5 Reading columns with a MultiIndex# By specifying list of row locations for the header argument, you can read in a MultiIndex for the columns. Specifying non-consecutive rows will skip the intervening rows. In [199]: from pandas._testing import makeCustomDataframe as mkdf In [200]: df = mkdf(5, 3, r_idx_nlevels=2, c_idx_nlevels=4) In [201]: df.to_csv("mi.csv") In [202]: print(open("mi.csv").read()) C0,,C_l0_g0,C_l0_g1,C_l0_g2 C1,,C_l1_g0,C_l1_g1,C_l1_g2 C2,,C_l2_g0,C_l2_g1,C_l2_g2 C3,,C_l3_g0,C_l3_g1,C_l3_g2 R0,R1,,, R_l0_g0,R_l1_g0,R0C0,R0C1,R0C2 R_l0_g1,R_l1_g1,R1C0,R1C1,R1C2 R_l0_g2,R_l1_g2,R2C0,R2C1,R2C2 R_l0_g3,R_l1_g3,R3C0,R3C1,R3C2 R_l0_g4,R_l1_g4,R4C0,R4C1,R4C2 In [203]: pd.read_csv("mi.csv", header=[0, 1, 2, 3], index_col=[0, 1]) Out[203]: C0 C_l0_g0 C_l0_g1 C_l0_g2 C1 C_l1_g0 C_l1_g1 C_l1_g2 C2 C_l2_g0 C_l2_g1 C_l2_g2 C3 C_l3_g0 C_l3_g1 C_l3_g2 R0 R1 R_l0_g0 R_l1_g0 R0C0 R0C1 R0C2 R_l0_g1 R_l1_g1 R1C0 R1C1 R1C2 R_l0_g2 R_l1_g2 R2C0 R2C1 R2C2 R_l0_g3 R_l1_g3 R3C0 R3C1 R3C2 R_l0_g4 R_l1_g4 R4C0 R4C1 R4C2 read_csv is also able to interpret a more common format of multi-columns indices. In [204]: data = ",a,a,a,b,c,c\n,q,r,s,t,u,v\none,1,2,3,4,5,6\ntwo,7,8,9,10,11,12" In [205]: print(data) ,a,a,a,b,c,c ,q,r,s,t,u,v one,1,2,3,4,5,6 two,7,8,9,10,11,12 In [206]: with open("mi2.csv", "w") as fh: .....: fh.write(data) .....: In [207]: pd.read_csv("mi2.csv", header=[0, 1], index_col=0) Out[207]: a b c q r s t u v one 1 2 3 4 5 6 two 7 8 9 10 11 12 Note If an index_col is not specified (e.g. you don’t have an index, or wrote it with df.to_csv(..., index=False), then any names on the columns index will be lost. Automatically “sniffing” the delimiter# read_csv is capable of inferring delimited (not necessarily comma-separated) files, as pandas uses the csv.Sniffer class of the csv module. For this, you have to specify sep=None. In [208]: df = pd.DataFrame(np.random.randn(10, 4)) In [209]: df.to_csv("tmp.csv", sep="|") In [210]: df.to_csv("tmp2.csv", sep=":") In [211]: pd.read_csv("tmp2.csv", sep=None, engine="python") Out[211]: Unnamed: 0 0 1 2 3 0 0 0.469112 -0.282863 -1.509059 -1.135632 1 1 1.212112 -0.173215 0.119209 -1.044236 2 2 -0.861849 -2.104569 -0.494929 1.071804 3 3 0.721555 -0.706771 -1.039575 0.271860 4 4 -0.424972 0.567020 0.276232 -1.087401 5 5 -0.673690 0.113648 -1.478427 0.524988 6 6 0.404705 0.577046 -1.715002 -1.039268 7 7 -0.370647 -1.157892 -1.344312 0.844885 8 8 1.075770 -0.109050 1.643563 -1.469388 9 9 0.357021 -0.674600 -1.776904 -0.968914 Reading multiple files to create a single DataFrame# It’s best to use concat() to combine multiple files. See the cookbook for an example. Iterating through files chunk by chunk# Suppose you wish to iterate through a (potentially very large) file lazily rather than reading the entire file into memory, such as the following: In [212]: df = pd.DataFrame(np.random.randn(10, 4)) In [213]: df.to_csv("tmp.csv", sep="|") In [214]: table = pd.read_csv("tmp.csv", sep="|") In [215]: table Out[215]: Unnamed: 0 0 1 2 3 0 0 -1.294524 0.413738 0.276662 -0.472035 1 1 -0.013960 -0.362543 -0.006154 -0.923061 2 2 0.895717 0.805244 -1.206412 2.565646 3 3 1.431256 1.340309 -1.170299 -0.226169 4 4 0.410835 0.813850 0.132003 -0.827317 5 5 -0.076467 -1.187678 1.130127 -1.436737 6 6 -1.413681 1.607920 1.024180 0.569605 7 7 0.875906 -2.211372 0.974466 -2.006747 8 8 -0.410001 -0.078638 0.545952 -1.219217 9 9 -1.226825 0.769804 -1.281247 -0.727707 By specifying a chunksize to read_csv, the return value will be an iterable object of type TextFileReader: In [216]: with pd.read_csv("tmp.csv", sep="|", chunksize=4) as reader: .....: reader .....: for chunk in reader: .....: print(chunk) .....: Unnamed: 0 0 1 2 3 0 0 -1.294524 0.413738 0.276662 -0.472035 1 1 -0.013960 -0.362543 -0.006154 -0.923061 2 2 0.895717 0.805244 -1.206412 2.565646 3 3 1.431256 1.340309 -1.170299 -0.226169 Unnamed: 0 0 1 2 3 4 4 0.410835 0.813850 0.132003 -0.827317 5 5 -0.076467 -1.187678 1.130127 -1.436737 6 6 -1.413681 1.607920 1.024180 0.569605 7 7 0.875906 -2.211372 0.974466 -2.006747 Unnamed: 0 0 1 2 3 8 8 -0.410001 -0.078638 0.545952 -1.219217 9 9 -1.226825 0.769804 -1.281247 -0.727707 Changed in version 1.2: read_csv/json/sas return a context-manager when iterating through a file. Specifying iterator=True will also return the TextFileReader object: In [217]: with pd.read_csv("tmp.csv", sep="|", iterator=True) as reader: .....: reader.get_chunk(5) .....: Specifying the parser engine# Pandas currently supports three engines, the C engine, the python engine, and an experimental pyarrow engine (requires the pyarrow package). In general, the pyarrow engine is fastest on larger workloads and is equivalent in speed to the C engine on most other workloads. The python engine tends to be slower than the pyarrow and C engines on most workloads. However, the pyarrow engine is much less robust than the C engine, which lacks a few features compared to the Python engine. Where possible, pandas uses the C parser (specified as engine='c'), but it may fall back to Python if C-unsupported options are specified. Currently, options unsupported by the C and pyarrow engines include: sep other than a single character (e.g. regex separators) skipfooter sep=None with delim_whitespace=False Specifying any of the above options will produce a ParserWarning unless the python engine is selected explicitly using engine='python'. Options that are unsupported by the pyarrow engine which are not covered by the list above include: float_precision chunksize comment nrows thousands memory_map dialect warn_bad_lines error_bad_lines on_bad_lines delim_whitespace quoting lineterminator converters decimal iterator dayfirst infer_datetime_format verbose skipinitialspace low_memory Specifying these options with engine='pyarrow' will raise a ValueError. Reading/writing remote files# You can pass in a URL to read or write remote files to many of pandas’ IO functions - the following example shows reading a CSV file: df = pd.read_csv("https://download.bls.gov/pub/time.series/cu/cu.item", sep="\t") New in version 1.3.0. A custom header can be sent alongside HTTP(s) requests by passing a dictionary of header key value mappings to the storage_options keyword argument as shown below: headers = {"User-Agent": "pandas"} df = pd.read_csv( "https://download.bls.gov/pub/time.series/cu/cu.item", sep="\t", storage_options=headers ) All URLs which are not local files or HTTP(s) are handled by fsspec, if installed, and its various filesystem implementations (including Amazon S3, Google Cloud, SSH, FTP, webHDFS…). Some of these implementations will require additional packages to be installed, for example S3 URLs require the s3fs library: df = pd.read_json("s3://pandas-test/adatafile.json") When dealing with remote storage systems, you might need extra configuration with environment variables or config files in special locations. For example, to access data in your S3 bucket, you will need to define credentials in one of the several ways listed in the S3Fs documentation. The same is true for several of the storage backends, and you should follow the links at fsimpl1 for implementations built into fsspec and fsimpl2 for those not included in the main fsspec distribution. You can also pass parameters directly to the backend driver. For example, if you do not have S3 credentials, you can still access public data by specifying an anonymous connection, such as New in version 1.2.0. pd.read_csv( "s3://ncei-wcsd-archive/data/processed/SH1305/18kHz/SaKe2013" "-D20130523-T080854_to_SaKe2013-D20130523-T085643.csv", storage_options={"anon": True}, ) fsspec also allows complex URLs, for accessing data in compressed archives, local caching of files, and more. To locally cache the above example, you would modify the call to pd.read_csv( "simplecache::s3://ncei-wcsd-archive/data/processed/SH1305/18kHz/" "SaKe2013-D20130523-T080854_to_SaKe2013-D20130523-T085643.csv", storage_options={"s3": {"anon": True}}, ) where we specify that the “anon” parameter is meant for the “s3” part of the implementation, not to the caching implementation. Note that this caches to a temporary directory for the duration of the session only, but you can also specify a permanent store. Writing out data# Writing to CSV format# The Series and DataFrame objects have an instance method to_csv which allows storing the contents of the object as a comma-separated-values file. The function takes a number of arguments. Only the first is required. path_or_buf: A string path to the file to write or a file object. If a file object it must be opened with newline='' sep : Field delimiter for the output file (default “,”) na_rep: A string representation of a missing value (default ‘’) float_format: Format string for floating point numbers columns: Columns to write (default None) header: Whether to write out the column names (default True) index: whether to write row (index) names (default True) index_label: Column label(s) for index column(s) if desired. If None (default), and header and index are True, then the index names are used. (A sequence should be given if the DataFrame uses MultiIndex). mode : Python write mode, default ‘w’ encoding: a string representing the encoding to use if the contents are non-ASCII, for Python versions prior to 3 lineterminator: Character sequence denoting line end (default os.linesep) quoting: Set quoting rules as in csv module (default csv.QUOTE_MINIMAL). Note that if you have set a float_format then floats are converted to strings and csv.QUOTE_NONNUMERIC will treat them as non-numeric quotechar: Character used to quote fields (default ‘”’) doublequote: Control quoting of quotechar in fields (default True) escapechar: Character used to escape sep and quotechar when appropriate (default None) chunksize: Number of rows to write at a time date_format: Format string for datetime objects Writing a formatted string# The DataFrame object has an instance method to_string which allows control over the string representation of the object. All arguments are optional: buf default None, for example a StringIO object columns default None, which columns to write col_space default None, minimum width of each column. na_rep default NaN, representation of NA value formatters default None, a dictionary (by column) of functions each of which takes a single argument and returns a formatted string float_format default None, a function which takes a single (float) argument and returns a formatted string; to be applied to floats in the DataFrame. sparsify default True, set to False for a DataFrame with a hierarchical index to print every MultiIndex key at each row. index_names default True, will print the names of the indices index default True, will print the index (ie, row labels) header default True, will print the column labels justify default left, will print column headers left- or right-justified The Series object also has a to_string method, but with only the buf, na_rep, float_format arguments. There is also a length argument which, if set to True, will additionally output the length of the Series. JSON# Read and write JSON format files and strings. Writing JSON# A Series or DataFrame can be converted to a valid JSON string. Use to_json with optional parameters: path_or_buf : the pathname or buffer to write the output This can be None in which case a JSON string is returned orient : Series: default is index allowed values are {split, records, index} DataFrame: default is columns allowed values are {split, records, index, columns, values, table} The format of the JSON string split dict like {index -> [index], columns -> [columns], data -> [values]} records list like [{column -> value}, … , {column -> value}] index dict like {index -> {column -> value}} columns dict like {column -> {index -> value}} values just the values array table adhering to the JSON Table Schema date_format : string, type of date conversion, ‘epoch’ for timestamp, ‘iso’ for ISO8601. double_precision : The number of decimal places to use when encoding floating point values, default 10. force_ascii : force encoded string to be ASCII, default True. date_unit : The time unit to encode to, governs timestamp and ISO8601 precision. One of ‘s’, ‘ms’, ‘us’ or ‘ns’ for seconds, milliseconds, microseconds and nanoseconds respectively. Default ‘ms’. default_handler : The handler to call if an object cannot otherwise be converted to a suitable format for JSON. Takes a single argument, which is the object to convert, and returns a serializable object. lines : If records orient, then will write each record per line as json. Note NaN’s, NaT’s and None will be converted to null and datetime objects will be converted based on the date_format and date_unit parameters. In [218]: dfj = pd.DataFrame(np.random.randn(5, 2), columns=list("AB")) In [219]: json = dfj.to_json() In [220]: json Out[220]: '{"A":{"0":-0.1213062281,"1":0.6957746499,"2":0.9597255933,"3":-0.6199759194,"4":-0.7323393705},"B":{"0":-0.0978826728,"1":0.3417343559,"2":-1.1103361029,"3":0.1497483186,"4":0.6877383895}}' Orient options# There are a number of different options for the format of the resulting JSON file / string. Consider the following DataFrame and Series: In [221]: dfjo = pd.DataFrame( .....: dict(A=range(1, 4), B=range(4, 7), C=range(7, 10)), .....: columns=list("ABC"), .....: index=list("xyz"), .....: ) .....: In [222]: dfjo Out[222]: A B C x 1 4 7 y 2 5 8 z 3 6 9 In [223]: sjo = pd.Series(dict(x=15, y=16, z=17), name="D") In [224]: sjo Out[224]: x 15 y 16 z 17 Name: D, dtype: int64 Column oriented (the default for DataFrame) serializes the data as nested JSON objects with column labels acting as the primary index: In [225]: dfjo.to_json(orient="columns") Out[225]: '{"A":{"x":1,"y":2,"z":3},"B":{"x":4,"y":5,"z":6},"C":{"x":7,"y":8,"z":9}}' # Not available for Series Index oriented (the default for Series) similar to column oriented but the index labels are now primary: In [226]: dfjo.to_json(orient="index") Out[226]: '{"x":{"A":1,"B":4,"C":7},"y":{"A":2,"B":5,"C":8},"z":{"A":3,"B":6,"C":9}}' In [227]: sjo.to_json(orient="index") Out[227]: '{"x":15,"y":16,"z":17}' Record oriented serializes the data to a JSON array of column -> value records, index labels are not included. This is useful for passing DataFrame data to plotting libraries, for example the JavaScript library d3.js: In [228]: dfjo.to_json(orient="records") Out[228]: '[{"A":1,"B":4,"C":7},{"A":2,"B":5,"C":8},{"A":3,"B":6,"C":9}]' In [229]: sjo.to_json(orient="records") Out[229]: '[15,16,17]' Value oriented is a bare-bones option which serializes to nested JSON arrays of values only, column and index labels are not included: In [230]: dfjo.to_json(orient="values") Out[230]: '[[1,4,7],[2,5,8],[3,6,9]]' # Not available for Series Split oriented serializes to a JSON object containing separate entries for values, index and columns. Name is also included for Series: In [231]: dfjo.to_json(orient="split") Out[231]: '{"columns":["A","B","C"],"index":["x","y","z"],"data":[[1,4,7],[2,5,8],[3,6,9]]}' In [232]: sjo.to_json(orient="split") Out[232]: '{"name":"D","index":["x","y","z"],"data":[15,16,17]}' Table oriented serializes to the JSON Table Schema, allowing for the preservation of metadata including but not limited to dtypes and index names. Note Any orient option that encodes to a JSON object will not preserve the ordering of index and column labels during round-trip serialization. If you wish to preserve label ordering use the split option as it uses ordered containers. Date handling# Writing in ISO date format: In [233]: dfd = pd.DataFrame(np.random.randn(5, 2), columns=list("AB")) In [234]: dfd["date"] = pd.Timestamp("20130101") In [235]: dfd = dfd.sort_index(axis=1, ascending=False) In [236]: json = dfd.to_json(date_format="iso") In [237]: json Out[237]: '{"date":{"0":"2013-01-01T00:00:00.000","1":"2013-01-01T00:00:00.000","2":"2013-01-01T00:00:00.000","3":"2013-01-01T00:00:00.000","4":"2013-01-01T00:00:00.000"},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Writing in ISO date format, with microseconds: In [238]: json = dfd.to_json(date_format="iso", date_unit="us") In [239]: json Out[239]: '{"date":{"0":"2013-01-01T00:00:00.000000","1":"2013-01-01T00:00:00.000000","2":"2013-01-01T00:00:00.000000","3":"2013-01-01T00:00:00.000000","4":"2013-01-01T00:00:00.000000"},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Epoch timestamps, in seconds: In [240]: json = dfd.to_json(date_format="epoch", date_unit="s") In [241]: json Out[241]: '{"date":{"0":1356998400,"1":1356998400,"2":1356998400,"3":1356998400,"4":1356998400},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Writing to a file, with a date index and a date column: In [242]: dfj2 = dfj.copy() In [243]: dfj2["date"] = pd.Timestamp("20130101") In [244]: dfj2["ints"] = list(range(5)) In [245]: dfj2["bools"] = True In [246]: dfj2.index = pd.date_range("20130101", periods=5) In [247]: dfj2.to_json("test.json") In [248]: with open("test.json") as fh: .....: print(fh.read()) .....: {"A":{"1356998400000":-0.1213062281,"1357084800000":0.6957746499,"1357171200000":0.9597255933,"1357257600000":-0.6199759194,"1357344000000":-0.7323393705},"B":{"1356998400000":-0.0978826728,"1357084800000":0.3417343559,"1357171200000":-1.1103361029,"1357257600000":0.1497483186,"1357344000000":0.6877383895},"date":{"1356998400000":1356998400000,"1357084800000":1356998400000,"1357171200000":1356998400000,"1357257600000":1356998400000,"1357344000000":1356998400000},"ints":{"1356998400000":0,"1357084800000":1,"1357171200000":2,"1357257600000":3,"1357344000000":4},"bools":{"1356998400000":true,"1357084800000":true,"1357171200000":true,"1357257600000":true,"1357344000000":true}} Fallback behavior# If the JSON serializer cannot handle the container contents directly it will fall back in the following manner: if the dtype is unsupported (e.g. np.complex_) then the default_handler, if provided, will be called for each value, otherwise an exception is raised. if an object is unsupported it will attempt the following: check if the object has defined a toDict method and call it. A toDict method should return a dict which will then be JSON serialized. invoke the default_handler if one was provided. convert the object to a dict by traversing its contents. However this will often fail with an OverflowError or give unexpected results. In general the best approach for unsupported objects or dtypes is to provide a default_handler. For example: >>> DataFrame([1.0, 2.0, complex(1.0, 2.0)]).to_json() # raises RuntimeError: Unhandled numpy dtype 15 can be dealt with by specifying a simple default_handler: In [249]: pd.DataFrame([1.0, 2.0, complex(1.0, 2.0)]).to_json(default_handler=str) Out[249]: '{"0":{"0":"(1+0j)","1":"(2+0j)","2":"(1+2j)"}}' Reading JSON# Reading a JSON string to pandas object can take a number of parameters. The parser will try to parse a DataFrame if typ is not supplied or is None. To explicitly force Series parsing, pass typ=series filepath_or_buffer : a VALID JSON string or file handle / StringIO. The string could be a URL. Valid URL schemes include http, ftp, S3, and file. For file URLs, a host is expected. For instance, a local file could be file ://localhost/path/to/table.json typ : type of object to recover (series or frame), default ‘frame’ orient : Series : default is index allowed values are {split, records, index} DataFrame default is columns allowed values are {split, records, index, columns, values, table} The format of the JSON string split dict like {index -> [index], columns -> [columns], data -> [values]} records list like [{column -> value}, … , {column -> value}] index dict like {index -> {column -> value}} columns dict like {column -> {index -> value}} values just the values array table adhering to the JSON Table Schema dtype : if True, infer dtypes, if a dict of column to dtype, then use those, if False, then don’t infer dtypes at all, default is True, apply only to the data. convert_axes : boolean, try to convert the axes to the proper dtypes, default is True convert_dates : a list of columns to parse for dates; If True, then try to parse date-like columns, default is True. keep_default_dates : boolean, default True. If parsing dates, then parse the default date-like columns. numpy : direct decoding to NumPy arrays. default is False; Supports numeric data only, although labels may be non-numeric. Also note that the JSON ordering MUST be the same for each term if numpy=True. precise_float : boolean, default False. Set to enable usage of higher precision (strtod) function when decoding string to double values. Default (False) is to use fast but less precise builtin functionality. date_unit : string, the timestamp unit to detect if converting dates. Default None. By default the timestamp precision will be detected, if this is not desired then pass one of ‘s’, ‘ms’, ‘us’ or ‘ns’ to force timestamp precision to seconds, milliseconds, microseconds or nanoseconds respectively. lines : reads file as one json object per line. encoding : The encoding to use to decode py3 bytes. chunksize : when used in combination with lines=True, return a JsonReader which reads in chunksize lines per iteration. The parser will raise one of ValueError/TypeError/AssertionError if the JSON is not parseable. If a non-default orient was used when encoding to JSON be sure to pass the same option here so that decoding produces sensible results, see Orient Options for an overview. Data conversion# The default of convert_axes=True, dtype=True, and convert_dates=True will try to parse the axes, and all of the data into appropriate types, including dates. If you need to override specific dtypes, pass a dict to dtype. convert_axes should only be set to False if you need to preserve string-like numbers (e.g. ‘1’, ‘2’) in an axes. Note Large integer values may be converted to dates if convert_dates=True and the data and / or column labels appear ‘date-like’. The exact threshold depends on the date_unit specified. ‘date-like’ means that the column label meets one of the following criteria: it ends with '_at' it ends with '_time' it begins with 'timestamp' it is 'modified' it is 'date' Warning When reading JSON data, automatic coercing into dtypes has some quirks: an index can be reconstructed in a different order from serialization, that is, the returned order is not guaranteed to be the same as before serialization a column that was float data will be converted to integer if it can be done safely, e.g. a column of 1. bool columns will be converted to integer on reconstruction Thus there are times where you may want to specify specific dtypes via the dtype keyword argument. Reading from a JSON string: In [250]: pd.read_json(json) Out[250]: date B A 0 2013-01-01 0.403310 0.176444 1 2013-01-01 0.301624 -0.154951 2 2013-01-01 -1.369849 -2.179861 3 2013-01-01 1.462696 -0.954208 4 2013-01-01 -0.826591 -1.743161 Reading from a file: In [251]: pd.read_json("test.json") Out[251]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True Don’t convert any data (but still convert axes and dates): In [252]: pd.read_json("test.json", dtype=object).dtypes Out[252]: A object B object date object ints object bools object dtype: object Specify dtypes for conversion: In [253]: pd.read_json("test.json", dtype={"A": "float32", "bools": "int8"}).dtypes Out[253]: A float32 B float64 date datetime64[ns] ints int64 bools int8 dtype: object Preserve string indices: In [254]: si = pd.DataFrame( .....: np.zeros((4, 4)), columns=list(range(4)), index=[str(i) for i in range(4)] .....: ) .....: In [255]: si Out[255]: 0 1 2 3 0 0.0 0.0 0.0 0.0 1 0.0 0.0 0.0 0.0 2 0.0 0.0 0.0 0.0 3 0.0 0.0 0.0 0.0 In [256]: si.index Out[256]: Index(['0', '1', '2', '3'], dtype='object') In [257]: si.columns Out[257]: Int64Index([0, 1, 2, 3], dtype='int64') In [258]: json = si.to_json() In [259]: sij = pd.read_json(json, convert_axes=False) In [260]: sij Out[260]: 0 1 2 3 0 0 0 0 0 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 In [261]: sij.index Out[261]: Index(['0', '1', '2', '3'], dtype='object') In [262]: sij.columns Out[262]: Index(['0', '1', '2', '3'], dtype='object') Dates written in nanoseconds need to be read back in nanoseconds: In [263]: json = dfj2.to_json(date_unit="ns") # Try to parse timestamps as milliseconds -> Won't Work In [264]: dfju = pd.read_json(json, date_unit="ms") In [265]: dfju Out[265]: A B date ints bools 1356998400000000000 -0.121306 -0.097883 1356998400000000000 0 True 1357084800000000000 0.695775 0.341734 1356998400000000000 1 True 1357171200000000000 0.959726 -1.110336 1356998400000000000 2 True 1357257600000000000 -0.619976 0.149748 1356998400000000000 3 True 1357344000000000000 -0.732339 0.687738 1356998400000000000 4 True # Let pandas detect the correct precision In [266]: dfju = pd.read_json(json) In [267]: dfju Out[267]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True # Or specify that all timestamps are in nanoseconds In [268]: dfju = pd.read_json(json, date_unit="ns") In [269]: dfju Out[269]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True The Numpy parameter# Note This param has been deprecated as of version 1.0.0 and will raise a FutureWarning. This supports numeric data only. Index and columns labels may be non-numeric, e.g. strings, dates etc. If numpy=True is passed to read_json an attempt will be made to sniff an appropriate dtype during deserialization and to subsequently decode directly to NumPy arrays, bypassing the need for intermediate Python objects. This can provide speedups if you are deserialising a large amount of numeric data: In [270]: randfloats = np.random.uniform(-100, 1000, 10000) In [271]: randfloats.shape = (1000, 10) In [272]: dffloats = pd.DataFrame(randfloats, columns=list("ABCDEFGHIJ")) In [273]: jsonfloats = dffloats.to_json() In [274]: %timeit pd.read_json(jsonfloats) 7.91 ms +- 77.3 us per loop (mean +- std. dev. of 7 runs, 100 loops each) In [275]: %timeit pd.read_json(jsonfloats, numpy=True) 5.71 ms +- 333 us per loop (mean +- std. dev. of 7 runs, 100 loops each) The speedup is less noticeable for smaller datasets: In [276]: jsonfloats = dffloats.head(100).to_json() In [277]: %timeit pd.read_json(jsonfloats) 4.46 ms +- 25.9 us per loop (mean +- std. dev. of 7 runs, 100 loops each) In [278]: %timeit pd.read_json(jsonfloats, numpy=True) 4.09 ms +- 32.3 us per loop (mean +- std. dev. of 7 runs, 100 loops each) Warning Direct NumPy decoding makes a number of assumptions and may fail or produce unexpected output if these assumptions are not satisfied: data is numeric. data is uniform. The dtype is sniffed from the first value decoded. A ValueError may be raised, or incorrect output may be produced if this condition is not satisfied. labels are ordered. Labels are only read from the first container, it is assumed that each subsequent row / column has been encoded in the same order. This should be satisfied if the data was encoded using to_json but may not be the case if the JSON is from another source. Normalization# pandas provides a utility function to take a dict or list of dicts and normalize this semi-structured data into a flat table. In [279]: data = [ .....: {"id": 1, "name": {"first": "Coleen", "last": "Volk"}}, .....: {"name": {"given": "Mark", "family": "Regner"}}, .....: {"id": 2, "name": "Faye Raker"}, .....: ] .....: In [280]: pd.json_normalize(data) Out[280]: id name.first name.last name.given name.family name 0 1.0 Coleen Volk NaN NaN NaN 1 NaN NaN NaN Mark Regner NaN 2 2.0 NaN NaN NaN NaN Faye Raker In [281]: data = [ .....: { .....: "state": "Florida", .....: "shortname": "FL", .....: "info": {"governor": "Rick Scott"}, .....: "county": [ .....: {"name": "Dade", "population": 12345}, .....: {"name": "Broward", "population": 40000}, .....: {"name": "Palm Beach", "population": 60000}, .....: ], .....: }, .....: { .....: "state": "Ohio", .....: "shortname": "OH", .....: "info": {"governor": "John Kasich"}, .....: "county": [ .....: {"name": "Summit", "population": 1234}, .....: {"name": "Cuyahoga", "population": 1337}, .....: ], .....: }, .....: ] .....: In [282]: pd.json_normalize(data, "county", ["state", "shortname", ["info", "governor"]]) Out[282]: name population state shortname info.governor 0 Dade 12345 Florida FL Rick Scott 1 Broward 40000 Florida FL Rick Scott 2 Palm Beach 60000 Florida FL Rick Scott 3 Summit 1234 Ohio OH John Kasich 4 Cuyahoga 1337 Ohio OH John Kasich The max_level parameter provides more control over which level to end normalization. With max_level=1 the following snippet normalizes until 1st nesting level of the provided dict. In [283]: data = [ .....: { .....: "CreatedBy": {"Name": "User001"}, .....: "Lookup": { .....: "TextField": "Some text", .....: "UserField": {"Id": "ID001", "Name": "Name001"}, .....: }, .....: "Image": {"a": "b"}, .....: } .....: ] .....: In [284]: pd.json_normalize(data, max_level=1) Out[284]: CreatedBy.Name Lookup.TextField Lookup.UserField Image.a 0 User001 Some text {'Id': 'ID001', 'Name': 'Name001'} b Line delimited json# pandas is able to read and write line-delimited json files that are common in data processing pipelines using Hadoop or Spark. For line-delimited json files, pandas can also return an iterator which reads in chunksize lines at a time. This can be useful for large files or to read from a stream. In [285]: jsonl = """ .....: {"a": 1, "b": 2} .....: {"a": 3, "b": 4} .....: """ .....: In [286]: df = pd.read_json(jsonl, lines=True) In [287]: df Out[287]: a b 0 1 2 1 3 4 In [288]: df.to_json(orient="records", lines=True) Out[288]: '{"a":1,"b":2}\n{"a":3,"b":4}\n' # reader is an iterator that returns ``chunksize`` lines each iteration In [289]: with pd.read_json(StringIO(jsonl), lines=True, chunksize=1) as reader: .....: reader .....: for chunk in reader: .....: print(chunk) .....: Empty DataFrame Columns: [] Index: [] a b 0 1 2 a b 1 3 4 Table schema# Table Schema is a spec for describing tabular datasets as a JSON object. The JSON includes information on the field names, types, and other attributes. You can use the orient table to build a JSON string with two fields, schema and data. In [290]: df = pd.DataFrame( .....: { .....: "A": [1, 2, 3], .....: "B": ["a", "b", "c"], .....: "C": pd.date_range("2016-01-01", freq="d", periods=3), .....: }, .....: index=pd.Index(range(3), name="idx"), .....: ) .....: In [291]: df Out[291]: A B C idx 0 1 a 2016-01-01 1 2 b 2016-01-02 2 3 c 2016-01-03 In [292]: df.to_json(orient="table", date_format="iso") Out[292]: '{"schema":{"fields":[{"name":"idx","type":"integer"},{"name":"A","type":"integer"},{"name":"B","type":"string"},{"name":"C","type":"datetime"}],"primaryKey":["idx"],"pandas_version":"1.4.0"},"data":[{"idx":0,"A":1,"B":"a","C":"2016-01-01T00:00:00.000"},{"idx":1,"A":2,"B":"b","C":"2016-01-02T00:00:00.000"},{"idx":2,"A":3,"B":"c","C":"2016-01-03T00:00:00.000"}]}' The schema field contains the fields key, which itself contains a list of column name to type pairs, including the Index or MultiIndex (see below for a list of types). The schema field also contains a primaryKey field if the (Multi)index is unique. The second field, data, contains the serialized data with the records orient. The index is included, and any datetimes are ISO 8601 formatted, as required by the Table Schema spec. The full list of types supported are described in the Table Schema spec. This table shows the mapping from pandas types: pandas type Table Schema type int64 integer float64 number bool boolean datetime64[ns] datetime timedelta64[ns] duration categorical any object str A few notes on the generated table schema: The schema object contains a pandas_version field. This contains the version of pandas’ dialect of the schema, and will be incremented with each revision. All dates are converted to UTC when serializing. Even timezone naive values, which are treated as UTC with an offset of 0. In [293]: from pandas.io.json import build_table_schema In [294]: s = pd.Series(pd.date_range("2016", periods=4)) In [295]: build_table_schema(s) Out[295]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'datetime'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} datetimes with a timezone (before serializing), include an additional field tz with the time zone name (e.g. 'US/Central'). In [296]: s_tz = pd.Series(pd.date_range("2016", periods=12, tz="US/Central")) In [297]: build_table_schema(s_tz) Out[297]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'datetime', 'tz': 'US/Central'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} Periods are converted to timestamps before serialization, and so have the same behavior of being converted to UTC. In addition, periods will contain and additional field freq with the period’s frequency, e.g. 'A-DEC'. In [298]: s_per = pd.Series(1, index=pd.period_range("2016", freq="A-DEC", periods=4)) In [299]: build_table_schema(s_per) Out[299]: {'fields': [{'name': 'index', 'type': 'datetime', 'freq': 'A-DEC'}, {'name': 'values', 'type': 'integer'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} Categoricals use the any type and an enum constraint listing the set of possible values. Additionally, an ordered field is included: In [300]: s_cat = pd.Series(pd.Categorical(["a", "b", "a"])) In [301]: build_table_schema(s_cat) Out[301]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'any', 'constraints': {'enum': ['a', 'b']}, 'ordered': False}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} A primaryKey field, containing an array of labels, is included if the index is unique: In [302]: s_dupe = pd.Series([1, 2], index=[1, 1]) In [303]: build_table_schema(s_dupe) Out[303]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'integer'}], 'pandas_version': '1.4.0'} The primaryKey behavior is the same with MultiIndexes, but in this case the primaryKey is an array: In [304]: s_multi = pd.Series(1, index=pd.MultiIndex.from_product([("a", "b"), (0, 1)])) In [305]: build_table_schema(s_multi) Out[305]: {'fields': [{'name': 'level_0', 'type': 'string'}, {'name': 'level_1', 'type': 'integer'}, {'name': 'values', 'type': 'integer'}], 'primaryKey': FrozenList(['level_0', 'level_1']), 'pandas_version': '1.4.0'} The default naming roughly follows these rules: For series, the object.name is used. If that’s none, then the name is values For DataFrames, the stringified version of the column name is used For Index (not MultiIndex), index.name is used, with a fallback to index if that is None. For MultiIndex, mi.names is used. If any level has no name, then level_<i> is used. read_json also accepts orient='table' as an argument. This allows for the preservation of metadata such as dtypes and index names in a round-trippable manner. In [306]: df = pd.DataFrame( .....: { .....: "foo": [1, 2, 3, 4], .....: "bar": ["a", "b", "c", "d"], .....: "baz": pd.date_range("2018-01-01", freq="d", periods=4), .....: "qux": pd.Categorical(["a", "b", "c", "c"]), .....: }, .....: index=pd.Index(range(4), name="idx"), .....: ) .....: In [307]: df Out[307]: foo bar baz qux idx 0 1 a 2018-01-01 a 1 2 b 2018-01-02 b 2 3 c 2018-01-03 c 3 4 d 2018-01-04 c In [308]: df.dtypes Out[308]: foo int64 bar object baz datetime64[ns] qux category dtype: object In [309]: df.to_json("test.json", orient="table") In [310]: new_df = pd.read_json("test.json", orient="table") In [311]: new_df Out[311]: foo bar baz qux idx 0 1 a 2018-01-01 a 1 2 b 2018-01-02 b 2 3 c 2018-01-03 c 3 4 d 2018-01-04 c In [312]: new_df.dtypes Out[312]: foo int64 bar object baz datetime64[ns] qux category dtype: object Please note that the literal string ‘index’ as the name of an Index is not round-trippable, nor are any names beginning with 'level_' within a MultiIndex. These are used by default in DataFrame.to_json() to indicate missing values and the subsequent read cannot distinguish the intent. In [313]: df.index.name = "index" In [314]: df.to_json("test.json", orient="table") In [315]: new_df = pd.read_json("test.json", orient="table") In [316]: print(new_df.index.name) None When using orient='table' along with user-defined ExtensionArray, the generated schema will contain an additional extDtype key in the respective fields element. This extra key is not standard but does enable JSON roundtrips for extension types (e.g. read_json(df.to_json(orient="table"), orient="table")). The extDtype key carries the name of the extension, if you have properly registered the ExtensionDtype, pandas will use said name to perform a lookup into the registry and re-convert the serialized data into your custom dtype. HTML# Reading HTML content# Warning We highly encourage you to read the HTML Table Parsing gotchas below regarding the issues surrounding the BeautifulSoup4/html5lib/lxml parsers. The top-level read_html() function can accept an HTML string/file/URL and will parse HTML tables into list of pandas DataFrames. Let’s look at a few examples. Note read_html returns a list of DataFrame objects, even if there is only a single table contained in the HTML content. Read a URL with no options: In [320]: "https://www.fdic.gov/resources/resolutions/bank-failures/failed-bank-list" In [321]: pd.read_html(url) Out[321]: [ Bank NameBank CityCity StateSt ... Acquiring InstitutionAI Closing DateClosing FundFund 0 Almena State Bank Almena KS ... Equity Bank October 23, 2020 10538 1 First City Bank of Florida Fort Walton Beach FL ... United Fidelity Bank, fsb October 16, 2020 10537 2 The First State Bank Barboursville WV ... MVB Bank, Inc. April 3, 2020 10536 3 Ericson State Bank Ericson NE ... Farmers and Merchants Bank February 14, 2020 10535 4 City National Bank of New Jersey Newark NJ ... Industrial Bank November 1, 2019 10534 .. ... ... ... ... ... ... ... 558 Superior Bank, FSB Hinsdale IL ... Superior Federal, FSB July 27, 2001 6004 559 Malta National Bank Malta OH ... North Valley Bank May 3, 2001 4648 560 First Alliance Bank & Trust Co. Manchester NH ... Southern New Hampshire Bank & Trust February 2, 2001 4647 561 National State Bank of Metropolis Metropolis IL ... Banterra Bank of Marion December 14, 2000 4646 562 Bank of Honolulu Honolulu HI ... Bank of the Orient October 13, 2000 4645 [563 rows x 7 columns]] Note The data from the above URL changes every Monday so the resulting data above may be slightly different. Read in the content of the file from the above URL and pass it to read_html as a string: In [317]: html_str = """ .....: <table> .....: <tr> .....: <th>A</th> .....: <th colspan="1">B</th> .....: <th rowspan="1">C</th> .....: </tr> .....: <tr> .....: <td>a</td> .....: <td>b</td> .....: <td>c</td> .....: </tr> .....: </table> .....: """ .....: In [318]: with open("tmp.html", "w") as f: .....: f.write(html_str) .....: In [319]: df = pd.read_html("tmp.html") In [320]: df[0] Out[320]: A B C 0 a b c You can even pass in an instance of StringIO if you so desire: In [321]: dfs = pd.read_html(StringIO(html_str)) In [322]: dfs[0] Out[322]: A B C 0 a b c Note The following examples are not run by the IPython evaluator due to the fact that having so many network-accessing functions slows down the documentation build. If you spot an error or an example that doesn’t run, please do not hesitate to report it over on pandas GitHub issues page. Read a URL and match a table that contains specific text: match = "Metcalf Bank" df_list = pd.read_html(url, match=match) Specify a header row (by default <th> or <td> elements located within a <thead> are used to form the column index, if multiple rows are contained within <thead> then a MultiIndex is created); if specified, the header row is taken from the data minus the parsed header elements (<th> elements). dfs = pd.read_html(url, header=0) Specify an index column: dfs = pd.read_html(url, index_col=0) Specify a number of rows to skip: dfs = pd.read_html(url, skiprows=0) Specify a number of rows to skip using a list (range works as well): dfs = pd.read_html(url, skiprows=range(2)) Specify an HTML attribute: dfs1 = pd.read_html(url, attrs={"id": "table"}) dfs2 = pd.read_html(url, attrs={"class": "sortable"}) print(np.array_equal(dfs1[0], dfs2[0])) # Should be True Specify values that should be converted to NaN: dfs = pd.read_html(url, na_values=["No Acquirer"]) Specify whether to keep the default set of NaN values: dfs = pd.read_html(url, keep_default_na=False) Specify converters for columns. This is useful for numerical text data that has leading zeros. By default columns that are numerical are cast to numeric types and the leading zeros are lost. To avoid this, we can convert these columns to strings. url_mcc = "https://en.wikipedia.org/wiki/Mobile_country_code" dfs = pd.read_html( url_mcc, match="Telekom Albania", header=0, converters={"MNC": str}, ) Use some combination of the above: dfs = pd.read_html(url, match="Metcalf Bank", index_col=0) Read in pandas to_html output (with some loss of floating point precision): df = pd.DataFrame(np.random.randn(2, 2)) s = df.to_html(float_format="{0:.40g}".format) dfin = pd.read_html(s, index_col=0) The lxml backend will raise an error on a failed parse if that is the only parser you provide. If you only have a single parser you can provide just a string, but it is considered good practice to pass a list with one string if, for example, the function expects a sequence of strings. You may use: dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor=["lxml"]) Or you could pass flavor='lxml' without a list: dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor="lxml") However, if you have bs4 and html5lib installed and pass None or ['lxml', 'bs4'] then the parse will most likely succeed. Note that as soon as a parse succeeds, the function will return. dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor=["lxml", "bs4"]) Links can be extracted from cells along with the text using extract_links="all". In [323]: html_table = """ .....: <table> .....: <tr> .....: <th>GitHub</th> .....: </tr> .....: <tr> .....: <td><a href="https://github.com/pandas-dev/pandas">pandas</a></td> .....: </tr> .....: </table> .....: """ .....: In [324]: df = pd.read_html( .....: html_table, .....: extract_links="all" .....: )[0] .....: In [325]: df Out[325]: (GitHub, None) 0 (pandas, https://github.com/pandas-dev/pandas) In [326]: df[("GitHub", None)] Out[326]: 0 (pandas, https://github.com/pandas-dev/pandas) Name: (GitHub, None), dtype: object In [327]: df[("GitHub", None)].str[1] Out[327]: 0 https://github.com/pandas-dev/pandas Name: (GitHub, None), dtype: object New in version 1.5.0. Writing to HTML files# DataFrame objects have an instance method to_html which renders the contents of the DataFrame as an HTML table. The function arguments are as in the method to_string described above. Note Not all of the possible options for DataFrame.to_html are shown here for brevity’s sake. See to_html() for the full set of options. Note In an HTML-rendering supported environment like a Jupyter Notebook, display(HTML(...))` will render the raw HTML into the environment. In [328]: from IPython.display import display, HTML In [329]: df = pd.DataFrame(np.random.randn(2, 2)) In [330]: df Out[330]: 0 1 0 0.070319 1.773907 1 0.253908 0.414581 In [331]: html = df.to_html() In [332]: print(html) # raw html <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <th>1</th> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> In [333]: display(HTML(html)) <IPython.core.display.HTML object> The columns argument will limit the columns shown: In [334]: html = df.to_html(columns=[0]) In [335]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> </tr> <tr> <th>1</th> <td>0.253908</td> </tr> </tbody> </table> In [336]: display(HTML(html)) <IPython.core.display.HTML object> float_format takes a Python callable to control the precision of floating point values: In [337]: html = df.to_html(float_format="{0:.10f}".format) In [338]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.0703192665</td> <td>1.7739074228</td> </tr> <tr> <th>1</th> <td>0.2539083433</td> <td>0.4145805920</td> </tr> </tbody> </table> In [339]: display(HTML(html)) <IPython.core.display.HTML object> bold_rows will make the row labels bold by default, but you can turn that off: In [340]: html = df.to_html(bold_rows=False) In [341]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <td>1</td> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> In [342]: display(HTML(html)) <IPython.core.display.HTML object> The classes argument provides the ability to give the resulting HTML table CSS classes. Note that these classes are appended to the existing 'dataframe' class. In [343]: print(df.to_html(classes=["awesome_table_class", "even_more_awesome_class"])) <table border="1" class="dataframe awesome_table_class even_more_awesome_class"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <th>1</th> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> The render_links argument provides the ability to add hyperlinks to cells that contain URLs. In [344]: url_df = pd.DataFrame( .....: { .....: "name": ["Python", "pandas"], .....: "url": ["https://www.python.org/", "https://pandas.pydata.org"], .....: } .....: ) .....: In [345]: html = url_df.to_html(render_links=True) In [346]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>name</th> <th>url</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>Python</td> <td><a href="https://www.python.org/" target="_blank">https://www.python.org/</a></td> </tr> <tr> <th>1</th> <td>pandas</td> <td><a href="https://pandas.pydata.org" target="_blank">https://pandas.pydata.org</a></td> </tr> </tbody> </table> In [347]: display(HTML(html)) <IPython.core.display.HTML object> Finally, the escape argument allows you to control whether the “<”, “>” and “&” characters escaped in the resulting HTML (by default it is True). So to get the HTML without escaped characters pass escape=False In [348]: df = pd.DataFrame({"a": list("&<>"), "b": np.random.randn(3)}) Escaped: In [349]: html = df.to_html() In [350]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>a</th> <th>b</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>&amp;</td> <td>0.842321</td> </tr> <tr> <th>1</th> <td>&lt;</td> <td>0.211337</td> </tr> <tr> <th>2</th> <td>&gt;</td> <td>-1.055427</td> </tr> </tbody> </table> In [351]: display(HTML(html)) <IPython.core.display.HTML object> Not escaped: In [352]: html = df.to_html(escape=False) In [353]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>a</th> <th>b</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>&</td> <td>0.842321</td> </tr> <tr> <th>1</th> <td><</td> <td>0.211337</td> </tr> <tr> <th>2</th> <td>></td> <td>-1.055427</td> </tr> </tbody> </table> In [354]: display(HTML(html)) <IPython.core.display.HTML object> Note Some browsers may not show a difference in the rendering of the previous two HTML tables. HTML Table Parsing Gotchas# There are some versioning issues surrounding the libraries that are used to parse HTML tables in the top-level pandas io function read_html. Issues with lxml Benefits lxml is very fast. lxml requires Cython to install correctly. Drawbacks lxml does not make any guarantees about the results of its parse unless it is given strictly valid markup. In light of the above, we have chosen to allow you, the user, to use the lxml backend, but this backend will use html5lib if lxml fails to parse It is therefore highly recommended that you install both BeautifulSoup4 and html5lib, so that you will still get a valid result (provided everything else is valid) even if lxml fails. Issues with BeautifulSoup4 using lxml as a backend The above issues hold here as well since BeautifulSoup4 is essentially just a wrapper around a parser backend. Issues with BeautifulSoup4 using html5lib as a backend Benefits html5lib is far more lenient than lxml and consequently deals with real-life markup in a much saner way rather than just, e.g., dropping an element without notifying you. html5lib generates valid HTML5 markup from invalid markup automatically. This is extremely important for parsing HTML tables, since it guarantees a valid document. However, that does NOT mean that it is “correct”, since the process of fixing markup does not have a single definition. html5lib is pure Python and requires no additional build steps beyond its own installation. Drawbacks The biggest drawback to using html5lib is that it is slow as molasses. However consider the fact that many tables on the web are not big enough for the parsing algorithm runtime to matter. It is more likely that the bottleneck will be in the process of reading the raw text from the URL over the web, i.e., IO (input-output). For very large tables, this might not be true. LaTeX# New in version 1.3.0. Currently there are no methods to read from LaTeX, only output methods. Writing to LaTeX files# Note DataFrame and Styler objects currently have a to_latex method. We recommend using the Styler.to_latex() method over DataFrame.to_latex() due to the former’s greater flexibility with conditional styling, and the latter’s possible future deprecation. Review the documentation for Styler.to_latex, which gives examples of conditional styling and explains the operation of its keyword arguments. For simple application the following pattern is sufficient. In [355]: df = pd.DataFrame([[1, 2], [3, 4]], index=["a", "b"], columns=["c", "d"]) In [356]: print(df.style.to_latex()) \begin{tabular}{lrr} & c & d \\ a & 1 & 2 \\ b & 3 & 4 \\ \end{tabular} To format values before output, chain the Styler.format method. In [357]: print(df.style.format("€ {}").to_latex()) \begin{tabular}{lrr} & c & d \\ a & € 1 & € 2 \\ b & € 3 & € 4 \\ \end{tabular} XML# Reading XML# New in version 1.3.0. The top-level read_xml() function can accept an XML string/file/URL and will parse nodes and attributes into a pandas DataFrame. Note Since there is no standard XML structure where design types can vary in many ways, read_xml works best with flatter, shallow versions. If an XML document is deeply nested, use the stylesheet feature to transform XML into a flatter version. Let’s look at a few examples. Read an XML string: In [358]: xml = """<?xml version="1.0" encoding="UTF-8"?> .....: <bookstore> .....: <book category="cooking"> .....: <title lang="en">Everyday Italian</title> .....: <author>Giada De Laurentiis</author> .....: <year>2005</year> .....: <price>30.00</price> .....: </book> .....: <book category="children"> .....: <title lang="en">Harry Potter</title> .....: <author>J K. Rowling</author> .....: <year>2005</year> .....: <price>29.99</price> .....: </book> .....: <book category="web"> .....: <title lang="en">Learning XML</title> .....: <author>Erik T. Ray</author> .....: <year>2003</year> .....: <price>39.95</price> .....: </book> .....: </bookstore>""" .....: In [359]: df = pd.read_xml(xml) In [360]: df Out[360]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Read a URL with no options: In [361]: df = pd.read_xml("https://www.w3schools.com/xml/books.xml") In [362]: df Out[362]: category title author year price cover 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 None 1 children Harry Potter J K. Rowling 2005 29.99 None 2 web XQuery Kick Start Vaidyanathan Nagarajan 2003 49.99 None 3 web Learning XML Erik T. Ray 2003 39.95 paperback Read in the content of the “books.xml” file and pass it to read_xml as a string: In [363]: file_path = "books.xml" In [364]: with open(file_path, "w") as f: .....: f.write(xml) .....: In [365]: with open(file_path, "r") as f: .....: df = pd.read_xml(f.read()) .....: In [366]: df Out[366]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Read in the content of the “books.xml” as instance of StringIO or BytesIO and pass it to read_xml: In [367]: with open(file_path, "r") as f: .....: sio = StringIO(f.read()) .....: In [368]: df = pd.read_xml(sio) In [369]: df Out[369]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 In [370]: with open(file_path, "rb") as f: .....: bio = BytesIO(f.read()) .....: In [371]: df = pd.read_xml(bio) In [372]: df Out[372]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Even read XML from AWS S3 buckets such as NIH NCBI PMC Article Datasets providing Biomedical and Life Science Jorurnals: In [373]: df = pd.read_xml( .....: "s3://pmc-oa-opendata/oa_comm/xml/all/PMC1236943.xml", .....: xpath=".//journal-meta", .....: ) .....: In [374]: df Out[374]: journal-id journal-title issn publisher 0 Cardiovasc Ultrasound Cardiovascular Ultrasound 1476-7120 NaN With lxml as default parser, you access the full-featured XML library that extends Python’s ElementTree API. One powerful tool is ability to query nodes selectively or conditionally with more expressive XPath: In [375]: df = pd.read_xml(file_path, xpath="//book[year=2005]") In [376]: df Out[376]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 Specify only elements or only attributes to parse: In [377]: df = pd.read_xml(file_path, elems_only=True) In [378]: df Out[378]: title author year price 0 Everyday Italian Giada De Laurentiis 2005 30.00 1 Harry Potter J K. Rowling 2005 29.99 2 Learning XML Erik T. Ray 2003 39.95 In [379]: df = pd.read_xml(file_path, attrs_only=True) In [380]: df Out[380]: category 0 cooking 1 children 2 web XML documents can have namespaces with prefixes and default namespaces without prefixes both of which are denoted with a special attribute xmlns. In order to parse by node under a namespace context, xpath must reference a prefix. For example, below XML contains a namespace with prefix, doc, and URI at https://example.com. In order to parse doc:row nodes, namespaces must be used. In [381]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <doc:data xmlns:doc="https://example.com"> .....: <doc:row> .....: <doc:shape>square</doc:shape> .....: <doc:degrees>360</doc:degrees> .....: <doc:sides>4.0</doc:sides> .....: </doc:row> .....: <doc:row> .....: <doc:shape>circle</doc:shape> .....: <doc:degrees>360</doc:degrees> .....: <doc:sides/> .....: </doc:row> .....: <doc:row> .....: <doc:shape>triangle</doc:shape> .....: <doc:degrees>180</doc:degrees> .....: <doc:sides>3.0</doc:sides> .....: </doc:row> .....: </doc:data>""" .....: In [382]: df = pd.read_xml(xml, .....: xpath="//doc:row", .....: namespaces={"doc": "https://example.com"}) .....: In [383]: df Out[383]: shape degrees sides 0 square 360 4.0 1 circle 360 NaN 2 triangle 180 3.0 Similarly, an XML document can have a default namespace without prefix. Failing to assign a temporary prefix will return no nodes and raise a ValueError. But assigning any temporary name to correct URI allows parsing by nodes. In [384]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <data xmlns="https://example.com"> .....: <row> .....: <shape>square</shape> .....: <degrees>360</degrees> .....: <sides>4.0</sides> .....: </row> .....: <row> .....: <shape>circle</shape> .....: <degrees>360</degrees> .....: <sides/> .....: </row> .....: <row> .....: <shape>triangle</shape> .....: <degrees>180</degrees> .....: <sides>3.0</sides> .....: </row> .....: </data>""" .....: In [385]: df = pd.read_xml(xml, .....: xpath="//pandas:row", .....: namespaces={"pandas": "https://example.com"}) .....: In [386]: df Out[386]: shape degrees sides 0 square 360 4.0 1 circle 360 NaN 2 triangle 180 3.0 However, if XPath does not reference node names such as default, /*, then namespaces is not required. With lxml as parser, you can flatten nested XML documents with an XSLT script which also can be string/file/URL types. As background, XSLT is a special-purpose language written in a special XML file that can transform original XML documents into other XML, HTML, even text (CSV, JSON, etc.) using an XSLT processor. For example, consider this somewhat nested structure of Chicago “L” Rides where station and rides elements encapsulate data in their own sections. With below XSLT, lxml can transform original nested document into a flatter output (as shown below for demonstration) for easier parse into DataFrame: In [387]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <response> .....: <row> .....: <station id="40850" name="Library"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>864.2</avg_weekday_rides> .....: <avg_saturday_rides>534</avg_saturday_rides> .....: <avg_sunday_holiday_rides>417.2</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: <row> .....: <station id="41700" name="Washington/Wabash"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>2707.4</avg_weekday_rides> .....: <avg_saturday_rides>1909.8</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1438.6</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: <row> .....: <station id="40380" name="Clark/Lake"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>2949.6</avg_weekday_rides> .....: <avg_saturday_rides>1657</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1453.8</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: </response>""" .....: In [388]: xsl = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> .....: <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/> .....: <xsl:strip-space elements="*"/> .....: <xsl:template match="/response"> .....: <xsl:copy> .....: <xsl:apply-templates select="row"/> .....: </xsl:copy> .....: </xsl:template> .....: <xsl:template match="row"> .....: <xsl:copy> .....: <station_id><xsl:value-of select="station/@id"/></station_id> .....: <station_name><xsl:value-of select="station/@name"/></station_name> .....: <xsl:copy-of select="month|rides/*"/> .....: </xsl:copy> .....: </xsl:template> .....: </xsl:stylesheet>""" .....: In [389]: output = """<?xml version='1.0' encoding='utf-8'?> .....: <response> .....: <row> .....: <station_id>40850</station_id> .....: <station_name>Library</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>864.2</avg_weekday_rides> .....: <avg_saturday_rides>534</avg_saturday_rides> .....: <avg_sunday_holiday_rides>417.2</avg_sunday_holiday_rides> .....: </row> .....: <row> .....: <station_id>41700</station_id> .....: <station_name>Washington/Wabash</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>2707.4</avg_weekday_rides> .....: <avg_saturday_rides>1909.8</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1438.6</avg_sunday_holiday_rides> .....: </row> .....: <row> .....: <station_id>40380</station_id> .....: <station_name>Clark/Lake</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>2949.6</avg_weekday_rides> .....: <avg_saturday_rides>1657</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1453.8</avg_sunday_holiday_rides> .....: </row> .....: </response>""" .....: In [390]: df = pd.read_xml(xml, stylesheet=xsl) In [391]: df Out[391]: station_id station_name ... avg_saturday_rides avg_sunday_holiday_rides 0 40850 Library ... 534.0 417.2 1 41700 Washington/Wabash ... 1909.8 1438.6 2 40380 Clark/Lake ... 1657.0 1453.8 [3 rows x 6 columns] For very large XML files that can range in hundreds of megabytes to gigabytes, pandas.read_xml() supports parsing such sizeable files using lxml’s iterparse and etree’s iterparse which are memory-efficient methods to iterate through an XML tree and extract specific elements and attributes. without holding entire tree in memory. New in version 1.5.0. To use this feature, you must pass a physical XML file path into read_xml and use the iterparse argument. Files should not be compressed or point to online sources but stored on local disk. Also, iterparse should be a dictionary where the key is the repeating nodes in document (which become the rows) and the value is a list of any element or attribute that is a descendant (i.e., child, grandchild) of repeating node. Since XPath is not used in this method, descendants do not need to share same relationship with one another. Below shows example of reading in Wikipedia’s very large (12 GB+) latest article data dump. In [1]: df = pd.read_xml( ... "/path/to/downloaded/enwikisource-latest-pages-articles.xml", ... iterparse = {"page": ["title", "ns", "id"]} ... ) ... df Out[2]: title ns id 0 Gettysburg Address 0 21450 1 Main Page 0 42950 2 Declaration by United Nations 0 8435 3 Constitution of the United States of America 0 8435 4 Declaration of Independence (Israel) 0 17858 ... ... ... ... 3578760 Page:Black cat 1897 07 v2 n10.pdf/17 104 219649 3578761 Page:Black cat 1897 07 v2 n10.pdf/43 104 219649 3578762 Page:Black cat 1897 07 v2 n10.pdf/44 104 219649 3578763 The History of Tom Jones, a Foundling/Book IX 0 12084291 3578764 Page:Shakespeare of Stratford (1926) Yale.djvu/91 104 21450 [3578765 rows x 3 columns] Writing XML# New in version 1.3.0. DataFrame objects have an instance method to_xml which renders the contents of the DataFrame as an XML document. Note This method does not support special properties of XML including DTD, CData, XSD schemas, processing instructions, comments, and others. Only namespaces at the root level is supported. However, stylesheet allows design changes after initial output. Let’s look at a few examples. Write an XML without options: In [392]: geom_df = pd.DataFrame( .....: { .....: "shape": ["square", "circle", "triangle"], .....: "degrees": [360, 360, 180], .....: "sides": [4, np.nan, 3], .....: } .....: ) .....: In [393]: print(geom_df.to_xml()) <?xml version='1.0' encoding='utf-8'?> <data> <row> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </row> <row> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </row> <row> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Write an XML with new root and row name: In [394]: print(geom_df.to_xml(root_name="geometry", row_name="objects")) <?xml version='1.0' encoding='utf-8'?> <geometry> <objects> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </objects> <objects> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </objects> <objects> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </objects> </geometry> Write an attribute-centric XML: In [395]: print(geom_df.to_xml(attr_cols=geom_df.columns.tolist())) <?xml version='1.0' encoding='utf-8'?> <data> <row index="0" shape="square" degrees="360" sides="4.0"/> <row index="1" shape="circle" degrees="360"/> <row index="2" shape="triangle" degrees="180" sides="3.0"/> </data> Write a mix of elements and attributes: In [396]: print( .....: geom_df.to_xml( .....: index=False, .....: attr_cols=['shape'], .....: elem_cols=['degrees', 'sides']) .....: ) .....: <?xml version='1.0' encoding='utf-8'?> <data> <row shape="square"> <degrees>360</degrees> <sides>4.0</sides> </row> <row shape="circle"> <degrees>360</degrees> <sides/> </row> <row shape="triangle"> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Any DataFrames with hierarchical columns will be flattened for XML element names with levels delimited by underscores: In [397]: ext_geom_df = pd.DataFrame( .....: { .....: "type": ["polygon", "other", "polygon"], .....: "shape": ["square", "circle", "triangle"], .....: "degrees": [360, 360, 180], .....: "sides": [4, np.nan, 3], .....: } .....: ) .....: In [398]: pvt_df = ext_geom_df.pivot_table(index='shape', .....: columns='type', .....: values=['degrees', 'sides'], .....: aggfunc='sum') .....: In [399]: pvt_df Out[399]: degrees sides type other polygon other polygon shape circle 360.0 NaN 0.0 NaN square NaN 360.0 NaN 4.0 triangle NaN 180.0 NaN 3.0 In [400]: print(pvt_df.to_xml()) <?xml version='1.0' encoding='utf-8'?> <data> <row> <shape>circle</shape> <degrees_other>360.0</degrees_other> <degrees_polygon/> <sides_other>0.0</sides_other> <sides_polygon/> </row> <row> <shape>square</shape> <degrees_other/> <degrees_polygon>360.0</degrees_polygon> <sides_other/> <sides_polygon>4.0</sides_polygon> </row> <row> <shape>triangle</shape> <degrees_other/> <degrees_polygon>180.0</degrees_polygon> <sides_other/> <sides_polygon>3.0</sides_polygon> </row> </data> Write an XML with default namespace: In [401]: print(geom_df.to_xml(namespaces={"": "https://example.com"})) <?xml version='1.0' encoding='utf-8'?> <data xmlns="https://example.com"> <row> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </row> <row> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </row> <row> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Write an XML with namespace prefix: In [402]: print( .....: geom_df.to_xml(namespaces={"doc": "https://example.com"}, .....: prefix="doc") .....: ) .....: <?xml version='1.0' encoding='utf-8'?> <doc:data xmlns:doc="https://example.com"> <doc:row> <doc:index>0</doc:index> <doc:shape>square</doc:shape> <doc:degrees>360</doc:degrees> <doc:sides>4.0</doc:sides> </doc:row> <doc:row> <doc:index>1</doc:index> <doc:shape>circle</doc:shape> <doc:degrees>360</doc:degrees> <doc:sides/> </doc:row> <doc:row> <doc:index>2</doc:index> <doc:shape>triangle</doc:shape> <doc:degrees>180</doc:degrees> <doc:sides>3.0</doc:sides> </doc:row> </doc:data> Write an XML without declaration or pretty print: In [403]: print( .....: geom_df.to_xml(xml_declaration=False, .....: pretty_print=False) .....: ) .....: <data><row><index>0</index><shape>square</shape><degrees>360</degrees><sides>4.0</sides></row><row><index>1</index><shape>circle</shape><degrees>360</degrees><sides/></row><row><index>2</index><shape>triangle</shape><degrees>180</degrees><sides>3.0</sides></row></data> Write an XML and transform with stylesheet: In [404]: xsl = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> .....: <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/> .....: <xsl:strip-space elements="*"/> .....: <xsl:template match="/data"> .....: <geometry> .....: <xsl:apply-templates select="row"/> .....: </geometry> .....: </xsl:template> .....: <xsl:template match="row"> .....: <object index="{index}"> .....: <xsl:if test="shape!='circle'"> .....: <xsl:attribute name="type">polygon</xsl:attribute> .....: </xsl:if> .....: <xsl:copy-of select="shape"/> .....: <property> .....: <xsl:copy-of select="degrees|sides"/> .....: </property> .....: </object> .....: </xsl:template> .....: </xsl:stylesheet>""" .....: In [405]: print(geom_df.to_xml(stylesheet=xsl)) <?xml version="1.0"?> <geometry> <object index="0" type="polygon"> <shape>square</shape> <property> <degrees>360</degrees> <sides>4.0</sides> </property> </object> <object index="1"> <shape>circle</shape> <property> <degrees>360</degrees> <sides/> </property> </object> <object index="2" type="polygon"> <shape>triangle</shape> <property> <degrees>180</degrees> <sides>3.0</sides> </property> </object> </geometry> XML Final Notes# All XML documents adhere to W3C specifications. Both etree and lxml parsers will fail to parse any markup document that is not well-formed or follows XML syntax rules. Do be aware HTML is not an XML document unless it follows XHTML specs. However, other popular markup types including KML, XAML, RSS, MusicML, MathML are compliant XML schemas. For above reason, if your application builds XML prior to pandas operations, use appropriate DOM libraries like etree and lxml to build the necessary document and not by string concatenation or regex adjustments. Always remember XML is a special text file with markup rules. With very large XML files (several hundred MBs to GBs), XPath and XSLT can become memory-intensive operations. Be sure to have enough available RAM for reading and writing to large XML files (roughly about 5 times the size of text). Because XSLT is a programming language, use it with caution since such scripts can pose a security risk in your environment and can run large or infinite recursive operations. Always test scripts on small fragments before full run. The etree parser supports all functionality of both read_xml and to_xml except for complex XPath and any XSLT. Though limited in features, etree is still a reliable and capable parser and tree builder. Its performance may trail lxml to a certain degree for larger files but relatively unnoticeable on small to medium size files. Excel files# The read_excel() method can read Excel 2007+ (.xlsx) files using the openpyxl Python module. Excel 2003 (.xls) files can be read using xlrd. Binary Excel (.xlsb) files can be read using pyxlsb. The to_excel() instance method is used for saving a DataFrame to Excel. Generally the semantics are similar to working with csv data. See the cookbook for some advanced strategies. Warning The xlwt package for writing old-style .xls excel files is no longer maintained. The xlrd package is now only for reading old-style .xls files. Before pandas 1.3.0, the default argument engine=None to read_excel() would result in using the xlrd engine in many cases, including new Excel 2007+ (.xlsx) files. pandas will now default to using the openpyxl engine. It is strongly encouraged to install openpyxl to read Excel 2007+ (.xlsx) files. Please do not report issues when using ``xlrd`` to read ``.xlsx`` files. This is no longer supported, switch to using openpyxl instead. Attempting to use the xlwt engine will raise a FutureWarning unless the option io.excel.xls.writer is set to "xlwt". While this option is now deprecated and will also raise a FutureWarning, it can be globally set and the warning suppressed. Users are recommended to write .xlsx files using the openpyxl engine instead. Reading Excel files# In the most basic use-case, read_excel takes a path to an Excel file, and the sheet_name indicating which sheet to parse. # Returns a DataFrame pd.read_excel("path_to_file.xls", sheet_name="Sheet1") ExcelFile class# To facilitate working with multiple sheets from the same file, the ExcelFile class can be used to wrap the file and can be passed into read_excel There will be a performance benefit for reading multiple sheets as the file is read into memory only once. xlsx = pd.ExcelFile("path_to_file.xls") df = pd.read_excel(xlsx, "Sheet1") The ExcelFile class can also be used as a context manager. with pd.ExcelFile("path_to_file.xls") as xls: df1 = pd.read_excel(xls, "Sheet1") df2 = pd.read_excel(xls, "Sheet2") The sheet_names property will generate a list of the sheet names in the file. The primary use-case for an ExcelFile is parsing multiple sheets with different parameters: data = {} # For when Sheet1's format differs from Sheet2 with pd.ExcelFile("path_to_file.xls") as xls: data["Sheet1"] = pd.read_excel(xls, "Sheet1", index_col=None, na_values=["NA"]) data["Sheet2"] = pd.read_excel(xls, "Sheet2", index_col=1) Note that if the same parsing parameters are used for all sheets, a list of sheet names can simply be passed to read_excel with no loss in performance. # using the ExcelFile class data = {} with pd.ExcelFile("path_to_file.xls") as xls: data["Sheet1"] = pd.read_excel(xls, "Sheet1", index_col=None, na_values=["NA"]) data["Sheet2"] = pd.read_excel(xls, "Sheet2", index_col=None, na_values=["NA"]) # equivalent using the read_excel function data = pd.read_excel( "path_to_file.xls", ["Sheet1", "Sheet2"], index_col=None, na_values=["NA"] ) ExcelFile can also be called with a xlrd.book.Book object as a parameter. This allows the user to control how the excel file is read. For example, sheets can be loaded on demand by calling xlrd.open_workbook() with on_demand=True. import xlrd xlrd_book = xlrd.open_workbook("path_to_file.xls", on_demand=True) with pd.ExcelFile(xlrd_book) as xls: df1 = pd.read_excel(xls, "Sheet1") df2 = pd.read_excel(xls, "Sheet2") Specifying sheets# Note The second argument is sheet_name, not to be confused with ExcelFile.sheet_names. Note An ExcelFile’s attribute sheet_names provides access to a list of sheets. The arguments sheet_name allows specifying the sheet or sheets to read. The default value for sheet_name is 0, indicating to read the first sheet Pass a string to refer to the name of a particular sheet in the workbook. Pass an integer to refer to the index of a sheet. Indices follow Python convention, beginning at 0. Pass a list of either strings or integers, to return a dictionary of specified sheets. Pass a None to return a dictionary of all available sheets. # Returns a DataFrame pd.read_excel("path_to_file.xls", "Sheet1", index_col=None, na_values=["NA"]) Using the sheet index: # Returns a DataFrame pd.read_excel("path_to_file.xls", 0, index_col=None, na_values=["NA"]) Using all default values: # Returns a DataFrame pd.read_excel("path_to_file.xls") Using None to get all sheets: # Returns a dictionary of DataFrames pd.read_excel("path_to_file.xls", sheet_name=None) Using a list to get multiple sheets: # Returns the 1st and 4th sheet, as a dictionary of DataFrames. pd.read_excel("path_to_file.xls", sheet_name=["Sheet1", 3]) read_excel can read more than one sheet, by setting sheet_name to either a list of sheet names, a list of sheet positions, or None to read all sheets. Sheets can be specified by sheet index or sheet name, using an integer or string, respectively. Reading a MultiIndex# read_excel can read a MultiIndex index, by passing a list of columns to index_col and a MultiIndex column by passing a list of rows to header. If either the index or columns have serialized level names those will be read in as well by specifying the rows/columns that make up the levels. For example, to read in a MultiIndex index without names: In [406]: df = pd.DataFrame( .....: {"a": [1, 2, 3, 4], "b": [5, 6, 7, 8]}, .....: index=pd.MultiIndex.from_product([["a", "b"], ["c", "d"]]), .....: ) .....: In [407]: df.to_excel("path_to_file.xlsx") In [408]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1]) In [409]: df Out[409]: a b a c 1 5 d 2 6 b c 3 7 d 4 8 If the index has level names, they will parsed as well, using the same parameters. In [410]: df.index = df.index.set_names(["lvl1", "lvl2"]) In [411]: df.to_excel("path_to_file.xlsx") In [412]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1]) In [413]: df Out[413]: a b lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 If the source file has both MultiIndex index and columns, lists specifying each should be passed to index_col and header: In [414]: df.columns = pd.MultiIndex.from_product([["a"], ["b", "d"]], names=["c1", "c2"]) In [415]: df.to_excel("path_to_file.xlsx") In [416]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1], header=[0, 1]) In [417]: df Out[417]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 Missing values in columns specified in index_col will be forward filled to allow roundtripping with to_excel for merged_cells=True. To avoid forward filling the missing values use set_index after reading the data instead of index_col. Parsing specific columns# It is often the case that users will insert columns to do temporary computations in Excel and you may not want to read in those columns. read_excel takes a usecols keyword to allow you to specify a subset of columns to parse. Changed in version 1.0.0. Passing in an integer for usecols will no longer work. Please pass in a list of ints from 0 to usecols inclusive instead. You can specify a comma-delimited set of Excel columns and ranges as a string: pd.read_excel("path_to_file.xls", "Sheet1", usecols="A,C:E") If usecols is a list of integers, then it is assumed to be the file column indices to be parsed. pd.read_excel("path_to_file.xls", "Sheet1", usecols=[0, 2, 3]) Element order is ignored, so usecols=[0, 1] is the same as [1, 0]. If usecols is a list of strings, it is assumed that each string corresponds to a column name provided either by the user in names or inferred from the document header row(s). Those strings define which columns will be parsed: pd.read_excel("path_to_file.xls", "Sheet1", usecols=["foo", "bar"]) Element order is ignored, so usecols=['baz', 'joe'] is the same as ['joe', 'baz']. If usecols is callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True. pd.read_excel("path_to_file.xls", "Sheet1", usecols=lambda x: x.isalpha()) Parsing dates# Datetime-like values are normally automatically converted to the appropriate dtype when reading the excel file. But if you have a column of strings that look like dates (but are not actually formatted as dates in excel), you can use the parse_dates keyword to parse those strings to datetimes: pd.read_excel("path_to_file.xls", "Sheet1", parse_dates=["date_strings"]) Cell converters# It is possible to transform the contents of Excel cells via the converters option. For instance, to convert a column to boolean: pd.read_excel("path_to_file.xls", "Sheet1", converters={"MyBools": bool}) This options handles missing values and treats exceptions in the converters as missing data. Transformations are applied cell by cell rather than to the column as a whole, so the array dtype is not guaranteed. For instance, a column of integers with missing values cannot be transformed to an array with integer dtype, because NaN is strictly a float. You can manually mask missing data to recover integer dtype: def cfun(x): return int(x) if x else -1 pd.read_excel("path_to_file.xls", "Sheet1", converters={"MyInts": cfun}) Dtype specifications# As an alternative to converters, the type for an entire column can be specified using the dtype keyword, which takes a dictionary mapping column names to types. To interpret data with no type inference, use the type str or object. pd.read_excel("path_to_file.xls", dtype={"MyInts": "int64", "MyText": str}) Writing Excel files# Writing Excel files to disk# To write a DataFrame object to a sheet of an Excel file, you can use the to_excel instance method. The arguments are largely the same as to_csv described above, the first argument being the name of the excel file, and the optional second argument the name of the sheet to which the DataFrame should be written. For example: df.to_excel("path_to_file.xlsx", sheet_name="Sheet1") Files with a .xls extension will be written using xlwt and those with a .xlsx extension will be written using xlsxwriter (if available) or openpyxl. The DataFrame will be written in a way that tries to mimic the REPL output. The index_label will be placed in the second row instead of the first. You can place it in the first row by setting the merge_cells option in to_excel() to False: df.to_excel("path_to_file.xlsx", index_label="label", merge_cells=False) In order to write separate DataFrames to separate sheets in a single Excel file, one can pass an ExcelWriter. with pd.ExcelWriter("path_to_file.xlsx") as writer: df1.to_excel(writer, sheet_name="Sheet1") df2.to_excel(writer, sheet_name="Sheet2") Writing Excel files to memory# pandas supports writing Excel files to buffer-like objects such as StringIO or BytesIO using ExcelWriter. from io import BytesIO bio = BytesIO() # By setting the 'engine' in the ExcelWriter constructor. writer = pd.ExcelWriter(bio, engine="xlsxwriter") df.to_excel(writer, sheet_name="Sheet1") # Save the workbook writer.save() # Seek to the beginning and read to copy the workbook to a variable in memory bio.seek(0) workbook = bio.read() Note engine is optional but recommended. Setting the engine determines the version of workbook produced. Setting engine='xlrd' will produce an Excel 2003-format workbook (xls). Using either 'openpyxl' or 'xlsxwriter' will produce an Excel 2007-format workbook (xlsx). If omitted, an Excel 2007-formatted workbook is produced. Excel writer engines# Deprecated since version 1.2.0: As the xlwt package is no longer maintained, the xlwt engine will be removed from a future version of pandas. This is the only engine in pandas that supports writing to .xls files. pandas chooses an Excel writer via two methods: the engine keyword argument the filename extension (via the default specified in config options) By default, pandas uses the XlsxWriter for .xlsx, openpyxl for .xlsm, and xlwt for .xls files. If you have multiple engines installed, you can set the default engine through setting the config options io.excel.xlsx.writer and io.excel.xls.writer. pandas will fall back on openpyxl for .xlsx files if Xlsxwriter is not available. To specify which writer you want to use, you can pass an engine keyword argument to to_excel and to ExcelWriter. The built-in engines are: openpyxl: version 2.4 or higher is required xlsxwriter xlwt # By setting the 'engine' in the DataFrame 'to_excel()' methods. df.to_excel("path_to_file.xlsx", sheet_name="Sheet1", engine="xlsxwriter") # By setting the 'engine' in the ExcelWriter constructor. writer = pd.ExcelWriter("path_to_file.xlsx", engine="xlsxwriter") # Or via pandas configuration. from pandas import options # noqa: E402 options.io.excel.xlsx.writer = "xlsxwriter" df.to_excel("path_to_file.xlsx", sheet_name="Sheet1") Style and formatting# The look and feel of Excel worksheets created from pandas can be modified using the following parameters on the DataFrame’s to_excel method. float_format : Format string for floating point numbers (default None). freeze_panes : A tuple of two integers representing the bottommost row and rightmost column to freeze. Each of these parameters is one-based, so (1, 1) will freeze the first row and first column (default None). Using the Xlsxwriter engine provides many options for controlling the format of an Excel worksheet created with the to_excel method. Excellent examples can be found in the Xlsxwriter documentation here: https://xlsxwriter.readthedocs.io/working_with_pandas.html OpenDocument Spreadsheets# New in version 0.25. The read_excel() method can also read OpenDocument spreadsheets using the odfpy module. The semantics and features for reading OpenDocument spreadsheets match what can be done for Excel files using engine='odf'. # Returns a DataFrame pd.read_excel("path_to_file.ods", engine="odf") Note Currently pandas only supports reading OpenDocument spreadsheets. Writing is not implemented. Binary Excel (.xlsb) files# New in version 1.0.0. The read_excel() method can also read binary Excel files using the pyxlsb module. The semantics and features for reading binary Excel files mostly match what can be done for Excel files using engine='pyxlsb'. pyxlsb does not recognize datetime types in files and will return floats instead. # Returns a DataFrame pd.read_excel("path_to_file.xlsb", engine="pyxlsb") Note Currently pandas only supports reading binary Excel files. Writing is not implemented. Clipboard# A handy way to grab data is to use the read_clipboard() method, which takes the contents of the clipboard buffer and passes them to the read_csv method. For instance, you can copy the following text to the clipboard (CTRL-C on many operating systems): A B C x 1 4 p y 2 5 q z 3 6 r And then import the data directly to a DataFrame by calling: >>> clipdf = pd.read_clipboard() >>> clipdf A B C x 1 4 p y 2 5 q z 3 6 r The to_clipboard method can be used to write the contents of a DataFrame to the clipboard. Following which you can paste the clipboard contents into other applications (CTRL-V on many operating systems). Here we illustrate writing a DataFrame into clipboard and reading it back. >>> df = pd.DataFrame( ... {"A": [1, 2, 3], "B": [4, 5, 6], "C": ["p", "q", "r"]}, index=["x", "y", "z"] ... ) >>> df A B C x 1 4 p y 2 5 q z 3 6 r >>> df.to_clipboard() >>> pd.read_clipboard() A B C x 1 4 p y 2 5 q z 3 6 r We can see that we got the same content back, which we had earlier written to the clipboard. Note You may need to install xclip or xsel (with PyQt5, PyQt4 or qtpy) on Linux to use these methods. Pickling# All pandas objects are equipped with to_pickle methods which use Python’s cPickle module to save data structures to disk using the pickle format. In [418]: df Out[418]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 In [419]: df.to_pickle("foo.pkl") The read_pickle function in the pandas namespace can be used to load any pickled pandas object (or any other pickled object) from file: In [420]: pd.read_pickle("foo.pkl") Out[420]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 Warning Loading pickled data received from untrusted sources can be unsafe. See: https://docs.python.org/3/library/pickle.html Warning read_pickle() is only guaranteed backwards compatible back to pandas version 0.20.3 Compressed pickle files# read_pickle(), DataFrame.to_pickle() and Series.to_pickle() can read and write compressed pickle files. The compression types of gzip, bz2, xz, zstd are supported for reading and writing. The zip file format only supports reading and must contain only one data file to be read. The compression type can be an explicit parameter or be inferred from the file extension. If ‘infer’, then use gzip, bz2, zip, xz, zstd if filename ends in '.gz', '.bz2', '.zip', '.xz', or '.zst', respectively. The compression parameter can also be a dict in order to pass options to the compression protocol. It must have a 'method' key set to the name of the compression protocol, which must be one of {'zip', 'gzip', 'bz2', 'xz', 'zstd'}. All other key-value pairs are passed to the underlying compression library. In [421]: df = pd.DataFrame( .....: { .....: "A": np.random.randn(1000), .....: "B": "foo", .....: "C": pd.date_range("20130101", periods=1000, freq="s"), .....: } .....: ) .....: In [422]: df Out[422]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] Using an explicit compression type: In [423]: df.to_pickle("data.pkl.compress", compression="gzip") In [424]: rt = pd.read_pickle("data.pkl.compress", compression="gzip") In [425]: rt Out[425]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] Inferring compression type from the extension: In [426]: df.to_pickle("data.pkl.xz", compression="infer") In [427]: rt = pd.read_pickle("data.pkl.xz", compression="infer") In [428]: rt Out[428]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] The default is to ‘infer’: In [429]: df.to_pickle("data.pkl.gz") In [430]: rt = pd.read_pickle("data.pkl.gz") In [431]: rt Out[431]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] In [432]: df["A"].to_pickle("s1.pkl.bz2") In [433]: rt = pd.read_pickle("s1.pkl.bz2") In [434]: rt Out[434]: 0 -0.828876 1 -0.110383 2 2.357598 3 -1.620073 4 0.440903 ... 995 -1.177365 996 1.236988 997 0.743946 998 -0.533097 999 -0.140850 Name: A, Length: 1000, dtype: float64 Passing options to the compression protocol in order to speed up compression: In [435]: df.to_pickle("data.pkl.gz", compression={"method": "gzip", "compresslevel": 1}) msgpack# pandas support for msgpack has been removed in version 1.0.0. It is recommended to use pickle instead. Alternatively, you can also the Arrow IPC serialization format for on-the-wire transmission of pandas objects. For documentation on pyarrow, see here. HDF5 (PyTables)# HDFStore is a dict-like object which reads and writes pandas using the high performance HDF5 format using the excellent PyTables library. See the cookbook for some advanced strategies Warning pandas uses PyTables for reading and writing HDF5 files, which allows serializing object-dtype data with pickle. Loading pickled data received from untrusted sources can be unsafe. See: https://docs.python.org/3/library/pickle.html for more. In [436]: store = pd.HDFStore("store.h5") In [437]: print(store) <class 'pandas.io.pytables.HDFStore'> File path: store.h5 Objects can be written to the file just like adding key-value pairs to a dict: In [438]: index = pd.date_range("1/1/2000", periods=8) In [439]: s = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"]) In [440]: df = pd.DataFrame(np.random.randn(8, 3), index=index, columns=["A", "B", "C"]) # store.put('s', s) is an equivalent method In [441]: store["s"] = s In [442]: store["df"] = df In [443]: store Out[443]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 In a current or later Python session, you can retrieve stored objects: # store.get('df') is an equivalent method In [444]: store["df"] Out[444]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 # dotted (attribute) access provides get as well In [445]: store.df Out[445]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Deletion of the object specified by the key: # store.remove('df') is an equivalent method In [446]: del store["df"] In [447]: store Out[447]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 Closing a Store and using a context manager: In [448]: store.close() In [449]: store Out[449]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 In [450]: store.is_open Out[450]: False # Working with, and automatically closing the store using a context manager In [451]: with pd.HDFStore("store.h5") as store: .....: store.keys() .....: Read/write API# HDFStore supports a top-level API using read_hdf for reading and to_hdf for writing, similar to how read_csv and to_csv work. In [452]: df_tl = pd.DataFrame({"A": list(range(5)), "B": list(range(5))}) In [453]: df_tl.to_hdf("store_tl.h5", "table", append=True) In [454]: pd.read_hdf("store_tl.h5", "table", where=["index>2"]) Out[454]: A B 3 3 3 4 4 4 HDFStore will by default not drop rows that are all missing. This behavior can be changed by setting dropna=True. In [455]: df_with_missing = pd.DataFrame( .....: { .....: "col1": [0, np.nan, 2], .....: "col2": [1, np.nan, np.nan], .....: } .....: ) .....: In [456]: df_with_missing Out[456]: col1 col2 0 0.0 1.0 1 NaN NaN 2 2.0 NaN In [457]: df_with_missing.to_hdf("file.h5", "df_with_missing", format="table", mode="w") In [458]: pd.read_hdf("file.h5", "df_with_missing") Out[458]: col1 col2 0 0.0 1.0 1 NaN NaN 2 2.0 NaN In [459]: df_with_missing.to_hdf( .....: "file.h5", "df_with_missing", format="table", mode="w", dropna=True .....: ) .....: In [460]: pd.read_hdf("file.h5", "df_with_missing") Out[460]: col1 col2 0 0.0 1.0 2 2.0 NaN Fixed format# The examples above show storing using put, which write the HDF5 to PyTables in a fixed array format, called the fixed format. These types of stores are not appendable once written (though you can simply remove them and rewrite). Nor are they queryable; they must be retrieved in their entirety. They also do not support dataframes with non-unique column names. The fixed format stores offer very fast writing and slightly faster reading than table stores. This format is specified by default when using put or to_hdf or by format='fixed' or format='f'. Warning A fixed format will raise a TypeError if you try to retrieve using a where: >>> pd.DataFrame(np.random.randn(10, 2)).to_hdf("test_fixed.h5", "df") >>> pd.read_hdf("test_fixed.h5", "df", where="index>5") TypeError: cannot pass a where specification when reading a fixed format. this store must be selected in its entirety Table format# HDFStore supports another PyTables format on disk, the table format. Conceptually a table is shaped very much like a DataFrame, with rows and columns. A table may be appended to in the same or other sessions. In addition, delete and query type operations are supported. This format is specified by format='table' or format='t' to append or put or to_hdf. This format can be set as an option as well pd.set_option('io.hdf.default_format','table') to enable put/append/to_hdf to by default store in the table format. In [461]: store = pd.HDFStore("store.h5") In [462]: df1 = df[0:4] In [463]: df2 = df[4:] # append data (creates a table automatically) In [464]: store.append("df", df1) In [465]: store.append("df", df2) In [466]: store Out[466]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # select the entire object In [467]: store.select("df") Out[467]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 # the type of stored data In [468]: store.root.df._v_attrs.pandas_type Out[468]: 'frame_table' Note You can also create a table by passing format='table' or format='t' to a put operation. Hierarchical keys# Keys to a store can be specified as a string. These can be in a hierarchical path-name like format (e.g. foo/bar/bah), which will generate a hierarchy of sub-stores (or Groups in PyTables parlance). Keys can be specified without the leading ‘/’ and are always absolute (e.g. ‘foo’ refers to ‘/foo’). Removal operations can remove everything in the sub-store and below, so be careful. In [469]: store.put("foo/bar/bah", df) In [470]: store.append("food/orange", df) In [471]: store.append("food/apple", df) In [472]: store Out[472]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # a list of keys are returned In [473]: store.keys() Out[473]: ['/df', '/food/apple', '/food/orange', '/foo/bar/bah'] # remove all nodes under this level In [474]: store.remove("food") In [475]: store Out[475]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 You can walk through the group hierarchy using the walk method which will yield a tuple for each group key along with the relative keys of its contents. In [476]: for (path, subgroups, subkeys) in store.walk(): .....: for subgroup in subgroups: .....: print("GROUP: {}/{}".format(path, subgroup)) .....: for subkey in subkeys: .....: key = "/".join([path, subkey]) .....: print("KEY: {}".format(key)) .....: print(store.get(key)) .....: GROUP: /foo KEY: /df A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 GROUP: /foo/bar KEY: /foo/bar/bah A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Warning Hierarchical keys cannot be retrieved as dotted (attribute) access as described above for items stored under the root node. In [8]: store.foo.bar.bah AttributeError: 'HDFStore' object has no attribute 'foo' # you can directly access the actual PyTables node but using the root node In [9]: store.root.foo.bar.bah Out[9]: /foo/bar/bah (Group) '' children := ['block0_items' (Array), 'block0_values' (Array), 'axis0' (Array), 'axis1' (Array)] Instead, use explicit string based keys: In [477]: store["foo/bar/bah"] Out[477]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Storing types# Storing mixed types in a table# Storing mixed-dtype data is supported. Strings are stored as a fixed-width using the maximum size of the appended column. Subsequent attempts at appending longer strings will raise a ValueError. Passing min_itemsize={`values`: size} as a parameter to append will set a larger minimum for the string columns. Storing floats, strings, ints, bools, datetime64 are currently supported. For string columns, passing nan_rep = 'nan' to append will change the default nan representation on disk (which converts to/from np.nan), this defaults to nan. In [478]: df_mixed = pd.DataFrame( .....: { .....: "A": np.random.randn(8), .....: "B": np.random.randn(8), .....: "C": np.array(np.random.randn(8), dtype="float32"), .....: "string": "string", .....: "int": 1, .....: "bool": True, .....: "datetime64": pd.Timestamp("20010102"), .....: }, .....: index=list(range(8)), .....: ) .....: In [479]: df_mixed.loc[df_mixed.index[3:5], ["A", "B", "string", "datetime64"]] = np.nan In [480]: store.append("df_mixed", df_mixed, min_itemsize={"values": 50}) In [481]: df_mixed1 = store.select("df_mixed") In [482]: df_mixed1 Out[482]: A B C string int bool datetime64 0 1.778161 -0.898283 -0.263043 string 1 True 2001-01-02 1 -0.913867 -0.218499 -0.639244 string 1 True 2001-01-02 2 -0.030004 1.408028 -0.866305 string 1 True 2001-01-02 3 NaN NaN -0.225250 NaN 1 True NaT 4 NaN NaN -0.890978 NaN 1 True NaT 5 0.081323 0.520995 -0.553839 string 1 True 2001-01-02 6 -0.268494 0.620028 -2.762875 string 1 True 2001-01-02 7 0.168016 0.159416 -1.244763 string 1 True 2001-01-02 In [483]: df_mixed1.dtypes.value_counts() Out[483]: float64 2 float32 1 object 1 int64 1 bool 1 datetime64[ns] 1 dtype: int64 # we have provided a minimum string column size In [484]: store.root.df_mixed.table Out[484]: /df_mixed/table (Table(8,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(2,), dflt=0.0, pos=1), "values_block_1": Float32Col(shape=(1,), dflt=0.0, pos=2), "values_block_2": StringCol(itemsize=50, shape=(1,), dflt=b'', pos=3), "values_block_3": Int64Col(shape=(1,), dflt=0, pos=4), "values_block_4": BoolCol(shape=(1,), dflt=False, pos=5), "values_block_5": Int64Col(shape=(1,), dflt=0, pos=6)} byteorder := 'little' chunkshape := (689,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False} Storing MultiIndex DataFrames# Storing MultiIndex DataFrames as tables is very similar to storing/selecting from homogeneous index DataFrames. In [485]: index = pd.MultiIndex( .....: levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], .....: codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], .....: names=["foo", "bar"], .....: ) .....: In [486]: df_mi = pd.DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"]) In [487]: df_mi Out[487]: A B C foo bar foo one -1.280289 0.692545 -0.536722 two 1.005707 0.296917 0.139796 three -1.083889 0.811865 1.648435 bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 baz two 0.183573 0.145277 0.308146 three -1.043530 -0.708145 1.430905 qux one -0.850136 0.813949 1.508891 two -1.556154 0.187597 1.176488 three -1.246093 -0.002726 -0.444249 In [488]: store.append("df_mi", df_mi) In [489]: store.select("df_mi") Out[489]: A B C foo bar foo one -1.280289 0.692545 -0.536722 two 1.005707 0.296917 0.139796 three -1.083889 0.811865 1.648435 bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 baz two 0.183573 0.145277 0.308146 three -1.043530 -0.708145 1.430905 qux one -0.850136 0.813949 1.508891 two -1.556154 0.187597 1.176488 three -1.246093 -0.002726 -0.444249 # the levels are automatically included as data columns In [490]: store.select("df_mi", "foo=bar") Out[490]: A B C foo bar bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 Note The index keyword is reserved and cannot be use as a level name. Querying# Querying a table# select and delete operations have an optional criterion that can be specified to select/delete only a subset of the data. This allows one to have a very large on-disk table and retrieve only a portion of the data. A query is specified using the Term class under the hood, as a boolean expression. index and columns are supported indexers of DataFrames. if data_columns are specified, these can be used as additional indexers. level name in a MultiIndex, with default name level_0, level_1, … if not provided. Valid comparison operators are: =, ==, !=, >, >=, <, <= Valid boolean expressions are combined with: | : or & : and ( and ) : for grouping These rules are similar to how boolean expressions are used in pandas for indexing. Note = will be automatically expanded to the comparison operator == ~ is the not operator, but can only be used in very limited circumstances If a list/tuple of expressions is passed they will be combined via & The following are valid expressions: 'index >= date' "columns = ['A', 'D']" "columns in ['A', 'D']" 'columns = A' 'columns == A' "~(columns = ['A', 'B'])" 'index > df.index[3] & string = "bar"' '(index > df.index[3] & index <= df.index[6]) | string = "bar"' "ts >= Timestamp('2012-02-01')" "major_axis>=20130101" The indexers are on the left-hand side of the sub-expression: columns, major_axis, ts The right-hand side of the sub-expression (after a comparison operator) can be: functions that will be evaluated, e.g. Timestamp('2012-02-01') strings, e.g. "bar" date-like, e.g. 20130101, or "20130101" lists, e.g. "['A', 'B']" variables that are defined in the local names space, e.g. date Note Passing a string to a query by interpolating it into the query expression is not recommended. Simply assign the string of interest to a variable and use that variable in an expression. For example, do this string = "HolyMoly'" store.select("df", "index == string") instead of this string = "HolyMoly'" store.select('df', f'index == {string}') The latter will not work and will raise a SyntaxError.Note that there’s a single quote followed by a double quote in the string variable. If you must interpolate, use the '%r' format specifier store.select("df", "index == %r" % string) which will quote string. Here are some examples: In [491]: dfq = pd.DataFrame( .....: np.random.randn(10, 4), .....: columns=list("ABCD"), .....: index=pd.date_range("20130101", periods=10), .....: ) .....: In [492]: store.append("dfq", dfq, format="table", data_columns=True) Use boolean expressions, with in-line function evaluation. In [493]: store.select("dfq", "index>pd.Timestamp('20130104') & columns=['A', 'B']") Out[493]: A B 2013-01-05 1.366810 1.073372 2013-01-06 2.119746 -2.628174 2013-01-07 0.337920 -0.634027 2013-01-08 1.053434 1.109090 2013-01-09 -0.772942 -0.269415 2013-01-10 0.048562 -0.285920 Use inline column reference. In [494]: store.select("dfq", where="A>0 or C>0") Out[494]: A B C D 2013-01-01 0.856838 1.491776 0.001283 0.701816 2013-01-02 -1.097917 0.102588 0.661740 0.443531 2013-01-03 0.559313 -0.459055 -1.222598 -0.455304 2013-01-05 1.366810 1.073372 -0.994957 0.755314 2013-01-06 2.119746 -2.628174 -0.089460 -0.133636 2013-01-07 0.337920 -0.634027 0.421107 0.604303 2013-01-08 1.053434 1.109090 -0.367891 -0.846206 2013-01-10 0.048562 -0.285920 1.334100 0.194462 The columns keyword can be supplied to select a list of columns to be returned, this is equivalent to passing a 'columns=list_of_columns_to_filter': In [495]: store.select("df", "columns=['A', 'B']") Out[495]: A B 2000-01-01 -0.398501 -0.677311 2000-01-02 -1.167564 -0.593353 2000-01-03 -0.131959 0.089012 2000-01-04 0.169405 -1.358046 2000-01-05 0.492195 0.076693 2000-01-06 -0.285283 -1.210529 2000-01-07 0.941577 -0.342447 2000-01-08 0.052607 2.093214 start and stop parameters can be specified to limit the total search space. These are in terms of the total number of rows in a table. Note select will raise a ValueError if the query expression has an unknown variable reference. Usually this means that you are trying to select on a column that is not a data_column. select will raise a SyntaxError if the query expression is not valid. Query timedelta64[ns]# You can store and query using the timedelta64[ns] type. Terms can be specified in the format: <float>(<unit>), where float may be signed (and fractional), and unit can be D,s,ms,us,ns for the timedelta. Here’s an example: In [496]: from datetime import timedelta In [497]: dftd = pd.DataFrame( .....: { .....: "A": pd.Timestamp("20130101"), .....: "B": [ .....: pd.Timestamp("20130101") + timedelta(days=i, seconds=10) .....: for i in range(10) .....: ], .....: } .....: ) .....: In [498]: dftd["C"] = dftd["A"] - dftd["B"] In [499]: dftd Out[499]: A B C 0 2013-01-01 2013-01-01 00:00:10 -1 days +23:59:50 1 2013-01-01 2013-01-02 00:00:10 -2 days +23:59:50 2 2013-01-01 2013-01-03 00:00:10 -3 days +23:59:50 3 2013-01-01 2013-01-04 00:00:10 -4 days +23:59:50 4 2013-01-01 2013-01-05 00:00:10 -5 days +23:59:50 5 2013-01-01 2013-01-06 00:00:10 -6 days +23:59:50 6 2013-01-01 2013-01-07 00:00:10 -7 days +23:59:50 7 2013-01-01 2013-01-08 00:00:10 -8 days +23:59:50 8 2013-01-01 2013-01-09 00:00:10 -9 days +23:59:50 9 2013-01-01 2013-01-10 00:00:10 -10 days +23:59:50 In [500]: store.append("dftd", dftd, data_columns=True) In [501]: store.select("dftd", "C<'-3.5D'") Out[501]: A B C 4 2013-01-01 2013-01-05 00:00:10 -5 days +23:59:50 5 2013-01-01 2013-01-06 00:00:10 -6 days +23:59:50 6 2013-01-01 2013-01-07 00:00:10 -7 days +23:59:50 7 2013-01-01 2013-01-08 00:00:10 -8 days +23:59:50 8 2013-01-01 2013-01-09 00:00:10 -9 days +23:59:50 9 2013-01-01 2013-01-10 00:00:10 -10 days +23:59:50 Query MultiIndex# Selecting from a MultiIndex can be achieved by using the name of the level. In [502]: df_mi.index.names Out[502]: FrozenList(['foo', 'bar']) In [503]: store.select("df_mi", "foo=baz and bar=two") Out[503]: A B C foo bar baz two 0.183573 0.145277 0.308146 If the MultiIndex levels names are None, the levels are automatically made available via the level_n keyword with n the level of the MultiIndex you want to select from. In [504]: index = pd.MultiIndex( .....: levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], .....: codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], .....: ) .....: In [505]: df_mi_2 = pd.DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"]) In [506]: df_mi_2 Out[506]: A B C foo one -0.646538 1.210676 -0.315409 two 1.528366 0.376542 0.174490 three 1.247943 -0.742283 0.710400 bar one 0.434128 -1.246384 1.139595 two 1.388668 -0.413554 -0.666287 baz two 0.010150 -0.163820 -0.115305 three 0.216467 0.633720 0.473945 qux one -0.155446 1.287082 0.320201 two -1.256989 0.874920 0.765944 three 0.025557 -0.729782 -0.127439 In [507]: store.append("df_mi_2", df_mi_2) # the levels are automatically included as data columns with keyword level_n In [508]: store.select("df_mi_2", "level_0=foo and level_1=two") Out[508]: A B C foo two 1.528366 0.376542 0.17449 Indexing# You can create/modify an index for a table with create_table_index after data is already in the table (after and append/put operation). Creating a table index is highly encouraged. This will speed your queries a great deal when you use a select with the indexed dimension as the where. Note Indexes are automagically created on the indexables and any data columns you specify. This behavior can be turned off by passing index=False to append. # we have automagically already created an index (in the first section) In [509]: i = store.root.df.table.cols.index.index In [510]: i.optlevel, i.kind Out[510]: (6, 'medium') # change an index by passing new parameters In [511]: store.create_table_index("df", optlevel=9, kind="full") In [512]: i = store.root.df.table.cols.index.index In [513]: i.optlevel, i.kind Out[513]: (9, 'full') Oftentimes when appending large amounts of data to a store, it is useful to turn off index creation for each append, then recreate at the end. In [514]: df_1 = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [515]: df_2 = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [516]: st = pd.HDFStore("appends.h5", mode="w") In [517]: st.append("df", df_1, data_columns=["B"], index=False) In [518]: st.append("df", df_2, data_columns=["B"], index=False) In [519]: st.get_storer("df").table Out[519]: /df/table (Table(20,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2)} byteorder := 'little' chunkshape := (2730,) Then create the index when finished appending. In [520]: st.create_table_index("df", columns=["B"], optlevel=9, kind="full") In [521]: st.get_storer("df").table Out[521]: /df/table (Table(20,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2)} byteorder := 'little' chunkshape := (2730,) autoindex := True colindexes := { "B": Index(9, fullshuffle, zlib(1)).is_csi=True} In [522]: st.close() See here for how to create a completely-sorted-index (CSI) on an existing store. Query via data columns# You can designate (and index) certain columns that you want to be able to perform queries (other than the indexable columns, which you can always query). For instance say you want to perform this common operation, on-disk, and return just the frame that matches this query. You can specify data_columns = True to force all columns to be data_columns. In [523]: df_dc = df.copy() In [524]: df_dc["string"] = "foo" In [525]: df_dc.loc[df_dc.index[4:6], "string"] = np.nan In [526]: df_dc.loc[df_dc.index[7:9], "string"] = "bar" In [527]: df_dc["string2"] = "cool" In [528]: df_dc.loc[df_dc.index[1:3], ["B", "C"]] = 1.0 In [529]: df_dc Out[529]: A B C string string2 2000-01-01 -0.398501 -0.677311 -0.874991 foo cool 2000-01-02 -1.167564 1.000000 1.000000 foo cool 2000-01-03 -0.131959 1.000000 1.000000 foo cool 2000-01-04 0.169405 -1.358046 -0.105563 foo cool 2000-01-05 0.492195 0.076693 0.213685 NaN cool 2000-01-06 -0.285283 -1.210529 -1.408386 NaN cool 2000-01-07 0.941577 -0.342447 0.222031 foo cool 2000-01-08 0.052607 2.093214 1.064908 bar cool # on-disk operations In [530]: store.append("df_dc", df_dc, data_columns=["B", "C", "string", "string2"]) In [531]: store.select("df_dc", where="B > 0") Out[531]: A B C string string2 2000-01-02 -1.167564 1.000000 1.000000 foo cool 2000-01-03 -0.131959 1.000000 1.000000 foo cool 2000-01-05 0.492195 0.076693 0.213685 NaN cool 2000-01-08 0.052607 2.093214 1.064908 bar cool # getting creative In [532]: store.select("df_dc", "B > 0 & C > 0 & string == foo") Out[532]: A B C string string2 2000-01-02 -1.167564 1.0 1.0 foo cool 2000-01-03 -0.131959 1.0 1.0 foo cool # this is in-memory version of this type of selection In [533]: df_dc[(df_dc.B > 0) & (df_dc.C > 0) & (df_dc.string == "foo")] Out[533]: A B C string string2 2000-01-02 -1.167564 1.0 1.0 foo cool 2000-01-03 -0.131959 1.0 1.0 foo cool # we have automagically created this index and the B/C/string/string2 # columns are stored separately as ``PyTables`` columns In [534]: store.root.df_dc.table Out[534]: /df_dc/table (Table(8,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2), "C": Float64Col(shape=(), dflt=0.0, pos=3), "string": StringCol(itemsize=3, shape=(), dflt=b'', pos=4), "string2": StringCol(itemsize=4, shape=(), dflt=b'', pos=5)} byteorder := 'little' chunkshape := (1680,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False, "B": Index(6, mediumshuffle, zlib(1)).is_csi=False, "C": Index(6, mediumshuffle, zlib(1)).is_csi=False, "string": Index(6, mediumshuffle, zlib(1)).is_csi=False, "string2": Index(6, mediumshuffle, zlib(1)).is_csi=False} There is some performance degradation by making lots of columns into data columns, so it is up to the user to designate these. In addition, you cannot change data columns (nor indexables) after the first append/put operation (Of course you can simply read in the data and create a new table!). Iterator# You can pass iterator=True or chunksize=number_in_a_chunk to select and select_as_multiple to return an iterator on the results. The default is 50,000 rows returned in a chunk. In [535]: for df in store.select("df", chunksize=3): .....: print(df) .....: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 A B C 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 A B C 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Note You can also use the iterator with read_hdf which will open, then automatically close the store when finished iterating. for df in pd.read_hdf("store.h5", "df", chunksize=3): print(df) Note, that the chunksize keyword applies to the source rows. So if you are doing a query, then the chunksize will subdivide the total rows in the table and the query applied, returning an iterator on potentially unequal sized chunks. Here is a recipe for generating a query and using it to create equal sized return chunks. In [536]: dfeq = pd.DataFrame({"number": np.arange(1, 11)}) In [537]: dfeq Out[537]: number 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 In [538]: store.append("dfeq", dfeq, data_columns=["number"]) In [539]: def chunks(l, n): .....: return [l[i: i + n] for i in range(0, len(l), n)] .....: In [540]: evens = [2, 4, 6, 8, 10] In [541]: coordinates = store.select_as_coordinates("dfeq", "number=evens") In [542]: for c in chunks(coordinates, 2): .....: print(store.select("dfeq", where=c)) .....: number 1 2 3 4 number 5 6 7 8 number 9 10 Advanced queries# Select a single column# To retrieve a single indexable or data column, use the method select_column. This will, for example, enable you to get the index very quickly. These return a Series of the result, indexed by the row number. These do not currently accept the where selector. In [543]: store.select_column("df_dc", "index") Out[543]: 0 2000-01-01 1 2000-01-02 2 2000-01-03 3 2000-01-04 4 2000-01-05 5 2000-01-06 6 2000-01-07 7 2000-01-08 Name: index, dtype: datetime64[ns] In [544]: store.select_column("df_dc", "string") Out[544]: 0 foo 1 foo 2 foo 3 foo 4 NaN 5 NaN 6 foo 7 bar Name: string, dtype: object Selecting coordinates# Sometimes you want to get the coordinates (a.k.a the index locations) of your query. This returns an Int64Index of the resulting locations. These coordinates can also be passed to subsequent where operations. In [545]: df_coord = pd.DataFrame( .....: np.random.randn(1000, 2), index=pd.date_range("20000101", periods=1000) .....: ) .....: In [546]: store.append("df_coord", df_coord) In [547]: c = store.select_as_coordinates("df_coord", "index > 20020101") In [548]: c Out[548]: Int64Index([732, 733, 734, 735, 736, 737, 738, 739, 740, 741, ... 990, 991, 992, 993, 994, 995, 996, 997, 998, 999], dtype='int64', length=268) In [549]: store.select("df_coord", where=c) Out[549]: 0 1 2002-01-02 0.009035 0.921784 2002-01-03 -1.476563 -1.376375 2002-01-04 1.266731 2.173681 2002-01-05 0.147621 0.616468 2002-01-06 0.008611 2.136001 ... ... ... 2002-09-22 0.781169 -0.791687 2002-09-23 -0.764810 -2.000933 2002-09-24 -0.345662 0.393915 2002-09-25 -0.116661 0.834638 2002-09-26 -1.341780 0.686366 [268 rows x 2 columns] Selecting using a where mask# Sometime your query can involve creating a list of rows to select. Usually this mask would be a resulting index from an indexing operation. This example selects the months of a datetimeindex which are 5. In [550]: df_mask = pd.DataFrame( .....: np.random.randn(1000, 2), index=pd.date_range("20000101", periods=1000) .....: ) .....: In [551]: store.append("df_mask", df_mask) In [552]: c = store.select_column("df_mask", "index") In [553]: where = c[pd.DatetimeIndex(c).month == 5].index In [554]: store.select("df_mask", where=where) Out[554]: 0 1 2000-05-01 -0.386742 -0.977433 2000-05-02 -0.228819 0.471671 2000-05-03 0.337307 1.840494 2000-05-04 0.050249 0.307149 2000-05-05 -0.802947 -0.946730 ... ... ... 2002-05-27 1.605281 1.741415 2002-05-28 -0.804450 -0.715040 2002-05-29 -0.874851 0.037178 2002-05-30 -0.161167 -1.294944 2002-05-31 -0.258463 -0.731969 [93 rows x 2 columns] Storer object# If you want to inspect the stored object, retrieve via get_storer. You could use this programmatically to say get the number of rows in an object. In [555]: store.get_storer("df_dc").nrows Out[555]: 8 Multiple table queries# The methods append_to_multiple and select_as_multiple can perform appending/selecting from multiple tables at once. The idea is to have one table (call it the selector table) that you index most/all of the columns, and perform your queries. The other table(s) are data tables with an index matching the selector table’s index. You can then perform a very fast query on the selector table, yet get lots of data back. This method is similar to having a very wide table, but enables more efficient queries. The append_to_multiple method splits a given single DataFrame into multiple tables according to d, a dictionary that maps the table names to a list of ‘columns’ you want in that table. If None is used in place of a list, that table will have the remaining unspecified columns of the given DataFrame. The argument selector defines which table is the selector table (which you can make queries from). The argument dropna will drop rows from the input DataFrame to ensure tables are synchronized. This means that if a row for one of the tables being written to is entirely np.NaN, that row will be dropped from all tables. If dropna is False, THE USER IS RESPONSIBLE FOR SYNCHRONIZING THE TABLES. Remember that entirely np.Nan rows are not written to the HDFStore, so if you choose to call dropna=False, some tables may have more rows than others, and therefore select_as_multiple may not work or it may return unexpected results. In [556]: df_mt = pd.DataFrame( .....: np.random.randn(8, 6), .....: index=pd.date_range("1/1/2000", periods=8), .....: columns=["A", "B", "C", "D", "E", "F"], .....: ) .....: In [557]: df_mt["foo"] = "bar" In [558]: df_mt.loc[df_mt.index[1], ("A", "B")] = np.nan # you can also create the tables individually In [559]: store.append_to_multiple( .....: {"df1_mt": ["A", "B"], "df2_mt": None}, df_mt, selector="df1_mt" .....: ) .....: In [560]: store Out[560]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # individual tables were created In [561]: store.select("df1_mt") Out[561]: A B 2000-01-01 0.079529 -1.459471 2000-01-02 NaN NaN 2000-01-03 -0.423113 2.314361 2000-01-04 0.756744 -0.792372 2000-01-05 -0.184971 0.170852 2000-01-06 0.678830 0.633974 2000-01-07 0.034973 0.974369 2000-01-08 -2.110103 0.243062 In [562]: store.select("df2_mt") Out[562]: C D E F foo 2000-01-01 -0.596306 -0.910022 -1.057072 -0.864360 bar 2000-01-02 0.477849 0.283128 -2.045700 -0.338206 bar 2000-01-03 -0.033100 -0.965461 -0.001079 -0.351689 bar 2000-01-04 -0.513555 -1.484776 -0.796280 -0.182321 bar 2000-01-05 -0.872407 -1.751515 0.934334 0.938818 bar 2000-01-06 -1.398256 1.347142 -0.029520 0.082738 bar 2000-01-07 -0.755544 0.380786 -1.634116 1.293610 bar 2000-01-08 1.453064 0.500558 -0.574475 0.694324 bar # as a multiple In [563]: store.select_as_multiple( .....: ["df1_mt", "df2_mt"], .....: where=["A>0", "B>0"], .....: selector="df1_mt", .....: ) .....: Out[563]: A B C D E F foo 2000-01-06 0.678830 0.633974 -1.398256 1.347142 -0.029520 0.082738 bar 2000-01-07 0.034973 0.974369 -0.755544 0.380786 -1.634116 1.293610 bar Delete from a table# You can delete from a table selectively by specifying a where. In deleting rows, it is important to understand the PyTables deletes rows by erasing the rows, then moving the following data. Thus deleting can potentially be a very expensive operation depending on the orientation of your data. To get optimal performance, it’s worthwhile to have the dimension you are deleting be the first of the indexables. Data is ordered (on the disk) in terms of the indexables. Here’s a simple use case. You store panel-type data, with dates in the major_axis and ids in the minor_axis. The data is then interleaved like this: date_1 id_1 id_2 . id_n date_2 id_1 . id_n It should be clear that a delete operation on the major_axis will be fairly quick, as one chunk is removed, then the following data moved. On the other hand a delete operation on the minor_axis will be very expensive. In this case it would almost certainly be faster to rewrite the table using a where that selects all but the missing data. Warning Please note that HDF5 DOES NOT RECLAIM SPACE in the h5 files automatically. Thus, repeatedly deleting (or removing nodes) and adding again, WILL TEND TO INCREASE THE FILE SIZE. To repack and clean the file, use ptrepack. Notes & caveats# Compression# PyTables allows the stored data to be compressed. This applies to all kinds of stores, not just tables. Two parameters are used to control compression: complevel and complib. complevel specifies if and how hard data is to be compressed. complevel=0 and complevel=None disables compression and 0<complevel<10 enables compression. complib specifies which compression library to use. If nothing is specified the default library zlib is used. A compression library usually optimizes for either good compression rates or speed and the results will depend on the type of data. Which type of compression to choose depends on your specific needs and data. The list of supported compression libraries: zlib: The default compression library. A classic in terms of compression, achieves good compression rates but is somewhat slow. lzo: Fast compression and decompression. bzip2: Good compression rates. blosc: Fast compression and decompression. Support for alternative blosc compressors: blosc:blosclz This is the default compressor for blosc blosc:lz4: A compact, very popular and fast compressor. blosc:lz4hc: A tweaked version of LZ4, produces better compression ratios at the expense of speed. blosc:snappy: A popular compressor used in many places. blosc:zlib: A classic; somewhat slower than the previous ones, but achieving better compression ratios. blosc:zstd: An extremely well balanced codec; it provides the best compression ratios among the others above, and at reasonably fast speed. If complib is defined as something other than the listed libraries a ValueError exception is issued. Note If the library specified with the complib option is missing on your platform, compression defaults to zlib without further ado. Enable compression for all objects within the file: store_compressed = pd.HDFStore( "store_compressed.h5", complevel=9, complib="blosc:blosclz" ) Or on-the-fly compression (this only applies to tables) in stores where compression is not enabled: store.append("df", df, complib="zlib", complevel=5) ptrepack# PyTables offers better write performance when tables are compressed after they are written, as opposed to turning on compression at the very beginning. You can use the supplied PyTables utility ptrepack. In addition, ptrepack can change compression levels after the fact. ptrepack --chunkshape=auto --propindexes --complevel=9 --complib=blosc in.h5 out.h5 Furthermore ptrepack in.h5 out.h5 will repack the file to allow you to reuse previously deleted space. Alternatively, one can simply remove the file and write again, or use the copy method. Caveats# Warning HDFStore is not-threadsafe for writing. The underlying PyTables only supports concurrent reads (via threading or processes). If you need reading and writing at the same time, you need to serialize these operations in a single thread in a single process. You will corrupt your data otherwise. See the (GH2397) for more information. If you use locks to manage write access between multiple processes, you may want to use fsync() before releasing write locks. For convenience you can use store.flush(fsync=True) to do this for you. Once a table is created columns (DataFrame) are fixed; only exactly the same columns can be appended Be aware that timezones (e.g., pytz.timezone('US/Eastern')) are not necessarily equal across timezone versions. So if data is localized to a specific timezone in the HDFStore using one version of a timezone library and that data is updated with another version, the data will be converted to UTC since these timezones are not considered equal. Either use the same version of timezone library or use tz_convert with the updated timezone definition. Warning PyTables will show a NaturalNameWarning if a column name cannot be used as an attribute selector. Natural identifiers contain only letters, numbers, and underscores, and may not begin with a number. Other identifiers cannot be used in a where clause and are generally a bad idea. DataTypes# HDFStore will map an object dtype to the PyTables underlying dtype. This means the following types are known to work: Type Represents missing values floating : float64, float32, float16 np.nan integer : int64, int32, int8, uint64,uint32, uint8 boolean datetime64[ns] NaT timedelta64[ns] NaT categorical : see the section below object : strings np.nan unicode columns are not supported, and WILL FAIL. Categorical data# You can write data that contains category dtypes to a HDFStore. Queries work the same as if it was an object array. However, the category dtyped data is stored in a more efficient manner. In [564]: dfcat = pd.DataFrame( .....: {"A": pd.Series(list("aabbcdba")).astype("category"), "B": np.random.randn(8)} .....: ) .....: In [565]: dfcat Out[565]: A B 0 a -1.608059 1 a 0.851060 2 b -0.736931 3 b 0.003538 4 c -1.422611 5 d 2.060901 6 b 0.993899 7 a -1.371768 In [566]: dfcat.dtypes Out[566]: A category B float64 dtype: object In [567]: cstore = pd.HDFStore("cats.h5", mode="w") In [568]: cstore.append("dfcat", dfcat, format="table", data_columns=["A"]) In [569]: result = cstore.select("dfcat", where="A in ['b', 'c']") In [570]: result Out[570]: A B 2 b -0.736931 3 b 0.003538 4 c -1.422611 6 b 0.993899 In [571]: result.dtypes Out[571]: A category B float64 dtype: object String columns# min_itemsize The underlying implementation of HDFStore uses a fixed column width (itemsize) for string columns. A string column itemsize is calculated as the maximum of the length of data (for that column) that is passed to the HDFStore, in the first append. Subsequent appends, may introduce a string for a column larger than the column can hold, an Exception will be raised (otherwise you could have a silent truncation of these columns, leading to loss of information). In the future we may relax this and allow a user-specified truncation to occur. Pass min_itemsize on the first table creation to a-priori specify the minimum length of a particular string column. min_itemsize can be an integer, or a dict mapping a column name to an integer. You can pass values as a key to allow all indexables or data_columns to have this min_itemsize. Passing a min_itemsize dict will cause all passed columns to be created as data_columns automatically. Note If you are not passing any data_columns, then the min_itemsize will be the maximum of the length of any string passed In [572]: dfs = pd.DataFrame({"A": "foo", "B": "bar"}, index=list(range(5))) In [573]: dfs Out[573]: A B 0 foo bar 1 foo bar 2 foo bar 3 foo bar 4 foo bar # A and B have a size of 30 In [574]: store.append("dfs", dfs, min_itemsize=30) In [575]: store.get_storer("dfs").table Out[575]: /dfs/table (Table(5,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": StringCol(itemsize=30, shape=(2,), dflt=b'', pos=1)} byteorder := 'little' chunkshape := (963,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False} # A is created as a data_column with a size of 30 # B is size is calculated In [576]: store.append("dfs2", dfs, min_itemsize={"A": 30}) In [577]: store.get_storer("dfs2").table Out[577]: /dfs2/table (Table(5,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": StringCol(itemsize=3, shape=(1,), dflt=b'', pos=1), "A": StringCol(itemsize=30, shape=(), dflt=b'', pos=2)} byteorder := 'little' chunkshape := (1598,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False, "A": Index(6, mediumshuffle, zlib(1)).is_csi=False} nan_rep String columns will serialize a np.nan (a missing value) with the nan_rep string representation. This defaults to the string value nan. You could inadvertently turn an actual nan value into a missing value. In [578]: dfss = pd.DataFrame({"A": ["foo", "bar", "nan"]}) In [579]: dfss Out[579]: A 0 foo 1 bar 2 nan In [580]: store.append("dfss", dfss) In [581]: store.select("dfss") Out[581]: A 0 foo 1 bar 2 NaN # here you need to specify a different nan rep In [582]: store.append("dfss2", dfss, nan_rep="_nan_") In [583]: store.select("dfss2") Out[583]: A 0 foo 1 bar 2 nan External compatibility# HDFStore writes table format objects in specific formats suitable for producing loss-less round trips to pandas objects. For external compatibility, HDFStore can read native PyTables format tables. It is possible to write an HDFStore object that can easily be imported into R using the rhdf5 library (Package website). Create a table format store like this: In [584]: df_for_r = pd.DataFrame( .....: { .....: "first": np.random.rand(100), .....: "second": np.random.rand(100), .....: "class": np.random.randint(0, 2, (100,)), .....: }, .....: index=range(100), .....: ) .....: In [585]: df_for_r.head() Out[585]: first second class 0 0.013480 0.504941 0 1 0.690984 0.898188 1 2 0.510113 0.618748 1 3 0.357698 0.004972 0 4 0.451658 0.012065 1 In [586]: store_export = pd.HDFStore("export.h5") In [587]: store_export.append("df_for_r", df_for_r, data_columns=df_dc.columns) In [588]: store_export Out[588]: <class 'pandas.io.pytables.HDFStore'> File path: export.h5 In R this file can be read into a data.frame object using the rhdf5 library. The following example function reads the corresponding column names and data values from the values and assembles them into a data.frame: # Load values and column names for all datasets from corresponding nodes and # insert them into one data.frame object. library(rhdf5) loadhdf5data <- function(h5File) { listing <- h5ls(h5File) # Find all data nodes, values are stored in *_values and corresponding column # titles in *_items data_nodes <- grep("_values", listing$name) name_nodes <- grep("_items", listing$name) data_paths = paste(listing$group[data_nodes], listing$name[data_nodes], sep = "/") name_paths = paste(listing$group[name_nodes], listing$name[name_nodes], sep = "/") columns = list() for (idx in seq(data_paths)) { # NOTE: matrices returned by h5read have to be transposed to obtain # required Fortran order! data <- data.frame(t(h5read(h5File, data_paths[idx]))) names <- t(h5read(h5File, name_paths[idx])) entry <- data.frame(data) colnames(entry) <- names columns <- append(columns, entry) } data <- data.frame(columns) return(data) } Now you can import the DataFrame into R: > data = loadhdf5data("transfer.hdf5") > head(data) first second class 1 0.4170220047 0.3266449 0 2 0.7203244934 0.5270581 0 3 0.0001143748 0.8859421 1 4 0.3023325726 0.3572698 1 5 0.1467558908 0.9085352 1 6 0.0923385948 0.6233601 1 Note The R function lists the entire HDF5 file’s contents and assembles the data.frame object from all matching nodes, so use this only as a starting point if you have stored multiple DataFrame objects to a single HDF5 file. Performance# tables format come with a writing performance penalty as compared to fixed stores. The benefit is the ability to append/delete and query (potentially very large amounts of data). Write times are generally longer as compared with regular stores. Query times can be quite fast, especially on an indexed axis. You can pass chunksize=<int> to append, specifying the write chunksize (default is 50000). This will significantly lower your memory usage on writing. You can pass expectedrows=<int> to the first append, to set the TOTAL number of rows that PyTables will expect. This will optimize read/write performance. Duplicate rows can be written to tables, but are filtered out in selection (with the last items being selected; thus a table is unique on major, minor pairs) A PerformanceWarning will be raised if you are attempting to store types that will be pickled by PyTables (rather than stored as endemic types). See Here for more information and some solutions. Feather# Feather provides binary columnar serialization for data frames. It is designed to make reading and writing data frames efficient, and to make sharing data across data analysis languages easy. Feather is designed to faithfully serialize and de-serialize DataFrames, supporting all of the pandas dtypes, including extension dtypes such as categorical and datetime with tz. Several caveats: The format will NOT write an Index, or MultiIndex for the DataFrame and will raise an error if a non-default one is provided. You can .reset_index() to store the index or .reset_index(drop=True) to ignore it. Duplicate column names and non-string columns names are not supported Actual Python objects in object dtype columns are not supported. These will raise a helpful error message on an attempt at serialization. See the Full Documentation. In [589]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(3, 6).astype("u1"), .....: "d": np.arange(4.0, 7.0, dtype="float64"), .....: "e": [True, False, True], .....: "f": pd.Categorical(list("abc")), .....: "g": pd.date_range("20130101", periods=3), .....: "h": pd.date_range("20130101", periods=3, tz="US/Eastern"), .....: "i": pd.date_range("20130101", periods=3, freq="ns"), .....: } .....: ) .....: In [590]: df Out[590]: a b c ... g h i 0 a 1 3 ... 2013-01-01 2013-01-01 00:00:00-05:00 2013-01-01 00:00:00.000000000 1 b 2 4 ... 2013-01-02 2013-01-02 00:00:00-05:00 2013-01-01 00:00:00.000000001 2 c 3 5 ... 2013-01-03 2013-01-03 00:00:00-05:00 2013-01-01 00:00:00.000000002 [3 rows x 9 columns] In [591]: df.dtypes Out[591]: a object b int64 c uint8 d float64 e bool f category g datetime64[ns] h datetime64[ns, US/Eastern] i datetime64[ns] dtype: object Write to a feather file. In [592]: df.to_feather("example.feather") Read from a feather file. In [593]: result = pd.read_feather("example.feather") In [594]: result Out[594]: a b c ... g h i 0 a 1 3 ... 2013-01-01 2013-01-01 00:00:00-05:00 2013-01-01 00:00:00.000000000 1 b 2 4 ... 2013-01-02 2013-01-02 00:00:00-05:00 2013-01-01 00:00:00.000000001 2 c 3 5 ... 2013-01-03 2013-01-03 00:00:00-05:00 2013-01-01 00:00:00.000000002 [3 rows x 9 columns] # we preserve dtypes In [595]: result.dtypes Out[595]: a object b int64 c uint8 d float64 e bool f category g datetime64[ns] h datetime64[ns, US/Eastern] i datetime64[ns] dtype: object Parquet# Apache Parquet provides a partitioned binary columnar serialization for data frames. It is designed to make reading and writing data frames efficient, and to make sharing data across data analysis languages easy. Parquet can use a variety of compression techniques to shrink the file size as much as possible while still maintaining good read performance. Parquet is designed to faithfully serialize and de-serialize DataFrame s, supporting all of the pandas dtypes, including extension dtypes such as datetime with tz. Several caveats. Duplicate column names and non-string columns names are not supported. The pyarrow engine always writes the index to the output, but fastparquet only writes non-default indexes. This extra column can cause problems for non-pandas consumers that are not expecting it. You can force including or omitting indexes with the index argument, regardless of the underlying engine. Index level names, if specified, must be strings. In the pyarrow engine, categorical dtypes for non-string types can be serialized to parquet, but will de-serialize as their primitive dtype. The pyarrow engine preserves the ordered flag of categorical dtypes with string types. fastparquet does not preserve the ordered flag. Non supported types include Interval and actual Python object types. These will raise a helpful error message on an attempt at serialization. Period type is supported with pyarrow >= 0.16.0. The pyarrow engine preserves extension data types such as the nullable integer and string data type (requiring pyarrow >= 0.16.0, and requiring the extension type to implement the needed protocols, see the extension types documentation). You can specify an engine to direct the serialization. This can be one of pyarrow, or fastparquet, or auto. If the engine is NOT specified, then the pd.options.io.parquet.engine option is checked; if this is also auto, then pyarrow is tried, and falling back to fastparquet. See the documentation for pyarrow and fastparquet. Note These engines are very similar and should read/write nearly identical parquet format files. pyarrow>=8.0.0 supports timedelta data, fastparquet>=0.1.4 supports timezone aware datetimes. These libraries differ by having different underlying dependencies (fastparquet by using numba, while pyarrow uses a c-library). In [596]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(3, 6).astype("u1"), .....: "d": np.arange(4.0, 7.0, dtype="float64"), .....: "e": [True, False, True], .....: "f": pd.date_range("20130101", periods=3), .....: "g": pd.date_range("20130101", periods=3, tz="US/Eastern"), .....: "h": pd.Categorical(list("abc")), .....: "i": pd.Categorical(list("abc"), ordered=True), .....: } .....: ) .....: In [597]: df Out[597]: a b c d e f g h i 0 a 1 3 4.0 True 2013-01-01 2013-01-01 00:00:00-05:00 a a 1 b 2 4 5.0 False 2013-01-02 2013-01-02 00:00:00-05:00 b b 2 c 3 5 6.0 True 2013-01-03 2013-01-03 00:00:00-05:00 c c In [598]: df.dtypes Out[598]: a object b int64 c uint8 d float64 e bool f datetime64[ns] g datetime64[ns, US/Eastern] h category i category dtype: object Write to a parquet file. In [599]: df.to_parquet("example_pa.parquet", engine="pyarrow") In [600]: df.to_parquet("example_fp.parquet", engine="fastparquet") Read from a parquet file. In [601]: result = pd.read_parquet("example_fp.parquet", engine="fastparquet") In [602]: result = pd.read_parquet("example_pa.parquet", engine="pyarrow") In [603]: result.dtypes Out[603]: a object b int64 c uint8 d float64 e bool f datetime64[ns] g datetime64[ns, US/Eastern] h category i category dtype: object Read only certain columns of a parquet file. In [604]: result = pd.read_parquet( .....: "example_fp.parquet", .....: engine="fastparquet", .....: columns=["a", "b"], .....: ) .....: In [605]: result = pd.read_parquet( .....: "example_pa.parquet", .....: engine="pyarrow", .....: columns=["a", "b"], .....: ) .....: In [606]: result.dtypes Out[606]: a object b int64 dtype: object Handling indexes# Serializing a DataFrame to parquet may include the implicit index as one or more columns in the output file. Thus, this code: In [607]: df = pd.DataFrame({"a": [1, 2], "b": [3, 4]}) In [608]: df.to_parquet("test.parquet", engine="pyarrow") creates a parquet file with three columns if you use pyarrow for serialization: a, b, and __index_level_0__. If you’re using fastparquet, the index may or may not be written to the file. This unexpected extra column causes some databases like Amazon Redshift to reject the file, because that column doesn’t exist in the target table. If you want to omit a dataframe’s indexes when writing, pass index=False to to_parquet(): In [609]: df.to_parquet("test.parquet", index=False) This creates a parquet file with just the two expected columns, a and b. If your DataFrame has a custom index, you won’t get it back when you load this file into a DataFrame. Passing index=True will always write the index, even if that’s not the underlying engine’s default behavior. Partitioning Parquet files# Parquet supports partitioning of data based on the values of one or more columns. In [610]: df = pd.DataFrame({"a": [0, 0, 1, 1], "b": [0, 1, 0, 1]}) In [611]: df.to_parquet(path="test", engine="pyarrow", partition_cols=["a"], compression=None) The path specifies the parent directory to which data will be saved. The partition_cols are the column names by which the dataset will be partitioned. Columns are partitioned in the order they are given. The partition splits are determined by the unique values in the partition columns. The above example creates a partitioned dataset that may look like: test ├── a=0 │ ├── 0bac803e32dc42ae83fddfd029cbdebc.parquet │ └── ... └── a=1 ├── e6ab24a4f45147b49b54a662f0c412a3.parquet └── ... ORC# New in version 1.0.0. Similar to the parquet format, the ORC Format is a binary columnar serialization for data frames. It is designed to make reading data frames efficient. pandas provides both the reader and the writer for the ORC format, read_orc() and to_orc(). This requires the pyarrow library. Warning It is highly recommended to install pyarrow using conda due to some issues occurred by pyarrow. to_orc() requires pyarrow>=7.0.0. read_orc() and to_orc() are not supported on Windows yet, you can find valid environments on install optional dependencies. For supported dtypes please refer to supported ORC features in Arrow. Currently timezones in datetime columns are not preserved when a dataframe is converted into ORC files. In [612]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(4.0, 7.0, dtype="float64"), .....: "d": [True, False, True], .....: "e": pd.date_range("20130101", periods=3), .....: } .....: ) .....: In [613]: df Out[613]: a b c d e 0 a 1 4.0 True 2013-01-01 1 b 2 5.0 False 2013-01-02 2 c 3 6.0 True 2013-01-03 In [614]: df.dtypes Out[614]: a object b int64 c float64 d bool e datetime64[ns] dtype: object Write to an orc file. In [615]: df.to_orc("example_pa.orc", engine="pyarrow") Read from an orc file. In [616]: result = pd.read_orc("example_pa.orc") In [617]: result.dtypes Out[617]: a object b int64 c float64 d bool e datetime64[ns] dtype: object Read only certain columns of an orc file. In [618]: result = pd.read_orc( .....: "example_pa.orc", .....: columns=["a", "b"], .....: ) .....: In [619]: result.dtypes Out[619]: a object b int64 dtype: object SQL queries# The pandas.io.sql module provides a collection of query wrappers to both facilitate data retrieval and to reduce dependency on DB-specific API. Database abstraction is provided by SQLAlchemy if installed. In addition you will need a driver library for your database. Examples of such drivers are psycopg2 for PostgreSQL or pymysql for MySQL. For SQLite this is included in Python’s standard library by default. You can find an overview of supported drivers for each SQL dialect in the SQLAlchemy docs. If SQLAlchemy is not installed, a fallback is only provided for sqlite (and for mysql for backwards compatibility, but this is deprecated and will be removed in a future version). This mode requires a Python database adapter which respect the Python DB-API. See also some cookbook examples for some advanced strategies. The key functions are: read_sql_table(table_name, con[, schema, ...]) Read SQL database table into a DataFrame. read_sql_query(sql, con[, index_col, ...]) Read SQL query into a DataFrame. read_sql(sql, con[, index_col, ...]) Read SQL query or database table into a DataFrame. DataFrame.to_sql(name, con[, schema, ...]) Write records stored in a DataFrame to a SQL database. Note The function read_sql() is a convenience wrapper around read_sql_table() and read_sql_query() (and for backward compatibility) and will delegate to specific function depending on the provided input (database table name or sql query). Table names do not need to be quoted if they have special characters. In the following example, we use the SQlite SQL database engine. You can use a temporary SQLite database where data are stored in “memory”. To connect with SQLAlchemy you use the create_engine() function to create an engine object from database URI. You only need to create the engine once per database you are connecting to. For more information on create_engine() and the URI formatting, see the examples below and the SQLAlchemy documentation In [620]: from sqlalchemy import create_engine # Create your engine. In [621]: engine = create_engine("sqlite:///:memory:") If you want to manage your own connections you can pass one of those instead. The example below opens a connection to the database using a Python context manager that automatically closes the connection after the block has completed. See the SQLAlchemy docs for an explanation of how the database connection is handled. with engine.connect() as conn, conn.begin(): data = pd.read_sql_table("data", conn) Warning When you open a connection to a database you are also responsible for closing it. Side effects of leaving a connection open may include locking the database or other breaking behaviour. Writing DataFrames# Assuming the following data is in a DataFrame data, we can insert it into the database using to_sql(). id Date Col_1 Col_2 Col_3 26 2012-10-18 X 25.7 True 42 2012-10-19 Y -12.4 False 63 2012-10-20 Z 5.73 True In [622]: import datetime In [623]: c = ["id", "Date", "Col_1", "Col_2", "Col_3"] In [624]: d = [ .....: (26, datetime.datetime(2010, 10, 18), "X", 27.5, True), .....: (42, datetime.datetime(2010, 10, 19), "Y", -12.5, False), .....: (63, datetime.datetime(2010, 10, 20), "Z", 5.73, True), .....: ] .....: In [625]: data = pd.DataFrame(d, columns=c) In [626]: data Out[626]: id Date Col_1 Col_2 Col_3 0 26 2010-10-18 X 27.50 True 1 42 2010-10-19 Y -12.50 False 2 63 2010-10-20 Z 5.73 True In [627]: data.to_sql("data", engine) Out[627]: 3 With some databases, writing large DataFrames can result in errors due to packet size limitations being exceeded. This can be avoided by setting the chunksize parameter when calling to_sql. For example, the following writes data to the database in batches of 1000 rows at a time: In [628]: data.to_sql("data_chunked", engine, chunksize=1000) Out[628]: 3 SQL data types# to_sql() will try to map your data to an appropriate SQL data type based on the dtype of the data. When you have columns of dtype object, pandas will try to infer the data type. You can always override the default type by specifying the desired SQL type of any of the columns by using the dtype argument. This argument needs a dictionary mapping column names to SQLAlchemy types (or strings for the sqlite3 fallback mode). For example, specifying to use the sqlalchemy String type instead of the default Text type for string columns: In [629]: from sqlalchemy.types import String In [630]: data.to_sql("data_dtype", engine, dtype={"Col_1": String}) Out[630]: 3 Note Due to the limited support for timedelta’s in the different database flavors, columns with type timedelta64 will be written as integer values as nanoseconds to the database and a warning will be raised. Note Columns of category dtype will be converted to the dense representation as you would get with np.asarray(categorical) (e.g. for string categories this gives an array of strings). Because of this, reading the database table back in does not generate a categorical. Datetime data types# Using SQLAlchemy, to_sql() is capable of writing datetime data that is timezone naive or timezone aware. However, the resulting data stored in the database ultimately depends on the supported data type for datetime data of the database system being used. The following table lists supported data types for datetime data for some common databases. Other database dialects may have different data types for datetime data. Database SQL Datetime Types Timezone Support SQLite TEXT No MySQL TIMESTAMP or DATETIME No PostgreSQL TIMESTAMP or TIMESTAMP WITH TIME ZONE Yes When writing timezone aware data to databases that do not support timezones, the data will be written as timezone naive timestamps that are in local time with respect to the timezone. read_sql_table() is also capable of reading datetime data that is timezone aware or naive. When reading TIMESTAMP WITH TIME ZONE types, pandas will convert the data to UTC. Insertion method# The parameter method controls the SQL insertion clause used. Possible values are: None: Uses standard SQL INSERT clause (one per row). 'multi': Pass multiple values in a single INSERT clause. It uses a special SQL syntax not supported by all backends. This usually provides better performance for analytic databases like Presto and Redshift, but has worse performance for traditional SQL backend if the table contains many columns. For more information check the SQLAlchemy documentation. callable with signature (pd_table, conn, keys, data_iter): This can be used to implement a more performant insertion method based on specific backend dialect features. Example of a callable using PostgreSQL COPY clause: # Alternative to_sql() *method* for DBs that support COPY FROM import csv from io import StringIO def psql_insert_copy(table, conn, keys, data_iter): """ Execute SQL statement inserting data Parameters ---------- table : pandas.io.sql.SQLTable conn : sqlalchemy.engine.Engine or sqlalchemy.engine.Connection keys : list of str Column names data_iter : Iterable that iterates the values to be inserted """ # gets a DBAPI connection that can provide a cursor dbapi_conn = conn.connection with dbapi_conn.cursor() as cur: s_buf = StringIO() writer = csv.writer(s_buf) writer.writerows(data_iter) s_buf.seek(0) columns = ', '.join(['"{}"'.format(k) for k in keys]) if table.schema: table_name = '{}.{}'.format(table.schema, table.name) else: table_name = table.name sql = 'COPY {} ({}) FROM STDIN WITH CSV'.format( table_name, columns) cur.copy_expert(sql=sql, file=s_buf) Reading tables# read_sql_table() will read a database table given the table name and optionally a subset of columns to read. Note In order to use read_sql_table(), you must have the SQLAlchemy optional dependency installed. In [631]: pd.read_sql_table("data", engine) Out[631]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 X 27.50 True 1 1 42 2010-10-19 Y -12.50 False 2 2 63 2010-10-20 Z 5.73 True Note Note that pandas infers column dtypes from query outputs, and not by looking up data types in the physical database schema. For example, assume userid is an integer column in a table. Then, intuitively, select userid ... will return integer-valued series, while select cast(userid as text) ... will return object-valued (str) series. Accordingly, if the query output is empty, then all resulting columns will be returned as object-valued (since they are most general). If you foresee that your query will sometimes generate an empty result, you may want to explicitly typecast afterwards to ensure dtype integrity. You can also specify the name of the column as the DataFrame index, and specify a subset of columns to be read. In [632]: pd.read_sql_table("data", engine, index_col="id") Out[632]: index Date Col_1 Col_2 Col_3 id 26 0 2010-10-18 X 27.50 True 42 1 2010-10-19 Y -12.50 False 63 2 2010-10-20 Z 5.73 True In [633]: pd.read_sql_table("data", engine, columns=["Col_1", "Col_2"]) Out[633]: Col_1 Col_2 0 X 27.50 1 Y -12.50 2 Z 5.73 And you can explicitly force columns to be parsed as dates: In [634]: pd.read_sql_table("data", engine, parse_dates=["Date"]) Out[634]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 X 27.50 True 1 1 42 2010-10-19 Y -12.50 False 2 2 63 2010-10-20 Z 5.73 True If needed you can explicitly specify a format string, or a dict of arguments to pass to pandas.to_datetime(): pd.read_sql_table("data", engine, parse_dates={"Date": "%Y-%m-%d"}) pd.read_sql_table( "data", engine, parse_dates={"Date": {"format": "%Y-%m-%d %H:%M:%S"}}, ) You can check if a table exists using has_table() Schema support# Reading from and writing to different schema’s is supported through the schema keyword in the read_sql_table() and to_sql() functions. Note however that this depends on the database flavor (sqlite does not have schema’s). For example: df.to_sql("table", engine, schema="other_schema") pd.read_sql_table("table", engine, schema="other_schema") Querying# You can query using raw SQL in the read_sql_query() function. In this case you must use the SQL variant appropriate for your database. When using SQLAlchemy, you can also pass SQLAlchemy Expression language constructs, which are database-agnostic. In [635]: pd.read_sql_query("SELECT * FROM data", engine) Out[635]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 00:00:00.000000 X 27.50 1 1 1 42 2010-10-19 00:00:00.000000 Y -12.50 0 2 2 63 2010-10-20 00:00:00.000000 Z 5.73 1 Of course, you can specify a more “complex” query. In [636]: pd.read_sql_query("SELECT id, Col_1, Col_2 FROM data WHERE id = 42;", engine) Out[636]: id Col_1 Col_2 0 42 Y -12.5 The read_sql_query() function supports a chunksize argument. Specifying this will return an iterator through chunks of the query result: In [637]: df = pd.DataFrame(np.random.randn(20, 3), columns=list("abc")) In [638]: df.to_sql("data_chunks", engine, index=False) Out[638]: 20 In [639]: for chunk in pd.read_sql_query("SELECT * FROM data_chunks", engine, chunksize=5): .....: print(chunk) .....: a b c 0 0.070470 0.901320 0.937577 1 0.295770 1.420548 -0.005283 2 -1.518598 -0.730065 0.226497 3 -2.061465 0.632115 0.853619 4 2.719155 0.139018 0.214557 a b c 0 -1.538924 -0.366973 -0.748801 1 -0.478137 -1.559153 -3.097759 2 -2.320335 -0.221090 0.119763 3 0.608228 1.064810 -0.780506 4 -2.736887 0.143539 1.170191 a b c 0 -1.573076 0.075792 -1.722223 1 -0.774650 0.803627 0.221665 2 0.584637 0.147264 1.057825 3 -0.284136 0.912395 1.552808 4 0.189376 -0.109830 0.539341 a b c 0 0.592591 -0.155407 -1.356475 1 0.833837 1.524249 1.606722 2 -0.029487 -0.051359 1.700152 3 0.921484 -0.926347 0.979818 4 0.182380 -0.186376 0.049820 You can also run a plain query without creating a DataFrame with execute(). This is useful for queries that don’t return values, such as INSERT. This is functionally equivalent to calling execute on the SQLAlchemy engine or db connection object. Again, you must use the SQL syntax variant appropriate for your database. from pandas.io import sql sql.execute("SELECT * FROM table_name", engine) sql.execute( "INSERT INTO table_name VALUES(?, ?, ?)", engine, params=[("id", 1, 12.2, True)] ) Engine connection examples# To connect with SQLAlchemy you use the create_engine() function to create an engine object from database URI. You only need to create the engine once per database you are connecting to. from sqlalchemy import create_engine engine = create_engine("postgresql://scott:[email protected]:5432/mydatabase") engine = create_engine("mysql+mysqldb://scott:[email protected]/foo") engine = create_engine("oracle://scott:[email protected]:1521/sidname") engine = create_engine("mssql+pyodbc://mydsn") # sqlite://<nohostname>/<path> # where <path> is relative: engine = create_engine("sqlite:///foo.db") # or absolute, starting with a slash: engine = create_engine("sqlite:////absolute/path/to/foo.db") For more information see the examples the SQLAlchemy documentation Advanced SQLAlchemy queries# You can use SQLAlchemy constructs to describe your query. Use sqlalchemy.text() to specify query parameters in a backend-neutral way In [640]: import sqlalchemy as sa In [641]: pd.read_sql( .....: sa.text("SELECT * FROM data where Col_1=:col1"), engine, params={"col1": "X"} .....: ) .....: Out[641]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 00:00:00.000000 X 27.5 1 If you have an SQLAlchemy description of your database you can express where conditions using SQLAlchemy expressions In [642]: metadata = sa.MetaData() In [643]: data_table = sa.Table( .....: "data", .....: metadata, .....: sa.Column("index", sa.Integer), .....: sa.Column("Date", sa.DateTime), .....: sa.Column("Col_1", sa.String), .....: sa.Column("Col_2", sa.Float), .....: sa.Column("Col_3", sa.Boolean), .....: ) .....: In [644]: pd.read_sql(sa.select([data_table]).where(data_table.c.Col_3 is True), engine) Out[644]: Empty DataFrame Columns: [index, Date, Col_1, Col_2, Col_3] Index: [] You can combine SQLAlchemy expressions with parameters passed to read_sql() using sqlalchemy.bindparam() In [645]: import datetime as dt In [646]: expr = sa.select([data_table]).where(data_table.c.Date > sa.bindparam("date")) In [647]: pd.read_sql(expr, engine, params={"date": dt.datetime(2010, 10, 18)}) Out[647]: index Date Col_1 Col_2 Col_3 0 1 2010-10-19 Y -12.50 False 1 2 2010-10-20 Z 5.73 True Sqlite fallback# The use of sqlite is supported without using SQLAlchemy. This mode requires a Python database adapter which respect the Python DB-API. You can create connections like so: import sqlite3 con = sqlite3.connect(":memory:") And then issue the following queries: data.to_sql("data", con) pd.read_sql_query("SELECT * FROM data", con) Google BigQuery# Warning Starting in 0.20.0, pandas has split off Google BigQuery support into the separate package pandas-gbq. You can pip install pandas-gbq to get it. The pandas-gbq package provides functionality to read/write from Google BigQuery. pandas integrates with this external package. if pandas-gbq is installed, you can use the pandas methods pd.read_gbq and DataFrame.to_gbq, which will call the respective functions from pandas-gbq. Full documentation can be found here. Stata format# Writing to stata format# The method to_stata() will write a DataFrame into a .dta file. The format version of this file is always 115 (Stata 12). In [648]: df = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [649]: df.to_stata("stata.dta") Stata data files have limited data type support; only strings with 244 or fewer characters, int8, int16, int32, float32 and float64 can be stored in .dta files. Additionally, Stata reserves certain values to represent missing data. Exporting a non-missing value that is outside of the permitted range in Stata for a particular data type will retype the variable to the next larger size. For example, int8 values are restricted to lie between -127 and 100 in Stata, and so variables with values above 100 will trigger a conversion to int16. nan values in floating points data types are stored as the basic missing data type (. in Stata). Note It is not possible to export missing data values for integer data types. The Stata writer gracefully handles other data types including int64, bool, uint8, uint16, uint32 by casting to the smallest supported type that can represent the data. For example, data with a type of uint8 will be cast to int8 if all values are less than 100 (the upper bound for non-missing int8 data in Stata), or, if values are outside of this range, the variable is cast to int16. Warning Conversion from int64 to float64 may result in a loss of precision if int64 values are larger than 2**53. Warning StataWriter and to_stata() only support fixed width strings containing up to 244 characters, a limitation imposed by the version 115 dta file format. Attempting to write Stata dta files with strings longer than 244 characters raises a ValueError. Reading from Stata format# The top-level function read_stata will read a dta file and return either a DataFrame or a StataReader that can be used to read the file incrementally. In [650]: pd.read_stata("stata.dta") Out[650]: index A B 0 0 -1.690072 0.405144 1 1 -1.511309 -1.531396 2 2 0.572698 -1.106845 3 3 -1.185859 0.174564 4 4 0.603797 -1.796129 5 5 -0.791679 1.173795 6 6 -0.277710 1.859988 7 7 -0.258413 1.251808 8 8 1.443262 0.441553 9 9 1.168163 -2.054946 Specifying a chunksize yields a StataReader instance that can be used to read chunksize lines from the file at a time. The StataReader object can be used as an iterator. In [651]: with pd.read_stata("stata.dta", chunksize=3) as reader: .....: for df in reader: .....: print(df.shape) .....: (3, 3) (3, 3) (3, 3) (1, 3) For more fine-grained control, use iterator=True and specify chunksize with each call to read(). In [652]: with pd.read_stata("stata.dta", iterator=True) as reader: .....: chunk1 = reader.read(5) .....: chunk2 = reader.read(5) .....: Currently the index is retrieved as a column. The parameter convert_categoricals indicates whether value labels should be read and used to create a Categorical variable from them. Value labels can also be retrieved by the function value_labels, which requires read() to be called before use. The parameter convert_missing indicates whether missing value representations in Stata should be preserved. If False (the default), missing values are represented as np.nan. If True, missing values are represented using StataMissingValue objects, and columns containing missing values will have object data type. Note read_stata() and StataReader support .dta formats 113-115 (Stata 10-12), 117 (Stata 13), and 118 (Stata 14). Note Setting preserve_dtypes=False will upcast to the standard pandas data types: int64 for all integer types and float64 for floating point data. By default, the Stata data types are preserved when importing. Categorical data# Categorical data can be exported to Stata data files as value labeled data. The exported data consists of the underlying category codes as integer data values and the categories as value labels. Stata does not have an explicit equivalent to a Categorical and information about whether the variable is ordered is lost when exporting. Warning Stata only supports string value labels, and so str is called on the categories when exporting data. Exporting Categorical variables with non-string categories produces a warning, and can result a loss of information if the str representations of the categories are not unique. Labeled data can similarly be imported from Stata data files as Categorical variables using the keyword argument convert_categoricals (True by default). The keyword argument order_categoricals (True by default) determines whether imported Categorical variables are ordered. Note When importing categorical data, the values of the variables in the Stata data file are not preserved since Categorical variables always use integer data types between -1 and n-1 where n is the number of categories. If the original values in the Stata data file are required, these can be imported by setting convert_categoricals=False, which will import original data (but not the variable labels). The original values can be matched to the imported categorical data since there is a simple mapping between the original Stata data values and the category codes of imported Categorical variables: missing values are assigned code -1, and the smallest original value is assigned 0, the second smallest is assigned 1 and so on until the largest original value is assigned the code n-1. Note Stata supports partially labeled series. These series have value labels for some but not all data values. Importing a partially labeled series will produce a Categorical with string categories for the values that are labeled and numeric categories for values with no label. SAS formats# The top-level function read_sas() can read (but not write) SAS XPORT (.xpt) and (since v0.18.0) SAS7BDAT (.sas7bdat) format files. SAS files only contain two value types: ASCII text and floating point values (usually 8 bytes but sometimes truncated). For xport files, there is no automatic type conversion to integers, dates, or categoricals. For SAS7BDAT files, the format codes may allow date variables to be automatically converted to dates. By default the whole file is read and returned as a DataFrame. Specify a chunksize or use iterator=True to obtain reader objects (XportReader or SAS7BDATReader) for incrementally reading the file. The reader objects also have attributes that contain additional information about the file and its variables. Read a SAS7BDAT file: df = pd.read_sas("sas_data.sas7bdat") Obtain an iterator and read an XPORT file 100,000 lines at a time: def do_something(chunk): pass with pd.read_sas("sas_xport.xpt", chunk=100000) as rdr: for chunk in rdr: do_something(chunk) The specification for the xport file format is available from the SAS web site. No official documentation is available for the SAS7BDAT format. SPSS formats# New in version 0.25.0. The top-level function read_spss() can read (but not write) SPSS SAV (.sav) and ZSAV (.zsav) format files. SPSS files contain column names. By default the whole file is read, categorical columns are converted into pd.Categorical, and a DataFrame with all columns is returned. Specify the usecols parameter to obtain a subset of columns. Specify convert_categoricals=False to avoid converting categorical columns into pd.Categorical. Read an SPSS file: df = pd.read_spss("spss_data.sav") Extract a subset of columns contained in usecols from an SPSS file and avoid converting categorical columns into pd.Categorical: df = pd.read_spss( "spss_data.sav", usecols=["foo", "bar"], convert_categoricals=False, ) More information about the SAV and ZSAV file formats is available here. Other file formats# pandas itself only supports IO with a limited set of file formats that map cleanly to its tabular data model. For reading and writing other file formats into and from pandas, we recommend these packages from the broader community. netCDF# xarray provides data structures inspired by the pandas DataFrame for working with multi-dimensional datasets, with a focus on the netCDF file format and easy conversion to and from pandas. Performance considerations# This is an informal comparison of various IO methods, using pandas 0.24.2. Timings are machine dependent and small differences should be ignored. In [1]: sz = 1000000 In [2]: df = pd.DataFrame({'A': np.random.randn(sz), 'B': [1] * sz}) In [3]: df.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 1000000 entries, 0 to 999999 Data columns (total 2 columns): A 1000000 non-null float64 B 1000000 non-null int64 dtypes: float64(1), int64(1) memory usage: 15.3 MB The following test functions will be used below to compare the performance of several IO methods: import numpy as np import os sz = 1000000 df = pd.DataFrame({"A": np.random.randn(sz), "B": [1] * sz}) sz = 1000000 np.random.seed(42) df = pd.DataFrame({"A": np.random.randn(sz), "B": [1] * sz}) def test_sql_write(df): if os.path.exists("test.sql"): os.remove("test.sql") sql_db = sqlite3.connect("test.sql") df.to_sql(name="test_table", con=sql_db) sql_db.close() def test_sql_read(): sql_db = sqlite3.connect("test.sql") pd.read_sql_query("select * from test_table", sql_db) sql_db.close() def test_hdf_fixed_write(df): df.to_hdf("test_fixed.hdf", "test", mode="w") def test_hdf_fixed_read(): pd.read_hdf("test_fixed.hdf", "test") def test_hdf_fixed_write_compress(df): df.to_hdf("test_fixed_compress.hdf", "test", mode="w", complib="blosc") def test_hdf_fixed_read_compress(): pd.read_hdf("test_fixed_compress.hdf", "test") def test_hdf_table_write(df): df.to_hdf("test_table.hdf", "test", mode="w", format="table") def test_hdf_table_read(): pd.read_hdf("test_table.hdf", "test") def test_hdf_table_write_compress(df): df.to_hdf( "test_table_compress.hdf", "test", mode="w", complib="blosc", format="table" ) def test_hdf_table_read_compress(): pd.read_hdf("test_table_compress.hdf", "test") def test_csv_write(df): df.to_csv("test.csv", mode="w") def test_csv_read(): pd.read_csv("test.csv", index_col=0) def test_feather_write(df): df.to_feather("test.feather") def test_feather_read(): pd.read_feather("test.feather") def test_pickle_write(df): df.to_pickle("test.pkl") def test_pickle_read(): pd.read_pickle("test.pkl") def test_pickle_write_compress(df): df.to_pickle("test.pkl.compress", compression="xz") def test_pickle_read_compress(): pd.read_pickle("test.pkl.compress", compression="xz") def test_parquet_write(df): df.to_parquet("test.parquet") def test_parquet_read(): pd.read_parquet("test.parquet") When writing, the top three functions in terms of speed are test_feather_write, test_hdf_fixed_write and test_hdf_fixed_write_compress. In [4]: %timeit test_sql_write(df) 3.29 s ± 43.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [5]: %timeit test_hdf_fixed_write(df) 19.4 ms ± 560 µs per loop (mean ± std. dev. of 7 runs, 1 loop each) In [6]: %timeit test_hdf_fixed_write_compress(df) 19.6 ms ± 308 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [7]: %timeit test_hdf_table_write(df) 449 ms ± 5.61 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [8]: %timeit test_hdf_table_write_compress(df) 448 ms ± 11.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [9]: %timeit test_csv_write(df) 3.66 s ± 26.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [10]: %timeit test_feather_write(df) 9.75 ms ± 117 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [11]: %timeit test_pickle_write(df) 30.1 ms ± 229 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [12]: %timeit test_pickle_write_compress(df) 4.29 s ± 15.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [13]: %timeit test_parquet_write(df) 67.6 ms ± 706 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) When reading, the top three functions in terms of speed are test_feather_read, test_pickle_read and test_hdf_fixed_read. In [14]: %timeit test_sql_read() 1.77 s ± 17.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [15]: %timeit test_hdf_fixed_read() 19.4 ms ± 436 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [16]: %timeit test_hdf_fixed_read_compress() 19.5 ms ± 222 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [17]: %timeit test_hdf_table_read() 38.6 ms ± 857 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [18]: %timeit test_hdf_table_read_compress() 38.8 ms ± 1.49 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) In [19]: %timeit test_csv_read() 452 ms ± 9.04 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [20]: %timeit test_feather_read() 12.4 ms ± 99.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [21]: %timeit test_pickle_read() 18.4 ms ± 191 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [22]: %timeit test_pickle_read_compress() 915 ms ± 7.48 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [23]: %timeit test_parquet_read() 24.4 ms ± 146 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) The files test.pkl.compress, test.parquet and test.feather took the least space on disk (in bytes). 29519500 Oct 10 06:45 test.csv 16000248 Oct 10 06:45 test.feather 8281983 Oct 10 06:49 test.parquet 16000857 Oct 10 06:47 test.pkl 7552144 Oct 10 06:48 test.pkl.compress 34816000 Oct 10 06:42 test.sql 24009288 Oct 10 06:43 test_fixed.hdf 24009288 Oct 10 06:43 test_fixed_compress.hdf 24458940 Oct 10 06:44 test_table.hdf 24458940 Oct 10 06:44 test_table_compress.hdf
898
1,125
Trying to read data with excel pandas... and getting a consistent error across multiple files Here is my test code, that gives my error: import pandas file = 'KBART EDINA.xlsx' data = list() with open(file, 'r') as xl_file: df = pandas.read_excel(xl_file) When I run it, I get the following error: ❯ python hack.py Traceback (most recent call last): File "hack.py", line 11, in <module> df = pandas.read_excel(xl_file) File "/CWD/deliberately/obscured/.direnv/python-3.7.6/lib/python3.7/site-packages/pandas/util/_decorators.py", line 299, in wrapper return func(*args, **kwargs) File "/CWD/deliberately/obscured/.direnv/python-3.7.6/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 336, in read_excel io = ExcelFile(io, storage_options=storage_options, engine=engine) File "/CWD/deliberately/obscured/.direnv/python-3.7.6/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 1072, in __init__ content=path_or_buffer, storage_options=storage_options File "/CWD/deliberately/obscured/.direnv/python-3.7.6/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 954, in inspect_excel_format buf = stream.read(PEEK_SIZE) File "/another/obscured/path/miniconda3/lib/python3.7/codecs.py", line 322, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb9 in position 16: invalid start byte I've tried with 6 different xls, xlsx, and ods files..... and they all return the same error I have the following (relevant) libraries installed: openpyxl 3.0.7 pandas 1.2.4 xlrd 2.0.1 I know the file(s) are readable (I had a if not os.path.isfile(file): print("####") clause to prove that) .... what am I missing?
Error between seat & keyboard: Wrong: import pandas file = 'KBART EDINA.xlsx' with open(file, 'r') as xl_file: df = pandas.read_excel(xl_file) Right import pandas file = 'KBART EDINA.xlsx' df = pandas.read_excel(file)
60,980,093
Pandas pivot and group by?
<p>I'm using pandas to load a .csv extract then do some transformations with the ultimate goal of exporting the data set back out to a .csv. I have a dataframe that is structured similarly to:</p> <pre><code>col1 col2 col3 col4 name1 Yes string1 val1 name2 string1 val1 name3 string1 val1 name2 Yes string2 val2 name3 string2 val2 name4 string2 val2 name3 Yes string3 val3 name4 string3 val3 name5 string3 val3 </code></pre> <p>and would like the output to appear like:</p> <pre><code>name1 name2 name3 name4 name5 col3 col4 Yes string1 val1 Yes string2 val2 Yes string3 val3 </code></pre> <p>I was able to use <code>.pivot()</code> to get the data in col1 and col2 from the original dataframe to appear as desired, but as far as I understand it, there's no way to include other columns with grouped values (col3 and col4 in this example) using <code>.pivot()</code>, and it appears as though <code>.pivot_table()</code> won't work for my purposes. The remaining problem I have using <code>.pivot()</code> is that when the new index is formed, I lose the ability to join back to those other grouped columns (again, col3 and col4 here).</p> <p>Am I heading down the right path? Or is there a better way of approaching what I'm trying to accomplish?</p> <p>Thanks in advance.</p>
60,980,314
"2020-04-01T20:50:51.243000"
1
null
0
219
python|pandas
<p>You want to use argument <code>dropna=False</code> to keep the groups which only have missing values:</p> <pre><code>piv = df.pivot_table(index='col3', columns='col1', values='col2', aggfunc=lambda x: x, dropna=False) col1 name1 name2 name3 name4 name5 col3 string1 Yes NaN NaN NaN NaN string2 NaN Yes NaN NaN NaN string3 NaN NaN Yes NaN NaN </code></pre> <p>To convert <code>NaN</code> to empty strings, use argument <code>fill_value</code>:</p> <pre><code>piv = df.pivot_table(index='col3', columns='col1', values='col2', aggfunc=lambda x: x, dropna=False, fill_value='') col1 name1 name2 name3 name4 name5 col3 string1 Yes string2 Yes string3 Yes </code></pre> <p>To remove the index and column attribute name, use <code>rename_axis</code>:</p> <pre><code>piv = (df.pivot_table(index='col3', columns='col1', values='col2', aggfunc=lambda x: x, dropna=False, fill_value='') .rename_axis(index=None, columns=None) ) name1 name2 name3 name4 name5 string1 Yes string2 Yes string3 Yes </code></pre>
"2020-04-01T21:05:22.650000"
0
https://pandas.pydata.org/docs/user_guide/reshaping.html
You want to use argument dropna=False to keep the groups which only have missing values: piv = df.pivot_table(index='col3', columns='col1', values='col2', aggfunc=lambda x: x, dropna=False) col1 name1 name2 name3 name4 name5 col3 string1 Yes NaN NaN NaN NaN string2 NaN Yes NaN NaN NaN string3 NaN NaN Yes NaN NaN To convert NaN to empty strings, use argument fill_value: piv = df.pivot_table(index='col3', columns='col1', values='col2', aggfunc=lambda x: x, dropna=False, fill_value='') col1 name1 name2 name3 name4 name5 col3 string1 Yes string2 Yes string3 Yes To remove the index and column attribute name, use rename_axis: piv = (df.pivot_table(index='col3', columns='col1', values='col2', aggfunc=lambda x: x, dropna=False, fill_value='') .rename_axis(index=None, columns=None) ) name1 name2 name3 name4 name5 string1 Yes string2 Yes string3 Yes
0
1,433
Pandas pivot and group by? I'm using pandas to load a .csv extract then do some transformations with the ultimate goal of exporting the data set back out to a .csv. I have a dataframe that is structured similarly to: col1 col2 col3 col4 name1 Yes string1 val1 name2 string1 val1 name3 string1 val1 name2 Yes string2 val2 name3 string2 val2 name4 string2 val2 name3 Yes string3 val3 name4 string3 val3 name5 string3 val3 and would like the output to appear like: name1 name2 name3 name4 name5 col3 col4 Yes string1 val1 Yes string2 val2 Yes string3 val3 I was able to use .pivot() to get the data in col1 and col2 from the original dataframe to appear as desired, but as far as I understand it, there's no way to include other columns with grouped values (col3 and col4 in this example) using .pivot(), and it appears as though .pivot_table() won't work for my purposes. The remaining problem I have using .pivot() is that when the new index is formed, I lose the ability to join back to those other grouped columns (again, col3 and col4 here). Am I heading down the right path? Or is there a better way of approaching what I'm trying to accomplish? Thanks in advance.
Pandas pivot and group by?
You want to use argument dropna=False to keep the groups which only have missing values: piv = df.pivot_table(index='col3', columns='col1', values='col2', aggfunc=lambda x: x, dropna=False) col1 name1 name2 name3 name4 name5 col3 string1 Yes NaN NaN NaN NaN string2 NaN Yes NaN NaN NaN string3 NaN NaN Yes NaN NaN To convert NaN to empty strings, use argument fill_value: piv = df.pivot_table(index='col3', columns='col1', values='col2', aggfunc=lambda x: x, dropna=False, fill_value='') col1 name1 name2 name3 name4 name5 col3 string1 Yes string2 Yes string3 Yes To remove the index and column attribute name, use rename_axis: piv = (df.pivot_table(index='col3', columns='col1', values='col2', aggfunc=lambda x: x, dropna=False, fill_value='') .rename_axis(index=None, columns=None) ) name1 name2 name3 name4 name5 string1 Yes string2 Yes string3 Yes
61,234,982
How to show column data that contains keyword in a dataframe
<p>I have a dataframe that has one column called 'job_title', there are many job titles, I want to show all job title contains 'Professor'</p> <p>I have the following code:</p> <pre><code>sunshin_df[sunshin_df['job_title'].isin(['Professor']) </code></pre> <p>But this only show title "Professor", other titles such as "Associate Professor" are not included</p> <p>Then I tried to use .str.contains, but it doesn't work, </p> <p>AttributeError: 'list' object has no attribute 'str'</p> <pre><code>sunshin_df[sunshin_df['job_title'].isin(['Professor'].str.contains('Professor', na=False, case=False))] </code></pre> <p>What is the best and simplest way to show all value that contains the keyword I want?</p>
61,235,097
"2020-04-15T17:32:51.023000"
2
null
0
219
python|pandas
<p>You should try following :</p> <pre><code>sunshin_df[sunshin_df['job_title'].str.contains('Professor')] </code></pre> <p>i.e check the value of the job_title column if it contains 'Professor' in it. </p>
"2020-04-15T17:39:58.940000"
0
https://pandas.pydata.org/docs/user_guide/visualization.html
Chart visualization# Chart visualization# Note The examples below assume that you’re using Jupyter. This section demonstrates visualization through charting. For information on visualization of tabular data please see the section on Table Visualization. We use the standard convention for referencing the matplotlib API: In [1]: import matplotlib.pyplot as plt In [2]: plt.close("all") We provide the basics in pandas to easily create decent looking plots. You should try following : sunshin_df[sunshin_df['job_title'].str.contains('Professor')] i.e check the value of the job_title column if it contains 'Professor' in it. See the ecosystem section for visualization libraries that go beyond the basics documented here. Note All calls to np.random are seeded with 123456. Basic plotting: plot# We will demonstrate the basics, see the cookbook for some advanced strategies. The plot method on Series and DataFrame is just a simple wrapper around plt.plot(): In [3]: ts = pd.Series(np.random.randn(1000), index=pd.date_range("1/1/2000", periods=1000)) In [4]: ts = ts.cumsum() In [5]: ts.plot(); If the index consists of dates, it calls gcf().autofmt_xdate() to try to format the x-axis nicely as per above. On DataFrame, plot() is a convenience to plot all of the columns with labels: In [6]: df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list("ABCD")) In [7]: df = df.cumsum() In [8]: plt.figure(); In [9]: df.plot(); You can plot one column versus another using the x and y keywords in plot(): In [10]: df3 = pd.DataFrame(np.random.randn(1000, 2), columns=["B", "C"]).cumsum() In [11]: df3["A"] = pd.Series(list(range(len(df)))) In [12]: df3.plot(x="A", y="B"); Note For more formatting and styling options, see formatting below. Other plots# Plotting methods allow for a handful of plot styles other than the default line plot. These methods can be provided as the kind keyword argument to plot(), and include: ‘bar’ or ‘barh’ for bar plots ‘hist’ for histogram ‘box’ for boxplot ‘kde’ or ‘density’ for density plots ‘area’ for area plots ‘scatter’ for scatter plots ‘hexbin’ for hexagonal bin plots ‘pie’ for pie plots For example, a bar plot can be created the following way: In [13]: plt.figure(); In [14]: df.iloc[5].plot(kind="bar"); You can also create these other plots using the methods DataFrame.plot.<kind> instead of providing the kind keyword argument. This makes it easier to discover plot methods and the specific arguments they use: In [15]: df = pd.DataFrame() In [16]: df.plot.<TAB> # noqa: E225, E999 df.plot.area df.plot.barh df.plot.density df.plot.hist df.plot.line df.plot.scatter df.plot.bar df.plot.box df.plot.hexbin df.plot.kde df.plot.pie In addition to these kind s, there are the DataFrame.hist(), and DataFrame.boxplot() methods, which use a separate interface. Finally, there are several plotting functions in pandas.plotting that take a Series or DataFrame as an argument. These include: Scatter Matrix Andrews Curves Parallel Coordinates Lag Plot Autocorrelation Plot Bootstrap Plot RadViz Plots may also be adorned with errorbars or tables. Bar plots# For labeled, non-time series data, you may wish to produce a bar plot: In [17]: plt.figure(); In [18]: df.iloc[5].plot.bar(); In [19]: plt.axhline(0, color="k"); Calling a DataFrame’s plot.bar() method produces a multiple bar plot: In [20]: df2 = pd.DataFrame(np.random.rand(10, 4), columns=["a", "b", "c", "d"]) In [21]: df2.plot.bar(); To produce a stacked bar plot, pass stacked=True: In [22]: df2.plot.bar(stacked=True); To get horizontal bar plots, use the barh method: In [23]: df2.plot.barh(stacked=True); Histograms# Histograms can be drawn by using the DataFrame.plot.hist() and Series.plot.hist() methods. In [24]: df4 = pd.DataFrame( ....: { ....: "a": np.random.randn(1000) + 1, ....: "b": np.random.randn(1000), ....: "c": np.random.randn(1000) - 1, ....: }, ....: columns=["a", "b", "c"], ....: ) ....: In [25]: plt.figure(); In [26]: df4.plot.hist(alpha=0.5); A histogram can be stacked using stacked=True. Bin size can be changed using the bins keyword. In [27]: plt.figure(); In [28]: df4.plot.hist(stacked=True, bins=20); You can pass other keywords supported by matplotlib hist. For example, horizontal and cumulative histograms can be drawn by orientation='horizontal' and cumulative=True. In [29]: plt.figure(); In [30]: df4["a"].plot.hist(orientation="horizontal", cumulative=True); See the hist method and the matplotlib hist documentation for more. The existing interface DataFrame.hist to plot histogram still can be used. In [31]: plt.figure(); In [32]: df["A"].diff().hist(); DataFrame.hist() plots the histograms of the columns on multiple subplots: In [33]: plt.figure(); In [34]: df.diff().hist(color="k", alpha=0.5, bins=50); The by keyword can be specified to plot grouped histograms: In [35]: data = pd.Series(np.random.randn(1000)) In [36]: data.hist(by=np.random.randint(0, 4, 1000), figsize=(6, 4)); In addition, the by keyword can also be specified in DataFrame.plot.hist(). Changed in version 1.4.0. In [37]: data = pd.DataFrame( ....: { ....: "a": np.random.choice(["x", "y", "z"], 1000), ....: "b": np.random.choice(["e", "f", "g"], 1000), ....: "c": np.random.randn(1000), ....: "d": np.random.randn(1000) - 1, ....: }, ....: ) ....: In [38]: data.plot.hist(by=["a", "b"], figsize=(10, 5)); Box plots# Boxplot can be drawn calling Series.plot.box() and DataFrame.plot.box(), or DataFrame.boxplot() to visualize the distribution of values within each column. For instance, here is a boxplot representing five trials of 10 observations of a uniform random variable on [0,1). In [39]: df = pd.DataFrame(np.random.rand(10, 5), columns=["A", "B", "C", "D", "E"]) In [40]: df.plot.box(); Boxplot can be colorized by passing color keyword. You can pass a dict whose keys are boxes, whiskers, medians and caps. If some keys are missing in the dict, default colors are used for the corresponding artists. Also, boxplot has sym keyword to specify fliers style. When you pass other type of arguments via color keyword, it will be directly passed to matplotlib for all the boxes, whiskers, medians and caps colorization. The colors are applied to every boxes to be drawn. If you want more complicated colorization, you can get each drawn artists by passing return_type. In [41]: color = { ....: "boxes": "DarkGreen", ....: "whiskers": "DarkOrange", ....: "medians": "DarkBlue", ....: "caps": "Gray", ....: } ....: In [42]: df.plot.box(color=color, sym="r+"); Also, you can pass other keywords supported by matplotlib boxplot. For example, horizontal and custom-positioned boxplot can be drawn by vert=False and positions keywords. In [43]: df.plot.box(vert=False, positions=[1, 4, 5, 6, 8]); See the boxplot method and the matplotlib boxplot documentation for more. The existing interface DataFrame.boxplot to plot boxplot still can be used. In [44]: df = pd.DataFrame(np.random.rand(10, 5)) In [45]: plt.figure(); In [46]: bp = df.boxplot() You can create a stratified boxplot using the by keyword argument to create groupings. For instance, In [47]: df = pd.DataFrame(np.random.rand(10, 2), columns=["Col1", "Col2"]) In [48]: df["X"] = pd.Series(["A", "A", "A", "A", "A", "B", "B", "B", "B", "B"]) In [49]: plt.figure(); In [50]: bp = df.boxplot(by="X") You can also pass a subset of columns to plot, as well as group by multiple columns: In [51]: df = pd.DataFrame(np.random.rand(10, 3), columns=["Col1", "Col2", "Col3"]) In [52]: df["X"] = pd.Series(["A", "A", "A", "A", "A", "B", "B", "B", "B", "B"]) In [53]: df["Y"] = pd.Series(["A", "B", "A", "B", "A", "B", "A", "B", "A", "B"]) In [54]: plt.figure(); In [55]: bp = df.boxplot(column=["Col1", "Col2"], by=["X", "Y"]) You could also create groupings with DataFrame.plot.box(), for instance: Changed in version 1.4.0. In [56]: df = pd.DataFrame(np.random.rand(10, 3), columns=["Col1", "Col2", "Col3"]) In [57]: df["X"] = pd.Series(["A", "A", "A", "A", "A", "B", "B", "B", "B", "B"]) In [58]: plt.figure(); In [59]: bp = df.plot.box(column=["Col1", "Col2"], by="X") In boxplot, the return type can be controlled by the return_type, keyword. The valid choices are {"axes", "dict", "both", None}. Faceting, created by DataFrame.boxplot with the by keyword, will affect the output type as well: return_type Faceted Output type None No axes None Yes 2-D ndarray of axes 'axes' No axes 'axes' Yes Series of axes 'dict' No dict of artists 'dict' Yes Series of dicts of artists 'both' No namedtuple 'both' Yes Series of namedtuples Groupby.boxplot always returns a Series of return_type. In [60]: np.random.seed(1234) In [61]: df_box = pd.DataFrame(np.random.randn(50, 2)) In [62]: df_box["g"] = np.random.choice(["A", "B"], size=50) In [63]: df_box.loc[df_box["g"] == "B", 1] += 3 In [64]: bp = df_box.boxplot(by="g") The subplots above are split by the numeric columns first, then the value of the g column. Below the subplots are first split by the value of g, then by the numeric columns. In [65]: bp = df_box.groupby("g").boxplot() Area plot# You can create area plots with Series.plot.area() and DataFrame.plot.area(). Area plots are stacked by default. To produce stacked area plot, each column must be either all positive or all negative values. When input data contains NaN, it will be automatically filled by 0. If you want to drop or fill by different values, use dataframe.dropna() or dataframe.fillna() before calling plot. In [66]: df = pd.DataFrame(np.random.rand(10, 4), columns=["a", "b", "c", "d"]) In [67]: df.plot.area(); To produce an unstacked plot, pass stacked=False. Alpha value is set to 0.5 unless otherwise specified: In [68]: df.plot.area(stacked=False); Scatter plot# Scatter plot can be drawn by using the DataFrame.plot.scatter() method. Scatter plot requires numeric columns for the x and y axes. These can be specified by the x and y keywords. In [69]: df = pd.DataFrame(np.random.rand(50, 4), columns=["a", "b", "c", "d"]) In [70]: df["species"] = pd.Categorical( ....: ["setosa"] * 20 + ["versicolor"] * 20 + ["virginica"] * 10 ....: ) ....: In [71]: df.plot.scatter(x="a", y="b"); To plot multiple column groups in a single axes, repeat plot method specifying target ax. It is recommended to specify color and label keywords to distinguish each groups. In [72]: ax = df.plot.scatter(x="a", y="b", color="DarkBlue", label="Group 1") In [73]: df.plot.scatter(x="c", y="d", color="DarkGreen", label="Group 2", ax=ax); The keyword c may be given as the name of a column to provide colors for each point: In [74]: df.plot.scatter(x="a", y="b", c="c", s=50); If a categorical column is passed to c, then a discrete colorbar will be produced: New in version 1.3.0. In [75]: df.plot.scatter(x="a", y="b", c="species", cmap="viridis", s=50); You can pass other keywords supported by matplotlib scatter. The example below shows a bubble chart using a column of the DataFrame as the bubble size. In [76]: df.plot.scatter(x="a", y="b", s=df["c"] * 200); See the scatter method and the matplotlib scatter documentation for more. Hexagonal bin plot# You can create hexagonal bin plots with DataFrame.plot.hexbin(). Hexbin plots can be a useful alternative to scatter plots if your data are too dense to plot each point individually. In [77]: df = pd.DataFrame(np.random.randn(1000, 2), columns=["a", "b"]) In [78]: df["b"] = df["b"] + np.arange(1000) In [79]: df.plot.hexbin(x="a", y="b", gridsize=25); A useful keyword argument is gridsize; it controls the number of hexagons in the x-direction, and defaults to 100. A larger gridsize means more, smaller bins. By default, a histogram of the counts around each (x, y) point is computed. You can specify alternative aggregations by passing values to the C and reduce_C_function arguments. C specifies the value at each (x, y) point and reduce_C_function is a function of one argument that reduces all the values in a bin to a single number (e.g. mean, max, sum, std). In this example the positions are given by columns a and b, while the value is given by column z. The bins are aggregated with NumPy’s max function. In [80]: df = pd.DataFrame(np.random.randn(1000, 2), columns=["a", "b"]) In [81]: df["b"] = df["b"] + np.arange(1000) In [82]: df["z"] = np.random.uniform(0, 3, 1000) In [83]: df.plot.hexbin(x="a", y="b", C="z", reduce_C_function=np.max, gridsize=25); See the hexbin method and the matplotlib hexbin documentation for more. Pie plot# You can create a pie plot with DataFrame.plot.pie() or Series.plot.pie(). If your data includes any NaN, they will be automatically filled with 0. A ValueError will be raised if there are any negative values in your data. In [84]: series = pd.Series(3 * np.random.rand(4), index=["a", "b", "c", "d"], name="series") In [85]: series.plot.pie(figsize=(6, 6)); For pie plots it’s best to use square figures, i.e. a figure aspect ratio 1. You can create the figure with equal width and height, or force the aspect ratio to be equal after plotting by calling ax.set_aspect('equal') on the returned axes object. Note that pie plot with DataFrame requires that you either specify a target column by the y argument or subplots=True. When y is specified, pie plot of selected column will be drawn. If subplots=True is specified, pie plots for each column are drawn as subplots. A legend will be drawn in each pie plots by default; specify legend=False to hide it. In [86]: df = pd.DataFrame( ....: 3 * np.random.rand(4, 2), index=["a", "b", "c", "d"], columns=["x", "y"] ....: ) ....: In [87]: df.plot.pie(subplots=True, figsize=(8, 4)); You can use the labels and colors keywords to specify the labels and colors of each wedge. Warning Most pandas plots use the label and color arguments (note the lack of “s” on those). To be consistent with matplotlib.pyplot.pie() you must use labels and colors. If you want to hide wedge labels, specify labels=None. If fontsize is specified, the value will be applied to wedge labels. Also, other keywords supported by matplotlib.pyplot.pie() can be used. In [88]: series.plot.pie( ....: labels=["AA", "BB", "CC", "DD"], ....: colors=["r", "g", "b", "c"], ....: autopct="%.2f", ....: fontsize=20, ....: figsize=(6, 6), ....: ); ....: If you pass values whose sum total is less than 1.0 they will be rescaled so that they sum to 1. In [89]: series = pd.Series([0.1] * 4, index=["a", "b", "c", "d"], name="series2") In [90]: series.plot.pie(figsize=(6, 6)); See the matplotlib pie documentation for more. Plotting with missing data# pandas tries to be pragmatic about plotting DataFrames or Series that contain missing data. Missing values are dropped, left out, or filled depending on the plot type. Plot Type NaN Handling Line Leave gaps at NaNs Line (stacked) Fill 0’s Bar Fill 0’s Scatter Drop NaNs Histogram Drop NaNs (column-wise) Box Drop NaNs (column-wise) Area Fill 0’s KDE Drop NaNs (column-wise) Hexbin Drop NaNs Pie Fill 0’s If any of these defaults are not what you want, or if you want to be explicit about how missing values are handled, consider using fillna() or dropna() before plotting. Plotting tools# These functions can be imported from pandas.plotting and take a Series or DataFrame as an argument. Scatter matrix plot# You can create a scatter plot matrix using the scatter_matrix method in pandas.plotting: In [91]: from pandas.plotting import scatter_matrix In [92]: df = pd.DataFrame(np.random.randn(1000, 4), columns=["a", "b", "c", "d"]) In [93]: scatter_matrix(df, alpha=0.2, figsize=(6, 6), diagonal="kde"); Density plot# You can create density plots using the Series.plot.kde() and DataFrame.plot.kde() methods. In [94]: ser = pd.Series(np.random.randn(1000)) In [95]: ser.plot.kde(); Andrews curves# Andrews curves allow one to plot multivariate data as a large number of curves that are created using the attributes of samples as coefficients for Fourier series, see the Wikipedia entry for more information. By coloring these curves differently for each class it is possible to visualize data clustering. Curves belonging to samples of the same class will usually be closer together and form larger structures. Note: The “Iris” dataset is available here. In [96]: from pandas.plotting import andrews_curves In [97]: data = pd.read_csv("data/iris.data") In [98]: plt.figure(); In [99]: andrews_curves(data, "Name"); Parallel coordinates# Parallel coordinates is a plotting technique for plotting multivariate data, see the Wikipedia entry for an introduction. Parallel coordinates allows one to see clusters in data and to estimate other statistics visually. Using parallel coordinates points are represented as connected line segments. Each vertical line represents one attribute. One set of connected line segments represents one data point. Points that tend to cluster will appear closer together. In [100]: from pandas.plotting import parallel_coordinates In [101]: data = pd.read_csv("data/iris.data") In [102]: plt.figure(); In [103]: parallel_coordinates(data, "Name"); Lag plot# Lag plots are used to check if a data set or time series is random. Random data should not exhibit any structure in the lag plot. Non-random structure implies that the underlying data are not random. The lag argument may be passed, and when lag=1 the plot is essentially data[:-1] vs. data[1:]. In [104]: from pandas.plotting import lag_plot In [105]: plt.figure(); In [106]: spacing = np.linspace(-99 * np.pi, 99 * np.pi, num=1000) In [107]: data = pd.Series(0.1 * np.random.rand(1000) + 0.9 * np.sin(spacing)) In [108]: lag_plot(data); Autocorrelation plot# Autocorrelation plots are often used for checking randomness in time series. This is done by computing autocorrelations for data values at varying time lags. If time series is random, such autocorrelations should be near zero for any and all time-lag separations. If time series is non-random then one or more of the autocorrelations will be significantly non-zero. The horizontal lines displayed in the plot correspond to 95% and 99% confidence bands. The dashed line is 99% confidence band. See the Wikipedia entry for more about autocorrelation plots. In [109]: from pandas.plotting import autocorrelation_plot In [110]: plt.figure(); In [111]: spacing = np.linspace(-9 * np.pi, 9 * np.pi, num=1000) In [112]: data = pd.Series(0.7 * np.random.rand(1000) + 0.3 * np.sin(spacing)) In [113]: autocorrelation_plot(data); Bootstrap plot# Bootstrap plots are used to visually assess the uncertainty of a statistic, such as mean, median, midrange, etc. A random subset of a specified size is selected from a data set, the statistic in question is computed for this subset and the process is repeated a specified number of times. Resulting plots and histograms are what constitutes the bootstrap plot. In [114]: from pandas.plotting import bootstrap_plot In [115]: data = pd.Series(np.random.rand(1000)) In [116]: bootstrap_plot(data, size=50, samples=500, color="grey"); RadViz# RadViz is a way of visualizing multi-variate data. It is based on a simple spring tension minimization algorithm. Basically you set up a bunch of points in a plane. In our case they are equally spaced on a unit circle. Each point represents a single attribute. You then pretend that each sample in the data set is attached to each of these points by a spring, the stiffness of which is proportional to the numerical value of that attribute (they are normalized to unit interval). The point in the plane, where our sample settles to (where the forces acting on our sample are at an equilibrium) is where a dot representing our sample will be drawn. Depending on which class that sample belongs it will be colored differently. See the R package Radviz for more information. Note: The “Iris” dataset is available here. In [117]: from pandas.plotting import radviz In [118]: data = pd.read_csv("data/iris.data") In [119]: plt.figure(); In [120]: radviz(data, "Name"); Plot formatting# Setting the plot style# From version 1.5 and up, matplotlib offers a range of pre-configured plotting styles. Setting the style can be used to easily give plots the general look that you want. Setting the style is as easy as calling matplotlib.style.use(my_plot_style) before creating your plot. For example you could write matplotlib.style.use('ggplot') for ggplot-style plots. You can see the various available style names at matplotlib.style.available and it’s very easy to try them out. General plot style arguments# Most plotting methods have a set of keyword arguments that control the layout and formatting of the returned plot: In [121]: plt.figure(); In [122]: ts.plot(style="k--", label="Series"); For each kind of plot (e.g. line, bar, scatter) any additional arguments keywords are passed along to the corresponding matplotlib function (ax.plot(), ax.bar(), ax.scatter()). These can be used to control additional styling, beyond what pandas provides. Controlling the legend# You may set the legend argument to False to hide the legend, which is shown by default. In [123]: df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list("ABCD")) In [124]: df = df.cumsum() In [125]: df.plot(legend=False); Controlling the labels# New in version 1.1.0. You may set the xlabel and ylabel arguments to give the plot custom labels for x and y axis. By default, pandas will pick up index name as xlabel, while leaving it empty for ylabel. In [126]: df.plot(); In [127]: df.plot(xlabel="new x", ylabel="new y"); Scales# You may pass logy to get a log-scale Y axis. In [128]: ts = pd.Series(np.random.randn(1000), index=pd.date_range("1/1/2000", periods=1000)) In [129]: ts = np.exp(ts.cumsum()) In [130]: ts.plot(logy=True); See also the logx and loglog keyword arguments. Plotting on a secondary y-axis# To plot data on a secondary y-axis, use the secondary_y keyword: In [131]: df["A"].plot(); In [132]: df["B"].plot(secondary_y=True, style="g"); To plot some columns in a DataFrame, give the column names to the secondary_y keyword: In [133]: plt.figure(); In [134]: ax = df.plot(secondary_y=["A", "B"]) In [135]: ax.set_ylabel("CD scale"); In [136]: ax.right_ax.set_ylabel("AB scale"); Note that the columns plotted on the secondary y-axis is automatically marked with “(right)” in the legend. To turn off the automatic marking, use the mark_right=False keyword: In [137]: plt.figure(); In [138]: df.plot(secondary_y=["A", "B"], mark_right=False); Custom formatters for timeseries plots# Changed in version 1.0.0. pandas provides custom formatters for timeseries plots. These change the formatting of the axis labels for dates and times. By default, the custom formatters are applied only to plots created by pandas with DataFrame.plot() or Series.plot(). To have them apply to all plots, including those made by matplotlib, set the option pd.options.plotting.matplotlib.register_converters = True or use pandas.plotting.register_matplotlib_converters(). Suppressing tick resolution adjustment# pandas includes automatic tick resolution adjustment for regular frequency time-series data. For limited cases where pandas cannot infer the frequency information (e.g., in an externally created twinx), you can choose to suppress this behavior for alignment purposes. Here is the default behavior, notice how the x-axis tick labeling is performed: In [139]: plt.figure(); In [140]: df["A"].plot(); Using the x_compat parameter, you can suppress this behavior: In [141]: plt.figure(); In [142]: df["A"].plot(x_compat=True); If you have more than one plot that needs to be suppressed, the use method in pandas.plotting.plot_params can be used in a with statement: In [143]: plt.figure(); In [144]: with pd.plotting.plot_params.use("x_compat", True): .....: df["A"].plot(color="r") .....: df["B"].plot(color="g") .....: df["C"].plot(color="b") .....: Automatic date tick adjustment# TimedeltaIndex now uses the native matplotlib tick locator methods, it is useful to call the automatic date tick adjustment from matplotlib for figures whose ticklabels overlap. See the autofmt_xdate method and the matplotlib documentation for more. Subplots# Each Series in a DataFrame can be plotted on a different axis with the subplots keyword: In [145]: df.plot(subplots=True, figsize=(6, 6)); Using layout and targeting multiple axes# The layout of subplots can be specified by the layout keyword. It can accept (rows, columns). The layout keyword can be used in hist and boxplot also. If the input is invalid, a ValueError will be raised. The number of axes which can be contained by rows x columns specified by layout must be larger than the number of required subplots. If layout can contain more axes than required, blank axes are not drawn. Similar to a NumPy array’s reshape method, you can use -1 for one dimension to automatically calculate the number of rows or columns needed, given the other. In [146]: df.plot(subplots=True, layout=(2, 3), figsize=(6, 6), sharex=False); The above example is identical to using: In [147]: df.plot(subplots=True, layout=(2, -1), figsize=(6, 6), sharex=False); The required number of columns (3) is inferred from the number of series to plot and the given number of rows (2). You can pass multiple axes created beforehand as list-like via ax keyword. This allows more complicated layouts. The passed axes must be the same number as the subplots being drawn. When multiple axes are passed via the ax keyword, layout, sharex and sharey keywords don’t affect to the output. You should explicitly pass sharex=False and sharey=False, otherwise you will see a warning. In [148]: fig, axes = plt.subplots(4, 4, figsize=(9, 9)) In [149]: plt.subplots_adjust(wspace=0.5, hspace=0.5) In [150]: target1 = [axes[0][0], axes[1][1], axes[2][2], axes[3][3]] In [151]: target2 = [axes[3][0], axes[2][1], axes[1][2], axes[0][3]] In [152]: df.plot(subplots=True, ax=target1, legend=False, sharex=False, sharey=False); In [153]: (-df).plot(subplots=True, ax=target2, legend=False, sharex=False, sharey=False); Another option is passing an ax argument to Series.plot() to plot on a particular axis: In [154]: fig, axes = plt.subplots(nrows=2, ncols=2) In [155]: plt.subplots_adjust(wspace=0.2, hspace=0.5) In [156]: df["A"].plot(ax=axes[0, 0]); In [157]: axes[0, 0].set_title("A"); In [158]: df["B"].plot(ax=axes[0, 1]); In [159]: axes[0, 1].set_title("B"); In [160]: df["C"].plot(ax=axes[1, 0]); In [161]: axes[1, 0].set_title("C"); In [162]: df["D"].plot(ax=axes[1, 1]); In [163]: axes[1, 1].set_title("D"); Plotting with error bars# Plotting with error bars is supported in DataFrame.plot() and Series.plot(). Horizontal and vertical error bars can be supplied to the xerr and yerr keyword arguments to plot(). The error values can be specified using a variety of formats: As a DataFrame or dict of errors with column names matching the columns attribute of the plotting DataFrame or matching the name attribute of the Series. As a str indicating which of the columns of plotting DataFrame contain the error values. As raw values (list, tuple, or np.ndarray). Must be the same length as the plotting DataFrame/Series. Here is an example of one way to easily plot group means with standard deviations from the raw data. # Generate the data In [164]: ix3 = pd.MultiIndex.from_arrays( .....: [ .....: ["a", "a", "a", "a", "a", "b", "b", "b", "b", "b"], .....: ["foo", "foo", "foo", "bar", "bar", "foo", "foo", "bar", "bar", "bar"], .....: ], .....: names=["letter", "word"], .....: ) .....: In [165]: df3 = pd.DataFrame( .....: { .....: "data1": [9, 3, 2, 4, 3, 2, 4, 6, 3, 2], .....: "data2": [9, 6, 5, 7, 5, 4, 5, 6, 5, 1], .....: }, .....: index=ix3, .....: ) .....: # Group by index labels and take the means and standard deviations # for each group In [166]: gp3 = df3.groupby(level=("letter", "word")) In [167]: means = gp3.mean() In [168]: errors = gp3.std() In [169]: means Out[169]: data1 data2 letter word a bar 3.500000 6.000000 foo 4.666667 6.666667 b bar 3.666667 4.000000 foo 3.000000 4.500000 In [170]: errors Out[170]: data1 data2 letter word a bar 0.707107 1.414214 foo 3.785939 2.081666 b bar 2.081666 2.645751 foo 1.414214 0.707107 # Plot In [171]: fig, ax = plt.subplots() In [172]: means.plot.bar(yerr=errors, ax=ax, capsize=4, rot=0); Asymmetrical error bars are also supported, however raw error values must be provided in this case. For a N length Series, a 2xN array should be provided indicating lower and upper (or left and right) errors. For a MxN DataFrame, asymmetrical errors should be in a Mx2xN array. Here is an example of one way to plot the min/max range using asymmetrical error bars. In [173]: mins = gp3.min() In [174]: maxs = gp3.max() # errors should be positive, and defined in the order of lower, upper In [175]: errors = [[means[c] - mins[c], maxs[c] - means[c]] for c in df3.columns] # Plot In [176]: fig, ax = plt.subplots() In [177]: means.plot.bar(yerr=errors, ax=ax, capsize=4, rot=0); Plotting tables# Plotting with matplotlib table is now supported in DataFrame.plot() and Series.plot() with a table keyword. The table keyword can accept bool, DataFrame or Series. The simple way to draw a table is to specify table=True. Data will be transposed to meet matplotlib’s default layout. In [178]: fig, ax = plt.subplots(1, 1, figsize=(7, 6.5)) In [179]: df = pd.DataFrame(np.random.rand(5, 3), columns=["a", "b", "c"]) In [180]: ax.xaxis.tick_top() # Display x-axis ticks on top. In [181]: df.plot(table=True, ax=ax); Also, you can pass a different DataFrame or Series to the table keyword. The data will be drawn as displayed in print method (not transposed automatically). If required, it should be transposed manually as seen in the example below. In [182]: fig, ax = plt.subplots(1, 1, figsize=(7, 6.75)) In [183]: ax.xaxis.tick_top() # Display x-axis ticks on top. In [184]: df.plot(table=np.round(df.T, 2), ax=ax); There also exists a helper function pandas.plotting.table, which creates a table from DataFrame or Series, and adds it to an matplotlib.Axes instance. This function can accept keywords which the matplotlib table has. In [185]: from pandas.plotting import table In [186]: fig, ax = plt.subplots(1, 1) In [187]: table(ax, np.round(df.describe(), 2), loc="upper right", colWidths=[0.2, 0.2, 0.2]); In [188]: df.plot(ax=ax, ylim=(0, 2), legend=None); Note: You can get table instances on the axes using axes.tables property for further decorations. See the matplotlib table documentation for more. Colormaps# A potential issue when plotting a large number of columns is that it can be difficult to distinguish some series due to repetition in the default colors. To remedy this, DataFrame plotting supports the use of the colormap argument, which accepts either a Matplotlib colormap or a string that is a name of a colormap registered with Matplotlib. A visualization of the default matplotlib colormaps is available here. As matplotlib does not directly support colormaps for line-based plots, the colors are selected based on an even spacing determined by the number of columns in the DataFrame. There is no consideration made for background color, so some colormaps will produce lines that are not easily visible. To use the cubehelix colormap, we can pass colormap='cubehelix'. In [189]: df = pd.DataFrame(np.random.randn(1000, 10), index=ts.index) In [190]: df = df.cumsum() In [191]: plt.figure(); In [192]: df.plot(colormap="cubehelix"); Alternatively, we can pass the colormap itself: In [193]: from matplotlib import cm In [194]: plt.figure(); In [195]: df.plot(colormap=cm.cubehelix); Colormaps can also be used other plot types, like bar charts: In [196]: dd = pd.DataFrame(np.random.randn(10, 10)).applymap(abs) In [197]: dd = dd.cumsum() In [198]: plt.figure(); In [199]: dd.plot.bar(colormap="Greens"); Parallel coordinates charts: In [200]: plt.figure(); In [201]: parallel_coordinates(data, "Name", colormap="gist_rainbow"); Andrews curves charts: In [202]: plt.figure(); In [203]: andrews_curves(data, "Name", colormap="winter"); Plotting directly with Matplotlib# In some situations it may still be preferable or necessary to prepare plots directly with matplotlib, for instance when a certain type of plot or customization is not (yet) supported by pandas. Series and DataFrame objects behave like arrays and can therefore be passed directly to matplotlib functions without explicit casts. pandas also automatically registers formatters and locators that recognize date indices, thereby extending date and time support to practically all plot types available in matplotlib. Although this formatting does not provide the same level of refinement you would get when plotting via pandas, it can be faster when plotting a large number of points. In [204]: price = pd.Series( .....: np.random.randn(150).cumsum(), .....: index=pd.date_range("2000-1-1", periods=150, freq="B"), .....: ) .....: In [205]: ma = price.rolling(20).mean() In [206]: mstd = price.rolling(20).std() In [207]: plt.figure(); In [208]: plt.plot(price.index, price, "k"); In [209]: plt.plot(ma.index, ma, "b"); In [210]: plt.fill_between(mstd.index, ma - 2 * mstd, ma + 2 * mstd, color="b", alpha=0.2); Plotting backends# Starting in version 0.25, pandas can be extended with third-party plotting backends. The main idea is letting users select a plotting backend different than the provided one based on Matplotlib. This can be done by passing ‘backend.module’ as the argument backend in plot function. For example: >>> Series([1, 2, 3]).plot(backend="backend.module") Alternatively, you can also set this option globally, do you don’t need to specify the keyword in each plot call. For example: >>> pd.set_option("plotting.backend", "backend.module") >>> pd.Series([1, 2, 3]).plot() Or: >>> pd.options.plotting.backend = "backend.module" >>> pd.Series([1, 2, 3]).plot() This would be more or less equivalent to: >>> import backend.module >>> backend.module.plot(pd.Series([1, 2, 3])) The backend module can then use other visualization tools (Bokeh, Altair, hvplot,…) to generate the plots. Some libraries implementing a backend for pandas are listed on the ecosystem Visualization page. Developers guide can be found at https://pandas.pydata.org/docs/dev/development/extending.html#plotting-backends
464
632
How to show column data that contains keyword in a dataframe I have a dataframe that has one column called 'job_title', there are many job titles, I want to show all job title contains 'Professor' I have the following code: sunshin_df[sunshin_df['job_title'].isin(['Professor']) But this only show title "Professor", other titles such as "Associate Professor" are not included Then I tried to use .str.contains, but it doesn't work, AttributeError: 'list' object has no attribute 'str' sunshin_df[sunshin_df['job_title'].isin(['Professor'].str.contains('Professor', na=False, case=False))] What is the best and simplest way to show all value that contains the keyword I want?
You should try following : sunshin_df[sunshin_df['job_title'].str.contains('Professor')] i.e check the value of the job_title column if it contains 'Professor' in it.
62,454,962
Error on import pandas python - DLL cannot be found
<p>I cannot compile script with pandas library. My code is simple <code>import pandas as pd</code>. I get error message like this: </p> <pre><code>Traceback (most recent call last): File "C:\Users\Valters_Kalme\Desktop\Python\var dzēst\k.py", line 4, in &lt;module&gt; import pandas as pd File "C:\Users\Valters_Kalme\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\__init__.py", line 55, in &lt;module&gt; from pandas.core.api import ( File "C:\Users\Valters_Kalme\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\api.py", line 29, in &lt;module&gt; from pandas.core.groupby import Grouper, NamedAgg File "C:\Users\Valters_Kalme\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\__init__.py", line 1, in &lt;module&gt; from pandas.core.groupby.generic import DataFrameGroupBy, NamedAgg, SeriesGroupBy File "C:\Users\Valters_Kalme\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\generic.py", line 60, in &lt;module&gt; from pandas.core.frame import DataFrame File "C:\Users\Valters_Kalme\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\frame.py", line 124, in &lt;module&gt; from pandas.core.series import Series File "C:\Users\Valters_Kalme\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\series.py", line 4572, in &lt;module&gt; Series._add_series_or_dataframe_operations() File "C:\Users\Valters_Kalme\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\generic.py", line 10349, in _add_series_or_dataframe_operations from pandas.core.window import EWM, Expanding, Rolling, Window File "C:\Users\Valters_Kalme\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\window\__init__.py", line 1, in &lt;module&gt; from pandas.core.window.ewm import EWM # noqa:F401 File "C:\Users\Valters_Kalme\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\window\ewm.py", line 5, in &lt;module&gt; import pandas._libs.window.aggregations as window_aggregations ImportError: DLL load failed while importing aggregations: The specified module could not be found. </code></pre> <p>I believe key word is <code>DLL load failed</code>. I cannot install pandas on this Windows PC with Anaconda as this is enterprise PC and Anaconda is free only for individual use. So I installed pandas with <code>pip install pandas</code>. When I run it again it gives back message: </p> <pre><code>Requirement already satisfied: pandas in c:\users\valters_kalme\appdata\local\programs\python\python38\lib\site-packages (1.0.5) Requirement already satisfied: python-dateutil&gt;=2.6.1 in c:\users\valters_kalme\appdata\local\programs\python\python38\lib\site-packages (from pandas) (2.8.1) Requirement already satisfied: numpy&gt;=1.13.3 in c:\users\valters_kalme\appdata\local\programs\python\python38\lib\site-packages (from pandas) (1.18.5) Requirement already satisfied: pytz&gt;=2017.2 in c:\users\valters_kalme\appdata\local\programs\python\python38\lib\site-packages (from pandas) (2020.1) Requirement already satisfied: six&gt;=1.5 in c:\users\valters_kalme\appdata\local\programs\python\python38\lib\site-packages (from python-dateutil&gt;=2.6.1-&gt;pandas) (1.15.0) </code></pre> <p>What possible could be wrong? Thank you!</p>
62,455,080
"2020-06-18T16:36:45.887000"
1
null
1
502
python|pandas
<p>Please try uninstalling pandas and installing it again: </p> <pre><code>pip uninstall pandas pip install pandas==1.0.1 </code></pre> <p>changing the version of pandas according to you python version could help, you could also try to uninstall or update pandas and numpy. </p>
"2020-06-18T16:42:40.510000"
0
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_gbq.html
pandas.DataFrame.to_gbq# pandas.DataFrame.to_gbq# DataFrame.to_gbq(destination_table, project_id=None, chunksize=None, reauth=False, if_exists='fail', auth_local_webserver=True, table_schema=None, location=None, progress_bar=True, credentials=None)[source]# Write a DataFrame to a Google BigQuery table. This function requires the pandas-gbq package. See the How to authenticate with Google BigQuery guide for authentication instructions. Parameters destination_tablestrName of table to be written, in the form dataset.tablename. project_idstr, optionalGoogle BigQuery Account project ID. Optional when available from the environment. chunksizeint, optionalNumber of rows to be inserted in each chunk from the dataframe. Set to None to load the whole dataframe at once. reauthbool, default FalseForce Google BigQuery to re-authenticate the user. This is useful Please try uninstalling pandas and installing it again: pip uninstall pandas pip install pandas==1.0.1 changing the version of pandas according to you python version could help, you could also try to uninstall or update pandas and numpy. if multiple accounts are used. if_existsstr, default ‘fail’Behavior when the destination table exists. Value can be one of: 'fail'If table exists raise pandas_gbq.gbq.TableCreationError. 'replace'If table exists, drop it, recreate it, and insert data. 'append'If table exists, insert data. Create if does not exist. auth_local_webserverbool, default TrueUse the local webserver flow instead of the console flow when getting user credentials. New in version 0.2.0 of pandas-gbq. Changed in version 1.5.0: Default value is changed to True. Google has deprecated the auth_local_webserver = False “out of band” (copy-paste) flow. table_schemalist of dicts, optionalList of BigQuery table fields to which according DataFrame columns conform to, e.g. [{'name': 'col1', 'type': 'STRING'},...]. If schema is not provided, it will be generated according to dtypes of DataFrame columns. See BigQuery API documentation on available names of a field. New in version 0.3.1 of pandas-gbq. locationstr, optionalLocation where the load job should run. See the BigQuery locations documentation for a list of available locations. The location must match that of the target dataset. New in version 0.5.0 of pandas-gbq. progress_barbool, default TrueUse the library tqdm to show the progress bar for the upload, chunk by chunk. New in version 0.5.0 of pandas-gbq. credentialsgoogle.auth.credentials.Credentials, optionalCredentials for accessing Google APIs. Use this parameter to override default credentials, such as to use Compute Engine google.auth.compute_engine.Credentials or Service Account google.oauth2.service_account.Credentials directly. New in version 0.8.0 of pandas-gbq. See also pandas_gbq.to_gbqThis function in the pandas-gbq library. read_gbqRead a DataFrame from Google BigQuery.
870
1,110
Error on import pandas python - DLL cannot be found I cannot compile script with pandas library. My code is simple import pandas as pd. I get error message like this: Traceback (most recent call last): File "C:\Users\Valters_Kalme\Desktop\Python\var dzēst\k.py", line 4, in <module> import pandas as pd File "C:\Users\Valters_Kalme\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\__init__.py", line 55, in <module> from pandas.core.api import ( File "C:\Users\Valters_Kalme\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\api.py", line 29, in <module> from pandas.core.groupby import Grouper, NamedAgg File "C:\Users\Valters_Kalme\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\__init__.py", line 1, in <module> from pandas.core.groupby.generic import DataFrameGroupBy, NamedAgg, SeriesGroupBy File "C:\Users\Valters_Kalme\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\groupby\generic.py", line 60, in <module> from pandas.core.frame import DataFrame File "C:\Users\Valters_Kalme\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\frame.py", line 124, in <module> from pandas.core.series import Series File "C:\Users\Valters_Kalme\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\series.py", line 4572, in <module> Series._add_series_or_dataframe_operations() File "C:\Users\Valters_Kalme\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\generic.py", line 10349, in _add_series_or_dataframe_operations from pandas.core.window import EWM, Expanding, Rolling, Window File "C:\Users\Valters_Kalme\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\window\__init__.py", line 1, in <module> from pandas.core.window.ewm import EWM # noqa:F401 File "C:\Users\Valters_Kalme\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\window\ewm.py", line 5, in <module> import pandas._libs.window.aggregations as window_aggregations ImportError: DLL load failed while importing aggregations: The specified module could not be found. I believe key word is DLL load failed. I cannot install pandas on this Windows PC with Anaconda as this is enterprise PC and Anaconda is free only for individual use. So I installed pandas with pip install pandas. When I run it again it gives back message: Requirement already satisfied: pandas in c:\users\valters_kalme\appdata\local\programs\python\python38\lib\site-packages (1.0.5) Requirement already satisfied: python-dateutil>=2.6.1 in c:\users\valters_kalme\appdata\local\programs\python\python38\lib\site-packages (from pandas) (2.8.1) Requirement already satisfied: numpy>=1.13.3 in c:\users\valters_kalme\appdata\local\programs\python\python38\lib\site-packages (from pandas) (1.18.5) Requirement already satisfied: pytz>=2017.2 in c:\users\valters_kalme\appdata\local\programs\python\python38\lib\site-packages (from pandas) (2020.1) Requirement already satisfied: six>=1.5 in c:\users\valters_kalme\appdata\local\programs\python\python38\lib\site-packages (from python-dateutil>=2.6.1->pandas) (1.15.0) What possible could be wrong? Thank you!
Please try uninstalling pandas and installing it again: pip uninstall pandas pip install pandas==1.0.1 changing the version of pandas according to you python version could help, you could also try to uninstall or update pandas and numpy.
62,584,567
Merge row of two different dataframe using a column as pattern matching
<p>I have two dataframes.</p> <pre><code>df = pd.DataFrame([['klf1', 10], ['sp2', 3], ['klf3', 12], ['egr1', 5], ['klf11', 2]], columns=['tf','count']) df2 = pd.DataFrame([['Homer-sp2', 0.01], ['Homer-klf1', 0.0001], ['Homer-klf3-chip', 0.05], ['klf11',0.002], ['Homer-egr1', 0.01]], columns=['Motif_name','p_val']) </code></pre> <p>I'm trying to merge them using df['tf'] content.</p> <p>For exemple 'klf1' row should be 'klf1' 10 0.0001</p> <p>So I have to use the content of df['tf'] as pattern matching in df2[Motif_name]</p> <p>I'm expecting to get this df</p> <pre><code>df_final=pd.DataFrame([['Klf1', 10,0.0001], ['sp2', 3, 0.01], ['klf3', 12,0.05], ['egr1', 5,0.01], ['klf11', 2,0.002]], columns=['tf','count','p_val']) </code></pre> <p>I tried to use .str.contains('pattern') but it works only for one pattern. Here I don't know how can I loop on the content of the column tf.</p> <pre><code>df2['Motif_name'].str.contains(df['tf'].str.lower()) </code></pre> <p>Also .str.contains will look for pattern and I know that I'll have a problem on klf1 VS klf11 because klf1 is in klf11. How can I manage this problem?</p>
62,585,694
"2020-06-25T21:25:12.203000"
3
1
3
265
python|pandas
<p>Just to fix your approach:</p> <pre class="lang-py prettyprint-override"><code>import re res=df.assign(key=1).merge(df2.assign(key=1), on='key').drop('key', axis=1) res=res.loc[map(lambda x: True if re.search(*x) else False, zip(res['tf'].str.lower()+r'($|[^\d])', res['Motif_name']))] </code></pre> <p>Outputs:</p> <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; res tf count Motif_name p_val 1 klf1 10 Homer-klf1 0.0001 5 klf2 3 Homer-klf2 0.0100 12 klf3 12 Homer-klf3-chip 0.0500 19 klf9 5 Homer-klf9 0.0100 23 klf11 2 klf11 0.0020 </code></pre>
"2020-06-25T23:18:40.300000"
1
https://pandas.pydata.org/docs/user_guide/merging.html
Merge, join, concatenate and compare# Merge, join, concatenate and compare# pandas provides various facilities for easily combining together Series or DataFrame with various kinds of set logic for the indexes Just to fix your approach: import re res=df.assign(key=1).merge(df2.assign(key=1), on='key').drop('key', axis=1) res=res.loc[map(lambda x: True if re.search(*x) else False, zip(res['tf'].str.lower()+r'($|[^\d])', res['Motif_name']))] Outputs: >>> res tf count Motif_name p_val 1 klf1 10 Homer-klf1 0.0001 5 klf2 3 Homer-klf2 0.0100 12 klf3 12 Homer-klf3-chip 0.0500 19 klf9 5 Homer-klf9 0.0100 23 klf11 2 klf11 0.0020 and relational algebra functionality in the case of join / merge-type operations. In addition, pandas also provides utilities to compare two Series or DataFrame and summarize their differences. Concatenating objects# The concat() function (in the main pandas namespace) does all of the heavy lifting of performing concatenation operations along an axis while performing optional set logic (union or intersection) of the indexes (if any) on the other axes. Note that I say “if any” because there is only a single possible axis of concatenation for Series. Before diving into all of the details of concat and what it can do, here is a simple example: In [1]: df1 = pd.DataFrame( ...: { ...: "A": ["A0", "A1", "A2", "A3"], ...: "B": ["B0", "B1", "B2", "B3"], ...: "C": ["C0", "C1", "C2", "C3"], ...: "D": ["D0", "D1", "D2", "D3"], ...: }, ...: index=[0, 1, 2, 3], ...: ) ...: In [2]: df2 = pd.DataFrame( ...: { ...: "A": ["A4", "A5", "A6", "A7"], ...: "B": ["B4", "B5", "B6", "B7"], ...: "C": ["C4", "C5", "C6", "C7"], ...: "D": ["D4", "D5", "D6", "D7"], ...: }, ...: index=[4, 5, 6, 7], ...: ) ...: In [3]: df3 = pd.DataFrame( ...: { ...: "A": ["A8", "A9", "A10", "A11"], ...: "B": ["B8", "B9", "B10", "B11"], ...: "C": ["C8", "C9", "C10", "C11"], ...: "D": ["D8", "D9", "D10", "D11"], ...: }, ...: index=[8, 9, 10, 11], ...: ) ...: In [4]: frames = [df1, df2, df3] In [5]: result = pd.concat(frames) Like its sibling function on ndarrays, numpy.concatenate, pandas.concat takes a list or dict of homogeneously-typed objects and concatenates them with some configurable handling of “what to do with the other axes”: pd.concat( objs, axis=0, join="outer", ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, copy=True, ) objs : a sequence or mapping of Series or DataFrame objects. If a dict is passed, the sorted keys will be used as the keys argument, unless it is passed, in which case the values will be selected (see below). Any None objects will be dropped silently unless they are all None in which case a ValueError will be raised. axis : {0, 1, …}, default 0. The axis to concatenate along. join : {‘inner’, ‘outer’}, default ‘outer’. How to handle indexes on other axis(es). Outer for union and inner for intersection. ignore_index : boolean, default False. If True, do not use the index values on the concatenation axis. The resulting axis will be labeled 0, …, n - 1. This is useful if you are concatenating objects where the concatenation axis does not have meaningful indexing information. Note the index values on the other axes are still respected in the join. keys : sequence, default None. Construct hierarchical index using the passed keys as the outermost level. If multiple levels passed, should contain tuples. levels : list of sequences, default None. Specific levels (unique values) to use for constructing a MultiIndex. Otherwise they will be inferred from the keys. names : list, default None. Names for the levels in the resulting hierarchical index. verify_integrity : boolean, default False. Check whether the new concatenated axis contains duplicates. This can be very expensive relative to the actual data concatenation. copy : boolean, default True. If False, do not copy data unnecessarily. Without a little bit of context many of these arguments don’t make much sense. Let’s revisit the above example. Suppose we wanted to associate specific keys with each of the pieces of the chopped up DataFrame. We can do this using the keys argument: In [6]: result = pd.concat(frames, keys=["x", "y", "z"]) As you can see (if you’ve read the rest of the documentation), the resulting object’s index has a hierarchical index. This means that we can now select out each chunk by key: In [7]: result.loc["y"] Out[7]: A B C D 4 A4 B4 C4 D4 5 A5 B5 C5 D5 6 A6 B6 C6 D6 7 A7 B7 C7 D7 It’s not a stretch to see how this can be very useful. More detail on this functionality below. Note It is worth noting that concat() (and therefore append()) makes a full copy of the data, and that constantly reusing this function can create a significant performance hit. If you need to use the operation over several datasets, use a list comprehension. frames = [ process_your_file(f) for f in files ] result = pd.concat(frames) Note When concatenating DataFrames with named axes, pandas will attempt to preserve these index/column names whenever possible. In the case where all inputs share a common name, this name will be assigned to the result. When the input names do not all agree, the result will be unnamed. The same is true for MultiIndex, but the logic is applied separately on a level-by-level basis. Set logic on the other axes# When gluing together multiple DataFrames, you have a choice of how to handle the other axes (other than the one being concatenated). This can be done in the following two ways: Take the union of them all, join='outer'. This is the default option as it results in zero information loss. Take the intersection, join='inner'. Here is an example of each of these methods. First, the default join='outer' behavior: In [8]: df4 = pd.DataFrame( ...: { ...: "B": ["B2", "B3", "B6", "B7"], ...: "D": ["D2", "D3", "D6", "D7"], ...: "F": ["F2", "F3", "F6", "F7"], ...: }, ...: index=[2, 3, 6, 7], ...: ) ...: In [9]: result = pd.concat([df1, df4], axis=1) Here is the same thing with join='inner': In [10]: result = pd.concat([df1, df4], axis=1, join="inner") Lastly, suppose we just wanted to reuse the exact index from the original DataFrame: In [11]: result = pd.concat([df1, df4], axis=1).reindex(df1.index) Similarly, we could index before the concatenation: In [12]: pd.concat([df1, df4.reindex(df1.index)], axis=1) Out[12]: A B C D B D F 0 A0 B0 C0 D0 NaN NaN NaN 1 A1 B1 C1 D1 NaN NaN NaN 2 A2 B2 C2 D2 B2 D2 F2 3 A3 B3 C3 D3 B3 D3 F3 Ignoring indexes on the concatenation axis# For DataFrame objects which don’t have a meaningful index, you may wish to append them and ignore the fact that they may have overlapping indexes. To do this, use the ignore_index argument: In [13]: result = pd.concat([df1, df4], ignore_index=True, sort=False) Concatenating with mixed ndims# You can concatenate a mix of Series and DataFrame objects. The Series will be transformed to DataFrame with the column name as the name of the Series. In [14]: s1 = pd.Series(["X0", "X1", "X2", "X3"], name="X") In [15]: result = pd.concat([df1, s1], axis=1) Note Since we’re concatenating a Series to a DataFrame, we could have achieved the same result with DataFrame.assign(). To concatenate an arbitrary number of pandas objects (DataFrame or Series), use concat. If unnamed Series are passed they will be numbered consecutively. In [16]: s2 = pd.Series(["_0", "_1", "_2", "_3"]) In [17]: result = pd.concat([df1, s2, s2, s2], axis=1) Passing ignore_index=True will drop all name references. In [18]: result = pd.concat([df1, s1], axis=1, ignore_index=True) More concatenating with group keys# A fairly common use of the keys argument is to override the column names when creating a new DataFrame based on existing Series. Notice how the default behaviour consists on letting the resulting DataFrame inherit the parent Series’ name, when these existed. In [19]: s3 = pd.Series([0, 1, 2, 3], name="foo") In [20]: s4 = pd.Series([0, 1, 2, 3]) In [21]: s5 = pd.Series([0, 1, 4, 5]) In [22]: pd.concat([s3, s4, s5], axis=1) Out[22]: foo 0 1 0 0 0 0 1 1 1 1 2 2 2 4 3 3 3 5 Through the keys argument we can override the existing column names. In [23]: pd.concat([s3, s4, s5], axis=1, keys=["red", "blue", "yellow"]) Out[23]: red blue yellow 0 0 0 0 1 1 1 1 2 2 2 4 3 3 3 5 Let’s consider a variation of the very first example presented: In [24]: result = pd.concat(frames, keys=["x", "y", "z"]) You can also pass a dict to concat in which case the dict keys will be used for the keys argument (unless other keys are specified): In [25]: pieces = {"x": df1, "y": df2, "z": df3} In [26]: result = pd.concat(pieces) In [27]: result = pd.concat(pieces, keys=["z", "y"]) The MultiIndex created has levels that are constructed from the passed keys and the index of the DataFrame pieces: In [28]: result.index.levels Out[28]: FrozenList([['z', 'y'], [4, 5, 6, 7, 8, 9, 10, 11]]) If you wish to specify other levels (as will occasionally be the case), you can do so using the levels argument: In [29]: result = pd.concat( ....: pieces, keys=["x", "y", "z"], levels=[["z", "y", "x", "w"]], names=["group_key"] ....: ) ....: In [30]: result.index.levels Out[30]: FrozenList([['z', 'y', 'x', 'w'], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]) This is fairly esoteric, but it is actually necessary for implementing things like GroupBy where the order of a categorical variable is meaningful. Appending rows to a DataFrame# If you have a series that you want to append as a single row to a DataFrame, you can convert the row into a DataFrame and use concat In [31]: s2 = pd.Series(["X0", "X1", "X2", "X3"], index=["A", "B", "C", "D"]) In [32]: result = pd.concat([df1, s2.to_frame().T], ignore_index=True) You should use ignore_index with this method to instruct DataFrame to discard its index. If you wish to preserve the index, you should construct an appropriately-indexed DataFrame and append or concatenate those objects. Database-style DataFrame or named Series joining/merging# pandas has full-featured, high performance in-memory join operations idiomatically very similar to relational databases like SQL. These methods perform significantly better (in some cases well over an order of magnitude better) than other open source implementations (like base::merge.data.frame in R). The reason for this is careful algorithmic design and the internal layout of the data in DataFrame. See the cookbook for some advanced strategies. Users who are familiar with SQL but new to pandas might be interested in a comparison with SQL. pandas provides a single function, merge(), as the entry point for all standard database join operations between DataFrame or named Series objects: pd.merge( left, right, how="inner", on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=True, suffixes=("_x", "_y"), copy=True, indicator=False, validate=None, ) left: A DataFrame or named Series object. right: Another DataFrame or named Series object. on: Column or index level names to join on. Must be found in both the left and right DataFrame and/or Series objects. If not passed and left_index and right_index are False, the intersection of the columns in the DataFrames and/or Series will be inferred to be the join keys. left_on: Columns or index levels from the left DataFrame or Series to use as keys. Can either be column names, index level names, or arrays with length equal to the length of the DataFrame or Series. right_on: Columns or index levels from the right DataFrame or Series to use as keys. Can either be column names, index level names, or arrays with length equal to the length of the DataFrame or Series. left_index: If True, use the index (row labels) from the left DataFrame or Series as its join key(s). In the case of a DataFrame or Series with a MultiIndex (hierarchical), the number of levels must match the number of join keys from the right DataFrame or Series. right_index: Same usage as left_index for the right DataFrame or Series how: One of 'left', 'right', 'outer', 'inner', 'cross'. Defaults to inner. See below for more detailed description of each method. sort: Sort the result DataFrame by the join keys in lexicographical order. Defaults to True, setting to False will improve performance substantially in many cases. suffixes: A tuple of string suffixes to apply to overlapping columns. Defaults to ('_x', '_y'). copy: Always copy data (default True) from the passed DataFrame or named Series objects, even when reindexing is not necessary. Cannot be avoided in many cases but may improve performance / memory usage. The cases where copying can be avoided are somewhat pathological but this option is provided nonetheless. indicator: Add a column to the output DataFrame called _merge with information on the source of each row. _merge is Categorical-type and takes on a value of left_only for observations whose merge key only appears in 'left' DataFrame or Series, right_only for observations whose merge key only appears in 'right' DataFrame or Series, and both if the observation’s merge key is found in both. validate : string, default None. If specified, checks if merge is of specified type. “one_to_one” or “1:1”: checks if merge keys are unique in both left and right datasets. “one_to_many” or “1:m”: checks if merge keys are unique in left dataset. “many_to_one” or “m:1”: checks if merge keys are unique in right dataset. “many_to_many” or “m:m”: allowed, but does not result in checks. Note Support for specifying index levels as the on, left_on, and right_on parameters was added in version 0.23.0. Support for merging named Series objects was added in version 0.24.0. The return type will be the same as left. If left is a DataFrame or named Series and right is a subclass of DataFrame, the return type will still be DataFrame. merge is a function in the pandas namespace, and it is also available as a DataFrame instance method merge(), with the calling DataFrame being implicitly considered the left object in the join. The related join() method, uses merge internally for the index-on-index (by default) and column(s)-on-index join. If you are joining on index only, you may wish to use DataFrame.join to save yourself some typing. Brief primer on merge methods (relational algebra)# Experienced users of relational databases like SQL will be familiar with the terminology used to describe join operations between two SQL-table like structures (DataFrame objects). There are several cases to consider which are very important to understand: one-to-one joins: for example when joining two DataFrame objects on their indexes (which must contain unique values). many-to-one joins: for example when joining an index (unique) to one or more columns in a different DataFrame. many-to-many joins: joining columns on columns. Note When joining columns on columns (potentially a many-to-many join), any indexes on the passed DataFrame objects will be discarded. It is worth spending some time understanding the result of the many-to-many join case. In SQL / standard relational algebra, if a key combination appears more than once in both tables, the resulting table will have the Cartesian product of the associated data. Here is a very basic example with one unique key combination: In [33]: left = pd.DataFrame( ....: { ....: "key": ["K0", "K1", "K2", "K3"], ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: } ....: ) ....: In [34]: right = pd.DataFrame( ....: { ....: "key": ["K0", "K1", "K2", "K3"], ....: "C": ["C0", "C1", "C2", "C3"], ....: "D": ["D0", "D1", "D2", "D3"], ....: } ....: ) ....: In [35]: result = pd.merge(left, right, on="key") Here is a more complicated example with multiple join keys. Only the keys appearing in left and right are present (the intersection), since how='inner' by default. In [36]: left = pd.DataFrame( ....: { ....: "key1": ["K0", "K0", "K1", "K2"], ....: "key2": ["K0", "K1", "K0", "K1"], ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: } ....: ) ....: In [37]: right = pd.DataFrame( ....: { ....: "key1": ["K0", "K1", "K1", "K2"], ....: "key2": ["K0", "K0", "K0", "K0"], ....: "C": ["C0", "C1", "C2", "C3"], ....: "D": ["D0", "D1", "D2", "D3"], ....: } ....: ) ....: In [38]: result = pd.merge(left, right, on=["key1", "key2"]) The how argument to merge specifies how to determine which keys are to be included in the resulting table. If a key combination does not appear in either the left or right tables, the values in the joined table will be NA. Here is a summary of the how options and their SQL equivalent names: Merge method SQL Join Name Description left LEFT OUTER JOIN Use keys from left frame only right RIGHT OUTER JOIN Use keys from right frame only outer FULL OUTER JOIN Use union of keys from both frames inner INNER JOIN Use intersection of keys from both frames cross CROSS JOIN Create the cartesian product of rows of both frames In [39]: result = pd.merge(left, right, how="left", on=["key1", "key2"]) In [40]: result = pd.merge(left, right, how="right", on=["key1", "key2"]) In [41]: result = pd.merge(left, right, how="outer", on=["key1", "key2"]) In [42]: result = pd.merge(left, right, how="inner", on=["key1", "key2"]) In [43]: result = pd.merge(left, right, how="cross") You can merge a mult-indexed Series and a DataFrame, if the names of the MultiIndex correspond to the columns from the DataFrame. Transform the Series to a DataFrame using Series.reset_index() before merging, as shown in the following example. In [44]: df = pd.DataFrame({"Let": ["A", "B", "C"], "Num": [1, 2, 3]}) In [45]: df Out[45]: Let Num 0 A 1 1 B 2 2 C 3 In [46]: ser = pd.Series( ....: ["a", "b", "c", "d", "e", "f"], ....: index=pd.MultiIndex.from_arrays( ....: [["A", "B", "C"] * 2, [1, 2, 3, 4, 5, 6]], names=["Let", "Num"] ....: ), ....: ) ....: In [47]: ser Out[47]: Let Num A 1 a B 2 b C 3 c A 4 d B 5 e C 6 f dtype: object In [48]: pd.merge(df, ser.reset_index(), on=["Let", "Num"]) Out[48]: Let Num 0 0 A 1 a 1 B 2 b 2 C 3 c Here is another example with duplicate join keys in DataFrames: In [49]: left = pd.DataFrame({"A": [1, 2], "B": [2, 2]}) In [50]: right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]}) In [51]: result = pd.merge(left, right, on="B", how="outer") Warning Joining / merging on duplicate keys can cause a returned frame that is the multiplication of the row dimensions, which may result in memory overflow. It is the user’ s responsibility to manage duplicate values in keys before joining large DataFrames. Checking for duplicate keys# Users can use the validate argument to automatically check whether there are unexpected duplicates in their merge keys. Key uniqueness is checked before merge operations and so should protect against memory overflows. Checking key uniqueness is also a good way to ensure user data structures are as expected. In the following example, there are duplicate values of B in the right DataFrame. As this is not a one-to-one merge – as specified in the validate argument – an exception will be raised. In [52]: left = pd.DataFrame({"A": [1, 2], "B": [1, 2]}) In [53]: right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]}) In [53]: result = pd.merge(left, right, on="B", how="outer", validate="one_to_one") ... MergeError: Merge keys are not unique in right dataset; not a one-to-one merge If the user is aware of the duplicates in the right DataFrame but wants to ensure there are no duplicates in the left DataFrame, one can use the validate='one_to_many' argument instead, which will not raise an exception. In [54]: pd.merge(left, right, on="B", how="outer", validate="one_to_many") Out[54]: A_x B A_y 0 1 1 NaN 1 2 2 4.0 2 2 2 5.0 3 2 2 6.0 The merge indicator# merge() accepts the argument indicator. If True, a Categorical-type column called _merge will be added to the output object that takes on values: Observation Origin _merge value Merge key only in 'left' frame left_only Merge key only in 'right' frame right_only Merge key in both frames both In [55]: df1 = pd.DataFrame({"col1": [0, 1], "col_left": ["a", "b"]}) In [56]: df2 = pd.DataFrame({"col1": [1, 2, 2], "col_right": [2, 2, 2]}) In [57]: pd.merge(df1, df2, on="col1", how="outer", indicator=True) Out[57]: col1 col_left col_right _merge 0 0 a NaN left_only 1 1 b 2.0 both 2 2 NaN 2.0 right_only 3 2 NaN 2.0 right_only The indicator argument will also accept string arguments, in which case the indicator function will use the value of the passed string as the name for the indicator column. In [58]: pd.merge(df1, df2, on="col1", how="outer", indicator="indicator_column") Out[58]: col1 col_left col_right indicator_column 0 0 a NaN left_only 1 1 b 2.0 both 2 2 NaN 2.0 right_only 3 2 NaN 2.0 right_only Merge dtypes# Merging will preserve the dtype of the join keys. In [59]: left = pd.DataFrame({"key": [1], "v1": [10]}) In [60]: left Out[60]: key v1 0 1 10 In [61]: right = pd.DataFrame({"key": [1, 2], "v1": [20, 30]}) In [62]: right Out[62]: key v1 0 1 20 1 2 30 We are able to preserve the join keys: In [63]: pd.merge(left, right, how="outer") Out[63]: key v1 0 1 10 1 1 20 2 2 30 In [64]: pd.merge(left, right, how="outer").dtypes Out[64]: key int64 v1 int64 dtype: object Of course if you have missing values that are introduced, then the resulting dtype will be upcast. In [65]: pd.merge(left, right, how="outer", on="key") Out[65]: key v1_x v1_y 0 1 10.0 20 1 2 NaN 30 In [66]: pd.merge(left, right, how="outer", on="key").dtypes Out[66]: key int64 v1_x float64 v1_y int64 dtype: object Merging will preserve category dtypes of the mergands. See also the section on categoricals. The left frame. In [67]: from pandas.api.types import CategoricalDtype In [68]: X = pd.Series(np.random.choice(["foo", "bar"], size=(10,))) In [69]: X = X.astype(CategoricalDtype(categories=["foo", "bar"])) In [70]: left = pd.DataFrame( ....: {"X": X, "Y": np.random.choice(["one", "two", "three"], size=(10,))} ....: ) ....: In [71]: left Out[71]: X Y 0 bar one 1 foo one 2 foo three 3 bar three 4 foo one 5 bar one 6 bar three 7 bar three 8 bar three 9 foo three In [72]: left.dtypes Out[72]: X category Y object dtype: object The right frame. In [73]: right = pd.DataFrame( ....: { ....: "X": pd.Series(["foo", "bar"], dtype=CategoricalDtype(["foo", "bar"])), ....: "Z": [1, 2], ....: } ....: ) ....: In [74]: right Out[74]: X Z 0 foo 1 1 bar 2 In [75]: right.dtypes Out[75]: X category Z int64 dtype: object The merged result: In [76]: result = pd.merge(left, right, how="outer") In [77]: result Out[77]: X Y Z 0 bar one 2 1 bar three 2 2 bar one 2 3 bar three 2 4 bar three 2 5 bar three 2 6 foo one 1 7 foo three 1 8 foo one 1 9 foo three 1 In [78]: result.dtypes Out[78]: X category Y object Z int64 dtype: object Note The category dtypes must be exactly the same, meaning the same categories and the ordered attribute. Otherwise the result will coerce to the categories’ dtype. Note Merging on category dtypes that are the same can be quite performant compared to object dtype merging. Joining on index# DataFrame.join() is a convenient method for combining the columns of two potentially differently-indexed DataFrames into a single result DataFrame. Here is a very basic example: In [79]: left = pd.DataFrame( ....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=["K0", "K1", "K2"] ....: ) ....: In [80]: right = pd.DataFrame( ....: {"C": ["C0", "C2", "C3"], "D": ["D0", "D2", "D3"]}, index=["K0", "K2", "K3"] ....: ) ....: In [81]: result = left.join(right) In [82]: result = left.join(right, how="outer") The same as above, but with how='inner'. In [83]: result = left.join(right, how="inner") The data alignment here is on the indexes (row labels). This same behavior can be achieved using merge plus additional arguments instructing it to use the indexes: In [84]: result = pd.merge(left, right, left_index=True, right_index=True, how="outer") In [85]: result = pd.merge(left, right, left_index=True, right_index=True, how="inner") Joining key columns on an index# join() takes an optional on argument which may be a column or multiple column names, which specifies that the passed DataFrame is to be aligned on that column in the DataFrame. These two function calls are completely equivalent: left.join(right, on=key_or_keys) pd.merge( left, right, left_on=key_or_keys, right_index=True, how="left", sort=False ) Obviously you can choose whichever form you find more convenient. For many-to-one joins (where one of the DataFrame’s is already indexed by the join key), using join may be more convenient. Here is a simple example: In [86]: left = pd.DataFrame( ....: { ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: "key": ["K0", "K1", "K0", "K1"], ....: } ....: ) ....: In [87]: right = pd.DataFrame({"C": ["C0", "C1"], "D": ["D0", "D1"]}, index=["K0", "K1"]) In [88]: result = left.join(right, on="key") In [89]: result = pd.merge( ....: left, right, left_on="key", right_index=True, how="left", sort=False ....: ) ....: To join on multiple keys, the passed DataFrame must have a MultiIndex: In [90]: left = pd.DataFrame( ....: { ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: "key1": ["K0", "K0", "K1", "K2"], ....: "key2": ["K0", "K1", "K0", "K1"], ....: } ....: ) ....: In [91]: index = pd.MultiIndex.from_tuples( ....: [("K0", "K0"), ("K1", "K0"), ("K2", "K0"), ("K2", "K1")] ....: ) ....: In [92]: right = pd.DataFrame( ....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=index ....: ) ....: Now this can be joined by passing the two key column names: In [93]: result = left.join(right, on=["key1", "key2"]) The default for DataFrame.join is to perform a left join (essentially a “VLOOKUP” operation, for Excel users), which uses only the keys found in the calling DataFrame. Other join types, for example inner join, can be just as easily performed: In [94]: result = left.join(right, on=["key1", "key2"], how="inner") As you can see, this drops any rows where there was no match. Joining a single Index to a MultiIndex# You can join a singly-indexed DataFrame with a level of a MultiIndexed DataFrame. The level will match on the name of the index of the singly-indexed frame against a level name of the MultiIndexed frame. In [95]: left = pd.DataFrame( ....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, ....: index=pd.Index(["K0", "K1", "K2"], name="key"), ....: ) ....: In [96]: index = pd.MultiIndex.from_tuples( ....: [("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")], ....: names=["key", "Y"], ....: ) ....: In [97]: right = pd.DataFrame( ....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, ....: index=index, ....: ) ....: In [98]: result = left.join(right, how="inner") This is equivalent but less verbose and more memory efficient / faster than this. In [99]: result = pd.merge( ....: left.reset_index(), right.reset_index(), on=["key"], how="inner" ....: ).set_index(["key","Y"]) ....: Joining with two MultiIndexes# This is supported in a limited way, provided that the index for the right argument is completely used in the join, and is a subset of the indices in the left argument, as in this example: In [100]: leftindex = pd.MultiIndex.from_product( .....: [list("abc"), list("xy"), [1, 2]], names=["abc", "xy", "num"] .....: ) .....: In [101]: left = pd.DataFrame({"v1": range(12)}, index=leftindex) In [102]: left Out[102]: v1 abc xy num a x 1 0 2 1 y 1 2 2 3 b x 1 4 2 5 y 1 6 2 7 c x 1 8 2 9 y 1 10 2 11 In [103]: rightindex = pd.MultiIndex.from_product( .....: [list("abc"), list("xy")], names=["abc", "xy"] .....: ) .....: In [104]: right = pd.DataFrame({"v2": [100 * i for i in range(1, 7)]}, index=rightindex) In [105]: right Out[105]: v2 abc xy a x 100 y 200 b x 300 y 400 c x 500 y 600 In [106]: left.join(right, on=["abc", "xy"], how="inner") Out[106]: v1 v2 abc xy num a x 1 0 100 2 1 100 y 1 2 200 2 3 200 b x 1 4 300 2 5 300 y 1 6 400 2 7 400 c x 1 8 500 2 9 500 y 1 10 600 2 11 600 If that condition is not satisfied, a join with two multi-indexes can be done using the following code. In [107]: leftindex = pd.MultiIndex.from_tuples( .....: [("K0", "X0"), ("K0", "X1"), ("K1", "X2")], names=["key", "X"] .....: ) .....: In [108]: left = pd.DataFrame( .....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=leftindex .....: ) .....: In [109]: rightindex = pd.MultiIndex.from_tuples( .....: [("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")], names=["key", "Y"] .....: ) .....: In [110]: right = pd.DataFrame( .....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=rightindex .....: ) .....: In [111]: result = pd.merge( .....: left.reset_index(), right.reset_index(), on=["key"], how="inner" .....: ).set_index(["key", "X", "Y"]) .....: Merging on a combination of columns and index levels# Strings passed as the on, left_on, and right_on parameters may refer to either column names or index level names. This enables merging DataFrame instances on a combination of index levels and columns without resetting indexes. In [112]: left_index = pd.Index(["K0", "K0", "K1", "K2"], name="key1") In [113]: left = pd.DataFrame( .....: { .....: "A": ["A0", "A1", "A2", "A3"], .....: "B": ["B0", "B1", "B2", "B3"], .....: "key2": ["K0", "K1", "K0", "K1"], .....: }, .....: index=left_index, .....: ) .....: In [114]: right_index = pd.Index(["K0", "K1", "K2", "K2"], name="key1") In [115]: right = pd.DataFrame( .....: { .....: "C": ["C0", "C1", "C2", "C3"], .....: "D": ["D0", "D1", "D2", "D3"], .....: "key2": ["K0", "K0", "K0", "K1"], .....: }, .....: index=right_index, .....: ) .....: In [116]: result = left.merge(right, on=["key1", "key2"]) Note When DataFrames are merged on a string that matches an index level in both frames, the index level is preserved as an index level in the resulting DataFrame. Note When DataFrames are merged using only some of the levels of a MultiIndex, the extra levels will be dropped from the resulting merge. In order to preserve those levels, use reset_index on those level names to move those levels to columns prior to doing the merge. Note If a string matches both a column name and an index level name, then a warning is issued and the column takes precedence. This will result in an ambiguity error in a future version. Overlapping value columns# The merge suffixes argument takes a tuple of list of strings to append to overlapping column names in the input DataFrames to disambiguate the result columns: In [117]: left = pd.DataFrame({"k": ["K0", "K1", "K2"], "v": [1, 2, 3]}) In [118]: right = pd.DataFrame({"k": ["K0", "K0", "K3"], "v": [4, 5, 6]}) In [119]: result = pd.merge(left, right, on="k") In [120]: result = pd.merge(left, right, on="k", suffixes=("_l", "_r")) DataFrame.join() has lsuffix and rsuffix arguments which behave similarly. In [121]: left = left.set_index("k") In [122]: right = right.set_index("k") In [123]: result = left.join(right, lsuffix="_l", rsuffix="_r") Joining multiple DataFrames# A list or tuple of DataFrames can also be passed to join() to join them together on their indexes. In [124]: right2 = pd.DataFrame({"v": [7, 8, 9]}, index=["K1", "K1", "K2"]) In [125]: result = left.join([right, right2]) Merging together values within Series or DataFrame columns# Another fairly common situation is to have two like-indexed (or similarly indexed) Series or DataFrame objects and wanting to “patch” values in one object from values for matching indices in the other. Here is an example: In [126]: df1 = pd.DataFrame( .....: [[np.nan, 3.0, 5.0], [-4.6, np.nan, np.nan], [np.nan, 7.0, np.nan]] .....: ) .....: In [127]: df2 = pd.DataFrame([[-42.6, np.nan, -8.2], [-5.0, 1.6, 4]], index=[1, 2]) For this, use the combine_first() method: In [128]: result = df1.combine_first(df2) Note that this method only takes values from the right DataFrame if they are missing in the left DataFrame. A related method, update(), alters non-NA values in place: In [129]: df1.update(df2) Timeseries friendly merging# Merging ordered data# A merge_ordered() function allows combining time series and other ordered data. In particular it has an optional fill_method keyword to fill/interpolate missing data: In [130]: left = pd.DataFrame( .....: {"k": ["K0", "K1", "K1", "K2"], "lv": [1, 2, 3, 4], "s": ["a", "b", "c", "d"]} .....: ) .....: In [131]: right = pd.DataFrame({"k": ["K1", "K2", "K4"], "rv": [1, 2, 3]}) In [132]: pd.merge_ordered(left, right, fill_method="ffill", left_by="s") Out[132]: k lv s rv 0 K0 1.0 a NaN 1 K1 1.0 a 1.0 2 K2 1.0 a 2.0 3 K4 1.0 a 3.0 4 K1 2.0 b 1.0 5 K2 2.0 b 2.0 6 K4 2.0 b 3.0 7 K1 3.0 c 1.0 8 K2 3.0 c 2.0 9 K4 3.0 c 3.0 10 K1 NaN d 1.0 11 K2 4.0 d 2.0 12 K4 4.0 d 3.0 Merging asof# A merge_asof() is similar to an ordered left-join except that we match on nearest key rather than equal keys. For each row in the left DataFrame, we select the last row in the right DataFrame whose on key is less than the left’s key. Both DataFrames must be sorted by the key. Optionally an asof merge can perform a group-wise merge. This matches the by key equally, in addition to the nearest match on the on key. For example; we might have trades and quotes and we want to asof merge them. In [133]: trades = pd.DataFrame( .....: { .....: "time": pd.to_datetime( .....: [ .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.038", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.048", .....: ] .....: ), .....: "ticker": ["MSFT", "MSFT", "GOOG", "GOOG", "AAPL"], .....: "price": [51.95, 51.95, 720.77, 720.92, 98.00], .....: "quantity": [75, 155, 100, 100, 100], .....: }, .....: columns=["time", "ticker", "price", "quantity"], .....: ) .....: In [134]: quotes = pd.DataFrame( .....: { .....: "time": pd.to_datetime( .....: [ .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.030", .....: "20160525 13:30:00.041", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.049", .....: "20160525 13:30:00.072", .....: "20160525 13:30:00.075", .....: ] .....: ), .....: "ticker": ["GOOG", "MSFT", "MSFT", "MSFT", "GOOG", "AAPL", "GOOG", "MSFT"], .....: "bid": [720.50, 51.95, 51.97, 51.99, 720.50, 97.99, 720.50, 52.01], .....: "ask": [720.93, 51.96, 51.98, 52.00, 720.93, 98.01, 720.88, 52.03], .....: }, .....: columns=["time", "ticker", "bid", "ask"], .....: ) .....: In [135]: trades Out[135]: time ticker price quantity 0 2016-05-25 13:30:00.023 MSFT 51.95 75 1 2016-05-25 13:30:00.038 MSFT 51.95 155 2 2016-05-25 13:30:00.048 GOOG 720.77 100 3 2016-05-25 13:30:00.048 GOOG 720.92 100 4 2016-05-25 13:30:00.048 AAPL 98.00 100 In [136]: quotes Out[136]: time ticker bid ask 0 2016-05-25 13:30:00.023 GOOG 720.50 720.93 1 2016-05-25 13:30:00.023 MSFT 51.95 51.96 2 2016-05-25 13:30:00.030 MSFT 51.97 51.98 3 2016-05-25 13:30:00.041 MSFT 51.99 52.00 4 2016-05-25 13:30:00.048 GOOG 720.50 720.93 5 2016-05-25 13:30:00.049 AAPL 97.99 98.01 6 2016-05-25 13:30:00.072 GOOG 720.50 720.88 7 2016-05-25 13:30:00.075 MSFT 52.01 52.03 By default we are taking the asof of the quotes. In [137]: pd.merge_asof(trades, quotes, on="time", by="ticker") Out[137]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 51.95 51.96 1 2016-05-25 13:30:00.038 MSFT 51.95 155 51.97 51.98 2 2016-05-25 13:30:00.048 GOOG 720.77 100 720.50 720.93 3 2016-05-25 13:30:00.048 GOOG 720.92 100 720.50 720.93 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN We only asof within 2ms between the quote time and the trade time. In [138]: pd.merge_asof(trades, quotes, on="time", by="ticker", tolerance=pd.Timedelta("2ms")) Out[138]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 51.95 51.96 1 2016-05-25 13:30:00.038 MSFT 51.95 155 NaN NaN 2 2016-05-25 13:30:00.048 GOOG 720.77 100 720.50 720.93 3 2016-05-25 13:30:00.048 GOOG 720.92 100 720.50 720.93 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN We only asof within 10ms between the quote time and the trade time and we exclude exact matches on time. Note that though we exclude the exact matches (of the quotes), prior quotes do propagate to that point in time. In [139]: pd.merge_asof( .....: trades, .....: quotes, .....: on="time", .....: by="ticker", .....: tolerance=pd.Timedelta("10ms"), .....: allow_exact_matches=False, .....: ) .....: Out[139]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 NaN NaN 1 2016-05-25 13:30:00.038 MSFT 51.95 155 51.97 51.98 2 2016-05-25 13:30:00.048 GOOG 720.77 100 NaN NaN 3 2016-05-25 13:30:00.048 GOOG 720.92 100 NaN NaN 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN Comparing objects# The compare() and compare() methods allow you to compare two DataFrame or Series, respectively, and summarize their differences. This feature was added in V1.1.0. For example, you might want to compare two DataFrame and stack their differences side by side. In [140]: df = pd.DataFrame( .....: { .....: "col1": ["a", "a", "b", "b", "a"], .....: "col2": [1.0, 2.0, 3.0, np.nan, 5.0], .....: "col3": [1.0, 2.0, 3.0, 4.0, 5.0], .....: }, .....: columns=["col1", "col2", "col3"], .....: ) .....: In [141]: df Out[141]: col1 col2 col3 0 a 1.0 1.0 1 a 2.0 2.0 2 b 3.0 3.0 3 b NaN 4.0 4 a 5.0 5.0 In [142]: df2 = df.copy() In [143]: df2.loc[0, "col1"] = "c" In [144]: df2.loc[2, "col3"] = 4.0 In [145]: df2 Out[145]: col1 col2 col3 0 c 1.0 1.0 1 a 2.0 2.0 2 b 3.0 4.0 3 b NaN 4.0 4 a 5.0 5.0 In [146]: df.compare(df2) Out[146]: col1 col3 self other self other 0 a c NaN NaN 2 NaN NaN 3.0 4.0 By default, if two corresponding values are equal, they will be shown as NaN. Furthermore, if all values in an entire row / column, the row / column will be omitted from the result. The remaining differences will be aligned on columns. If you wish, you may choose to stack the differences on rows. In [147]: df.compare(df2, align_axis=0) Out[147]: col1 col3 0 self a NaN other c NaN 2 self NaN 3.0 other NaN 4.0 If you wish to keep all original rows and columns, set keep_shape argument to True. In [148]: df.compare(df2, keep_shape=True) Out[148]: col1 col2 col3 self other self other self other 0 a c NaN NaN NaN NaN 1 NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN 3.0 4.0 3 NaN NaN NaN NaN NaN NaN 4 NaN NaN NaN NaN NaN NaN You may also keep all the original values even if they are equal. In [149]: df.compare(df2, keep_shape=True, keep_equal=True) Out[149]: col1 col2 col3 self other self other self other 0 a c 1.0 1.0 1.0 1.0 1 a a 2.0 2.0 2.0 2.0 2 b b 3.0 3.0 3.0 4.0 3 b b NaN NaN 4.0 4.0 4 a a 5.0 5.0 5.0 5.0
211
718
Merge row of two different dataframe using a column as pattern matching I have two dataframes. df = pd.DataFrame([['klf1', 10], ['sp2', 3], ['klf3', 12], ['egr1', 5], ['klf11', 2]], columns=['tf','count']) df2 = pd.DataFrame([['Homer-sp2', 0.01], ['Homer-klf1', 0.0001], ['Homer-klf3-chip', 0.05], ['klf11',0.002], ['Homer-egr1', 0.01]], columns=['Motif_name','p_val']) I'm trying to merge them using df['tf'] content. For exemple 'klf1' row should be 'klf1' 10 0.0001 So I have to use the content of df['tf'] as pattern matching in df2[Motif_name] I'm expecting to get this df df_final=pd.DataFrame([['Klf1', 10,0.0001], ['sp2', 3, 0.01], ['klf3', 12,0.05], ['egr1', 5,0.01], ['klf11', 2,0.002]], columns=['tf','count','p_val']) I tried to use .str.contains('pattern') but it works only for one pattern. Here I don't know how can I loop on the content of the column tf. df2['Motif_name'].str.contains(df['tf'].str.lower()) Also .str.contains will look for pattern and I know that I'll have a problem on klf1 VS klf11 because klf1 is in klf11. How can I manage this problem?
/
Just to fix your approach: import re res=df.assign(key=1).merge(df2.assign(key=1), on='key').drop('key', axis=1) res=res.loc[map(lambda x: True if re.search(*x) else False, zip(res['tf'].str.lower()+r'($|[^\d])', res['Motif_name']))] Outputs: >>> res tf count Motif_name p_val 1 klf1 10 Homer-klf1 0.0001 5 klf2 3 Homer-klf2 0.0100 12 klf3 12 Homer-klf3-chip 0.0500 19 klf9 5 Homer-klf9 0.0100 23 klf11 2 klf11 0.0020
65,673,228
Substracting column values that satisfy a condition from constant value
<p>I'm trying to calculate heating degree days.I have the following dataframe:</p> <pre><code>df.head() Out[196]: NUTS_ID t2m Date 2010-01-01 AT11 4.134250 2010-07-01 AT11 24.019817 2010-07-01 AT12 21.902833 2010-01-01 AT12 2.687778 2010-01-01 AT13 3.796989 </code></pre> <p>I want to substract all the temperatures in column <code>t2m</code> from 18 if they are below this number, and assign 0 to the columns where t2m is over 18. That is,</p> <pre><code> NUTS_ID t2m HDD Date 2010-01-01 AT11 4.134250 13.865750 2010-07-01 AT11 24.019817 0 2010-07-01 AT12 21.902833 0 2010-01-01 AT12 2.687778 15.312222 2010-01-01 AT13 3.796989 14.203011 </code></pre> <p>I tried doing</p> <pre><code>df.loc[df['t2m']&lt;18,'HDD']=18-df['t2m'] df.loc[df['t2m']&gt;18,'HDD']=0 </code></pre> <p>But I get <code>ValueError: cannot reindex from a duplicate axis</code> (and possibly in the first line I would be affecting the values of the rows higher than 18 anyway). How could I fix it? Thanks for your help.</p>
65,673,292
"2021-01-11T19:22:03.183000"
2
null
1
26
python|pandas
<p>A good and efficient way is to use <code>np.where</code>:</p> <pre><code>import numpy as np df['HDD'] = np.where(df['t2m'] &gt; 18,0,18 - df['t2m']) Out[97]: Date NUTS_ID t2m HDD 0 2010-01-01 AT11 4.13 13.87 1 2010-07-01 AT11 24.02 0.00 2 2010-07-01 AT12 21.90 0.00 3 2010-01-01 AT12 2.69 15.31 4 2010-01-01 AT13 3.80 14.20 </code></pre>
"2021-01-11T19:26:00.330000"
1
https://pandas.pydata.org/docs/reference/api/pandas.Series.where.html
pandas.Series.where# pandas.Series.where# Series.where(cond, other=_NoDefault.no_default, *, inplace=False, axis=None, level=None, errors=_NoDefault.no_default, try_cast=_NoDefault.no_default)[source]# A good and efficient way is to use np.where: import numpy as np df['HDD'] = np.where(df['t2m'] > 18,0,18 - df['t2m']) Out[97]: Date NUTS_ID t2m HDD 0 2010-01-01 AT11 4.13 13.87 1 2010-07-01 AT11 24.02 0.00 2 2010-07-01 AT12 21.90 0.00 3 2010-01-01 AT12 2.69 15.31 4 2010-01-01 AT13 3.80 14.20 Replace values where the condition is False. Parameters condbool Series/DataFrame, array-like, or callableWhere cond is True, keep the original value. Where False, replace with corresponding value from other. If cond is callable, it is computed on the Series/DataFrame and should return boolean Series/DataFrame or array. The callable must not change input Series/DataFrame (though pandas doesn’t check it). otherscalar, Series/DataFrame, or callableEntries where cond is False are replaced with corresponding value from other. If other is callable, it is computed on the Series/DataFrame and should return scalar or Series/DataFrame. The callable must not change input Series/DataFrame (though pandas doesn’t check it). inplacebool, default FalseWhether to perform the operation in place on the data. axisint, default NoneAlignment axis if needed. For Series this parameter is unused and defaults to 0. levelint, default NoneAlignment level if needed. errorsstr, {‘raise’, ‘ignore’}, default ‘raise’Note that currently this parameter won’t affect the results and will always coerce to a suitable dtype. ‘raise’ : allow exceptions to be raised. ‘ignore’ : suppress exceptions. On error return original object. Deprecated since version 1.5.0: This argument had no effect. try_castbool, default NoneTry to cast the result back to the input type (if possible). Deprecated since version 1.3.0: Manually cast back if necessary. Returns Same type as caller or None if inplace=True. See also DataFrame.mask()Return an object of same shape as self. Notes The where method is an application of the if-then idiom. For each element in the calling DataFrame, if cond is True the element is used; otherwise the corresponding element from the DataFrame other is used. If the axis of other does not align with axis of cond Series/DataFrame, the misaligned index positions will be filled with False. The signature for DataFrame.where() differs from numpy.where(). Roughly df1.where(m, df2) is equivalent to np.where(m, df1, df2). For further details and examples see the where documentation in indexing. The dtype of the object takes precedence. The fill value is casted to the object’s dtype, if this can be done losslessly. Examples >>> s = pd.Series(range(5)) >>> s.where(s > 0) 0 NaN 1 1.0 2 2.0 3 3.0 4 4.0 dtype: float64 >>> s.mask(s > 0) 0 0.0 1 NaN 2 NaN 3 NaN 4 NaN dtype: float64 >>> s = pd.Series(range(5)) >>> t = pd.Series([True, False]) >>> s.where(t, 99) 0 0 1 99 2 99 3 99 4 99 dtype: int64 >>> s.mask(t, 99) 0 99 1 1 2 99 3 99 4 99 dtype: int64 >>> s.where(s > 1, 10) 0 10 1 10 2 2 3 3 4 4 dtype: int64 >>> s.mask(s > 1, 10) 0 0 1 1 2 10 3 10 4 10 dtype: int64 >>> df = pd.DataFrame(np.arange(10).reshape(-1, 2), columns=['A', 'B']) >>> df A B 0 0 1 1 2 3 2 4 5 3 6 7 4 8 9 >>> m = df % 3 == 0 >>> df.where(m, -df) A B 0 0 -1 1 -2 3 2 -4 -5 3 6 -7 4 -8 9 >>> df.where(m, -df) == np.where(m, df, -df) A B 0 True True 1 True True 2 True True 3 True True 4 True True >>> df.where(m, -df) == df.mask(~m, -df) A B 0 True True 1 True True 2 True True 3 True True 4 True True
206
533
Substracting column values that satisfy a condition from constant value I'm trying to calculate heating degree days.I have the following dataframe: df.head() Out[196]: NUTS_ID t2m Date 2010-01-01 AT11 4.134250 2010-07-01 AT11 24.019817 2010-07-01 AT12 21.902833 2010-01-01 AT12 2.687778 2010-01-01 AT13 3.796989 I want to substract all the temperatures in column t2m from 18 if they are below this number, and assign 0 to the columns where t2m is over 18. That is, NUTS_ID t2m HDD Date 2010-01-01 AT11 4.134250 13.865750 2010-07-01 AT11 24.019817 0 2010-07-01 AT12 21.902833 0 2010-01-01 AT12 2.687778 15.312222 2010-01-01 AT13 3.796989 14.203011 I tried doing df.loc[df['t2m']<18,'HDD']=18-df['t2m'] df.loc[df['t2m']>18,'HDD']=0 But I get ValueError: cannot reindex from a duplicate axis (and possibly in the first line I would be affecting the values of the rows higher than 18 anyway). How could I fix it? Thanks for your help.
A good and efficient way is to use np.where: import numpy as np df['HDD'] = np.where(df['t2m'] > 18,0,18 - df['t2m']) Out[97]: Date NUTS_ID t2m HDD 0 2010-01-01 AT11 4.13 13.87 1 2010-07-01 AT11 24.02 0.00 2 2010-07-01 AT12 21.90 0.00 3 2010-01-01 AT12 2.69 15.31 4 2010-01-01 AT13 3.80 14.20
65,432,498
How to merge rows of a dataframe where index is the same
<p>Let's say I have a dataframe, where indexes are the same:</p> <pre><code>index date1 date2 date3 A 1 NaN NaN A NaN 3 NaN B 1 NaN NaN A NaN NaN 0 </code></pre> <p>How can I transofrm it into a more concise dataframe:</p> <pre><code>index date1 date2 date3 A 1 3 0 B 1 NaN NaN </code></pre>
65,432,563
"2020-12-24T00:18:45.500000"
1
1
0
29
python|pandas
<p>assuming <code>index</code> is your actual index, a <code>groupby.first()</code> will do.</p> <pre><code>df1 = df.groupby(level=0).first() print(df1) date1 date2 date3 index A 1.0 3.0 0.0 B 1.0 NaN NaN </code></pre>
"2020-12-24T00:29:33.017000"
1
https://pandas.pydata.org/docs/dev/user_guide/merging.html
Merge, join, concatenate and compare# Merge, join, concatenate and compare# pandas provides various facilities for easily combining together Series or assuming index is your actual index, a groupby.first() will do. df1 = df.groupby(level=0).first() print(df1) date1 date2 date3 index A 1.0 3.0 0.0 B 1.0 NaN NaN DataFrame with various kinds of set logic for the indexes and relational algebra functionality in the case of join / merge-type operations. In addition, pandas also provides utilities to compare two Series or DataFrame and summarize their differences. Concatenating objects# The concat() function (in the main pandas namespace) does all of the heavy lifting of performing concatenation operations along an axis while performing optional set logic (union or intersection) of the indexes (if any) on the other axes. Note that I say “if any” because there is only a single possible axis of concatenation for Series. Before diving into all of the details of concat and what it can do, here is a simple example: In [1]: df1 = pd.DataFrame( ...: { ...: "A": ["A0", "A1", "A2", "A3"], ...: "B": ["B0", "B1", "B2", "B3"], ...: "C": ["C0", "C1", "C2", "C3"], ...: "D": ["D0", "D1", "D2", "D3"], ...: }, ...: index=[0, 1, 2, 3], ...: ) ...: In [2]: df2 = pd.DataFrame( ...: { ...: "A": ["A4", "A5", "A6", "A7"], ...: "B": ["B4", "B5", "B6", "B7"], ...: "C": ["C4", "C5", "C6", "C7"], ...: "D": ["D4", "D5", "D6", "D7"], ...: }, ...: index=[4, 5, 6, 7], ...: ) ...: In [3]: df3 = pd.DataFrame( ...: { ...: "A": ["A8", "A9", "A10", "A11"], ...: "B": ["B8", "B9", "B10", "B11"], ...: "C": ["C8", "C9", "C10", "C11"], ...: "D": ["D8", "D9", "D10", "D11"], ...: }, ...: index=[8, 9, 10, 11], ...: ) ...: In [4]: frames = [df1, df2, df3] In [5]: result = pd.concat(frames) Like its sibling function on ndarrays, numpy.concatenate, pandas.concat takes a list or dict of homogeneously-typed objects and concatenates them with some configurable handling of “what to do with the other axes”: pd.concat( objs, axis=0, join="outer", ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, copy=True, ) objs : a sequence or mapping of Series or DataFrame objects. If a dict is passed, the sorted keys will be used as the keys argument, unless it is passed, in which case the values will be selected (see below). Any None objects will be dropped silently unless they are all None in which case a ValueError will be raised. axis : {0, 1, …}, default 0. The axis to concatenate along. join : {‘inner’, ‘outer’}, default ‘outer’. How to handle indexes on other axis(es). Outer for union and inner for intersection. ignore_index : boolean, default False. If True, do not use the index values on the concatenation axis. The resulting axis will be labeled 0, …, n - 1. This is useful if you are concatenating objects where the concatenation axis does not have meaningful indexing information. Note the index values on the other axes are still respected in the join. keys : sequence, default None. Construct hierarchical index using the passed keys as the outermost level. If multiple levels passed, should contain tuples. levels : list of sequences, default None. Specific levels (unique values) to use for constructing a MultiIndex. Otherwise they will be inferred from the keys. names : list, default None. Names for the levels in the resulting hierarchical index. verify_integrity : boolean, default False. Check whether the new concatenated axis contains duplicates. This can be very expensive relative to the actual data concatenation. copy : boolean, default True. If False, do not copy data unnecessarily. Without a little bit of context many of these arguments don’t make much sense. Let’s revisit the above example. Suppose we wanted to associate specific keys with each of the pieces of the chopped up DataFrame. We can do this using the keys argument: In [6]: result = pd.concat(frames, keys=["x", "y", "z"]) As you can see (if you’ve read the rest of the documentation), the resulting object’s index has a hierarchical index. This means that we can now select out each chunk by key: In [7]: result.loc["y"] Out[7]: A B C D 4 A4 B4 C4 D4 5 A5 B5 C5 D5 6 A6 B6 C6 D6 7 A7 B7 C7 D7 It’s not a stretch to see how this can be very useful. More detail on this functionality below. Note It is worth noting that concat() makes a full copy of the data, and that constantly reusing this function can create a significant performance hit. If you need to use the operation over several datasets, use a list comprehension. frames = [ process_your_file(f) for f in files ] result = pd.concat(frames) Note When concatenating DataFrames with named axes, pandas will attempt to preserve these index/column names whenever possible. In the case where all inputs share a common name, this name will be assigned to the result. When the input names do not all agree, the result will be unnamed. The same is true for MultiIndex, but the logic is applied separately on a level-by-level basis. Set logic on the other axes# When gluing together multiple DataFrames, you have a choice of how to handle the other axes (other than the one being concatenated). This can be done in the following two ways: Take the union of them all, join='outer'. This is the default option as it results in zero information loss. Take the intersection, join='inner'. Here is an example of each of these methods. First, the default join='outer' behavior: In [8]: df4 = pd.DataFrame( ...: { ...: "B": ["B2", "B3", "B6", "B7"], ...: "D": ["D2", "D3", "D6", "D7"], ...: "F": ["F2", "F3", "F6", "F7"], ...: }, ...: index=[2, 3, 6, 7], ...: ) ...: In [9]: result = pd.concat([df1, df4], axis=1) Here is the same thing with join='inner': In [10]: result = pd.concat([df1, df4], axis=1, join="inner") Lastly, suppose we just wanted to reuse the exact index from the original DataFrame: In [11]: result = pd.concat([df1, df4], axis=1).reindex(df1.index) Similarly, we could index before the concatenation: In [12]: pd.concat([df1, df4.reindex(df1.index)], axis=1) Out[12]: A B C D B D F 0 A0 B0 C0 D0 NaN NaN NaN 1 A1 B1 C1 D1 NaN NaN NaN 2 A2 B2 C2 D2 B2 D2 F2 3 A3 B3 C3 D3 B3 D3 F3 Ignoring indexes on the concatenation axis# For DataFrame objects which don’t have a meaningful index, you may wish to append them and ignore the fact that they may have overlapping indexes. To do this, use the ignore_index argument: In [13]: result = pd.concat([df1, df4], ignore_index=True, sort=False) Concatenating with mixed ndims# You can concatenate a mix of Series and DataFrame objects. The Series will be transformed to DataFrame with the column name as the name of the Series. In [14]: s1 = pd.Series(["X0", "X1", "X2", "X3"], name="X") In [15]: result = pd.concat([df1, s1], axis=1) Note Since we’re concatenating a Series to a DataFrame, we could have achieved the same result with DataFrame.assign(). To concatenate an arbitrary number of pandas objects (DataFrame or Series), use concat. If unnamed Series are passed they will be numbered consecutively. In [16]: s2 = pd.Series(["_0", "_1", "_2", "_3"]) In [17]: result = pd.concat([df1, s2, s2, s2], axis=1) Passing ignore_index=True will drop all name references. In [18]: result = pd.concat([df1, s1], axis=1, ignore_index=True) More concatenating with group keys# A fairly common use of the keys argument is to override the column names when creating a new DataFrame based on existing Series. Notice how the default behaviour consists on letting the resulting DataFrame inherit the parent Series’ name, when these existed. In [19]: s3 = pd.Series([0, 1, 2, 3], name="foo") In [20]: s4 = pd.Series([0, 1, 2, 3]) In [21]: s5 = pd.Series([0, 1, 4, 5]) In [22]: pd.concat([s3, s4, s5], axis=1) Out[22]: foo 0 1 0 0 0 0 1 1 1 1 2 2 2 4 3 3 3 5 Through the keys argument we can override the existing column names. In [23]: pd.concat([s3, s4, s5], axis=1, keys=["red", "blue", "yellow"]) Out[23]: red blue yellow 0 0 0 0 1 1 1 1 2 2 2 4 3 3 3 5 Let’s consider a variation of the very first example presented: In [24]: result = pd.concat(frames, keys=["x", "y", "z"]) You can also pass a dict to concat in which case the dict keys will be used for the keys argument (unless other keys are specified): In [25]: pieces = {"x": df1, "y": df2, "z": df3} In [26]: result = pd.concat(pieces) In [27]: result = pd.concat(pieces, keys=["z", "y"]) The MultiIndex created has levels that are constructed from the passed keys and the index of the DataFrame pieces: In [28]: result.index.levels Out[28]: FrozenList([['z', 'y'], [4, 5, 6, 7, 8, 9, 10, 11]]) If you wish to specify other levels (as will occasionally be the case), you can do so using the levels argument: In [29]: result = pd.concat( ....: pieces, keys=["x", "y", "z"], levels=[["z", "y", "x", "w"]], names=["group_key"] ....: ) ....: In [30]: result.index.levels Out[30]: FrozenList([['z', 'y', 'x', 'w'], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]) This is fairly esoteric, but it is actually necessary for implementing things like GroupBy where the order of a categorical variable is meaningful. Appending rows to a DataFrame# If you have a series that you want to append as a single row to a DataFrame, you can convert the row into a DataFrame and use concat In [31]: s2 = pd.Series(["X0", "X1", "X2", "X3"], index=["A", "B", "C", "D"]) In [32]: result = pd.concat([df1, s2.to_frame().T], ignore_index=True) You should use ignore_index with this method to instruct DataFrame to discard its index. If you wish to preserve the index, you should construct an appropriately-indexed DataFrame and append or concatenate those objects. Database-style DataFrame or named Series joining/merging# pandas has full-featured, high performance in-memory join operations idiomatically very similar to relational databases like SQL. These methods perform significantly better (in some cases well over an order of magnitude better) than other open source implementations (like base::merge.data.frame in R). The reason for this is careful algorithmic design and the internal layout of the data in DataFrame. See the cookbook for some advanced strategies. Users who are familiar with SQL but new to pandas might be interested in a comparison with SQL. pandas provides a single function, merge(), as the entry point for all standard database join operations between DataFrame or named Series objects: pd.merge( left, right, how="inner", on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=True, suffixes=("_x", "_y"), copy=True, indicator=False, validate=None, ) left: A DataFrame or named Series object. right: Another DataFrame or named Series object. on: Column or index level names to join on. Must be found in both the left and right DataFrame and/or Series objects. If not passed and left_index and right_index are False, the intersection of the columns in the DataFrames and/or Series will be inferred to be the join keys. left_on: Columns or index levels from the left DataFrame or Series to use as keys. Can either be column names, index level names, or arrays with length equal to the length of the DataFrame or Series. right_on: Columns or index levels from the right DataFrame or Series to use as keys. Can either be column names, index level names, or arrays with length equal to the length of the DataFrame or Series. left_index: If True, use the index (row labels) from the left DataFrame or Series as its join key(s). In the case of a DataFrame or Series with a MultiIndex (hierarchical), the number of levels must match the number of join keys from the right DataFrame or Series. right_index: Same usage as left_index for the right DataFrame or Series how: One of 'left', 'right', 'outer', 'inner', 'cross'. Defaults to inner. See below for more detailed description of each method. sort: Sort the result DataFrame by the join keys in lexicographical order. Defaults to True, setting to False will improve performance substantially in many cases. suffixes: A tuple of string suffixes to apply to overlapping columns. Defaults to ('_x', '_y'). copy: Always copy data (default True) from the passed DataFrame or named Series objects, even when reindexing is not necessary. Cannot be avoided in many cases but may improve performance / memory usage. The cases where copying can be avoided are somewhat pathological but this option is provided nonetheless. indicator: Add a column to the output DataFrame called _merge with information on the source of each row. _merge is Categorical-type and takes on a value of left_only for observations whose merge key only appears in 'left' DataFrame or Series, right_only for observations whose merge key only appears in 'right' DataFrame or Series, and both if the observation’s merge key is found in both. validate : string, default None. If specified, checks if merge is of specified type. “one_to_one” or “1:1”: checks if merge keys are unique in both left and right datasets. “one_to_many” or “1:m”: checks if merge keys are unique in left dataset. “many_to_one” or “m:1”: checks if merge keys are unique in right dataset. “many_to_many” or “m:m”: allowed, but does not result in checks. Note Support for specifying index levels as the on, left_on, and right_on parameters was added in version 0.23.0. Support for merging named Series objects was added in version 0.24.0. The return type will be the same as left. If left is a DataFrame or named Series and right is a subclass of DataFrame, the return type will still be DataFrame. merge is a function in the pandas namespace, and it is also available as a DataFrame instance method merge(), with the calling DataFrame being implicitly considered the left object in the join. The related join() method, uses merge internally for the index-on-index (by default) and column(s)-on-index join. If you are joining on index only, you may wish to use DataFrame.join to save yourself some typing. Brief primer on merge methods (relational algebra)# Experienced users of relational databases like SQL will be familiar with the terminology used to describe join operations between two SQL-table like structures (DataFrame objects). There are several cases to consider which are very important to understand: one-to-one joins: for example when joining two DataFrame objects on their indexes (which must contain unique values). many-to-one joins: for example when joining an index (unique) to one or more columns in a different DataFrame. many-to-many joins: joining columns on columns. Note When joining columns on columns (potentially a many-to-many join), any indexes on the passed DataFrame objects will be discarded. It is worth spending some time understanding the result of the many-to-many join case. In SQL / standard relational algebra, if a key combination appears more than once in both tables, the resulting table will have the Cartesian product of the associated data. Here is a very basic example with one unique key combination: In [33]: left = pd.DataFrame( ....: { ....: "key": ["K0", "K1", "K2", "K3"], ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: } ....: ) ....: In [34]: right = pd.DataFrame( ....: { ....: "key": ["K0", "K1", "K2", "K3"], ....: "C": ["C0", "C1", "C2", "C3"], ....: "D": ["D0", "D1", "D2", "D3"], ....: } ....: ) ....: In [35]: result = pd.merge(left, right, on="key") Here is a more complicated example with multiple join keys. Only the keys appearing in left and right are present (the intersection), since how='inner' by default. In [36]: left = pd.DataFrame( ....: { ....: "key1": ["K0", "K0", "K1", "K2"], ....: "key2": ["K0", "K1", "K0", "K1"], ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: } ....: ) ....: In [37]: right = pd.DataFrame( ....: { ....: "key1": ["K0", "K1", "K1", "K2"], ....: "key2": ["K0", "K0", "K0", "K0"], ....: "C": ["C0", "C1", "C2", "C3"], ....: "D": ["D0", "D1", "D2", "D3"], ....: } ....: ) ....: In [38]: result = pd.merge(left, right, on=["key1", "key2"]) The how argument to merge specifies how to determine which keys are to be included in the resulting table. If a key combination does not appear in either the left or right tables, the values in the joined table will be NA. Here is a summary of the how options and their SQL equivalent names: Merge method SQL Join Name Description left LEFT OUTER JOIN Use keys from left frame only right RIGHT OUTER JOIN Use keys from right frame only outer FULL OUTER JOIN Use union of keys from both frames inner INNER JOIN Use intersection of keys from both frames cross CROSS JOIN Create the cartesian product of rows of both frames In [39]: result = pd.merge(left, right, how="left", on=["key1", "key2"]) In [40]: result = pd.merge(left, right, how="right", on=["key1", "key2"]) In [41]: result = pd.merge(left, right, how="outer", on=["key1", "key2"]) In [42]: result = pd.merge(left, right, how="inner", on=["key1", "key2"]) In [43]: result = pd.merge(left, right, how="cross") You can merge a mult-indexed Series and a DataFrame, if the names of the MultiIndex correspond to the columns from the DataFrame. Transform the Series to a DataFrame using Series.reset_index() before merging, as shown in the following example. In [44]: df = pd.DataFrame({"Let": ["A", "B", "C"], "Num": [1, 2, 3]}) In [45]: df Out[45]: Let Num 0 A 1 1 B 2 2 C 3 In [46]: ser = pd.Series( ....: ["a", "b", "c", "d", "e", "f"], ....: index=pd.MultiIndex.from_arrays( ....: [["A", "B", "C"] * 2, [1, 2, 3, 4, 5, 6]], names=["Let", "Num"] ....: ), ....: ) ....: In [47]: ser Out[47]: Let Num A 1 a B 2 b C 3 c A 4 d B 5 e C 6 f dtype: object In [48]: pd.merge(df, ser.reset_index(), on=["Let", "Num"]) Out[48]: Let Num 0 0 A 1 a 1 B 2 b 2 C 3 c Here is another example with duplicate join keys in DataFrames: In [49]: left = pd.DataFrame({"A": [1, 2], "B": [2, 2]}) In [50]: right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]}) In [51]: result = pd.merge(left, right, on="B", how="outer") Warning Joining / merging on duplicate keys can cause a returned frame that is the multiplication of the row dimensions, which may result in memory overflow. It is the user’ s responsibility to manage duplicate values in keys before joining large DataFrames. Checking for duplicate keys# Users can use the validate argument to automatically check whether there are unexpected duplicates in their merge keys. Key uniqueness is checked before merge operations and so should protect against memory overflows. Checking key uniqueness is also a good way to ensure user data structures are as expected. In the following example, there are duplicate values of B in the right DataFrame. As this is not a one-to-one merge – as specified in the validate argument – an exception will be raised. In [52]: left = pd.DataFrame({"A": [1, 2], "B": [1, 2]}) In [53]: right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]}) In [53]: result = pd.merge(left, right, on="B", how="outer", validate="one_to_one") ... MergeError: Merge keys are not unique in right dataset; not a one-to-one merge If the user is aware of the duplicates in the right DataFrame but wants to ensure there are no duplicates in the left DataFrame, one can use the validate='one_to_many' argument instead, which will not raise an exception. In [54]: pd.merge(left, right, on="B", how="outer", validate="one_to_many") Out[54]: A_x B A_y 0 1 1 NaN 1 2 2 4.0 2 2 2 5.0 3 2 2 6.0 The merge indicator# merge() accepts the argument indicator. If True, a Categorical-type column called _merge will be added to the output object that takes on values: Observation Origin _merge value Merge key only in 'left' frame left_only Merge key only in 'right' frame right_only Merge key in both frames both In [55]: df1 = pd.DataFrame({"col1": [0, 1], "col_left": ["a", "b"]}) In [56]: df2 = pd.DataFrame({"col1": [1, 2, 2], "col_right": [2, 2, 2]}) In [57]: pd.merge(df1, df2, on="col1", how="outer", indicator=True) Out[57]: col1 col_left col_right _merge 0 0 a NaN left_only 1 1 b 2.0 both 2 2 NaN 2.0 right_only 3 2 NaN 2.0 right_only The indicator argument will also accept string arguments, in which case the indicator function will use the value of the passed string as the name for the indicator column. In [58]: pd.merge(df1, df2, on="col1", how="outer", indicator="indicator_column") Out[58]: col1 col_left col_right indicator_column 0 0 a NaN left_only 1 1 b 2.0 both 2 2 NaN 2.0 right_only 3 2 NaN 2.0 right_only Merge dtypes# Merging will preserve the dtype of the join keys. In [59]: left = pd.DataFrame({"key": [1], "v1": [10]}) In [60]: left Out[60]: key v1 0 1 10 In [61]: right = pd.DataFrame({"key": [1, 2], "v1": [20, 30]}) In [62]: right Out[62]: key v1 0 1 20 1 2 30 We are able to preserve the join keys: In [63]: pd.merge(left, right, how="outer") Out[63]: key v1 0 1 10 1 1 20 2 2 30 In [64]: pd.merge(left, right, how="outer").dtypes Out[64]: key int64 v1 int64 dtype: object Of course if you have missing values that are introduced, then the resulting dtype will be upcast. In [65]: pd.merge(left, right, how="outer", on="key") Out[65]: key v1_x v1_y 0 1 10.0 20 1 2 NaN 30 In [66]: pd.merge(left, right, how="outer", on="key").dtypes Out[66]: key int64 v1_x float64 v1_y int64 dtype: object Merging will preserve category dtypes of the mergands. See also the section on categoricals. The left frame. In [67]: from pandas.api.types import CategoricalDtype In [68]: X = pd.Series(np.random.choice(["foo", "bar"], size=(10,))) In [69]: X = X.astype(CategoricalDtype(categories=["foo", "bar"])) In [70]: left = pd.DataFrame( ....: {"X": X, "Y": np.random.choice(["one", "two", "three"], size=(10,))} ....: ) ....: In [71]: left Out[71]: X Y 0 bar one 1 foo one 2 foo three 3 bar three 4 foo one 5 bar one 6 bar three 7 bar three 8 bar three 9 foo three In [72]: left.dtypes Out[72]: X category Y object dtype: object The right frame. In [73]: right = pd.DataFrame( ....: { ....: "X": pd.Series(["foo", "bar"], dtype=CategoricalDtype(["foo", "bar"])), ....: "Z": [1, 2], ....: } ....: ) ....: In [74]: right Out[74]: X Z 0 foo 1 1 bar 2 In [75]: right.dtypes Out[75]: X category Z int64 dtype: object The merged result: In [76]: result = pd.merge(left, right, how="outer") In [77]: result Out[77]: X Y Z 0 bar one 2 1 bar three 2 2 bar one 2 3 bar three 2 4 bar three 2 5 bar three 2 6 foo one 1 7 foo three 1 8 foo one 1 9 foo three 1 In [78]: result.dtypes Out[78]: X category Y object Z int64 dtype: object Note The category dtypes must be exactly the same, meaning the same categories and the ordered attribute. Otherwise the result will coerce to the categories’ dtype. Note Merging on category dtypes that are the same can be quite performant compared to object dtype merging. Joining on index# DataFrame.join() is a convenient method for combining the columns of two potentially differently-indexed DataFrames into a single result DataFrame. Here is a very basic example: In [79]: left = pd.DataFrame( ....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=["K0", "K1", "K2"] ....: ) ....: In [80]: right = pd.DataFrame( ....: {"C": ["C0", "C2", "C3"], "D": ["D0", "D2", "D3"]}, index=["K0", "K2", "K3"] ....: ) ....: In [81]: result = left.join(right) In [82]: result = left.join(right, how="outer") The same as above, but with how='inner'. In [83]: result = left.join(right, how="inner") The data alignment here is on the indexes (row labels). This same behavior can be achieved using merge plus additional arguments instructing it to use the indexes: In [84]: result = pd.merge(left, right, left_index=True, right_index=True, how="outer") In [85]: result = pd.merge(left, right, left_index=True, right_index=True, how="inner") Joining key columns on an index# join() takes an optional on argument which may be a column or multiple column names, which specifies that the passed DataFrame is to be aligned on that column in the DataFrame. These two function calls are completely equivalent: left.join(right, on=key_or_keys) pd.merge( left, right, left_on=key_or_keys, right_index=True, how="left", sort=False ) Obviously you can choose whichever form you find more convenient. For many-to-one joins (where one of the DataFrame’s is already indexed by the join key), using join may be more convenient. Here is a simple example: In [86]: left = pd.DataFrame( ....: { ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: "key": ["K0", "K1", "K0", "K1"], ....: } ....: ) ....: In [87]: right = pd.DataFrame({"C": ["C0", "C1"], "D": ["D0", "D1"]}, index=["K0", "K1"]) In [88]: result = left.join(right, on="key") In [89]: result = pd.merge( ....: left, right, left_on="key", right_index=True, how="left", sort=False ....: ) ....: To join on multiple keys, the passed DataFrame must have a MultiIndex: In [90]: left = pd.DataFrame( ....: { ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: "key1": ["K0", "K0", "K1", "K2"], ....: "key2": ["K0", "K1", "K0", "K1"], ....: } ....: ) ....: In [91]: index = pd.MultiIndex.from_tuples( ....: [("K0", "K0"), ("K1", "K0"), ("K2", "K0"), ("K2", "K1")] ....: ) ....: In [92]: right = pd.DataFrame( ....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=index ....: ) ....: Now this can be joined by passing the two key column names: In [93]: result = left.join(right, on=["key1", "key2"]) The default for DataFrame.join is to perform a left join (essentially a “VLOOKUP” operation, for Excel users), which uses only the keys found in the calling DataFrame. Other join types, for example inner join, can be just as easily performed: In [94]: result = left.join(right, on=["key1", "key2"], how="inner") As you can see, this drops any rows where there was no match. Joining a single Index to a MultiIndex# You can join a singly-indexed DataFrame with a level of a MultiIndexed DataFrame. The level will match on the name of the index of the singly-indexed frame against a level name of the MultiIndexed frame. In [95]: left = pd.DataFrame( ....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, ....: index=pd.Index(["K0", "K1", "K2"], name="key"), ....: ) ....: In [96]: index = pd.MultiIndex.from_tuples( ....: [("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")], ....: names=["key", "Y"], ....: ) ....: In [97]: right = pd.DataFrame( ....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, ....: index=index, ....: ) ....: In [98]: result = left.join(right, how="inner") This is equivalent but less verbose and more memory efficient / faster than this. In [99]: result = pd.merge( ....: left.reset_index(), right.reset_index(), on=["key"], how="inner" ....: ).set_index(["key","Y"]) ....: Joining with two MultiIndexes# This is supported in a limited way, provided that the index for the right argument is completely used in the join, and is a subset of the indices in the left argument, as in this example: In [100]: leftindex = pd.MultiIndex.from_product( .....: [list("abc"), list("xy"), [1, 2]], names=["abc", "xy", "num"] .....: ) .....: In [101]: left = pd.DataFrame({"v1": range(12)}, index=leftindex) In [102]: left Out[102]: v1 abc xy num a x 1 0 2 1 y 1 2 2 3 b x 1 4 2 5 y 1 6 2 7 c x 1 8 2 9 y 1 10 2 11 In [103]: rightindex = pd.MultiIndex.from_product( .....: [list("abc"), list("xy")], names=["abc", "xy"] .....: ) .....: In [104]: right = pd.DataFrame({"v2": [100 * i for i in range(1, 7)]}, index=rightindex) In [105]: right Out[105]: v2 abc xy a x 100 y 200 b x 300 y 400 c x 500 y 600 In [106]: left.join(right, on=["abc", "xy"], how="inner") Out[106]: v1 v2 abc xy num a x 1 0 100 2 1 100 y 1 2 200 2 3 200 b x 1 4 300 2 5 300 y 1 6 400 2 7 400 c x 1 8 500 2 9 500 y 1 10 600 2 11 600 If that condition is not satisfied, a join with two multi-indexes can be done using the following code. In [107]: leftindex = pd.MultiIndex.from_tuples( .....: [("K0", "X0"), ("K0", "X1"), ("K1", "X2")], names=["key", "X"] .....: ) .....: In [108]: left = pd.DataFrame( .....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=leftindex .....: ) .....: In [109]: rightindex = pd.MultiIndex.from_tuples( .....: [("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")], names=["key", "Y"] .....: ) .....: In [110]: right = pd.DataFrame( .....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=rightindex .....: ) .....: In [111]: result = pd.merge( .....: left.reset_index(), right.reset_index(), on=["key"], how="inner" .....: ).set_index(["key", "X", "Y"]) .....: Merging on a combination of columns and index levels# Strings passed as the on, left_on, and right_on parameters may refer to either column names or index level names. This enables merging DataFrame instances on a combination of index levels and columns without resetting indexes. In [112]: left_index = pd.Index(["K0", "K0", "K1", "K2"], name="key1") In [113]: left = pd.DataFrame( .....: { .....: "A": ["A0", "A1", "A2", "A3"], .....: "B": ["B0", "B1", "B2", "B3"], .....: "key2": ["K0", "K1", "K0", "K1"], .....: }, .....: index=left_index, .....: ) .....: In [114]: right_index = pd.Index(["K0", "K1", "K2", "K2"], name="key1") In [115]: right = pd.DataFrame( .....: { .....: "C": ["C0", "C1", "C2", "C3"], .....: "D": ["D0", "D1", "D2", "D3"], .....: "key2": ["K0", "K0", "K0", "K1"], .....: }, .....: index=right_index, .....: ) .....: In [116]: result = left.merge(right, on=["key1", "key2"]) Note When DataFrames are merged on a string that matches an index level in both frames, the index level is preserved as an index level in the resulting DataFrame. Note When DataFrames are merged using only some of the levels of a MultiIndex, the extra levels will be dropped from the resulting merge. In order to preserve those levels, use reset_index on those level names to move those levels to columns prior to doing the merge. Note If a string matches both a column name and an index level name, then a warning is issued and the column takes precedence. This will result in an ambiguity error in a future version. Overlapping value columns# The merge suffixes argument takes a tuple of list of strings to append to overlapping column names in the input DataFrames to disambiguate the result columns: In [117]: left = pd.DataFrame({"k": ["K0", "K1", "K2"], "v": [1, 2, 3]}) In [118]: right = pd.DataFrame({"k": ["K0", "K0", "K3"], "v": [4, 5, 6]}) In [119]: result = pd.merge(left, right, on="k") In [120]: result = pd.merge(left, right, on="k", suffixes=("_l", "_r")) DataFrame.join() has lsuffix and rsuffix arguments which behave similarly. In [121]: left = left.set_index("k") In [122]: right = right.set_index("k") In [123]: result = left.join(right, lsuffix="_l", rsuffix="_r") Joining multiple DataFrames# A list or tuple of DataFrames can also be passed to join() to join them together on their indexes. In [124]: right2 = pd.DataFrame({"v": [7, 8, 9]}, index=["K1", "K1", "K2"]) In [125]: result = left.join([right, right2]) Merging together values within Series or DataFrame columns# Another fairly common situation is to have two like-indexed (or similarly indexed) Series or DataFrame objects and wanting to “patch” values in one object from values for matching indices in the other. Here is an example: In [126]: df1 = pd.DataFrame( .....: [[np.nan, 3.0, 5.0], [-4.6, np.nan, np.nan], [np.nan, 7.0, np.nan]] .....: ) .....: In [127]: df2 = pd.DataFrame([[-42.6, np.nan, -8.2], [-5.0, 1.6, 4]], index=[1, 2]) For this, use the combine_first() method: In [128]: result = df1.combine_first(df2) Note that this method only takes values from the right DataFrame if they are missing in the left DataFrame. A related method, update(), alters non-NA values in place: In [129]: df1.update(df2) Timeseries friendly merging# Merging ordered data# A merge_ordered() function allows combining time series and other ordered data. In particular it has an optional fill_method keyword to fill/interpolate missing data: In [130]: left = pd.DataFrame( .....: {"k": ["K0", "K1", "K1", "K2"], "lv": [1, 2, 3, 4], "s": ["a", "b", "c", "d"]} .....: ) .....: In [131]: right = pd.DataFrame({"k": ["K1", "K2", "K4"], "rv": [1, 2, 3]}) In [132]: pd.merge_ordered(left, right, fill_method="ffill", left_by="s") Out[132]: k lv s rv 0 K0 1.0 a NaN 1 K1 1.0 a 1.0 2 K2 1.0 a 2.0 3 K4 1.0 a 3.0 4 K1 2.0 b 1.0 5 K2 2.0 b 2.0 6 K4 2.0 b 3.0 7 K1 3.0 c 1.0 8 K2 3.0 c 2.0 9 K4 3.0 c 3.0 10 K1 NaN d 1.0 11 K2 4.0 d 2.0 12 K4 4.0 d 3.0 Merging asof# A merge_asof() is similar to an ordered left-join except that we match on nearest key rather than equal keys. For each row in the left DataFrame, we select the last row in the right DataFrame whose on key is less than the left’s key. Both DataFrames must be sorted by the key. Optionally an asof merge can perform a group-wise merge. This matches the by key equally, in addition to the nearest match on the on key. For example; we might have trades and quotes and we want to asof merge them. In [133]: trades = pd.DataFrame( .....: { .....: "time": pd.to_datetime( .....: [ .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.038", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.048", .....: ] .....: ), .....: "ticker": ["MSFT", "MSFT", "GOOG", "GOOG", "AAPL"], .....: "price": [51.95, 51.95, 720.77, 720.92, 98.00], .....: "quantity": [75, 155, 100, 100, 100], .....: }, .....: columns=["time", "ticker", "price", "quantity"], .....: ) .....: In [134]: quotes = pd.DataFrame( .....: { .....: "time": pd.to_datetime( .....: [ .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.030", .....: "20160525 13:30:00.041", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.049", .....: "20160525 13:30:00.072", .....: "20160525 13:30:00.075", .....: ] .....: ), .....: "ticker": ["GOOG", "MSFT", "MSFT", "MSFT", "GOOG", "AAPL", "GOOG", "MSFT"], .....: "bid": [720.50, 51.95, 51.97, 51.99, 720.50, 97.99, 720.50, 52.01], .....: "ask": [720.93, 51.96, 51.98, 52.00, 720.93, 98.01, 720.88, 52.03], .....: }, .....: columns=["time", "ticker", "bid", "ask"], .....: ) .....: In [135]: trades Out[135]: time ticker price quantity 0 2016-05-25 13:30:00.023 MSFT 51.95 75 1 2016-05-25 13:30:00.038 MSFT 51.95 155 2 2016-05-25 13:30:00.048 GOOG 720.77 100 3 2016-05-25 13:30:00.048 GOOG 720.92 100 4 2016-05-25 13:30:00.048 AAPL 98.00 100 In [136]: quotes Out[136]: time ticker bid ask 0 2016-05-25 13:30:00.023 GOOG 720.50 720.93 1 2016-05-25 13:30:00.023 MSFT 51.95 51.96 2 2016-05-25 13:30:00.030 MSFT 51.97 51.98 3 2016-05-25 13:30:00.041 MSFT 51.99 52.00 4 2016-05-25 13:30:00.048 GOOG 720.50 720.93 5 2016-05-25 13:30:00.049 AAPL 97.99 98.01 6 2016-05-25 13:30:00.072 GOOG 720.50 720.88 7 2016-05-25 13:30:00.075 MSFT 52.01 52.03 By default we are taking the asof of the quotes. In [137]: pd.merge_asof(trades, quotes, on="time", by="ticker") Out[137]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 51.95 51.96 1 2016-05-25 13:30:00.038 MSFT 51.95 155 51.97 51.98 2 2016-05-25 13:30:00.048 GOOG 720.77 100 720.50 720.93 3 2016-05-25 13:30:00.048 GOOG 720.92 100 720.50 720.93 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN We only asof within 2ms between the quote time and the trade time. In [138]: pd.merge_asof(trades, quotes, on="time", by="ticker", tolerance=pd.Timedelta("2ms")) Out[138]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 51.95 51.96 1 2016-05-25 13:30:00.038 MSFT 51.95 155 NaN NaN 2 2016-05-25 13:30:00.048 GOOG 720.77 100 720.50 720.93 3 2016-05-25 13:30:00.048 GOOG 720.92 100 720.50 720.93 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN We only asof within 10ms between the quote time and the trade time and we exclude exact matches on time. Note that though we exclude the exact matches (of the quotes), prior quotes do propagate to that point in time. In [139]: pd.merge_asof( .....: trades, .....: quotes, .....: on="time", .....: by="ticker", .....: tolerance=pd.Timedelta("10ms"), .....: allow_exact_matches=False, .....: ) .....: Out[139]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 NaN NaN 1 2016-05-25 13:30:00.038 MSFT 51.95 155 51.97 51.98 2 2016-05-25 13:30:00.048 GOOG 720.77 100 NaN NaN 3 2016-05-25 13:30:00.048 GOOG 720.92 100 NaN NaN 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN Comparing objects# The compare() and compare() methods allow you to compare two DataFrame or Series, respectively, and summarize their differences. This feature was added in V1.1.0. For example, you might want to compare two DataFrame and stack their differences side by side. In [140]: df = pd.DataFrame( .....: { .....: "col1": ["a", "a", "b", "b", "a"], .....: "col2": [1.0, 2.0, 3.0, np.nan, 5.0], .....: "col3": [1.0, 2.0, 3.0, 4.0, 5.0], .....: }, .....: columns=["col1", "col2", "col3"], .....: ) .....: In [141]: df Out[141]: col1 col2 col3 0 a 1.0 1.0 1 a 2.0 2.0 2 b 3.0 3.0 3 b NaN 4.0 4 a 5.0 5.0 In [142]: df2 = df.copy() In [143]: df2.loc[0, "col1"] = "c" In [144]: df2.loc[2, "col3"] = 4.0 In [145]: df2 Out[145]: col1 col2 col3 0 c 1.0 1.0 1 a 2.0 2.0 2 b 3.0 4.0 3 b NaN 4.0 4 a 5.0 5.0 In [146]: df.compare(df2) Out[146]: col1 col3 self other self other 0 a c NaN NaN 2 NaN NaN 3.0 4.0 By default, if two corresponding values are equal, they will be shown as NaN. Furthermore, if all values in an entire row / column, the row / column will be omitted from the result. The remaining differences will be aligned on columns. If you wish, you may choose to stack the differences on rows. In [147]: df.compare(df2, align_axis=0) Out[147]: col1 col3 0 self a NaN other c NaN 2 self NaN 3.0 other NaN 4.0 If you wish to keep all original rows and columns, set keep_shape argument to True. In [148]: df.compare(df2, keep_shape=True) Out[148]: col1 col2 col3 self other self other self other 0 a c NaN NaN NaN NaN 1 NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN 3.0 4.0 3 NaN NaN NaN NaN NaN NaN 4 NaN NaN NaN NaN NaN NaN You may also keep all the original values even if they are equal. In [149]: df.compare(df2, keep_shape=True, keep_equal=True) Out[149]: col1 col2 col3 self other self other self other 0 a c 1.0 1.0 1.0 1.0 1 a a 2.0 2.0 2.0 2.0 2 b b 3.0 3.0 3.0 4.0 3 b b NaN NaN 4.0 4.0 4 a a 5.0 5.0 5.0 5.0
153
342
How to merge rows of a dataframe where index is the same Let's say I have a dataframe, where indexes are the same: index date1 date2 date3 A 1 NaN NaN A NaN 3 NaN B 1 NaN NaN A NaN NaN 0 How can I transofrm it into a more concise dataframe: index date1 date2 date3 A 1 3 0 B 1 NaN NaN
assuming index is your actual index, a groupby.first() will do. df1 = df.groupby(level=0).first() print(df1) date1 date2 date3 index A 1.0 3.0 0.0 B 1.0 NaN NaN
68,821,679
Divide into groups with approximately equal count of rows and draw bars with average values
<p>I have many rows in:</p> <pre class="lang-py prettyprint-override"><code>df = pd.DataFrame(columns = ['param_1', 'param_2', ..., 'param_n']) </code></pre> <p>and I need to group rows into several groups and display stacked bars with averaged values:</p> <pre class="lang-py prettyprint-override"><code>bin_count = 10 step = math.ceil(df.shape[0] / bin_count) df['p'] = 0 for i in range(0, df.shape[0], step): df.iloc[i:i + step]['p'] = i df_group = df.groupby('p').mean() df_group.plot.bar(stacked = True) </code></pre> <p>How to do it more efficiently?</p>
68,821,784
"2021-08-17T17:29:29.587000"
1
null
0
30
python|pandas
<p>Use <code>pd.qcut</code> to split the DataFrame by passing it a <code>range</code> based on the length of the DataFrame.</p> <h3>Sample Data</h3> <pre><code>import pandas as pd import numpy as np # 111 rows to split into (10) groups Nr = 111 df = pd.DataFrame({'param_1': np.random.randint(0, 10, Nr), 'param_2': range(Nr)}) </code></pre> <h3>Code</h3> <pre><code># Number of bins bin_count = 10 df_group = df.groupby(pd.qcut(range(len(df)), bin_count, labels=False)).mean() df_group.plot.bar(stacked=True, rot=0) </code></pre> <p><a href="https://i.stack.imgur.com/dDusr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dDusr.png" alt="enter image description here" /></a></p> <hr /> <p>This will form groups as close to evenly sized as possible given the length of your data, similar to <code>np.array_split</code></p> <pre><code>df.groupby(pd.qcut(range(len(df)), bin_count, labels=False)).size() 0 12 1 11 2 11 3 11 4 11 5 11 6 11 7 11 8 11 9 11 dtype: int64 </code></pre>
"2021-08-17T17:37:27.197000"
1
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.bar.html
pandas.DataFrame.plot.bar# pandas.DataFrame.plot.bar# DataFrame.plot.bar(x=None, y=None, **kwargs)[source]# Vertical bar plot. A bar plot is a plot that presents categorical data with rectangular bars with lengths proportional to the values that they represent. A bar plot shows comparisons among discrete categories. One axis of the plot shows the specific categories being compared, and the Use pd.qcut to split the DataFrame by passing it a range based on the length of the DataFrame. Sample Data import pandas as pd import numpy as np # 111 rows to split into (10) groups Nr = 111 df = pd.DataFrame({'param_1': np.random.randint(0, 10, Nr), 'param_2': range(Nr)}) Code # Number of bins bin_count = 10 df_group = df.groupby(pd.qcut(range(len(df)), bin_count, labels=False)).mean() df_group.plot.bar(stacked=True, rot=0) This will form groups as close to evenly sized as possible given the length of your data, similar to np.array_split df.groupby(pd.qcut(range(len(df)), bin_count, labels=False)).size() 0 12 1 11 2 11 3 11 4 11 5 11 6 11 7 11 8 11 9 11 dtype: int64 other axis represents a measured value. Parameters xlabel or position, optionalAllows plotting of one column versus another. If not specified, the index of the DataFrame is used. ylabel or position, optionalAllows plotting of one column versus another. If not specified, all numerical columns are used. colorstr, array-like, or dict, optionalThe color for each of the DataFrame’s columns. Possible values are: A single color string referred to by name, RGB or RGBA code,for instance ‘red’ or ‘#a98d19’. A sequence of color strings referred to by name, RGB or RGBAcode, which will be used for each column recursively. For instance [‘green’,’yellow’] each column’s bar will be filled in green or yellow, alternatively. If there is only a single column to be plotted, then only the first color from the color list will be used. A dict of the form {column namecolor}, so that each column will becolored accordingly. For example, if your columns are called a and b, then passing {‘a’: ‘green’, ‘b’: ‘red’} will color bars for column a in green and bars for column b in red. New in version 1.1.0. **kwargsAdditional keyword arguments are documented in DataFrame.plot(). Returns matplotlib.axes.Axes or np.ndarray of themAn ndarray is returned with one matplotlib.axes.Axes per column when subplots=True. See also DataFrame.plot.barhHorizontal bar plot. DataFrame.plotMake plots of a DataFrame. matplotlib.pyplot.barMake a bar plot with matplotlib. Examples Basic plot. >>> df = pd.DataFrame({'lab':['A', 'B', 'C'], 'val':[10, 30, 20]}) >>> ax = df.plot.bar(x='lab', y='val', rot=0) Plot a whole dataframe to a bar plot. Each column is assigned a distinct color, and each row is nested in a group along the horizontal axis. >>> speed = [0.1, 17.5, 40, 48, 52, 69, 88] >>> lifespan = [2, 8, 70, 1.5, 25, 12, 28] >>> index = ['snail', 'pig', 'elephant', ... 'rabbit', 'giraffe', 'coyote', 'horse'] >>> df = pd.DataFrame({'speed': speed, ... 'lifespan': lifespan}, index=index) >>> ax = df.plot.bar(rot=0) Plot stacked bar charts for the DataFrame >>> ax = df.plot.bar(stacked=True) Instead of nesting, the figure can be split by column with subplots=True. In this case, a numpy.ndarray of matplotlib.axes.Axes are returned. >>> axes = df.plot.bar(rot=0, subplots=True) >>> axes[1].legend(loc=2) If you don’t like the default colours, you can specify how you’d like each column to be colored. >>> axes = df.plot.bar( ... rot=0, subplots=True, color={"speed": "red", "lifespan": "green"} ... ) >>> axes[1].legend(loc=2) Plot a single column. >>> ax = df.plot.bar(y='speed', rot=0) Plot only selected categories for the DataFrame. >>> ax = df.plot.bar(x='lifespan', rot=0)
397
1,132
Divide into groups with approximately equal count of rows and draw bars with average values I have many rows in: df = pd.DataFrame(columns = ['param_1', 'param_2', ..., 'param_n']) and I need to group rows into several groups and display stacked bars with averaged values: bin_count = 10 step = math.ceil(df.shape[0] / bin_count) df['p'] = 0 for i in range(0, df.shape[0], step): df.iloc[i:i + step]['p'] = i df_group = df.groupby('p').mean() df_group.plot.bar(stacked = True) How to do it more efficiently?
Use pd.qcut to split the DataFrame by passing it a range based on the length of the DataFrame. Sample Data import pandas as pd import numpy as np # 111 rows to split into (10) groups Nr = 111 df = pd.DataFrame({'param_1': np.random.randint(0, 10, Nr), 'param_2': range(Nr)}) Code # Number of bins bin_count = 10 df_group = df.groupby(pd.qcut(range(len(df)), bin_count, labels=False)).mean() df_group.plot.bar(stacked=True, rot=0) This will form groups as close to evenly sized as possible given the length of your data, similar to np.array_split df.groupby(pd.qcut(range(len(df)), bin_count, labels=False)).size() 0 12 1 11 2 11 3 11 4 11 5 11 6 11 7 11 8 11 9 11 dtype: int64
64,519,828
Is there a way to add up multiple pandas columns conditionally based on text in the column label?
<p>I am building a dataframe that shows availability in a hotel. The rows are the dates and the columns are the room types. There are 5 columns labelled PD1_Total, PD2_Total, PDSK_Total, PDEK_Total, and PDSU_Total.</p> <p>I need to create a column that shows all the individual room availability added together. The obvious manual way to do it is with the code:</p> <p><code>ga['Total_Rooms'] = ga['PD1_Total'] + ga['PD2_Total'] + ga['PDSK_Total'] + ga['PDEK_Total'] + ga['PDSU_Total']</code></p> <p>However, the source data may be updated at a later date to include other room types with the suffix '_Total', so I'd like to figure out a way to create that formula to automatically include all the columns with that suffix without having to manually update the formula.</p> <p><a href="https://i.stack.imgur.com/5OMi5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5OMi5.png" alt="enter image description here" /></a></p> <p>Thank-you</p>
64,519,851
"2020-10-25T02:44:00.697000"
2
null
1
31
python|pandas
<p>A simple list comprehension for column selection might do the trick:</p> <pre><code>ga['Total_Rooms'] = ga[[col for col in ga.columns if col.endswith('_Total')]].sum(axis = 1) </code></pre>
"2020-10-25T02:48:00.397000"
1
https://pandas.pydata.org/docs/user_guide/style.html
Table Visualization# Table Visualization# This section demonstrates visualization of tabular data using the Styler class. For information on visualization with charting please see Chart Visualization. This document is written as a Jupyter Notebook, and can be viewed or downloaded here. Styler Object and HTML# Styling should be performed after the data in a DataFrame has been processed. The Styler creates an HTML <table> and leverages CSS styling language to manipulate many parameters including colors, fonts, borders, background, etc. See here for more information on styling HTML tables. This allows a lot of flexibility out of the box, and even enables web developers to integrate DataFrames into their exiting user interface designs. The DataFrame.style attribute is a property that returns a Styler object. It has a _repr_html_ method defined on it so they are rendered automatically in Jupyter Notebook. A simple list comprehension for column selection might do the trick: ga['Total_Rooms'] = ga[[col for col in ga.columns if col.endswith('_Total')]].sum(axis = 1) [2]: import pandas as pd import numpy as np import matplotlib as mpl df = pd.DataFrame([[38.0, 2.0, 18.0, 22.0, 21, np.nan],[19, 439, 6, 452, 226,232]], index=pd.Index(['Tumour (Positive)', 'Non-Tumour (Negative)'], name='Actual Label:'), columns=pd.MultiIndex.from_product([['Decision Tree', 'Regression', 'Random'],['Tumour', 'Non-Tumour']], names=['Model:', 'Predicted:'])) df.style [2]: Model: Decision Tree Regression Random Predicted: Tumour Non-Tumour Tumour Non-Tumour Tumour Non-Tumour Actual Label:             Tumour (Positive) 38.000000 2.000000 18.000000 22.000000 21 nan Non-Tumour (Negative) 19.000000 439.000000 6.000000 452.000000 226 232.000000 The above output looks very similar to the standard DataFrame HTML representation. But the HTML here has already attached some CSS classes to each cell, even if we haven’t yet created any styles. We can view these by calling the .to_html() method, which returns the raw HTML as string, which is useful for further processing or adding to a file - read on in More about CSS and HTML. Below we will show how we can use these to format the DataFrame to be more communicative. For example how we can build s: [4]: s [4]: Confusion matrix for multiple cancer prediction models. Model: Decision Tree Regression Predicted: Tumour Non-Tumour Tumour Non-Tumour Actual Label:         Tumour (Positive) 38 2 18 22 Non-Tumour (Negative) 19 439 6 452 Formatting the Display# Formatting Values# Before adding styles it is useful to show that the Styler can distinguish the display value from the actual value, in both datavalues and index or columns headers. To control the display value, the text is printed in each cell as string, and we can use the .format() and .format_index() methods to manipulate this according to a format spec string or a callable that takes a single value and returns a string. It is possible to define this for the whole table, or index, or for individual columns, or MultiIndex levels. Additionally, the format function has a precision argument to specifically help formatting floats, as well as decimal and thousands separators to support other locales, an na_rep argument to display missing data, and an escape argument to help displaying safe-HTML or safe-LaTeX. The default formatter is configured to adopt pandas’ styler.format.precision option, controllable using with pd.option_context('format.precision', 2): [5]: df.style.format(precision=0, na_rep='MISSING', thousands=" ", formatter={('Decision Tree', 'Tumour'): "{:.2f}", ('Regression', 'Non-Tumour'): lambda x: "$ {:,.1f}".format(x*-1e6) }) [5]: Model: Decision Tree Regression Random Predicted: Tumour Non-Tumour Tumour Non-Tumour Tumour Non-Tumour Actual Label:             Tumour (Positive) 38.00 2 18 $ -22 000 000.0 21 MISSING Non-Tumour (Negative) 19.00 439 6 $ -452 000 000.0 226 232 Using Styler to manipulate the display is a useful feature because maintaining the indexing and datavalues for other purposes gives greater control. You do not have to overwrite your DataFrame to display it how you like. Here is an example of using the formatting functions whilst still relying on the underlying data for indexing and calculations. [6]: weather_df = pd.DataFrame(np.random.rand(10,2)*5, index=pd.date_range(start="2021-01-01", periods=10), columns=["Tokyo", "Beijing"]) def rain_condition(v): if v < 1.75: return "Dry" elif v < 2.75: return "Rain" return "Heavy Rain" def make_pretty(styler): styler.set_caption("Weather Conditions") styler.format(rain_condition) styler.format_index(lambda v: v.strftime("%A")) styler.background_gradient(axis=None, vmin=1, vmax=5, cmap="YlGnBu") return styler weather_df [6]: Tokyo Beijing 2021-01-01 1.156896 0.483482 2021-01-02 4.274907 2.740275 2021-01-03 2.347367 4.046314 2021-01-04 4.762118 1.187866 2021-01-05 3.364955 1.436871 2021-01-06 1.714027 0.031307 2021-01-07 2.402132 4.665891 2021-01-08 3.262800 0.759015 2021-01-09 4.260268 2.226552 2021-01-10 4.277346 2.286653 [7]: weather_df.loc["2021-01-04":"2021-01-08"].style.pipe(make_pretty) [7]: Weather Conditions   Tokyo Beijing Monday Heavy Rain Dry Tuesday Heavy Rain Dry Wednesday Dry Dry Thursday Rain Heavy Rain Friday Heavy Rain Dry Hiding Data# The index and column headers can be completely hidden, as well subselecting rows or columns that one wishes to exclude. Both these options are performed using the same methods. The index can be hidden from rendering by calling .hide() without any arguments, which might be useful if your index is integer based. Similarly column headers can be hidden by calling .hide(axis=“columns”) without any further arguments. Specific rows or columns can be hidden from rendering by calling the same .hide() method and passing in a row/column label, a list-like or a slice of row/column labels to for the subset argument. Hiding does not change the integer arrangement of CSS classes, e.g. hiding the first two columns of a DataFrame means the column class indexing will still start at col2, since col0 and col1 are simply ignored. We can update our Styler object from before to hide some data and format the values. [8]: s = df.style.format('{:.0f}').hide([('Random', 'Tumour'), ('Random', 'Non-Tumour')], axis="columns") s [8]: Model: Decision Tree Regression Predicted: Tumour Non-Tumour Tumour Non-Tumour Actual Label:         Tumour (Positive) 38 2 18 22 Non-Tumour (Negative) 19 439 6 452 Methods to Add Styles# There are 3 primary methods of adding custom CSS styles to Styler: Using .set_table_styles() to control broader areas of the table with specified internal CSS. Although table styles allow the flexibility to add CSS selectors and properties controlling all individual parts of the table, they are unwieldy for individual cell specifications. Also, note that table styles cannot be exported to Excel. Using .set_td_classes() to directly link either external CSS classes to your data cells or link the internal CSS classes created by .set_table_styles(). See here. These cannot be used on column header rows or indexes, and also won’t export to Excel. Using the .apply() and .applymap() functions to add direct internal CSS to specific data cells. See here. As of v1.4.0 there are also methods that work directly on column header rows or indexes; .apply_index() and .applymap_index(). Note that only these methods add styles that will export to Excel. These methods work in a similar way to DataFrame.apply() and DataFrame.applymap(). Table Styles# Table styles are flexible enough to control all individual parts of the table, including column headers and indexes. However, they can be unwieldy to type for individual data cells or for any kind of conditional formatting, so we recommend that table styles are used for broad styling, such as entire rows or columns at a time. Table styles are also used to control features which can apply to the whole table at once such as creating a generic hover functionality. The :hover pseudo-selector, as well as other pseudo-selectors, can only be used this way. To replicate the normal format of CSS selectors and properties (attribute value pairs), e.g. tr:hover { background-color: #ffff99; } the necessary format to pass styles to .set_table_styles() is as a list of dicts, each with a CSS-selector tag and CSS-properties. Properties can either be a list of 2-tuples, or a regular CSS-string, for example: [10]: cell_hover = { # for row hover use <tr> instead of <td> 'selector': 'td:hover', 'props': [('background-color', '#ffffb3')] } index_names = { 'selector': '.index_name', 'props': 'font-style: italic; color: darkgrey; font-weight:normal;' } headers = { 'selector': 'th:not(.index_name)', 'props': 'background-color: #000066; color: white;' } s.set_table_styles([cell_hover, index_names, headers]) [10]: Model: Decision Tree Regression Predicted: Tumour Non-Tumour Tumour Non-Tumour Actual Label:         Tumour (Positive) 38 2 18 22 Non-Tumour (Negative) 19 439 6 452 Next we just add a couple more styling artifacts targeting specific parts of the table. Be careful here, since we are chaining methods we need to explicitly instruct the method not to overwrite the existing styles. [12]: s.set_table_styles([ {'selector': 'th.col_heading', 'props': 'text-align: center;'}, {'selector': 'th.col_heading.level0', 'props': 'font-size: 1.5em;'}, {'selector': 'td', 'props': 'text-align: center; font-weight: bold;'}, ], overwrite=False) [12]: Model: Decision Tree Regression Predicted: Tumour Non-Tumour Tumour Non-Tumour Actual Label:         Tumour (Positive) 38 2 18 22 Non-Tumour (Negative) 19 439 6 452 As a convenience method (since version 1.2.0) we can also pass a dict to .set_table_styles() which contains row or column keys. Behind the scenes Styler just indexes the keys and adds relevant .col<m> or .row<n> classes as necessary to the given CSS selectors. [14]: s.set_table_styles({ ('Regression', 'Tumour'): [{'selector': 'th', 'props': 'border-left: 1px solid white'}, {'selector': 'td', 'props': 'border-left: 1px solid #000066'}] }, overwrite=False, axis=0) [14]: Model: Decision Tree Regression Predicted: Tumour Non-Tumour Tumour Non-Tumour Actual Label:         Tumour (Positive) 38 2 18 22 Non-Tumour (Negative) 19 439 6 452 Setting Classes and Linking to External CSS# If you have designed a website then it is likely you will already have an external CSS file that controls the styling of table and cell objects within it. You may want to use these native files rather than duplicate all the CSS in python (and duplicate any maintenance work). Table Attributes# It is very easy to add a class to the main <table> using .set_table_attributes(). This method can also attach inline styles - read more in CSS Hierarchies. [16]: out = s.set_table_attributes('class="my-table-cls"').to_html() print(out[out.find('<table'):][:109]) <table id="T_xyz01" class="my-table-cls"> <thead> <tr> <th class="index_name level0" >Model:</th> Data Cell CSS Classes# New in version 1.2.0 The .set_td_classes() method accepts a DataFrame with matching indices and columns to the underlying Styler’s DataFrame. That DataFrame will contain strings as css-classes to add to individual data cells: the <td> elements of the <table>. Rather than use external CSS we will create our classes internally and add them to table style. We will save adding the borders until the section on tooltips. [17]: s.set_table_styles([ # create internal CSS classes {'selector': '.true', 'props': 'background-color: #e6ffe6;'}, {'selector': '.false', 'props': 'background-color: #ffe6e6;'}, ], overwrite=False) cell_color = pd.DataFrame([['true ', 'false ', 'true ', 'false '], ['false ', 'true ', 'false ', 'true ']], index=df.index, columns=df.columns[:4]) s.set_td_classes(cell_color) [17]: Model: Decision Tree Regression Predicted: Tumour Non-Tumour Tumour Non-Tumour Actual Label:         Tumour (Positive) 38 2 18 22 Non-Tumour (Negative) 19 439 6 452 Styler Functions# Acting on Data# We use the following methods to pass your style functions. Both of those methods take a function (and some other keyword arguments) and apply it to the DataFrame in a certain way, rendering CSS styles. .applymap() (elementwise): accepts a function that takes a single value and returns a string with the CSS attribute-value pair. .apply() (column-/row-/table-wise): accepts a function that takes a Series or DataFrame and returns a Series, DataFrame, or numpy array with an identical shape where each element is a string with a CSS attribute-value pair. This method passes each column or row of your DataFrame one-at-a-time or the entire table at once, depending on the axis keyword argument. For columnwise use axis=0, rowwise use axis=1, and for the entire table at once use axis=None. This method is powerful for applying multiple, complex logic to data cells. We create a new DataFrame to demonstrate this. [19]: np.random.seed(0) df2 = pd.DataFrame(np.random.randn(10,4), columns=['A','B','C','D']) df2.style [19]:   A B C D 0 1.764052 0.400157 0.978738 2.240893 1 1.867558 -0.977278 0.950088 -0.151357 2 -0.103219 0.410599 0.144044 1.454274 3 0.761038 0.121675 0.443863 0.333674 4 1.494079 -0.205158 0.313068 -0.854096 5 -2.552990 0.653619 0.864436 -0.742165 6 2.269755 -1.454366 0.045759 -0.187184 7 1.532779 1.469359 0.154947 0.378163 8 -0.887786 -1.980796 -0.347912 0.156349 9 1.230291 1.202380 -0.387327 -0.302303 For example we can build a function that colors text if it is negative, and chain this with a function that partially fades cells of negligible value. Since this looks at each element in turn we use applymap. [20]: def style_negative(v, props=''): return props if v < 0 else None s2 = df2.style.applymap(style_negative, props='color:red;')\ .applymap(lambda v: 'opacity: 20%;' if (v < 0.3) and (v > -0.3) else None) s2 [20]:   A B C D 0 1.764052 0.400157 0.978738 2.240893 1 1.867558 -0.977278 0.950088 -0.151357 2 -0.103219 0.410599 0.144044 1.454274 3 0.761038 0.121675 0.443863 0.333674 4 1.494079 -0.205158 0.313068 -0.854096 5 -2.552990 0.653619 0.864436 -0.742165 6 2.269755 -1.454366 0.045759 -0.187184 7 1.532779 1.469359 0.154947 0.378163 8 -0.887786 -1.980796 -0.347912 0.156349 9 1.230291 1.202380 -0.387327 -0.302303 We can also build a function that highlights the maximum value across rows, cols, and the DataFrame all at once. In this case we use apply. Below we highlight the maximum in a column. [22]: def highlight_max(s, props=''): return np.where(s == np.nanmax(s.values), props, '') s2.apply(highlight_max, props='color:white;background-color:darkblue', axis=0) [22]:   A B C D 0 1.764052 0.400157 0.978738 2.240893 1 1.867558 -0.977278 0.950088 -0.151357 2 -0.103219 0.410599 0.144044 1.454274 3 0.761038 0.121675 0.443863 0.333674 4 1.494079 -0.205158 0.313068 -0.854096 5 -2.552990 0.653619 0.864436 -0.742165 6 2.269755 -1.454366 0.045759 -0.187184 7 1.532779 1.469359 0.154947 0.378163 8 -0.887786 -1.980796 -0.347912 0.156349 9 1.230291 1.202380 -0.387327 -0.302303 We can use the same function across the different axes, highlighting here the DataFrame maximum in purple, and row maximums in pink. [24]: s2.apply(highlight_max, props='color:white;background-color:pink;', axis=1)\ .apply(highlight_max, props='color:white;background-color:purple', axis=None) [24]:   A B C D 0 1.764052 0.400157 0.978738 2.240893 1 1.867558 -0.977278 0.950088 -0.151357 2 -0.103219 0.410599 0.144044 1.454274 3 0.761038 0.121675 0.443863 0.333674 4 1.494079 -0.205158 0.313068 -0.854096 5 -2.552990 0.653619 0.864436 -0.742165 6 2.269755 -1.454366 0.045759 -0.187184 7 1.532779 1.469359 0.154947 0.378163 8 -0.887786 -1.980796 -0.347912 0.156349 9 1.230291 1.202380 -0.387327 -0.302303 This last example shows how some styles have been overwritten by others. In general the most recent style applied is active but you can read more in the section on CSS hierarchies. You can also apply these styles to more granular parts of the DataFrame - read more in section on subset slicing. It is possible to replicate some of this functionality using just classes but it can be more cumbersome. See item 3) of Optimization Debugging Tip: If you’re having trouble writing your style function, try just passing it into DataFrame.apply. Internally, Styler.apply uses DataFrame.apply so the result should be the same, and with DataFrame.apply you will be able to inspect the CSS string output of your intended function in each cell. Acting on the Index and Column Headers# Similar application is achieved for headers by using: .applymap_index() (elementwise): accepts a function that takes a single value and returns a string with the CSS attribute-value pair. .apply_index() (level-wise): accepts a function that takes a Series and returns a Series, or numpy array with an identical shape where each element is a string with a CSS attribute-value pair. This method passes each level of your Index one-at-a-time. To style the index use axis=0 and to style the column headers use axis=1. You can select a level of a MultiIndex but currently no similar subset application is available for these methods. [26]: s2.applymap_index(lambda v: "color:pink;" if v>4 else "color:darkblue;", axis=0) s2.apply_index(lambda s: np.where(s.isin(["A", "B"]), "color:pink;", "color:darkblue;"), axis=1) [26]:   A B C D 0 1.764052 0.400157 0.978738 2.240893 1 1.867558 -0.977278 0.950088 -0.151357 2 -0.103219 0.410599 0.144044 1.454274 3 0.761038 0.121675 0.443863 0.333674 4 1.494079 -0.205158 0.313068 -0.854096 5 -2.552990 0.653619 0.864436 -0.742165 6 2.269755 -1.454366 0.045759 -0.187184 7 1.532779 1.469359 0.154947 0.378163 8 -0.887786 -1.980796 -0.347912 0.156349 9 1.230291 1.202380 -0.387327 -0.302303 Tooltips and Captions# Table captions can be added with the .set_caption() method. You can use table styles to control the CSS relevant to the caption. [27]: s.set_caption("Confusion matrix for multiple cancer prediction models.")\ .set_table_styles([{ 'selector': 'caption', 'props': 'caption-side: bottom; font-size:1.25em;' }], overwrite=False) [27]: Confusion matrix for multiple cancer prediction models. Model: Decision Tree Regression Predicted: Tumour Non-Tumour Tumour Non-Tumour Actual Label:         Tumour (Positive) 38 2 18 22 Non-Tumour (Negative) 19 439 6 452 Adding tooltips (since version 1.3.0) can be done using the .set_tooltips() method in the same way you can add CSS classes to data cells by providing a string based DataFrame with intersecting indices and columns. You don’t have to specify a css_class name or any css props for the tooltips, since there are standard defaults, but the option is there if you want more visual control. [29]: tt = pd.DataFrame([['This model has a very strong true positive rate', "This model's total number of false negatives is too high"]], index=['Tumour (Positive)'], columns=df.columns[[0,3]]) s.set_tooltips(tt, props='visibility: hidden; position: absolute; z-index: 1; border: 1px solid #000066;' 'background-color: white; color: #000066; font-size: 0.8em;' 'transform: translate(0px, -24px); padding: 0.6em; border-radius: 0.5em;') [29]: Confusion matrix for multiple cancer prediction models. Model: Decision Tree Regression Predicted: Tumour Non-Tumour Tumour Non-Tumour Actual Label:         Tumour (Positive) 38 2 18 22 Non-Tumour (Negative) 19 439 6 452 The only thing left to do for our table is to add the highlighting borders to draw the audience attention to the tooltips. We will create internal CSS classes as before using table styles. Setting classes always overwrites so we need to make sure we add the previous classes. [31]: s.set_table_styles([ # create internal CSS classes {'selector': '.border-red', 'props': 'border: 2px dashed red;'}, {'selector': '.border-green', 'props': 'border: 2px dashed green;'}, ], overwrite=False) cell_border = pd.DataFrame([['border-green ', ' ', ' ', 'border-red '], [' ', ' ', ' ', ' ']], index=df.index, columns=df.columns[:4]) s.set_td_classes(cell_color + cell_border) [31]: Confusion matrix for multiple cancer prediction models. Model: Decision Tree Regression Predicted: Tumour Non-Tumour Tumour Non-Tumour Actual Label:         Tumour (Positive) 38 2 18 22 Non-Tumour (Negative) 19 439 6 452 Finer Control with Slicing# The examples we have shown so far for the Styler.apply and Styler.applymap functions have not demonstrated the use of the subset argument. This is a useful argument which permits a lot of flexibility: it allows you to apply styles to specific rows or columns, without having to code that logic into your style function. The value passed to subset behaves similar to slicing a DataFrame; A scalar is treated as a column label A list (or Series or NumPy array) is treated as multiple column labels A tuple is treated as (row_indexer, column_indexer) Consider using pd.IndexSlice to construct the tuple for the last one. We will create a MultiIndexed DataFrame to demonstrate the functionality. [33]: df3 = pd.DataFrame(np.random.randn(4,4), pd.MultiIndex.from_product([['A', 'B'], ['r1', 'r2']]), columns=['c1','c2','c3','c4']) df3 [33]: c1 c2 c3 c4 A r1 -1.048553 -1.420018 -1.706270 1.950775 r2 -0.509652 -0.438074 -1.252795 0.777490 B r1 -1.613898 -0.212740 -0.895467 0.386902 r2 -0.510805 -1.180632 -0.028182 0.428332 We will use subset to highlight the maximum in the third and fourth columns with red text. We will highlight the subset sliced region in yellow. [34]: slice_ = ['c3', 'c4'] df3.style.apply(highlight_max, props='color:red;', axis=0, subset=slice_)\ .set_properties(**{'background-color': '#ffffb3'}, subset=slice_) [34]:     c1 c2 c3 c4 A r1 -1.048553 -1.420018 -1.706270 1.950775 r2 -0.509652 -0.438074 -1.252795 0.777490 B r1 -1.613898 -0.212740 -0.895467 0.386902 r2 -0.510805 -1.180632 -0.028182 0.428332 If combined with the IndexSlice as suggested then it can index across both dimensions with greater flexibility. [35]: idx = pd.IndexSlice slice_ = idx[idx[:,'r1'], idx['c2':'c4']] df3.style.apply(highlight_max, props='color:red;', axis=0, subset=slice_)\ .set_properties(**{'background-color': '#ffffb3'}, subset=slice_) [35]:     c1 c2 c3 c4 A r1 -1.048553 -1.420018 -1.706270 1.950775 r2 -0.509652 -0.438074 -1.252795 0.777490 B r1 -1.613898 -0.212740 -0.895467 0.386902 r2 -0.510805 -1.180632 -0.028182 0.428332 This also provides the flexibility to sub select rows when used with the axis=1. [36]: slice_ = idx[idx[:,'r2'], :] df3.style.apply(highlight_max, props='color:red;', axis=1, subset=slice_)\ .set_properties(**{'background-color': '#ffffb3'}, subset=slice_) [36]:     c1 c2 c3 c4 A r1 -1.048553 -1.420018 -1.706270 1.950775 r2 -0.509652 -0.438074 -1.252795 0.777490 B r1 -1.613898 -0.212740 -0.895467 0.386902 r2 -0.510805 -1.180632 -0.028182 0.428332 There is also scope to provide conditional filtering. Suppose we want to highlight the maximum across columns 2 and 4 only in the case that the sum of columns 1 and 3 is less than -2.0 (essentially excluding rows (:,'r2')). [37]: slice_ = idx[idx[(df3['c1'] + df3['c3']) < -2.0], ['c2', 'c4']] df3.style.apply(highlight_max, props='color:red;', axis=1, subset=slice_)\ .set_properties(**{'background-color': '#ffffb3'}, subset=slice_) [37]:     c1 c2 c3 c4 A r1 -1.048553 -1.420018 -1.706270 1.950775 r2 -0.509652 -0.438074 -1.252795 0.777490 B r1 -1.613898 -0.212740 -0.895467 0.386902 r2 -0.510805 -1.180632 -0.028182 0.428332 Only label-based slicing is supported right now, not positional, and not callables. If your style function uses a subset or axis keyword argument, consider wrapping your function in a functools.partial, partialing out that keyword. my_func2 = functools.partial(my_func, subset=42) Optimization# Generally, for smaller tables and most cases, the rendered HTML does not need to be optimized, and we don’t really recommend it. There are two cases where it is worth considering: If you are rendering and styling a very large HTML table, certain browsers have performance issues. If you are using Styler to dynamically create part of online user interfaces and want to improve network performance. Here we recommend the following steps to implement: 1. Remove UUID and cell_ids# Ignore the uuid and set cell_ids to False. This will prevent unnecessary HTML. This is sub-optimal: [38]: df4 = pd.DataFrame([[1,2],[3,4]]) s4 = df4.style This is better: [39]: from pandas.io.formats.style import Styler s4 = Styler(df4, uuid_len=0, cell_ids=False) 2. Use table styles# Use table styles where possible (e.g. for all cells or rows or columns at a time) since the CSS is nearly always more efficient than other formats. This is sub-optimal: [40]: props = 'font-family: "Times New Roman", Times, serif; color: #e83e8c; font-size:1.3em;' df4.style.applymap(lambda x: props, subset=[1]) [40]:   0 1 0 1 2 1 3 4 This is better: [41]: df4.style.set_table_styles([{'selector': 'td.col1', 'props': props}]) [41]:   0 1 0 1 2 1 3 4 3. Set classes instead of using Styler functions# For large DataFrames where the same style is applied to many cells it can be more efficient to declare the styles as classes and then apply those classes to data cells, rather than directly applying styles to cells. It is, however, probably still easier to use the Styler function api when you are not concerned about optimization. This is sub-optimal: [42]: df2.style.apply(highlight_max, props='color:white;background-color:darkblue;', axis=0)\ .apply(highlight_max, props='color:white;background-color:pink;', axis=1)\ .apply(highlight_max, props='color:white;background-color:purple', axis=None) [42]:   A B C D 0 1.764052 0.400157 0.978738 2.240893 1 1.867558 -0.977278 0.950088 -0.151357 2 -0.103219 0.410599 0.144044 1.454274 3 0.761038 0.121675 0.443863 0.333674 4 1.494079 -0.205158 0.313068 -0.854096 5 -2.552990 0.653619 0.864436 -0.742165 6 2.269755 -1.454366 0.045759 -0.187184 7 1.532779 1.469359 0.154947 0.378163 8 -0.887786 -1.980796 -0.347912 0.156349 9 1.230291 1.202380 -0.387327 -0.302303 This is better: [43]: build = lambda x: pd.DataFrame(x, index=df2.index, columns=df2.columns) cls1 = build(df2.apply(highlight_max, props='cls-1 ', axis=0)) cls2 = build(df2.apply(highlight_max, props='cls-2 ', axis=1, result_type='expand').values) cls3 = build(highlight_max(df2, props='cls-3 ')) df2.style.set_table_styles([ {'selector': '.cls-1', 'props': 'color:white;background-color:darkblue;'}, {'selector': '.cls-2', 'props': 'color:white;background-color:pink;'}, {'selector': '.cls-3', 'props': 'color:white;background-color:purple;'} ]).set_td_classes(cls1 + cls2 + cls3) [43]:   A B C D 0 1.764052 0.400157 0.978738 2.240893 1 1.867558 -0.977278 0.950088 -0.151357 2 -0.103219 0.410599 0.144044 1.454274 3 0.761038 0.121675 0.443863 0.333674 4 1.494079 -0.205158 0.313068 -0.854096 5 -2.552990 0.653619 0.864436 -0.742165 6 2.269755 -1.454366 0.045759 -0.187184 7 1.532779 1.469359 0.154947 0.378163 8 -0.887786 -1.980796 -0.347912 0.156349 9 1.230291 1.202380 -0.387327 -0.302303 4. Don’t use tooltips# Tooltips require cell_ids to work and they generate extra HTML elements for every data cell. 5. If every byte counts use string replacement# You can remove unnecessary HTML, or shorten the default class names by replacing the default css dict. You can read a little more about CSS below. [44]: my_css = { "row_heading": "", "col_heading": "", "index_name": "", "col": "c", "row": "r", "col_trim": "", "row_trim": "", "level": "l", "data": "", "blank": "", } html = Styler(df4, uuid_len=0, cell_ids=False) html.set_table_styles([{'selector': 'td', 'props': props}, {'selector': '.c1', 'props': 'color:green;'}, {'selector': '.l0', 'props': 'color:blue;'}], css_class_names=my_css) print(html.to_html()) <style type="text/css"> #T_ td { font-family: "Times New Roman", Times, serif; color: #e83e8c; font-size: 1.3em; } #T_ .c1 { color: green; } #T_ .l0 { color: blue; } </style> <table id="T_"> <thead> <tr> <th class=" l0" >&nbsp;</th> <th class=" l0 c0" >0</th> <th class=" l0 c1" >1</th> </tr> </thead> <tbody> <tr> <th class=" l0 r0" >0</th> <td class=" r0 c0" >1</td> <td class=" r0 c1" >2</td> </tr> <tr> <th class=" l0 r1" >1</th> <td class=" r1 c0" >3</td> <td class=" r1 c1" >4</td> </tr> </tbody> </table> [45]: html [45]:   0 1 0 1 2 1 3 4 Builtin Styles# Some styling functions are common enough that we’ve “built them in” to the Styler, so you don’t have to write them and apply them yourself. The current list of such functions is: .highlight_null: for use with identifying missing data. .highlight_min and .highlight_max: for use with identifying extremeties in data. .highlight_between and .highlight_quantile: for use with identifying classes within data. .background_gradient: a flexible method for highlighting cells based on their, or other, values on a numeric scale. .text_gradient: similar method for highlighting text based on their, or other, values on a numeric scale. .bar: to display mini-charts within cell backgrounds. The individual documentation on each function often gives more examples of their arguments. Highlight Null# [46]: df2.iloc[0,2] = np.nan df2.iloc[4,3] = np.nan df2.loc[:4].style.highlight_null(color='yellow') [46]:   A B C D 0 1.764052 0.400157 nan 2.240893 1 1.867558 -0.977278 0.950088 -0.151357 2 -0.103219 0.410599 0.144044 1.454274 3 0.761038 0.121675 0.443863 0.333674 4 1.494079 -0.205158 0.313068 nan Highlight Min or Max# [47]: df2.loc[:4].style.highlight_max(axis=1, props='color:white; font-weight:bold; background-color:darkblue;') [47]:   A B C D 0 1.764052 0.400157 nan 2.240893 1 1.867558 -0.977278 0.950088 -0.151357 2 -0.103219 0.410599 0.144044 1.454274 3 0.761038 0.121675 0.443863 0.333674 4 1.494079 -0.205158 0.313068 nan Highlight Between# This method accepts ranges as float, or NumPy arrays or Series provided the indexes match. [48]: left = pd.Series([1.0, 0.0, 1.0], index=["A", "B", "D"]) df2.loc[:4].style.highlight_between(left=left, right=1.5, axis=1, props='color:white; background-color:purple;') [48]:   A B C D 0 1.764052 0.400157 nan 2.240893 1 1.867558 -0.977278 0.950088 -0.151357 2 -0.103219 0.410599 0.144044 1.454274 3 0.761038 0.121675 0.443863 0.333674 4 1.494079 -0.205158 0.313068 nan Highlight Quantile# Useful for detecting the highest or lowest percentile values [49]: df2.loc[:4].style.highlight_quantile(q_left=0.85, axis=None, color='yellow') [49]:   A B C D 0 1.764052 0.400157 nan 2.240893 1 1.867558 -0.977278 0.950088 -0.151357 2 -0.103219 0.410599 0.144044 1.454274 3 0.761038 0.121675 0.443863 0.333674 4 1.494079 -0.205158 0.313068 nan Background Gradient and Text Gradient# You can create “heatmaps” with the background_gradient and text_gradient methods. These require matplotlib, and we’ll use Seaborn to get a nice colormap. [50]: import seaborn as sns cm = sns.light_palette("green", as_cmap=True) df2.style.background_gradient(cmap=cm) [50]:   A B C D 0 1.764052 0.400157 nan 2.240893 1 1.867558 -0.977278 0.950088 -0.151357 2 -0.103219 0.410599 0.144044 1.454274 3 0.761038 0.121675 0.443863 0.333674 4 1.494079 -0.205158 0.313068 nan 5 -2.552990 0.653619 0.864436 -0.742165 6 2.269755 -1.454366 0.045759 -0.187184 7 1.532779 1.469359 0.154947 0.378163 8 -0.887786 -1.980796 -0.347912 0.156349 9 1.230291 1.202380 -0.387327 -0.302303 [51]: df2.style.text_gradient(cmap=cm) [51]:   A B C D 0 1.764052 0.400157 nan 2.240893 1 1.867558 -0.977278 0.950088 -0.151357 2 -0.103219 0.410599 0.144044 1.454274 3 0.761038 0.121675 0.443863 0.333674 4 1.494079 -0.205158 0.313068 nan 5 -2.552990 0.653619 0.864436 -0.742165 6 2.269755 -1.454366 0.045759 -0.187184 7 1.532779 1.469359 0.154947 0.378163 8 -0.887786 -1.980796 -0.347912 0.156349 9 1.230291 1.202380 -0.387327 -0.302303 .background_gradient and .text_gradient have a number of keyword arguments to customise the gradients and colors. See the documentation. Set properties# Use Styler.set_properties when the style doesn’t actually depend on the values. This is just a simple wrapper for .applymap where the function returns the same properties for all cells. [52]: df2.loc[:4].style.set_properties(**{'background-color': 'black', 'color': 'lawngreen', 'border-color': 'white'}) [52]:   A B C D 0 1.764052 0.400157 nan 2.240893 1 1.867558 -0.977278 0.950088 -0.151357 2 -0.103219 0.410599 0.144044 1.454274 3 0.761038 0.121675 0.443863 0.333674 4 1.494079 -0.205158 0.313068 nan Bar charts# You can include “bar charts” in your DataFrame. [53]: df2.style.bar(subset=['A', 'B'], color='#d65f5f') [53]:   A B C D 0 1.764052 0.400157 nan 2.240893 1 1.867558 -0.977278 0.950088 -0.151357 2 -0.103219 0.410599 0.144044 1.454274 3 0.761038 0.121675 0.443863 0.333674 4 1.494079 -0.205158 0.313068 nan 5 -2.552990 0.653619 0.864436 -0.742165 6 2.269755 -1.454366 0.045759 -0.187184 7 1.532779 1.469359 0.154947 0.378163 8 -0.887786 -1.980796 -0.347912 0.156349 9 1.230291 1.202380 -0.387327 -0.302303 Additional keyword arguments give more control on centering and positioning, and you can pass a list of [color_negative, color_positive] to highlight lower and higher values or a matplotlib colormap. To showcase an example here’s how you can change the above with the new align option, combined with setting vmin and vmax limits, the width of the figure, and underlying css props of cells, leaving space to display the text and the bars. We also use text_gradient to color the text the same as the bars using a matplotlib colormap (although in this case the visualization is probably better without this additional effect). [54]: df2.style.format('{:.3f}', na_rep="")\ .bar(align=0, vmin=-2.5, vmax=2.5, cmap="bwr", height=50, width=60, props="width: 120px; border-right: 1px solid black;")\ .text_gradient(cmap="bwr", vmin=-2.5, vmax=2.5) [54]:   A B C D 0 1.764 0.400 2.241 1 1.868 -0.977 0.950 -0.151 2 -0.103 0.411 0.144 1.454 3 0.761 0.122 0.444 0.334 4 1.494 -0.205 0.313 5 -2.553 0.654 0.864 -0.742 6 2.270 -1.454 0.046 -0.187 7 1.533 1.469 0.155 0.378 8 -0.888 -1.981 -0.348 0.156 9 1.230 1.202 -0.387 -0.302 The following example aims to give a highlight of the behavior of the new align options: [56]: HTML(head) [56]: Align All Negative Both Neg and Pos All Positive Large Positive left -100 -60 -30 -20 -10 -5 0 90 10 20 50 100 100 103 101 102 right -100 -60 -30 -20 -10 -5 0 90 10 20 50 100 100 103 101 102 zero -100 -60 -30 -20 -10 -5 0 90 10 20 50 100 100 103 101 102 mid -100 -60 -30 -20 -10 -5 0 90 10 20 50 100 100 103 101 102 mean -100 -60 -30 -20 -10 -5 0 90 10 20 50 100 100 103 101 102 99 -100 -60 -30 -20 -10 -5 0 90 10 20 50 100 100 103 101 102 Sharing styles# Say you have a lovely style built up for a DataFrame, and now you want to apply the same style to a second DataFrame. Export the style with df1.style.export, and import it on the second DataFrame with df1.style.set [57]: style1 = df2.style\ .applymap(style_negative, props='color:red;')\ .applymap(lambda v: 'opacity: 20%;' if (v < 0.3) and (v > -0.3) else None)\ .set_table_styles([{"selector": "th", "props": "color: blue;"}])\ .hide(axis="index") style1 [57]: A B C D 1.764052 0.400157 nan 2.240893 1.867558 -0.977278 0.950088 -0.151357 -0.103219 0.410599 0.144044 1.454274 0.761038 0.121675 0.443863 0.333674 1.494079 -0.205158 0.313068 nan -2.552990 0.653619 0.864436 -0.742165 2.269755 -1.454366 0.045759 -0.187184 1.532779 1.469359 0.154947 0.378163 -0.887786 -1.980796 -0.347912 0.156349 1.230291 1.202380 -0.387327 -0.302303 [58]: style2 = df3.style style2.use(style1.export()) style2 [58]: c1 c2 c3 c4 -1.048553 -1.420018 -1.706270 1.950775 -0.509652 -0.438074 -1.252795 0.777490 -1.613898 -0.212740 -0.895467 0.386902 -0.510805 -1.180632 -0.028182 0.428332 Notice that you’re able to share the styles even though they’re data aware. The styles are re-evaluated on the new DataFrame they’ve been used upon. Limitations# DataFrame only (use Series.to_frame().style) The index and columns do not need to be unique, but certain styling functions can only work with unique indexes. No large repr, and construction performance isn’t great; although we have some HTML optimizations You can only apply styles, you can’t insert new HTML entities, except via subclassing. Other Fun and Useful Stuff# Here are a few interesting examples. Widgets# Styler interacts pretty well with widgets. If you’re viewing this online instead of running the notebook yourself, you’re missing out on interactively adjusting the color palette. [59]: from ipywidgets import widgets @widgets.interact def f(h_neg=(0, 359, 1), h_pos=(0, 359), s=(0., 99.9), l=(0., 99.9)): return df2.style.background_gradient( cmap=sns.palettes.diverging_palette(h_neg=h_neg, h_pos=h_pos, s=s, l=l, as_cmap=True) ) Magnify# [60]: def magnify(): return [dict(selector="th", props=[("font-size", "4pt")]), dict(selector="td", props=[('padding', "0em 0em")]), dict(selector="th:hover", props=[("font-size", "12pt")]), dict(selector="tr:hover td:hover", props=[('max-width', '200px'), ('font-size', '12pt')]) ] [61]: np.random.seed(25) cmap = cmap=sns.diverging_palette(5, 250, as_cmap=True) bigdf = pd.DataFrame(np.random.randn(20, 25)).cumsum() bigdf.style.background_gradient(cmap, axis=1)\ .set_properties(**{'max-width': '80px', 'font-size': '1pt'})\ .set_caption("Hover to magnify")\ .format(precision=2)\ .set_table_styles(magnify()) [61]: Hover to magnify   0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 0 0.23 1.03 -0.84 -0.59 -0.96 -0.22 -0.62 1.84 -2.05 0.87 -0.92 -0.23 2.15 -1.33 0.08 -1.25 1.20 -1.05 1.06 -0.42 2.29 -2.59 2.82 0.68 -1.58 1 -1.75 1.56 -1.13 -1.10 1.03 0.00 -2.46 3.45 -1.66 1.27 -0.52 -0.02 1.52 -1.09 -1.86 -1.13 -0.68 -0.81 0.35 -0.06 1.79 -2.82 2.26 0.78 0.44 2 -0.65 3.22 -1.76 0.52 2.20 -0.37 -3.00 3.73 -1.87 2.46 0.21 -0.24 -0.10 -0.78 -3.02 -0.82 -0.21 -0.23 0.86 -0.68 1.45 -4.89 3.03 1.91 0.61 3 -1.62 3.71 -2.31 0.43 4.17 -0.43 -3.86 4.16 -2.15 1.08 0.12 0.60 -0.89 0.27 -3.67 -2.71 -0.31 -1.59 1.35 -1.83 0.91 -5.80 2.81 2.11 0.28 4 -3.35 4.48 -1.86 -1.70 5.19 -1.02 -3.81 4.72 -0.72 1.08 -0.18 0.83 -0.22 -1.08 -4.27 -2.88 -0.97 -1.78 1.53 -1.80 2.21 -6.34 3.34 2.49 2.09 5 -0.84 4.23 -1.65 -2.00 5.34 -0.99 -4.13 3.94 -1.06 -0.94 1.24 0.09 -1.78 -0.11 -4.45 -0.85 -2.06 -1.35 0.80 -1.63 1.54 -6.51 2.80 2.14 3.77 6 -0.74 5.35 -2.11 -1.13 4.20 -1.85 -3.20 3.76 -3.22 -1.23 0.34 0.57 -1.82 0.54 -4.43 -1.83 -4.03 -2.62 -0.20 -4.68 1.93 -8.46 3.34 2.52 5.81 7 -0.44 4.69 -2.30 -0.21 5.93 -2.63 -1.83 5.46 -4.50 -3.16 -1.73 0.18 0.11 0.04 -5.99 -0.45 -6.20 -3.89 0.71 -3.95 0.67 -7.26 2.97 3.39 6.66 8 0.92 5.80 -3.33 -0.65 5.99 -3.19 -1.83 5.63 -3.53 -1.30 -1.61 0.82 -2.45 -0.40 -6.06 -0.52 -6.60 -3.48 -0.04 -4.60 0.51 -5.85 3.23 2.40 5.08 9 0.38 5.54 -4.49 -0.80 7.05 -2.64 -0.44 5.35 -1.96 -0.33 -0.80 0.26 -3.37 -0.82 -6.05 -2.61 -8.45 -4.45 0.41 -4.71 1.89 -6.93 2.14 3.00 5.16 10 2.06 5.84 -3.90 -0.98 7.78 -2.49 -0.59 5.59 -2.22 -0.71 -0.46 1.80 -2.79 0.48 -5.97 -3.44 -7.77 -5.49 -0.70 -4.61 -0.52 -7.72 1.54 5.02 5.81 11 1.86 4.47 -2.17 -1.38 5.90 -0.49 0.02 5.78 -1.04 -0.60 0.49 1.96 -1.47 1.88 -5.92 -4.55 -8.15 -3.42 -2.24 -4.33 -1.17 -7.90 1.36 5.31 5.83 12 3.19 4.22 -3.06 -2.27 5.93 -2.64 0.33 6.72 -2.84 -0.20 1.89 2.63 -1.53 0.75 -5.27 -4.53 -7.57 -2.85 -2.17 -4.78 -1.13 -8.99 2.11 6.42 5.60 13 2.31 4.45 -3.87 -2.05 6.76 -3.25 -2.17 7.99 -2.56 -0.80 0.71 2.33 -0.16 -0.46 -5.10 -3.79 -7.58 -4.00 0.33 -3.67 -1.05 -8.71 2.47 5.87 6.71 14 3.78 4.33 -3.88 -1.58 6.22 -3.23 -1.46 5.57 -2.93 -0.33 -0.97 1.72 3.61 0.29 -4.21 -4.10 -6.68 -4.50 -2.19 -2.43 -1.64 -9.36 3.36 6.11 7.53 15 5.64 5.31 -3.98 -2.26 5.91 -3.30 -1.03 5.68 -3.06 -0.33 -1.16 2.19 4.20 1.01 -3.22 -4.31 -5.74 -4.44 -2.30 -1.36 -1.20 -11.27 2.59 6.69 5.91 16 4.08 4.34 -2.44 -3.30 6.04 -2.52 -0.47 5.28 -4.84 1.58 0.23 0.10 5.79 1.80 -3.13 -3.85 -5.53 -2.97 -2.13 -1.15 -0.56 -13.13 2.07 6.16 4.94 17 5.64 4.57 -3.53 -3.76 6.58 -2.58 -0.75 6.58 -4.78 3.63 -0.29 0.56 5.76 2.05 -2.27 -2.31 -4.95 -3.16 -3.06 -2.43 0.84 -12.57 3.56 7.36 4.70 18 5.99 5.82 -2.85 -4.15 7.12 -3.32 -1.21 7.93 -4.85 1.44 -0.63 0.35 7.47 0.87 -1.52 -2.09 -4.23 -2.55 -2.46 -2.89 1.90 -9.74 3.43 7.07 4.39 19 4.03 6.23 -4.10 -4.11 7.19 -4.10 -1.52 6.53 -5.21 -0.24 0.01 1.16 6.43 -1.97 -2.64 -1.66 -5.20 -3.25 -2.87 -1.65 1.64 -10.66 2.83 7.48 3.94 Sticky Headers# If you display a large matrix or DataFrame in a notebook, but you want to always see the column and row headers you can use the .set_sticky method which manipulates the table styles CSS. [62]: bigdf = pd.DataFrame(np.random.randn(16, 100)) bigdf.style.set_sticky(axis="index") [62]:   0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 0 -0.773866 -0.240521 -0.217165 1.173609 0.686390 0.008358 0.696232 0.173166 0.620498 0.504067 0.428066 -0.051824 0.719915 0.057165 0.562808 -0.369536 0.483399 0.620765 -0.354342 -1.469471 -1.937266 0.038031 -1.518162 -0.417599 0.386717 0.716193 0.489961 0.733957 0.914415 0.679894 0.255448 -0.508338 0.332030 -0.111107 -0.251983 -1.456620 0.409630 1.062320 -0.577115 0.718796 -0.399260 -1.311389 0.649122 0.091566 0.628872 0.297894 -0.142290 -0.542291 -0.914290 1.144514 0.313584 1.182635 1.214235 -0.416446 -1.653940 -2.550787 0.442473 0.052127 -0.464469 -0.523852 0.989726 -1.325539 -0.199687 -1.226727 0.290018 1.164574 0.817841 -0.309509 0.496599 0.943536 -0.091850 -2.802658 2.126219 -0.521161 0.288098 -0.454663 -1.676143 -0.357661 -0.788960 0.185911 -0.017106 2.454020 1.832706 -0.911743 -0.655873 -0.000514 -2.226997 0.677285 -0.140249 -0.408407 -0.838665 0.482228 1.243458 -0.477394 -0.220343 -2.463966 0.237325 -0.307380 1.172478 0.819492 1 0.405906 -0.978919 1.267526 0.145250 -1.066786 -2.114192 -1.128346 -1.082523 0.372216 0.004127 -0.211984 0.937326 -0.935890 -1.704118 0.611789 -1.030015 0.636123 -1.506193 1.736609 1.392958 1.009424 0.353266 0.697339 -0.297424 0.428702 -0.145346 -0.333553 -0.974699 0.665314 0.971944 0.121950 -1.439668 1.018808 1.442399 -0.199585 -1.165916 0.645656 1.436466 -0.921215 1.293906 -2.706443 1.460928 -0.823197 0.292952 -1.448992 0.026692 -0.975883 0.392823 0.442166 0.745741 1.187982 -0.218570 0.305288 0.054932 -1.476953 -0.114434 0.014103 0.825394 -0.060654 -0.413688 0.974836 1.339210 1.034838 0.040775 0.705001 0.017796 1.867681 -0.390173 2.285277 2.311464 -0.085070 -0.648115 0.576300 -0.790087 -1.183798 -1.334558 -0.454118 0.319302 1.706488 0.830429 0.502476 -0.079631 0.414635 0.332511 0.042935 -0.160910 0.918553 -0.292697 -1.303834 -0.199604 0.871023 -1.370681 -0.205701 -0.492973 1.123083 -0.081842 -0.118527 0.245838 -0.315742 -0.511806 2 0.011470 -0.036104 1.399603 -0.418176 -0.412229 -1.234783 -1.121500 1.196478 -0.569522 0.422022 -0.220484 0.804338 2.892667 -0.511055 -0.168722 -1.477996 -1.969917 0.471354 1.698548 0.137105 -0.762052 0.199379 -0.964346 -0.256692 1.265275 0.848762 -0.784161 1.863776 -0.355569 0.854552 0.768061 -2.075718 -2.501069 1.109868 0.957545 -0.683276 0.307764 0.733073 1.706250 -1.118091 0.374961 -1.414503 -0.524183 -1.662696 0.687921 0.521732 1.451396 -0.833491 -0.362796 -1.174444 -0.813893 -0.893220 0.770743 1.156647 -0.647444 0.125929 0.513600 -0.537874 1.992052 -1.946584 -0.104759 0.484779 -0.290936 -0.441075 0.542993 -1.050038 1.630482 0.239771 -1.177310 0.464804 -0.966995 0.646086 0.486899 1.022196 -2.267827 -1.229616 1.313805 1.073292 2.324940 -0.542720 -1.504292 0.777643 -0.618553 0.011342 1.385062 1.363552 -0.549834 0.688896 1.361288 -0.381137 0.797812 -1.128198 0.369208 0.540132 0.413853 -0.200308 -0.969126 0.981293 -0.009783 -0.320020 3 -0.574816 1.419977 0.434813 -1.101217 -1.586275 1.979573 0.378298 0.782326 2.178987 0.657564 0.683774 -0.091000 -0.059552 -0.738908 -0.907653 -0.701936 0.580039 -0.618757 0.453684 1.665382 -0.152321 0.880077 0.571073 -0.604736 0.532359 0.515031 -0.959844 -0.887184 0.435781 0.862093 -0.956321 -0.625909 0.194472 0.442490 0.526503 -0.215274 0.090711 0.932592 0.811999 -2.497026 0.631545 0.321418 -0.425549 -1.078832 0.753444 0.199790 -0.360526 -0.013448 -0.819476 0.814869 0.442118 -0.972048 -0.060603 -2.349825 1.265445 -0.573257 0.429124 1.049783 1.954773 0.071883 -0.094209 0.265616 0.948318 0.331645 1.343401 -0.167934 -1.105252 -0.167077 -0.096576 -0.838161 -0.208564 0.394534 0.762533 1.235357 -0.207282 -0.202946 -0.468025 0.256944 2.587584 1.186697 -1.031903 1.428316 0.658899 -0.046582 -0.075422 1.329359 -0.684267 -1.524182 2.014061 3.770933 0.647353 -1.021377 -0.345493 0.582811 0.797812 1.326020 1.422857 -3.077007 0.184083 1.478935 4 -0.600142 1.929561 -2.346771 -0.669700 -1.165258 0.814788 0.444449 -0.576758 0.353091 0.408893 0.091391 -2.294389 0.485506 -0.081304 -0.716272 -1.648010 1.005361 -1.489603 0.363098 0.758602 -1.373847 -0.972057 1.988537 0.319829 1.169060 0.146585 1.030388 1.165984 1.369563 0.730984 -1.383696 -0.515189 -0.808927 -1.174651 -1.631502 -1.123414 -0.478155 -1.583067 1.419074 1.668777 1.567517 0.222103 -0.336040 -1.352064 0.251032 -0.401695 0.268413 -0.012299 -0.918953 2.921208 -0.581588 0.672848 1.251136 1.382263 1.429897 1.290990 -1.272673 -0.308611 -0.422988 -0.675642 0.874441 1.305736 -0.262585 -1.099395 -0.667101 -0.646737 -0.556338 -0.196591 0.119306 -0.266455 -0.524267 2.650951 0.097318 -0.974697 0.189964 1.141155 -0.064434 1.104971 -1.508908 -0.031833 0.803919 -0.659221 0.939145 0.214041 -0.531805 0.956060 0.249328 0.637903 -0.510158 1.850287 -0.348407 2.001376 -0.389643 -0.024786 -0.470973 0.869339 0.170667 0.598062 1.217262 1.274013 5 -0.389981 -0.752441 -0.734871 3.517318 -1.173559 -0.004956 0.145419 2.151368 -3.086037 -1.569139 1.449784 -0.868951 -1.687716 -0.994401 1.153266 1.803045 -0.819059 0.847970 0.227102 -0.500762 0.868210 1.823540 1.161007 -0.307606 -0.713416 0.363560 -0.822162 2.427681 -0.129537 -0.078716 1.345644 -1.286094 0.237242 -0.136056 0.596664 -1.412381 1.206341 0.299860 0.705238 0.142412 -1.059382 0.833468 1.060015 -0.527045 -1.135732 -1.140983 -0.779540 -0.640875 -1.217196 -1.675663 0.241263 -0.273322 -1.697936 -0.594943 0.101154 1.391735 -0.426953 1.008344 -0.818577 1.924570 -0.578900 -0.457395 -1.096705 0.418522 -0.155623 0.169706 -2.533706 0.018904 1.434160 0.744095 0.647626 -0.770309 2.329141 -0.141547 -1.761594 0.702091 -1.531450 -0.788427 -0.184622 -1.942321 1.530113 0.503406 1.105845 -0.935120 -1.115483 -2.249762 1.307135 0.788412 -0.441091 0.073561 0.812101 -0.916146 1.573714 -0.309508 0.499987 0.187594 0.558913 0.903246 0.317901 -0.809797 6 1.128248 1.516826 -0.186735 -0.668157 1.132259 -0.246648 -0.855167 0.732283 0.931802 1.318684 -1.198418 -1.149318 0.586321 -1.171937 -0.607731 2.753747 1.479287 -1.136365 -0.020485 0.320444 -1.955755 0.660402 -1.545371 0.200519 -0.017263 1.634686 0.599246 0.462989 0.023721 0.225546 0.170972 -0.027496 -0.061233 -0.566411 -0.669567 0.601618 0.503656 -0.678253 -2.907108 -1.717123 0.397631 1.300108 0.215821 -0.593075 -0.225944 -0.946057 1.000308 0.393160 1.342074 -0.370687 -0.166413 -0.419814 -0.255931 1.789478 0.282378 0.742260 -0.050498 1.415309 0.838166 -1.400292 -0.937976 -1.499148 0.801859 0.224824 0.283572 0.643703 -1.198465 0.527206 0.215202 0.437048 1.312868 0.741243 0.077988 0.006123 0.190370 0.018007 -1.026036 -2.378430 -1.069949 0.843822 1.289216 -1.423369 -0.462887 0.197330 -0.935076 0.441271 0.414643 -0.377887 -0.530515 0.621592 1.009572 0.569718 0.175291 -0.656279 -0.112273 -0.392137 -1.043558 -0.467318 -0.384329 -2.009207 7 0.658598 0.101830 -0.682781 0.229349 -0.305657 0.404877 0.252244 -0.837784 -0.039624 0.329457 0.751694 1.469070 -0.157199 1.032628 -0.584639 -0.925544 0.342474 -0.969363 0.133480 -0.385974 -0.600278 0.281939 0.868579 1.129803 -0.041898 0.961193 0.131521 -0.792889 -1.285737 0.073934 -1.333315 -1.044125 1.277338 1.492257 0.411379 1.771805 -1.111128 1.123233 -1.019449 1.738357 -0.690764 -0.120710 -0.421359 -0.727294 -0.857759 -0.069436 -0.328334 -0.558180 1.063474 -0.519133 -0.496902 1.089589 -1.615801 0.080174 -0.229938 -0.498420 -0.624615 0.059481 -0.093158 -1.784549 -0.503789 -0.140528 0.002653 -0.484930 0.055914 -0.680948 -0.994271 1.277052 0.037651 2.155421 -0.437589 0.696404 0.417752 -0.544785 1.190690 0.978262 0.752102 0.504472 0.139853 -0.505089 -0.264975 -1.603194 0.731847 0.010903 -1.165346 -0.125195 -1.032685 -0.465520 1.514808 0.304762 0.793414 0.314635 -1.638279 0.111737 -0.777037 0.251783 1.126303 -0.808798 0.422064 -0.349264 8 -0.356362 -0.089227 0.609373 0.542382 -0.768681 -0.048074 2.015458 -1.552351 0.251552 1.459635 0.949707 0.339465 -0.001372 1.798589 1.559163 0.231783 0.423141 -0.310530 0.353795 2.173336 -0.196247 -0.375636 -0.858221 0.258410 0.656430 0.960819 1.137893 1.553405 0.038981 -0.632038 -0.132009 -1.834997 -0.242576 -0.297879 -0.441559 -0.769691 0.224077 -0.153009 0.519526 -0.680188 0.535851 0.671496 -0.183064 0.301234 1.288256 -2.478240 -0.360403 0.424067 -0.834659 -0.128464 -0.489013 -0.014888 -1.461230 -1.435223 -1.319802 1.083675 0.979140 -0.375291 1.110189 -1.011351 0.587886 -0.822775 -1.183865 1.455173 1.134328 0.239403 -0.837991 -1.130932 0.783168 1.845520 1.437072 -1.198443 1.379098 2.129113 0.260096 -0.011975 0.043302 0.722941 1.028152 -0.235806 1.145245 -1.359598 0.232189 0.503712 -0.614264 -0.530606 -2.435803 -0.255238 -0.064423 0.784643 0.256346 0.128023 1.414103 -1.118659 0.877353 0.500561 0.463651 -2.034512 -0.981683 -0.691944 9 -1.113376 -1.169402 0.680539 -1.534212 1.653817 -1.295181 -0.566826 0.477014 1.413371 0.517105 1.401153 -0.872685 0.830957 0.181507 -0.145616 0.694592 -0.751208 0.324444 0.681973 -0.054972 0.917776 -1.024810 -0.206446 -0.600113 0.852805 1.455109 -0.079769 0.076076 0.207699 -1.850458 -0.124124 -0.610871 -0.883362 0.219049 -0.685094 -0.645330 -0.242805 -0.775602 0.233070 2.422642 -1.423040 -0.582421 0.968304 -0.701025 -0.167850 0.277264 1.301231 0.301205 -3.081249 -0.562868 0.192944 -0.664592 0.565686 0.190913 -0.841858 -1.856545 -1.022777 1.295968 0.451921 0.659955 0.065818 -0.319586 0.253495 -1.144646 -0.483404 0.555902 0.807069 0.714196 0.661196 0.053667 0.346833 -1.288977 -0.386734 -1.262127 0.477495 -0.494034 -0.911414 1.152963 -0.342365 -0.160187 0.470054 -0.853063 -1.387949 -0.257257 -1.030690 -0.110210 0.328911 -0.555923 0.987713 -0.501957 2.069887 -0.067503 0.316029 -1.506232 2.201621 0.492097 -0.085193 -0.977822 1.039147 -0.653932 10 -0.405638 -1.402027 -1.166242 1.306184 0.856283 -1.236170 -0.646721 -1.474064 0.082960 0.090310 -0.169977 0.406345 0.915427 -0.974503 0.271637 1.539184 -0.098866 -0.525149 1.063933 0.085827 -0.129622 0.947959 -0.072496 -0.237592 0.012549 1.065761 0.996596 -0.172481 2.583139 -0.028578 -0.254856 1.328794 -1.592951 2.434350 -0.341500 -0.307719 -1.333273 -1.100845 0.209097 1.734777 0.639632 0.424779 -0.129327 0.905029 -0.482909 1.731628 -2.783425 -0.333677 -0.110895 1.212636 -0.208412 0.427117 1.348563 0.043859 1.772519 -1.416106 0.401155 0.807157 0.303427 -1.246288 0.178774 -0.066126 -1.862288 1.241295 0.377021 -0.822320 -0.749014 1.463652 1.602268 -1.043877 1.185290 -0.565783 -1.076879 1.360241 -0.121991 0.991043 1.007952 0.450185 -0.744376 1.388876 -0.316847 -0.841655 -1.056842 -0.500226 0.096959 1.176896 -2.939652 1.792213 0.316340 0.303218 1.024967 -0.590871 -0.453326 -0.795981 -0.393301 -0.374372 -1.270199 1.618372 1.197727 -0.914863 11 -0.625210 0.288911 0.288374 -1.372667 -0.591395 -0.478942 1.335664 -0.459855 -1.615975 -1.189676 0.374767 -2.488733 0.586656 -1.422008 0.496030 1.911128 -0.560660 -0.499614 -0.372171 -1.833069 0.237124 -0.944446 0.912140 0.359790 -1.359235 0.166966 -0.047107 -0.279789 -0.594454 -0.739013 -1.527645 0.401668 1.791252 -2.774848 0.523873 2.207585 0.488999 -0.339283 0.131711 0.018409 1.186551 -0.424318 1.554994 -0.205917 -0.934975 0.654102 -1.227761 -0.461025 -0.421201 -0.058615 -0.584563 0.336913 -0.477102 -1.381463 0.757745 -0.268968 0.034870 1.231686 0.236600 1.234720 -0.040247 0.029582 1.034905 0.380204 -0.012108 -0.859511 -0.990340 -1.205172 -1.030178 0.426676 0.497796 -0.876808 0.957963 0.173016 0.131612 -1.003556 -1.069908 -1.799207 1.429598 -0.116015 -1.454980 0.261917 0.444412 0.273290 0.844115 0.218745 -1.033350 -1.188295 0.058373 0.800523 -1.627068 0.861651 0.871018 -0.003733 -0.243354 0.947296 0.509406 0.044546 0.266896 1.337165 12 0.699142 -1.928033 0.105363 1.042322 0.715206 -0.763783 0.098798 -1.157898 0.134105 0.042041 0.674826 0.165649 -1.622970 -3.131274 0.597649 -1.880331 0.663980 -0.256033 -1.524058 0.492799 0.221163 0.429622 -0.659584 1.264506 -0.032131 -2.114907 -0.264043 0.457835 -0.676837 -0.629003 0.489145 -0.551686 0.942622 -0.512043 -0.455893 0.021244 -0.178035 -2.498073 -0.171292 0.323510 -0.545163 -0.668909 -0.150031 0.521620 -0.428980 0.676463 0.369081 -0.724832 0.793542 1.237422 0.401275 2.141523 0.249012 0.486755 -0.163274 0.592222 -0.292600 -0.547168 0.619104 -0.013605 0.776734 0.131424 1.189480 -0.666317 -0.939036 1.105515 0.621452 1.586605 -0.760970 1.649646 0.283199 1.275812 -0.452012 0.301361 -0.976951 -0.268106 -0.079255 -1.258332 2.216658 -1.175988 -0.863497 -1.653022 -0.561514 0.450753 0.417200 0.094676 -2.231054 1.316862 -0.477441 0.646654 -0.200252 1.074354 -0.058176 0.120990 0.222522 -0.179507 0.421655 -0.914341 -0.234178 0.741524 13 0.932714 1.423761 -1.280835 0.347882 -0.863171 -0.852580 1.044933 2.094536 0.806206 0.416201 -1.109503 0.145302 -0.996871 0.325456 -0.605081 1.175326 1.645054 0.293432 -2.766822 1.032849 0.079115 -1.414132 1.463376 2.335486 0.411951 -0.048543 0.159284 -0.651554 -1.093128 1.568390 -0.077807 -2.390779 -0.842346 -0.229675 -0.999072 -1.367219 -0.792042 -1.878575 1.451452 1.266250 -0.734315 0.266152 0.735523 -0.430860 0.229864 0.850083 -2.241241 1.063850 0.289409 -0.354360 0.113063 -0.173006 1.386998 1.886236 0.587119 -0.961133 0.399295 1.461560 0.310823 0.280220 -0.879103 -1.326348 0.003337 -1.085908 -0.436723 2.111926 0.106068 0.615597 2.152996 -0.196155 0.025747 -0.039061 0.656823 -0.347105 2.513979 1.758070 1.288473 -0.739185 -0.691592 -0.098728 -0.276386 0.489981 0.516278 -0.838258 0.596673 -0.331053 0.521174 -0.145023 0.836693 -1.092166 0.361733 -1.169981 0.046731 0.655377 -0.756852 1.285805 -0.095019 0.360253 1.370621 0.083010 14 0.888893 2.288725 -1.032332 0.212273 -1.091826 1.692498 1.025367 0.550854 0.679430 -1.335712 -0.798341 2.265351 -1.006938 2.059761 0.420266 -1.189657 0.506674 0.260847 -0.533145 0.727267 1.412276 1.482106 -0.996258 0.588641 -0.412642 -0.920733 -0.874691 0.839002 0.501668 -0.342493 -0.533806 -2.146352 -0.597339 0.115726 0.850683 -0.752239 0.377263 -0.561982 0.262783 -0.356676 -0.367462 0.753611 -1.267414 -1.330698 -0.536453 0.840938 -0.763108 -0.268100 -0.677424 1.606831 0.151732 -2.085701 1.219296 0.400863 0.591165 -1.485213 1.501979 1.196569 -0.214154 0.339554 -0.034446 1.176452 0.546340 -1.255630 -1.309210 -0.445437 0.189437 -0.737463 0.843767 -0.605632 -0.060777 0.409310 1.285569 -0.622638 1.018193 0.880680 0.046805 -1.818058 -0.809829 0.875224 0.409569 -0.116621 -1.238919 3.305724 -0.024121 -1.756500 1.328958 0.507593 -0.866554 -2.240848 -0.661376 -0.671824 0.215720 -0.296326 0.481402 0.829645 -0.721025 1.263914 0.549047 -1.234945 15 -1.978838 0.721823 -0.559067 -1.235243 0.420716 -0.598845 0.359576 -0.619366 -1.757772 -1.156251 0.705212 0.875071 -1.020376 0.394760 -0.147970 0.230249 1.355203 1.794488 2.678058 -0.153565 -0.460959 -0.098108 -1.407930 -2.487702 1.823014 0.099873 -0.517603 -0.509311 -1.833175 -0.900906 0.459493 -0.655440 1.466122 -1.531389 -0.422106 0.421422 0.578615 0.259795 0.018941 -0.168726 1.611107 -1.586550 -1.384941 0.858377 1.033242 1.701343 1.748344 -0.371182 -0.843575 2.089641 -0.345430 -1.740556 0.141915 -2.197138 0.689569 -0.150025 0.287456 0.654016 -1.521919 -0.918008 -0.587528 0.230636 0.262637 0.615674 0.600044 -0.494699 -0.743089 0.220026 -0.242207 0.528216 -0.328174 -1.536517 -1.476640 -1.162114 -1.260222 1.106252 -1.467408 -0.349341 -1.841217 0.031296 -0.076475 -0.353383 0.807545 0.779064 -2.398417 -0.267828 1.549734 0.814397 0.284770 -0.659369 0.761040 -0.722067 0.810332 1.501295 1.440865 -1.367459 -0.700301 -1.540662 0.159837 -0.625415 It is also possible to stick MultiIndexes and even only specific levels. [63]: bigdf.index = pd.MultiIndex.from_product([["A","B"],[0,1],[0,1,2,3]]) bigdf.style.set_sticky(axis="index", pixel_size=18, levels=[1,2]) [63]:       0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 A 0 0 -0.773866 -0.240521 -0.217165 1.173609 0.686390 0.008358 0.696232 0.173166 0.620498 0.504067 0.428066 -0.051824 0.719915 0.057165 0.562808 -0.369536 0.483399 0.620765 -0.354342 -1.469471 -1.937266 0.038031 -1.518162 -0.417599 0.386717 0.716193 0.489961 0.733957 0.914415 0.679894 0.255448 -0.508338 0.332030 -0.111107 -0.251983 -1.456620 0.409630 1.062320 -0.577115 0.718796 -0.399260 -1.311389 0.649122 0.091566 0.628872 0.297894 -0.142290 -0.542291 -0.914290 1.144514 0.313584 1.182635 1.214235 -0.416446 -1.653940 -2.550787 0.442473 0.052127 -0.464469 -0.523852 0.989726 -1.325539 -0.199687 -1.226727 0.290018 1.164574 0.817841 -0.309509 0.496599 0.943536 -0.091850 -2.802658 2.126219 -0.521161 0.288098 -0.454663 -1.676143 -0.357661 -0.788960 0.185911 -0.017106 2.454020 1.832706 -0.911743 -0.655873 -0.000514 -2.226997 0.677285 -0.140249 -0.408407 -0.838665 0.482228 1.243458 -0.477394 -0.220343 -2.463966 0.237325 -0.307380 1.172478 0.819492 1 0.405906 -0.978919 1.267526 0.145250 -1.066786 -2.114192 -1.128346 -1.082523 0.372216 0.004127 -0.211984 0.937326 -0.935890 -1.704118 0.611789 -1.030015 0.636123 -1.506193 1.736609 1.392958 1.009424 0.353266 0.697339 -0.297424 0.428702 -0.145346 -0.333553 -0.974699 0.665314 0.971944 0.121950 -1.439668 1.018808 1.442399 -0.199585 -1.165916 0.645656 1.436466 -0.921215 1.293906 -2.706443 1.460928 -0.823197 0.292952 -1.448992 0.026692 -0.975883 0.392823 0.442166 0.745741 1.187982 -0.218570 0.305288 0.054932 -1.476953 -0.114434 0.014103 0.825394 -0.060654 -0.413688 0.974836 1.339210 1.034838 0.040775 0.705001 0.017796 1.867681 -0.390173 2.285277 2.311464 -0.085070 -0.648115 0.576300 -0.790087 -1.183798 -1.334558 -0.454118 0.319302 1.706488 0.830429 0.502476 -0.079631 0.414635 0.332511 0.042935 -0.160910 0.918553 -0.292697 -1.303834 -0.199604 0.871023 -1.370681 -0.205701 -0.492973 1.123083 -0.081842 -0.118527 0.245838 -0.315742 -0.511806 2 0.011470 -0.036104 1.399603 -0.418176 -0.412229 -1.234783 -1.121500 1.196478 -0.569522 0.422022 -0.220484 0.804338 2.892667 -0.511055 -0.168722 -1.477996 -1.969917 0.471354 1.698548 0.137105 -0.762052 0.199379 -0.964346 -0.256692 1.265275 0.848762 -0.784161 1.863776 -0.355569 0.854552 0.768061 -2.075718 -2.501069 1.109868 0.957545 -0.683276 0.307764 0.733073 1.706250 -1.118091 0.374961 -1.414503 -0.524183 -1.662696 0.687921 0.521732 1.451396 -0.833491 -0.362796 -1.174444 -0.813893 -0.893220 0.770743 1.156647 -0.647444 0.125929 0.513600 -0.537874 1.992052 -1.946584 -0.104759 0.484779 -0.290936 -0.441075 0.542993 -1.050038 1.630482 0.239771 -1.177310 0.464804 -0.966995 0.646086 0.486899 1.022196 -2.267827 -1.229616 1.313805 1.073292 2.324940 -0.542720 -1.504292 0.777643 -0.618553 0.011342 1.385062 1.363552 -0.549834 0.688896 1.361288 -0.381137 0.797812 -1.128198 0.369208 0.540132 0.413853 -0.200308 -0.969126 0.981293 -0.009783 -0.320020 3 -0.574816 1.419977 0.434813 -1.101217 -1.586275 1.979573 0.378298 0.782326 2.178987 0.657564 0.683774 -0.091000 -0.059552 -0.738908 -0.907653 -0.701936 0.580039 -0.618757 0.453684 1.665382 -0.152321 0.880077 0.571073 -0.604736 0.532359 0.515031 -0.959844 -0.887184 0.435781 0.862093 -0.956321 -0.625909 0.194472 0.442490 0.526503 -0.215274 0.090711 0.932592 0.811999 -2.497026 0.631545 0.321418 -0.425549 -1.078832 0.753444 0.199790 -0.360526 -0.013448 -0.819476 0.814869 0.442118 -0.972048 -0.060603 -2.349825 1.265445 -0.573257 0.429124 1.049783 1.954773 0.071883 -0.094209 0.265616 0.948318 0.331645 1.343401 -0.167934 -1.105252 -0.167077 -0.096576 -0.838161 -0.208564 0.394534 0.762533 1.235357 -0.207282 -0.202946 -0.468025 0.256944 2.587584 1.186697 -1.031903 1.428316 0.658899 -0.046582 -0.075422 1.329359 -0.684267 -1.524182 2.014061 3.770933 0.647353 -1.021377 -0.345493 0.582811 0.797812 1.326020 1.422857 -3.077007 0.184083 1.478935 1 0 -0.600142 1.929561 -2.346771 -0.669700 -1.165258 0.814788 0.444449 -0.576758 0.353091 0.408893 0.091391 -2.294389 0.485506 -0.081304 -0.716272 -1.648010 1.005361 -1.489603 0.363098 0.758602 -1.373847 -0.972057 1.988537 0.319829 1.169060 0.146585 1.030388 1.165984 1.369563 0.730984 -1.383696 -0.515189 -0.808927 -1.174651 -1.631502 -1.123414 -0.478155 -1.583067 1.419074 1.668777 1.567517 0.222103 -0.336040 -1.352064 0.251032 -0.401695 0.268413 -0.012299 -0.918953 2.921208 -0.581588 0.672848 1.251136 1.382263 1.429897 1.290990 -1.272673 -0.308611 -0.422988 -0.675642 0.874441 1.305736 -0.262585 -1.099395 -0.667101 -0.646737 -0.556338 -0.196591 0.119306 -0.266455 -0.524267 2.650951 0.097318 -0.974697 0.189964 1.141155 -0.064434 1.104971 -1.508908 -0.031833 0.803919 -0.659221 0.939145 0.214041 -0.531805 0.956060 0.249328 0.637903 -0.510158 1.850287 -0.348407 2.001376 -0.389643 -0.024786 -0.470973 0.869339 0.170667 0.598062 1.217262 1.274013 1 -0.389981 -0.752441 -0.734871 3.517318 -1.173559 -0.004956 0.145419 2.151368 -3.086037 -1.569139 1.449784 -0.868951 -1.687716 -0.994401 1.153266 1.803045 -0.819059 0.847970 0.227102 -0.500762 0.868210 1.823540 1.161007 -0.307606 -0.713416 0.363560 -0.822162 2.427681 -0.129537 -0.078716 1.345644 -1.286094 0.237242 -0.136056 0.596664 -1.412381 1.206341 0.299860 0.705238 0.142412 -1.059382 0.833468 1.060015 -0.527045 -1.135732 -1.140983 -0.779540 -0.640875 -1.217196 -1.675663 0.241263 -0.273322 -1.697936 -0.594943 0.101154 1.391735 -0.426953 1.008344 -0.818577 1.924570 -0.578900 -0.457395 -1.096705 0.418522 -0.155623 0.169706 -2.533706 0.018904 1.434160 0.744095 0.647626 -0.770309 2.329141 -0.141547 -1.761594 0.702091 -1.531450 -0.788427 -0.184622 -1.942321 1.530113 0.503406 1.105845 -0.935120 -1.115483 -2.249762 1.307135 0.788412 -0.441091 0.073561 0.812101 -0.916146 1.573714 -0.309508 0.499987 0.187594 0.558913 0.903246 0.317901 -0.809797 2 1.128248 1.516826 -0.186735 -0.668157 1.132259 -0.246648 -0.855167 0.732283 0.931802 1.318684 -1.198418 -1.149318 0.586321 -1.171937 -0.607731 2.753747 1.479287 -1.136365 -0.020485 0.320444 -1.955755 0.660402 -1.545371 0.200519 -0.017263 1.634686 0.599246 0.462989 0.023721 0.225546 0.170972 -0.027496 -0.061233 -0.566411 -0.669567 0.601618 0.503656 -0.678253 -2.907108 -1.717123 0.397631 1.300108 0.215821 -0.593075 -0.225944 -0.946057 1.000308 0.393160 1.342074 -0.370687 -0.166413 -0.419814 -0.255931 1.789478 0.282378 0.742260 -0.050498 1.415309 0.838166 -1.400292 -0.937976 -1.499148 0.801859 0.224824 0.283572 0.643703 -1.198465 0.527206 0.215202 0.437048 1.312868 0.741243 0.077988 0.006123 0.190370 0.018007 -1.026036 -2.378430 -1.069949 0.843822 1.289216 -1.423369 -0.462887 0.197330 -0.935076 0.441271 0.414643 -0.377887 -0.530515 0.621592 1.009572 0.569718 0.175291 -0.656279 -0.112273 -0.392137 -1.043558 -0.467318 -0.384329 -2.009207 3 0.658598 0.101830 -0.682781 0.229349 -0.305657 0.404877 0.252244 -0.837784 -0.039624 0.329457 0.751694 1.469070 -0.157199 1.032628 -0.584639 -0.925544 0.342474 -0.969363 0.133480 -0.385974 -0.600278 0.281939 0.868579 1.129803 -0.041898 0.961193 0.131521 -0.792889 -1.285737 0.073934 -1.333315 -1.044125 1.277338 1.492257 0.411379 1.771805 -1.111128 1.123233 -1.019449 1.738357 -0.690764 -0.120710 -0.421359 -0.727294 -0.857759 -0.069436 -0.328334 -0.558180 1.063474 -0.519133 -0.496902 1.089589 -1.615801 0.080174 -0.229938 -0.498420 -0.624615 0.059481 -0.093158 -1.784549 -0.503789 -0.140528 0.002653 -0.484930 0.055914 -0.680948 -0.994271 1.277052 0.037651 2.155421 -0.437589 0.696404 0.417752 -0.544785 1.190690 0.978262 0.752102 0.504472 0.139853 -0.505089 -0.264975 -1.603194 0.731847 0.010903 -1.165346 -0.125195 -1.032685 -0.465520 1.514808 0.304762 0.793414 0.314635 -1.638279 0.111737 -0.777037 0.251783 1.126303 -0.808798 0.422064 -0.349264 B 0 0 -0.356362 -0.089227 0.609373 0.542382 -0.768681 -0.048074 2.015458 -1.552351 0.251552 1.459635 0.949707 0.339465 -0.001372 1.798589 1.559163 0.231783 0.423141 -0.310530 0.353795 2.173336 -0.196247 -0.375636 -0.858221 0.258410 0.656430 0.960819 1.137893 1.553405 0.038981 -0.632038 -0.132009 -1.834997 -0.242576 -0.297879 -0.441559 -0.769691 0.224077 -0.153009 0.519526 -0.680188 0.535851 0.671496 -0.183064 0.301234 1.288256 -2.478240 -0.360403 0.424067 -0.834659 -0.128464 -0.489013 -0.014888 -1.461230 -1.435223 -1.319802 1.083675 0.979140 -0.375291 1.110189 -1.011351 0.587886 -0.822775 -1.183865 1.455173 1.134328 0.239403 -0.837991 -1.130932 0.783168 1.845520 1.437072 -1.198443 1.379098 2.129113 0.260096 -0.011975 0.043302 0.722941 1.028152 -0.235806 1.145245 -1.359598 0.232189 0.503712 -0.614264 -0.530606 -2.435803 -0.255238 -0.064423 0.784643 0.256346 0.128023 1.414103 -1.118659 0.877353 0.500561 0.463651 -2.034512 -0.981683 -0.691944 1 -1.113376 -1.169402 0.680539 -1.534212 1.653817 -1.295181 -0.566826 0.477014 1.413371 0.517105 1.401153 -0.872685 0.830957 0.181507 -0.145616 0.694592 -0.751208 0.324444 0.681973 -0.054972 0.917776 -1.024810 -0.206446 -0.600113 0.852805 1.455109 -0.079769 0.076076 0.207699 -1.850458 -0.124124 -0.610871 -0.883362 0.219049 -0.685094 -0.645330 -0.242805 -0.775602 0.233070 2.422642 -1.423040 -0.582421 0.968304 -0.701025 -0.167850 0.277264 1.301231 0.301205 -3.081249 -0.562868 0.192944 -0.664592 0.565686 0.190913 -0.841858 -1.856545 -1.022777 1.295968 0.451921 0.659955 0.065818 -0.319586 0.253495 -1.144646 -0.483404 0.555902 0.807069 0.714196 0.661196 0.053667 0.346833 -1.288977 -0.386734 -1.262127 0.477495 -0.494034 -0.911414 1.152963 -0.342365 -0.160187 0.470054 -0.853063 -1.387949 -0.257257 -1.030690 -0.110210 0.328911 -0.555923 0.987713 -0.501957 2.069887 -0.067503 0.316029 -1.506232 2.201621 0.492097 -0.085193 -0.977822 1.039147 -0.653932 2 -0.405638 -1.402027 -1.166242 1.306184 0.856283 -1.236170 -0.646721 -1.474064 0.082960 0.090310 -0.169977 0.406345 0.915427 -0.974503 0.271637 1.539184 -0.098866 -0.525149 1.063933 0.085827 -0.129622 0.947959 -0.072496 -0.237592 0.012549 1.065761 0.996596 -0.172481 2.583139 -0.028578 -0.254856 1.328794 -1.592951 2.434350 -0.341500 -0.307719 -1.333273 -1.100845 0.209097 1.734777 0.639632 0.424779 -0.129327 0.905029 -0.482909 1.731628 -2.783425 -0.333677 -0.110895 1.212636 -0.208412 0.427117 1.348563 0.043859 1.772519 -1.416106 0.401155 0.807157 0.303427 -1.246288 0.178774 -0.066126 -1.862288 1.241295 0.377021 -0.822320 -0.749014 1.463652 1.602268 -1.043877 1.185290 -0.565783 -1.076879 1.360241 -0.121991 0.991043 1.007952 0.450185 -0.744376 1.388876 -0.316847 -0.841655 -1.056842 -0.500226 0.096959 1.176896 -2.939652 1.792213 0.316340 0.303218 1.024967 -0.590871 -0.453326 -0.795981 -0.393301 -0.374372 -1.270199 1.618372 1.197727 -0.914863 3 -0.625210 0.288911 0.288374 -1.372667 -0.591395 -0.478942 1.335664 -0.459855 -1.615975 -1.189676 0.374767 -2.488733 0.586656 -1.422008 0.496030 1.911128 -0.560660 -0.499614 -0.372171 -1.833069 0.237124 -0.944446 0.912140 0.359790 -1.359235 0.166966 -0.047107 -0.279789 -0.594454 -0.739013 -1.527645 0.401668 1.791252 -2.774848 0.523873 2.207585 0.488999 -0.339283 0.131711 0.018409 1.186551 -0.424318 1.554994 -0.205917 -0.934975 0.654102 -1.227761 -0.461025 -0.421201 -0.058615 -0.584563 0.336913 -0.477102 -1.381463 0.757745 -0.268968 0.034870 1.231686 0.236600 1.234720 -0.040247 0.029582 1.034905 0.380204 -0.012108 -0.859511 -0.990340 -1.205172 -1.030178 0.426676 0.497796 -0.876808 0.957963 0.173016 0.131612 -1.003556 -1.069908 -1.799207 1.429598 -0.116015 -1.454980 0.261917 0.444412 0.273290 0.844115 0.218745 -1.033350 -1.188295 0.058373 0.800523 -1.627068 0.861651 0.871018 -0.003733 -0.243354 0.947296 0.509406 0.044546 0.266896 1.337165 1 0 0.699142 -1.928033 0.105363 1.042322 0.715206 -0.763783 0.098798 -1.157898 0.134105 0.042041 0.674826 0.165649 -1.622970 -3.131274 0.597649 -1.880331 0.663980 -0.256033 -1.524058 0.492799 0.221163 0.429622 -0.659584 1.264506 -0.032131 -2.114907 -0.264043 0.457835 -0.676837 -0.629003 0.489145 -0.551686 0.942622 -0.512043 -0.455893 0.021244 -0.178035 -2.498073 -0.171292 0.323510 -0.545163 -0.668909 -0.150031 0.521620 -0.428980 0.676463 0.369081 -0.724832 0.793542 1.237422 0.401275 2.141523 0.249012 0.486755 -0.163274 0.592222 -0.292600 -0.547168 0.619104 -0.013605 0.776734 0.131424 1.189480 -0.666317 -0.939036 1.105515 0.621452 1.586605 -0.760970 1.649646 0.283199 1.275812 -0.452012 0.301361 -0.976951 -0.268106 -0.079255 -1.258332 2.216658 -1.175988 -0.863497 -1.653022 -0.561514 0.450753 0.417200 0.094676 -2.231054 1.316862 -0.477441 0.646654 -0.200252 1.074354 -0.058176 0.120990 0.222522 -0.179507 0.421655 -0.914341 -0.234178 0.741524 1 0.932714 1.423761 -1.280835 0.347882 -0.863171 -0.852580 1.044933 2.094536 0.806206 0.416201 -1.109503 0.145302 -0.996871 0.325456 -0.605081 1.175326 1.645054 0.293432 -2.766822 1.032849 0.079115 -1.414132 1.463376 2.335486 0.411951 -0.048543 0.159284 -0.651554 -1.093128 1.568390 -0.077807 -2.390779 -0.842346 -0.229675 -0.999072 -1.367219 -0.792042 -1.878575 1.451452 1.266250 -0.734315 0.266152 0.735523 -0.430860 0.229864 0.850083 -2.241241 1.063850 0.289409 -0.354360 0.113063 -0.173006 1.386998 1.886236 0.587119 -0.961133 0.399295 1.461560 0.310823 0.280220 -0.879103 -1.326348 0.003337 -1.085908 -0.436723 2.111926 0.106068 0.615597 2.152996 -0.196155 0.025747 -0.039061 0.656823 -0.347105 2.513979 1.758070 1.288473 -0.739185 -0.691592 -0.098728 -0.276386 0.489981 0.516278 -0.838258 0.596673 -0.331053 0.521174 -0.145023 0.836693 -1.092166 0.361733 -1.169981 0.046731 0.655377 -0.756852 1.285805 -0.095019 0.360253 1.370621 0.083010 2 0.888893 2.288725 -1.032332 0.212273 -1.091826 1.692498 1.025367 0.550854 0.679430 -1.335712 -0.798341 2.265351 -1.006938 2.059761 0.420266 -1.189657 0.506674 0.260847 -0.533145 0.727267 1.412276 1.482106 -0.996258 0.588641 -0.412642 -0.920733 -0.874691 0.839002 0.501668 -0.342493 -0.533806 -2.146352 -0.597339 0.115726 0.850683 -0.752239 0.377263 -0.561982 0.262783 -0.356676 -0.367462 0.753611 -1.267414 -1.330698 -0.536453 0.840938 -0.763108 -0.268100 -0.677424 1.606831 0.151732 -2.085701 1.219296 0.400863 0.591165 -1.485213 1.501979 1.196569 -0.214154 0.339554 -0.034446 1.176452 0.546340 -1.255630 -1.309210 -0.445437 0.189437 -0.737463 0.843767 -0.605632 -0.060777 0.409310 1.285569 -0.622638 1.018193 0.880680 0.046805 -1.818058 -0.809829 0.875224 0.409569 -0.116621 -1.238919 3.305724 -0.024121 -1.756500 1.328958 0.507593 -0.866554 -2.240848 -0.661376 -0.671824 0.215720 -0.296326 0.481402 0.829645 -0.721025 1.263914 0.549047 -1.234945 3 -1.978838 0.721823 -0.559067 -1.235243 0.420716 -0.598845 0.359576 -0.619366 -1.757772 -1.156251 0.705212 0.875071 -1.020376 0.394760 -0.147970 0.230249 1.355203 1.794488 2.678058 -0.153565 -0.460959 -0.098108 -1.407930 -2.487702 1.823014 0.099873 -0.517603 -0.509311 -1.833175 -0.900906 0.459493 -0.655440 1.466122 -1.531389 -0.422106 0.421422 0.578615 0.259795 0.018941 -0.168726 1.611107 -1.586550 -1.384941 0.858377 1.033242 1.701343 1.748344 -0.371182 -0.843575 2.089641 -0.345430 -1.740556 0.141915 -2.197138 0.689569 -0.150025 0.287456 0.654016 -1.521919 -0.918008 -0.587528 0.230636 0.262637 0.615674 0.600044 -0.494699 -0.743089 0.220026 -0.242207 0.528216 -0.328174 -1.536517 -1.476640 -1.162114 -1.260222 1.106252 -1.467408 -0.349341 -1.841217 0.031296 -0.076475 -0.353383 0.807545 0.779064 -2.398417 -0.267828 1.549734 0.814397 0.284770 -0.659369 0.761040 -0.722067 0.810332 1.501295 1.440865 -1.367459 -0.700301 -1.540662 0.159837 -0.625415 HTML Escaping# Suppose you have to display HTML within HTML, that can be a bit of pain when the renderer can’t distinguish. You can use the escape formatting option to handle this, and even use it within a formatter that contains HTML itself. [64]: df4 = pd.DataFrame([['<div></div>', '"&other"', '<span></span>']]) df4.style [64]:   0 1 2 0 "&other" [65]: df4.style.format(escape="html") [65]:   0 1 2 0 <div></div> "&other" <span></span> [66]: df4.style.format('<a href="https://pandas.pydata.org" target="_blank">{}</a>', escape="html") [66]:   0 1 2 0 <div></div> "&other" <span></span> Export to Excel# Some support (since version 0.20.0) is available for exporting styled DataFrames to Excel worksheets using the OpenPyXL or XlsxWriter engines. CSS2.2 properties handled include: background-color border-style properties border-width properties border-color properties color font-family font-style font-weight text-align text-decoration vertical-align white-space: nowrap Shorthand and side-specific border properties are supported (e.g. border-style and border-left-style) as well as the border shorthands for all sides (border: 1px solid green) or specified sides (border-left: 1px solid green). Using a border shorthand will override any border properties set before it (See CSS Working Group for more details) Only CSS2 named colors and hex colors of the form #rgb or #rrggbb are currently supported. The following pseudo CSS properties are also available to set Excel specific style properties: number-format border-style (for Excel-specific styles: “hair”, “mediumDashDot”, “dashDotDot”, “mediumDashDotDot”, “dashDot”, “slantDashDot”, or “mediumDashed”) Table level styles, and data cell CSS-classes are not included in the export to Excel: individual cells must have their properties mapped by the Styler.apply and/or Styler.applymap methods. [67]: df2.style.\ applymap(style_negative, props='color:red;').\ highlight_max(axis=0).\ to_excel('styled.xlsx', engine='openpyxl') A screenshot of the output: Export to LaTeX# There is support (since version 1.3.0) to export Styler to LaTeX. The documentation for the .to_latex method gives further detail and numerous examples. More About CSS and HTML# Cascading Style Sheet (CSS) language, which is designed to influence how a browser renders HTML elements, has its own peculiarities. It never reports errors: it just silently ignores them and doesn’t render your objects how you intend so can sometimes be frustrating. Here is a very brief primer on how Styler creates HTML and interacts with CSS, with advice on common pitfalls to avoid. CSS Classes and Ids# The precise structure of the CSS class attached to each cell is as follows. Cells with Index and Column names include index_name and level<k> where k is its level in a MultiIndex Index label cells include row_heading level<k> where k is the level in a MultiIndex row<m> where m is the numeric position of the row Column label cells include col_heading level<k> where k is the level in a MultiIndex col<n> where n is the numeric position of the column Data cells include data row<m>, where m is the numeric position of the cell. col<n>, where n is the numeric position of the cell. Blank cells include blank Trimmed cells include col_trim or row_trim The structure of the id is T_uuid_level<k>_row<m>_col<n> where level<k> is used only on headings, and headings will only have either row<m> or col<n> whichever is needed. By default we’ve also prepended each row/column identifier with a UUID unique to each DataFrame so that the style from one doesn’t collide with the styling from another within the same notebook or page. You can read more about the use of UUIDs in Optimization. We can see example of the HTML by calling the .to_html() method. [68]: print(pd.DataFrame([[1,2],[3,4]], index=['i1', 'i2'], columns=['c1', 'c2']).style.to_html()) <style type="text/css"> </style> <table id="T_d505a"> <thead> <tr> <th class="blank level0" >&nbsp;</th> <th id="T_d505a_level0_col0" class="col_heading level0 col0" >c1</th> <th id="T_d505a_level0_col1" class="col_heading level0 col1" >c2</th> </tr> </thead> <tbody> <tr> <th id="T_d505a_level0_row0" class="row_heading level0 row0" >i1</th> <td id="T_d505a_row0_col0" class="data row0 col0" >1</td> <td id="T_d505a_row0_col1" class="data row0 col1" >2</td> </tr> <tr> <th id="T_d505a_level0_row1" class="row_heading level0 row1" >i2</th> <td id="T_d505a_row1_col0" class="data row1 col0" >3</td> <td id="T_d505a_row1_col1" class="data row1 col1" >4</td> </tr> </tbody> </table> CSS Hierarchies# The examples have shown that when CSS styles overlap, the one that comes last in the HTML render, takes precedence. So the following yield different results: [69]: df4 = pd.DataFrame([['text']]) df4.style.applymap(lambda x: 'color:green;')\ .applymap(lambda x: 'color:red;') [69]:   0 0 text [70]: df4.style.applymap(lambda x: 'color:red;')\ .applymap(lambda x: 'color:green;') [70]:   0 0 text This is only true for CSS rules that are equivalent in hierarchy, or importance. You can read more about CSS specificity here but for our purposes it suffices to summarize the key points: A CSS importance score for each HTML element is derived by starting at zero and adding: 1000 for an inline style attribute 100 for each ID 10 for each attribute, class or pseudo-class 1 for each element name or pseudo-element Let’s use this to describe the action of the following configurations [71]: df4.style.set_uuid('a_')\ .set_table_styles([{'selector': 'td', 'props': 'color:red;'}])\ .applymap(lambda x: 'color:green;') [71]:   0 0 text This text is red because the generated selector #T_a_ td is worth 101 (ID plus element), whereas #T_a_row0_col0 is only worth 100 (ID), so is considered inferior even though in the HTML it comes after the previous. [72]: df4.style.set_uuid('b_')\ .set_table_styles([{'selector': 'td', 'props': 'color:red;'}, {'selector': '.cls-1', 'props': 'color:blue;'}])\ .applymap(lambda x: 'color:green;')\ .set_td_classes(pd.DataFrame([['cls-1']])) [72]:   0 0 text In the above case the text is blue because the selector #T_b_ .cls-1 is worth 110 (ID plus class), which takes precedence. [73]: df4.style.set_uuid('c_')\ .set_table_styles([{'selector': 'td', 'props': 'color:red;'}, {'selector': '.cls-1', 'props': 'color:blue;'}, {'selector': 'td.data', 'props': 'color:yellow;'}])\ .applymap(lambda x: 'color:green;')\ .set_td_classes(pd.DataFrame([['cls-1']])) [73]:   0 0 text Now we have created another table style this time the selector T_c_ td.data (ID plus element plus class) gets bumped up to 111. If your style fails to be applied, and its really frustrating, try the !important trump card. [74]: df4.style.set_uuid('d_')\ .set_table_styles([{'selector': 'td', 'props': 'color:red;'}, {'selector': '.cls-1', 'props': 'color:blue;'}, {'selector': 'td.data', 'props': 'color:yellow;'}])\ .applymap(lambda x: 'color:green !important;')\ .set_td_classes(pd.DataFrame([['cls-1']])) [74]:   0 0 text Finally got that green text after all! Extensibility# The core of pandas is, and will remain, its “high-performance, easy-to-use data structures”. With that in mind, we hope that DataFrame.style accomplishes two goals Provide an API that is pleasing to use interactively and is “good enough” for many tasks Provide the foundations for dedicated libraries to build on If you build a great library on top of this, let us know and we’ll link to it. Subclassing# If the default template doesn’t quite suit your needs, you can subclass Styler and extend or override the template. We’ll show an example of extending the default template to insert a custom header before each table. [75]: from jinja2 import Environment, ChoiceLoader, FileSystemLoader from IPython.display import HTML from pandas.io.formats.style import Styler We’ll use the following template: [76]: with open("templates/myhtml.tpl") as f: print(f.read()) {% extends "html_table.tpl" %} {% block table %} <h1>{{ table_title|default("My Table") }}</h1> {{ super() }} {% endblock table %} Now that we’ve created a template, we need to set up a subclass of Styler that knows about it. [77]: class MyStyler(Styler): env = Environment( loader=ChoiceLoader([ FileSystemLoader("templates"), # contains ours Styler.loader, # the default ]) ) template_html_table = env.get_template("myhtml.tpl") Notice that we include the original loader in our environment’s loader. That’s because we extend the original template, so the Jinja environment needs to be able to find it. Now we can use that custom styler. It’s __init__ takes a DataFrame. [78]: MyStyler(df3) [78]: My Table     c1 c2 c3 c4 A r1 -1.048553 -1.420018 -1.706270 1.950775 r2 -0.509652 -0.438074 -1.252795 0.777490 B r1 -1.613898 -0.212740 -0.895467 0.386902 r2 -0.510805 -1.180632 -0.028182 0.428332 Our custom template accepts a table_title keyword. We can provide the value in the .to_html method. [79]: HTML(MyStyler(df3).to_html(table_title="Extending Example")) [79]: Extending Example     c1 c2 c3 c4 A r1 -1.048553 -1.420018 -1.706270 1.950775 r2 -0.509652 -0.438074 -1.252795 0.777490 B r1 -1.613898 -0.212740 -0.895467 0.386902 r2 -0.510805 -1.180632 -0.028182 0.428332 For convenience, we provide the Styler.from_custom_template method that does the same as the custom subclass. [80]: EasyStyler = Styler.from_custom_template("templates", "myhtml.tpl") HTML(EasyStyler(df3).to_html(table_title="Another Title")) [80]: Another Title     c1 c2 c3 c4 A r1 -1.048553 -1.420018 -1.706270 1.950775 r2 -0.509652 -0.438074 -1.252795 0.777490 B r1 -1.613898 -0.212740 -0.895467 0.386902 r2 -0.510805 -1.180632 -0.028182 0.428332 Template Structure# Here’s the template structure for the both the style generation template and the table generation template: Style template: [82]: HTML(style_structure) [82]: before_style style <style type="text/css"> table_styles before_cellstyle cellstyle </style> Table template: [84]: HTML(table_structure) [84]: before_table table <table ...> caption thead before_head_rows head_tr (loop over headers) after_head_rows tbody before_rows tr (loop over data rows) after_rows </table> after_table See the template in the GitHub repo for more details.
918
1,079
Is there a way to add up multiple pandas columns conditionally based on text in the column label? I am building a dataframe that shows availability in a hotel. The rows are the dates and the columns are the room types. There are 5 columns labelled PD1_Total, PD2_Total, PDSK_Total, PDEK_Total, and PDSU_Total. I need to create a column that shows all the individual room availability added together. The obvious manual way to do it is with the code: ga['Total_Rooms'] = ga['PD1_Total'] + ga['PD2_Total'] + ga['PDSK_Total'] + ga['PDEK_Total'] + ga['PDSU_Total'] However, the source data may be updated at a later date to include other room types with the suffix '_Total', so I'd like to figure out a way to create that formula to automatically include all the columns with that suffix without having to manually update the formula. Thank-you
[CLS] Python multiple pandas columns conditionally based [SEP]155 - 1. 583067 1. 419074 1. 668777 1. 567517 0. 222103 - 0. 336040 - 1. 352064 0. 251032 - 0. 401695 0. 268413 - 0. 012299 - 0. 918953 2. 921208 - 0. 581588 0. 672848 1. 251136 1. 382263 1. 429897 1. 290990 - 1. 272673 - 0. 308611 - 0. 422988 - 0. 675642 0. 874441 1. 305736 - 0. 262585 - 1. 099395 - 0. 667101 - 0. 646737 - 0. 556338 - 0. 196591 0. 119306 - 0. 266455 - 0. 524267 / [CLS] Python multiple pandas columns conditionally based [SEP] 124124 - 0. 610871 - 0. 883362 0. 219049 - 0. 685094 - 0. 645330 - 0. 242805 - 0. 775602 0. 233070 2. 422642 - 1. 423040 - 0. 582421 0. 968304 - 0. 701025 - 0. 167850 0. 277264 1. 301231 0. 301205 - 3. 081249 - 0. 562868 0. 192944 - 0. 664592 0. 565686 0. 190913 - 0. 841858 - 1. 856545 - 1. 022777 / [CLS] Python multiple pandas columns conditionally based [SEP] 598845 0. 359576 - 0. 619366 - 1. 757772 - 1. 156251 0. 705212 0. 875071 - 1. 020376 0. 394760 - 0. 147970 0. 230249 1. 355203 1. 794488 2. 678058 - 0. 153565 - 0. 460959 - 0. 098108 - 1. 407930 - 2. 487702 1. 823014 0. 099873 - 0. 517603 - 0. 509311 - 1. 833175 - 0. 900906 / [CLS] Python multiple pandas columns conditionally based [SEP]7643 - 0. 618553 0. 011342 1. 385062 1. 363552 - 0. 549834 0. 688896 1. 361288 - 0. 381137 0. 797812 - 1. 128198 0. 369208 0. 540132 0. 413853 - 0. 200308 - 0. 969126 0. 981293 - 0. 009783 - 0. 320020 3 - 0. 574816 1. 419977 0. 434813 - 1. 101217 - 1. 586275 / [CLS] Python multiple pandas columns conditionally based [SEP]2162 2. 427681 - 0. 129537 - 0. 078716 1. 345644 - 1. 286094 0. 237242 - 0. 136056 0. 596664 - 1. 412381 1. 206341 0. 299860 0. 705238 0. 142412 - 1. 059382 0. 833468 1. 060015 - 0. 527045 - 1. 135732 - 1. 140983 - 0. 779540 - 0. 640875 - 1. 217196 - 1. 675663 / [CLS] Python multiple pandas columns conditionally based [SEP]06 - 2. 146352 - 0. 597339 0. 115726 0. 850683 - 0. 752239 0. 377263 - 0. 561982 0. 262783 - 0. 356676 - 0. 367462 0. 753611 - 1. 267414 - 1. 330698 - 0. 536453 0. 840938 - 0. 763108 - 0. 268100 - 0. 677424 1. 606831 0. 151732 - 2. 085701 1. 219296 0. 400863 /
A simple list comprehension for column selection might do the trick: ga['Total_Rooms'] = ga[[col for col in ga.columns if col.endswith('_Total')]].sum(axis = 1)
69,123,835
Pandas: How to split row of sentences with list values for each row on other column?
<p>I have a Pandas Dataframe like this:</p> <pre><code> id data label 1336 'The PARTIES are willing to hold' [1, 11, label_1] 1336 'The PARTIES are willing to hold' [12, 15, label_2] </code></pre> <p>I would like to split each data row thanks to informations in list of label column. For example, for the first row I would like to keep only the characters of data from first to 11 ('The PARTIES') and create a column 'label' corresponding to the last element of the list.</p> <p>Expected output:</p> <pre><code> id data label 1336 'The PARTIES' label_1 1336 'are wi' label_2 </code></pre>
69,123,975
"2021-09-09T19:35:20.173000"
1
null
0
33
python|pandas
<p>You can apply a custom function on <code>data</code> column using <code>label</code> column information:</p> <pre><code>extract_lbl = lambda x: (x['data'][x['label'][0]:x['label'][1]+1], x['label'][2]) df[['data', 'label']] = df.apply(extract_lbl, axis=1, result_type='expand') </code></pre> <p>Output:</p> <pre><code>&gt;&gt;&gt; df id data label 0 1336 The PARTIES label_1 1 1336 are label_2 </code></pre>
"2021-09-09T19:49:18.817000"
1
https://pandas.pydata.org/docs/user_guide/groupby.html
Group by: split-apply-combine# Group by: split-apply-combine# By “group by” we are referring to a process involving one or more of the following steps: Splitting the data into groups based on some criteria. Applying a function to each group independently. Combining the results into a data structure. Out of these, the split step is the most straightforward. In fact, in many situations we may wish to split the data set into groups and do something with You can apply a custom function on data column using label column information: extract_lbl = lambda x: (x['data'][x['label'][0]:x['label'][1]+1], x['label'][2]) df[['data', 'label']] = df.apply(extract_lbl, axis=1, result_type='expand') Output: >>> df id data label 0 1336 The PARTIES label_1 1 1336 are label_2 those groups. In the apply step, we might wish to do one of the following: Aggregation: compute a summary statistic (or statistics) for each group. Some examples: Compute group sums or means. Compute group sizes / counts. Transformation: perform some group-specific computations and return a like-indexed object. Some examples: Standardize data (zscore) within a group. Filling NAs within groups with a value derived from each group. Filtration: discard some groups, according to a group-wise computation that evaluates True or False. Some examples: Discard data that belongs to groups with only a few members. Filter out data based on the group sum or mean. Some combination of the above: GroupBy will examine the results of the apply step and try to return a sensibly combined result if it doesn’t fit into either of the above two categories. Since the set of object instance methods on pandas data structures are generally rich and expressive, we often simply want to invoke, say, a DataFrame function on each group. The name GroupBy should be quite familiar to those who have used a SQL-based tool (or itertools), in which you can write code like: SELECT Column1, Column2, mean(Column3), sum(Column4) FROM SomeTable GROUP BY Column1, Column2 We aim to make operations like this natural and easy to express using pandas. We’ll address each area of GroupBy functionality then provide some non-trivial examples / use cases. See the cookbook for some advanced strategies. Splitting an object into groups# pandas objects can be split on any of their axes. The abstract definition of grouping is to provide a mapping of labels to group names. To create a GroupBy object (more on what the GroupBy object is later), you may do the following: In [1]: df = pd.DataFrame( ...: [ ...: ("bird", "Falconiformes", 389.0), ...: ("bird", "Psittaciformes", 24.0), ...: ("mammal", "Carnivora", 80.2), ...: ("mammal", "Primates", np.nan), ...: ("mammal", "Carnivora", 58), ...: ], ...: index=["falcon", "parrot", "lion", "monkey", "leopard"], ...: columns=("class", "order", "max_speed"), ...: ) ...: In [2]: df Out[2]: class order max_speed falcon bird Falconiformes 389.0 parrot bird Psittaciformes 24.0 lion mammal Carnivora 80.2 monkey mammal Primates NaN leopard mammal Carnivora 58.0 # default is axis=0 In [3]: grouped = df.groupby("class") In [4]: grouped = df.groupby("order", axis="columns") In [5]: grouped = df.groupby(["class", "order"]) The mapping can be specified many different ways: A Python function, to be called on each of the axis labels. A list or NumPy array of the same length as the selected axis. A dict or Series, providing a label -> group name mapping. For DataFrame objects, a string indicating either a column name or an index level name to be used to group. df.groupby('A') is just syntactic sugar for df.groupby(df['A']). A list of any of the above things. Collectively we refer to the grouping objects as the keys. For example, consider the following DataFrame: Note A string passed to groupby may refer to either a column or an index level. If a string matches both a column name and an index level name, a ValueError will be raised. In [6]: df = pd.DataFrame( ...: { ...: "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"], ...: "B": ["one", "one", "two", "three", "two", "two", "one", "three"], ...: "C": np.random.randn(8), ...: "D": np.random.randn(8), ...: } ...: ) ...: In [7]: df Out[7]: A B C D 0 foo one 0.469112 -0.861849 1 bar one -0.282863 -2.104569 2 foo two -1.509059 -0.494929 3 bar three -1.135632 1.071804 4 foo two 1.212112 0.721555 5 bar two -0.173215 -0.706771 6 foo one 0.119209 -1.039575 7 foo three -1.044236 0.271860 On a DataFrame, we obtain a GroupBy object by calling groupby(). We could naturally group by either the A or B columns, or both: In [8]: grouped = df.groupby("A") In [9]: grouped = df.groupby(["A", "B"]) If we also have a MultiIndex on columns A and B, we can group by all but the specified columns In [10]: df2 = df.set_index(["A", "B"]) In [11]: grouped = df2.groupby(level=df2.index.names.difference(["B"])) In [12]: grouped.sum() Out[12]: C D A bar -1.591710 -1.739537 foo -0.752861 -1.402938 These will split the DataFrame on its index (rows). We could also split by the columns: In [13]: def get_letter_type(letter): ....: if letter.lower() in 'aeiou': ....: return 'vowel' ....: else: ....: return 'consonant' ....: In [14]: grouped = df.groupby(get_letter_type, axis=1) pandas Index objects support duplicate values. If a non-unique index is used as the group key in a groupby operation, all values for the same index value will be considered to be in one group and thus the output of aggregation functions will only contain unique index values: In [15]: lst = [1, 2, 3, 1, 2, 3] In [16]: s = pd.Series([1, 2, 3, 10, 20, 30], lst) In [17]: grouped = s.groupby(level=0) In [18]: grouped.first() Out[18]: 1 1 2 2 3 3 dtype: int64 In [19]: grouped.last() Out[19]: 1 10 2 20 3 30 dtype: int64 In [20]: grouped.sum() Out[20]: 1 11 2 22 3 33 dtype: int64 Note that no splitting occurs until it’s needed. Creating the GroupBy object only verifies that you’ve passed a valid mapping. Note Many kinds of complicated data manipulations can be expressed in terms of GroupBy operations (though can’t be guaranteed to be the most efficient). You can get quite creative with the label mapping functions. GroupBy sorting# By default the group keys are sorted during the groupby operation. You may however pass sort=False for potential speedups: In [21]: df2 = pd.DataFrame({"X": ["B", "B", "A", "A"], "Y": [1, 2, 3, 4]}) In [22]: df2.groupby(["X"]).sum() Out[22]: Y X A 7 B 3 In [23]: df2.groupby(["X"], sort=False).sum() Out[23]: Y X B 3 A 7 Note that groupby will preserve the order in which observations are sorted within each group. For example, the groups created by groupby() below are in the order they appeared in the original DataFrame: In [24]: df3 = pd.DataFrame({"X": ["A", "B", "A", "B"], "Y": [1, 4, 3, 2]}) In [25]: df3.groupby(["X"]).get_group("A") Out[25]: X Y 0 A 1 2 A 3 In [26]: df3.groupby(["X"]).get_group("B") Out[26]: X Y 1 B 4 3 B 2 New in version 1.1.0. GroupBy dropna# By default NA values are excluded from group keys during the groupby operation. However, in case you want to include NA values in group keys, you could pass dropna=False to achieve it. In [27]: df_list = [[1, 2, 3], [1, None, 4], [2, 1, 3], [1, 2, 2]] In [28]: df_dropna = pd.DataFrame(df_list, columns=["a", "b", "c"]) In [29]: df_dropna Out[29]: a b c 0 1 2.0 3 1 1 NaN 4 2 2 1.0 3 3 1 2.0 2 # Default ``dropna`` is set to True, which will exclude NaNs in keys In [30]: df_dropna.groupby(by=["b"], dropna=True).sum() Out[30]: a c b 1.0 2 3 2.0 2 5 # In order to allow NaN in keys, set ``dropna`` to False In [31]: df_dropna.groupby(by=["b"], dropna=False).sum() Out[31]: a c b 1.0 2 3 2.0 2 5 NaN 1 4 The default setting of dropna argument is True which means NA are not included in group keys. GroupBy object attributes# The groups attribute is a dict whose keys are the computed unique groups and corresponding values being the axis labels belonging to each group. In the above example we have: In [32]: df.groupby("A").groups Out[32]: {'bar': [1, 3, 5], 'foo': [0, 2, 4, 6, 7]} In [33]: df.groupby(get_letter_type, axis=1).groups Out[33]: {'consonant': ['B', 'C', 'D'], 'vowel': ['A']} Calling the standard Python len function on the GroupBy object just returns the length of the groups dict, so it is largely just a convenience: In [34]: grouped = df.groupby(["A", "B"]) In [35]: grouped.groups Out[35]: {('bar', 'one'): [1], ('bar', 'three'): [3], ('bar', 'two'): [5], ('foo', 'one'): [0, 6], ('foo', 'three'): [7], ('foo', 'two'): [2, 4]} In [36]: len(grouped) Out[36]: 6 GroupBy will tab complete column names (and other attributes): In [37]: df Out[37]: height weight gender 2000-01-01 42.849980 157.500553 male 2000-01-02 49.607315 177.340407 male 2000-01-03 56.293531 171.524640 male 2000-01-04 48.421077 144.251986 female 2000-01-05 46.556882 152.526206 male 2000-01-06 68.448851 168.272968 female 2000-01-07 70.757698 136.431469 male 2000-01-08 58.909500 176.499753 female 2000-01-09 76.435631 174.094104 female 2000-01-10 45.306120 177.540920 male In [38]: gb = df.groupby("gender") In [39]: gb.<TAB> # noqa: E225, E999 gb.agg gb.boxplot gb.cummin gb.describe gb.filter gb.get_group gb.height gb.last gb.median gb.ngroups gb.plot gb.rank gb.std gb.transform gb.aggregate gb.count gb.cumprod gb.dtype gb.first gb.groups gb.hist gb.max gb.min gb.nth gb.prod gb.resample gb.sum gb.var gb.apply gb.cummax gb.cumsum gb.fillna gb.gender gb.head gb.indices gb.mean gb.name gb.ohlc gb.quantile gb.size gb.tail gb.weight GroupBy with MultiIndex# With hierarchically-indexed data, it’s quite natural to group by one of the levels of the hierarchy. Let’s create a Series with a two-level MultiIndex. In [40]: arrays = [ ....: ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"], ....: ["one", "two", "one", "two", "one", "two", "one", "two"], ....: ] ....: In [41]: index = pd.MultiIndex.from_arrays(arrays, names=["first", "second"]) In [42]: s = pd.Series(np.random.randn(8), index=index) In [43]: s Out[43]: first second bar one -0.919854 two -0.042379 baz one 1.247642 two -0.009920 foo one 0.290213 two 0.495767 qux one 0.362949 two 1.548106 dtype: float64 We can then group by one of the levels in s. In [44]: grouped = s.groupby(level=0) In [45]: grouped.sum() Out[45]: first bar -0.962232 baz 1.237723 foo 0.785980 qux 1.911055 dtype: float64 If the MultiIndex has names specified, these can be passed instead of the level number: In [46]: s.groupby(level="second").sum() Out[46]: second one 0.980950 two 1.991575 dtype: float64 Grouping with multiple levels is supported. In [47]: s Out[47]: first second third bar doo one -1.131345 two -0.089329 baz bee one 0.337863 two -0.945867 foo bop one -0.932132 two 1.956030 qux bop one 0.017587 two -0.016692 dtype: float64 In [48]: s.groupby(level=["first", "second"]).sum() Out[48]: first second bar doo -1.220674 baz bee -0.608004 foo bop 1.023898 qux bop 0.000895 dtype: float64 Index level names may be supplied as keys. In [49]: s.groupby(["first", "second"]).sum() Out[49]: first second bar doo -1.220674 baz bee -0.608004 foo bop 1.023898 qux bop 0.000895 dtype: float64 More on the sum function and aggregation later. Grouping DataFrame with Index levels and columns# A DataFrame may be grouped by a combination of columns and index levels by specifying the column names as strings and the index levels as pd.Grouper objects. In [50]: arrays = [ ....: ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"], ....: ["one", "two", "one", "two", "one", "two", "one", "two"], ....: ] ....: In [51]: index = pd.MultiIndex.from_arrays(arrays, names=["first", "second"]) In [52]: df = pd.DataFrame({"A": [1, 1, 1, 1, 2, 2, 3, 3], "B": np.arange(8)}, index=index) In [53]: df Out[53]: A B first second bar one 1 0 two 1 1 baz one 1 2 two 1 3 foo one 2 4 two 2 5 qux one 3 6 two 3 7 The following example groups df by the second index level and the A column. In [54]: df.groupby([pd.Grouper(level=1), "A"]).sum() Out[54]: B second A one 1 2 2 4 3 6 two 1 4 2 5 3 7 Index levels may also be specified by name. In [55]: df.groupby([pd.Grouper(level="second"), "A"]).sum() Out[55]: B second A one 1 2 2 4 3 6 two 1 4 2 5 3 7 Index level names may be specified as keys directly to groupby. In [56]: df.groupby(["second", "A"]).sum() Out[56]: B second A one 1 2 2 4 3 6 two 1 4 2 5 3 7 DataFrame column selection in GroupBy# Once you have created the GroupBy object from a DataFrame, you might want to do something different for each of the columns. Thus, using [] similar to getting a column from a DataFrame, you can do: In [57]: df = pd.DataFrame( ....: { ....: "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"], ....: "B": ["one", "one", "two", "three", "two", "two", "one", "three"], ....: "C": np.random.randn(8), ....: "D": np.random.randn(8), ....: } ....: ) ....: In [58]: df Out[58]: A B C D 0 foo one -0.575247 1.346061 1 bar one 0.254161 1.511763 2 foo two -1.143704 1.627081 3 bar three 0.215897 -0.990582 4 foo two 1.193555 -0.441652 5 bar two -0.077118 1.211526 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 In [59]: grouped = df.groupby(["A"]) In [60]: grouped_C = grouped["C"] In [61]: grouped_D = grouped["D"] This is mainly syntactic sugar for the alternative and much more verbose: In [62]: df["C"].groupby(df["A"]) Out[62]: <pandas.core.groupby.generic.SeriesGroupBy object at 0x7f1ea100a490> Additionally this method avoids recomputing the internal grouping information derived from the passed key. Iterating through groups# With the GroupBy object in hand, iterating through the grouped data is very natural and functions similarly to itertools.groupby(): In [63]: grouped = df.groupby('A') In [64]: for name, group in grouped: ....: print(name) ....: print(group) ....: bar A B C D 1 bar one 0.254161 1.511763 3 bar three 0.215897 -0.990582 5 bar two -0.077118 1.211526 foo A B C D 0 foo one -0.575247 1.346061 2 foo two -1.143704 1.627081 4 foo two 1.193555 -0.441652 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 In the case of grouping by multiple keys, the group name will be a tuple: In [65]: for name, group in df.groupby(['A', 'B']): ....: print(name) ....: print(group) ....: ('bar', 'one') A B C D 1 bar one 0.254161 1.511763 ('bar', 'three') A B C D 3 bar three 0.215897 -0.990582 ('bar', 'two') A B C D 5 bar two -0.077118 1.211526 ('foo', 'one') A B C D 0 foo one -0.575247 1.346061 6 foo one -0.408530 0.268520 ('foo', 'three') A B C D 7 foo three -0.862495 0.02458 ('foo', 'two') A B C D 2 foo two -1.143704 1.627081 4 foo two 1.193555 -0.441652 See Iterating through groups. Selecting a group# A single group can be selected using get_group(): In [66]: grouped.get_group("bar") Out[66]: A B C D 1 bar one 0.254161 1.511763 3 bar three 0.215897 -0.990582 5 bar two -0.077118 1.211526 Or for an object grouped on multiple columns: In [67]: df.groupby(["A", "B"]).get_group(("bar", "one")) Out[67]: A B C D 1 bar one 0.254161 1.511763 Aggregation# Once the GroupBy object has been created, several methods are available to perform a computation on the grouped data. These operations are similar to the aggregating API, window API, and resample API. An obvious one is aggregation via the aggregate() or equivalently agg() method: In [68]: grouped = df.groupby("A") In [69]: grouped[["C", "D"]].aggregate(np.sum) Out[69]: C D A bar 0.392940 1.732707 foo -1.796421 2.824590 In [70]: grouped = df.groupby(["A", "B"]) In [71]: grouped.aggregate(np.sum) Out[71]: C D A B bar one 0.254161 1.511763 three 0.215897 -0.990582 two -0.077118 1.211526 foo one -0.983776 1.614581 three -0.862495 0.024580 two 0.049851 1.185429 As you can see, the result of the aggregation will have the group names as the new index along the grouped axis. In the case of multiple keys, the result is a MultiIndex by default, though this can be changed by using the as_index option: In [72]: grouped = df.groupby(["A", "B"], as_index=False) In [73]: grouped.aggregate(np.sum) Out[73]: A B C D 0 bar one 0.254161 1.511763 1 bar three 0.215897 -0.990582 2 bar two -0.077118 1.211526 3 foo one -0.983776 1.614581 4 foo three -0.862495 0.024580 5 foo two 0.049851 1.185429 In [74]: df.groupby("A", as_index=False)[["C", "D"]].sum() Out[74]: A C D 0 bar 0.392940 1.732707 1 foo -1.796421 2.824590 Note that you could use the reset_index DataFrame function to achieve the same result as the column names are stored in the resulting MultiIndex: In [75]: df.groupby(["A", "B"]).sum().reset_index() Out[75]: A B C D 0 bar one 0.254161 1.511763 1 bar three 0.215897 -0.990582 2 bar two -0.077118 1.211526 3 foo one -0.983776 1.614581 4 foo three -0.862495 0.024580 5 foo two 0.049851 1.185429 Another simple aggregation example is to compute the size of each group. This is included in GroupBy as the size method. It returns a Series whose index are the group names and whose values are the sizes of each group. In [76]: grouped.size() Out[76]: A B size 0 bar one 1 1 bar three 1 2 bar two 1 3 foo one 2 4 foo three 1 5 foo two 2 In [77]: grouped.describe() Out[77]: C ... D count mean std min ... 25% 50% 75% max 0 1.0 0.254161 NaN 0.254161 ... 1.511763 1.511763 1.511763 1.511763 1 1.0 0.215897 NaN 0.215897 ... -0.990582 -0.990582 -0.990582 -0.990582 2 1.0 -0.077118 NaN -0.077118 ... 1.211526 1.211526 1.211526 1.211526 3 2.0 -0.491888 0.117887 -0.575247 ... 0.537905 0.807291 1.076676 1.346061 4 1.0 -0.862495 NaN -0.862495 ... 0.024580 0.024580 0.024580 0.024580 5 2.0 0.024925 1.652692 -1.143704 ... 0.075531 0.592714 1.109898 1.627081 [6 rows x 16 columns] Another aggregation example is to compute the number of unique values of each group. This is similar to the value_counts function, except that it only counts unique values. In [78]: ll = [['foo', 1], ['foo', 2], ['foo', 2], ['bar', 1], ['bar', 1]] In [79]: df4 = pd.DataFrame(ll, columns=["A", "B"]) In [80]: df4 Out[80]: A B 0 foo 1 1 foo 2 2 foo 2 3 bar 1 4 bar 1 In [81]: df4.groupby("A")["B"].nunique() Out[81]: A bar 1 foo 2 Name: B, dtype: int64 Note Aggregation functions will not return the groups that you are aggregating over if they are named columns, when as_index=True, the default. The grouped columns will be the indices of the returned object. Passing as_index=False will return the groups that you are aggregating over, if they are named columns. Aggregating functions are the ones that reduce the dimension of the returned objects. Some common aggregating functions are tabulated below: Function Description mean() Compute mean of groups sum() Compute sum of group values size() Compute group sizes count() Compute count of group std() Standard deviation of groups var() Compute variance of groups sem() Standard error of the mean of groups describe() Generates descriptive statistics first() Compute first of group values last() Compute last of group values nth() Take nth value, or a subset if n is a list min() Compute min of group values max() Compute max of group values The aggregating functions above will exclude NA values. Any function which reduces a Series to a scalar value is an aggregation function and will work, a trivial example is df.groupby('A').agg(lambda ser: 1). Note that nth() can act as a reducer or a filter, see here. Applying multiple functions at once# With grouped Series you can also pass a list or dict of functions to do aggregation with, outputting a DataFrame: In [82]: grouped = df.groupby("A") In [83]: grouped["C"].agg([np.sum, np.mean, np.std]) Out[83]: sum mean std A bar 0.392940 0.130980 0.181231 foo -1.796421 -0.359284 0.912265 On a grouped DataFrame, you can pass a list of functions to apply to each column, which produces an aggregated result with a hierarchical index: In [84]: grouped[["C", "D"]].agg([np.sum, np.mean, np.std]) Out[84]: C D sum mean std sum mean std A bar 0.392940 0.130980 0.181231 1.732707 0.577569 1.366330 foo -1.796421 -0.359284 0.912265 2.824590 0.564918 0.884785 The resulting aggregations are named for the functions themselves. If you need to rename, then you can add in a chained operation for a Series like this: In [85]: ( ....: grouped["C"] ....: .agg([np.sum, np.mean, np.std]) ....: .rename(columns={"sum": "foo", "mean": "bar", "std": "baz"}) ....: ) ....: Out[85]: foo bar baz A bar 0.392940 0.130980 0.181231 foo -1.796421 -0.359284 0.912265 For a grouped DataFrame, you can rename in a similar manner: In [86]: ( ....: grouped[["C", "D"]].agg([np.sum, np.mean, np.std]).rename( ....: columns={"sum": "foo", "mean": "bar", "std": "baz"} ....: ) ....: ) ....: Out[86]: C D foo bar baz foo bar baz A bar 0.392940 0.130980 0.181231 1.732707 0.577569 1.366330 foo -1.796421 -0.359284 0.912265 2.824590 0.564918 0.884785 Note In general, the output column names should be unique. You can’t apply the same function (or two functions with the same name) to the same column. In [87]: grouped["C"].agg(["sum", "sum"]) Out[87]: sum sum A bar 0.392940 0.392940 foo -1.796421 -1.796421 pandas does allow you to provide multiple lambdas. In this case, pandas will mangle the name of the (nameless) lambda functions, appending _<i> to each subsequent lambda. In [88]: grouped["C"].agg([lambda x: x.max() - x.min(), lambda x: x.median() - x.mean()]) Out[88]: <lambda_0> <lambda_1> A bar 0.331279 0.084917 foo 2.337259 -0.215962 Named aggregation# New in version 0.25.0. To support column-specific aggregation with control over the output column names, pandas accepts the special syntax in GroupBy.agg(), known as “named aggregation”, where The keywords are the output column names The values are tuples whose first element is the column to select and the second element is the aggregation to apply to that column. pandas provides the pandas.NamedAgg namedtuple with the fields ['column', 'aggfunc'] to make it clearer what the arguments are. As usual, the aggregation can be a callable or a string alias. In [89]: animals = pd.DataFrame( ....: { ....: "kind": ["cat", "dog", "cat", "dog"], ....: "height": [9.1, 6.0, 9.5, 34.0], ....: "weight": [7.9, 7.5, 9.9, 198.0], ....: } ....: ) ....: In [90]: animals Out[90]: kind height weight 0 cat 9.1 7.9 1 dog 6.0 7.5 2 cat 9.5 9.9 3 dog 34.0 198.0 In [91]: animals.groupby("kind").agg( ....: min_height=pd.NamedAgg(column="height", aggfunc="min"), ....: max_height=pd.NamedAgg(column="height", aggfunc="max"), ....: average_weight=pd.NamedAgg(column="weight", aggfunc=np.mean), ....: ) ....: Out[91]: min_height max_height average_weight kind cat 9.1 9.5 8.90 dog 6.0 34.0 102.75 pandas.NamedAgg is just a namedtuple. Plain tuples are allowed as well. In [92]: animals.groupby("kind").agg( ....: min_height=("height", "min"), ....: max_height=("height", "max"), ....: average_weight=("weight", np.mean), ....: ) ....: Out[92]: min_height max_height average_weight kind cat 9.1 9.5 8.90 dog 6.0 34.0 102.75 If your desired output column names are not valid Python keywords, construct a dictionary and unpack the keyword arguments In [93]: animals.groupby("kind").agg( ....: **{ ....: "total weight": pd.NamedAgg(column="weight", aggfunc=sum) ....: } ....: ) ....: Out[93]: total weight kind cat 17.8 dog 205.5 Additional keyword arguments are not passed through to the aggregation functions. Only pairs of (column, aggfunc) should be passed as **kwargs. If your aggregation functions requires additional arguments, partially apply them with functools.partial(). Note For Python 3.5 and earlier, the order of **kwargs in a functions was not preserved. This means that the output column ordering would not be consistent. To ensure consistent ordering, the keys (and so output columns) will always be sorted for Python 3.5. Named aggregation is also valid for Series groupby aggregations. In this case there’s no column selection, so the values are just the functions. In [94]: animals.groupby("kind").height.agg( ....: min_height="min", ....: max_height="max", ....: ) ....: Out[94]: min_height max_height kind cat 9.1 9.5 dog 6.0 34.0 Applying different functions to DataFrame columns# By passing a dict to aggregate you can apply a different aggregation to the columns of a DataFrame: In [95]: grouped.agg({"C": np.sum, "D": lambda x: np.std(x, ddof=1)}) Out[95]: C D A bar 0.392940 1.366330 foo -1.796421 0.884785 The function names can also be strings. In order for a string to be valid it must be either implemented on GroupBy or available via dispatching: In [96]: grouped.agg({"C": "sum", "D": "std"}) Out[96]: C D A bar 0.392940 1.366330 foo -1.796421 0.884785 Cython-optimized aggregation functions# Some common aggregations, currently only sum, mean, std, and sem, have optimized Cython implementations: In [97]: df.groupby("A")[["C", "D"]].sum() Out[97]: C D A bar 0.392940 1.732707 foo -1.796421 2.824590 In [98]: df.groupby(["A", "B"]).mean() Out[98]: C D A B bar one 0.254161 1.511763 three 0.215897 -0.990582 two -0.077118 1.211526 foo one -0.491888 0.807291 three -0.862495 0.024580 two 0.024925 0.592714 Of course sum and mean are implemented on pandas objects, so the above code would work even without the special versions via dispatching (see below). Aggregations with User-Defined Functions# Users can also provide their own functions for custom aggregations. When aggregating with a User-Defined Function (UDF), the UDF should not mutate the provided Series, see Mutating with User Defined Function (UDF) methods for more information. In [99]: animals.groupby("kind")[["height"]].agg(lambda x: set(x)) Out[99]: height kind cat {9.1, 9.5} dog {34.0, 6.0} The resulting dtype will reflect that of the aggregating function. If the results from different groups have different dtypes, then a common dtype will be determined in the same way as DataFrame construction. In [100]: animals.groupby("kind")[["height"]].agg(lambda x: x.astype(int).sum()) Out[100]: height kind cat 18 dog 40 Transformation# The transform method returns an object that is indexed the same as the one being grouped. The transform function must: Return a result that is either the same size as the group chunk or broadcastable to the size of the group chunk (e.g., a scalar, grouped.transform(lambda x: x.iloc[-1])). Operate column-by-column on the group chunk. The transform is applied to the first group chunk using chunk.apply. Not perform in-place operations on the group chunk. Group chunks should be treated as immutable, and changes to a group chunk may produce unexpected results. For example, when using fillna, inplace must be False (grouped.transform(lambda x: x.fillna(inplace=False))). (Optionally) operates on the entire group chunk. If this is supported, a fast path is used starting from the second chunk. Deprecated since version 1.5.0: When using .transform on a grouped DataFrame and the transformation function returns a DataFrame, currently pandas does not align the result’s index with the input’s index. This behavior is deprecated and alignment will be performed in a future version of pandas. You can apply .to_numpy() to the result of the transformation function to avoid alignment. Similar to Aggregations with User-Defined Functions, the resulting dtype will reflect that of the transformation function. If the results from different groups have different dtypes, then a common dtype will be determined in the same way as DataFrame construction. Suppose we wished to standardize the data within each group: In [101]: index = pd.date_range("10/1/1999", periods=1100) In [102]: ts = pd.Series(np.random.normal(0.5, 2, 1100), index) In [103]: ts = ts.rolling(window=100, min_periods=100).mean().dropna() In [104]: ts.head() Out[104]: 2000-01-08 0.779333 2000-01-09 0.778852 2000-01-10 0.786476 2000-01-11 0.782797 2000-01-12 0.798110 Freq: D, dtype: float64 In [105]: ts.tail() Out[105]: 2002-09-30 0.660294 2002-10-01 0.631095 2002-10-02 0.673601 2002-10-03 0.709213 2002-10-04 0.719369 Freq: D, dtype: float64 In [106]: transformed = ts.groupby(lambda x: x.year).transform( .....: lambda x: (x - x.mean()) / x.std() .....: ) .....: We would expect the result to now have mean 0 and standard deviation 1 within each group, which we can easily check: # Original Data In [107]: grouped = ts.groupby(lambda x: x.year) In [108]: grouped.mean() Out[108]: 2000 0.442441 2001 0.526246 2002 0.459365 dtype: float64 In [109]: grouped.std() Out[109]: 2000 0.131752 2001 0.210945 2002 0.128753 dtype: float64 # Transformed Data In [110]: grouped_trans = transformed.groupby(lambda x: x.year) In [111]: grouped_trans.mean() Out[111]: 2000 -4.870756e-16 2001 -1.545187e-16 2002 4.136282e-16 dtype: float64 In [112]: grouped_trans.std() Out[112]: 2000 1.0 2001 1.0 2002 1.0 dtype: float64 We can also visually compare the original and transformed data sets. In [113]: compare = pd.DataFrame({"Original": ts, "Transformed": transformed}) In [114]: compare.plot() Out[114]: <AxesSubplot: > Transformation functions that have lower dimension outputs are broadcast to match the shape of the input array. In [115]: ts.groupby(lambda x: x.year).transform(lambda x: x.max() - x.min()) Out[115]: 2000-01-08 0.623893 2000-01-09 0.623893 2000-01-10 0.623893 2000-01-11 0.623893 2000-01-12 0.623893 ... 2002-09-30 0.558275 2002-10-01 0.558275 2002-10-02 0.558275 2002-10-03 0.558275 2002-10-04 0.558275 Freq: D, Length: 1001, dtype: float64 Alternatively, the built-in methods could be used to produce the same outputs. In [116]: max_ts = ts.groupby(lambda x: x.year).transform("max") In [117]: min_ts = ts.groupby(lambda x: x.year).transform("min") In [118]: max_ts - min_ts Out[118]: 2000-01-08 0.623893 2000-01-09 0.623893 2000-01-10 0.623893 2000-01-11 0.623893 2000-01-12 0.623893 ... 2002-09-30 0.558275 2002-10-01 0.558275 2002-10-02 0.558275 2002-10-03 0.558275 2002-10-04 0.558275 Freq: D, Length: 1001, dtype: float64 Another common data transform is to replace missing data with the group mean. In [119]: data_df Out[119]: A B C 0 1.539708 -1.166480 0.533026 1 1.302092 -0.505754 NaN 2 -0.371983 1.104803 -0.651520 3 -1.309622 1.118697 -1.161657 4 -1.924296 0.396437 0.812436 .. ... ... ... 995 -0.093110 0.683847 -0.774753 996 -0.185043 1.438572 NaN 997 -0.394469 -0.642343 0.011374 998 -1.174126 1.857148 NaN 999 0.234564 0.517098 0.393534 [1000 rows x 3 columns] In [120]: countries = np.array(["US", "UK", "GR", "JP"]) In [121]: key = countries[np.random.randint(0, 4, 1000)] In [122]: grouped = data_df.groupby(key) # Non-NA count in each group In [123]: grouped.count() Out[123]: A B C GR 209 217 189 JP 240 255 217 UK 216 231 193 US 239 250 217 In [124]: transformed = grouped.transform(lambda x: x.fillna(x.mean())) We can verify that the group means have not changed in the transformed data and that the transformed data contains no NAs. In [125]: grouped_trans = transformed.groupby(key) In [126]: grouped.mean() # original group means Out[126]: A B C GR -0.098371 -0.015420 0.068053 JP 0.069025 0.023100 -0.077324 UK 0.034069 -0.052580 -0.116525 US 0.058664 -0.020399 0.028603 In [127]: grouped_trans.mean() # transformation did not change group means Out[127]: A B C GR -0.098371 -0.015420 0.068053 JP 0.069025 0.023100 -0.077324 UK 0.034069 -0.052580 -0.116525 US 0.058664 -0.020399 0.028603 In [128]: grouped.count() # original has some missing data points Out[128]: A B C GR 209 217 189 JP 240 255 217 UK 216 231 193 US 239 250 217 In [129]: grouped_trans.count() # counts after transformation Out[129]: A B C GR 228 228 228 JP 267 267 267 UK 247 247 247 US 258 258 258 In [130]: grouped_trans.size() # Verify non-NA count equals group size Out[130]: GR 228 JP 267 UK 247 US 258 dtype: int64 Note Some functions will automatically transform the input when applied to a GroupBy object, but returning an object of the same shape as the original. Passing as_index=False will not affect these transformation methods. For example: fillna, ffill, bfill, shift.. In [131]: grouped.ffill() Out[131]: A B C 0 1.539708 -1.166480 0.533026 1 1.302092 -0.505754 0.533026 2 -0.371983 1.104803 -0.651520 3 -1.309622 1.118697 -1.161657 4 -1.924296 0.396437 0.812436 .. ... ... ... 995 -0.093110 0.683847 -0.774753 996 -0.185043 1.438572 -0.774753 997 -0.394469 -0.642343 0.011374 998 -1.174126 1.857148 -0.774753 999 0.234564 0.517098 0.393534 [1000 rows x 3 columns] Window and resample operations# It is possible to use resample(), expanding() and rolling() as methods on groupbys. The example below will apply the rolling() method on the samples of the column B based on the groups of column A. In [132]: df_re = pd.DataFrame({"A": [1] * 10 + [5] * 10, "B": np.arange(20)}) In [133]: df_re Out[133]: A B 0 1 0 1 1 1 2 1 2 3 1 3 4 1 4 .. .. .. 15 5 15 16 5 16 17 5 17 18 5 18 19 5 19 [20 rows x 2 columns] In [134]: df_re.groupby("A").rolling(4).B.mean() Out[134]: A 1 0 NaN 1 NaN 2 NaN 3 1.5 4 2.5 ... 5 15 13.5 16 14.5 17 15.5 18 16.5 19 17.5 Name: B, Length: 20, dtype: float64 The expanding() method will accumulate a given operation (sum() in the example) for all the members of each particular group. In [135]: df_re.groupby("A").expanding().sum() Out[135]: B A 1 0 0.0 1 1.0 2 3.0 3 6.0 4 10.0 ... ... 5 15 75.0 16 91.0 17 108.0 18 126.0 19 145.0 [20 rows x 1 columns] Suppose you want to use the resample() method to get a daily frequency in each group of your dataframe and wish to complete the missing values with the ffill() method. In [136]: df_re = pd.DataFrame( .....: { .....: "date": pd.date_range(start="2016-01-01", periods=4, freq="W"), .....: "group": [1, 1, 2, 2], .....: "val": [5, 6, 7, 8], .....: } .....: ).set_index("date") .....: In [137]: df_re Out[137]: group val date 2016-01-03 1 5 2016-01-10 1 6 2016-01-17 2 7 2016-01-24 2 8 In [138]: df_re.groupby("group").resample("1D").ffill() Out[138]: group val group date 1 2016-01-03 1 5 2016-01-04 1 5 2016-01-05 1 5 2016-01-06 1 5 2016-01-07 1 5 ... ... ... 2 2016-01-20 2 7 2016-01-21 2 7 2016-01-22 2 7 2016-01-23 2 7 2016-01-24 2 8 [16 rows x 2 columns] Filtration# The filter method returns a subset of the original object. Suppose we want to take only elements that belong to groups with a group sum greater than 2. In [139]: sf = pd.Series([1, 1, 2, 3, 3, 3]) In [140]: sf.groupby(sf).filter(lambda x: x.sum() > 2) Out[140]: 3 3 4 3 5 3 dtype: int64 The argument of filter must be a function that, applied to the group as a whole, returns True or False. Another useful operation is filtering out elements that belong to groups with only a couple members. In [141]: dff = pd.DataFrame({"A": np.arange(8), "B": list("aabbbbcc")}) In [142]: dff.groupby("B").filter(lambda x: len(x) > 2) Out[142]: A B 2 2 b 3 3 b 4 4 b 5 5 b Alternatively, instead of dropping the offending groups, we can return a like-indexed objects where the groups that do not pass the filter are filled with NaNs. In [143]: dff.groupby("B").filter(lambda x: len(x) > 2, dropna=False) Out[143]: A B 0 NaN NaN 1 NaN NaN 2 2.0 b 3 3.0 b 4 4.0 b 5 5.0 b 6 NaN NaN 7 NaN NaN For DataFrames with multiple columns, filters should explicitly specify a column as the filter criterion. In [144]: dff["C"] = np.arange(8) In [145]: dff.groupby("B").filter(lambda x: len(x["C"]) > 2) Out[145]: A B C 2 2 b 2 3 3 b 3 4 4 b 4 5 5 b 5 Note Some functions when applied to a groupby object will act as a filter on the input, returning a reduced shape of the original (and potentially eliminating groups), but with the index unchanged. Passing as_index=False will not affect these transformation methods. For example: head, tail. In [146]: dff.groupby("B").head(2) Out[146]: A B C 0 0 a 0 1 1 a 1 2 2 b 2 3 3 b 3 6 6 c 6 7 7 c 7 Dispatching to instance methods# When doing an aggregation or transformation, you might just want to call an instance method on each data group. This is pretty easy to do by passing lambda functions: In [147]: grouped = df.groupby("A") In [148]: grouped.agg(lambda x: x.std()) Out[148]: C D A bar 0.181231 1.366330 foo 0.912265 0.884785 But, it’s rather verbose and can be untidy if you need to pass additional arguments. Using a bit of metaprogramming cleverness, GroupBy now has the ability to “dispatch” method calls to the groups: In [149]: grouped.std() Out[149]: C D A bar 0.181231 1.366330 foo 0.912265 0.884785 What is actually happening here is that a function wrapper is being generated. When invoked, it takes any passed arguments and invokes the function with any arguments on each group (in the above example, the std function). The results are then combined together much in the style of agg and transform (it actually uses apply to infer the gluing, documented next). This enables some operations to be carried out rather succinctly: In [150]: tsdf = pd.DataFrame( .....: np.random.randn(1000, 3), .....: index=pd.date_range("1/1/2000", periods=1000), .....: columns=["A", "B", "C"], .....: ) .....: In [151]: tsdf.iloc[::2] = np.nan In [152]: grouped = tsdf.groupby(lambda x: x.year) In [153]: grouped.fillna(method="pad") Out[153]: A B C 2000-01-01 NaN NaN NaN 2000-01-02 -0.353501 -0.080957 -0.876864 2000-01-03 -0.353501 -0.080957 -0.876864 2000-01-04 0.050976 0.044273 -0.559849 2000-01-05 0.050976 0.044273 -0.559849 ... ... ... ... 2002-09-22 0.005011 0.053897 -1.026922 2002-09-23 0.005011 0.053897 -1.026922 2002-09-24 -0.456542 -1.849051 1.559856 2002-09-25 -0.456542 -1.849051 1.559856 2002-09-26 1.123162 0.354660 1.128135 [1000 rows x 3 columns] In this example, we chopped the collection of time series into yearly chunks then independently called fillna on the groups. The nlargest and nsmallest methods work on Series style groupbys: In [154]: s = pd.Series([9, 8, 7, 5, 19, 1, 4.2, 3.3]) In [155]: g = pd.Series(list("abababab")) In [156]: gb = s.groupby(g) In [157]: gb.nlargest(3) Out[157]: a 4 19.0 0 9.0 2 7.0 b 1 8.0 3 5.0 7 3.3 dtype: float64 In [158]: gb.nsmallest(3) Out[158]: a 6 4.2 2 7.0 0 9.0 b 5 1.0 7 3.3 3 5.0 dtype: float64 Flexible apply# Some operations on the grouped data might not fit into either the aggregate or transform categories. Or, you may simply want GroupBy to infer how to combine the results. For these, use the apply function, which can be substituted for both aggregate and transform in many standard use cases. However, apply can handle some exceptional use cases. Note apply can act as a reducer, transformer, or filter function, depending on exactly what is passed to it. It can depend on the passed function and exactly what you are grouping. Thus the grouped column(s) may be included in the output as well as set the indices. In [159]: df Out[159]: A B C D 0 foo one -0.575247 1.346061 1 bar one 0.254161 1.511763 2 foo two -1.143704 1.627081 3 bar three 0.215897 -0.990582 4 foo two 1.193555 -0.441652 5 bar two -0.077118 1.211526 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 In [160]: grouped = df.groupby("A") # could also just call .describe() In [161]: grouped["C"].apply(lambda x: x.describe()) Out[161]: A bar count 3.000000 mean 0.130980 std 0.181231 min -0.077118 25% 0.069390 ... foo min -1.143704 25% -0.862495 50% -0.575247 75% -0.408530 max 1.193555 Name: C, Length: 16, dtype: float64 The dimension of the returned result can also change: In [162]: grouped = df.groupby('A')['C'] In [163]: def f(group): .....: return pd.DataFrame({'original': group, .....: 'demeaned': group - group.mean()}) .....: apply on a Series can operate on a returned value from the applied function, that is itself a series, and possibly upcast the result to a DataFrame: In [164]: def f(x): .....: return pd.Series([x, x ** 2], index=["x", "x^2"]) .....: In [165]: s = pd.Series(np.random.rand(5)) In [166]: s Out[166]: 0 0.321438 1 0.493496 2 0.139505 3 0.910103 4 0.194158 dtype: float64 In [167]: s.apply(f) Out[167]: x x^2 0 0.321438 0.103323 1 0.493496 0.243538 2 0.139505 0.019462 3 0.910103 0.828287 4 0.194158 0.037697 Control grouped column(s) placement with group_keys# Note If group_keys=True is specified when calling groupby(), functions passed to apply that return like-indexed outputs will have the group keys added to the result index. Previous versions of pandas would add the group keys only when the result from the applied function had a different index than the input. If group_keys is not specified, the group keys will not be added for like-indexed outputs. In the future this behavior will change to always respect group_keys, which defaults to True. Changed in version 1.5.0. To control whether the grouped column(s) are included in the indices, you can use the argument group_keys. Compare In [168]: df.groupby("A", group_keys=True).apply(lambda x: x) Out[168]: A B C D A bar 1 bar one 0.254161 1.511763 3 bar three 0.215897 -0.990582 5 bar two -0.077118 1.211526 foo 0 foo one -0.575247 1.346061 2 foo two -1.143704 1.627081 4 foo two 1.193555 -0.441652 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 with In [169]: df.groupby("A", group_keys=False).apply(lambda x: x) Out[169]: A B C D 0 foo one -0.575247 1.346061 1 bar one 0.254161 1.511763 2 foo two -1.143704 1.627081 3 bar three 0.215897 -0.990582 4 foo two 1.193555 -0.441652 5 bar two -0.077118 1.211526 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 Similar to Aggregations with User-Defined Functions, the resulting dtype will reflect that of the apply function. If the results from different groups have different dtypes, then a common dtype will be determined in the same way as DataFrame construction. Numba Accelerated Routines# New in version 1.1. If Numba is installed as an optional dependency, the transform and aggregate methods support engine='numba' and engine_kwargs arguments. See enhancing performance with Numba for general usage of the arguments and performance considerations. The function signature must start with values, index exactly as the data belonging to each group will be passed into values, and the group index will be passed into index. Warning When using engine='numba', there will be no “fall back” behavior internally. The group data and group index will be passed as NumPy arrays to the JITed user defined function, and no alternative execution attempts will be tried. Other useful features# Automatic exclusion of “nuisance” columns# Again consider the example DataFrame we’ve been looking at: In [170]: df Out[170]: A B C D 0 foo one -0.575247 1.346061 1 bar one 0.254161 1.511763 2 foo two -1.143704 1.627081 3 bar three 0.215897 -0.990582 4 foo two 1.193555 -0.441652 5 bar two -0.077118 1.211526 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 Suppose we wish to compute the standard deviation grouped by the A column. There is a slight problem, namely that we don’t care about the data in column B. We refer to this as a “nuisance” column. You can avoid nuisance columns by specifying numeric_only=True: In [171]: df.groupby("A").std(numeric_only=True) Out[171]: C D A bar 0.181231 1.366330 foo 0.912265 0.884785 Note that df.groupby('A').colname.std(). is more efficient than df.groupby('A').std().colname, so if the result of an aggregation function is only interesting over one column (here colname), it may be filtered before applying the aggregation function. Note Any object column, also if it contains numerical values such as Decimal objects, is considered as a “nuisance” columns. They are excluded from aggregate functions automatically in groupby. If you do wish to include decimal or object columns in an aggregation with other non-nuisance data types, you must do so explicitly. Warning The automatic dropping of nuisance columns has been deprecated and will be removed in a future version of pandas. If columns are included that cannot be operated on, pandas will instead raise an error. In order to avoid this, either select the columns you wish to operate on or specify numeric_only=True. In [172]: from decimal import Decimal In [173]: df_dec = pd.DataFrame( .....: { .....: "id": [1, 2, 1, 2], .....: "int_column": [1, 2, 3, 4], .....: "dec_column": [ .....: Decimal("0.50"), .....: Decimal("0.15"), .....: Decimal("0.25"), .....: Decimal("0.40"), .....: ], .....: } .....: ) .....: # Decimal columns can be sum'd explicitly by themselves... In [174]: df_dec.groupby(["id"])[["dec_column"]].sum() Out[174]: dec_column id 1 0.75 2 0.55 # ...but cannot be combined with standard data types or they will be excluded In [175]: df_dec.groupby(["id"])[["int_column", "dec_column"]].sum() Out[175]: int_column id 1 4 2 6 # Use .agg function to aggregate over standard and "nuisance" data types # at the same time In [176]: df_dec.groupby(["id"]).agg({"int_column": "sum", "dec_column": "sum"}) Out[176]: int_column dec_column id 1 4 0.75 2 6 0.55 Handling of (un)observed Categorical values# When using a Categorical grouper (as a single grouper, or as part of multiple groupers), the observed keyword controls whether to return a cartesian product of all possible groupers values (observed=False) or only those that are observed groupers (observed=True). Show all values: In [177]: pd.Series([1, 1, 1]).groupby( .....: pd.Categorical(["a", "a", "a"], categories=["a", "b"]), observed=False .....: ).count() .....: Out[177]: a 3 b 0 dtype: int64 Show only the observed values: In [178]: pd.Series([1, 1, 1]).groupby( .....: pd.Categorical(["a", "a", "a"], categories=["a", "b"]), observed=True .....: ).count() .....: Out[178]: a 3 dtype: int64 The returned dtype of the grouped will always include all of the categories that were grouped. In [179]: s = ( .....: pd.Series([1, 1, 1]) .....: .groupby(pd.Categorical(["a", "a", "a"], categories=["a", "b"]), observed=False) .....: .count() .....: ) .....: In [180]: s.index.dtype Out[180]: CategoricalDtype(categories=['a', 'b'], ordered=False) NA and NaT group handling# If there are any NaN or NaT values in the grouping key, these will be automatically excluded. In other words, there will never be an “NA group” or “NaT group”. This was not the case in older versions of pandas, but users were generally discarding the NA group anyway (and supporting it was an implementation headache). Grouping with ordered factors# Categorical variables represented as instance of pandas’s Categorical class can be used as group keys. If so, the order of the levels will be preserved: In [181]: data = pd.Series(np.random.randn(100)) In [182]: factor = pd.qcut(data, [0, 0.25, 0.5, 0.75, 1.0]) In [183]: data.groupby(factor).mean() Out[183]: (-2.645, -0.523] -1.362896 (-0.523, 0.0296] -0.260266 (0.0296, 0.654] 0.361802 (0.654, 2.21] 1.073801 dtype: float64 Grouping with a grouper specification# You may need to specify a bit more data to properly group. You can use the pd.Grouper to provide this local control. In [184]: import datetime In [185]: df = pd.DataFrame( .....: { .....: "Branch": "A A A A A A A B".split(), .....: "Buyer": "Carl Mark Carl Carl Joe Joe Joe Carl".split(), .....: "Quantity": [1, 3, 5, 1, 8, 1, 9, 3], .....: "Date": [ .....: datetime.datetime(2013, 1, 1, 13, 0), .....: datetime.datetime(2013, 1, 1, 13, 5), .....: datetime.datetime(2013, 10, 1, 20, 0), .....: datetime.datetime(2013, 10, 2, 10, 0), .....: datetime.datetime(2013, 10, 1, 20, 0), .....: datetime.datetime(2013, 10, 2, 10, 0), .....: datetime.datetime(2013, 12, 2, 12, 0), .....: datetime.datetime(2013, 12, 2, 14, 0), .....: ], .....: } .....: ) .....: In [186]: df Out[186]: Branch Buyer Quantity Date 0 A Carl 1 2013-01-01 13:00:00 1 A Mark 3 2013-01-01 13:05:00 2 A Carl 5 2013-10-01 20:00:00 3 A Carl 1 2013-10-02 10:00:00 4 A Joe 8 2013-10-01 20:00:00 5 A Joe 1 2013-10-02 10:00:00 6 A Joe 9 2013-12-02 12:00:00 7 B Carl 3 2013-12-02 14:00:00 Groupby a specific column with the desired frequency. This is like resampling. In [187]: df.groupby([pd.Grouper(freq="1M", key="Date"), "Buyer"])[["Quantity"]].sum() Out[187]: Quantity Date Buyer 2013-01-31 Carl 1 Mark 3 2013-10-31 Carl 6 Joe 9 2013-12-31 Carl 3 Joe 9 You have an ambiguous specification in that you have a named index and a column that could be potential groupers. In [188]: df = df.set_index("Date") In [189]: df["Date"] = df.index + pd.offsets.MonthEnd(2) In [190]: df.groupby([pd.Grouper(freq="6M", key="Date"), "Buyer"])[["Quantity"]].sum() Out[190]: Quantity Date Buyer 2013-02-28 Carl 1 Mark 3 2014-02-28 Carl 9 Joe 18 In [191]: df.groupby([pd.Grouper(freq="6M", level="Date"), "Buyer"])[["Quantity"]].sum() Out[191]: Quantity Date Buyer 2013-01-31 Carl 1 Mark 3 2014-01-31 Carl 9 Joe 18 Taking the first rows of each group# Just like for a DataFrame or Series you can call head and tail on a groupby: In [192]: df = pd.DataFrame([[1, 2], [1, 4], [5, 6]], columns=["A", "B"]) In [193]: df Out[193]: A B 0 1 2 1 1 4 2 5 6 In [194]: g = df.groupby("A") In [195]: g.head(1) Out[195]: A B 0 1 2 2 5 6 In [196]: g.tail(1) Out[196]: A B 1 1 4 2 5 6 This shows the first or last n rows from each group. Taking the nth row of each group# To select from a DataFrame or Series the nth item, use nth(). This is a reduction method, and will return a single row (or no row) per group if you pass an int for n: In [197]: df = pd.DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=["A", "B"]) In [198]: g = df.groupby("A") In [199]: g.nth(0) Out[199]: B A 1 NaN 5 6.0 In [200]: g.nth(-1) Out[200]: B A 1 4.0 5 6.0 In [201]: g.nth(1) Out[201]: B A 1 4.0 If you want to select the nth not-null item, use the dropna kwarg. For a DataFrame this should be either 'any' or 'all' just like you would pass to dropna: # nth(0) is the same as g.first() In [202]: g.nth(0, dropna="any") Out[202]: B A 1 4.0 5 6.0 In [203]: g.first() Out[203]: B A 1 4.0 5 6.0 # nth(-1) is the same as g.last() In [204]: g.nth(-1, dropna="any") # NaNs denote group exhausted when using dropna Out[204]: B A 1 4.0 5 6.0 In [205]: g.last() Out[205]: B A 1 4.0 5 6.0 In [206]: g.B.nth(0, dropna="all") Out[206]: A 1 4.0 5 6.0 Name: B, dtype: float64 As with other methods, passing as_index=False, will achieve a filtration, which returns the grouped row. In [207]: df = pd.DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=["A", "B"]) In [208]: g = df.groupby("A", as_index=False) In [209]: g.nth(0) Out[209]: A B 0 1 NaN 2 5 6.0 In [210]: g.nth(-1) Out[210]: A B 1 1 4.0 2 5 6.0 You can also select multiple rows from each group by specifying multiple nth values as a list of ints. In [211]: business_dates = pd.date_range(start="4/1/2014", end="6/30/2014", freq="B") In [212]: df = pd.DataFrame(1, index=business_dates, columns=["a", "b"]) # get the first, 4th, and last date index for each month In [213]: df.groupby([df.index.year, df.index.month]).nth([0, 3, -1]) Out[213]: a b 2014 4 1 1 4 1 1 4 1 1 5 1 1 5 1 1 5 1 1 6 1 1 6 1 1 6 1 1 Enumerate group items# To see the order in which each row appears within its group, use the cumcount method: In [214]: dfg = pd.DataFrame(list("aaabba"), columns=["A"]) In [215]: dfg Out[215]: A 0 a 1 a 2 a 3 b 4 b 5 a In [216]: dfg.groupby("A").cumcount() Out[216]: 0 0 1 1 2 2 3 0 4 1 5 3 dtype: int64 In [217]: dfg.groupby("A").cumcount(ascending=False) Out[217]: 0 3 1 2 2 1 3 1 4 0 5 0 dtype: int64 Enumerate groups# To see the ordering of the groups (as opposed to the order of rows within a group given by cumcount) you can use ngroup(). Note that the numbers given to the groups match the order in which the groups would be seen when iterating over the groupby object, not the order they are first observed. In [218]: dfg = pd.DataFrame(list("aaabba"), columns=["A"]) In [219]: dfg Out[219]: A 0 a 1 a 2 a 3 b 4 b 5 a In [220]: dfg.groupby("A").ngroup() Out[220]: 0 0 1 0 2 0 3 1 4 1 5 0 dtype: int64 In [221]: dfg.groupby("A").ngroup(ascending=False) Out[221]: 0 1 1 1 2 1 3 0 4 0 5 1 dtype: int64 Plotting# Groupby also works with some plotting methods. For example, suppose we suspect that some features in a DataFrame may differ by group, in this case, the values in column 1 where the group is “B” are 3 higher on average. In [222]: np.random.seed(1234) In [223]: df = pd.DataFrame(np.random.randn(50, 2)) In [224]: df["g"] = np.random.choice(["A", "B"], size=50) In [225]: df.loc[df["g"] == "B", 1] += 3 We can easily visualize this with a boxplot: In [226]: df.groupby("g").boxplot() Out[226]: A AxesSubplot(0.1,0.15;0.363636x0.75) B AxesSubplot(0.536364,0.15;0.363636x0.75) dtype: object The result of calling boxplot is a dictionary whose keys are the values of our grouping column g (“A” and “B”). The values of the resulting dictionary can be controlled by the return_type keyword of boxplot. See the visualization documentation for more. Warning For historical reasons, df.groupby("g").boxplot() is not equivalent to df.boxplot(by="g"). See here for an explanation. Piping function calls# Similar to the functionality provided by DataFrame and Series, functions that take GroupBy objects can be chained together using a pipe method to allow for a cleaner, more readable syntax. To read about .pipe in general terms, see here. Combining .groupby and .pipe is often useful when you need to reuse GroupBy objects. As an example, imagine having a DataFrame with columns for stores, products, revenue and quantity sold. We’d like to do a groupwise calculation of prices (i.e. revenue/quantity) per store and per product. We could do this in a multi-step operation, but expressing it in terms of piping can make the code more readable. First we set the data: In [227]: n = 1000 In [228]: df = pd.DataFrame( .....: { .....: "Store": np.random.choice(["Store_1", "Store_2"], n), .....: "Product": np.random.choice(["Product_1", "Product_2"], n), .....: "Revenue": (np.random.random(n) * 50 + 10).round(2), .....: "Quantity": np.random.randint(1, 10, size=n), .....: } .....: ) .....: In [229]: df.head(2) Out[229]: Store Product Revenue Quantity 0 Store_2 Product_1 26.12 1 1 Store_2 Product_1 28.86 1 Now, to find prices per store/product, we can simply do: In [230]: ( .....: df.groupby(["Store", "Product"]) .....: .pipe(lambda grp: grp.Revenue.sum() / grp.Quantity.sum()) .....: .unstack() .....: .round(2) .....: ) .....: Out[230]: Product Product_1 Product_2 Store Store_1 6.82 7.05 Store_2 6.30 6.64 Piping can also be expressive when you want to deliver a grouped object to some arbitrary function, for example: In [231]: def mean(groupby): .....: return groupby.mean() .....: In [232]: df.groupby(["Store", "Product"]).pipe(mean) Out[232]: Revenue Quantity Store Product Store_1 Product_1 34.622727 5.075758 Product_2 35.482815 5.029630 Store_2 Product_1 32.972837 5.237589 Product_2 34.684360 5.224000 where mean takes a GroupBy object and finds the mean of the Revenue and Quantity columns respectively for each Store-Product combination. The mean function can be any function that takes in a GroupBy object; the .pipe will pass the GroupBy object as a parameter into the function you specify. Examples# Regrouping by factor# Regroup columns of a DataFrame according to their sum, and sum the aggregated ones. In [233]: df = pd.DataFrame({"a": [1, 0, 0], "b": [0, 1, 0], "c": [1, 0, 0], "d": [2, 3, 4]}) In [234]: df Out[234]: a b c d 0 1 0 1 2 1 0 1 0 3 2 0 0 0 4 In [235]: df.groupby(df.sum(), axis=1).sum() Out[235]: 1 9 0 2 2 1 1 3 2 0 4 Multi-column factorization# By using ngroup(), we can extract information about the groups in a way similar to factorize() (as described further in the reshaping API) but which applies naturally to multiple columns of mixed type and different sources. This can be useful as an intermediate categorical-like step in processing, when the relationships between the group rows are more important than their content, or as input to an algorithm which only accepts the integer encoding. (For more information about support in pandas for full categorical data, see the Categorical introduction and the API documentation.) In [236]: dfg = pd.DataFrame({"A": [1, 1, 2, 3, 2], "B": list("aaaba")}) In [237]: dfg Out[237]: A B 0 1 a 1 1 a 2 2 a 3 3 b 4 2 a In [238]: dfg.groupby(["A", "B"]).ngroup() Out[238]: 0 0 1 0 2 1 3 2 4 1 dtype: int64 In [239]: dfg.groupby(["A", [0, 0, 0, 1, 1]]).ngroup() Out[239]: 0 0 1 0 2 1 3 3 4 2 dtype: int64 Groupby by indexer to ‘resample’ data# Resampling produces new hypothetical samples (resamples) from already existing observed data or from a model that generates data. These new samples are similar to the pre-existing samples. In order to resample to work on indices that are non-datetimelike, the following procedure can be utilized. In the following examples, df.index // 5 returns a binary array which is used to determine what gets selected for the groupby operation. Note The below example shows how we can downsample by consolidation of samples into fewer samples. Here by using df.index // 5, we are aggregating the samples in bins. By applying std() function, we aggregate the information contained in many samples into a small subset of values which is their standard deviation thereby reducing the number of samples. In [240]: df = pd.DataFrame(np.random.randn(10, 2)) In [241]: df Out[241]: 0 1 0 -0.793893 0.321153 1 0.342250 1.618906 2 -0.975807 1.918201 3 -0.810847 -1.405919 4 -1.977759 0.461659 5 0.730057 -1.316938 6 -0.751328 0.528290 7 -0.257759 -1.081009 8 0.505895 -1.701948 9 -1.006349 0.020208 In [242]: df.index // 5 Out[242]: Int64Index([0, 0, 0, 0, 0, 1, 1, 1, 1, 1], dtype='int64') In [243]: df.groupby(df.index // 5).std() Out[243]: 0 1 0 0.823647 1.312912 1 0.760109 0.942941 Returning a Series to propagate names# Group DataFrame columns, compute a set of metrics and return a named Series. The Series name is used as the name for the column index. This is especially useful in conjunction with reshaping operations such as stacking in which the column index name will be used as the name of the inserted column: In [244]: df = pd.DataFrame( .....: { .....: "a": [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], .....: "b": [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1], .....: "c": [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], .....: "d": [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1], .....: } .....: ) .....: In [245]: def compute_metrics(x): .....: result = {"b_sum": x["b"].sum(), "c_mean": x["c"].mean()} .....: return pd.Series(result, name="metrics") .....: In [246]: result = df.groupby("a").apply(compute_metrics) In [247]: result Out[247]: metrics b_sum c_mean a 0 2.0 0.5 1 2.0 0.5 2 2.0 0.5 In [248]: result.stack() Out[248]: a metrics 0 b_sum 2.0 c_mean 0.5 1 b_sum 2.0 c_mean 0.5 2 b_sum 2.0 c_mean 0.5 dtype: float64
459
802
Pandas: How to split row of sentences with list values for each row on other column? I have a Pandas Dataframe like this: id data label 1336 'The PARTIES are willing to hold' [1, 11, label_1] 1336 'The PARTIES are willing to hold' [12, 15, label_2] I would like to split each data row thanks to informations in list of label column. For example, for the first row I would like to keep only the characters of data from first to 11 ('The PARTIES') and create a column 'label' corresponding to the last element of the list. Expected output: id data label 1336 'The PARTIES' label_1 1336 'are wi' label_2
You can apply a custom function on data column using label column information: extract_lbl = lambda x: (x['data'][x['label'][0]:x['label'][1]+1], x['label'][2]) df[['data', 'label']] = df.apply(extract_lbl, axis=1, result_type='expand') Output: >>> df id data label 0 1336 The PARTIES label_1 1 1336 are label_2
61,187,030
rank pandas dataframe by row instead of by column?
<p>Is it possible to rank a pandas dataframe by row instead of by column?</p> <p>Ranking should be highest to smallest.</p> <p>Example data:</p> <pre><code>Item Apple Orange Pear Watermelon Grape Weight 72 94 55 34 78 </code></pre> <p>I would like to place the rank within the dataframe such that my output is:</p> <pre><code>Item Apple Orange Pear Watermelon Grape Weight 72 94 55 34 78 Rank 3 1 4 5 2 </code></pre> <p>Many thanks</p>
61,187,074
"2020-04-13T11:36:16.503000"
1
null
1
34
pandas
<p>If only one row <code>DataFrame</code> use <a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.rank.html" rel="nofollow noreferrer"><code>DataFrame.rank</code></a> with <code>ascending=False</code> and <code>axis=1</code>:</p> <pre><code>df.loc['rank'] = df.rank(axis=1, ascending=False).astype(int).squeeze() </code></pre> <p>Or is possible select row and use <a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.rank.html" rel="nofollow noreferrer"><code>Series.rank</code></a>:</p> <pre><code>df.loc['Rank'] = df.loc['Weight'].rank(ascending=False).astype(int) </code></pre> <hr> <pre><code>print (df) Apple Orange Pear Watermelon Grape Item Weight 72 94 55 34 78 rank 3 1 4 5 2 </code></pre>
"2020-04-13T11:38:39.310000"
1
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rank.html
pandas.DataFrame.rank# pandas.DataFrame.rank# DataFrame.rank(axis=0, method='average', numeric_only=_NoDefault.no_default, na_option='keep', ascending=True, pct=False)[source]# If only one row DataFrame use DataFrame.rank with ascending=False and axis=1: df.loc['rank'] = df.rank(axis=1, ascending=False).astype(int).squeeze() Or is possible select row and use Series.rank: df.loc['Rank'] = df.loc['Weight'].rank(ascending=False).astype(int) print (df) Apple Orange Pear Watermelon Grape Item Weight 72 94 55 34 78 rank 3 1 4 5 2 Compute numerical data ranks (1 through n) along axis. By default, equal values are assigned a rank that is the average of the ranks of those values. Parameters axis{0 or ‘index’, 1 or ‘columns’}, default 0Index to direct ranking. For Series this parameter is unused and defaults to 0. method{‘average’, ‘min’, ‘max’, ‘first’, ‘dense’}, default ‘average’How to rank the group of records that have the same value (i.e. ties): average: average rank of the group min: lowest rank in the group max: highest rank in the group first: ranks assigned in order they appear in the array dense: like ‘min’, but rank always increases by 1 between groups. numeric_onlybool, optionalFor DataFrame objects, rank only numeric columns if set to True. na_option{‘keep’, ‘top’, ‘bottom’}, default ‘keep’How to rank NaN values: keep: assign NaN rank to NaN values top: assign lowest rank to NaN values bottom: assign highest rank to NaN values ascendingbool, default TrueWhether or not the elements should be ranked in ascending order. pctbool, default FalseWhether or not to display the returned rankings in percentile form. Returns same type as callerReturn a Series or DataFrame with data ranks as values. See also core.groupby.GroupBy.rankRank of values within each group. Examples >>> df = pd.DataFrame(data={'Animal': ['cat', 'penguin', 'dog', ... 'spider', 'snake'], ... 'Number_legs': [4, 2, 4, 8, np.nan]}) >>> df Animal Number_legs 0 cat 4.0 1 penguin 2.0 2 dog 4.0 3 spider 8.0 4 snake NaN Ties are assigned the mean of the ranks (by default) for the group. >>> s = pd.Series(range(5), index=list("abcde")) >>> s["d"] = s["b"] >>> s.rank() a 1.0 b 2.5 c 4.0 d 2.5 e 5.0 dtype: float64 The following example shows how the method behaves with the above parameters: default_rank: this is the default behaviour obtained without using any parameter. max_rank: setting method = 'max' the records that have the same values are ranked using the highest rank (e.g.: since ‘cat’ and ‘dog’ are both in the 2nd and 3rd position, rank 3 is assigned.) NA_bottom: choosing na_option = 'bottom', if there are records with NaN values they are placed at the bottom of the ranking. pct_rank: when setting pct = True, the ranking is expressed as percentile rank. >>> df['default_rank'] = df['Number_legs'].rank() >>> df['max_rank'] = df['Number_legs'].rank(method='max') >>> df['NA_bottom'] = df['Number_legs'].rank(na_option='bottom') >>> df['pct_rank'] = df['Number_legs'].rank(pct=True) >>> df Animal Number_legs default_rank max_rank NA_bottom pct_rank 0 cat 4.0 2.5 3.0 2.5 0.625 1 penguin 2.0 1.0 1.0 1.0 0.250 2 dog 4.0 2.5 3.0 2.5 0.625 3 spider 8.0 4.0 4.0 4.0 1.000 4 snake NaN NaN NaN 5.0 NaN
181
648
rank pandas dataframe by row instead of by column? Is it possible to rank a pandas dataframe by row instead of by column? Ranking should be highest to smallest. Example data: Item Apple Orange Pear Watermelon Grape Weight 72 94 55 34 78 I would like to place the rank within the dataframe such that my output is: Item Apple Orange Pear Watermelon Grape Weight 72 94 55 34 78 Rank 3 1 4 5 2 Many thanks
If only one row DataFrame use DataFrame.rank with ascending=False and axis=1: df.loc['rank'] = df.rank(axis=1, ascending=False).astype(int).squeeze() Or is possible select row and use Series.rank: df.loc['Rank'] = df.loc['Weight'].rank(ascending=False).astype(int) print (df) Apple Orange Pear Watermelon Grape Item Weight 72 94 55 34 78 rank 3 1 4 5 2
65,071,426
Unable to Read a tsv file in pandas. Gives UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa5 in position 113: invalid start byte
<p>I am trying to read a tsv file (one of many) but it is given me the following error.</p> <p><code>UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa5 in position 113: invalid start byte</code></p> <p>Before this I have read similar other files using the same code.</p> <p><code>df = pd.read_csv(fname,sep='\t',error_bad_lines=False)</code></p> <p>But for this particular, I am getting an error.</p> <p>How can I read this file? Hopefully without missing any data.</p>
65,072,828
"2020-11-30T10:00:09.870000"
1
1
0
294
python|pandas
<p>A suggestion would be to check waht encoding you actually have. Do it this way:</p> <pre><code>with open('filename.tsv) as f: ### or whatever your etension is print(f) </code></pre> <p>from that you'll obtain the encoding. Then,</p> <p>df=pd.read_csv('filename.tsv', encoding=&quot;the encoding that was returned&quot;)</p> <p>It would be nice if you could post lien 113 of you dataset, where the first occurrence of the error occured.</p>
"2020-11-30T11:36:40.837000"
1
https://pandas.pydata.org/docs/user_guide/io.html
IO tools (text, CSV, HDF5, …)# IO tools (text, CSV, HDF5, …)# The pandas I/O API is a set of top level reader functions accessed like pandas.read_csv() that generally return a pandas object. The corresponding writer functions are object methods that are accessed like DataFrame.to_csv(). Below is a table containing available readers and A suggestion would be to check waht encoding you actually have. Do it this way: with open('filename.tsv) as f: ### or whatever your etension is print(f) from that you'll obtain the encoding. Then, df=pd.read_csv('filename.tsv', encoding="the encoding that was returned") It would be nice if you could post lien 113 of you dataset, where the first occurrence of the error occured. writers. Format Type Data Description Reader Writer text CSV read_csv to_csv text Fixed-Width Text File read_fwf text JSON read_json to_json text HTML read_html to_html text LaTeX Styler.to_latex text XML read_xml to_xml text Local clipboard read_clipboard to_clipboard binary MS Excel read_excel to_excel binary OpenDocument read_excel binary HDF5 Format read_hdf to_hdf binary Feather Format read_feather to_feather binary Parquet Format read_parquet to_parquet binary ORC Format read_orc to_orc binary Stata read_stata to_stata binary SAS read_sas binary SPSS read_spss binary Python Pickle Format read_pickle to_pickle SQL SQL read_sql to_sql SQL Google BigQuery read_gbq to_gbq Here is an informal performance comparison for some of these IO methods. Note For examples that use the StringIO class, make sure you import it with from io import StringIO for Python 3. CSV & text files# The workhorse function for reading text files (a.k.a. flat files) is read_csv(). See the cookbook for some advanced strategies. Parsing options# read_csv() accepts the following common arguments: Basic# filepath_or_buffervariousEither a path to a file (a str, pathlib.Path, or py:py._path.local.LocalPath), URL (including http, ftp, and S3 locations), or any object with a read() method (such as an open file or StringIO). sepstr, defaults to ',' for read_csv(), \t for read_table()Delimiter to use. If sep is None, the C engine cannot automatically detect the separator, but the Python parsing engine can, meaning the latter will be used and automatically detect the separator by Python’s builtin sniffer tool, csv.Sniffer. In addition, separators longer than 1 character and different from '\s+' will be interpreted as regular expressions and will also force the use of the Python parsing engine. Note that regex delimiters are prone to ignoring quoted data. Regex example: '\\r\\t'. delimiterstr, default NoneAlternative argument name for sep. delim_whitespaceboolean, default FalseSpecifies whether or not whitespace (e.g. ' ' or '\t') will be used as the delimiter. Equivalent to setting sep='\s+'. If this option is set to True, nothing should be passed in for the delimiter parameter. Column and index locations and names# headerint or list of ints, default 'infer'Row number(s) to use as the column names, and the start of the data. Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first line of the file, if column names are passed explicitly then the behavior is identical to header=None. Explicitly pass header=0 to be able to replace existing names. The header can be a list of ints that specify row locations for a MultiIndex on the columns e.g. [0,1,3]. Intervening rows that are not specified will be skipped (e.g. 2 in this example is skipped). Note that this parameter ignores commented lines and empty lines if skip_blank_lines=True, so header=0 denotes the first line of data rather than the first line of the file. namesarray-like, default NoneList of column names to use. If file contains no header row, then you should explicitly pass header=None. Duplicates in this list are not allowed. index_colint, str, sequence of int / str, or False, optional, default NoneColumn(s) to use as the row labels of the DataFrame, either given as string name or column index. If a sequence of int / str is given, a MultiIndex is used. Note index_col=False can be used to force pandas to not use the first column as the index, e.g. when you have a malformed file with delimiters at the end of each line. The default value of None instructs pandas to guess. If the number of fields in the column header row is equal to the number of fields in the body of the data file, then a default index is used. If it is larger, then the first columns are used as index so that the remaining number of fields in the body are equal to the number of fields in the header. The first row after the header is used to determine the number of columns, which will go into the index. If the subsequent rows contain less columns than the first row, they are filled with NaN. This can be avoided through usecols. This ensures that the columns are taken as is and the trailing data are ignored. usecolslist-like or callable, default NoneReturn a subset of the columns. If list-like, all elements must either be positional (i.e. integer indices into the document columns) or strings that correspond to column names provided either by the user in names or inferred from the document header row(s). If names are given, the document header row(s) are not taken into account. For example, a valid list-like usecols parameter would be [0, 1, 2] or ['foo', 'bar', 'baz']. Element order is ignored, so usecols=[0, 1] is the same as [1, 0]. To instantiate a DataFrame from data with element order preserved use pd.read_csv(data, usecols=['foo', 'bar'])[['foo', 'bar']] for columns in ['foo', 'bar'] order or pd.read_csv(data, usecols=['foo', 'bar'])[['bar', 'foo']] for ['bar', 'foo'] order. If callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True: In [1]: import pandas as pd In [2]: from io import StringIO In [3]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [4]: pd.read_csv(StringIO(data)) Out[4]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [5]: pd.read_csv(StringIO(data), usecols=lambda x: x.upper() in ["COL1", "COL3"]) Out[5]: col1 col3 0 a 1 1 a 2 2 c 3 Using this parameter results in much faster parsing time and lower memory usage when using the c engine. The Python engine loads the data first before deciding which columns to drop. squeezeboolean, default FalseIf the parsed data only contains one column then return a Series. Deprecated since version 1.4.0: Append .squeeze("columns") to the call to {func_name} to squeeze the data. prefixstr, default NonePrefix to add to column numbers when no header, e.g. ‘X’ for X0, X1, … Deprecated since version 1.4.0: Use a list comprehension on the DataFrame’s columns after calling read_csv. In [6]: data = "col1,col2,col3\na,b,1" In [7]: df = pd.read_csv(StringIO(data)) In [8]: df.columns = [f"pre_{col}" for col in df.columns] In [9]: df Out[9]: pre_col1 pre_col2 pre_col3 0 a b 1 mangle_dupe_colsboolean, default TrueDuplicate columns will be specified as ‘X’, ‘X.1’…’X.N’, rather than ‘X’…’X’. Passing in False will cause data to be overwritten if there are duplicate names in the columns. Deprecated since version 1.5.0: The argument was never implemented, and a new argument where the renaming pattern can be specified will be added instead. General parsing configuration# dtypeType name or dict of column -> type, default NoneData type for data or columns. E.g. {'a': np.float64, 'b': np.int32, 'c': 'Int64'} Use str or object together with suitable na_values settings to preserve and not interpret dtype. If converters are specified, they will be applied INSTEAD of dtype conversion. New in version 1.5.0: Support for defaultdict was added. Specify a defaultdict as input where the default determines the dtype of the columns which are not explicitly listed. engine{'c', 'python', 'pyarrow'}Parser engine to use. The C and pyarrow engines are faster, while the python engine is currently more feature-complete. Multithreading is currently only supported by the pyarrow engine. New in version 1.4.0: The “pyarrow” engine was added as an experimental engine, and some features are unsupported, or may not work correctly, with this engine. convertersdict, default NoneDict of functions for converting values in certain columns. Keys can either be integers or column labels. true_valueslist, default NoneValues to consider as True. false_valueslist, default NoneValues to consider as False. skipinitialspaceboolean, default FalseSkip spaces after delimiter. skiprowslist-like or integer, default NoneLine numbers to skip (0-indexed) or number of lines to skip (int) at the start of the file. If callable, the callable function will be evaluated against the row indices, returning True if the row should be skipped and False otherwise: In [10]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [11]: pd.read_csv(StringIO(data)) Out[11]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [12]: pd.read_csv(StringIO(data), skiprows=lambda x: x % 2 != 0) Out[12]: col1 col2 col3 0 a b 2 skipfooterint, default 0Number of lines at bottom of file to skip (unsupported with engine=’c’). nrowsint, default NoneNumber of rows of file to read. Useful for reading pieces of large files. low_memoryboolean, default TrueInternally process the file in chunks, resulting in lower memory use while parsing, but possibly mixed type inference. To ensure no mixed types either set False, or specify the type with the dtype parameter. Note that the entire file is read into a single DataFrame regardless, use the chunksize or iterator parameter to return the data in chunks. (Only valid with C parser) memory_mapboolean, default FalseIf a filepath is provided for filepath_or_buffer, map the file object directly onto memory and access the data directly from there. Using this option can improve performance because there is no longer any I/O overhead. NA and missing data handling# na_valuesscalar, str, list-like, or dict, default NoneAdditional strings to recognize as NA/NaN. If dict passed, specific per-column NA values. See na values const below for a list of the values interpreted as NaN by default. keep_default_naboolean, default TrueWhether or not to include the default NaN values when parsing the data. Depending on whether na_values is passed in, the behavior is as follows: If keep_default_na is True, and na_values are specified, na_values is appended to the default NaN values used for parsing. If keep_default_na is True, and na_values are not specified, only the default NaN values are used for parsing. If keep_default_na is False, and na_values are specified, only the NaN values specified na_values are used for parsing. If keep_default_na is False, and na_values are not specified, no strings will be parsed as NaN. Note that if na_filter is passed in as False, the keep_default_na and na_values parameters will be ignored. na_filterboolean, default TrueDetect missing value markers (empty strings and the value of na_values). In data without any NAs, passing na_filter=False can improve the performance of reading a large file. verboseboolean, default FalseIndicate number of NA values placed in non-numeric columns. skip_blank_linesboolean, default TrueIf True, skip over blank lines rather than interpreting as NaN values. Datetime handling# parse_datesboolean or list of ints or names or list of lists or dict, default False. If True -> try parsing the index. If [1, 2, 3] -> try parsing columns 1, 2, 3 each as a separate date column. If [[1, 3]] -> combine columns 1 and 3 and parse as a single date column. If {'foo': [1, 3]} -> parse columns 1, 3 as date and call result ‘foo’. Note A fast-path exists for iso8601-formatted dates. infer_datetime_formatboolean, default FalseIf True and parse_dates is enabled for a column, attempt to infer the datetime format to speed up the processing. keep_date_colboolean, default FalseIf True and parse_dates specifies combining multiple columns then keep the original columns. date_parserfunction, default NoneFunction to use for converting a sequence of string columns to an array of datetime instances. The default uses dateutil.parser.parser to do the conversion. pandas will try to call date_parser in three different ways, advancing to the next if an exception occurs: 1) Pass one or more arrays (as defined by parse_dates) as arguments; 2) concatenate (row-wise) the string values from the columns defined by parse_dates into a single array and pass that; and 3) call date_parser once for each row using one or more strings (corresponding to the columns defined by parse_dates) as arguments. dayfirstboolean, default FalseDD/MM format dates, international and European format. cache_datesboolean, default TrueIf True, use a cache of unique, converted dates to apply the datetime conversion. May produce significant speed-up when parsing duplicate date strings, especially ones with timezone offsets. New in version 0.25.0. Iteration# iteratorboolean, default FalseReturn TextFileReader object for iteration or getting chunks with get_chunk(). chunksizeint, default NoneReturn TextFileReader object for iteration. See iterating and chunking below. Quoting, compression, and file format# compression{'infer', 'gzip', 'bz2', 'zip', 'xz', 'zstd', None, dict}, default 'infer'For on-the-fly decompression of on-disk data. If ‘infer’, then use gzip, bz2, zip, xz, or zstandard if filepath_or_buffer is path-like ending in ‘.gz’, ‘.bz2’, ‘.zip’, ‘.xz’, ‘.zst’, respectively, and no decompression otherwise. If using ‘zip’, the ZIP file must contain only one data file to be read in. Set to None for no decompression. Can also be a dict with key 'method' set to one of {'zip', 'gzip', 'bz2', 'zstd'} and other key-value pairs are forwarded to zipfile.ZipFile, gzip.GzipFile, bz2.BZ2File, or zstandard.ZstdDecompressor. As an example, the following could be passed for faster compression and to create a reproducible gzip archive: compression={'method': 'gzip', 'compresslevel': 1, 'mtime': 1}. Changed in version 1.1.0: dict option extended to support gzip and bz2. Changed in version 1.2.0: Previous versions forwarded dict entries for ‘gzip’ to gzip.open. thousandsstr, default NoneThousands separator. decimalstr, default '.'Character to recognize as decimal point. E.g. use ',' for European data. float_precisionstring, default NoneSpecifies which converter the C engine should use for floating-point values. The options are None for the ordinary converter, high for the high-precision converter, and round_trip for the round-trip converter. lineterminatorstr (length 1), default NoneCharacter to break file into lines. Only valid with C parser. quotecharstr (length 1)The character used to denote the start and end of a quoted item. Quoted items can include the delimiter and it will be ignored. quotingint or csv.QUOTE_* instance, default 0Control field quoting behavior per csv.QUOTE_* constants. Use one of QUOTE_MINIMAL (0), QUOTE_ALL (1), QUOTE_NONNUMERIC (2) or QUOTE_NONE (3). doublequoteboolean, default TrueWhen quotechar is specified and quoting is not QUOTE_NONE, indicate whether or not to interpret two consecutive quotechar elements inside a field as a single quotechar element. escapecharstr (length 1), default NoneOne-character string used to escape delimiter when quoting is QUOTE_NONE. commentstr, default NoneIndicates remainder of line should not be parsed. If found at the beginning of a line, the line will be ignored altogether. This parameter must be a single character. Like empty lines (as long as skip_blank_lines=True), fully commented lines are ignored by the parameter header but not by skiprows. For example, if comment='#', parsing ‘#empty\na,b,c\n1,2,3’ with header=0 will result in ‘a,b,c’ being treated as the header. encodingstr, default NoneEncoding to use for UTF when reading/writing (e.g. 'utf-8'). List of Python standard encodings. dialectstr or csv.Dialect instance, default NoneIf provided, this parameter will override values (default or not) for the following parameters: delimiter, doublequote, escapechar, skipinitialspace, quotechar, and quoting. If it is necessary to override values, a ParserWarning will be issued. See csv.Dialect documentation for more details. Error handling# error_bad_linesboolean, optional, default NoneLines with too many fields (e.g. a csv line with too many commas) will by default cause an exception to be raised, and no DataFrame will be returned. If False, then these “bad lines” will dropped from the DataFrame that is returned. See bad lines below. Deprecated since version 1.3.0: The on_bad_lines parameter should be used instead to specify behavior upon encountering a bad line instead. warn_bad_linesboolean, optional, default NoneIf error_bad_lines is False, and warn_bad_lines is True, a warning for each “bad line” will be output. Deprecated since version 1.3.0: The on_bad_lines parameter should be used instead to specify behavior upon encountering a bad line instead. on_bad_lines(‘error’, ‘warn’, ‘skip’), default ‘error’Specifies what to do upon encountering a bad line (a line with too many fields). Allowed values are : ‘error’, raise an ParserError when a bad line is encountered. ‘warn’, print a warning when a bad line is encountered and skip that line. ‘skip’, skip bad lines without raising or warning when they are encountered. New in version 1.3.0. Specifying column data types# You can indicate the data type for the whole DataFrame or individual columns: In [13]: import numpy as np In [14]: data = "a,b,c,d\n1,2,3,4\n5,6,7,8\n9,10,11" In [15]: print(data) a,b,c,d 1,2,3,4 5,6,7,8 9,10,11 In [16]: df = pd.read_csv(StringIO(data), dtype=object) In [17]: df Out[17]: a b c d 0 1 2 3 4 1 5 6 7 8 2 9 10 11 NaN In [18]: df["a"][0] Out[18]: '1' In [19]: df = pd.read_csv(StringIO(data), dtype={"b": object, "c": np.float64, "d": "Int64"}) In [20]: df.dtypes Out[20]: a int64 b object c float64 d Int64 dtype: object Fortunately, pandas offers more than one way to ensure that your column(s) contain only one dtype. If you’re unfamiliar with these concepts, you can see here to learn more about dtypes, and here to learn more about object conversion in pandas. For instance, you can use the converters argument of read_csv(): In [21]: data = "col_1\n1\n2\n'A'\n4.22" In [22]: df = pd.read_csv(StringIO(data), converters={"col_1": str}) In [23]: df Out[23]: col_1 0 1 1 2 2 'A' 3 4.22 In [24]: df["col_1"].apply(type).value_counts() Out[24]: <class 'str'> 4 Name: col_1, dtype: int64 Or you can use the to_numeric() function to coerce the dtypes after reading in the data, In [25]: df2 = pd.read_csv(StringIO(data)) In [26]: df2["col_1"] = pd.to_numeric(df2["col_1"], errors="coerce") In [27]: df2 Out[27]: col_1 0 1.00 1 2.00 2 NaN 3 4.22 In [28]: df2["col_1"].apply(type).value_counts() Out[28]: <class 'float'> 4 Name: col_1, dtype: int64 which will convert all valid parsing to floats, leaving the invalid parsing as NaN. Ultimately, how you deal with reading in columns containing mixed dtypes depends on your specific needs. In the case above, if you wanted to NaN out the data anomalies, then to_numeric() is probably your best option. However, if you wanted for all the data to be coerced, no matter the type, then using the converters argument of read_csv() would certainly be worth trying. Note In some cases, reading in abnormal data with columns containing mixed dtypes will result in an inconsistent dataset. If you rely on pandas to infer the dtypes of your columns, the parsing engine will go and infer the dtypes for different chunks of the data, rather than the whole dataset at once. Consequently, you can end up with column(s) with mixed dtypes. For example, In [29]: col_1 = list(range(500000)) + ["a", "b"] + list(range(500000)) In [30]: df = pd.DataFrame({"col_1": col_1}) In [31]: df.to_csv("foo.csv") In [32]: mixed_df = pd.read_csv("foo.csv") In [33]: mixed_df["col_1"].apply(type).value_counts() Out[33]: <class 'int'> 737858 <class 'str'> 262144 Name: col_1, dtype: int64 In [34]: mixed_df["col_1"].dtype Out[34]: dtype('O') will result with mixed_df containing an int dtype for certain chunks of the column, and str for others due to the mixed dtypes from the data that was read in. It is important to note that the overall column will be marked with a dtype of object, which is used for columns with mixed dtypes. Specifying categorical dtype# Categorical columns can be parsed directly by specifying dtype='category' or dtype=CategoricalDtype(categories, ordered). In [35]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [36]: pd.read_csv(StringIO(data)) Out[36]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [37]: pd.read_csv(StringIO(data)).dtypes Out[37]: col1 object col2 object col3 int64 dtype: object In [38]: pd.read_csv(StringIO(data), dtype="category").dtypes Out[38]: col1 category col2 category col3 category dtype: object Individual columns can be parsed as a Categorical using a dict specification: In [39]: pd.read_csv(StringIO(data), dtype={"col1": "category"}).dtypes Out[39]: col1 category col2 object col3 int64 dtype: object Specifying dtype='category' will result in an unordered Categorical whose categories are the unique values observed in the data. For more control on the categories and order, create a CategoricalDtype ahead of time, and pass that for that column’s dtype. In [40]: from pandas.api.types import CategoricalDtype In [41]: dtype = CategoricalDtype(["d", "c", "b", "a"], ordered=True) In [42]: pd.read_csv(StringIO(data), dtype={"col1": dtype}).dtypes Out[42]: col1 category col2 object col3 int64 dtype: object When using dtype=CategoricalDtype, “unexpected” values outside of dtype.categories are treated as missing values. In [43]: dtype = CategoricalDtype(["a", "b", "d"]) # No 'c' In [44]: pd.read_csv(StringIO(data), dtype={"col1": dtype}).col1 Out[44]: 0 a 1 a 2 NaN Name: col1, dtype: category Categories (3, object): ['a', 'b', 'd'] This matches the behavior of Categorical.set_categories(). Note With dtype='category', the resulting categories will always be parsed as strings (object dtype). If the categories are numeric they can be converted using the to_numeric() function, or as appropriate, another converter such as to_datetime(). When dtype is a CategoricalDtype with homogeneous categories ( all numeric, all datetimes, etc.), the conversion is done automatically. In [45]: df = pd.read_csv(StringIO(data), dtype="category") In [46]: df.dtypes Out[46]: col1 category col2 category col3 category dtype: object In [47]: df["col3"] Out[47]: 0 1 1 2 2 3 Name: col3, dtype: category Categories (3, object): ['1', '2', '3'] In [48]: new_categories = pd.to_numeric(df["col3"].cat.categories) In [49]: df["col3"] = df["col3"].cat.rename_categories(new_categories) In [50]: df["col3"] Out[50]: 0 1 1 2 2 3 Name: col3, dtype: category Categories (3, int64): [1, 2, 3] Naming and using columns# Handling column names# A file may or may not have a header row. pandas assumes the first row should be used as the column names: In [51]: data = "a,b,c\n1,2,3\n4,5,6\n7,8,9" In [52]: print(data) a,b,c 1,2,3 4,5,6 7,8,9 In [53]: pd.read_csv(StringIO(data)) Out[53]: a b c 0 1 2 3 1 4 5 6 2 7 8 9 By specifying the names argument in conjunction with header you can indicate other names to use and whether or not to throw away the header row (if any): In [54]: print(data) a,b,c 1,2,3 4,5,6 7,8,9 In [55]: pd.read_csv(StringIO(data), names=["foo", "bar", "baz"], header=0) Out[55]: foo bar baz 0 1 2 3 1 4 5 6 2 7 8 9 In [56]: pd.read_csv(StringIO(data), names=["foo", "bar", "baz"], header=None) Out[56]: foo bar baz 0 a b c 1 1 2 3 2 4 5 6 3 7 8 9 If the header is in a row other than the first, pass the row number to header. This will skip the preceding rows: In [57]: data = "skip this skip it\na,b,c\n1,2,3\n4,5,6\n7,8,9" In [58]: pd.read_csv(StringIO(data), header=1) Out[58]: a b c 0 1 2 3 1 4 5 6 2 7 8 9 Note Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first non-blank line of the file, if column names are passed explicitly then the behavior is identical to header=None. Duplicate names parsing# Deprecated since version 1.5.0: mangle_dupe_cols was never implemented, and a new argument where the renaming pattern can be specified will be added instead. If the file or header contains duplicate names, pandas will by default distinguish between them so as to prevent overwriting data: In [59]: data = "a,b,a\n0,1,2\n3,4,5" In [60]: pd.read_csv(StringIO(data)) Out[60]: a b a.1 0 0 1 2 1 3 4 5 There is no more duplicate data because mangle_dupe_cols=True by default, which modifies a series of duplicate columns ‘X’, …, ‘X’ to become ‘X’, ‘X.1’, …, ‘X.N’. Filtering columns (usecols)# The usecols argument allows you to select any subset of the columns in a file, either using the column names, position numbers or a callable: In [61]: data = "a,b,c,d\n1,2,3,foo\n4,5,6,bar\n7,8,9,baz" In [62]: pd.read_csv(StringIO(data)) Out[62]: a b c d 0 1 2 3 foo 1 4 5 6 bar 2 7 8 9 baz In [63]: pd.read_csv(StringIO(data), usecols=["b", "d"]) Out[63]: b d 0 2 foo 1 5 bar 2 8 baz In [64]: pd.read_csv(StringIO(data), usecols=[0, 2, 3]) Out[64]: a c d 0 1 3 foo 1 4 6 bar 2 7 9 baz In [65]: pd.read_csv(StringIO(data), usecols=lambda x: x.upper() in ["A", "C"]) Out[65]: a c 0 1 3 1 4 6 2 7 9 The usecols argument can also be used to specify which columns not to use in the final result: In [66]: pd.read_csv(StringIO(data), usecols=lambda x: x not in ["a", "c"]) Out[66]: b d 0 2 foo 1 5 bar 2 8 baz In this case, the callable is specifying that we exclude the “a” and “c” columns from the output. Comments and empty lines# Ignoring line comments and empty lines# If the comment parameter is specified, then completely commented lines will be ignored. By default, completely blank lines will be ignored as well. In [67]: data = "\na,b,c\n \n# commented line\n1,2,3\n\n4,5,6" In [68]: print(data) a,b,c # commented line 1,2,3 4,5,6 In [69]: pd.read_csv(StringIO(data), comment="#") Out[69]: a b c 0 1 2 3 1 4 5 6 If skip_blank_lines=False, then read_csv will not ignore blank lines: In [70]: data = "a,b,c\n\n1,2,3\n\n\n4,5,6" In [71]: pd.read_csv(StringIO(data), skip_blank_lines=False) Out[71]: a b c 0 NaN NaN NaN 1 1.0 2.0 3.0 2 NaN NaN NaN 3 NaN NaN NaN 4 4.0 5.0 6.0 Warning The presence of ignored lines might create ambiguities involving line numbers; the parameter header uses row numbers (ignoring commented/empty lines), while skiprows uses line numbers (including commented/empty lines): In [72]: data = "#comment\na,b,c\nA,B,C\n1,2,3" In [73]: pd.read_csv(StringIO(data), comment="#", header=1) Out[73]: A B C 0 1 2 3 In [74]: data = "A,B,C\n#comment\na,b,c\n1,2,3" In [75]: pd.read_csv(StringIO(data), comment="#", skiprows=2) Out[75]: a b c 0 1 2 3 If both header and skiprows are specified, header will be relative to the end of skiprows. For example: In [76]: data = ( ....: "# empty\n" ....: "# second empty line\n" ....: "# third emptyline\n" ....: "X,Y,Z\n" ....: "1,2,3\n" ....: "A,B,C\n" ....: "1,2.,4.\n" ....: "5.,NaN,10.0\n" ....: ) ....: In [77]: print(data) # empty # second empty line # third emptyline X,Y,Z 1,2,3 A,B,C 1,2.,4. 5.,NaN,10.0 In [78]: pd.read_csv(StringIO(data), comment="#", skiprows=4, header=1) Out[78]: A B C 0 1.0 2.0 4.0 1 5.0 NaN 10.0 Comments# Sometimes comments or meta data may be included in a file: In [79]: print(open("tmp.csv").read()) ID,level,category Patient1,123000,x # really unpleasant Patient2,23000,y # wouldn't take his medicine Patient3,1234018,z # awesome By default, the parser includes the comments in the output: In [80]: df = pd.read_csv("tmp.csv") In [81]: df Out[81]: ID level category 0 Patient1 123000 x # really unpleasant 1 Patient2 23000 y # wouldn't take his medicine 2 Patient3 1234018 z # awesome We can suppress the comments using the comment keyword: In [82]: df = pd.read_csv("tmp.csv", comment="#") In [83]: df Out[83]: ID level category 0 Patient1 123000 x 1 Patient2 23000 y 2 Patient3 1234018 z Dealing with Unicode data# The encoding argument should be used for encoded unicode data, which will result in byte strings being decoded to unicode in the result: In [84]: from io import BytesIO In [85]: data = b"word,length\n" b"Tr\xc3\xa4umen,7\n" b"Gr\xc3\xbc\xc3\x9fe,5" In [86]: data = data.decode("utf8").encode("latin-1") In [87]: df = pd.read_csv(BytesIO(data), encoding="latin-1") In [88]: df Out[88]: word length 0 Träumen 7 1 Grüße 5 In [89]: df["word"][1] Out[89]: 'Grüße' Some formats which encode all characters as multiple bytes, like UTF-16, won’t parse correctly at all without specifying the encoding. Full list of Python standard encodings. Index columns and trailing delimiters# If a file has one more column of data than the number of column names, the first column will be used as the DataFrame’s row names: In [90]: data = "a,b,c\n4,apple,bat,5.7\n8,orange,cow,10" In [91]: pd.read_csv(StringIO(data)) Out[91]: a b c 4 apple bat 5.7 8 orange cow 10.0 In [92]: data = "index,a,b,c\n4,apple,bat,5.7\n8,orange,cow,10" In [93]: pd.read_csv(StringIO(data), index_col=0) Out[93]: a b c index 4 apple bat 5.7 8 orange cow 10.0 Ordinarily, you can achieve this behavior using the index_col option. There are some exception cases when a file has been prepared with delimiters at the end of each data line, confusing the parser. To explicitly disable the index column inference and discard the last column, pass index_col=False: In [94]: data = "a,b,c\n4,apple,bat,\n8,orange,cow," In [95]: print(data) a,b,c 4,apple,bat, 8,orange,cow, In [96]: pd.read_csv(StringIO(data)) Out[96]: a b c 4 apple bat NaN 8 orange cow NaN In [97]: pd.read_csv(StringIO(data), index_col=False) Out[97]: a b c 0 4 apple bat 1 8 orange cow If a subset of data is being parsed using the usecols option, the index_col specification is based on that subset, not the original data. In [98]: data = "a,b,c\n4,apple,bat,\n8,orange,cow," In [99]: print(data) a,b,c 4,apple,bat, 8,orange,cow, In [100]: pd.read_csv(StringIO(data), usecols=["b", "c"]) Out[100]: b c 4 bat NaN 8 cow NaN In [101]: pd.read_csv(StringIO(data), usecols=["b", "c"], index_col=0) Out[101]: b c 4 bat NaN 8 cow NaN Date Handling# Specifying date columns# To better facilitate working with datetime data, read_csv() uses the keyword arguments parse_dates and date_parser to allow users to specify a variety of columns and date/time formats to turn the input text data into datetime objects. The simplest case is to just pass in parse_dates=True: In [102]: with open("foo.csv", mode="w") as f: .....: f.write("date,A,B,C\n20090101,a,1,2\n20090102,b,3,4\n20090103,c,4,5") .....: # Use a column as an index, and parse it as dates. In [103]: df = pd.read_csv("foo.csv", index_col=0, parse_dates=True) In [104]: df Out[104]: A B C date 2009-01-01 a 1 2 2009-01-02 b 3 4 2009-01-03 c 4 5 # These are Python datetime objects In [105]: df.index Out[105]: DatetimeIndex(['2009-01-01', '2009-01-02', '2009-01-03'], dtype='datetime64[ns]', name='date', freq=None) It is often the case that we may want to store date and time data separately, or store various date fields separately. the parse_dates keyword can be used to specify a combination of columns to parse the dates and/or times from. You can specify a list of column lists to parse_dates, the resulting date columns will be prepended to the output (so as to not affect the existing column order) and the new column names will be the concatenation of the component column names: In [106]: data = ( .....: "KORD,19990127, 19:00:00, 18:56:00, 0.8100\n" .....: "KORD,19990127, 20:00:00, 19:56:00, 0.0100\n" .....: "KORD,19990127, 21:00:00, 20:56:00, -0.5900\n" .....: "KORD,19990127, 21:00:00, 21:18:00, -0.9900\n" .....: "KORD,19990127, 22:00:00, 21:56:00, -0.5900\n" .....: "KORD,19990127, 23:00:00, 22:56:00, -0.5900" .....: ) .....: In [107]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [108]: df = pd.read_csv("tmp.csv", header=None, parse_dates=[[1, 2], [1, 3]]) In [109]: df Out[109]: 1_2 1_3 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 By default the parser removes the component date columns, but you can choose to retain them via the keep_date_col keyword: In [110]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=[[1, 2], [1, 3]], keep_date_col=True .....: ) .....: In [111]: df Out[111]: 1_2 1_3 0 ... 2 3 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD ... 19:00:00 18:56:00 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD ... 20:00:00 19:56:00 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD ... 21:00:00 20:56:00 -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD ... 21:00:00 21:18:00 -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD ... 22:00:00 21:56:00 -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD ... 23:00:00 22:56:00 -0.59 [6 rows x 7 columns] Note that if you wish to combine multiple columns into a single date column, a nested list must be used. In other words, parse_dates=[1, 2] indicates that the second and third columns should each be parsed as separate date columns while parse_dates=[[1, 2]] means the two columns should be parsed into a single column. You can also use a dict to specify custom name columns: In [112]: date_spec = {"nominal": [1, 2], "actual": [1, 3]} In [113]: df = pd.read_csv("tmp.csv", header=None, parse_dates=date_spec) In [114]: df Out[114]: nominal actual 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 It is important to remember that if multiple text columns are to be parsed into a single date column, then a new column is prepended to the data. The index_col specification is based off of this new set of columns rather than the original data columns: In [115]: date_spec = {"nominal": [1, 2], "actual": [1, 3]} In [116]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=date_spec, index_col=0 .....: ) # index is the nominal column .....: In [117]: df Out[117]: actual 0 4 nominal 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 Note If a column or index contains an unparsable date, the entire column or index will be returned unaltered as an object data type. For non-standard datetime parsing, use to_datetime() after pd.read_csv. Note read_csv has a fast_path for parsing datetime strings in iso8601 format, e.g “2000-01-01T00:01:02+00:00” and similar variations. If you can arrange for your data to store datetimes in this format, load times will be significantly faster, ~20x has been observed. Date parsing functions# Finally, the parser allows you to specify a custom date_parser function to take full advantage of the flexibility of the date parsing API: In [118]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=date_spec, date_parser=pd.to_datetime .....: ) .....: In [119]: df Out[119]: nominal actual 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 pandas will try to call the date_parser function in three different ways. If an exception is raised, the next one is tried: date_parser is first called with one or more arrays as arguments, as defined using parse_dates (e.g., date_parser(['2013', '2013'], ['1', '2'])). If #1 fails, date_parser is called with all the columns concatenated row-wise into a single array (e.g., date_parser(['2013 1', '2013 2'])). Note that performance-wise, you should try these methods of parsing dates in order: Try to infer the format using infer_datetime_format=True (see section below). If you know the format, use pd.to_datetime(): date_parser=lambda x: pd.to_datetime(x, format=...). If you have a really non-standard format, use a custom date_parser function. For optimal performance, this should be vectorized, i.e., it should accept arrays as arguments. Parsing a CSV with mixed timezones# pandas cannot natively represent a column or index with mixed timezones. If your CSV file contains columns with a mixture of timezones, the default result will be an object-dtype column with strings, even with parse_dates. In [120]: content = """\ .....: a .....: 2000-01-01T00:00:00+05:00 .....: 2000-01-01T00:00:00+06:00""" .....: In [121]: df = pd.read_csv(StringIO(content), parse_dates=["a"]) In [122]: df["a"] Out[122]: 0 2000-01-01 00:00:00+05:00 1 2000-01-01 00:00:00+06:00 Name: a, dtype: object To parse the mixed-timezone values as a datetime column, pass a partially-applied to_datetime() with utc=True as the date_parser. In [123]: df = pd.read_csv( .....: StringIO(content), .....: parse_dates=["a"], .....: date_parser=lambda col: pd.to_datetime(col, utc=True), .....: ) .....: In [124]: df["a"] Out[124]: 0 1999-12-31 19:00:00+00:00 1 1999-12-31 18:00:00+00:00 Name: a, dtype: datetime64[ns, UTC] Inferring datetime format# If you have parse_dates enabled for some or all of your columns, and your datetime strings are all formatted the same way, you may get a large speed up by setting infer_datetime_format=True. If set, pandas will attempt to guess the format of your datetime strings, and then use a faster means of parsing the strings. 5-10x parsing speeds have been observed. pandas will fallback to the usual parsing if either the format cannot be guessed or the format that was guessed cannot properly parse the entire column of strings. So in general, infer_datetime_format should not have any negative consequences if enabled. Here are some examples of datetime strings that can be guessed (All representing December 30th, 2011 at 00:00:00): “20111230” “2011/12/30” “20111230 00:00:00” “12/30/2011 00:00:00” “30/Dec/2011 00:00:00” “30/December/2011 00:00:00” Note that infer_datetime_format is sensitive to dayfirst. With dayfirst=True, it will guess “01/12/2011” to be December 1st. With dayfirst=False (default) it will guess “01/12/2011” to be January 12th. # Try to infer the format for the index column In [125]: df = pd.read_csv( .....: "foo.csv", .....: index_col=0, .....: parse_dates=True, .....: infer_datetime_format=True, .....: ) .....: In [126]: df Out[126]: A B C date 2009-01-01 a 1 2 2009-01-02 b 3 4 2009-01-03 c 4 5 International date formats# While US date formats tend to be MM/DD/YYYY, many international formats use DD/MM/YYYY instead. For convenience, a dayfirst keyword is provided: In [127]: data = "date,value,cat\n1/6/2000,5,a\n2/6/2000,10,b\n3/6/2000,15,c" In [128]: print(data) date,value,cat 1/6/2000,5,a 2/6/2000,10,b 3/6/2000,15,c In [129]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [130]: pd.read_csv("tmp.csv", parse_dates=[0]) Out[130]: date value cat 0 2000-01-06 5 a 1 2000-02-06 10 b 2 2000-03-06 15 c In [131]: pd.read_csv("tmp.csv", dayfirst=True, parse_dates=[0]) Out[131]: date value cat 0 2000-06-01 5 a 1 2000-06-02 10 b 2 2000-06-03 15 c Writing CSVs to binary file objects# New in version 1.2.0. df.to_csv(..., mode="wb") allows writing a CSV to a file object opened binary mode. In most cases, it is not necessary to specify mode as Pandas will auto-detect whether the file object is opened in text or binary mode. In [132]: import io In [133]: data = pd.DataFrame([0, 1, 2]) In [134]: buffer = io.BytesIO() In [135]: data.to_csv(buffer, encoding="utf-8", compression="gzip") Specifying method for floating-point conversion# The parameter float_precision can be specified in order to use a specific floating-point converter during parsing with the C engine. The options are the ordinary converter, the high-precision converter, and the round-trip converter (which is guaranteed to round-trip values after writing to a file). For example: In [136]: val = "0.3066101993807095471566981359501369297504425048828125" In [137]: data = "a,b,c\n1,2,{0}".format(val) In [138]: abs( .....: pd.read_csv( .....: StringIO(data), .....: engine="c", .....: float_precision=None, .....: )["c"][0] - float(val) .....: ) .....: Out[138]: 5.551115123125783e-17 In [139]: abs( .....: pd.read_csv( .....: StringIO(data), .....: engine="c", .....: float_precision="high", .....: )["c"][0] - float(val) .....: ) .....: Out[139]: 5.551115123125783e-17 In [140]: abs( .....: pd.read_csv(StringIO(data), engine="c", float_precision="round_trip")["c"][0] .....: - float(val) .....: ) .....: Out[140]: 0.0 Thousand separators# For large numbers that have been written with a thousands separator, you can set the thousands keyword to a string of length 1 so that integers will be parsed correctly: By default, numbers with a thousands separator will be parsed as strings: In [141]: data = ( .....: "ID|level|category\n" .....: "Patient1|123,000|x\n" .....: "Patient2|23,000|y\n" .....: "Patient3|1,234,018|z" .....: ) .....: In [142]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [143]: df = pd.read_csv("tmp.csv", sep="|") In [144]: df Out[144]: ID level category 0 Patient1 123,000 x 1 Patient2 23,000 y 2 Patient3 1,234,018 z In [145]: df.level.dtype Out[145]: dtype('O') The thousands keyword allows integers to be parsed correctly: In [146]: df = pd.read_csv("tmp.csv", sep="|", thousands=",") In [147]: df Out[147]: ID level category 0 Patient1 123000 x 1 Patient2 23000 y 2 Patient3 1234018 z In [148]: df.level.dtype Out[148]: dtype('int64') NA values# To control which values are parsed as missing values (which are signified by NaN), specify a string in na_values. If you specify a list of strings, then all values in it are considered to be missing values. If you specify a number (a float, like 5.0 or an integer like 5), the corresponding equivalent values will also imply a missing value (in this case effectively [5.0, 5] are recognized as NaN). To completely override the default values that are recognized as missing, specify keep_default_na=False. The default NaN recognized values are ['-1.#IND', '1.#QNAN', '1.#IND', '-1.#QNAN', '#N/A N/A', '#N/A', 'N/A', 'n/a', 'NA', '<NA>', '#NA', 'NULL', 'null', 'NaN', '-NaN', 'nan', '-nan', '']. Let us consider some examples: pd.read_csv("path_to_file.csv", na_values=[5]) In the example above 5 and 5.0 will be recognized as NaN, in addition to the defaults. A string will first be interpreted as a numerical 5, then as a NaN. pd.read_csv("path_to_file.csv", keep_default_na=False, na_values=[""]) Above, only an empty field will be recognized as NaN. pd.read_csv("path_to_file.csv", keep_default_na=False, na_values=["NA", "0"]) Above, both NA and 0 as strings are NaN. pd.read_csv("path_to_file.csv", na_values=["Nope"]) The default values, in addition to the string "Nope" are recognized as NaN. Infinity# inf like values will be parsed as np.inf (positive infinity), and -inf as -np.inf (negative infinity). These will ignore the case of the value, meaning Inf, will also be parsed as np.inf. Returning Series# Using the squeeze keyword, the parser will return output with a single column as a Series: Deprecated since version 1.4.0: Users should append .squeeze("columns") to the DataFrame returned by read_csv instead. In [149]: data = "level\nPatient1,123000\nPatient2,23000\nPatient3,1234018" In [150]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [151]: print(open("tmp.csv").read()) level Patient1,123000 Patient2,23000 Patient3,1234018 In [152]: output = pd.read_csv("tmp.csv", squeeze=True) In [153]: output Out[153]: Patient1 123000 Patient2 23000 Patient3 1234018 Name: level, dtype: int64 In [154]: type(output) Out[154]: pandas.core.series.Series Boolean values# The common values True, False, TRUE, and FALSE are all recognized as boolean. Occasionally you might want to recognize other values as being boolean. To do this, use the true_values and false_values options as follows: In [155]: data = "a,b,c\n1,Yes,2\n3,No,4" In [156]: print(data) a,b,c 1,Yes,2 3,No,4 In [157]: pd.read_csv(StringIO(data)) Out[157]: a b c 0 1 Yes 2 1 3 No 4 In [158]: pd.read_csv(StringIO(data), true_values=["Yes"], false_values=["No"]) Out[158]: a b c 0 1 True 2 1 3 False 4 Handling “bad” lines# Some files may have malformed lines with too few fields or too many. Lines with too few fields will have NA values filled in the trailing fields. Lines with too many fields will raise an error by default: In [159]: data = "a,b,c\n1,2,3\n4,5,6,7\n8,9,10" In [160]: pd.read_csv(StringIO(data)) --------------------------------------------------------------------------- ParserError Traceback (most recent call last) Cell In[160], line 1 ----> 1 pd.read_csv(StringIO(data)) File ~/work/pandas/pandas/pandas/util/_decorators.py:211, in deprecate_kwarg.<locals>._deprecate_kwarg.<locals>.wrapper(*args, **kwargs) 209 else: 210 kwargs[new_arg_name] = new_arg_value --> 211 return func(*args, **kwargs) File ~/work/pandas/pandas/pandas/util/_decorators.py:331, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs) 325 if len(args) > num_allow_args: 326 warnings.warn( 327 msg.format(arguments=_format_argument_list(allow_args)), 328 FutureWarning, 329 stacklevel=find_stack_level(), 330 ) --> 331 return func(*args, **kwargs) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:950, in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, error_bad_lines, warn_bad_lines, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options) 935 kwds_defaults = _refine_defaults_read( 936 dialect, 937 delimiter, (...) 946 defaults={"delimiter": ","}, 947 ) 948 kwds.update(kwds_defaults) --> 950 return _read(filepath_or_buffer, kwds) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:611, in _read(filepath_or_buffer, kwds) 608 return parser 610 with parser: --> 611 return parser.read(nrows) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:1778, in TextFileReader.read(self, nrows) 1771 nrows = validate_integer("nrows", nrows) 1772 try: 1773 # error: "ParserBase" has no attribute "read" 1774 ( 1775 index, 1776 columns, 1777 col_dict, -> 1778 ) = self._engine.read( # type: ignore[attr-defined] 1779 nrows 1780 ) 1781 except Exception: 1782 self.close() File ~/work/pandas/pandas/pandas/io/parsers/c_parser_wrapper.py:230, in CParserWrapper.read(self, nrows) 228 try: 229 if self.low_memory: --> 230 chunks = self._reader.read_low_memory(nrows) 231 # destructive to chunks 232 data = _concatenate_chunks(chunks) File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:808, in pandas._libs.parsers.TextReader.read_low_memory() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:866, in pandas._libs.parsers.TextReader._read_rows() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:852, in pandas._libs.parsers.TextReader._tokenize_rows() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:1973, in pandas._libs.parsers.raise_parser_error() ParserError: Error tokenizing data. C error: Expected 3 fields in line 3, saw 4 You can elect to skip bad lines: In [29]: pd.read_csv(StringIO(data), on_bad_lines="warn") Skipping line 3: expected 3 fields, saw 4 Out[29]: a b c 0 1 2 3 1 8 9 10 Or pass a callable function to handle the bad line if engine="python". The bad line will be a list of strings that was split by the sep: In [29]: external_list = [] In [30]: def bad_lines_func(line): ...: external_list.append(line) ...: return line[-3:] In [31]: pd.read_csv(StringIO(data), on_bad_lines=bad_lines_func, engine="python") Out[31]: a b c 0 1 2 3 1 5 6 7 2 8 9 10 In [32]: external_list Out[32]: [4, 5, 6, 7] .. versionadded:: 1.4.0 You can also use the usecols parameter to eliminate extraneous column data that appear in some lines but not others: In [33]: pd.read_csv(StringIO(data), usecols=[0, 1, 2]) Out[33]: a b c 0 1 2 3 1 4 5 6 2 8 9 10 In case you want to keep all data including the lines with too many fields, you can specify a sufficient number of names. This ensures that lines with not enough fields are filled with NaN. In [34]: pd.read_csv(StringIO(data), names=['a', 'b', 'c', 'd']) Out[34]: a b c d 0 1 2 3 NaN 1 4 5 6 7 2 8 9 10 NaN Dialect# The dialect keyword gives greater flexibility in specifying the file format. By default it uses the Excel dialect but you can specify either the dialect name or a csv.Dialect instance. Suppose you had data with unenclosed quotes: In [161]: data = "label1,label2,label3\n" 'index1,"a,c,e\n' "index2,b,d,f" In [162]: print(data) label1,label2,label3 index1,"a,c,e index2,b,d,f By default, read_csv uses the Excel dialect and treats the double quote as the quote character, which causes it to fail when it finds a newline before it finds the closing double quote. We can get around this using dialect: In [163]: import csv In [164]: dia = csv.excel() In [165]: dia.quoting = csv.QUOTE_NONE In [166]: pd.read_csv(StringIO(data), dialect=dia) Out[166]: label1 label2 label3 index1 "a c e index2 b d f All of the dialect options can be specified separately by keyword arguments: In [167]: data = "a,b,c~1,2,3~4,5,6" In [168]: pd.read_csv(StringIO(data), lineterminator="~") Out[168]: a b c 0 1 2 3 1 4 5 6 Another common dialect option is skipinitialspace, to skip any whitespace after a delimiter: In [169]: data = "a, b, c\n1, 2, 3\n4, 5, 6" In [170]: print(data) a, b, c 1, 2, 3 4, 5, 6 In [171]: pd.read_csv(StringIO(data), skipinitialspace=True) Out[171]: a b c 0 1 2 3 1 4 5 6 The parsers make every attempt to “do the right thing” and not be fragile. Type inference is a pretty big deal. If a column can be coerced to integer dtype without altering the contents, the parser will do so. Any non-numeric columns will come through as object dtype as with the rest of pandas objects. Quoting and Escape Characters# Quotes (and other escape characters) in embedded fields can be handled in any number of ways. One way is to use backslashes; to properly parse this data, you should pass the escapechar option: In [172]: data = 'a,b\n"hello, \\"Bob\\", nice to see you",5' In [173]: print(data) a,b "hello, \"Bob\", nice to see you",5 In [174]: pd.read_csv(StringIO(data), escapechar="\\") Out[174]: a b 0 hello, "Bob", nice to see you 5 Files with fixed width columns# While read_csv() reads delimited data, the read_fwf() function works with data files that have known and fixed column widths. The function parameters to read_fwf are largely the same as read_csv with two extra parameters, and a different usage of the delimiter parameter: colspecs: A list of pairs (tuples) giving the extents of the fixed-width fields of each line as half-open intervals (i.e., [from, to[ ). String value ‘infer’ can be used to instruct the parser to try detecting the column specifications from the first 100 rows of the data. Default behavior, if not specified, is to infer. widths: A list of field widths which can be used instead of ‘colspecs’ if the intervals are contiguous. delimiter: Characters to consider as filler characters in the fixed-width file. Can be used to specify the filler character of the fields if it is not spaces (e.g., ‘~’). Consider a typical fixed-width data file: In [175]: data1 = ( .....: "id8141 360.242940 149.910199 11950.7\n" .....: "id1594 444.953632 166.985655 11788.4\n" .....: "id1849 364.136849 183.628767 11806.2\n" .....: "id1230 413.836124 184.375703 11916.8\n" .....: "id1948 502.953953 173.237159 12468.3" .....: ) .....: In [176]: with open("bar.csv", "w") as f: .....: f.write(data1) .....: In order to parse this file into a DataFrame, we simply need to supply the column specifications to the read_fwf function along with the file name: # Column specifications are a list of half-intervals In [177]: colspecs = [(0, 6), (8, 20), (21, 33), (34, 43)] In [178]: df = pd.read_fwf("bar.csv", colspecs=colspecs, header=None, index_col=0) In [179]: df Out[179]: 1 2 3 0 id8141 360.242940 149.910199 11950.7 id1594 444.953632 166.985655 11788.4 id1849 364.136849 183.628767 11806.2 id1230 413.836124 184.375703 11916.8 id1948 502.953953 173.237159 12468.3 Note how the parser automatically picks column names X.<column number> when header=None argument is specified. Alternatively, you can supply just the column widths for contiguous columns: # Widths are a list of integers In [180]: widths = [6, 14, 13, 10] In [181]: df = pd.read_fwf("bar.csv", widths=widths, header=None) In [182]: df Out[182]: 0 1 2 3 0 id8141 360.242940 149.910199 11950.7 1 id1594 444.953632 166.985655 11788.4 2 id1849 364.136849 183.628767 11806.2 3 id1230 413.836124 184.375703 11916.8 4 id1948 502.953953 173.237159 12468.3 The parser will take care of extra white spaces around the columns so it’s ok to have extra separation between the columns in the file. By default, read_fwf will try to infer the file’s colspecs by using the first 100 rows of the file. It can do it only in cases when the columns are aligned and correctly separated by the provided delimiter (default delimiter is whitespace). In [183]: df = pd.read_fwf("bar.csv", header=None, index_col=0) In [184]: df Out[184]: 1 2 3 0 id8141 360.242940 149.910199 11950.7 id1594 444.953632 166.985655 11788.4 id1849 364.136849 183.628767 11806.2 id1230 413.836124 184.375703 11916.8 id1948 502.953953 173.237159 12468.3 read_fwf supports the dtype parameter for specifying the types of parsed columns to be different from the inferred type. In [185]: pd.read_fwf("bar.csv", header=None, index_col=0).dtypes Out[185]: 1 float64 2 float64 3 float64 dtype: object In [186]: pd.read_fwf("bar.csv", header=None, dtype={2: "object"}).dtypes Out[186]: 0 object 1 float64 2 object 3 float64 dtype: object Indexes# Files with an “implicit” index column# Consider a file with one less entry in the header than the number of data column: In [187]: data = "A,B,C\n20090101,a,1,2\n20090102,b,3,4\n20090103,c,4,5" In [188]: print(data) A,B,C 20090101,a,1,2 20090102,b,3,4 20090103,c,4,5 In [189]: with open("foo.csv", "w") as f: .....: f.write(data) .....: In this special case, read_csv assumes that the first column is to be used as the index of the DataFrame: In [190]: pd.read_csv("foo.csv") Out[190]: A B C 20090101 a 1 2 20090102 b 3 4 20090103 c 4 5 Note that the dates weren’t automatically parsed. In that case you would need to do as before: In [191]: df = pd.read_csv("foo.csv", parse_dates=True) In [192]: df.index Out[192]: DatetimeIndex(['2009-01-01', '2009-01-02', '2009-01-03'], dtype='datetime64[ns]', freq=None) Reading an index with a MultiIndex# Suppose you have data indexed by two columns: In [193]: data = 'year,indiv,zit,xit\n1977,"A",1.2,.6\n1977,"B",1.5,.5' In [194]: print(data) year,indiv,zit,xit 1977,"A",1.2,.6 1977,"B",1.5,.5 In [195]: with open("mindex_ex.csv", mode="w") as f: .....: f.write(data) .....: The index_col argument to read_csv can take a list of column numbers to turn multiple columns into a MultiIndex for the index of the returned object: In [196]: df = pd.read_csv("mindex_ex.csv", index_col=[0, 1]) In [197]: df Out[197]: zit xit year indiv 1977 A 1.2 0.6 B 1.5 0.5 In [198]: df.loc[1977] Out[198]: zit xit indiv A 1.2 0.6 B 1.5 0.5 Reading columns with a MultiIndex# By specifying list of row locations for the header argument, you can read in a MultiIndex for the columns. Specifying non-consecutive rows will skip the intervening rows. In [199]: from pandas._testing import makeCustomDataframe as mkdf In [200]: df = mkdf(5, 3, r_idx_nlevels=2, c_idx_nlevels=4) In [201]: df.to_csv("mi.csv") In [202]: print(open("mi.csv").read()) C0,,C_l0_g0,C_l0_g1,C_l0_g2 C1,,C_l1_g0,C_l1_g1,C_l1_g2 C2,,C_l2_g0,C_l2_g1,C_l2_g2 C3,,C_l3_g0,C_l3_g1,C_l3_g2 R0,R1,,, R_l0_g0,R_l1_g0,R0C0,R0C1,R0C2 R_l0_g1,R_l1_g1,R1C0,R1C1,R1C2 R_l0_g2,R_l1_g2,R2C0,R2C1,R2C2 R_l0_g3,R_l1_g3,R3C0,R3C1,R3C2 R_l0_g4,R_l1_g4,R4C0,R4C1,R4C2 In [203]: pd.read_csv("mi.csv", header=[0, 1, 2, 3], index_col=[0, 1]) Out[203]: C0 C_l0_g0 C_l0_g1 C_l0_g2 C1 C_l1_g0 C_l1_g1 C_l1_g2 C2 C_l2_g0 C_l2_g1 C_l2_g2 C3 C_l3_g0 C_l3_g1 C_l3_g2 R0 R1 R_l0_g0 R_l1_g0 R0C0 R0C1 R0C2 R_l0_g1 R_l1_g1 R1C0 R1C1 R1C2 R_l0_g2 R_l1_g2 R2C0 R2C1 R2C2 R_l0_g3 R_l1_g3 R3C0 R3C1 R3C2 R_l0_g4 R_l1_g4 R4C0 R4C1 R4C2 read_csv is also able to interpret a more common format of multi-columns indices. In [204]: data = ",a,a,a,b,c,c\n,q,r,s,t,u,v\none,1,2,3,4,5,6\ntwo,7,8,9,10,11,12" In [205]: print(data) ,a,a,a,b,c,c ,q,r,s,t,u,v one,1,2,3,4,5,6 two,7,8,9,10,11,12 In [206]: with open("mi2.csv", "w") as fh: .....: fh.write(data) .....: In [207]: pd.read_csv("mi2.csv", header=[0, 1], index_col=0) Out[207]: a b c q r s t u v one 1 2 3 4 5 6 two 7 8 9 10 11 12 Note If an index_col is not specified (e.g. you don’t have an index, or wrote it with df.to_csv(..., index=False), then any names on the columns index will be lost. Automatically “sniffing” the delimiter# read_csv is capable of inferring delimited (not necessarily comma-separated) files, as pandas uses the csv.Sniffer class of the csv module. For this, you have to specify sep=None. In [208]: df = pd.DataFrame(np.random.randn(10, 4)) In [209]: df.to_csv("tmp.csv", sep="|") In [210]: df.to_csv("tmp2.csv", sep=":") In [211]: pd.read_csv("tmp2.csv", sep=None, engine="python") Out[211]: Unnamed: 0 0 1 2 3 0 0 0.469112 -0.282863 -1.509059 -1.135632 1 1 1.212112 -0.173215 0.119209 -1.044236 2 2 -0.861849 -2.104569 -0.494929 1.071804 3 3 0.721555 -0.706771 -1.039575 0.271860 4 4 -0.424972 0.567020 0.276232 -1.087401 5 5 -0.673690 0.113648 -1.478427 0.524988 6 6 0.404705 0.577046 -1.715002 -1.039268 7 7 -0.370647 -1.157892 -1.344312 0.844885 8 8 1.075770 -0.109050 1.643563 -1.469388 9 9 0.357021 -0.674600 -1.776904 -0.968914 Reading multiple files to create a single DataFrame# It’s best to use concat() to combine multiple files. See the cookbook for an example. Iterating through files chunk by chunk# Suppose you wish to iterate through a (potentially very large) file lazily rather than reading the entire file into memory, such as the following: In [212]: df = pd.DataFrame(np.random.randn(10, 4)) In [213]: df.to_csv("tmp.csv", sep="|") In [214]: table = pd.read_csv("tmp.csv", sep="|") In [215]: table Out[215]: Unnamed: 0 0 1 2 3 0 0 -1.294524 0.413738 0.276662 -0.472035 1 1 -0.013960 -0.362543 -0.006154 -0.923061 2 2 0.895717 0.805244 -1.206412 2.565646 3 3 1.431256 1.340309 -1.170299 -0.226169 4 4 0.410835 0.813850 0.132003 -0.827317 5 5 -0.076467 -1.187678 1.130127 -1.436737 6 6 -1.413681 1.607920 1.024180 0.569605 7 7 0.875906 -2.211372 0.974466 -2.006747 8 8 -0.410001 -0.078638 0.545952 -1.219217 9 9 -1.226825 0.769804 -1.281247 -0.727707 By specifying a chunksize to read_csv, the return value will be an iterable object of type TextFileReader: In [216]: with pd.read_csv("tmp.csv", sep="|", chunksize=4) as reader: .....: reader .....: for chunk in reader: .....: print(chunk) .....: Unnamed: 0 0 1 2 3 0 0 -1.294524 0.413738 0.276662 -0.472035 1 1 -0.013960 -0.362543 -0.006154 -0.923061 2 2 0.895717 0.805244 -1.206412 2.565646 3 3 1.431256 1.340309 -1.170299 -0.226169 Unnamed: 0 0 1 2 3 4 4 0.410835 0.813850 0.132003 -0.827317 5 5 -0.076467 -1.187678 1.130127 -1.436737 6 6 -1.413681 1.607920 1.024180 0.569605 7 7 0.875906 -2.211372 0.974466 -2.006747 Unnamed: 0 0 1 2 3 8 8 -0.410001 -0.078638 0.545952 -1.219217 9 9 -1.226825 0.769804 -1.281247 -0.727707 Changed in version 1.2: read_csv/json/sas return a context-manager when iterating through a file. Specifying iterator=True will also return the TextFileReader object: In [217]: with pd.read_csv("tmp.csv", sep="|", iterator=True) as reader: .....: reader.get_chunk(5) .....: Specifying the parser engine# Pandas currently supports three engines, the C engine, the python engine, and an experimental pyarrow engine (requires the pyarrow package). In general, the pyarrow engine is fastest on larger workloads and is equivalent in speed to the C engine on most other workloads. The python engine tends to be slower than the pyarrow and C engines on most workloads. However, the pyarrow engine is much less robust than the C engine, which lacks a few features compared to the Python engine. Where possible, pandas uses the C parser (specified as engine='c'), but it may fall back to Python if C-unsupported options are specified. Currently, options unsupported by the C and pyarrow engines include: sep other than a single character (e.g. regex separators) skipfooter sep=None with delim_whitespace=False Specifying any of the above options will produce a ParserWarning unless the python engine is selected explicitly using engine='python'. Options that are unsupported by the pyarrow engine which are not covered by the list above include: float_precision chunksize comment nrows thousands memory_map dialect warn_bad_lines error_bad_lines on_bad_lines delim_whitespace quoting lineterminator converters decimal iterator dayfirst infer_datetime_format verbose skipinitialspace low_memory Specifying these options with engine='pyarrow' will raise a ValueError. Reading/writing remote files# You can pass in a URL to read or write remote files to many of pandas’ IO functions - the following example shows reading a CSV file: df = pd.read_csv("https://download.bls.gov/pub/time.series/cu/cu.item", sep="\t") New in version 1.3.0. A custom header can be sent alongside HTTP(s) requests by passing a dictionary of header key value mappings to the storage_options keyword argument as shown below: headers = {"User-Agent": "pandas"} df = pd.read_csv( "https://download.bls.gov/pub/time.series/cu/cu.item", sep="\t", storage_options=headers ) All URLs which are not local files or HTTP(s) are handled by fsspec, if installed, and its various filesystem implementations (including Amazon S3, Google Cloud, SSH, FTP, webHDFS…). Some of these implementations will require additional packages to be installed, for example S3 URLs require the s3fs library: df = pd.read_json("s3://pandas-test/adatafile.json") When dealing with remote storage systems, you might need extra configuration with environment variables or config files in special locations. For example, to access data in your S3 bucket, you will need to define credentials in one of the several ways listed in the S3Fs documentation. The same is true for several of the storage backends, and you should follow the links at fsimpl1 for implementations built into fsspec and fsimpl2 for those not included in the main fsspec distribution. You can also pass parameters directly to the backend driver. For example, if you do not have S3 credentials, you can still access public data by specifying an anonymous connection, such as New in version 1.2.0. pd.read_csv( "s3://ncei-wcsd-archive/data/processed/SH1305/18kHz/SaKe2013" "-D20130523-T080854_to_SaKe2013-D20130523-T085643.csv", storage_options={"anon": True}, ) fsspec also allows complex URLs, for accessing data in compressed archives, local caching of files, and more. To locally cache the above example, you would modify the call to pd.read_csv( "simplecache::s3://ncei-wcsd-archive/data/processed/SH1305/18kHz/" "SaKe2013-D20130523-T080854_to_SaKe2013-D20130523-T085643.csv", storage_options={"s3": {"anon": True}}, ) where we specify that the “anon” parameter is meant for the “s3” part of the implementation, not to the caching implementation. Note that this caches to a temporary directory for the duration of the session only, but you can also specify a permanent store. Writing out data# Writing to CSV format# The Series and DataFrame objects have an instance method to_csv which allows storing the contents of the object as a comma-separated-values file. The function takes a number of arguments. Only the first is required. path_or_buf: A string path to the file to write or a file object. If a file object it must be opened with newline='' sep : Field delimiter for the output file (default “,”) na_rep: A string representation of a missing value (default ‘’) float_format: Format string for floating point numbers columns: Columns to write (default None) header: Whether to write out the column names (default True) index: whether to write row (index) names (default True) index_label: Column label(s) for index column(s) if desired. If None (default), and header and index are True, then the index names are used. (A sequence should be given if the DataFrame uses MultiIndex). mode : Python write mode, default ‘w’ encoding: a string representing the encoding to use if the contents are non-ASCII, for Python versions prior to 3 lineterminator: Character sequence denoting line end (default os.linesep) quoting: Set quoting rules as in csv module (default csv.QUOTE_MINIMAL). Note that if you have set a float_format then floats are converted to strings and csv.QUOTE_NONNUMERIC will treat them as non-numeric quotechar: Character used to quote fields (default ‘”’) doublequote: Control quoting of quotechar in fields (default True) escapechar: Character used to escape sep and quotechar when appropriate (default None) chunksize: Number of rows to write at a time date_format: Format string for datetime objects Writing a formatted string# The DataFrame object has an instance method to_string which allows control over the string representation of the object. All arguments are optional: buf default None, for example a StringIO object columns default None, which columns to write col_space default None, minimum width of each column. na_rep default NaN, representation of NA value formatters default None, a dictionary (by column) of functions each of which takes a single argument and returns a formatted string float_format default None, a function which takes a single (float) argument and returns a formatted string; to be applied to floats in the DataFrame. sparsify default True, set to False for a DataFrame with a hierarchical index to print every MultiIndex key at each row. index_names default True, will print the names of the indices index default True, will print the index (ie, row labels) header default True, will print the column labels justify default left, will print column headers left- or right-justified The Series object also has a to_string method, but with only the buf, na_rep, float_format arguments. There is also a length argument which, if set to True, will additionally output the length of the Series. JSON# Read and write JSON format files and strings. Writing JSON# A Series or DataFrame can be converted to a valid JSON string. Use to_json with optional parameters: path_or_buf : the pathname or buffer to write the output This can be None in which case a JSON string is returned orient : Series: default is index allowed values are {split, records, index} DataFrame: default is columns allowed values are {split, records, index, columns, values, table} The format of the JSON string split dict like {index -> [index], columns -> [columns], data -> [values]} records list like [{column -> value}, … , {column -> value}] index dict like {index -> {column -> value}} columns dict like {column -> {index -> value}} values just the values array table adhering to the JSON Table Schema date_format : string, type of date conversion, ‘epoch’ for timestamp, ‘iso’ for ISO8601. double_precision : The number of decimal places to use when encoding floating point values, default 10. force_ascii : force encoded string to be ASCII, default True. date_unit : The time unit to encode to, governs timestamp and ISO8601 precision. One of ‘s’, ‘ms’, ‘us’ or ‘ns’ for seconds, milliseconds, microseconds and nanoseconds respectively. Default ‘ms’. default_handler : The handler to call if an object cannot otherwise be converted to a suitable format for JSON. Takes a single argument, which is the object to convert, and returns a serializable object. lines : If records orient, then will write each record per line as json. Note NaN’s, NaT’s and None will be converted to null and datetime objects will be converted based on the date_format and date_unit parameters. In [218]: dfj = pd.DataFrame(np.random.randn(5, 2), columns=list("AB")) In [219]: json = dfj.to_json() In [220]: json Out[220]: '{"A":{"0":-0.1213062281,"1":0.6957746499,"2":0.9597255933,"3":-0.6199759194,"4":-0.7323393705},"B":{"0":-0.0978826728,"1":0.3417343559,"2":-1.1103361029,"3":0.1497483186,"4":0.6877383895}}' Orient options# There are a number of different options for the format of the resulting JSON file / string. Consider the following DataFrame and Series: In [221]: dfjo = pd.DataFrame( .....: dict(A=range(1, 4), B=range(4, 7), C=range(7, 10)), .....: columns=list("ABC"), .....: index=list("xyz"), .....: ) .....: In [222]: dfjo Out[222]: A B C x 1 4 7 y 2 5 8 z 3 6 9 In [223]: sjo = pd.Series(dict(x=15, y=16, z=17), name="D") In [224]: sjo Out[224]: x 15 y 16 z 17 Name: D, dtype: int64 Column oriented (the default for DataFrame) serializes the data as nested JSON objects with column labels acting as the primary index: In [225]: dfjo.to_json(orient="columns") Out[225]: '{"A":{"x":1,"y":2,"z":3},"B":{"x":4,"y":5,"z":6},"C":{"x":7,"y":8,"z":9}}' # Not available for Series Index oriented (the default for Series) similar to column oriented but the index labels are now primary: In [226]: dfjo.to_json(orient="index") Out[226]: '{"x":{"A":1,"B":4,"C":7},"y":{"A":2,"B":5,"C":8},"z":{"A":3,"B":6,"C":9}}' In [227]: sjo.to_json(orient="index") Out[227]: '{"x":15,"y":16,"z":17}' Record oriented serializes the data to a JSON array of column -> value records, index labels are not included. This is useful for passing DataFrame data to plotting libraries, for example the JavaScript library d3.js: In [228]: dfjo.to_json(orient="records") Out[228]: '[{"A":1,"B":4,"C":7},{"A":2,"B":5,"C":8},{"A":3,"B":6,"C":9}]' In [229]: sjo.to_json(orient="records") Out[229]: '[15,16,17]' Value oriented is a bare-bones option which serializes to nested JSON arrays of values only, column and index labels are not included: In [230]: dfjo.to_json(orient="values") Out[230]: '[[1,4,7],[2,5,8],[3,6,9]]' # Not available for Series Split oriented serializes to a JSON object containing separate entries for values, index and columns. Name is also included for Series: In [231]: dfjo.to_json(orient="split") Out[231]: '{"columns":["A","B","C"],"index":["x","y","z"],"data":[[1,4,7],[2,5,8],[3,6,9]]}' In [232]: sjo.to_json(orient="split") Out[232]: '{"name":"D","index":["x","y","z"],"data":[15,16,17]}' Table oriented serializes to the JSON Table Schema, allowing for the preservation of metadata including but not limited to dtypes and index names. Note Any orient option that encodes to a JSON object will not preserve the ordering of index and column labels during round-trip serialization. If you wish to preserve label ordering use the split option as it uses ordered containers. Date handling# Writing in ISO date format: In [233]: dfd = pd.DataFrame(np.random.randn(5, 2), columns=list("AB")) In [234]: dfd["date"] = pd.Timestamp("20130101") In [235]: dfd = dfd.sort_index(axis=1, ascending=False) In [236]: json = dfd.to_json(date_format="iso") In [237]: json Out[237]: '{"date":{"0":"2013-01-01T00:00:00.000","1":"2013-01-01T00:00:00.000","2":"2013-01-01T00:00:00.000","3":"2013-01-01T00:00:00.000","4":"2013-01-01T00:00:00.000"},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Writing in ISO date format, with microseconds: In [238]: json = dfd.to_json(date_format="iso", date_unit="us") In [239]: json Out[239]: '{"date":{"0":"2013-01-01T00:00:00.000000","1":"2013-01-01T00:00:00.000000","2":"2013-01-01T00:00:00.000000","3":"2013-01-01T00:00:00.000000","4":"2013-01-01T00:00:00.000000"},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Epoch timestamps, in seconds: In [240]: json = dfd.to_json(date_format="epoch", date_unit="s") In [241]: json Out[241]: '{"date":{"0":1356998400,"1":1356998400,"2":1356998400,"3":1356998400,"4":1356998400},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Writing to a file, with a date index and a date column: In [242]: dfj2 = dfj.copy() In [243]: dfj2["date"] = pd.Timestamp("20130101") In [244]: dfj2["ints"] = list(range(5)) In [245]: dfj2["bools"] = True In [246]: dfj2.index = pd.date_range("20130101", periods=5) In [247]: dfj2.to_json("test.json") In [248]: with open("test.json") as fh: .....: print(fh.read()) .....: {"A":{"1356998400000":-0.1213062281,"1357084800000":0.6957746499,"1357171200000":0.9597255933,"1357257600000":-0.6199759194,"1357344000000":-0.7323393705},"B":{"1356998400000":-0.0978826728,"1357084800000":0.3417343559,"1357171200000":-1.1103361029,"1357257600000":0.1497483186,"1357344000000":0.6877383895},"date":{"1356998400000":1356998400000,"1357084800000":1356998400000,"1357171200000":1356998400000,"1357257600000":1356998400000,"1357344000000":1356998400000},"ints":{"1356998400000":0,"1357084800000":1,"1357171200000":2,"1357257600000":3,"1357344000000":4},"bools":{"1356998400000":true,"1357084800000":true,"1357171200000":true,"1357257600000":true,"1357344000000":true}} Fallback behavior# If the JSON serializer cannot handle the container contents directly it will fall back in the following manner: if the dtype is unsupported (e.g. np.complex_) then the default_handler, if provided, will be called for each value, otherwise an exception is raised. if an object is unsupported it will attempt the following: check if the object has defined a toDict method and call it. A toDict method should return a dict which will then be JSON serialized. invoke the default_handler if one was provided. convert the object to a dict by traversing its contents. However this will often fail with an OverflowError or give unexpected results. In general the best approach for unsupported objects or dtypes is to provide a default_handler. For example: >>> DataFrame([1.0, 2.0, complex(1.0, 2.0)]).to_json() # raises RuntimeError: Unhandled numpy dtype 15 can be dealt with by specifying a simple default_handler: In [249]: pd.DataFrame([1.0, 2.0, complex(1.0, 2.0)]).to_json(default_handler=str) Out[249]: '{"0":{"0":"(1+0j)","1":"(2+0j)","2":"(1+2j)"}}' Reading JSON# Reading a JSON string to pandas object can take a number of parameters. The parser will try to parse a DataFrame if typ is not supplied or is None. To explicitly force Series parsing, pass typ=series filepath_or_buffer : a VALID JSON string or file handle / StringIO. The string could be a URL. Valid URL schemes include http, ftp, S3, and file. For file URLs, a host is expected. For instance, a local file could be file ://localhost/path/to/table.json typ : type of object to recover (series or frame), default ‘frame’ orient : Series : default is index allowed values are {split, records, index} DataFrame default is columns allowed values are {split, records, index, columns, values, table} The format of the JSON string split dict like {index -> [index], columns -> [columns], data -> [values]} records list like [{column -> value}, … , {column -> value}] index dict like {index -> {column -> value}} columns dict like {column -> {index -> value}} values just the values array table adhering to the JSON Table Schema dtype : if True, infer dtypes, if a dict of column to dtype, then use those, if False, then don’t infer dtypes at all, default is True, apply only to the data. convert_axes : boolean, try to convert the axes to the proper dtypes, default is True convert_dates : a list of columns to parse for dates; If True, then try to parse date-like columns, default is True. keep_default_dates : boolean, default True. If parsing dates, then parse the default date-like columns. numpy : direct decoding to NumPy arrays. default is False; Supports numeric data only, although labels may be non-numeric. Also note that the JSON ordering MUST be the same for each term if numpy=True. precise_float : boolean, default False. Set to enable usage of higher precision (strtod) function when decoding string to double values. Default (False) is to use fast but less precise builtin functionality. date_unit : string, the timestamp unit to detect if converting dates. Default None. By default the timestamp precision will be detected, if this is not desired then pass one of ‘s’, ‘ms’, ‘us’ or ‘ns’ to force timestamp precision to seconds, milliseconds, microseconds or nanoseconds respectively. lines : reads file as one json object per line. encoding : The encoding to use to decode py3 bytes. chunksize : when used in combination with lines=True, return a JsonReader which reads in chunksize lines per iteration. The parser will raise one of ValueError/TypeError/AssertionError if the JSON is not parseable. If a non-default orient was used when encoding to JSON be sure to pass the same option here so that decoding produces sensible results, see Orient Options for an overview. Data conversion# The default of convert_axes=True, dtype=True, and convert_dates=True will try to parse the axes, and all of the data into appropriate types, including dates. If you need to override specific dtypes, pass a dict to dtype. convert_axes should only be set to False if you need to preserve string-like numbers (e.g. ‘1’, ‘2’) in an axes. Note Large integer values may be converted to dates if convert_dates=True and the data and / or column labels appear ‘date-like’. The exact threshold depends on the date_unit specified. ‘date-like’ means that the column label meets one of the following criteria: it ends with '_at' it ends with '_time' it begins with 'timestamp' it is 'modified' it is 'date' Warning When reading JSON data, automatic coercing into dtypes has some quirks: an index can be reconstructed in a different order from serialization, that is, the returned order is not guaranteed to be the same as before serialization a column that was float data will be converted to integer if it can be done safely, e.g. a column of 1. bool columns will be converted to integer on reconstruction Thus there are times where you may want to specify specific dtypes via the dtype keyword argument. Reading from a JSON string: In [250]: pd.read_json(json) Out[250]: date B A 0 2013-01-01 0.403310 0.176444 1 2013-01-01 0.301624 -0.154951 2 2013-01-01 -1.369849 -2.179861 3 2013-01-01 1.462696 -0.954208 4 2013-01-01 -0.826591 -1.743161 Reading from a file: In [251]: pd.read_json("test.json") Out[251]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True Don’t convert any data (but still convert axes and dates): In [252]: pd.read_json("test.json", dtype=object).dtypes Out[252]: A object B object date object ints object bools object dtype: object Specify dtypes for conversion: In [253]: pd.read_json("test.json", dtype={"A": "float32", "bools": "int8"}).dtypes Out[253]: A float32 B float64 date datetime64[ns] ints int64 bools int8 dtype: object Preserve string indices: In [254]: si = pd.DataFrame( .....: np.zeros((4, 4)), columns=list(range(4)), index=[str(i) for i in range(4)] .....: ) .....: In [255]: si Out[255]: 0 1 2 3 0 0.0 0.0 0.0 0.0 1 0.0 0.0 0.0 0.0 2 0.0 0.0 0.0 0.0 3 0.0 0.0 0.0 0.0 In [256]: si.index Out[256]: Index(['0', '1', '2', '3'], dtype='object') In [257]: si.columns Out[257]: Int64Index([0, 1, 2, 3], dtype='int64') In [258]: json = si.to_json() In [259]: sij = pd.read_json(json, convert_axes=False) In [260]: sij Out[260]: 0 1 2 3 0 0 0 0 0 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 In [261]: sij.index Out[261]: Index(['0', '1', '2', '3'], dtype='object') In [262]: sij.columns Out[262]: Index(['0', '1', '2', '3'], dtype='object') Dates written in nanoseconds need to be read back in nanoseconds: In [263]: json = dfj2.to_json(date_unit="ns") # Try to parse timestamps as milliseconds -> Won't Work In [264]: dfju = pd.read_json(json, date_unit="ms") In [265]: dfju Out[265]: A B date ints bools 1356998400000000000 -0.121306 -0.097883 1356998400000000000 0 True 1357084800000000000 0.695775 0.341734 1356998400000000000 1 True 1357171200000000000 0.959726 -1.110336 1356998400000000000 2 True 1357257600000000000 -0.619976 0.149748 1356998400000000000 3 True 1357344000000000000 -0.732339 0.687738 1356998400000000000 4 True # Let pandas detect the correct precision In [266]: dfju = pd.read_json(json) In [267]: dfju Out[267]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True # Or specify that all timestamps are in nanoseconds In [268]: dfju = pd.read_json(json, date_unit="ns") In [269]: dfju Out[269]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True The Numpy parameter# Note This param has been deprecated as of version 1.0.0 and will raise a FutureWarning. This supports numeric data only. Index and columns labels may be non-numeric, e.g. strings, dates etc. If numpy=True is passed to read_json an attempt will be made to sniff an appropriate dtype during deserialization and to subsequently decode directly to NumPy arrays, bypassing the need for intermediate Python objects. This can provide speedups if you are deserialising a large amount of numeric data: In [270]: randfloats = np.random.uniform(-100, 1000, 10000) In [271]: randfloats.shape = (1000, 10) In [272]: dffloats = pd.DataFrame(randfloats, columns=list("ABCDEFGHIJ")) In [273]: jsonfloats = dffloats.to_json() In [274]: %timeit pd.read_json(jsonfloats) 7.91 ms +- 77.3 us per loop (mean +- std. dev. of 7 runs, 100 loops each) In [275]: %timeit pd.read_json(jsonfloats, numpy=True) 5.71 ms +- 333 us per loop (mean +- std. dev. of 7 runs, 100 loops each) The speedup is less noticeable for smaller datasets: In [276]: jsonfloats = dffloats.head(100).to_json() In [277]: %timeit pd.read_json(jsonfloats) 4.46 ms +- 25.9 us per loop (mean +- std. dev. of 7 runs, 100 loops each) In [278]: %timeit pd.read_json(jsonfloats, numpy=True) 4.09 ms +- 32.3 us per loop (mean +- std. dev. of 7 runs, 100 loops each) Warning Direct NumPy decoding makes a number of assumptions and may fail or produce unexpected output if these assumptions are not satisfied: data is numeric. data is uniform. The dtype is sniffed from the first value decoded. A ValueError may be raised, or incorrect output may be produced if this condition is not satisfied. labels are ordered. Labels are only read from the first container, it is assumed that each subsequent row / column has been encoded in the same order. This should be satisfied if the data was encoded using to_json but may not be the case if the JSON is from another source. Normalization# pandas provides a utility function to take a dict or list of dicts and normalize this semi-structured data into a flat table. In [279]: data = [ .....: {"id": 1, "name": {"first": "Coleen", "last": "Volk"}}, .....: {"name": {"given": "Mark", "family": "Regner"}}, .....: {"id": 2, "name": "Faye Raker"}, .....: ] .....: In [280]: pd.json_normalize(data) Out[280]: id name.first name.last name.given name.family name 0 1.0 Coleen Volk NaN NaN NaN 1 NaN NaN NaN Mark Regner NaN 2 2.0 NaN NaN NaN NaN Faye Raker In [281]: data = [ .....: { .....: "state": "Florida", .....: "shortname": "FL", .....: "info": {"governor": "Rick Scott"}, .....: "county": [ .....: {"name": "Dade", "population": 12345}, .....: {"name": "Broward", "population": 40000}, .....: {"name": "Palm Beach", "population": 60000}, .....: ], .....: }, .....: { .....: "state": "Ohio", .....: "shortname": "OH", .....: "info": {"governor": "John Kasich"}, .....: "county": [ .....: {"name": "Summit", "population": 1234}, .....: {"name": "Cuyahoga", "population": 1337}, .....: ], .....: }, .....: ] .....: In [282]: pd.json_normalize(data, "county", ["state", "shortname", ["info", "governor"]]) Out[282]: name population state shortname info.governor 0 Dade 12345 Florida FL Rick Scott 1 Broward 40000 Florida FL Rick Scott 2 Palm Beach 60000 Florida FL Rick Scott 3 Summit 1234 Ohio OH John Kasich 4 Cuyahoga 1337 Ohio OH John Kasich The max_level parameter provides more control over which level to end normalization. With max_level=1 the following snippet normalizes until 1st nesting level of the provided dict. In [283]: data = [ .....: { .....: "CreatedBy": {"Name": "User001"}, .....: "Lookup": { .....: "TextField": "Some text", .....: "UserField": {"Id": "ID001", "Name": "Name001"}, .....: }, .....: "Image": {"a": "b"}, .....: } .....: ] .....: In [284]: pd.json_normalize(data, max_level=1) Out[284]: CreatedBy.Name Lookup.TextField Lookup.UserField Image.a 0 User001 Some text {'Id': 'ID001', 'Name': 'Name001'} b Line delimited json# pandas is able to read and write line-delimited json files that are common in data processing pipelines using Hadoop or Spark. For line-delimited json files, pandas can also return an iterator which reads in chunksize lines at a time. This can be useful for large files or to read from a stream. In [285]: jsonl = """ .....: {"a": 1, "b": 2} .....: {"a": 3, "b": 4} .....: """ .....: In [286]: df = pd.read_json(jsonl, lines=True) In [287]: df Out[287]: a b 0 1 2 1 3 4 In [288]: df.to_json(orient="records", lines=True) Out[288]: '{"a":1,"b":2}\n{"a":3,"b":4}\n' # reader is an iterator that returns ``chunksize`` lines each iteration In [289]: with pd.read_json(StringIO(jsonl), lines=True, chunksize=1) as reader: .....: reader .....: for chunk in reader: .....: print(chunk) .....: Empty DataFrame Columns: [] Index: [] a b 0 1 2 a b 1 3 4 Table schema# Table Schema is a spec for describing tabular datasets as a JSON object. The JSON includes information on the field names, types, and other attributes. You can use the orient table to build a JSON string with two fields, schema and data. In [290]: df = pd.DataFrame( .....: { .....: "A": [1, 2, 3], .....: "B": ["a", "b", "c"], .....: "C": pd.date_range("2016-01-01", freq="d", periods=3), .....: }, .....: index=pd.Index(range(3), name="idx"), .....: ) .....: In [291]: df Out[291]: A B C idx 0 1 a 2016-01-01 1 2 b 2016-01-02 2 3 c 2016-01-03 In [292]: df.to_json(orient="table", date_format="iso") Out[292]: '{"schema":{"fields":[{"name":"idx","type":"integer"},{"name":"A","type":"integer"},{"name":"B","type":"string"},{"name":"C","type":"datetime"}],"primaryKey":["idx"],"pandas_version":"1.4.0"},"data":[{"idx":0,"A":1,"B":"a","C":"2016-01-01T00:00:00.000"},{"idx":1,"A":2,"B":"b","C":"2016-01-02T00:00:00.000"},{"idx":2,"A":3,"B":"c","C":"2016-01-03T00:00:00.000"}]}' The schema field contains the fields key, which itself contains a list of column name to type pairs, including the Index or MultiIndex (see below for a list of types). The schema field also contains a primaryKey field if the (Multi)index is unique. The second field, data, contains the serialized data with the records orient. The index is included, and any datetimes are ISO 8601 formatted, as required by the Table Schema spec. The full list of types supported are described in the Table Schema spec. This table shows the mapping from pandas types: pandas type Table Schema type int64 integer float64 number bool boolean datetime64[ns] datetime timedelta64[ns] duration categorical any object str A few notes on the generated table schema: The schema object contains a pandas_version field. This contains the version of pandas’ dialect of the schema, and will be incremented with each revision. All dates are converted to UTC when serializing. Even timezone naive values, which are treated as UTC with an offset of 0. In [293]: from pandas.io.json import build_table_schema In [294]: s = pd.Series(pd.date_range("2016", periods=4)) In [295]: build_table_schema(s) Out[295]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'datetime'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} datetimes with a timezone (before serializing), include an additional field tz with the time zone name (e.g. 'US/Central'). In [296]: s_tz = pd.Series(pd.date_range("2016", periods=12, tz="US/Central")) In [297]: build_table_schema(s_tz) Out[297]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'datetime', 'tz': 'US/Central'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} Periods are converted to timestamps before serialization, and so have the same behavior of being converted to UTC. In addition, periods will contain and additional field freq with the period’s frequency, e.g. 'A-DEC'. In [298]: s_per = pd.Series(1, index=pd.period_range("2016", freq="A-DEC", periods=4)) In [299]: build_table_schema(s_per) Out[299]: {'fields': [{'name': 'index', 'type': 'datetime', 'freq': 'A-DEC'}, {'name': 'values', 'type': 'integer'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} Categoricals use the any type and an enum constraint listing the set of possible values. Additionally, an ordered field is included: In [300]: s_cat = pd.Series(pd.Categorical(["a", "b", "a"])) In [301]: build_table_schema(s_cat) Out[301]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'any', 'constraints': {'enum': ['a', 'b']}, 'ordered': False}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} A primaryKey field, containing an array of labels, is included if the index is unique: In [302]: s_dupe = pd.Series([1, 2], index=[1, 1]) In [303]: build_table_schema(s_dupe) Out[303]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'integer'}], 'pandas_version': '1.4.0'} The primaryKey behavior is the same with MultiIndexes, but in this case the primaryKey is an array: In [304]: s_multi = pd.Series(1, index=pd.MultiIndex.from_product([("a", "b"), (0, 1)])) In [305]: build_table_schema(s_multi) Out[305]: {'fields': [{'name': 'level_0', 'type': 'string'}, {'name': 'level_1', 'type': 'integer'}, {'name': 'values', 'type': 'integer'}], 'primaryKey': FrozenList(['level_0', 'level_1']), 'pandas_version': '1.4.0'} The default naming roughly follows these rules: For series, the object.name is used. If that’s none, then the name is values For DataFrames, the stringified version of the column name is used For Index (not MultiIndex), index.name is used, with a fallback to index if that is None. For MultiIndex, mi.names is used. If any level has no name, then level_<i> is used. read_json also accepts orient='table' as an argument. This allows for the preservation of metadata such as dtypes and index names in a round-trippable manner. In [306]: df = pd.DataFrame( .....: { .....: "foo": [1, 2, 3, 4], .....: "bar": ["a", "b", "c", "d"], .....: "baz": pd.date_range("2018-01-01", freq="d", periods=4), .....: "qux": pd.Categorical(["a", "b", "c", "c"]), .....: }, .....: index=pd.Index(range(4), name="idx"), .....: ) .....: In [307]: df Out[307]: foo bar baz qux idx 0 1 a 2018-01-01 a 1 2 b 2018-01-02 b 2 3 c 2018-01-03 c 3 4 d 2018-01-04 c In [308]: df.dtypes Out[308]: foo int64 bar object baz datetime64[ns] qux category dtype: object In [309]: df.to_json("test.json", orient="table") In [310]: new_df = pd.read_json("test.json", orient="table") In [311]: new_df Out[311]: foo bar baz qux idx 0 1 a 2018-01-01 a 1 2 b 2018-01-02 b 2 3 c 2018-01-03 c 3 4 d 2018-01-04 c In [312]: new_df.dtypes Out[312]: foo int64 bar object baz datetime64[ns] qux category dtype: object Please note that the literal string ‘index’ as the name of an Index is not round-trippable, nor are any names beginning with 'level_' within a MultiIndex. These are used by default in DataFrame.to_json() to indicate missing values and the subsequent read cannot distinguish the intent. In [313]: df.index.name = "index" In [314]: df.to_json("test.json", orient="table") In [315]: new_df = pd.read_json("test.json", orient="table") In [316]: print(new_df.index.name) None When using orient='table' along with user-defined ExtensionArray, the generated schema will contain an additional extDtype key in the respective fields element. This extra key is not standard but does enable JSON roundtrips for extension types (e.g. read_json(df.to_json(orient="table"), orient="table")). The extDtype key carries the name of the extension, if you have properly registered the ExtensionDtype, pandas will use said name to perform a lookup into the registry and re-convert the serialized data into your custom dtype. HTML# Reading HTML content# Warning We highly encourage you to read the HTML Table Parsing gotchas below regarding the issues surrounding the BeautifulSoup4/html5lib/lxml parsers. The top-level read_html() function can accept an HTML string/file/URL and will parse HTML tables into list of pandas DataFrames. Let’s look at a few examples. Note read_html returns a list of DataFrame objects, even if there is only a single table contained in the HTML content. Read a URL with no options: In [320]: "https://www.fdic.gov/resources/resolutions/bank-failures/failed-bank-list" In [321]: pd.read_html(url) Out[321]: [ Bank NameBank CityCity StateSt ... Acquiring InstitutionAI Closing DateClosing FundFund 0 Almena State Bank Almena KS ... Equity Bank October 23, 2020 10538 1 First City Bank of Florida Fort Walton Beach FL ... United Fidelity Bank, fsb October 16, 2020 10537 2 The First State Bank Barboursville WV ... MVB Bank, Inc. April 3, 2020 10536 3 Ericson State Bank Ericson NE ... Farmers and Merchants Bank February 14, 2020 10535 4 City National Bank of New Jersey Newark NJ ... Industrial Bank November 1, 2019 10534 .. ... ... ... ... ... ... ... 558 Superior Bank, FSB Hinsdale IL ... Superior Federal, FSB July 27, 2001 6004 559 Malta National Bank Malta OH ... North Valley Bank May 3, 2001 4648 560 First Alliance Bank & Trust Co. Manchester NH ... Southern New Hampshire Bank & Trust February 2, 2001 4647 561 National State Bank of Metropolis Metropolis IL ... Banterra Bank of Marion December 14, 2000 4646 562 Bank of Honolulu Honolulu HI ... Bank of the Orient October 13, 2000 4645 [563 rows x 7 columns]] Note The data from the above URL changes every Monday so the resulting data above may be slightly different. Read in the content of the file from the above URL and pass it to read_html as a string: In [317]: html_str = """ .....: <table> .....: <tr> .....: <th>A</th> .....: <th colspan="1">B</th> .....: <th rowspan="1">C</th> .....: </tr> .....: <tr> .....: <td>a</td> .....: <td>b</td> .....: <td>c</td> .....: </tr> .....: </table> .....: """ .....: In [318]: with open("tmp.html", "w") as f: .....: f.write(html_str) .....: In [319]: df = pd.read_html("tmp.html") In [320]: df[0] Out[320]: A B C 0 a b c You can even pass in an instance of StringIO if you so desire: In [321]: dfs = pd.read_html(StringIO(html_str)) In [322]: dfs[0] Out[322]: A B C 0 a b c Note The following examples are not run by the IPython evaluator due to the fact that having so many network-accessing functions slows down the documentation build. If you spot an error or an example that doesn’t run, please do not hesitate to report it over on pandas GitHub issues page. Read a URL and match a table that contains specific text: match = "Metcalf Bank" df_list = pd.read_html(url, match=match) Specify a header row (by default <th> or <td> elements located within a <thead> are used to form the column index, if multiple rows are contained within <thead> then a MultiIndex is created); if specified, the header row is taken from the data minus the parsed header elements (<th> elements). dfs = pd.read_html(url, header=0) Specify an index column: dfs = pd.read_html(url, index_col=0) Specify a number of rows to skip: dfs = pd.read_html(url, skiprows=0) Specify a number of rows to skip using a list (range works as well): dfs = pd.read_html(url, skiprows=range(2)) Specify an HTML attribute: dfs1 = pd.read_html(url, attrs={"id": "table"}) dfs2 = pd.read_html(url, attrs={"class": "sortable"}) print(np.array_equal(dfs1[0], dfs2[0])) # Should be True Specify values that should be converted to NaN: dfs = pd.read_html(url, na_values=["No Acquirer"]) Specify whether to keep the default set of NaN values: dfs = pd.read_html(url, keep_default_na=False) Specify converters for columns. This is useful for numerical text data that has leading zeros. By default columns that are numerical are cast to numeric types and the leading zeros are lost. To avoid this, we can convert these columns to strings. url_mcc = "https://en.wikipedia.org/wiki/Mobile_country_code" dfs = pd.read_html( url_mcc, match="Telekom Albania", header=0, converters={"MNC": str}, ) Use some combination of the above: dfs = pd.read_html(url, match="Metcalf Bank", index_col=0) Read in pandas to_html output (with some loss of floating point precision): df = pd.DataFrame(np.random.randn(2, 2)) s = df.to_html(float_format="{0:.40g}".format) dfin = pd.read_html(s, index_col=0) The lxml backend will raise an error on a failed parse if that is the only parser you provide. If you only have a single parser you can provide just a string, but it is considered good practice to pass a list with one string if, for example, the function expects a sequence of strings. You may use: dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor=["lxml"]) Or you could pass flavor='lxml' without a list: dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor="lxml") However, if you have bs4 and html5lib installed and pass None or ['lxml', 'bs4'] then the parse will most likely succeed. Note that as soon as a parse succeeds, the function will return. dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor=["lxml", "bs4"]) Links can be extracted from cells along with the text using extract_links="all". In [323]: html_table = """ .....: <table> .....: <tr> .....: <th>GitHub</th> .....: </tr> .....: <tr> .....: <td><a href="https://github.com/pandas-dev/pandas">pandas</a></td> .....: </tr> .....: </table> .....: """ .....: In [324]: df = pd.read_html( .....: html_table, .....: extract_links="all" .....: )[0] .....: In [325]: df Out[325]: (GitHub, None) 0 (pandas, https://github.com/pandas-dev/pandas) In [326]: df[("GitHub", None)] Out[326]: 0 (pandas, https://github.com/pandas-dev/pandas) Name: (GitHub, None), dtype: object In [327]: df[("GitHub", None)].str[1] Out[327]: 0 https://github.com/pandas-dev/pandas Name: (GitHub, None), dtype: object New in version 1.5.0. Writing to HTML files# DataFrame objects have an instance method to_html which renders the contents of the DataFrame as an HTML table. The function arguments are as in the method to_string described above. Note Not all of the possible options for DataFrame.to_html are shown here for brevity’s sake. See to_html() for the full set of options. Note In an HTML-rendering supported environment like a Jupyter Notebook, display(HTML(...))` will render the raw HTML into the environment. In [328]: from IPython.display import display, HTML In [329]: df = pd.DataFrame(np.random.randn(2, 2)) In [330]: df Out[330]: 0 1 0 0.070319 1.773907 1 0.253908 0.414581 In [331]: html = df.to_html() In [332]: print(html) # raw html <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <th>1</th> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> In [333]: display(HTML(html)) <IPython.core.display.HTML object> The columns argument will limit the columns shown: In [334]: html = df.to_html(columns=[0]) In [335]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> </tr> <tr> <th>1</th> <td>0.253908</td> </tr> </tbody> </table> In [336]: display(HTML(html)) <IPython.core.display.HTML object> float_format takes a Python callable to control the precision of floating point values: In [337]: html = df.to_html(float_format="{0:.10f}".format) In [338]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.0703192665</td> <td>1.7739074228</td> </tr> <tr> <th>1</th> <td>0.2539083433</td> <td>0.4145805920</td> </tr> </tbody> </table> In [339]: display(HTML(html)) <IPython.core.display.HTML object> bold_rows will make the row labels bold by default, but you can turn that off: In [340]: html = df.to_html(bold_rows=False) In [341]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <td>1</td> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> In [342]: display(HTML(html)) <IPython.core.display.HTML object> The classes argument provides the ability to give the resulting HTML table CSS classes. Note that these classes are appended to the existing 'dataframe' class. In [343]: print(df.to_html(classes=["awesome_table_class", "even_more_awesome_class"])) <table border="1" class="dataframe awesome_table_class even_more_awesome_class"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <th>1</th> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> The render_links argument provides the ability to add hyperlinks to cells that contain URLs. In [344]: url_df = pd.DataFrame( .....: { .....: "name": ["Python", "pandas"], .....: "url": ["https://www.python.org/", "https://pandas.pydata.org"], .....: } .....: ) .....: In [345]: html = url_df.to_html(render_links=True) In [346]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>name</th> <th>url</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>Python</td> <td><a href="https://www.python.org/" target="_blank">https://www.python.org/</a></td> </tr> <tr> <th>1</th> <td>pandas</td> <td><a href="https://pandas.pydata.org" target="_blank">https://pandas.pydata.org</a></td> </tr> </tbody> </table> In [347]: display(HTML(html)) <IPython.core.display.HTML object> Finally, the escape argument allows you to control whether the “<”, “>” and “&” characters escaped in the resulting HTML (by default it is True). So to get the HTML without escaped characters pass escape=False In [348]: df = pd.DataFrame({"a": list("&<>"), "b": np.random.randn(3)}) Escaped: In [349]: html = df.to_html() In [350]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>a</th> <th>b</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>&amp;</td> <td>0.842321</td> </tr> <tr> <th>1</th> <td>&lt;</td> <td>0.211337</td> </tr> <tr> <th>2</th> <td>&gt;</td> <td>-1.055427</td> </tr> </tbody> </table> In [351]: display(HTML(html)) <IPython.core.display.HTML object> Not escaped: In [352]: html = df.to_html(escape=False) In [353]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>a</th> <th>b</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>&</td> <td>0.842321</td> </tr> <tr> <th>1</th> <td><</td> <td>0.211337</td> </tr> <tr> <th>2</th> <td>></td> <td>-1.055427</td> </tr> </tbody> </table> In [354]: display(HTML(html)) <IPython.core.display.HTML object> Note Some browsers may not show a difference in the rendering of the previous two HTML tables. HTML Table Parsing Gotchas# There are some versioning issues surrounding the libraries that are used to parse HTML tables in the top-level pandas io function read_html. Issues with lxml Benefits lxml is very fast. lxml requires Cython to install correctly. Drawbacks lxml does not make any guarantees about the results of its parse unless it is given strictly valid markup. In light of the above, we have chosen to allow you, the user, to use the lxml backend, but this backend will use html5lib if lxml fails to parse It is therefore highly recommended that you install both BeautifulSoup4 and html5lib, so that you will still get a valid result (provided everything else is valid) even if lxml fails. Issues with BeautifulSoup4 using lxml as a backend The above issues hold here as well since BeautifulSoup4 is essentially just a wrapper around a parser backend. Issues with BeautifulSoup4 using html5lib as a backend Benefits html5lib is far more lenient than lxml and consequently deals with real-life markup in a much saner way rather than just, e.g., dropping an element without notifying you. html5lib generates valid HTML5 markup from invalid markup automatically. This is extremely important for parsing HTML tables, since it guarantees a valid document. However, that does NOT mean that it is “correct”, since the process of fixing markup does not have a single definition. html5lib is pure Python and requires no additional build steps beyond its own installation. Drawbacks The biggest drawback to using html5lib is that it is slow as molasses. However consider the fact that many tables on the web are not big enough for the parsing algorithm runtime to matter. It is more likely that the bottleneck will be in the process of reading the raw text from the URL over the web, i.e., IO (input-output). For very large tables, this might not be true. LaTeX# New in version 1.3.0. Currently there are no methods to read from LaTeX, only output methods. Writing to LaTeX files# Note DataFrame and Styler objects currently have a to_latex method. We recommend using the Styler.to_latex() method over DataFrame.to_latex() due to the former’s greater flexibility with conditional styling, and the latter’s possible future deprecation. Review the documentation for Styler.to_latex, which gives examples of conditional styling and explains the operation of its keyword arguments. For simple application the following pattern is sufficient. In [355]: df = pd.DataFrame([[1, 2], [3, 4]], index=["a", "b"], columns=["c", "d"]) In [356]: print(df.style.to_latex()) \begin{tabular}{lrr} & c & d \\ a & 1 & 2 \\ b & 3 & 4 \\ \end{tabular} To format values before output, chain the Styler.format method. In [357]: print(df.style.format("€ {}").to_latex()) \begin{tabular}{lrr} & c & d \\ a & € 1 & € 2 \\ b & € 3 & € 4 \\ \end{tabular} XML# Reading XML# New in version 1.3.0. The top-level read_xml() function can accept an XML string/file/URL and will parse nodes and attributes into a pandas DataFrame. Note Since there is no standard XML structure where design types can vary in many ways, read_xml works best with flatter, shallow versions. If an XML document is deeply nested, use the stylesheet feature to transform XML into a flatter version. Let’s look at a few examples. Read an XML string: In [358]: xml = """<?xml version="1.0" encoding="UTF-8"?> .....: <bookstore> .....: <book category="cooking"> .....: <title lang="en">Everyday Italian</title> .....: <author>Giada De Laurentiis</author> .....: <year>2005</year> .....: <price>30.00</price> .....: </book> .....: <book category="children"> .....: <title lang="en">Harry Potter</title> .....: <author>J K. Rowling</author> .....: <year>2005</year> .....: <price>29.99</price> .....: </book> .....: <book category="web"> .....: <title lang="en">Learning XML</title> .....: <author>Erik T. Ray</author> .....: <year>2003</year> .....: <price>39.95</price> .....: </book> .....: </bookstore>""" .....: In [359]: df = pd.read_xml(xml) In [360]: df Out[360]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Read a URL with no options: In [361]: df = pd.read_xml("https://www.w3schools.com/xml/books.xml") In [362]: df Out[362]: category title author year price cover 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 None 1 children Harry Potter J K. Rowling 2005 29.99 None 2 web XQuery Kick Start Vaidyanathan Nagarajan 2003 49.99 None 3 web Learning XML Erik T. Ray 2003 39.95 paperback Read in the content of the “books.xml” file and pass it to read_xml as a string: In [363]: file_path = "books.xml" In [364]: with open(file_path, "w") as f: .....: f.write(xml) .....: In [365]: with open(file_path, "r") as f: .....: df = pd.read_xml(f.read()) .....: In [366]: df Out[366]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Read in the content of the “books.xml” as instance of StringIO or BytesIO and pass it to read_xml: In [367]: with open(file_path, "r") as f: .....: sio = StringIO(f.read()) .....: In [368]: df = pd.read_xml(sio) In [369]: df Out[369]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 In [370]: with open(file_path, "rb") as f: .....: bio = BytesIO(f.read()) .....: In [371]: df = pd.read_xml(bio) In [372]: df Out[372]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Even read XML from AWS S3 buckets such as NIH NCBI PMC Article Datasets providing Biomedical and Life Science Jorurnals: In [373]: df = pd.read_xml( .....: "s3://pmc-oa-opendata/oa_comm/xml/all/PMC1236943.xml", .....: xpath=".//journal-meta", .....: ) .....: In [374]: df Out[374]: journal-id journal-title issn publisher 0 Cardiovasc Ultrasound Cardiovascular Ultrasound 1476-7120 NaN With lxml as default parser, you access the full-featured XML library that extends Python’s ElementTree API. One powerful tool is ability to query nodes selectively or conditionally with more expressive XPath: In [375]: df = pd.read_xml(file_path, xpath="//book[year=2005]") In [376]: df Out[376]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 Specify only elements or only attributes to parse: In [377]: df = pd.read_xml(file_path, elems_only=True) In [378]: df Out[378]: title author year price 0 Everyday Italian Giada De Laurentiis 2005 30.00 1 Harry Potter J K. Rowling 2005 29.99 2 Learning XML Erik T. Ray 2003 39.95 In [379]: df = pd.read_xml(file_path, attrs_only=True) In [380]: df Out[380]: category 0 cooking 1 children 2 web XML documents can have namespaces with prefixes and default namespaces without prefixes both of which are denoted with a special attribute xmlns. In order to parse by node under a namespace context, xpath must reference a prefix. For example, below XML contains a namespace with prefix, doc, and URI at https://example.com. In order to parse doc:row nodes, namespaces must be used. In [381]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <doc:data xmlns:doc="https://example.com"> .....: <doc:row> .....: <doc:shape>square</doc:shape> .....: <doc:degrees>360</doc:degrees> .....: <doc:sides>4.0</doc:sides> .....: </doc:row> .....: <doc:row> .....: <doc:shape>circle</doc:shape> .....: <doc:degrees>360</doc:degrees> .....: <doc:sides/> .....: </doc:row> .....: <doc:row> .....: <doc:shape>triangle</doc:shape> .....: <doc:degrees>180</doc:degrees> .....: <doc:sides>3.0</doc:sides> .....: </doc:row> .....: </doc:data>""" .....: In [382]: df = pd.read_xml(xml, .....: xpath="//doc:row", .....: namespaces={"doc": "https://example.com"}) .....: In [383]: df Out[383]: shape degrees sides 0 square 360 4.0 1 circle 360 NaN 2 triangle 180 3.0 Similarly, an XML document can have a default namespace without prefix. Failing to assign a temporary prefix will return no nodes and raise a ValueError. But assigning any temporary name to correct URI allows parsing by nodes. In [384]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <data xmlns="https://example.com"> .....: <row> .....: <shape>square</shape> .....: <degrees>360</degrees> .....: <sides>4.0</sides> .....: </row> .....: <row> .....: <shape>circle</shape> .....: <degrees>360</degrees> .....: <sides/> .....: </row> .....: <row> .....: <shape>triangle</shape> .....: <degrees>180</degrees> .....: <sides>3.0</sides> .....: </row> .....: </data>""" .....: In [385]: df = pd.read_xml(xml, .....: xpath="//pandas:row", .....: namespaces={"pandas": "https://example.com"}) .....: In [386]: df Out[386]: shape degrees sides 0 square 360 4.0 1 circle 360 NaN 2 triangle 180 3.0 However, if XPath does not reference node names such as default, /*, then namespaces is not required. With lxml as parser, you can flatten nested XML documents with an XSLT script which also can be string/file/URL types. As background, XSLT is a special-purpose language written in a special XML file that can transform original XML documents into other XML, HTML, even text (CSV, JSON, etc.) using an XSLT processor. For example, consider this somewhat nested structure of Chicago “L” Rides where station and rides elements encapsulate data in their own sections. With below XSLT, lxml can transform original nested document into a flatter output (as shown below for demonstration) for easier parse into DataFrame: In [387]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <response> .....: <row> .....: <station id="40850" name="Library"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>864.2</avg_weekday_rides> .....: <avg_saturday_rides>534</avg_saturday_rides> .....: <avg_sunday_holiday_rides>417.2</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: <row> .....: <station id="41700" name="Washington/Wabash"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>2707.4</avg_weekday_rides> .....: <avg_saturday_rides>1909.8</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1438.6</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: <row> .....: <station id="40380" name="Clark/Lake"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>2949.6</avg_weekday_rides> .....: <avg_saturday_rides>1657</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1453.8</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: </response>""" .....: In [388]: xsl = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> .....: <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/> .....: <xsl:strip-space elements="*"/> .....: <xsl:template match="/response"> .....: <xsl:copy> .....: <xsl:apply-templates select="row"/> .....: </xsl:copy> .....: </xsl:template> .....: <xsl:template match="row"> .....: <xsl:copy> .....: <station_id><xsl:value-of select="station/@id"/></station_id> .....: <station_name><xsl:value-of select="station/@name"/></station_name> .....: <xsl:copy-of select="month|rides/*"/> .....: </xsl:copy> .....: </xsl:template> .....: </xsl:stylesheet>""" .....: In [389]: output = """<?xml version='1.0' encoding='utf-8'?> .....: <response> .....: <row> .....: <station_id>40850</station_id> .....: <station_name>Library</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>864.2</avg_weekday_rides> .....: <avg_saturday_rides>534</avg_saturday_rides> .....: <avg_sunday_holiday_rides>417.2</avg_sunday_holiday_rides> .....: </row> .....: <row> .....: <station_id>41700</station_id> .....: <station_name>Washington/Wabash</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>2707.4</avg_weekday_rides> .....: <avg_saturday_rides>1909.8</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1438.6</avg_sunday_holiday_rides> .....: </row> .....: <row> .....: <station_id>40380</station_id> .....: <station_name>Clark/Lake</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>2949.6</avg_weekday_rides> .....: <avg_saturday_rides>1657</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1453.8</avg_sunday_holiday_rides> .....: </row> .....: </response>""" .....: In [390]: df = pd.read_xml(xml, stylesheet=xsl) In [391]: df Out[391]: station_id station_name ... avg_saturday_rides avg_sunday_holiday_rides 0 40850 Library ... 534.0 417.2 1 41700 Washington/Wabash ... 1909.8 1438.6 2 40380 Clark/Lake ... 1657.0 1453.8 [3 rows x 6 columns] For very large XML files that can range in hundreds of megabytes to gigabytes, pandas.read_xml() supports parsing such sizeable files using lxml’s iterparse and etree’s iterparse which are memory-efficient methods to iterate through an XML tree and extract specific elements and attributes. without holding entire tree in memory. New in version 1.5.0. To use this feature, you must pass a physical XML file path into read_xml and use the iterparse argument. Files should not be compressed or point to online sources but stored on local disk. Also, iterparse should be a dictionary where the key is the repeating nodes in document (which become the rows) and the value is a list of any element or attribute that is a descendant (i.e., child, grandchild) of repeating node. Since XPath is not used in this method, descendants do not need to share same relationship with one another. Below shows example of reading in Wikipedia’s very large (12 GB+) latest article data dump. In [1]: df = pd.read_xml( ... "/path/to/downloaded/enwikisource-latest-pages-articles.xml", ... iterparse = {"page": ["title", "ns", "id"]} ... ) ... df Out[2]: title ns id 0 Gettysburg Address 0 21450 1 Main Page 0 42950 2 Declaration by United Nations 0 8435 3 Constitution of the United States of America 0 8435 4 Declaration of Independence (Israel) 0 17858 ... ... ... ... 3578760 Page:Black cat 1897 07 v2 n10.pdf/17 104 219649 3578761 Page:Black cat 1897 07 v2 n10.pdf/43 104 219649 3578762 Page:Black cat 1897 07 v2 n10.pdf/44 104 219649 3578763 The History of Tom Jones, a Foundling/Book IX 0 12084291 3578764 Page:Shakespeare of Stratford (1926) Yale.djvu/91 104 21450 [3578765 rows x 3 columns] Writing XML# New in version 1.3.0. DataFrame objects have an instance method to_xml which renders the contents of the DataFrame as an XML document. Note This method does not support special properties of XML including DTD, CData, XSD schemas, processing instructions, comments, and others. Only namespaces at the root level is supported. However, stylesheet allows design changes after initial output. Let’s look at a few examples. Write an XML without options: In [392]: geom_df = pd.DataFrame( .....: { .....: "shape": ["square", "circle", "triangle"], .....: "degrees": [360, 360, 180], .....: "sides": [4, np.nan, 3], .....: } .....: ) .....: In [393]: print(geom_df.to_xml()) <?xml version='1.0' encoding='utf-8'?> <data> <row> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </row> <row> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </row> <row> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Write an XML with new root and row name: In [394]: print(geom_df.to_xml(root_name="geometry", row_name="objects")) <?xml version='1.0' encoding='utf-8'?> <geometry> <objects> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </objects> <objects> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </objects> <objects> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </objects> </geometry> Write an attribute-centric XML: In [395]: print(geom_df.to_xml(attr_cols=geom_df.columns.tolist())) <?xml version='1.0' encoding='utf-8'?> <data> <row index="0" shape="square" degrees="360" sides="4.0"/> <row index="1" shape="circle" degrees="360"/> <row index="2" shape="triangle" degrees="180" sides="3.0"/> </data> Write a mix of elements and attributes: In [396]: print( .....: geom_df.to_xml( .....: index=False, .....: attr_cols=['shape'], .....: elem_cols=['degrees', 'sides']) .....: ) .....: <?xml version='1.0' encoding='utf-8'?> <data> <row shape="square"> <degrees>360</degrees> <sides>4.0</sides> </row> <row shape="circle"> <degrees>360</degrees> <sides/> </row> <row shape="triangle"> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Any DataFrames with hierarchical columns will be flattened for XML element names with levels delimited by underscores: In [397]: ext_geom_df = pd.DataFrame( .....: { .....: "type": ["polygon", "other", "polygon"], .....: "shape": ["square", "circle", "triangle"], .....: "degrees": [360, 360, 180], .....: "sides": [4, np.nan, 3], .....: } .....: ) .....: In [398]: pvt_df = ext_geom_df.pivot_table(index='shape', .....: columns='type', .....: values=['degrees', 'sides'], .....: aggfunc='sum') .....: In [399]: pvt_df Out[399]: degrees sides type other polygon other polygon shape circle 360.0 NaN 0.0 NaN square NaN 360.0 NaN 4.0 triangle NaN 180.0 NaN 3.0 In [400]: print(pvt_df.to_xml()) <?xml version='1.0' encoding='utf-8'?> <data> <row> <shape>circle</shape> <degrees_other>360.0</degrees_other> <degrees_polygon/> <sides_other>0.0</sides_other> <sides_polygon/> </row> <row> <shape>square</shape> <degrees_other/> <degrees_polygon>360.0</degrees_polygon> <sides_other/> <sides_polygon>4.0</sides_polygon> </row> <row> <shape>triangle</shape> <degrees_other/> <degrees_polygon>180.0</degrees_polygon> <sides_other/> <sides_polygon>3.0</sides_polygon> </row> </data> Write an XML with default namespace: In [401]: print(geom_df.to_xml(namespaces={"": "https://example.com"})) <?xml version='1.0' encoding='utf-8'?> <data xmlns="https://example.com"> <row> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </row> <row> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </row> <row> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Write an XML with namespace prefix: In [402]: print( .....: geom_df.to_xml(namespaces={"doc": "https://example.com"}, .....: prefix="doc") .....: ) .....: <?xml version='1.0' encoding='utf-8'?> <doc:data xmlns:doc="https://example.com"> <doc:row> <doc:index>0</doc:index> <doc:shape>square</doc:shape> <doc:degrees>360</doc:degrees> <doc:sides>4.0</doc:sides> </doc:row> <doc:row> <doc:index>1</doc:index> <doc:shape>circle</doc:shape> <doc:degrees>360</doc:degrees> <doc:sides/> </doc:row> <doc:row> <doc:index>2</doc:index> <doc:shape>triangle</doc:shape> <doc:degrees>180</doc:degrees> <doc:sides>3.0</doc:sides> </doc:row> </doc:data> Write an XML without declaration or pretty print: In [403]: print( .....: geom_df.to_xml(xml_declaration=False, .....: pretty_print=False) .....: ) .....: <data><row><index>0</index><shape>square</shape><degrees>360</degrees><sides>4.0</sides></row><row><index>1</index><shape>circle</shape><degrees>360</degrees><sides/></row><row><index>2</index><shape>triangle</shape><degrees>180</degrees><sides>3.0</sides></row></data> Write an XML and transform with stylesheet: In [404]: xsl = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> .....: <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/> .....: <xsl:strip-space elements="*"/> .....: <xsl:template match="/data"> .....: <geometry> .....: <xsl:apply-templates select="row"/> .....: </geometry> .....: </xsl:template> .....: <xsl:template match="row"> .....: <object index="{index}"> .....: <xsl:if test="shape!='circle'"> .....: <xsl:attribute name="type">polygon</xsl:attribute> .....: </xsl:if> .....: <xsl:copy-of select="shape"/> .....: <property> .....: <xsl:copy-of select="degrees|sides"/> .....: </property> .....: </object> .....: </xsl:template> .....: </xsl:stylesheet>""" .....: In [405]: print(geom_df.to_xml(stylesheet=xsl)) <?xml version="1.0"?> <geometry> <object index="0" type="polygon"> <shape>square</shape> <property> <degrees>360</degrees> <sides>4.0</sides> </property> </object> <object index="1"> <shape>circle</shape> <property> <degrees>360</degrees> <sides/> </property> </object> <object index="2" type="polygon"> <shape>triangle</shape> <property> <degrees>180</degrees> <sides>3.0</sides> </property> </object> </geometry> XML Final Notes# All XML documents adhere to W3C specifications. Both etree and lxml parsers will fail to parse any markup document that is not well-formed or follows XML syntax rules. Do be aware HTML is not an XML document unless it follows XHTML specs. However, other popular markup types including KML, XAML, RSS, MusicML, MathML are compliant XML schemas. For above reason, if your application builds XML prior to pandas operations, use appropriate DOM libraries like etree and lxml to build the necessary document and not by string concatenation or regex adjustments. Always remember XML is a special text file with markup rules. With very large XML files (several hundred MBs to GBs), XPath and XSLT can become memory-intensive operations. Be sure to have enough available RAM for reading and writing to large XML files (roughly about 5 times the size of text). Because XSLT is a programming language, use it with caution since such scripts can pose a security risk in your environment and can run large or infinite recursive operations. Always test scripts on small fragments before full run. The etree parser supports all functionality of both read_xml and to_xml except for complex XPath and any XSLT. Though limited in features, etree is still a reliable and capable parser and tree builder. Its performance may trail lxml to a certain degree for larger files but relatively unnoticeable on small to medium size files. Excel files# The read_excel() method can read Excel 2007+ (.xlsx) files using the openpyxl Python module. Excel 2003 (.xls) files can be read using xlrd. Binary Excel (.xlsb) files can be read using pyxlsb. The to_excel() instance method is used for saving a DataFrame to Excel. Generally the semantics are similar to working with csv data. See the cookbook for some advanced strategies. Warning The xlwt package for writing old-style .xls excel files is no longer maintained. The xlrd package is now only for reading old-style .xls files. Before pandas 1.3.0, the default argument engine=None to read_excel() would result in using the xlrd engine in many cases, including new Excel 2007+ (.xlsx) files. pandas will now default to using the openpyxl engine. It is strongly encouraged to install openpyxl to read Excel 2007+ (.xlsx) files. Please do not report issues when using ``xlrd`` to read ``.xlsx`` files. This is no longer supported, switch to using openpyxl instead. Attempting to use the xlwt engine will raise a FutureWarning unless the option io.excel.xls.writer is set to "xlwt". While this option is now deprecated and will also raise a FutureWarning, it can be globally set and the warning suppressed. Users are recommended to write .xlsx files using the openpyxl engine instead. Reading Excel files# In the most basic use-case, read_excel takes a path to an Excel file, and the sheet_name indicating which sheet to parse. # Returns a DataFrame pd.read_excel("path_to_file.xls", sheet_name="Sheet1") ExcelFile class# To facilitate working with multiple sheets from the same file, the ExcelFile class can be used to wrap the file and can be passed into read_excel There will be a performance benefit for reading multiple sheets as the file is read into memory only once. xlsx = pd.ExcelFile("path_to_file.xls") df = pd.read_excel(xlsx, "Sheet1") The ExcelFile class can also be used as a context manager. with pd.ExcelFile("path_to_file.xls") as xls: df1 = pd.read_excel(xls, "Sheet1") df2 = pd.read_excel(xls, "Sheet2") The sheet_names property will generate a list of the sheet names in the file. The primary use-case for an ExcelFile is parsing multiple sheets with different parameters: data = {} # For when Sheet1's format differs from Sheet2 with pd.ExcelFile("path_to_file.xls") as xls: data["Sheet1"] = pd.read_excel(xls, "Sheet1", index_col=None, na_values=["NA"]) data["Sheet2"] = pd.read_excel(xls, "Sheet2", index_col=1) Note that if the same parsing parameters are used for all sheets, a list of sheet names can simply be passed to read_excel with no loss in performance. # using the ExcelFile class data = {} with pd.ExcelFile("path_to_file.xls") as xls: data["Sheet1"] = pd.read_excel(xls, "Sheet1", index_col=None, na_values=["NA"]) data["Sheet2"] = pd.read_excel(xls, "Sheet2", index_col=None, na_values=["NA"]) # equivalent using the read_excel function data = pd.read_excel( "path_to_file.xls", ["Sheet1", "Sheet2"], index_col=None, na_values=["NA"] ) ExcelFile can also be called with a xlrd.book.Book object as a parameter. This allows the user to control how the excel file is read. For example, sheets can be loaded on demand by calling xlrd.open_workbook() with on_demand=True. import xlrd xlrd_book = xlrd.open_workbook("path_to_file.xls", on_demand=True) with pd.ExcelFile(xlrd_book) as xls: df1 = pd.read_excel(xls, "Sheet1") df2 = pd.read_excel(xls, "Sheet2") Specifying sheets# Note The second argument is sheet_name, not to be confused with ExcelFile.sheet_names. Note An ExcelFile’s attribute sheet_names provides access to a list of sheets. The arguments sheet_name allows specifying the sheet or sheets to read. The default value for sheet_name is 0, indicating to read the first sheet Pass a string to refer to the name of a particular sheet in the workbook. Pass an integer to refer to the index of a sheet. Indices follow Python convention, beginning at 0. Pass a list of either strings or integers, to return a dictionary of specified sheets. Pass a None to return a dictionary of all available sheets. # Returns a DataFrame pd.read_excel("path_to_file.xls", "Sheet1", index_col=None, na_values=["NA"]) Using the sheet index: # Returns a DataFrame pd.read_excel("path_to_file.xls", 0, index_col=None, na_values=["NA"]) Using all default values: # Returns a DataFrame pd.read_excel("path_to_file.xls") Using None to get all sheets: # Returns a dictionary of DataFrames pd.read_excel("path_to_file.xls", sheet_name=None) Using a list to get multiple sheets: # Returns the 1st and 4th sheet, as a dictionary of DataFrames. pd.read_excel("path_to_file.xls", sheet_name=["Sheet1", 3]) read_excel can read more than one sheet, by setting sheet_name to either a list of sheet names, a list of sheet positions, or None to read all sheets. Sheets can be specified by sheet index or sheet name, using an integer or string, respectively. Reading a MultiIndex# read_excel can read a MultiIndex index, by passing a list of columns to index_col and a MultiIndex column by passing a list of rows to header. If either the index or columns have serialized level names those will be read in as well by specifying the rows/columns that make up the levels. For example, to read in a MultiIndex index without names: In [406]: df = pd.DataFrame( .....: {"a": [1, 2, 3, 4], "b": [5, 6, 7, 8]}, .....: index=pd.MultiIndex.from_product([["a", "b"], ["c", "d"]]), .....: ) .....: In [407]: df.to_excel("path_to_file.xlsx") In [408]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1]) In [409]: df Out[409]: a b a c 1 5 d 2 6 b c 3 7 d 4 8 If the index has level names, they will parsed as well, using the same parameters. In [410]: df.index = df.index.set_names(["lvl1", "lvl2"]) In [411]: df.to_excel("path_to_file.xlsx") In [412]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1]) In [413]: df Out[413]: a b lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 If the source file has both MultiIndex index and columns, lists specifying each should be passed to index_col and header: In [414]: df.columns = pd.MultiIndex.from_product([["a"], ["b", "d"]], names=["c1", "c2"]) In [415]: df.to_excel("path_to_file.xlsx") In [416]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1], header=[0, 1]) In [417]: df Out[417]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 Missing values in columns specified in index_col will be forward filled to allow roundtripping with to_excel for merged_cells=True. To avoid forward filling the missing values use set_index after reading the data instead of index_col. Parsing specific columns# It is often the case that users will insert columns to do temporary computations in Excel and you may not want to read in those columns. read_excel takes a usecols keyword to allow you to specify a subset of columns to parse. Changed in version 1.0.0. Passing in an integer for usecols will no longer work. Please pass in a list of ints from 0 to usecols inclusive instead. You can specify a comma-delimited set of Excel columns and ranges as a string: pd.read_excel("path_to_file.xls", "Sheet1", usecols="A,C:E") If usecols is a list of integers, then it is assumed to be the file column indices to be parsed. pd.read_excel("path_to_file.xls", "Sheet1", usecols=[0, 2, 3]) Element order is ignored, so usecols=[0, 1] is the same as [1, 0]. If usecols is a list of strings, it is assumed that each string corresponds to a column name provided either by the user in names or inferred from the document header row(s). Those strings define which columns will be parsed: pd.read_excel("path_to_file.xls", "Sheet1", usecols=["foo", "bar"]) Element order is ignored, so usecols=['baz', 'joe'] is the same as ['joe', 'baz']. If usecols is callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True. pd.read_excel("path_to_file.xls", "Sheet1", usecols=lambda x: x.isalpha()) Parsing dates# Datetime-like values are normally automatically converted to the appropriate dtype when reading the excel file. But if you have a column of strings that look like dates (but are not actually formatted as dates in excel), you can use the parse_dates keyword to parse those strings to datetimes: pd.read_excel("path_to_file.xls", "Sheet1", parse_dates=["date_strings"]) Cell converters# It is possible to transform the contents of Excel cells via the converters option. For instance, to convert a column to boolean: pd.read_excel("path_to_file.xls", "Sheet1", converters={"MyBools": bool}) This options handles missing values and treats exceptions in the converters as missing data. Transformations are applied cell by cell rather than to the column as a whole, so the array dtype is not guaranteed. For instance, a column of integers with missing values cannot be transformed to an array with integer dtype, because NaN is strictly a float. You can manually mask missing data to recover integer dtype: def cfun(x): return int(x) if x else -1 pd.read_excel("path_to_file.xls", "Sheet1", converters={"MyInts": cfun}) Dtype specifications# As an alternative to converters, the type for an entire column can be specified using the dtype keyword, which takes a dictionary mapping column names to types. To interpret data with no type inference, use the type str or object. pd.read_excel("path_to_file.xls", dtype={"MyInts": "int64", "MyText": str}) Writing Excel files# Writing Excel files to disk# To write a DataFrame object to a sheet of an Excel file, you can use the to_excel instance method. The arguments are largely the same as to_csv described above, the first argument being the name of the excel file, and the optional second argument the name of the sheet to which the DataFrame should be written. For example: df.to_excel("path_to_file.xlsx", sheet_name="Sheet1") Files with a .xls extension will be written using xlwt and those with a .xlsx extension will be written using xlsxwriter (if available) or openpyxl. The DataFrame will be written in a way that tries to mimic the REPL output. The index_label will be placed in the second row instead of the first. You can place it in the first row by setting the merge_cells option in to_excel() to False: df.to_excel("path_to_file.xlsx", index_label="label", merge_cells=False) In order to write separate DataFrames to separate sheets in a single Excel file, one can pass an ExcelWriter. with pd.ExcelWriter("path_to_file.xlsx") as writer: df1.to_excel(writer, sheet_name="Sheet1") df2.to_excel(writer, sheet_name="Sheet2") Writing Excel files to memory# pandas supports writing Excel files to buffer-like objects such as StringIO or BytesIO using ExcelWriter. from io import BytesIO bio = BytesIO() # By setting the 'engine' in the ExcelWriter constructor. writer = pd.ExcelWriter(bio, engine="xlsxwriter") df.to_excel(writer, sheet_name="Sheet1") # Save the workbook writer.save() # Seek to the beginning and read to copy the workbook to a variable in memory bio.seek(0) workbook = bio.read() Note engine is optional but recommended. Setting the engine determines the version of workbook produced. Setting engine='xlrd' will produce an Excel 2003-format workbook (xls). Using either 'openpyxl' or 'xlsxwriter' will produce an Excel 2007-format workbook (xlsx). If omitted, an Excel 2007-formatted workbook is produced. Excel writer engines# Deprecated since version 1.2.0: As the xlwt package is no longer maintained, the xlwt engine will be removed from a future version of pandas. This is the only engine in pandas that supports writing to .xls files. pandas chooses an Excel writer via two methods: the engine keyword argument the filename extension (via the default specified in config options) By default, pandas uses the XlsxWriter for .xlsx, openpyxl for .xlsm, and xlwt for .xls files. If you have multiple engines installed, you can set the default engine through setting the config options io.excel.xlsx.writer and io.excel.xls.writer. pandas will fall back on openpyxl for .xlsx files if Xlsxwriter is not available. To specify which writer you want to use, you can pass an engine keyword argument to to_excel and to ExcelWriter. The built-in engines are: openpyxl: version 2.4 or higher is required xlsxwriter xlwt # By setting the 'engine' in the DataFrame 'to_excel()' methods. df.to_excel("path_to_file.xlsx", sheet_name="Sheet1", engine="xlsxwriter") # By setting the 'engine' in the ExcelWriter constructor. writer = pd.ExcelWriter("path_to_file.xlsx", engine="xlsxwriter") # Or via pandas configuration. from pandas import options # noqa: E402 options.io.excel.xlsx.writer = "xlsxwriter" df.to_excel("path_to_file.xlsx", sheet_name="Sheet1") Style and formatting# The look and feel of Excel worksheets created from pandas can be modified using the following parameters on the DataFrame’s to_excel method. float_format : Format string for floating point numbers (default None). freeze_panes : A tuple of two integers representing the bottommost row and rightmost column to freeze. Each of these parameters is one-based, so (1, 1) will freeze the first row and first column (default None). Using the Xlsxwriter engine provides many options for controlling the format of an Excel worksheet created with the to_excel method. Excellent examples can be found in the Xlsxwriter documentation here: https://xlsxwriter.readthedocs.io/working_with_pandas.html OpenDocument Spreadsheets# New in version 0.25. The read_excel() method can also read OpenDocument spreadsheets using the odfpy module. The semantics and features for reading OpenDocument spreadsheets match what can be done for Excel files using engine='odf'. # Returns a DataFrame pd.read_excel("path_to_file.ods", engine="odf") Note Currently pandas only supports reading OpenDocument spreadsheets. Writing is not implemented. Binary Excel (.xlsb) files# New in version 1.0.0. The read_excel() method can also read binary Excel files using the pyxlsb module. The semantics and features for reading binary Excel files mostly match what can be done for Excel files using engine='pyxlsb'. pyxlsb does not recognize datetime types in files and will return floats instead. # Returns a DataFrame pd.read_excel("path_to_file.xlsb", engine="pyxlsb") Note Currently pandas only supports reading binary Excel files. Writing is not implemented. Clipboard# A handy way to grab data is to use the read_clipboard() method, which takes the contents of the clipboard buffer and passes them to the read_csv method. For instance, you can copy the following text to the clipboard (CTRL-C on many operating systems): A B C x 1 4 p y 2 5 q z 3 6 r And then import the data directly to a DataFrame by calling: >>> clipdf = pd.read_clipboard() >>> clipdf A B C x 1 4 p y 2 5 q z 3 6 r The to_clipboard method can be used to write the contents of a DataFrame to the clipboard. Following which you can paste the clipboard contents into other applications (CTRL-V on many operating systems). Here we illustrate writing a DataFrame into clipboard and reading it back. >>> df = pd.DataFrame( ... {"A": [1, 2, 3], "B": [4, 5, 6], "C": ["p", "q", "r"]}, index=["x", "y", "z"] ... ) >>> df A B C x 1 4 p y 2 5 q z 3 6 r >>> df.to_clipboard() >>> pd.read_clipboard() A B C x 1 4 p y 2 5 q z 3 6 r We can see that we got the same content back, which we had earlier written to the clipboard. Note You may need to install xclip or xsel (with PyQt5, PyQt4 or qtpy) on Linux to use these methods. Pickling# All pandas objects are equipped with to_pickle methods which use Python’s cPickle module to save data structures to disk using the pickle format. In [418]: df Out[418]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 In [419]: df.to_pickle("foo.pkl") The read_pickle function in the pandas namespace can be used to load any pickled pandas object (or any other pickled object) from file: In [420]: pd.read_pickle("foo.pkl") Out[420]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 Warning Loading pickled data received from untrusted sources can be unsafe. See: https://docs.python.org/3/library/pickle.html Warning read_pickle() is only guaranteed backwards compatible back to pandas version 0.20.3 Compressed pickle files# read_pickle(), DataFrame.to_pickle() and Series.to_pickle() can read and write compressed pickle files. The compression types of gzip, bz2, xz, zstd are supported for reading and writing. The zip file format only supports reading and must contain only one data file to be read. The compression type can be an explicit parameter or be inferred from the file extension. If ‘infer’, then use gzip, bz2, zip, xz, zstd if filename ends in '.gz', '.bz2', '.zip', '.xz', or '.zst', respectively. The compression parameter can also be a dict in order to pass options to the compression protocol. It must have a 'method' key set to the name of the compression protocol, which must be one of {'zip', 'gzip', 'bz2', 'xz', 'zstd'}. All other key-value pairs are passed to the underlying compression library. In [421]: df = pd.DataFrame( .....: { .....: "A": np.random.randn(1000), .....: "B": "foo", .....: "C": pd.date_range("20130101", periods=1000, freq="s"), .....: } .....: ) .....: In [422]: df Out[422]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] Using an explicit compression type: In [423]: df.to_pickle("data.pkl.compress", compression="gzip") In [424]: rt = pd.read_pickle("data.pkl.compress", compression="gzip") In [425]: rt Out[425]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] Inferring compression type from the extension: In [426]: df.to_pickle("data.pkl.xz", compression="infer") In [427]: rt = pd.read_pickle("data.pkl.xz", compression="infer") In [428]: rt Out[428]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] The default is to ‘infer’: In [429]: df.to_pickle("data.pkl.gz") In [430]: rt = pd.read_pickle("data.pkl.gz") In [431]: rt Out[431]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] In [432]: df["A"].to_pickle("s1.pkl.bz2") In [433]: rt = pd.read_pickle("s1.pkl.bz2") In [434]: rt Out[434]: 0 -0.828876 1 -0.110383 2 2.357598 3 -1.620073 4 0.440903 ... 995 -1.177365 996 1.236988 997 0.743946 998 -0.533097 999 -0.140850 Name: A, Length: 1000, dtype: float64 Passing options to the compression protocol in order to speed up compression: In [435]: df.to_pickle("data.pkl.gz", compression={"method": "gzip", "compresslevel": 1}) msgpack# pandas support for msgpack has been removed in version 1.0.0. It is recommended to use pickle instead. Alternatively, you can also the Arrow IPC serialization format for on-the-wire transmission of pandas objects. For documentation on pyarrow, see here. HDF5 (PyTables)# HDFStore is a dict-like object which reads and writes pandas using the high performance HDF5 format using the excellent PyTables library. See the cookbook for some advanced strategies Warning pandas uses PyTables for reading and writing HDF5 files, which allows serializing object-dtype data with pickle. Loading pickled data received from untrusted sources can be unsafe. See: https://docs.python.org/3/library/pickle.html for more. In [436]: store = pd.HDFStore("store.h5") In [437]: print(store) <class 'pandas.io.pytables.HDFStore'> File path: store.h5 Objects can be written to the file just like adding key-value pairs to a dict: In [438]: index = pd.date_range("1/1/2000", periods=8) In [439]: s = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"]) In [440]: df = pd.DataFrame(np.random.randn(8, 3), index=index, columns=["A", "B", "C"]) # store.put('s', s) is an equivalent method In [441]: store["s"] = s In [442]: store["df"] = df In [443]: store Out[443]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 In a current or later Python session, you can retrieve stored objects: # store.get('df') is an equivalent method In [444]: store["df"] Out[444]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 # dotted (attribute) access provides get as well In [445]: store.df Out[445]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Deletion of the object specified by the key: # store.remove('df') is an equivalent method In [446]: del store["df"] In [447]: store Out[447]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 Closing a Store and using a context manager: In [448]: store.close() In [449]: store Out[449]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 In [450]: store.is_open Out[450]: False # Working with, and automatically closing the store using a context manager In [451]: with pd.HDFStore("store.h5") as store: .....: store.keys() .....: Read/write API# HDFStore supports a top-level API using read_hdf for reading and to_hdf for writing, similar to how read_csv and to_csv work. In [452]: df_tl = pd.DataFrame({"A": list(range(5)), "B": list(range(5))}) In [453]: df_tl.to_hdf("store_tl.h5", "table", append=True) In [454]: pd.read_hdf("store_tl.h5", "table", where=["index>2"]) Out[454]: A B 3 3 3 4 4 4 HDFStore will by default not drop rows that are all missing. This behavior can be changed by setting dropna=True. In [455]: df_with_missing = pd.DataFrame( .....: { .....: "col1": [0, np.nan, 2], .....: "col2": [1, np.nan, np.nan], .....: } .....: ) .....: In [456]: df_with_missing Out[456]: col1 col2 0 0.0 1.0 1 NaN NaN 2 2.0 NaN In [457]: df_with_missing.to_hdf("file.h5", "df_with_missing", format="table", mode="w") In [458]: pd.read_hdf("file.h5", "df_with_missing") Out[458]: col1 col2 0 0.0 1.0 1 NaN NaN 2 2.0 NaN In [459]: df_with_missing.to_hdf( .....: "file.h5", "df_with_missing", format="table", mode="w", dropna=True .....: ) .....: In [460]: pd.read_hdf("file.h5", "df_with_missing") Out[460]: col1 col2 0 0.0 1.0 2 2.0 NaN Fixed format# The examples above show storing using put, which write the HDF5 to PyTables in a fixed array format, called the fixed format. These types of stores are not appendable once written (though you can simply remove them and rewrite). Nor are they queryable; they must be retrieved in their entirety. They also do not support dataframes with non-unique column names. The fixed format stores offer very fast writing and slightly faster reading than table stores. This format is specified by default when using put or to_hdf or by format='fixed' or format='f'. Warning A fixed format will raise a TypeError if you try to retrieve using a where: >>> pd.DataFrame(np.random.randn(10, 2)).to_hdf("test_fixed.h5", "df") >>> pd.read_hdf("test_fixed.h5", "df", where="index>5") TypeError: cannot pass a where specification when reading a fixed format. this store must be selected in its entirety Table format# HDFStore supports another PyTables format on disk, the table format. Conceptually a table is shaped very much like a DataFrame, with rows and columns. A table may be appended to in the same or other sessions. In addition, delete and query type operations are supported. This format is specified by format='table' or format='t' to append or put or to_hdf. This format can be set as an option as well pd.set_option('io.hdf.default_format','table') to enable put/append/to_hdf to by default store in the table format. In [461]: store = pd.HDFStore("store.h5") In [462]: df1 = df[0:4] In [463]: df2 = df[4:] # append data (creates a table automatically) In [464]: store.append("df", df1) In [465]: store.append("df", df2) In [466]: store Out[466]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # select the entire object In [467]: store.select("df") Out[467]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 # the type of stored data In [468]: store.root.df._v_attrs.pandas_type Out[468]: 'frame_table' Note You can also create a table by passing format='table' or format='t' to a put operation. Hierarchical keys# Keys to a store can be specified as a string. These can be in a hierarchical path-name like format (e.g. foo/bar/bah), which will generate a hierarchy of sub-stores (or Groups in PyTables parlance). Keys can be specified without the leading ‘/’ and are always absolute (e.g. ‘foo’ refers to ‘/foo’). Removal operations can remove everything in the sub-store and below, so be careful. In [469]: store.put("foo/bar/bah", df) In [470]: store.append("food/orange", df) In [471]: store.append("food/apple", df) In [472]: store Out[472]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # a list of keys are returned In [473]: store.keys() Out[473]: ['/df', '/food/apple', '/food/orange', '/foo/bar/bah'] # remove all nodes under this level In [474]: store.remove("food") In [475]: store Out[475]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 You can walk through the group hierarchy using the walk method which will yield a tuple for each group key along with the relative keys of its contents. In [476]: for (path, subgroups, subkeys) in store.walk(): .....: for subgroup in subgroups: .....: print("GROUP: {}/{}".format(path, subgroup)) .....: for subkey in subkeys: .....: key = "/".join([path, subkey]) .....: print("KEY: {}".format(key)) .....: print(store.get(key)) .....: GROUP: /foo KEY: /df A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 GROUP: /foo/bar KEY: /foo/bar/bah A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Warning Hierarchical keys cannot be retrieved as dotted (attribute) access as described above for items stored under the root node. In [8]: store.foo.bar.bah AttributeError: 'HDFStore' object has no attribute 'foo' # you can directly access the actual PyTables node but using the root node In [9]: store.root.foo.bar.bah Out[9]: /foo/bar/bah (Group) '' children := ['block0_items' (Array), 'block0_values' (Array), 'axis0' (Array), 'axis1' (Array)] Instead, use explicit string based keys: In [477]: store["foo/bar/bah"] Out[477]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Storing types# Storing mixed types in a table# Storing mixed-dtype data is supported. Strings are stored as a fixed-width using the maximum size of the appended column. Subsequent attempts at appending longer strings will raise a ValueError. Passing min_itemsize={`values`: size} as a parameter to append will set a larger minimum for the string columns. Storing floats, strings, ints, bools, datetime64 are currently supported. For string columns, passing nan_rep = 'nan' to append will change the default nan representation on disk (which converts to/from np.nan), this defaults to nan. In [478]: df_mixed = pd.DataFrame( .....: { .....: "A": np.random.randn(8), .....: "B": np.random.randn(8), .....: "C": np.array(np.random.randn(8), dtype="float32"), .....: "string": "string", .....: "int": 1, .....: "bool": True, .....: "datetime64": pd.Timestamp("20010102"), .....: }, .....: index=list(range(8)), .....: ) .....: In [479]: df_mixed.loc[df_mixed.index[3:5], ["A", "B", "string", "datetime64"]] = np.nan In [480]: store.append("df_mixed", df_mixed, min_itemsize={"values": 50}) In [481]: df_mixed1 = store.select("df_mixed") In [482]: df_mixed1 Out[482]: A B C string int bool datetime64 0 1.778161 -0.898283 -0.263043 string 1 True 2001-01-02 1 -0.913867 -0.218499 -0.639244 string 1 True 2001-01-02 2 -0.030004 1.408028 -0.866305 string 1 True 2001-01-02 3 NaN NaN -0.225250 NaN 1 True NaT 4 NaN NaN -0.890978 NaN 1 True NaT 5 0.081323 0.520995 -0.553839 string 1 True 2001-01-02 6 -0.268494 0.620028 -2.762875 string 1 True 2001-01-02 7 0.168016 0.159416 -1.244763 string 1 True 2001-01-02 In [483]: df_mixed1.dtypes.value_counts() Out[483]: float64 2 float32 1 object 1 int64 1 bool 1 datetime64[ns] 1 dtype: int64 # we have provided a minimum string column size In [484]: store.root.df_mixed.table Out[484]: /df_mixed/table (Table(8,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(2,), dflt=0.0, pos=1), "values_block_1": Float32Col(shape=(1,), dflt=0.0, pos=2), "values_block_2": StringCol(itemsize=50, shape=(1,), dflt=b'', pos=3), "values_block_3": Int64Col(shape=(1,), dflt=0, pos=4), "values_block_4": BoolCol(shape=(1,), dflt=False, pos=5), "values_block_5": Int64Col(shape=(1,), dflt=0, pos=6)} byteorder := 'little' chunkshape := (689,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False} Storing MultiIndex DataFrames# Storing MultiIndex DataFrames as tables is very similar to storing/selecting from homogeneous index DataFrames. In [485]: index = pd.MultiIndex( .....: levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], .....: codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], .....: names=["foo", "bar"], .....: ) .....: In [486]: df_mi = pd.DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"]) In [487]: df_mi Out[487]: A B C foo bar foo one -1.280289 0.692545 -0.536722 two 1.005707 0.296917 0.139796 three -1.083889 0.811865 1.648435 bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 baz two 0.183573 0.145277 0.308146 three -1.043530 -0.708145 1.430905 qux one -0.850136 0.813949 1.508891 two -1.556154 0.187597 1.176488 three -1.246093 -0.002726 -0.444249 In [488]: store.append("df_mi", df_mi) In [489]: store.select("df_mi") Out[489]: A B C foo bar foo one -1.280289 0.692545 -0.536722 two 1.005707 0.296917 0.139796 three -1.083889 0.811865 1.648435 bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 baz two 0.183573 0.145277 0.308146 three -1.043530 -0.708145 1.430905 qux one -0.850136 0.813949 1.508891 two -1.556154 0.187597 1.176488 three -1.246093 -0.002726 -0.444249 # the levels are automatically included as data columns In [490]: store.select("df_mi", "foo=bar") Out[490]: A B C foo bar bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 Note The index keyword is reserved and cannot be use as a level name. Querying# Querying a table# select and delete operations have an optional criterion that can be specified to select/delete only a subset of the data. This allows one to have a very large on-disk table and retrieve only a portion of the data. A query is specified using the Term class under the hood, as a boolean expression. index and columns are supported indexers of DataFrames. if data_columns are specified, these can be used as additional indexers. level name in a MultiIndex, with default name level_0, level_1, … if not provided. Valid comparison operators are: =, ==, !=, >, >=, <, <= Valid boolean expressions are combined with: | : or & : and ( and ) : for grouping These rules are similar to how boolean expressions are used in pandas for indexing. Note = will be automatically expanded to the comparison operator == ~ is the not operator, but can only be used in very limited circumstances If a list/tuple of expressions is passed they will be combined via & The following are valid expressions: 'index >= date' "columns = ['A', 'D']" "columns in ['A', 'D']" 'columns = A' 'columns == A' "~(columns = ['A', 'B'])" 'index > df.index[3] & string = "bar"' '(index > df.index[3] & index <= df.index[6]) | string = "bar"' "ts >= Timestamp('2012-02-01')" "major_axis>=20130101" The indexers are on the left-hand side of the sub-expression: columns, major_axis, ts The right-hand side of the sub-expression (after a comparison operator) can be: functions that will be evaluated, e.g. Timestamp('2012-02-01') strings, e.g. "bar" date-like, e.g. 20130101, or "20130101" lists, e.g. "['A', 'B']" variables that are defined in the local names space, e.g. date Note Passing a string to a query by interpolating it into the query expression is not recommended. Simply assign the string of interest to a variable and use that variable in an expression. For example, do this string = "HolyMoly'" store.select("df", "index == string") instead of this string = "HolyMoly'" store.select('df', f'index == {string}') The latter will not work and will raise a SyntaxError.Note that there’s a single quote followed by a double quote in the string variable. If you must interpolate, use the '%r' format specifier store.select("df", "index == %r" % string) which will quote string. Here are some examples: In [491]: dfq = pd.DataFrame( .....: np.random.randn(10, 4), .....: columns=list("ABCD"), .....: index=pd.date_range("20130101", periods=10), .....: ) .....: In [492]: store.append("dfq", dfq, format="table", data_columns=True) Use boolean expressions, with in-line function evaluation. In [493]: store.select("dfq", "index>pd.Timestamp('20130104') & columns=['A', 'B']") Out[493]: A B 2013-01-05 1.366810 1.073372 2013-01-06 2.119746 -2.628174 2013-01-07 0.337920 -0.634027 2013-01-08 1.053434 1.109090 2013-01-09 -0.772942 -0.269415 2013-01-10 0.048562 -0.285920 Use inline column reference. In [494]: store.select("dfq", where="A>0 or C>0") Out[494]: A B C D 2013-01-01 0.856838 1.491776 0.001283 0.701816 2013-01-02 -1.097917 0.102588 0.661740 0.443531 2013-01-03 0.559313 -0.459055 -1.222598 -0.455304 2013-01-05 1.366810 1.073372 -0.994957 0.755314 2013-01-06 2.119746 -2.628174 -0.089460 -0.133636 2013-01-07 0.337920 -0.634027 0.421107 0.604303 2013-01-08 1.053434 1.109090 -0.367891 -0.846206 2013-01-10 0.048562 -0.285920 1.334100 0.194462 The columns keyword can be supplied to select a list of columns to be returned, this is equivalent to passing a 'columns=list_of_columns_to_filter': In [495]: store.select("df", "columns=['A', 'B']") Out[495]: A B 2000-01-01 -0.398501 -0.677311 2000-01-02 -1.167564 -0.593353 2000-01-03 -0.131959 0.089012 2000-01-04 0.169405 -1.358046 2000-01-05 0.492195 0.076693 2000-01-06 -0.285283 -1.210529 2000-01-07 0.941577 -0.342447 2000-01-08 0.052607 2.093214 start and stop parameters can be specified to limit the total search space. These are in terms of the total number of rows in a table. Note select will raise a ValueError if the query expression has an unknown variable reference. Usually this means that you are trying to select on a column that is not a data_column. select will raise a SyntaxError if the query expression is not valid. Query timedelta64[ns]# You can store and query using the timedelta64[ns] type. Terms can be specified in the format: <float>(<unit>), where float may be signed (and fractional), and unit can be D,s,ms,us,ns for the timedelta. Here’s an example: In [496]: from datetime import timedelta In [497]: dftd = pd.DataFrame( .....: { .....: "A": pd.Timestamp("20130101"), .....: "B": [ .....: pd.Timestamp("20130101") + timedelta(days=i, seconds=10) .....: for i in range(10) .....: ], .....: } .....: ) .....: In [498]: dftd["C"] = dftd["A"] - dftd["B"] In [499]: dftd Out[499]: A B C 0 2013-01-01 2013-01-01 00:00:10 -1 days +23:59:50 1 2013-01-01 2013-01-02 00:00:10 -2 days +23:59:50 2 2013-01-01 2013-01-03 00:00:10 -3 days +23:59:50 3 2013-01-01 2013-01-04 00:00:10 -4 days +23:59:50 4 2013-01-01 2013-01-05 00:00:10 -5 days +23:59:50 5 2013-01-01 2013-01-06 00:00:10 -6 days +23:59:50 6 2013-01-01 2013-01-07 00:00:10 -7 days +23:59:50 7 2013-01-01 2013-01-08 00:00:10 -8 days +23:59:50 8 2013-01-01 2013-01-09 00:00:10 -9 days +23:59:50 9 2013-01-01 2013-01-10 00:00:10 -10 days +23:59:50 In [500]: store.append("dftd", dftd, data_columns=True) In [501]: store.select("dftd", "C<'-3.5D'") Out[501]: A B C 4 2013-01-01 2013-01-05 00:00:10 -5 days +23:59:50 5 2013-01-01 2013-01-06 00:00:10 -6 days +23:59:50 6 2013-01-01 2013-01-07 00:00:10 -7 days +23:59:50 7 2013-01-01 2013-01-08 00:00:10 -8 days +23:59:50 8 2013-01-01 2013-01-09 00:00:10 -9 days +23:59:50 9 2013-01-01 2013-01-10 00:00:10 -10 days +23:59:50 Query MultiIndex# Selecting from a MultiIndex can be achieved by using the name of the level. In [502]: df_mi.index.names Out[502]: FrozenList(['foo', 'bar']) In [503]: store.select("df_mi", "foo=baz and bar=two") Out[503]: A B C foo bar baz two 0.183573 0.145277 0.308146 If the MultiIndex levels names are None, the levels are automatically made available via the level_n keyword with n the level of the MultiIndex you want to select from. In [504]: index = pd.MultiIndex( .....: levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], .....: codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], .....: ) .....: In [505]: df_mi_2 = pd.DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"]) In [506]: df_mi_2 Out[506]: A B C foo one -0.646538 1.210676 -0.315409 two 1.528366 0.376542 0.174490 three 1.247943 -0.742283 0.710400 bar one 0.434128 -1.246384 1.139595 two 1.388668 -0.413554 -0.666287 baz two 0.010150 -0.163820 -0.115305 three 0.216467 0.633720 0.473945 qux one -0.155446 1.287082 0.320201 two -1.256989 0.874920 0.765944 three 0.025557 -0.729782 -0.127439 In [507]: store.append("df_mi_2", df_mi_2) # the levels are automatically included as data columns with keyword level_n In [508]: store.select("df_mi_2", "level_0=foo and level_1=two") Out[508]: A B C foo two 1.528366 0.376542 0.17449 Indexing# You can create/modify an index for a table with create_table_index after data is already in the table (after and append/put operation). Creating a table index is highly encouraged. This will speed your queries a great deal when you use a select with the indexed dimension as the where. Note Indexes are automagically created on the indexables and any data columns you specify. This behavior can be turned off by passing index=False to append. # we have automagically already created an index (in the first section) In [509]: i = store.root.df.table.cols.index.index In [510]: i.optlevel, i.kind Out[510]: (6, 'medium') # change an index by passing new parameters In [511]: store.create_table_index("df", optlevel=9, kind="full") In [512]: i = store.root.df.table.cols.index.index In [513]: i.optlevel, i.kind Out[513]: (9, 'full') Oftentimes when appending large amounts of data to a store, it is useful to turn off index creation for each append, then recreate at the end. In [514]: df_1 = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [515]: df_2 = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [516]: st = pd.HDFStore("appends.h5", mode="w") In [517]: st.append("df", df_1, data_columns=["B"], index=False) In [518]: st.append("df", df_2, data_columns=["B"], index=False) In [519]: st.get_storer("df").table Out[519]: /df/table (Table(20,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2)} byteorder := 'little' chunkshape := (2730,) Then create the index when finished appending. In [520]: st.create_table_index("df", columns=["B"], optlevel=9, kind="full") In [521]: st.get_storer("df").table Out[521]: /df/table (Table(20,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2)} byteorder := 'little' chunkshape := (2730,) autoindex := True colindexes := { "B": Index(9, fullshuffle, zlib(1)).is_csi=True} In [522]: st.close() See here for how to create a completely-sorted-index (CSI) on an existing store. Query via data columns# You can designate (and index) certain columns that you want to be able to perform queries (other than the indexable columns, which you can always query). For instance say you want to perform this common operation, on-disk, and return just the frame that matches this query. You can specify data_columns = True to force all columns to be data_columns. In [523]: df_dc = df.copy() In [524]: df_dc["string"] = "foo" In [525]: df_dc.loc[df_dc.index[4:6], "string"] = np.nan In [526]: df_dc.loc[df_dc.index[7:9], "string"] = "bar" In [527]: df_dc["string2"] = "cool" In [528]: df_dc.loc[df_dc.index[1:3], ["B", "C"]] = 1.0 In [529]: df_dc Out[529]: A B C string string2 2000-01-01 -0.398501 -0.677311 -0.874991 foo cool 2000-01-02 -1.167564 1.000000 1.000000 foo cool 2000-01-03 -0.131959 1.000000 1.000000 foo cool 2000-01-04 0.169405 -1.358046 -0.105563 foo cool 2000-01-05 0.492195 0.076693 0.213685 NaN cool 2000-01-06 -0.285283 -1.210529 -1.408386 NaN cool 2000-01-07 0.941577 -0.342447 0.222031 foo cool 2000-01-08 0.052607 2.093214 1.064908 bar cool # on-disk operations In [530]: store.append("df_dc", df_dc, data_columns=["B", "C", "string", "string2"]) In [531]: store.select("df_dc", where="B > 0") Out[531]: A B C string string2 2000-01-02 -1.167564 1.000000 1.000000 foo cool 2000-01-03 -0.131959 1.000000 1.000000 foo cool 2000-01-05 0.492195 0.076693 0.213685 NaN cool 2000-01-08 0.052607 2.093214 1.064908 bar cool # getting creative In [532]: store.select("df_dc", "B > 0 & C > 0 & string == foo") Out[532]: A B C string string2 2000-01-02 -1.167564 1.0 1.0 foo cool 2000-01-03 -0.131959 1.0 1.0 foo cool # this is in-memory version of this type of selection In [533]: df_dc[(df_dc.B > 0) & (df_dc.C > 0) & (df_dc.string == "foo")] Out[533]: A B C string string2 2000-01-02 -1.167564 1.0 1.0 foo cool 2000-01-03 -0.131959 1.0 1.0 foo cool # we have automagically created this index and the B/C/string/string2 # columns are stored separately as ``PyTables`` columns In [534]: store.root.df_dc.table Out[534]: /df_dc/table (Table(8,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2), "C": Float64Col(shape=(), dflt=0.0, pos=3), "string": StringCol(itemsize=3, shape=(), dflt=b'', pos=4), "string2": StringCol(itemsize=4, shape=(), dflt=b'', pos=5)} byteorder := 'little' chunkshape := (1680,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False, "B": Index(6, mediumshuffle, zlib(1)).is_csi=False, "C": Index(6, mediumshuffle, zlib(1)).is_csi=False, "string": Index(6, mediumshuffle, zlib(1)).is_csi=False, "string2": Index(6, mediumshuffle, zlib(1)).is_csi=False} There is some performance degradation by making lots of columns into data columns, so it is up to the user to designate these. In addition, you cannot change data columns (nor indexables) after the first append/put operation (Of course you can simply read in the data and create a new table!). Iterator# You can pass iterator=True or chunksize=number_in_a_chunk to select and select_as_multiple to return an iterator on the results. The default is 50,000 rows returned in a chunk. In [535]: for df in store.select("df", chunksize=3): .....: print(df) .....: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 A B C 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 A B C 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Note You can also use the iterator with read_hdf which will open, then automatically close the store when finished iterating. for df in pd.read_hdf("store.h5", "df", chunksize=3): print(df) Note, that the chunksize keyword applies to the source rows. So if you are doing a query, then the chunksize will subdivide the total rows in the table and the query applied, returning an iterator on potentially unequal sized chunks. Here is a recipe for generating a query and using it to create equal sized return chunks. In [536]: dfeq = pd.DataFrame({"number": np.arange(1, 11)}) In [537]: dfeq Out[537]: number 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 In [538]: store.append("dfeq", dfeq, data_columns=["number"]) In [539]: def chunks(l, n): .....: return [l[i: i + n] for i in range(0, len(l), n)] .....: In [540]: evens = [2, 4, 6, 8, 10] In [541]: coordinates = store.select_as_coordinates("dfeq", "number=evens") In [542]: for c in chunks(coordinates, 2): .....: print(store.select("dfeq", where=c)) .....: number 1 2 3 4 number 5 6 7 8 number 9 10 Advanced queries# Select a single column# To retrieve a single indexable or data column, use the method select_column. This will, for example, enable you to get the index very quickly. These return a Series of the result, indexed by the row number. These do not currently accept the where selector. In [543]: store.select_column("df_dc", "index") Out[543]: 0 2000-01-01 1 2000-01-02 2 2000-01-03 3 2000-01-04 4 2000-01-05 5 2000-01-06 6 2000-01-07 7 2000-01-08 Name: index, dtype: datetime64[ns] In [544]: store.select_column("df_dc", "string") Out[544]: 0 foo 1 foo 2 foo 3 foo 4 NaN 5 NaN 6 foo 7 bar Name: string, dtype: object Selecting coordinates# Sometimes you want to get the coordinates (a.k.a the index locations) of your query. This returns an Int64Index of the resulting locations. These coordinates can also be passed to subsequent where operations. In [545]: df_coord = pd.DataFrame( .....: np.random.randn(1000, 2), index=pd.date_range("20000101", periods=1000) .....: ) .....: In [546]: store.append("df_coord", df_coord) In [547]: c = store.select_as_coordinates("df_coord", "index > 20020101") In [548]: c Out[548]: Int64Index([732, 733, 734, 735, 736, 737, 738, 739, 740, 741, ... 990, 991, 992, 993, 994, 995, 996, 997, 998, 999], dtype='int64', length=268) In [549]: store.select("df_coord", where=c) Out[549]: 0 1 2002-01-02 0.009035 0.921784 2002-01-03 -1.476563 -1.376375 2002-01-04 1.266731 2.173681 2002-01-05 0.147621 0.616468 2002-01-06 0.008611 2.136001 ... ... ... 2002-09-22 0.781169 -0.791687 2002-09-23 -0.764810 -2.000933 2002-09-24 -0.345662 0.393915 2002-09-25 -0.116661 0.834638 2002-09-26 -1.341780 0.686366 [268 rows x 2 columns] Selecting using a where mask# Sometime your query can involve creating a list of rows to select. Usually this mask would be a resulting index from an indexing operation. This example selects the months of a datetimeindex which are 5. In [550]: df_mask = pd.DataFrame( .....: np.random.randn(1000, 2), index=pd.date_range("20000101", periods=1000) .....: ) .....: In [551]: store.append("df_mask", df_mask) In [552]: c = store.select_column("df_mask", "index") In [553]: where = c[pd.DatetimeIndex(c).month == 5].index In [554]: store.select("df_mask", where=where) Out[554]: 0 1 2000-05-01 -0.386742 -0.977433 2000-05-02 -0.228819 0.471671 2000-05-03 0.337307 1.840494 2000-05-04 0.050249 0.307149 2000-05-05 -0.802947 -0.946730 ... ... ... 2002-05-27 1.605281 1.741415 2002-05-28 -0.804450 -0.715040 2002-05-29 -0.874851 0.037178 2002-05-30 -0.161167 -1.294944 2002-05-31 -0.258463 -0.731969 [93 rows x 2 columns] Storer object# If you want to inspect the stored object, retrieve via get_storer. You could use this programmatically to say get the number of rows in an object. In [555]: store.get_storer("df_dc").nrows Out[555]: 8 Multiple table queries# The methods append_to_multiple and select_as_multiple can perform appending/selecting from multiple tables at once. The idea is to have one table (call it the selector table) that you index most/all of the columns, and perform your queries. The other table(s) are data tables with an index matching the selector table’s index. You can then perform a very fast query on the selector table, yet get lots of data back. This method is similar to having a very wide table, but enables more efficient queries. The append_to_multiple method splits a given single DataFrame into multiple tables according to d, a dictionary that maps the table names to a list of ‘columns’ you want in that table. If None is used in place of a list, that table will have the remaining unspecified columns of the given DataFrame. The argument selector defines which table is the selector table (which you can make queries from). The argument dropna will drop rows from the input DataFrame to ensure tables are synchronized. This means that if a row for one of the tables being written to is entirely np.NaN, that row will be dropped from all tables. If dropna is False, THE USER IS RESPONSIBLE FOR SYNCHRONIZING THE TABLES. Remember that entirely np.Nan rows are not written to the HDFStore, so if you choose to call dropna=False, some tables may have more rows than others, and therefore select_as_multiple may not work or it may return unexpected results. In [556]: df_mt = pd.DataFrame( .....: np.random.randn(8, 6), .....: index=pd.date_range("1/1/2000", periods=8), .....: columns=["A", "B", "C", "D", "E", "F"], .....: ) .....: In [557]: df_mt["foo"] = "bar" In [558]: df_mt.loc[df_mt.index[1], ("A", "B")] = np.nan # you can also create the tables individually In [559]: store.append_to_multiple( .....: {"df1_mt": ["A", "B"], "df2_mt": None}, df_mt, selector="df1_mt" .....: ) .....: In [560]: store Out[560]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # individual tables were created In [561]: store.select("df1_mt") Out[561]: A B 2000-01-01 0.079529 -1.459471 2000-01-02 NaN NaN 2000-01-03 -0.423113 2.314361 2000-01-04 0.756744 -0.792372 2000-01-05 -0.184971 0.170852 2000-01-06 0.678830 0.633974 2000-01-07 0.034973 0.974369 2000-01-08 -2.110103 0.243062 In [562]: store.select("df2_mt") Out[562]: C D E F foo 2000-01-01 -0.596306 -0.910022 -1.057072 -0.864360 bar 2000-01-02 0.477849 0.283128 -2.045700 -0.338206 bar 2000-01-03 -0.033100 -0.965461 -0.001079 -0.351689 bar 2000-01-04 -0.513555 -1.484776 -0.796280 -0.182321 bar 2000-01-05 -0.872407 -1.751515 0.934334 0.938818 bar 2000-01-06 -1.398256 1.347142 -0.029520 0.082738 bar 2000-01-07 -0.755544 0.380786 -1.634116 1.293610 bar 2000-01-08 1.453064 0.500558 -0.574475 0.694324 bar # as a multiple In [563]: store.select_as_multiple( .....: ["df1_mt", "df2_mt"], .....: where=["A>0", "B>0"], .....: selector="df1_mt", .....: ) .....: Out[563]: A B C D E F foo 2000-01-06 0.678830 0.633974 -1.398256 1.347142 -0.029520 0.082738 bar 2000-01-07 0.034973 0.974369 -0.755544 0.380786 -1.634116 1.293610 bar Delete from a table# You can delete from a table selectively by specifying a where. In deleting rows, it is important to understand the PyTables deletes rows by erasing the rows, then moving the following data. Thus deleting can potentially be a very expensive operation depending on the orientation of your data. To get optimal performance, it’s worthwhile to have the dimension you are deleting be the first of the indexables. Data is ordered (on the disk) in terms of the indexables. Here’s a simple use case. You store panel-type data, with dates in the major_axis and ids in the minor_axis. The data is then interleaved like this: date_1 id_1 id_2 . id_n date_2 id_1 . id_n It should be clear that a delete operation on the major_axis will be fairly quick, as one chunk is removed, then the following data moved. On the other hand a delete operation on the minor_axis will be very expensive. In this case it would almost certainly be faster to rewrite the table using a where that selects all but the missing data. Warning Please note that HDF5 DOES NOT RECLAIM SPACE in the h5 files automatically. Thus, repeatedly deleting (or removing nodes) and adding again, WILL TEND TO INCREASE THE FILE SIZE. To repack and clean the file, use ptrepack. Notes & caveats# Compression# PyTables allows the stored data to be compressed. This applies to all kinds of stores, not just tables. Two parameters are used to control compression: complevel and complib. complevel specifies if and how hard data is to be compressed. complevel=0 and complevel=None disables compression and 0<complevel<10 enables compression. complib specifies which compression library to use. If nothing is specified the default library zlib is used. A compression library usually optimizes for either good compression rates or speed and the results will depend on the type of data. Which type of compression to choose depends on your specific needs and data. The list of supported compression libraries: zlib: The default compression library. A classic in terms of compression, achieves good compression rates but is somewhat slow. lzo: Fast compression and decompression. bzip2: Good compression rates. blosc: Fast compression and decompression. Support for alternative blosc compressors: blosc:blosclz This is the default compressor for blosc blosc:lz4: A compact, very popular and fast compressor. blosc:lz4hc: A tweaked version of LZ4, produces better compression ratios at the expense of speed. blosc:snappy: A popular compressor used in many places. blosc:zlib: A classic; somewhat slower than the previous ones, but achieving better compression ratios. blosc:zstd: An extremely well balanced codec; it provides the best compression ratios among the others above, and at reasonably fast speed. If complib is defined as something other than the listed libraries a ValueError exception is issued. Note If the library specified with the complib option is missing on your platform, compression defaults to zlib without further ado. Enable compression for all objects within the file: store_compressed = pd.HDFStore( "store_compressed.h5", complevel=9, complib="blosc:blosclz" ) Or on-the-fly compression (this only applies to tables) in stores where compression is not enabled: store.append("df", df, complib="zlib", complevel=5) ptrepack# PyTables offers better write performance when tables are compressed after they are written, as opposed to turning on compression at the very beginning. You can use the supplied PyTables utility ptrepack. In addition, ptrepack can change compression levels after the fact. ptrepack --chunkshape=auto --propindexes --complevel=9 --complib=blosc in.h5 out.h5 Furthermore ptrepack in.h5 out.h5 will repack the file to allow you to reuse previously deleted space. Alternatively, one can simply remove the file and write again, or use the copy method. Caveats# Warning HDFStore is not-threadsafe for writing. The underlying PyTables only supports concurrent reads (via threading or processes). If you need reading and writing at the same time, you need to serialize these operations in a single thread in a single process. You will corrupt your data otherwise. See the (GH2397) for more information. If you use locks to manage write access between multiple processes, you may want to use fsync() before releasing write locks. For convenience you can use store.flush(fsync=True) to do this for you. Once a table is created columns (DataFrame) are fixed; only exactly the same columns can be appended Be aware that timezones (e.g., pytz.timezone('US/Eastern')) are not necessarily equal across timezone versions. So if data is localized to a specific timezone in the HDFStore using one version of a timezone library and that data is updated with another version, the data will be converted to UTC since these timezones are not considered equal. Either use the same version of timezone library or use tz_convert with the updated timezone definition. Warning PyTables will show a NaturalNameWarning if a column name cannot be used as an attribute selector. Natural identifiers contain only letters, numbers, and underscores, and may not begin with a number. Other identifiers cannot be used in a where clause and are generally a bad idea. DataTypes# HDFStore will map an object dtype to the PyTables underlying dtype. This means the following types are known to work: Type Represents missing values floating : float64, float32, float16 np.nan integer : int64, int32, int8, uint64,uint32, uint8 boolean datetime64[ns] NaT timedelta64[ns] NaT categorical : see the section below object : strings np.nan unicode columns are not supported, and WILL FAIL. Categorical data# You can write data that contains category dtypes to a HDFStore. Queries work the same as if it was an object array. However, the category dtyped data is stored in a more efficient manner. In [564]: dfcat = pd.DataFrame( .....: {"A": pd.Series(list("aabbcdba")).astype("category"), "B": np.random.randn(8)} .....: ) .....: In [565]: dfcat Out[565]: A B 0 a -1.608059 1 a 0.851060 2 b -0.736931 3 b 0.003538 4 c -1.422611 5 d 2.060901 6 b 0.993899 7 a -1.371768 In [566]: dfcat.dtypes Out[566]: A category B float64 dtype: object In [567]: cstore = pd.HDFStore("cats.h5", mode="w") In [568]: cstore.append("dfcat", dfcat, format="table", data_columns=["A"]) In [569]: result = cstore.select("dfcat", where="A in ['b', 'c']") In [570]: result Out[570]: A B 2 b -0.736931 3 b 0.003538 4 c -1.422611 6 b 0.993899 In [571]: result.dtypes Out[571]: A category B float64 dtype: object String columns# min_itemsize The underlying implementation of HDFStore uses a fixed column width (itemsize) for string columns. A string column itemsize is calculated as the maximum of the length of data (for that column) that is passed to the HDFStore, in the first append. Subsequent appends, may introduce a string for a column larger than the column can hold, an Exception will be raised (otherwise you could have a silent truncation of these columns, leading to loss of information). In the future we may relax this and allow a user-specified truncation to occur. Pass min_itemsize on the first table creation to a-priori specify the minimum length of a particular string column. min_itemsize can be an integer, or a dict mapping a column name to an integer. You can pass values as a key to allow all indexables or data_columns to have this min_itemsize. Passing a min_itemsize dict will cause all passed columns to be created as data_columns automatically. Note If you are not passing any data_columns, then the min_itemsize will be the maximum of the length of any string passed In [572]: dfs = pd.DataFrame({"A": "foo", "B": "bar"}, index=list(range(5))) In [573]: dfs Out[573]: A B 0 foo bar 1 foo bar 2 foo bar 3 foo bar 4 foo bar # A and B have a size of 30 In [574]: store.append("dfs", dfs, min_itemsize=30) In [575]: store.get_storer("dfs").table Out[575]: /dfs/table (Table(5,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": StringCol(itemsize=30, shape=(2,), dflt=b'', pos=1)} byteorder := 'little' chunkshape := (963,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False} # A is created as a data_column with a size of 30 # B is size is calculated In [576]: store.append("dfs2", dfs, min_itemsize={"A": 30}) In [577]: store.get_storer("dfs2").table Out[577]: /dfs2/table (Table(5,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": StringCol(itemsize=3, shape=(1,), dflt=b'', pos=1), "A": StringCol(itemsize=30, shape=(), dflt=b'', pos=2)} byteorder := 'little' chunkshape := (1598,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False, "A": Index(6, mediumshuffle, zlib(1)).is_csi=False} nan_rep String columns will serialize a np.nan (a missing value) with the nan_rep string representation. This defaults to the string value nan. You could inadvertently turn an actual nan value into a missing value. In [578]: dfss = pd.DataFrame({"A": ["foo", "bar", "nan"]}) In [579]: dfss Out[579]: A 0 foo 1 bar 2 nan In [580]: store.append("dfss", dfss) In [581]: store.select("dfss") Out[581]: A 0 foo 1 bar 2 NaN # here you need to specify a different nan rep In [582]: store.append("dfss2", dfss, nan_rep="_nan_") In [583]: store.select("dfss2") Out[583]: A 0 foo 1 bar 2 nan External compatibility# HDFStore writes table format objects in specific formats suitable for producing loss-less round trips to pandas objects. For external compatibility, HDFStore can read native PyTables format tables. It is possible to write an HDFStore object that can easily be imported into R using the rhdf5 library (Package website). Create a table format store like this: In [584]: df_for_r = pd.DataFrame( .....: { .....: "first": np.random.rand(100), .....: "second": np.random.rand(100), .....: "class": np.random.randint(0, 2, (100,)), .....: }, .....: index=range(100), .....: ) .....: In [585]: df_for_r.head() Out[585]: first second class 0 0.013480 0.504941 0 1 0.690984 0.898188 1 2 0.510113 0.618748 1 3 0.357698 0.004972 0 4 0.451658 0.012065 1 In [586]: store_export = pd.HDFStore("export.h5") In [587]: store_export.append("df_for_r", df_for_r, data_columns=df_dc.columns) In [588]: store_export Out[588]: <class 'pandas.io.pytables.HDFStore'> File path: export.h5 In R this file can be read into a data.frame object using the rhdf5 library. The following example function reads the corresponding column names and data values from the values and assembles them into a data.frame: # Load values and column names for all datasets from corresponding nodes and # insert them into one data.frame object. library(rhdf5) loadhdf5data <- function(h5File) { listing <- h5ls(h5File) # Find all data nodes, values are stored in *_values and corresponding column # titles in *_items data_nodes <- grep("_values", listing$name) name_nodes <- grep("_items", listing$name) data_paths = paste(listing$group[data_nodes], listing$name[data_nodes], sep = "/") name_paths = paste(listing$group[name_nodes], listing$name[name_nodes], sep = "/") columns = list() for (idx in seq(data_paths)) { # NOTE: matrices returned by h5read have to be transposed to obtain # required Fortran order! data <- data.frame(t(h5read(h5File, data_paths[idx]))) names <- t(h5read(h5File, name_paths[idx])) entry <- data.frame(data) colnames(entry) <- names columns <- append(columns, entry) } data <- data.frame(columns) return(data) } Now you can import the DataFrame into R: > data = loadhdf5data("transfer.hdf5") > head(data) first second class 1 0.4170220047 0.3266449 0 2 0.7203244934 0.5270581 0 3 0.0001143748 0.8859421 1 4 0.3023325726 0.3572698 1 5 0.1467558908 0.9085352 1 6 0.0923385948 0.6233601 1 Note The R function lists the entire HDF5 file’s contents and assembles the data.frame object from all matching nodes, so use this only as a starting point if you have stored multiple DataFrame objects to a single HDF5 file. Performance# tables format come with a writing performance penalty as compared to fixed stores. The benefit is the ability to append/delete and query (potentially very large amounts of data). Write times are generally longer as compared with regular stores. Query times can be quite fast, especially on an indexed axis. You can pass chunksize=<int> to append, specifying the write chunksize (default is 50000). This will significantly lower your memory usage on writing. You can pass expectedrows=<int> to the first append, to set the TOTAL number of rows that PyTables will expect. This will optimize read/write performance. Duplicate rows can be written to tables, but are filtered out in selection (with the last items being selected; thus a table is unique on major, minor pairs) A PerformanceWarning will be raised if you are attempting to store types that will be pickled by PyTables (rather than stored as endemic types). See Here for more information and some solutions. Feather# Feather provides binary columnar serialization for data frames. It is designed to make reading and writing data frames efficient, and to make sharing data across data analysis languages easy. Feather is designed to faithfully serialize and de-serialize DataFrames, supporting all of the pandas dtypes, including extension dtypes such as categorical and datetime with tz. Several caveats: The format will NOT write an Index, or MultiIndex for the DataFrame and will raise an error if a non-default one is provided. You can .reset_index() to store the index or .reset_index(drop=True) to ignore it. Duplicate column names and non-string columns names are not supported Actual Python objects in object dtype columns are not supported. These will raise a helpful error message on an attempt at serialization. See the Full Documentation. In [589]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(3, 6).astype("u1"), .....: "d": np.arange(4.0, 7.0, dtype="float64"), .....: "e": [True, False, True], .....: "f": pd.Categorical(list("abc")), .....: "g": pd.date_range("20130101", periods=3), .....: "h": pd.date_range("20130101", periods=3, tz="US/Eastern"), .....: "i": pd.date_range("20130101", periods=3, freq="ns"), .....: } .....: ) .....: In [590]: df Out[590]: a b c ... g h i 0 a 1 3 ... 2013-01-01 2013-01-01 00:00:00-05:00 2013-01-01 00:00:00.000000000 1 b 2 4 ... 2013-01-02 2013-01-02 00:00:00-05:00 2013-01-01 00:00:00.000000001 2 c 3 5 ... 2013-01-03 2013-01-03 00:00:00-05:00 2013-01-01 00:00:00.000000002 [3 rows x 9 columns] In [591]: df.dtypes Out[591]: a object b int64 c uint8 d float64 e bool f category g datetime64[ns] h datetime64[ns, US/Eastern] i datetime64[ns] dtype: object Write to a feather file. In [592]: df.to_feather("example.feather") Read from a feather file. In [593]: result = pd.read_feather("example.feather") In [594]: result Out[594]: a b c ... g h i 0 a 1 3 ... 2013-01-01 2013-01-01 00:00:00-05:00 2013-01-01 00:00:00.000000000 1 b 2 4 ... 2013-01-02 2013-01-02 00:00:00-05:00 2013-01-01 00:00:00.000000001 2 c 3 5 ... 2013-01-03 2013-01-03 00:00:00-05:00 2013-01-01 00:00:00.000000002 [3 rows x 9 columns] # we preserve dtypes In [595]: result.dtypes Out[595]: a object b int64 c uint8 d float64 e bool f category g datetime64[ns] h datetime64[ns, US/Eastern] i datetime64[ns] dtype: object Parquet# Apache Parquet provides a partitioned binary columnar serialization for data frames. It is designed to make reading and writing data frames efficient, and to make sharing data across data analysis languages easy. Parquet can use a variety of compression techniques to shrink the file size as much as possible while still maintaining good read performance. Parquet is designed to faithfully serialize and de-serialize DataFrame s, supporting all of the pandas dtypes, including extension dtypes such as datetime with tz. Several caveats. Duplicate column names and non-string columns names are not supported. The pyarrow engine always writes the index to the output, but fastparquet only writes non-default indexes. This extra column can cause problems for non-pandas consumers that are not expecting it. You can force including or omitting indexes with the index argument, regardless of the underlying engine. Index level names, if specified, must be strings. In the pyarrow engine, categorical dtypes for non-string types can be serialized to parquet, but will de-serialize as their primitive dtype. The pyarrow engine preserves the ordered flag of categorical dtypes with string types. fastparquet does not preserve the ordered flag. Non supported types include Interval and actual Python object types. These will raise a helpful error message on an attempt at serialization. Period type is supported with pyarrow >= 0.16.0. The pyarrow engine preserves extension data types such as the nullable integer and string data type (requiring pyarrow >= 0.16.0, and requiring the extension type to implement the needed protocols, see the extension types documentation). You can specify an engine to direct the serialization. This can be one of pyarrow, or fastparquet, or auto. If the engine is NOT specified, then the pd.options.io.parquet.engine option is checked; if this is also auto, then pyarrow is tried, and falling back to fastparquet. See the documentation for pyarrow and fastparquet. Note These engines are very similar and should read/write nearly identical parquet format files. pyarrow>=8.0.0 supports timedelta data, fastparquet>=0.1.4 supports timezone aware datetimes. These libraries differ by having different underlying dependencies (fastparquet by using numba, while pyarrow uses a c-library). In [596]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(3, 6).astype("u1"), .....: "d": np.arange(4.0, 7.0, dtype="float64"), .....: "e": [True, False, True], .....: "f": pd.date_range("20130101", periods=3), .....: "g": pd.date_range("20130101", periods=3, tz="US/Eastern"), .....: "h": pd.Categorical(list("abc")), .....: "i": pd.Categorical(list("abc"), ordered=True), .....: } .....: ) .....: In [597]: df Out[597]: a b c d e f g h i 0 a 1 3 4.0 True 2013-01-01 2013-01-01 00:00:00-05:00 a a 1 b 2 4 5.0 False 2013-01-02 2013-01-02 00:00:00-05:00 b b 2 c 3 5 6.0 True 2013-01-03 2013-01-03 00:00:00-05:00 c c In [598]: df.dtypes Out[598]: a object b int64 c uint8 d float64 e bool f datetime64[ns] g datetime64[ns, US/Eastern] h category i category dtype: object Write to a parquet file. In [599]: df.to_parquet("example_pa.parquet", engine="pyarrow") In [600]: df.to_parquet("example_fp.parquet", engine="fastparquet") Read from a parquet file. In [601]: result = pd.read_parquet("example_fp.parquet", engine="fastparquet") In [602]: result = pd.read_parquet("example_pa.parquet", engine="pyarrow") In [603]: result.dtypes Out[603]: a object b int64 c uint8 d float64 e bool f datetime64[ns] g datetime64[ns, US/Eastern] h category i category dtype: object Read only certain columns of a parquet file. In [604]: result = pd.read_parquet( .....: "example_fp.parquet", .....: engine="fastparquet", .....: columns=["a", "b"], .....: ) .....: In [605]: result = pd.read_parquet( .....: "example_pa.parquet", .....: engine="pyarrow", .....: columns=["a", "b"], .....: ) .....: In [606]: result.dtypes Out[606]: a object b int64 dtype: object Handling indexes# Serializing a DataFrame to parquet may include the implicit index as one or more columns in the output file. Thus, this code: In [607]: df = pd.DataFrame({"a": [1, 2], "b": [3, 4]}) In [608]: df.to_parquet("test.parquet", engine="pyarrow") creates a parquet file with three columns if you use pyarrow for serialization: a, b, and __index_level_0__. If you’re using fastparquet, the index may or may not be written to the file. This unexpected extra column causes some databases like Amazon Redshift to reject the file, because that column doesn’t exist in the target table. If you want to omit a dataframe’s indexes when writing, pass index=False to to_parquet(): In [609]: df.to_parquet("test.parquet", index=False) This creates a parquet file with just the two expected columns, a and b. If your DataFrame has a custom index, you won’t get it back when you load this file into a DataFrame. Passing index=True will always write the index, even if that’s not the underlying engine’s default behavior. Partitioning Parquet files# Parquet supports partitioning of data based on the values of one or more columns. In [610]: df = pd.DataFrame({"a": [0, 0, 1, 1], "b": [0, 1, 0, 1]}) In [611]: df.to_parquet(path="test", engine="pyarrow", partition_cols=["a"], compression=None) The path specifies the parent directory to which data will be saved. The partition_cols are the column names by which the dataset will be partitioned. Columns are partitioned in the order they are given. The partition splits are determined by the unique values in the partition columns. The above example creates a partitioned dataset that may look like: test ├── a=0 │ ├── 0bac803e32dc42ae83fddfd029cbdebc.parquet │ └── ... └── a=1 ├── e6ab24a4f45147b49b54a662f0c412a3.parquet └── ... ORC# New in version 1.0.0. Similar to the parquet format, the ORC Format is a binary columnar serialization for data frames. It is designed to make reading data frames efficient. pandas provides both the reader and the writer for the ORC format, read_orc() and to_orc(). This requires the pyarrow library. Warning It is highly recommended to install pyarrow using conda due to some issues occurred by pyarrow. to_orc() requires pyarrow>=7.0.0. read_orc() and to_orc() are not supported on Windows yet, you can find valid environments on install optional dependencies. For supported dtypes please refer to supported ORC features in Arrow. Currently timezones in datetime columns are not preserved when a dataframe is converted into ORC files. In [612]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(4.0, 7.0, dtype="float64"), .....: "d": [True, False, True], .....: "e": pd.date_range("20130101", periods=3), .....: } .....: ) .....: In [613]: df Out[613]: a b c d e 0 a 1 4.0 True 2013-01-01 1 b 2 5.0 False 2013-01-02 2 c 3 6.0 True 2013-01-03 In [614]: df.dtypes Out[614]: a object b int64 c float64 d bool e datetime64[ns] dtype: object Write to an orc file. In [615]: df.to_orc("example_pa.orc", engine="pyarrow") Read from an orc file. In [616]: result = pd.read_orc("example_pa.orc") In [617]: result.dtypes Out[617]: a object b int64 c float64 d bool e datetime64[ns] dtype: object Read only certain columns of an orc file. In [618]: result = pd.read_orc( .....: "example_pa.orc", .....: columns=["a", "b"], .....: ) .....: In [619]: result.dtypes Out[619]: a object b int64 dtype: object SQL queries# The pandas.io.sql module provides a collection of query wrappers to both facilitate data retrieval and to reduce dependency on DB-specific API. Database abstraction is provided by SQLAlchemy if installed. In addition you will need a driver library for your database. Examples of such drivers are psycopg2 for PostgreSQL or pymysql for MySQL. For SQLite this is included in Python’s standard library by default. You can find an overview of supported drivers for each SQL dialect in the SQLAlchemy docs. If SQLAlchemy is not installed, a fallback is only provided for sqlite (and for mysql for backwards compatibility, but this is deprecated and will be removed in a future version). This mode requires a Python database adapter which respect the Python DB-API. See also some cookbook examples for some advanced strategies. The key functions are: read_sql_table(table_name, con[, schema, ...]) Read SQL database table into a DataFrame. read_sql_query(sql, con[, index_col, ...]) Read SQL query into a DataFrame. read_sql(sql, con[, index_col, ...]) Read SQL query or database table into a DataFrame. DataFrame.to_sql(name, con[, schema, ...]) Write records stored in a DataFrame to a SQL database. Note The function read_sql() is a convenience wrapper around read_sql_table() and read_sql_query() (and for backward compatibility) and will delegate to specific function depending on the provided input (database table name or sql query). Table names do not need to be quoted if they have special characters. In the following example, we use the SQlite SQL database engine. You can use a temporary SQLite database where data are stored in “memory”. To connect with SQLAlchemy you use the create_engine() function to create an engine object from database URI. You only need to create the engine once per database you are connecting to. For more information on create_engine() and the URI formatting, see the examples below and the SQLAlchemy documentation In [620]: from sqlalchemy import create_engine # Create your engine. In [621]: engine = create_engine("sqlite:///:memory:") If you want to manage your own connections you can pass one of those instead. The example below opens a connection to the database using a Python context manager that automatically closes the connection after the block has completed. See the SQLAlchemy docs for an explanation of how the database connection is handled. with engine.connect() as conn, conn.begin(): data = pd.read_sql_table("data", conn) Warning When you open a connection to a database you are also responsible for closing it. Side effects of leaving a connection open may include locking the database or other breaking behaviour. Writing DataFrames# Assuming the following data is in a DataFrame data, we can insert it into the database using to_sql(). id Date Col_1 Col_2 Col_3 26 2012-10-18 X 25.7 True 42 2012-10-19 Y -12.4 False 63 2012-10-20 Z 5.73 True In [622]: import datetime In [623]: c = ["id", "Date", "Col_1", "Col_2", "Col_3"] In [624]: d = [ .....: (26, datetime.datetime(2010, 10, 18), "X", 27.5, True), .....: (42, datetime.datetime(2010, 10, 19), "Y", -12.5, False), .....: (63, datetime.datetime(2010, 10, 20), "Z", 5.73, True), .....: ] .....: In [625]: data = pd.DataFrame(d, columns=c) In [626]: data Out[626]: id Date Col_1 Col_2 Col_3 0 26 2010-10-18 X 27.50 True 1 42 2010-10-19 Y -12.50 False 2 63 2010-10-20 Z 5.73 True In [627]: data.to_sql("data", engine) Out[627]: 3 With some databases, writing large DataFrames can result in errors due to packet size limitations being exceeded. This can be avoided by setting the chunksize parameter when calling to_sql. For example, the following writes data to the database in batches of 1000 rows at a time: In [628]: data.to_sql("data_chunked", engine, chunksize=1000) Out[628]: 3 SQL data types# to_sql() will try to map your data to an appropriate SQL data type based on the dtype of the data. When you have columns of dtype object, pandas will try to infer the data type. You can always override the default type by specifying the desired SQL type of any of the columns by using the dtype argument. This argument needs a dictionary mapping column names to SQLAlchemy types (or strings for the sqlite3 fallback mode). For example, specifying to use the sqlalchemy String type instead of the default Text type for string columns: In [629]: from sqlalchemy.types import String In [630]: data.to_sql("data_dtype", engine, dtype={"Col_1": String}) Out[630]: 3 Note Due to the limited support for timedelta’s in the different database flavors, columns with type timedelta64 will be written as integer values as nanoseconds to the database and a warning will be raised. Note Columns of category dtype will be converted to the dense representation as you would get with np.asarray(categorical) (e.g. for string categories this gives an array of strings). Because of this, reading the database table back in does not generate a categorical. Datetime data types# Using SQLAlchemy, to_sql() is capable of writing datetime data that is timezone naive or timezone aware. However, the resulting data stored in the database ultimately depends on the supported data type for datetime data of the database system being used. The following table lists supported data types for datetime data for some common databases. Other database dialects may have different data types for datetime data. Database SQL Datetime Types Timezone Support SQLite TEXT No MySQL TIMESTAMP or DATETIME No PostgreSQL TIMESTAMP or TIMESTAMP WITH TIME ZONE Yes When writing timezone aware data to databases that do not support timezones, the data will be written as timezone naive timestamps that are in local time with respect to the timezone. read_sql_table() is also capable of reading datetime data that is timezone aware or naive. When reading TIMESTAMP WITH TIME ZONE types, pandas will convert the data to UTC. Insertion method# The parameter method controls the SQL insertion clause used. Possible values are: None: Uses standard SQL INSERT clause (one per row). 'multi': Pass multiple values in a single INSERT clause. It uses a special SQL syntax not supported by all backends. This usually provides better performance for analytic databases like Presto and Redshift, but has worse performance for traditional SQL backend if the table contains many columns. For more information check the SQLAlchemy documentation. callable with signature (pd_table, conn, keys, data_iter): This can be used to implement a more performant insertion method based on specific backend dialect features. Example of a callable using PostgreSQL COPY clause: # Alternative to_sql() *method* for DBs that support COPY FROM import csv from io import StringIO def psql_insert_copy(table, conn, keys, data_iter): """ Execute SQL statement inserting data Parameters ---------- table : pandas.io.sql.SQLTable conn : sqlalchemy.engine.Engine or sqlalchemy.engine.Connection keys : list of str Column names data_iter : Iterable that iterates the values to be inserted """ # gets a DBAPI connection that can provide a cursor dbapi_conn = conn.connection with dbapi_conn.cursor() as cur: s_buf = StringIO() writer = csv.writer(s_buf) writer.writerows(data_iter) s_buf.seek(0) columns = ', '.join(['"{}"'.format(k) for k in keys]) if table.schema: table_name = '{}.{}'.format(table.schema, table.name) else: table_name = table.name sql = 'COPY {} ({}) FROM STDIN WITH CSV'.format( table_name, columns) cur.copy_expert(sql=sql, file=s_buf) Reading tables# read_sql_table() will read a database table given the table name and optionally a subset of columns to read. Note In order to use read_sql_table(), you must have the SQLAlchemy optional dependency installed. In [631]: pd.read_sql_table("data", engine) Out[631]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 X 27.50 True 1 1 42 2010-10-19 Y -12.50 False 2 2 63 2010-10-20 Z 5.73 True Note Note that pandas infers column dtypes from query outputs, and not by looking up data types in the physical database schema. For example, assume userid is an integer column in a table. Then, intuitively, select userid ... will return integer-valued series, while select cast(userid as text) ... will return object-valued (str) series. Accordingly, if the query output is empty, then all resulting columns will be returned as object-valued (since they are most general). If you foresee that your query will sometimes generate an empty result, you may want to explicitly typecast afterwards to ensure dtype integrity. You can also specify the name of the column as the DataFrame index, and specify a subset of columns to be read. In [632]: pd.read_sql_table("data", engine, index_col="id") Out[632]: index Date Col_1 Col_2 Col_3 id 26 0 2010-10-18 X 27.50 True 42 1 2010-10-19 Y -12.50 False 63 2 2010-10-20 Z 5.73 True In [633]: pd.read_sql_table("data", engine, columns=["Col_1", "Col_2"]) Out[633]: Col_1 Col_2 0 X 27.50 1 Y -12.50 2 Z 5.73 And you can explicitly force columns to be parsed as dates: In [634]: pd.read_sql_table("data", engine, parse_dates=["Date"]) Out[634]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 X 27.50 True 1 1 42 2010-10-19 Y -12.50 False 2 2 63 2010-10-20 Z 5.73 True If needed you can explicitly specify a format string, or a dict of arguments to pass to pandas.to_datetime(): pd.read_sql_table("data", engine, parse_dates={"Date": "%Y-%m-%d"}) pd.read_sql_table( "data", engine, parse_dates={"Date": {"format": "%Y-%m-%d %H:%M:%S"}}, ) You can check if a table exists using has_table() Schema support# Reading from and writing to different schema’s is supported through the schema keyword in the read_sql_table() and to_sql() functions. Note however that this depends on the database flavor (sqlite does not have schema’s). For example: df.to_sql("table", engine, schema="other_schema") pd.read_sql_table("table", engine, schema="other_schema") Querying# You can query using raw SQL in the read_sql_query() function. In this case you must use the SQL variant appropriate for your database. When using SQLAlchemy, you can also pass SQLAlchemy Expression language constructs, which are database-agnostic. In [635]: pd.read_sql_query("SELECT * FROM data", engine) Out[635]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 00:00:00.000000 X 27.50 1 1 1 42 2010-10-19 00:00:00.000000 Y -12.50 0 2 2 63 2010-10-20 00:00:00.000000 Z 5.73 1 Of course, you can specify a more “complex” query. In [636]: pd.read_sql_query("SELECT id, Col_1, Col_2 FROM data WHERE id = 42;", engine) Out[636]: id Col_1 Col_2 0 42 Y -12.5 The read_sql_query() function supports a chunksize argument. Specifying this will return an iterator through chunks of the query result: In [637]: df = pd.DataFrame(np.random.randn(20, 3), columns=list("abc")) In [638]: df.to_sql("data_chunks", engine, index=False) Out[638]: 20 In [639]: for chunk in pd.read_sql_query("SELECT * FROM data_chunks", engine, chunksize=5): .....: print(chunk) .....: a b c 0 0.070470 0.901320 0.937577 1 0.295770 1.420548 -0.005283 2 -1.518598 -0.730065 0.226497 3 -2.061465 0.632115 0.853619 4 2.719155 0.139018 0.214557 a b c 0 -1.538924 -0.366973 -0.748801 1 -0.478137 -1.559153 -3.097759 2 -2.320335 -0.221090 0.119763 3 0.608228 1.064810 -0.780506 4 -2.736887 0.143539 1.170191 a b c 0 -1.573076 0.075792 -1.722223 1 -0.774650 0.803627 0.221665 2 0.584637 0.147264 1.057825 3 -0.284136 0.912395 1.552808 4 0.189376 -0.109830 0.539341 a b c 0 0.592591 -0.155407 -1.356475 1 0.833837 1.524249 1.606722 2 -0.029487 -0.051359 1.700152 3 0.921484 -0.926347 0.979818 4 0.182380 -0.186376 0.049820 You can also run a plain query without creating a DataFrame with execute(). This is useful for queries that don’t return values, such as INSERT. This is functionally equivalent to calling execute on the SQLAlchemy engine or db connection object. Again, you must use the SQL syntax variant appropriate for your database. from pandas.io import sql sql.execute("SELECT * FROM table_name", engine) sql.execute( "INSERT INTO table_name VALUES(?, ?, ?)", engine, params=[("id", 1, 12.2, True)] ) Engine connection examples# To connect with SQLAlchemy you use the create_engine() function to create an engine object from database URI. You only need to create the engine once per database you are connecting to. from sqlalchemy import create_engine engine = create_engine("postgresql://scott:[email protected]:5432/mydatabase") engine = create_engine("mysql+mysqldb://scott:[email protected]/foo") engine = create_engine("oracle://scott:[email protected]:1521/sidname") engine = create_engine("mssql+pyodbc://mydsn") # sqlite://<nohostname>/<path> # where <path> is relative: engine = create_engine("sqlite:///foo.db") # or absolute, starting with a slash: engine = create_engine("sqlite:////absolute/path/to/foo.db") For more information see the examples the SQLAlchemy documentation Advanced SQLAlchemy queries# You can use SQLAlchemy constructs to describe your query. Use sqlalchemy.text() to specify query parameters in a backend-neutral way In [640]: import sqlalchemy as sa In [641]: pd.read_sql( .....: sa.text("SELECT * FROM data where Col_1=:col1"), engine, params={"col1": "X"} .....: ) .....: Out[641]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 00:00:00.000000 X 27.5 1 If you have an SQLAlchemy description of your database you can express where conditions using SQLAlchemy expressions In [642]: metadata = sa.MetaData() In [643]: data_table = sa.Table( .....: "data", .....: metadata, .....: sa.Column("index", sa.Integer), .....: sa.Column("Date", sa.DateTime), .....: sa.Column("Col_1", sa.String), .....: sa.Column("Col_2", sa.Float), .....: sa.Column("Col_3", sa.Boolean), .....: ) .....: In [644]: pd.read_sql(sa.select([data_table]).where(data_table.c.Col_3 is True), engine) Out[644]: Empty DataFrame Columns: [index, Date, Col_1, Col_2, Col_3] Index: [] You can combine SQLAlchemy expressions with parameters passed to read_sql() using sqlalchemy.bindparam() In [645]: import datetime as dt In [646]: expr = sa.select([data_table]).where(data_table.c.Date > sa.bindparam("date")) In [647]: pd.read_sql(expr, engine, params={"date": dt.datetime(2010, 10, 18)}) Out[647]: index Date Col_1 Col_2 Col_3 0 1 2010-10-19 Y -12.50 False 1 2 2010-10-20 Z 5.73 True Sqlite fallback# The use of sqlite is supported without using SQLAlchemy. This mode requires a Python database adapter which respect the Python DB-API. You can create connections like so: import sqlite3 con = sqlite3.connect(":memory:") And then issue the following queries: data.to_sql("data", con) pd.read_sql_query("SELECT * FROM data", con) Google BigQuery# Warning Starting in 0.20.0, pandas has split off Google BigQuery support into the separate package pandas-gbq. You can pip install pandas-gbq to get it. The pandas-gbq package provides functionality to read/write from Google BigQuery. pandas integrates with this external package. if pandas-gbq is installed, you can use the pandas methods pd.read_gbq and DataFrame.to_gbq, which will call the respective functions from pandas-gbq. Full documentation can be found here. Stata format# Writing to stata format# The method to_stata() will write a DataFrame into a .dta file. The format version of this file is always 115 (Stata 12). In [648]: df = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [649]: df.to_stata("stata.dta") Stata data files have limited data type support; only strings with 244 or fewer characters, int8, int16, int32, float32 and float64 can be stored in .dta files. Additionally, Stata reserves certain values to represent missing data. Exporting a non-missing value that is outside of the permitted range in Stata for a particular data type will retype the variable to the next larger size. For example, int8 values are restricted to lie between -127 and 100 in Stata, and so variables with values above 100 will trigger a conversion to int16. nan values in floating points data types are stored as the basic missing data type (. in Stata). Note It is not possible to export missing data values for integer data types. The Stata writer gracefully handles other data types including int64, bool, uint8, uint16, uint32 by casting to the smallest supported type that can represent the data. For example, data with a type of uint8 will be cast to int8 if all values are less than 100 (the upper bound for non-missing int8 data in Stata), or, if values are outside of this range, the variable is cast to int16. Warning Conversion from int64 to float64 may result in a loss of precision if int64 values are larger than 2**53. Warning StataWriter and to_stata() only support fixed width strings containing up to 244 characters, a limitation imposed by the version 115 dta file format. Attempting to write Stata dta files with strings longer than 244 characters raises a ValueError. Reading from Stata format# The top-level function read_stata will read a dta file and return either a DataFrame or a StataReader that can be used to read the file incrementally. In [650]: pd.read_stata("stata.dta") Out[650]: index A B 0 0 -1.690072 0.405144 1 1 -1.511309 -1.531396 2 2 0.572698 -1.106845 3 3 -1.185859 0.174564 4 4 0.603797 -1.796129 5 5 -0.791679 1.173795 6 6 -0.277710 1.859988 7 7 -0.258413 1.251808 8 8 1.443262 0.441553 9 9 1.168163 -2.054946 Specifying a chunksize yields a StataReader instance that can be used to read chunksize lines from the file at a time. The StataReader object can be used as an iterator. In [651]: with pd.read_stata("stata.dta", chunksize=3) as reader: .....: for df in reader: .....: print(df.shape) .....: (3, 3) (3, 3) (3, 3) (1, 3) For more fine-grained control, use iterator=True and specify chunksize with each call to read(). In [652]: with pd.read_stata("stata.dta", iterator=True) as reader: .....: chunk1 = reader.read(5) .....: chunk2 = reader.read(5) .....: Currently the index is retrieved as a column. The parameter convert_categoricals indicates whether value labels should be read and used to create a Categorical variable from them. Value labels can also be retrieved by the function value_labels, which requires read() to be called before use. The parameter convert_missing indicates whether missing value representations in Stata should be preserved. If False (the default), missing values are represented as np.nan. If True, missing values are represented using StataMissingValue objects, and columns containing missing values will have object data type. Note read_stata() and StataReader support .dta formats 113-115 (Stata 10-12), 117 (Stata 13), and 118 (Stata 14). Note Setting preserve_dtypes=False will upcast to the standard pandas data types: int64 for all integer types and float64 for floating point data. By default, the Stata data types are preserved when importing. Categorical data# Categorical data can be exported to Stata data files as value labeled data. The exported data consists of the underlying category codes as integer data values and the categories as value labels. Stata does not have an explicit equivalent to a Categorical and information about whether the variable is ordered is lost when exporting. Warning Stata only supports string value labels, and so str is called on the categories when exporting data. Exporting Categorical variables with non-string categories produces a warning, and can result a loss of information if the str representations of the categories are not unique. Labeled data can similarly be imported from Stata data files as Categorical variables using the keyword argument convert_categoricals (True by default). The keyword argument order_categoricals (True by default) determines whether imported Categorical variables are ordered. Note When importing categorical data, the values of the variables in the Stata data file are not preserved since Categorical variables always use integer data types between -1 and n-1 where n is the number of categories. If the original values in the Stata data file are required, these can be imported by setting convert_categoricals=False, which will import original data (but not the variable labels). The original values can be matched to the imported categorical data since there is a simple mapping between the original Stata data values and the category codes of imported Categorical variables: missing values are assigned code -1, and the smallest original value is assigned 0, the second smallest is assigned 1 and so on until the largest original value is assigned the code n-1. Note Stata supports partially labeled series. These series have value labels for some but not all data values. Importing a partially labeled series will produce a Categorical with string categories for the values that are labeled and numeric categories for values with no label. SAS formats# The top-level function read_sas() can read (but not write) SAS XPORT (.xpt) and (since v0.18.0) SAS7BDAT (.sas7bdat) format files. SAS files only contain two value types: ASCII text and floating point values (usually 8 bytes but sometimes truncated). For xport files, there is no automatic type conversion to integers, dates, or categoricals. For SAS7BDAT files, the format codes may allow date variables to be automatically converted to dates. By default the whole file is read and returned as a DataFrame. Specify a chunksize or use iterator=True to obtain reader objects (XportReader or SAS7BDATReader) for incrementally reading the file. The reader objects also have attributes that contain additional information about the file and its variables. Read a SAS7BDAT file: df = pd.read_sas("sas_data.sas7bdat") Obtain an iterator and read an XPORT file 100,000 lines at a time: def do_something(chunk): pass with pd.read_sas("sas_xport.xpt", chunk=100000) as rdr: for chunk in rdr: do_something(chunk) The specification for the xport file format is available from the SAS web site. No official documentation is available for the SAS7BDAT format. SPSS formats# New in version 0.25.0. The top-level function read_spss() can read (but not write) SPSS SAV (.sav) and ZSAV (.zsav) format files. SPSS files contain column names. By default the whole file is read, categorical columns are converted into pd.Categorical, and a DataFrame with all columns is returned. Specify the usecols parameter to obtain a subset of columns. Specify convert_categoricals=False to avoid converting categorical columns into pd.Categorical. Read an SPSS file: df = pd.read_spss("spss_data.sav") Extract a subset of columns contained in usecols from an SPSS file and avoid converting categorical columns into pd.Categorical: df = pd.read_spss( "spss_data.sav", usecols=["foo", "bar"], convert_categoricals=False, ) More information about the SAV and ZSAV file formats is available here. Other file formats# pandas itself only supports IO with a limited set of file formats that map cleanly to its tabular data model. For reading and writing other file formats into and from pandas, we recommend these packages from the broader community. netCDF# xarray provides data structures inspired by the pandas DataFrame for working with multi-dimensional datasets, with a focus on the netCDF file format and easy conversion to and from pandas. Performance considerations# This is an informal comparison of various IO methods, using pandas 0.24.2. Timings are machine dependent and small differences should be ignored. In [1]: sz = 1000000 In [2]: df = pd.DataFrame({'A': np.random.randn(sz), 'B': [1] * sz}) In [3]: df.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 1000000 entries, 0 to 999999 Data columns (total 2 columns): A 1000000 non-null float64 B 1000000 non-null int64 dtypes: float64(1), int64(1) memory usage: 15.3 MB The following test functions will be used below to compare the performance of several IO methods: import numpy as np import os sz = 1000000 df = pd.DataFrame({"A": np.random.randn(sz), "B": [1] * sz}) sz = 1000000 np.random.seed(42) df = pd.DataFrame({"A": np.random.randn(sz), "B": [1] * sz}) def test_sql_write(df): if os.path.exists("test.sql"): os.remove("test.sql") sql_db = sqlite3.connect("test.sql") df.to_sql(name="test_table", con=sql_db) sql_db.close() def test_sql_read(): sql_db = sqlite3.connect("test.sql") pd.read_sql_query("select * from test_table", sql_db) sql_db.close() def test_hdf_fixed_write(df): df.to_hdf("test_fixed.hdf", "test", mode="w") def test_hdf_fixed_read(): pd.read_hdf("test_fixed.hdf", "test") def test_hdf_fixed_write_compress(df): df.to_hdf("test_fixed_compress.hdf", "test", mode="w", complib="blosc") def test_hdf_fixed_read_compress(): pd.read_hdf("test_fixed_compress.hdf", "test") def test_hdf_table_write(df): df.to_hdf("test_table.hdf", "test", mode="w", format="table") def test_hdf_table_read(): pd.read_hdf("test_table.hdf", "test") def test_hdf_table_write_compress(df): df.to_hdf( "test_table_compress.hdf", "test", mode="w", complib="blosc", format="table" ) def test_hdf_table_read_compress(): pd.read_hdf("test_table_compress.hdf", "test") def test_csv_write(df): df.to_csv("test.csv", mode="w") def test_csv_read(): pd.read_csv("test.csv", index_col=0) def test_feather_write(df): df.to_feather("test.feather") def test_feather_read(): pd.read_feather("test.feather") def test_pickle_write(df): df.to_pickle("test.pkl") def test_pickle_read(): pd.read_pickle("test.pkl") def test_pickle_write_compress(df): df.to_pickle("test.pkl.compress", compression="xz") def test_pickle_read_compress(): pd.read_pickle("test.pkl.compress", compression="xz") def test_parquet_write(df): df.to_parquet("test.parquet") def test_parquet_read(): pd.read_parquet("test.parquet") When writing, the top three functions in terms of speed are test_feather_write, test_hdf_fixed_write and test_hdf_fixed_write_compress. In [4]: %timeit test_sql_write(df) 3.29 s ± 43.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [5]: %timeit test_hdf_fixed_write(df) 19.4 ms ± 560 µs per loop (mean ± std. dev. of 7 runs, 1 loop each) In [6]: %timeit test_hdf_fixed_write_compress(df) 19.6 ms ± 308 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [7]: %timeit test_hdf_table_write(df) 449 ms ± 5.61 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [8]: %timeit test_hdf_table_write_compress(df) 448 ms ± 11.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [9]: %timeit test_csv_write(df) 3.66 s ± 26.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [10]: %timeit test_feather_write(df) 9.75 ms ± 117 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [11]: %timeit test_pickle_write(df) 30.1 ms ± 229 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [12]: %timeit test_pickle_write_compress(df) 4.29 s ± 15.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [13]: %timeit test_parquet_write(df) 67.6 ms ± 706 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) When reading, the top three functions in terms of speed are test_feather_read, test_pickle_read and test_hdf_fixed_read. In [14]: %timeit test_sql_read() 1.77 s ± 17.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [15]: %timeit test_hdf_fixed_read() 19.4 ms ± 436 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [16]: %timeit test_hdf_fixed_read_compress() 19.5 ms ± 222 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [17]: %timeit test_hdf_table_read() 38.6 ms ± 857 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [18]: %timeit test_hdf_table_read_compress() 38.8 ms ± 1.49 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) In [19]: %timeit test_csv_read() 452 ms ± 9.04 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [20]: %timeit test_feather_read() 12.4 ms ± 99.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [21]: %timeit test_pickle_read() 18.4 ms ± 191 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [22]: %timeit test_pickle_read_compress() 915 ms ± 7.48 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [23]: %timeit test_parquet_read() 24.4 ms ± 146 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) The files test.pkl.compress, test.parquet and test.feather took the least space on disk (in bytes). 29519500 Oct 10 06:45 test.csv 16000248 Oct 10 06:45 test.feather 8281983 Oct 10 06:49 test.parquet 16000857 Oct 10 06:47 test.pkl 7552144 Oct 10 06:48 test.pkl.compress 34816000 Oct 10 06:42 test.sql 24009288 Oct 10 06:43 test_fixed.hdf 24009288 Oct 10 06:43 test_fixed_compress.hdf 24458940 Oct 10 06:44 test_table.hdf 24458940 Oct 10 06:44 test_table_compress.hdf
340
724
Unable to Read a tsv file in pandas. Gives UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa5 in position 113: invalid start byte I am trying to read a tsv file (one of many) but it is given me the following error. UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa5 in position 113: invalid start byte Before this I have read similar other files using the same code. df = pd.read_csv(fname,sep='\t',error_bad_lines=False) But for this particular, I am getting an error. How can I read this file? Hopefully without missing any data.
A suggestion would be to check waht encoding you actually have. Do it this way: with open('filename.tsv) as f: ### or whatever your etension is print(f) from that you'll obtain the encoding. Then, df=pd.read_csv('filename.tsv', encoding="the encoding that was returned") It would be nice if you could post lien 113 of you dataset, where the first occurrence of the error occured.
63,499,844
Multiply two single index dataframe with multi index dataframe on selected level
<p>I would like to multiply df1 by df2 on <code>base_currency</code> level. There are duplicate in df1 index. I have an error when I try the following code:</p> <pre><code>df1.loc[df1['total'].mul(df2['balance'])] </code></pre> <p>Any help would be appreciated. I don't know exactly what is the best way to do it. should I keep single index <code>base_currency</code> in df1 OR change it to a multi index <code>[base_currency, quoted_currency]</code>?</p> <p>df1:</p> <pre><code> quoted_currency total base_currency BTC USD 1.496949e+00 BTC EUR 2.889100e-03 BTC JPY 1.328009e+04 BTC AUD 5.683850e-03 ETH USD 3.407804e+01 ETH JPY 2.532950e+03 ETH BTC 6.235470e+01 BCH USD 7.352000e-02 BCH JPY 1.637898e+02 CASH JPY 3.304434e+05 CASH ETH 1.218826e+02 CASH BTC 1.314054e+05 CASH USD 1.155685e+03 CASH EUR 1.159377e+03 CASH AUD 1.111565e+03 XRP JPY 6.699868e+06 XRP USD 1.859000e+02 XRP QASH 1.215415e+03 XRP BTC 1.111366e+04 BCH BTC 6.317441e+01 </code></pre> <p>df2:</p> <pre><code> balance currency BCH 3.110515e+04 BTC 1.249170e+06 ETH 4.332074e+04 XRP 3.081000e+01 CASH 4.565000e+00 AUD 7.104848e-01 CNY 1.433651e-01 EUR 1.174428e+00 HKD 1.283253e-01 IDR 5.674280e-05 INR 1.334581e-02 JPY 9.441869e-03 PHP 2.033362e-02 SGD 7.227208e-01 USD 1.000000e+00 </code></pre>
63,499,893
"2020-08-20T06:59:51.877000"
2
null
1
41
python|pandas
<p>Here are possible both solutions:</p> <p>With <code>MultiIndex</code> is simplier, add <code>level=0</code> parameter to <a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.mul.html" rel="nofollow noreferrer"><code>Series.mul</code></a>:</p> <pre><code>df11 = df1.set_index('quoted_currency', append=True) s = df11['total'].mul(df2['balance'], level=0) print (s) base_currency quoted_currency BTC USD 1.869944e+06 EUR 3.608977e+03 JPY 1.658909e+10 AUD 7.100095e+03 ETH USD 1.476286e+06 JPY 1.097293e+08 BTC 2.701252e+06 BCH USD 2.286851e+03 JPY 5.094706e+06 CASH JPY 1.508474e+06 ETH 5.563941e+02 BTC 5.998657e+05 USD 5.275702e+03 EUR 5.292556e+03 AUD 5.074294e+03 XRP JPY 2.064229e+08 USD 5.727579e+03 QASH 3.744694e+04 BTC 3.424119e+05 BCH BTC 1.965049e+06 dtype: float64 </code></pre> <p>Without <code>MultiIndex</code> with <a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Index.intersection.html" rel="nofollow noreferrer"><code>Index.intersection</code></a> and <a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.reindex.html" rel="nofollow noreferrer"><code>Series.reindex</code></a>:</p> <pre><code>idx = df1.index.intersection(df2.index).unique() s = df1['total'].mul(df2['balance'].reindex(idx)) print (s) base_currency BTC 1.869944e+06 BTC 3.608977e+03 BTC 1.658909e+10 BTC 7.100095e+03 ETH 1.476286e+06 ETH 1.097293e+08 ETH 2.701252e+06 BCH 2.286851e+03 BCH 5.094706e+06 CASH 1.508474e+06 CASH 5.563941e+02 CASH 5.998657e+05 CASH 5.275702e+03 CASH 5.292556e+03 CASH 5.074294e+03 XRP 2.064229e+08 XRP 5.727579e+03 XRP 3.744694e+04 XRP 3.424119e+05 BCH 1.965049e+06 dtype: float64 </code></pre>
"2020-08-20T07:03:21.297000"
1
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.multiply.html
Here are possible both solutions: With MultiIndex is simplier, add level=0 parameter to Series.mul: df11 = df1.set_index('quoted_currency', append=True) s = df11['total'].mul(df2['balance'], level=0) print (s) base_currency quoted_currency BTC USD 1.869944e+06 EUR 3.608977e+03 JPY 1.658909e+10 AUD 7.100095e+03 ETH USD 1.476286e+06 JPY 1.097293e+08 BTC 2.701252e+06 BCH USD 2.286851e+03 JPY 5.094706e+06 CASH JPY 1.508474e+06 ETH 5.563941e+02 BTC 5.998657e+05 USD 5.275702e+03 EUR 5.292556e+03 AUD 5.074294e+03 XRP JPY 2.064229e+08 USD 5.727579e+03 QASH 3.744694e+04 BTC 3.424119e+05 BCH BTC 1.965049e+06 dtype: float64 Without MultiIndex with Index.intersection and Series.reindex: idx = df1.index.intersection(df2.index).unique() s = df1['total'].mul(df2['balance'].reindex(idx)) print (s) base_currency BTC 1.869944e+06 BTC 3.608977e+03 BTC 1.658909e+10 BTC 7.100095e+03 ETH 1.476286e+06 ETH 1.097293e+08 ETH 2.701252e+06 BCH 2.286851e+03 BCH 5.094706e+06 CASH 1.508474e+06 CASH 5.563941e+02 CASH 5.998657e+05 CASH 5.275702e+03 CASH 5.292556e+03 CASH 5.074294e+03 XRP 2.064229e+08 XRP 5.727579e+03 XRP 3.744694e+04 XRP 3.424119e+05 BCH 1.965049e+06 dtype: float64
0
1,818
Multiply two single index dataframe with multi index dataframe on selected level I would like to multiply df1 by df2 on base_currency level. There are duplicate in df1 index. I have an error when I try the following code: df1.loc[df1['total'].mul(df2['balance'])] Any help would be appreciated. I don't know exactly what is the best way to do it. should I keep single index base_currency in df1 OR change it to a multi index [base_currency, quoted_currency]? df1: quoted_currency total base_currency BTC USD 1.496949e+00 BTC EUR 2.889100e-03 BTC JPY 1.328009e+04 BTC AUD 5.683850e-03 ETH USD 3.407804e+01 ETH JPY 2.532950e+03 ETH BTC 6.235470e+01 BCH USD 7.352000e-02 BCH JPY 1.637898e+02 CASH JPY 3.304434e+05 CASH ETH 1.218826e+02 CASH BTC 1.314054e+05 CASH USD 1.155685e+03 CASH EUR 1.159377e+03 CASH AUD 1.111565e+03 XRP JPY 6.699868e+06 XRP USD 1.859000e+02 XRP QASH 1.215415e+03 XRP BTC 1.111366e+04 BCH BTC 6.317441e+01 df2: balance currency BCH 3.110515e+04 BTC 1.249170e+06 ETH 4.332074e+04 XRP 3.081000e+01 CASH 4.565000e+00 AUD 7.104848e-01 CNY 1.433651e-01 EUR 1.174428e+00 HKD 1.283253e-01 IDR 5.674280e-05 INR 1.334581e-02 JPY 9.441869e-03 PHP 2.033362e-02 SGD 7.227208e-01 USD 1.000000e+00
float64 /
Here are possible both solutions: With MultiIndex is simplier, add level=0 parameter to Series.mul: df11 = df1.set_index('quoted_currency', append=True) s = df11['total'].mul(df2['balance'], level=0) print (s) base_currency quoted_currency BTC USD 1.869944e+06 EUR 3.608977e+03 JPY 1.658909e+10 AUD 7.100095e+03 ETH USD 1.476286e+06 JPY 1.097293e+08 BTC 2.701252e+06 BCH USD 2.286851e+03 JPY 5.094706e+06 CASH JPY 1.508474e+06 ETH 5.563941e+02 BTC 5.998657e+05 USD 5.275702e+03 EUR 5.292556e+03 AUD 5.074294e+03 XRP JPY 2.064229e+08 USD 5.727579e+03 QASH 3.744694e+04 BTC 3.424119e+05 BCH BTC 1.965049e+06 dtype: float64 Without MultiIndex with Index.intersection and Series.reindex: idx = df1.index.intersection(df2.index).unique() s = df1['total'].mul(df2['balance'].reindex(idx)) print (s) base_currency BTC 1.869944e+06 BTC 3.608977e+03 BTC 1.658909e+10 BTC 7.100095e+03 ETH 1.476286e+06 ETH 1.097293e+08 ETH 2.701252e+06 BCH 2.286851e+03 BCH 5.094706e+06 CASH 1.508474e+06 CASH 5.563941e+02 CASH 5.998657e+05 CASH 5.275702e+03 CASH 5.292556e+03 CASH 5.074294e+03 XRP 2.064229e+08 XRP 5.727579e+03 XRP 3.744694e+04 XRP 3.424119e+05 BCH 1.965049e+06 dtype: float64
69,529,405
Find matching column interval in pandas
<p>I have a pandas dataframe with multiple columns were their values increase from some value between 0 and 1 for column <code>A</code> up to column <code>E</code> which is always 1 (representing cumulative probabilities).</p> <pre><code>ID A B C D E SIM 1: 0.49 0.64 0.86 0.97 1.00 0.98 2: 0.76 0.84 0.98 0.99 1.00 0.87 3: 0.32 0.56 0.72 0.92 1.00 0.12 </code></pre> <p>The column <code>SIM</code> represents a column with random uniform numbers.</p> <p>I wish to add a new column <code>SIM_CAT</code> with values equal to the column-name which value is the right boundary of the interval in which the value in column <code>SIM</code> falls:</p> <pre><code>ID A B C D E SIM SIM_CAT 1: 0.49 0.64 0.86 0.97 1.00 0.98 E 2: 0.76 0.84 0.98 0.99 1.00 0.87 C 3: 0.32 0.56 0.72 0.92 1.00 0.12 A </code></pre> <p>I there a concise way to do that?</p>
69,529,495
"2021-10-11T16:15:44.293000"
1
1
0
46
python|pandas
<p>You can compare columns with <code>SIM</code> and use <a href="https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.idxmax.html" rel="nofollow noreferrer"><code>idxmax</code></a> to find the 1st greater value:</p> <pre><code>cols = list('ABCDE') df['SIM_CAT'] = df[cols].ge(df.SIM, axis=0).idxmax(axis=1) df ID A B C D E SIM SIM_CAT 0 1: 0.49 0.64 0.86 0.97 1.0 0.98 E 1 2: 0.76 0.84 0.98 0.99 1.0 0.87 C 2 3: 0.32 0.56 0.72 0.92 1.0 0.12 A </code></pre> <p>If <code>SIM</code> can contain values greater than 1:</p> <pre><code>cols = list('ABCDE') df['SIM_CAT'] = None df.loc[df.SIM &lt;= 1, 'SIM_CAT'] = df[cols].ge(df.SIM, axis=0).idxmax(axis=1) df ID A B C D E SIM SIM_CAT 0 1: 0.49 0.64 0.86 0.97 1.0 0.98 E 1 2: 0.76 0.84 0.98 0.99 1.0 0.87 C 2 3: 0.32 0.56 0.72 0.92 1.0 0.12 A </code></pre>
"2021-10-11T16:22:13.357000"
1
https://pandas.pydata.org/docs/reference/api/pandas.Interval.html
pandas.Interval# pandas.Interval# class pandas.Interval# Immutable object implementing an Interval, a bounded slice-like interval. You can compare columns with SIM and use idxmax to find the 1st greater value: cols = list('ABCDE') df['SIM_CAT'] = df[cols].ge(df.SIM, axis=0).idxmax(axis=1) df ID A B C D E SIM SIM_CAT 0 1: 0.49 0.64 0.86 0.97 1.0 0.98 E 1 2: 0.76 0.84 0.98 0.99 1.0 0.87 C 2 3: 0.32 0.56 0.72 0.92 1.0 0.12 A If SIM can contain values greater than 1: cols = list('ABCDE') df['SIM_CAT'] = None df.loc[df.SIM <= 1, 'SIM_CAT'] = df[cols].ge(df.SIM, axis=0).idxmax(axis=1) df ID A B C D E SIM SIM_CAT 0 1: 0.49 0.64 0.86 0.97 1.0 0.98 E 1 2: 0.76 0.84 0.98 0.99 1.0 0.87 C 2 3: 0.32 0.56 0.72 0.92 1.0 0.12 A Parameters leftorderable scalarLeft bound for the interval. rightorderable scalarRight bound for the interval. closed{‘right’, ‘left’, ‘both’, ‘neither’}, default ‘right’Whether the interval is closed on the left-side, right-side, both or neither. See the Notes for more detailed explanation. See also IntervalIndexAn Index of Interval objects that are all closed on the same side. cutConvert continuous data into discrete bins (Categorical of Interval objects). qcutConvert continuous data into bins (Categorical of Interval objects) based on quantiles. PeriodRepresents a period of time. Notes The parameters left and right must be from the same type, you must be able to compare them and they must satisfy left <= right. A closed interval (in mathematics denoted by square brackets) contains its endpoints, i.e. the closed interval [0, 5] is characterized by the conditions 0 <= x <= 5. This is what closed='both' stands for. An open interval (in mathematics denoted by parentheses) does not contain its endpoints, i.e. the open interval (0, 5) is characterized by the conditions 0 < x < 5. This is what closed='neither' stands for. Intervals can also be half-open or half-closed, i.e. [0, 5) is described by 0 <= x < 5 (closed='left') and (0, 5] is described by 0 < x <= 5 (closed='right'). Examples It is possible to build Intervals of different types, like numeric ones: >>> iv = pd.Interval(left=0, right=5) >>> iv Interval(0, 5, closed='right') You can check if an element belongs to it, or if it contains another interval: >>> 2.5 in iv True >>> pd.Interval(left=2, right=5, closed='both') in iv True You can test the bounds (closed='right', so 0 < x <= 5): >>> 0 in iv False >>> 5 in iv True >>> 0.0001 in iv True Calculate its length >>> iv.length 5 You can operate with + and * over an Interval and the operation is applied to each of its bounds, so the result depends on the type of the bound elements >>> shifted_iv = iv + 3 >>> shifted_iv Interval(3, 8, closed='right') >>> extended_iv = iv * 10.0 >>> extended_iv Interval(0.0, 50.0, closed='right') To create a time interval you can use Timestamps as the bounds >>> year_2017 = pd.Interval(pd.Timestamp('2017-01-01 00:00:00'), ... pd.Timestamp('2018-01-01 00:00:00'), ... closed='left') >>> pd.Timestamp('2017-01-01 00:00') in year_2017 True >>> year_2017.length Timedelta('365 days 00:00:00') Attributes closed String describing the inclusive side the intervals. closed_left Check if the interval is closed on the left side. closed_right Check if the interval is closed on the right side. is_empty Indicates if an interval is empty, meaning it contains no points. left Left bound for the interval. length Return the length of the Interval. mid Return the midpoint of the Interval. open_left Check if the interval is open on the left side. open_right Check if the interval is open on the right side. right Right bound for the interval. Methods overlaps Check whether two Interval objects overlap.
135
854
Find matching column interval in pandas I have a pandas dataframe with multiple columns were their values increase from some value between 0 and 1 for column A up to column E which is always 1 (representing cumulative probabilities). ID A B C D E SIM 1: 0.49 0.64 0.86 0.97 1.00 0.98 2: 0.76 0.84 0.98 0.99 1.00 0.87 3: 0.32 0.56 0.72 0.92 1.00 0.12 The column SIM represents a column with random uniform numbers. I wish to add a new column SIM_CAT with values equal to the column-name which value is the right boundary of the interval in which the value in column SIM falls: ID A B C D E SIM SIM_CAT 1: 0.49 0.64 0.86 0.97 1.00 0.98 E 2: 0.76 0.84 0.98 0.99 1.00 0.87 C 3: 0.32 0.56 0.72 0.92 1.00 0.12 A I there a concise way to do that?
You can compare columns with SIM and use idxmax to find the 1st greater value: cols = list('ABCDE') df['SIM_CAT'] = df[cols].ge(df.SIM, axis=0).idxmax(axis=1) df ID A B C D E SIM SIM_CAT 0 1: 0.49 0.64 0.86 0.97 1.0 0.98 E 1 2: 0.76 0.84 0.98 0.99 1.0 0.87 C 2 3: 0.32 0.56 0.72 0.92 1.0 0.12 A If SIM can contain values greater than 1: cols = list('ABCDE') df['SIM_CAT'] = None df.loc[df.SIM <= 1, 'SIM_CAT'] = df[cols].ge(df.SIM, axis=0).idxmax(axis=1) df ID A B C D E SIM SIM_CAT 0 1: 0.49 0.64 0.86 0.97 1.0 0.98 E 1 2: 0.76 0.84 0.98 0.99 1.0 0.87 C 2 3: 0.32 0.56 0.72 0.92 1.0 0.12 A
69,963,196
Pandas DF, DateOffset, creating new column
<p>So I'm working with the JHU covid19 data and they've left their recovered dataset go, they're no longer tracking it, just confirmed cases and deaths. What I'm trying to do here is recreate it. The table is the confirmed cases and deaths for every country for every date sorted by date and my getRecovered function below attempts to pick the date for that row, find the date two weeks before that and for the country of that row, and return a 'Recovered' column, which is the confirmed of two weeks ago - the dead today.</p> <p>Maybe a pointless exercise, but still would like to know how to do it haha. I know it's a big dataset also and there's a lot of operations there, but I've been running it 20 mins now and still going. Did I do something wrong or would it just take this long?</p> <p>Thanks for any help, friends.</p> <pre><code>urls = [ 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv', 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv' ] [wget.download(url) for url in urls] confirmed = pd.read_csv('time_series_covid19_confirmed_global.csv') deaths = pd.read_csv('time_series_covid19_deaths_global.csv') dates = confirmed.columns[4:] confirmed_long_form = confirmed.melt( id_vars =['Province/State', 'Country/Region', 'Lat', 'Long'], value_vars=dates, var_name='Date', value_name='Confirmed' ) deaths_long_form = deaths.melt( id_vars =['Province/State', 'Country/Region', 'Lat', 'Long'], value_vars=dates, var_name='Date', value_name='Deaths' ) full_table = confirmed_long_form.merge( right=deaths_long_form, how='left', on=['Province/State', 'Country/Region', 'Date', 'Lat', 'Long'] ) full_table['Date'] = pd.to_datetime(full_table['Date']) full_table = full_table.sort_values(by='Date', ascending=True) </code></pre> <p><a href="https://i.stack.imgur.com/Ez0bW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ez0bW.png" alt="enter image description here" /></a></p> <pre><code>def getRecovered(row): ts = row['Date'] country = row['Country/Region'] ts = pd.Timestamp(ts) do = pd.tseries.offsets.DateOffset(n = 14) newTimeStamp = ts - do oldrow = full_table.loc[(full_table['Date'] == newTimeStamp) &amp; (full_table['Country/Region'] == country)] return oldrow['Confirmed'] - row['Deaths'] full_table['Recovered'] = full_table.apply (lambda row: getRecovered(row), axis=1) full_table </code></pre>
69,963,440
"2021-11-14T12:56:56.957000"
1
null
0
61
python|pandas
<p>Your function is being applied row by row, which is likely why performance is suffering. Pandas is fastest when you make use of vectorised functions. For example you can use</p> <pre><code>pd.to_datetime(full_table['Date']) </code></pre> <p>to convert the whole date column much faster (see here: <a href="https://stackoverflow.com/questions/17134716/convert-dataframe-column-type-from-string-to-datetime">Convert DataFrame column type from string to datetime</a>).</p> <p>You can then add the date offset to that column, something like:</p> <pre><code>full_table['Recovery_date'] = pd.to_datetime(full_table['Date']) - pd.tseries.offsets.DateOffset(n = 14) </code></pre> <p>You can then self merge the table on date==recovery_date (plus any other keys) and subtract the numbers.</p>
"2021-11-14T13:27:18.567000"
1
https://pandas.pydata.org/docs/user_guide/timeseries.html
Time series / date functionality# Time series / date functionality# pandas contains extensive capabilities and features for working with time series data for all domains. Using the NumPy datetime64 and timedelta64 dtypes, pandas has consolidated a large number of features from other Python libraries like scikits.timeseries as well as created a tremendous amount of new functionality for manipulating time series data. For example, pandas supports: Parsing time series information from various sources and formats In [1]: import datetime In [2]: dti = pd.to_datetime( ...: ["1/1/2018", np.datetime64("2018-01-01"), datetime.datetime(2018, 1, 1)] ...: ) ...: In [3]: dti Your function is being applied row by row, which is likely why performance is suffering. Pandas is fastest when you make use of vectorised functions. For example you can use pd.to_datetime(full_table['Date']) to convert the whole date column much faster (see here: Convert DataFrame column type from string to datetime). You can then add the date offset to that column, something like: full_table['Recovery_date'] = pd.to_datetime(full_table['Date']) - pd.tseries.offsets.DateOffset(n = 14) You can then self merge the table on date==recovery_date (plus any other keys) and subtract the numbers. Out[3]: DatetimeIndex(['2018-01-01', '2018-01-01', '2018-01-01'], dtype='datetime64[ns]', freq=None) Generate sequences of fixed-frequency dates and time spans In [4]: dti = pd.date_range("2018-01-01", periods=3, freq="H") In [5]: dti Out[5]: DatetimeIndex(['2018-01-01 00:00:00', '2018-01-01 01:00:00', '2018-01-01 02:00:00'], dtype='datetime64[ns]', freq='H') Manipulating and converting date times with timezone information In [6]: dti = dti.tz_localize("UTC") In [7]: dti Out[7]: DatetimeIndex(['2018-01-01 00:00:00+00:00', '2018-01-01 01:00:00+00:00', '2018-01-01 02:00:00+00:00'], dtype='datetime64[ns, UTC]', freq='H') In [8]: dti.tz_convert("US/Pacific") Out[8]: DatetimeIndex(['2017-12-31 16:00:00-08:00', '2017-12-31 17:00:00-08:00', '2017-12-31 18:00:00-08:00'], dtype='datetime64[ns, US/Pacific]', freq='H') Resampling or converting a time series to a particular frequency In [9]: idx = pd.date_range("2018-01-01", periods=5, freq="H") In [10]: ts = pd.Series(range(len(idx)), index=idx) In [11]: ts Out[11]: 2018-01-01 00:00:00 0 2018-01-01 01:00:00 1 2018-01-01 02:00:00 2 2018-01-01 03:00:00 3 2018-01-01 04:00:00 4 Freq: H, dtype: int64 In [12]: ts.resample("2H").mean() Out[12]: 2018-01-01 00:00:00 0.5 2018-01-01 02:00:00 2.5 2018-01-01 04:00:00 4.0 Freq: 2H, dtype: float64 Performing date and time arithmetic with absolute or relative time increments In [13]: friday = pd.Timestamp("2018-01-05") In [14]: friday.day_name() Out[14]: 'Friday' # Add 1 day In [15]: saturday = friday + pd.Timedelta("1 day") In [16]: saturday.day_name() Out[16]: 'Saturday' # Add 1 business day (Friday --> Monday) In [17]: monday = friday + pd.offsets.BDay() In [18]: monday.day_name() Out[18]: 'Monday' pandas provides a relatively compact and self-contained set of tools for performing the above tasks and more. Overview# pandas captures 4 general time related concepts: Date times: A specific date and time with timezone support. Similar to datetime.datetime from the standard library. Time deltas: An absolute time duration. Similar to datetime.timedelta from the standard library. Time spans: A span of time defined by a point in time and its associated frequency. Date offsets: A relative time duration that respects calendar arithmetic. Similar to dateutil.relativedelta.relativedelta from the dateutil package. Concept Scalar Class Array Class pandas Data Type Primary Creation Method Date times Timestamp DatetimeIndex datetime64[ns] or datetime64[ns, tz] to_datetime or date_range Time deltas Timedelta TimedeltaIndex timedelta64[ns] to_timedelta or timedelta_range Time spans Period PeriodIndex period[freq] Period or period_range Date offsets DateOffset None None DateOffset For time series data, it’s conventional to represent the time component in the index of a Series or DataFrame so manipulations can be performed with respect to the time element. In [19]: pd.Series(range(3), index=pd.date_range("2000", freq="D", periods=3)) Out[19]: 2000-01-01 0 2000-01-02 1 2000-01-03 2 Freq: D, dtype: int64 However, Series and DataFrame can directly also support the time component as data itself. In [20]: pd.Series(pd.date_range("2000", freq="D", periods=3)) Out[20]: 0 2000-01-01 1 2000-01-02 2 2000-01-03 dtype: datetime64[ns] Series and DataFrame have extended data type support and functionality for datetime, timedelta and Period data when passed into those constructors. DateOffset data however will be stored as object data. In [21]: pd.Series(pd.period_range("1/1/2011", freq="M", periods=3)) Out[21]: 0 2011-01 1 2011-02 2 2011-03 dtype: period[M] In [22]: pd.Series([pd.DateOffset(1), pd.DateOffset(2)]) Out[22]: 0 <DateOffset> 1 <2 * DateOffsets> dtype: object In [23]: pd.Series(pd.date_range("1/1/2011", freq="M", periods=3)) Out[23]: 0 2011-01-31 1 2011-02-28 2 2011-03-31 dtype: datetime64[ns] Lastly, pandas represents null date times, time deltas, and time spans as NaT which is useful for representing missing or null date like values and behaves similar as np.nan does for float data. In [24]: pd.Timestamp(pd.NaT) Out[24]: NaT In [25]: pd.Timedelta(pd.NaT) Out[25]: NaT In [26]: pd.Period(pd.NaT) Out[26]: NaT # Equality acts as np.nan would In [27]: pd.NaT == pd.NaT Out[27]: False Timestamps vs. time spans# Timestamped data is the most basic type of time series data that associates values with points in time. For pandas objects it means using the points in time. In [28]: pd.Timestamp(datetime.datetime(2012, 5, 1)) Out[28]: Timestamp('2012-05-01 00:00:00') In [29]: pd.Timestamp("2012-05-01") Out[29]: Timestamp('2012-05-01 00:00:00') In [30]: pd.Timestamp(2012, 5, 1) Out[30]: Timestamp('2012-05-01 00:00:00') However, in many cases it is more natural to associate things like change variables with a time span instead. The span represented by Period can be specified explicitly, or inferred from datetime string format. For example: In [31]: pd.Period("2011-01") Out[31]: Period('2011-01', 'M') In [32]: pd.Period("2012-05", freq="D") Out[32]: Period('2012-05-01', 'D') Timestamp and Period can serve as an index. Lists of Timestamp and Period are automatically coerced to DatetimeIndex and PeriodIndex respectively. In [33]: dates = [ ....: pd.Timestamp("2012-05-01"), ....: pd.Timestamp("2012-05-02"), ....: pd.Timestamp("2012-05-03"), ....: ] ....: In [34]: ts = pd.Series(np.random.randn(3), dates) In [35]: type(ts.index) Out[35]: pandas.core.indexes.datetimes.DatetimeIndex In [36]: ts.index Out[36]: DatetimeIndex(['2012-05-01', '2012-05-02', '2012-05-03'], dtype='datetime64[ns]', freq=None) In [37]: ts Out[37]: 2012-05-01 0.469112 2012-05-02 -0.282863 2012-05-03 -1.509059 dtype: float64 In [38]: periods = [pd.Period("2012-01"), pd.Period("2012-02"), pd.Period("2012-03")] In [39]: ts = pd.Series(np.random.randn(3), periods) In [40]: type(ts.index) Out[40]: pandas.core.indexes.period.PeriodIndex In [41]: ts.index Out[41]: PeriodIndex(['2012-01', '2012-02', '2012-03'], dtype='period[M]') In [42]: ts Out[42]: 2012-01 -1.135632 2012-02 1.212112 2012-03 -0.173215 Freq: M, dtype: float64 pandas allows you to capture both representations and convert between them. Under the hood, pandas represents timestamps using instances of Timestamp and sequences of timestamps using instances of DatetimeIndex. For regular time spans, pandas uses Period objects for scalar values and PeriodIndex for sequences of spans. Better support for irregular intervals with arbitrary start and end points are forth-coming in future releases. Converting to timestamps# To convert a Series or list-like object of date-like objects e.g. strings, epochs, or a mixture, you can use the to_datetime function. When passed a Series, this returns a Series (with the same index), while a list-like is converted to a DatetimeIndex: In [43]: pd.to_datetime(pd.Series(["Jul 31, 2009", "2010-01-10", None])) Out[43]: 0 2009-07-31 1 2010-01-10 2 NaT dtype: datetime64[ns] In [44]: pd.to_datetime(["2005/11/23", "2010.12.31"]) Out[44]: DatetimeIndex(['2005-11-23', '2010-12-31'], dtype='datetime64[ns]', freq=None) If you use dates which start with the day first (i.e. European style), you can pass the dayfirst flag: In [45]: pd.to_datetime(["04-01-2012 10:00"], dayfirst=True) Out[45]: DatetimeIndex(['2012-01-04 10:00:00'], dtype='datetime64[ns]', freq=None) In [46]: pd.to_datetime(["14-01-2012", "01-14-2012"], dayfirst=True) Out[46]: DatetimeIndex(['2012-01-14', '2012-01-14'], dtype='datetime64[ns]', freq=None) Warning You see in the above example that dayfirst isn’t strict. If a date can’t be parsed with the day being first it will be parsed as if dayfirst were False, and in the case of parsing delimited date strings (e.g. 31-12-2012) then a warning will also be raised. If you pass a single string to to_datetime, it returns a single Timestamp. Timestamp can also accept string input, but it doesn’t accept string parsing options like dayfirst or format, so use to_datetime if these are required. In [47]: pd.to_datetime("2010/11/12") Out[47]: Timestamp('2010-11-12 00:00:00') In [48]: pd.Timestamp("2010/11/12") Out[48]: Timestamp('2010-11-12 00:00:00') You can also use the DatetimeIndex constructor directly: In [49]: pd.DatetimeIndex(["2018-01-01", "2018-01-03", "2018-01-05"]) Out[49]: DatetimeIndex(['2018-01-01', '2018-01-03', '2018-01-05'], dtype='datetime64[ns]', freq=None) The string ‘infer’ can be passed in order to set the frequency of the index as the inferred frequency upon creation: In [50]: pd.DatetimeIndex(["2018-01-01", "2018-01-03", "2018-01-05"], freq="infer") Out[50]: DatetimeIndex(['2018-01-01', '2018-01-03', '2018-01-05'], dtype='datetime64[ns]', freq='2D') Providing a format argument# In addition to the required datetime string, a format argument can be passed to ensure specific parsing. This could also potentially speed up the conversion considerably. In [51]: pd.to_datetime("2010/11/12", format="%Y/%m/%d") Out[51]: Timestamp('2010-11-12 00:00:00') In [52]: pd.to_datetime("12-11-2010 00:00", format="%d-%m-%Y %H:%M") Out[52]: Timestamp('2010-11-12 00:00:00') For more information on the choices available when specifying the format option, see the Python datetime documentation. Assembling datetime from multiple DataFrame columns# You can also pass a DataFrame of integer or string columns to assemble into a Series of Timestamps. In [53]: df = pd.DataFrame( ....: {"year": [2015, 2016], "month": [2, 3], "day": [4, 5], "hour": [2, 3]} ....: ) ....: In [54]: pd.to_datetime(df) Out[54]: 0 2015-02-04 02:00:00 1 2016-03-05 03:00:00 dtype: datetime64[ns] You can pass only the columns that you need to assemble. In [55]: pd.to_datetime(df[["year", "month", "day"]]) Out[55]: 0 2015-02-04 1 2016-03-05 dtype: datetime64[ns] pd.to_datetime looks for standard designations of the datetime component in the column names, including: required: year, month, day optional: hour, minute, second, millisecond, microsecond, nanosecond Invalid data# The default behavior, errors='raise', is to raise when unparsable: In [2]: pd.to_datetime(['2009/07/31', 'asd'], errors='raise') ValueError: Unknown string format Pass errors='ignore' to return the original input when unparsable: In [56]: pd.to_datetime(["2009/07/31", "asd"], errors="ignore") Out[56]: Index(['2009/07/31', 'asd'], dtype='object') Pass errors='coerce' to convert unparsable data to NaT (not a time): In [57]: pd.to_datetime(["2009/07/31", "asd"], errors="coerce") Out[57]: DatetimeIndex(['2009-07-31', 'NaT'], dtype='datetime64[ns]', freq=None) Epoch timestamps# pandas supports converting integer or float epoch times to Timestamp and DatetimeIndex. The default unit is nanoseconds, since that is how Timestamp objects are stored internally. However, epochs are often stored in another unit which can be specified. These are computed from the starting point specified by the origin parameter. In [58]: pd.to_datetime( ....: [1349720105, 1349806505, 1349892905, 1349979305, 1350065705], unit="s" ....: ) ....: Out[58]: DatetimeIndex(['2012-10-08 18:15:05', '2012-10-09 18:15:05', '2012-10-10 18:15:05', '2012-10-11 18:15:05', '2012-10-12 18:15:05'], dtype='datetime64[ns]', freq=None) In [59]: pd.to_datetime( ....: [1349720105100, 1349720105200, 1349720105300, 1349720105400, 1349720105500], ....: unit="ms", ....: ) ....: Out[59]: DatetimeIndex(['2012-10-08 18:15:05.100000', '2012-10-08 18:15:05.200000', '2012-10-08 18:15:05.300000', '2012-10-08 18:15:05.400000', '2012-10-08 18:15:05.500000'], dtype='datetime64[ns]', freq=None) Note The unit parameter does not use the same strings as the format parameter that was discussed above). The available units are listed on the documentation for pandas.to_datetime(). Changed in version 1.0.0. Constructing a Timestamp or DatetimeIndex with an epoch timestamp with the tz argument specified will raise a ValueError. If you have epochs in wall time in another timezone, you can read the epochs as timezone-naive timestamps and then localize to the appropriate timezone: In [60]: pd.Timestamp(1262347200000000000).tz_localize("US/Pacific") Out[60]: Timestamp('2010-01-01 12:00:00-0800', tz='US/Pacific') In [61]: pd.DatetimeIndex([1262347200000000000]).tz_localize("US/Pacific") Out[61]: DatetimeIndex(['2010-01-01 12:00:00-08:00'], dtype='datetime64[ns, US/Pacific]', freq=None) Note Epoch times will be rounded to the nearest nanosecond. Warning Conversion of float epoch times can lead to inaccurate and unexpected results. Python floats have about 15 digits precision in decimal. Rounding during conversion from float to high precision Timestamp is unavoidable. The only way to achieve exact precision is to use a fixed-width types (e.g. an int64). In [62]: pd.to_datetime([1490195805.433, 1490195805.433502912], unit="s") Out[62]: DatetimeIndex(['2017-03-22 15:16:45.433000088', '2017-03-22 15:16:45.433502913'], dtype='datetime64[ns]', freq=None) In [63]: pd.to_datetime(1490195805433502912, unit="ns") Out[63]: Timestamp('2017-03-22 15:16:45.433502912') See also Using the origin parameter From timestamps to epoch# To invert the operation from above, namely, to convert from a Timestamp to a ‘unix’ epoch: In [64]: stamps = pd.date_range("2012-10-08 18:15:05", periods=4, freq="D") In [65]: stamps Out[65]: DatetimeIndex(['2012-10-08 18:15:05', '2012-10-09 18:15:05', '2012-10-10 18:15:05', '2012-10-11 18:15:05'], dtype='datetime64[ns]', freq='D') We subtract the epoch (midnight at January 1, 1970 UTC) and then floor divide by the “unit” (1 second). In [66]: (stamps - pd.Timestamp("1970-01-01")) // pd.Timedelta("1s") Out[66]: Int64Index([1349720105, 1349806505, 1349892905, 1349979305], dtype='int64') Using the origin parameter# Using the origin parameter, one can specify an alternative starting point for creation of a DatetimeIndex. For example, to use 1960-01-01 as the starting date: In [67]: pd.to_datetime([1, 2, 3], unit="D", origin=pd.Timestamp("1960-01-01")) Out[67]: DatetimeIndex(['1960-01-02', '1960-01-03', '1960-01-04'], dtype='datetime64[ns]', freq=None) The default is set at origin='unix', which defaults to 1970-01-01 00:00:00. Commonly called ‘unix epoch’ or POSIX time. In [68]: pd.to_datetime([1, 2, 3], unit="D") Out[68]: DatetimeIndex(['1970-01-02', '1970-01-03', '1970-01-04'], dtype='datetime64[ns]', freq=None) Generating ranges of timestamps# To generate an index with timestamps, you can use either the DatetimeIndex or Index constructor and pass in a list of datetime objects: In [69]: dates = [ ....: datetime.datetime(2012, 5, 1), ....: datetime.datetime(2012, 5, 2), ....: datetime.datetime(2012, 5, 3), ....: ] ....: # Note the frequency information In [70]: index = pd.DatetimeIndex(dates) In [71]: index Out[71]: DatetimeIndex(['2012-05-01', '2012-05-02', '2012-05-03'], dtype='datetime64[ns]', freq=None) # Automatically converted to DatetimeIndex In [72]: index = pd.Index(dates) In [73]: index Out[73]: DatetimeIndex(['2012-05-01', '2012-05-02', '2012-05-03'], dtype='datetime64[ns]', freq=None) In practice this becomes very cumbersome because we often need a very long index with a large number of timestamps. If we need timestamps on a regular frequency, we can use the date_range() and bdate_range() functions to create a DatetimeIndex. The default frequency for date_range is a calendar day while the default for bdate_range is a business day: In [74]: start = datetime.datetime(2011, 1, 1) In [75]: end = datetime.datetime(2012, 1, 1) In [76]: index = pd.date_range(start, end) In [77]: index Out[77]: DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03', '2011-01-04', '2011-01-05', '2011-01-06', '2011-01-07', '2011-01-08', '2011-01-09', '2011-01-10', ... '2011-12-23', '2011-12-24', '2011-12-25', '2011-12-26', '2011-12-27', '2011-12-28', '2011-12-29', '2011-12-30', '2011-12-31', '2012-01-01'], dtype='datetime64[ns]', length=366, freq='D') In [78]: index = pd.bdate_range(start, end) In [79]: index Out[79]: DatetimeIndex(['2011-01-03', '2011-01-04', '2011-01-05', '2011-01-06', '2011-01-07', '2011-01-10', '2011-01-11', '2011-01-12', '2011-01-13', '2011-01-14', ... '2011-12-19', '2011-12-20', '2011-12-21', '2011-12-22', '2011-12-23', '2011-12-26', '2011-12-27', '2011-12-28', '2011-12-29', '2011-12-30'], dtype='datetime64[ns]', length=260, freq='B') Convenience functions like date_range and bdate_range can utilize a variety of frequency aliases: In [80]: pd.date_range(start, periods=1000, freq="M") Out[80]: DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-30', '2011-05-31', '2011-06-30', '2011-07-31', '2011-08-31', '2011-09-30', '2011-10-31', ... '2093-07-31', '2093-08-31', '2093-09-30', '2093-10-31', '2093-11-30', '2093-12-31', '2094-01-31', '2094-02-28', '2094-03-31', '2094-04-30'], dtype='datetime64[ns]', length=1000, freq='M') In [81]: pd.bdate_range(start, periods=250, freq="BQS") Out[81]: DatetimeIndex(['2011-01-03', '2011-04-01', '2011-07-01', '2011-10-03', '2012-01-02', '2012-04-02', '2012-07-02', '2012-10-01', '2013-01-01', '2013-04-01', ... '2071-01-01', '2071-04-01', '2071-07-01', '2071-10-01', '2072-01-01', '2072-04-01', '2072-07-01', '2072-10-03', '2073-01-02', '2073-04-03'], dtype='datetime64[ns]', length=250, freq='BQS-JAN') date_range and bdate_range make it easy to generate a range of dates using various combinations of parameters like start, end, periods, and freq. The start and end dates are strictly inclusive, so dates outside of those specified will not be generated: In [82]: pd.date_range(start, end, freq="BM") Out[82]: DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-29', '2011-05-31', '2011-06-30', '2011-07-29', '2011-08-31', '2011-09-30', '2011-10-31', '2011-11-30', '2011-12-30'], dtype='datetime64[ns]', freq='BM') In [83]: pd.date_range(start, end, freq="W") Out[83]: DatetimeIndex(['2011-01-02', '2011-01-09', '2011-01-16', '2011-01-23', '2011-01-30', '2011-02-06', '2011-02-13', '2011-02-20', '2011-02-27', '2011-03-06', '2011-03-13', '2011-03-20', '2011-03-27', '2011-04-03', '2011-04-10', '2011-04-17', '2011-04-24', '2011-05-01', '2011-05-08', '2011-05-15', '2011-05-22', '2011-05-29', '2011-06-05', '2011-06-12', '2011-06-19', '2011-06-26', '2011-07-03', '2011-07-10', '2011-07-17', '2011-07-24', '2011-07-31', '2011-08-07', '2011-08-14', '2011-08-21', '2011-08-28', '2011-09-04', '2011-09-11', '2011-09-18', '2011-09-25', '2011-10-02', '2011-10-09', '2011-10-16', '2011-10-23', '2011-10-30', '2011-11-06', '2011-11-13', '2011-11-20', '2011-11-27', '2011-12-04', '2011-12-11', '2011-12-18', '2011-12-25', '2012-01-01'], dtype='datetime64[ns]', freq='W-SUN') In [84]: pd.bdate_range(end=end, periods=20) Out[84]: DatetimeIndex(['2011-12-05', '2011-12-06', '2011-12-07', '2011-12-08', '2011-12-09', '2011-12-12', '2011-12-13', '2011-12-14', '2011-12-15', '2011-12-16', '2011-12-19', '2011-12-20', '2011-12-21', '2011-12-22', '2011-12-23', '2011-12-26', '2011-12-27', '2011-12-28', '2011-12-29', '2011-12-30'], dtype='datetime64[ns]', freq='B') In [85]: pd.bdate_range(start=start, periods=20) Out[85]: DatetimeIndex(['2011-01-03', '2011-01-04', '2011-01-05', '2011-01-06', '2011-01-07', '2011-01-10', '2011-01-11', '2011-01-12', '2011-01-13', '2011-01-14', '2011-01-17', '2011-01-18', '2011-01-19', '2011-01-20', '2011-01-21', '2011-01-24', '2011-01-25', '2011-01-26', '2011-01-27', '2011-01-28'], dtype='datetime64[ns]', freq='B') Specifying start, end, and periods will generate a range of evenly spaced dates from start to end inclusively, with periods number of elements in the resulting DatetimeIndex: In [86]: pd.date_range("2018-01-01", "2018-01-05", periods=5) Out[86]: DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04', '2018-01-05'], dtype='datetime64[ns]', freq=None) In [87]: pd.date_range("2018-01-01", "2018-01-05", periods=10) Out[87]: DatetimeIndex(['2018-01-01 00:00:00', '2018-01-01 10:40:00', '2018-01-01 21:20:00', '2018-01-02 08:00:00', '2018-01-02 18:40:00', '2018-01-03 05:20:00', '2018-01-03 16:00:00', '2018-01-04 02:40:00', '2018-01-04 13:20:00', '2018-01-05 00:00:00'], dtype='datetime64[ns]', freq=None) Custom frequency ranges# bdate_range can also generate a range of custom frequency dates by using the weekmask and holidays parameters. These parameters will only be used if a custom frequency string is passed. In [88]: weekmask = "Mon Wed Fri" In [89]: holidays = [datetime.datetime(2011, 1, 5), datetime.datetime(2011, 3, 14)] In [90]: pd.bdate_range(start, end, freq="C", weekmask=weekmask, holidays=holidays) Out[90]: DatetimeIndex(['2011-01-03', '2011-01-07', '2011-01-10', '2011-01-12', '2011-01-14', '2011-01-17', '2011-01-19', '2011-01-21', '2011-01-24', '2011-01-26', ... '2011-12-09', '2011-12-12', '2011-12-14', '2011-12-16', '2011-12-19', '2011-12-21', '2011-12-23', '2011-12-26', '2011-12-28', '2011-12-30'], dtype='datetime64[ns]', length=154, freq='C') In [91]: pd.bdate_range(start, end, freq="CBMS", weekmask=weekmask) Out[91]: DatetimeIndex(['2011-01-03', '2011-02-02', '2011-03-02', '2011-04-01', '2011-05-02', '2011-06-01', '2011-07-01', '2011-08-01', '2011-09-02', '2011-10-03', '2011-11-02', '2011-12-02'], dtype='datetime64[ns]', freq='CBMS') See also Custom business days Timestamp limitations# Since pandas represents timestamps in nanosecond resolution, the time span that can be represented using a 64-bit integer is limited to approximately 584 years: In [92]: pd.Timestamp.min Out[92]: Timestamp('1677-09-21 00:12:43.145224193') In [93]: pd.Timestamp.max Out[93]: Timestamp('2262-04-11 23:47:16.854775807') See also Representing out-of-bounds spans Indexing# One of the main uses for DatetimeIndex is as an index for pandas objects. The DatetimeIndex class contains many time series related optimizations: A large range of dates for various offsets are pre-computed and cached under the hood in order to make generating subsequent date ranges very fast (just have to grab a slice). Fast shifting using the shift method on pandas objects. Unioning of overlapping DatetimeIndex objects with the same frequency is very fast (important for fast data alignment). Quick access to date fields via properties such as year, month, etc. Regularization functions like snap and very fast asof logic. DatetimeIndex objects have all the basic functionality of regular Index objects, and a smorgasbord of advanced time series specific methods for easy frequency processing. See also Reindexing methods Note While pandas does not force you to have a sorted date index, some of these methods may have unexpected or incorrect behavior if the dates are unsorted. DatetimeIndex can be used like a regular index and offers all of its intelligent functionality like selection, slicing, etc. In [94]: rng = pd.date_range(start, end, freq="BM") In [95]: ts = pd.Series(np.random.randn(len(rng)), index=rng) In [96]: ts.index Out[96]: DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-29', '2011-05-31', '2011-06-30', '2011-07-29', '2011-08-31', '2011-09-30', '2011-10-31', '2011-11-30', '2011-12-30'], dtype='datetime64[ns]', freq='BM') In [97]: ts[:5].index Out[97]: DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-29', '2011-05-31'], dtype='datetime64[ns]', freq='BM') In [98]: ts[::2].index Out[98]: DatetimeIndex(['2011-01-31', '2011-03-31', '2011-05-31', '2011-07-29', '2011-09-30', '2011-11-30'], dtype='datetime64[ns]', freq='2BM') Partial string indexing# Dates and strings that parse to timestamps can be passed as indexing parameters: In [99]: ts["1/31/2011"] Out[99]: 0.11920871129693428 In [100]: ts[datetime.datetime(2011, 12, 25):] Out[100]: 2011-12-30 0.56702 Freq: BM, dtype: float64 In [101]: ts["10/31/2011":"12/31/2011"] Out[101]: 2011-10-31 0.271860 2011-11-30 -0.424972 2011-12-30 0.567020 Freq: BM, dtype: float64 To provide convenience for accessing longer time series, you can also pass in the year or year and month as strings: In [102]: ts["2011"] Out[102]: 2011-01-31 0.119209 2011-02-28 -1.044236 2011-03-31 -0.861849 2011-04-29 -2.104569 2011-05-31 -0.494929 2011-06-30 1.071804 2011-07-29 0.721555 2011-08-31 -0.706771 2011-09-30 -1.039575 2011-10-31 0.271860 2011-11-30 -0.424972 2011-12-30 0.567020 Freq: BM, dtype: float64 In [103]: ts["2011-6"] Out[103]: 2011-06-30 1.071804 Freq: BM, dtype: float64 This type of slicing will work on a DataFrame with a DatetimeIndex as well. Since the partial string selection is a form of label slicing, the endpoints will be included. This would include matching times on an included date: Warning Indexing DataFrame rows with a single string with getitem (e.g. frame[dtstring]) is deprecated starting with pandas 1.2.0 (given the ambiguity whether it is indexing the rows or selecting a column) and will be removed in a future version. The equivalent with .loc (e.g. frame.loc[dtstring]) is still supported. In [104]: dft = pd.DataFrame( .....: np.random.randn(100000, 1), .....: columns=["A"], .....: index=pd.date_range("20130101", periods=100000, freq="T"), .....: ) .....: In [105]: dft Out[105]: A 2013-01-01 00:00:00 0.276232 2013-01-01 00:01:00 -1.087401 2013-01-01 00:02:00 -0.673690 2013-01-01 00:03:00 0.113648 2013-01-01 00:04:00 -1.478427 ... ... 2013-03-11 10:35:00 -0.747967 2013-03-11 10:36:00 -0.034523 2013-03-11 10:37:00 -0.201754 2013-03-11 10:38:00 -1.509067 2013-03-11 10:39:00 -1.693043 [100000 rows x 1 columns] In [106]: dft.loc["2013"] Out[106]: A 2013-01-01 00:00:00 0.276232 2013-01-01 00:01:00 -1.087401 2013-01-01 00:02:00 -0.673690 2013-01-01 00:03:00 0.113648 2013-01-01 00:04:00 -1.478427 ... ... 2013-03-11 10:35:00 -0.747967 2013-03-11 10:36:00 -0.034523 2013-03-11 10:37:00 -0.201754 2013-03-11 10:38:00 -1.509067 2013-03-11 10:39:00 -1.693043 [100000 rows x 1 columns] This starts on the very first time in the month, and includes the last date and time for the month: In [107]: dft["2013-1":"2013-2"] Out[107]: A 2013-01-01 00:00:00 0.276232 2013-01-01 00:01:00 -1.087401 2013-01-01 00:02:00 -0.673690 2013-01-01 00:03:00 0.113648 2013-01-01 00:04:00 -1.478427 ... ... 2013-02-28 23:55:00 0.850929 2013-02-28 23:56:00 0.976712 2013-02-28 23:57:00 -2.693884 2013-02-28 23:58:00 -1.575535 2013-02-28 23:59:00 -1.573517 [84960 rows x 1 columns] This specifies a stop time that includes all of the times on the last day: In [108]: dft["2013-1":"2013-2-28"] Out[108]: A 2013-01-01 00:00:00 0.276232 2013-01-01 00:01:00 -1.087401 2013-01-01 00:02:00 -0.673690 2013-01-01 00:03:00 0.113648 2013-01-01 00:04:00 -1.478427 ... ... 2013-02-28 23:55:00 0.850929 2013-02-28 23:56:00 0.976712 2013-02-28 23:57:00 -2.693884 2013-02-28 23:58:00 -1.575535 2013-02-28 23:59:00 -1.573517 [84960 rows x 1 columns] This specifies an exact stop time (and is not the same as the above): In [109]: dft["2013-1":"2013-2-28 00:00:00"] Out[109]: A 2013-01-01 00:00:00 0.276232 2013-01-01 00:01:00 -1.087401 2013-01-01 00:02:00 -0.673690 2013-01-01 00:03:00 0.113648 2013-01-01 00:04:00 -1.478427 ... ... 2013-02-27 23:56:00 1.197749 2013-02-27 23:57:00 0.720521 2013-02-27 23:58:00 -0.072718 2013-02-27 23:59:00 -0.681192 2013-02-28 00:00:00 -0.557501 [83521 rows x 1 columns] We are stopping on the included end-point as it is part of the index: In [110]: dft["2013-1-15":"2013-1-15 12:30:00"] Out[110]: A 2013-01-15 00:00:00 -0.984810 2013-01-15 00:01:00 0.941451 2013-01-15 00:02:00 1.559365 2013-01-15 00:03:00 1.034374 2013-01-15 00:04:00 -1.480656 ... ... 2013-01-15 12:26:00 0.371454 2013-01-15 12:27:00 -0.930806 2013-01-15 12:28:00 -0.069177 2013-01-15 12:29:00 0.066510 2013-01-15 12:30:00 -0.003945 [751 rows x 1 columns] DatetimeIndex partial string indexing also works on a DataFrame with a MultiIndex: In [111]: dft2 = pd.DataFrame( .....: np.random.randn(20, 1), .....: columns=["A"], .....: index=pd.MultiIndex.from_product( .....: [pd.date_range("20130101", periods=10, freq="12H"), ["a", "b"]] .....: ), .....: ) .....: In [112]: dft2 Out[112]: A 2013-01-01 00:00:00 a -0.298694 b 0.823553 2013-01-01 12:00:00 a 0.943285 b -1.479399 2013-01-02 00:00:00 a -1.643342 ... ... 2013-01-04 12:00:00 b 0.069036 2013-01-05 00:00:00 a 0.122297 b 1.422060 2013-01-05 12:00:00 a 0.370079 b 1.016331 [20 rows x 1 columns] In [113]: dft2.loc["2013-01-05"] Out[113]: A 2013-01-05 00:00:00 a 0.122297 b 1.422060 2013-01-05 12:00:00 a 0.370079 b 1.016331 In [114]: idx = pd.IndexSlice In [115]: dft2 = dft2.swaplevel(0, 1).sort_index() In [116]: dft2.loc[idx[:, "2013-01-05"], :] Out[116]: A a 2013-01-05 00:00:00 0.122297 2013-01-05 12:00:00 0.370079 b 2013-01-05 00:00:00 1.422060 2013-01-05 12:00:00 1.016331 New in version 0.25.0. Slicing with string indexing also honors UTC offset. In [117]: df = pd.DataFrame([0], index=pd.DatetimeIndex(["2019-01-01"], tz="US/Pacific")) In [118]: df Out[118]: 0 2019-01-01 00:00:00-08:00 0 In [119]: df["2019-01-01 12:00:00+04:00":"2019-01-01 13:00:00+04:00"] Out[119]: 0 2019-01-01 00:00:00-08:00 0 Slice vs. exact match# The same string used as an indexing parameter can be treated either as a slice or as an exact match depending on the resolution of the index. If the string is less accurate than the index, it will be treated as a slice, otherwise as an exact match. Consider a Series object with a minute resolution index: In [120]: series_minute = pd.Series( .....: [1, 2, 3], .....: pd.DatetimeIndex( .....: ["2011-12-31 23:59:00", "2012-01-01 00:00:00", "2012-01-01 00:02:00"] .....: ), .....: ) .....: In [121]: series_minute.index.resolution Out[121]: 'minute' A timestamp string less accurate than a minute gives a Series object. In [122]: series_minute["2011-12-31 23"] Out[122]: 2011-12-31 23:59:00 1 dtype: int64 A timestamp string with minute resolution (or more accurate), gives a scalar instead, i.e. it is not casted to a slice. In [123]: series_minute["2011-12-31 23:59"] Out[123]: 1 In [124]: series_minute["2011-12-31 23:59:00"] Out[124]: 1 If index resolution is second, then the minute-accurate timestamp gives a Series. In [125]: series_second = pd.Series( .....: [1, 2, 3], .....: pd.DatetimeIndex( .....: ["2011-12-31 23:59:59", "2012-01-01 00:00:00", "2012-01-01 00:00:01"] .....: ), .....: ) .....: In [126]: series_second.index.resolution Out[126]: 'second' In [127]: series_second["2011-12-31 23:59"] Out[127]: 2011-12-31 23:59:59 1 dtype: int64 If the timestamp string is treated as a slice, it can be used to index DataFrame with .loc[] as well. In [128]: dft_minute = pd.DataFrame( .....: {"a": [1, 2, 3], "b": [4, 5, 6]}, index=series_minute.index .....: ) .....: In [129]: dft_minute.loc["2011-12-31 23"] Out[129]: a b 2011-12-31 23:59:00 1 4 Warning However, if the string is treated as an exact match, the selection in DataFrame’s [] will be column-wise and not row-wise, see Indexing Basics. For example dft_minute['2011-12-31 23:59'] will raise KeyError as '2012-12-31 23:59' has the same resolution as the index and there is no column with such name: To always have unambiguous selection, whether the row is treated as a slice or a single selection, use .loc. In [130]: dft_minute.loc["2011-12-31 23:59"] Out[130]: a 1 b 4 Name: 2011-12-31 23:59:00, dtype: int64 Note also that DatetimeIndex resolution cannot be less precise than day. In [131]: series_monthly = pd.Series( .....: [1, 2, 3], pd.DatetimeIndex(["2011-12", "2012-01", "2012-02"]) .....: ) .....: In [132]: series_monthly.index.resolution Out[132]: 'day' In [133]: series_monthly["2011-12"] # returns Series Out[133]: 2011-12-01 1 dtype: int64 Exact indexing# As discussed in previous section, indexing a DatetimeIndex with a partial string depends on the “accuracy” of the period, in other words how specific the interval is in relation to the resolution of the index. In contrast, indexing with Timestamp or datetime objects is exact, because the objects have exact meaning. These also follow the semantics of including both endpoints. These Timestamp and datetime objects have exact hours, minutes, and seconds, even though they were not explicitly specified (they are 0). In [134]: dft[datetime.datetime(2013, 1, 1): datetime.datetime(2013, 2, 28)] Out[134]: A 2013-01-01 00:00:00 0.276232 2013-01-01 00:01:00 -1.087401 2013-01-01 00:02:00 -0.673690 2013-01-01 00:03:00 0.113648 2013-01-01 00:04:00 -1.478427 ... ... 2013-02-27 23:56:00 1.197749 2013-02-27 23:57:00 0.720521 2013-02-27 23:58:00 -0.072718 2013-02-27 23:59:00 -0.681192 2013-02-28 00:00:00 -0.557501 [83521 rows x 1 columns] With no defaults. In [135]: dft[ .....: datetime.datetime(2013, 1, 1, 10, 12, 0): datetime.datetime( .....: 2013, 2, 28, 10, 12, 0 .....: ) .....: ] .....: Out[135]: A 2013-01-01 10:12:00 0.565375 2013-01-01 10:13:00 0.068184 2013-01-01 10:14:00 0.788871 2013-01-01 10:15:00 -0.280343 2013-01-01 10:16:00 0.931536 ... ... 2013-02-28 10:08:00 0.148098 2013-02-28 10:09:00 -0.388138 2013-02-28 10:10:00 0.139348 2013-02-28 10:11:00 0.085288 2013-02-28 10:12:00 0.950146 [83521 rows x 1 columns] Truncating & fancy indexing# A truncate() convenience function is provided that is similar to slicing. Note that truncate assumes a 0 value for any unspecified date component in a DatetimeIndex in contrast to slicing which returns any partially matching dates: In [136]: rng2 = pd.date_range("2011-01-01", "2012-01-01", freq="W") In [137]: ts2 = pd.Series(np.random.randn(len(rng2)), index=rng2) In [138]: ts2.truncate(before="2011-11", after="2011-12") Out[138]: 2011-11-06 0.437823 2011-11-13 -0.293083 2011-11-20 -0.059881 2011-11-27 1.252450 Freq: W-SUN, dtype: float64 In [139]: ts2["2011-11":"2011-12"] Out[139]: 2011-11-06 0.437823 2011-11-13 -0.293083 2011-11-20 -0.059881 2011-11-27 1.252450 2011-12-04 0.046611 2011-12-11 0.059478 2011-12-18 -0.286539 2011-12-25 0.841669 Freq: W-SUN, dtype: float64 Even complicated fancy indexing that breaks the DatetimeIndex frequency regularity will result in a DatetimeIndex, although frequency is lost: In [140]: ts2[[0, 2, 6]].index Out[140]: DatetimeIndex(['2011-01-02', '2011-01-16', '2011-02-13'], dtype='datetime64[ns]', freq=None) Time/date components# There are several time/date properties that one can access from Timestamp or a collection of timestamps like a DatetimeIndex. Property Description year The year of the datetime month The month of the datetime day The days of the datetime hour The hour of the datetime minute The minutes of the datetime second The seconds of the datetime microsecond The microseconds of the datetime nanosecond The nanoseconds of the datetime date Returns datetime.date (does not contain timezone information) time Returns datetime.time (does not contain timezone information) timetz Returns datetime.time as local time with timezone information dayofyear The ordinal day of year day_of_year The ordinal day of year weekofyear The week ordinal of the year week The week ordinal of the year dayofweek The number of the day of the week with Monday=0, Sunday=6 day_of_week The number of the day of the week with Monday=0, Sunday=6 weekday The number of the day of the week with Monday=0, Sunday=6 quarter Quarter of the date: Jan-Mar = 1, Apr-Jun = 2, etc. days_in_month The number of days in the month of the datetime is_month_start Logical indicating if first day of month (defined by frequency) is_month_end Logical indicating if last day of month (defined by frequency) is_quarter_start Logical indicating if first day of quarter (defined by frequency) is_quarter_end Logical indicating if last day of quarter (defined by frequency) is_year_start Logical indicating if first day of year (defined by frequency) is_year_end Logical indicating if last day of year (defined by frequency) is_leap_year Logical indicating if the date belongs to a leap year Furthermore, if you have a Series with datetimelike values, then you can access these properties via the .dt accessor, as detailed in the section on .dt accessors. New in version 1.1.0. You may obtain the year, week and day components of the ISO year from the ISO 8601 standard: In [141]: idx = pd.date_range(start="2019-12-29", freq="D", periods=4) In [142]: idx.isocalendar() Out[142]: year week day 2019-12-29 2019 52 7 2019-12-30 2020 1 1 2019-12-31 2020 1 2 2020-01-01 2020 1 3 In [143]: idx.to_series().dt.isocalendar() Out[143]: year week day 2019-12-29 2019 52 7 2019-12-30 2020 1 1 2019-12-31 2020 1 2 2020-01-01 2020 1 3 DateOffset objects# In the preceding examples, frequency strings (e.g. 'D') were used to specify a frequency that defined: how the date times in DatetimeIndex were spaced when using date_range() the frequency of a Period or PeriodIndex These frequency strings map to a DateOffset object and its subclasses. A DateOffset is similar to a Timedelta that represents a duration of time but follows specific calendar duration rules. For example, a Timedelta day will always increment datetimes by 24 hours, while a DateOffset day will increment datetimes to the same time the next day whether a day represents 23, 24 or 25 hours due to daylight savings time. However, all DateOffset subclasses that are an hour or smaller (Hour, Minute, Second, Milli, Micro, Nano) behave like Timedelta and respect absolute time. The basic DateOffset acts similar to dateutil.relativedelta (relativedelta documentation) that shifts a date time by the corresponding calendar duration specified. The arithmetic operator (+) can be used to perform the shift. # This particular day contains a day light savings time transition In [144]: ts = pd.Timestamp("2016-10-30 00:00:00", tz="Europe/Helsinki") # Respects absolute time In [145]: ts + pd.Timedelta(days=1) Out[145]: Timestamp('2016-10-30 23:00:00+0200', tz='Europe/Helsinki') # Respects calendar time In [146]: ts + pd.DateOffset(days=1) Out[146]: Timestamp('2016-10-31 00:00:00+0200', tz='Europe/Helsinki') In [147]: friday = pd.Timestamp("2018-01-05") In [148]: friday.day_name() Out[148]: 'Friday' # Add 2 business days (Friday --> Tuesday) In [149]: two_business_days = 2 * pd.offsets.BDay() In [150]: friday + two_business_days Out[150]: Timestamp('2018-01-09 00:00:00') In [151]: (friday + two_business_days).day_name() Out[151]: 'Tuesday' Most DateOffsets have associated frequencies strings, or offset aliases, that can be passed into freq keyword arguments. The available date offsets and associated frequency strings can be found below: Date Offset Frequency String Description DateOffset None Generic offset class, defaults to absolute 24 hours BDay or BusinessDay 'B' business day (weekday) CDay or CustomBusinessDay 'C' custom business day Week 'W' one week, optionally anchored on a day of the week WeekOfMonth 'WOM' the x-th day of the y-th week of each month LastWeekOfMonth 'LWOM' the x-th day of the last week of each month MonthEnd 'M' calendar month end MonthBegin 'MS' calendar month begin BMonthEnd or BusinessMonthEnd 'BM' business month end BMonthBegin or BusinessMonthBegin 'BMS' business month begin CBMonthEnd or CustomBusinessMonthEnd 'CBM' custom business month end CBMonthBegin or CustomBusinessMonthBegin 'CBMS' custom business month begin SemiMonthEnd 'SM' 15th (or other day_of_month) and calendar month end SemiMonthBegin 'SMS' 15th (or other day_of_month) and calendar month begin QuarterEnd 'Q' calendar quarter end QuarterBegin 'QS' calendar quarter begin BQuarterEnd 'BQ business quarter end BQuarterBegin 'BQS' business quarter begin FY5253Quarter 'REQ' retail (aka 52-53 week) quarter YearEnd 'A' calendar year end YearBegin 'AS' or 'BYS' calendar year begin BYearEnd 'BA' business year end BYearBegin 'BAS' business year begin FY5253 'RE' retail (aka 52-53 week) year Easter None Easter holiday BusinessHour 'BH' business hour CustomBusinessHour 'CBH' custom business hour Day 'D' one absolute day Hour 'H' one hour Minute 'T' or 'min' one minute Second 'S' one second Milli 'L' or 'ms' one millisecond Micro 'U' or 'us' one microsecond Nano 'N' one nanosecond DateOffsets additionally have rollforward() and rollback() methods for moving a date forward or backward respectively to a valid offset date relative to the offset. For example, business offsets will roll dates that land on the weekends (Saturday and Sunday) forward to Monday since business offsets operate on the weekdays. In [152]: ts = pd.Timestamp("2018-01-06 00:00:00") In [153]: ts.day_name() Out[153]: 'Saturday' # BusinessHour's valid offset dates are Monday through Friday In [154]: offset = pd.offsets.BusinessHour(start="09:00") # Bring the date to the closest offset date (Monday) In [155]: offset.rollforward(ts) Out[155]: Timestamp('2018-01-08 09:00:00') # Date is brought to the closest offset date first and then the hour is added In [156]: ts + offset Out[156]: Timestamp('2018-01-08 10:00:00') These operations preserve time (hour, minute, etc) information by default. To reset time to midnight, use normalize() before or after applying the operation (depending on whether you want the time information included in the operation). In [157]: ts = pd.Timestamp("2014-01-01 09:00") In [158]: day = pd.offsets.Day() In [159]: day + ts Out[159]: Timestamp('2014-01-02 09:00:00') In [160]: (day + ts).normalize() Out[160]: Timestamp('2014-01-02 00:00:00') In [161]: ts = pd.Timestamp("2014-01-01 22:00") In [162]: hour = pd.offsets.Hour() In [163]: hour + ts Out[163]: Timestamp('2014-01-01 23:00:00') In [164]: (hour + ts).normalize() Out[164]: Timestamp('2014-01-01 00:00:00') In [165]: (hour + pd.Timestamp("2014-01-01 23:30")).normalize() Out[165]: Timestamp('2014-01-02 00:00:00') Parametric offsets# Some of the offsets can be “parameterized” when created to result in different behaviors. For example, the Week offset for generating weekly data accepts a weekday parameter which results in the generated dates always lying on a particular day of the week: In [166]: d = datetime.datetime(2008, 8, 18, 9, 0) In [167]: d Out[167]: datetime.datetime(2008, 8, 18, 9, 0) In [168]: d + pd.offsets.Week() Out[168]: Timestamp('2008-08-25 09:00:00') In [169]: d + pd.offsets.Week(weekday=4) Out[169]: Timestamp('2008-08-22 09:00:00') In [170]: (d + pd.offsets.Week(weekday=4)).weekday() Out[170]: 4 In [171]: d - pd.offsets.Week() Out[171]: Timestamp('2008-08-11 09:00:00') The normalize option will be effective for addition and subtraction. In [172]: d + pd.offsets.Week(normalize=True) Out[172]: Timestamp('2008-08-25 00:00:00') In [173]: d - pd.offsets.Week(normalize=True) Out[173]: Timestamp('2008-08-11 00:00:00') Another example is parameterizing YearEnd with the specific ending month: In [174]: d + pd.offsets.YearEnd() Out[174]: Timestamp('2008-12-31 09:00:00') In [175]: d + pd.offsets.YearEnd(month=6) Out[175]: Timestamp('2009-06-30 09:00:00') Using offsets with Series / DatetimeIndex# Offsets can be used with either a Series or DatetimeIndex to apply the offset to each element. In [176]: rng = pd.date_range("2012-01-01", "2012-01-03") In [177]: s = pd.Series(rng) In [178]: rng Out[178]: DatetimeIndex(['2012-01-01', '2012-01-02', '2012-01-03'], dtype='datetime64[ns]', freq='D') In [179]: rng + pd.DateOffset(months=2) Out[179]: DatetimeIndex(['2012-03-01', '2012-03-02', '2012-03-03'], dtype='datetime64[ns]', freq=None) In [180]: s + pd.DateOffset(months=2) Out[180]: 0 2012-03-01 1 2012-03-02 2 2012-03-03 dtype: datetime64[ns] In [181]: s - pd.DateOffset(months=2) Out[181]: 0 2011-11-01 1 2011-11-02 2 2011-11-03 dtype: datetime64[ns] If the offset class maps directly to a Timedelta (Day, Hour, Minute, Second, Micro, Milli, Nano) it can be used exactly like a Timedelta - see the Timedelta section for more examples. In [182]: s - pd.offsets.Day(2) Out[182]: 0 2011-12-30 1 2011-12-31 2 2012-01-01 dtype: datetime64[ns] In [183]: td = s - pd.Series(pd.date_range("2011-12-29", "2011-12-31")) In [184]: td Out[184]: 0 3 days 1 3 days 2 3 days dtype: timedelta64[ns] In [185]: td + pd.offsets.Minute(15) Out[185]: 0 3 days 00:15:00 1 3 days 00:15:00 2 3 days 00:15:00 dtype: timedelta64[ns] Note that some offsets (such as BQuarterEnd) do not have a vectorized implementation. They can still be used but may calculate significantly slower and will show a PerformanceWarning In [186]: rng + pd.offsets.BQuarterEnd() Out[186]: DatetimeIndex(['2012-03-30', '2012-03-30', '2012-03-30'], dtype='datetime64[ns]', freq=None) Custom business days# The CDay or CustomBusinessDay class provides a parametric BusinessDay class which can be used to create customized business day calendars which account for local holidays and local weekend conventions. As an interesting example, let’s look at Egypt where a Friday-Saturday weekend is observed. In [187]: weekmask_egypt = "Sun Mon Tue Wed Thu" # They also observe International Workers' Day so let's # add that for a couple of years In [188]: holidays = [ .....: "2012-05-01", .....: datetime.datetime(2013, 5, 1), .....: np.datetime64("2014-05-01"), .....: ] .....: In [189]: bday_egypt = pd.offsets.CustomBusinessDay( .....: holidays=holidays, .....: weekmask=weekmask_egypt, .....: ) .....: In [190]: dt = datetime.datetime(2013, 4, 30) In [191]: dt + 2 * bday_egypt Out[191]: Timestamp('2013-05-05 00:00:00') Let’s map to the weekday names: In [192]: dts = pd.date_range(dt, periods=5, freq=bday_egypt) In [193]: pd.Series(dts.weekday, dts).map(pd.Series("Mon Tue Wed Thu Fri Sat Sun".split())) Out[193]: 2013-04-30 Tue 2013-05-02 Thu 2013-05-05 Sun 2013-05-06 Mon 2013-05-07 Tue Freq: C, dtype: object Holiday calendars can be used to provide the list of holidays. See the holiday calendar section for more information. In [194]: from pandas.tseries.holiday import USFederalHolidayCalendar In [195]: bday_us = pd.offsets.CustomBusinessDay(calendar=USFederalHolidayCalendar()) # Friday before MLK Day In [196]: dt = datetime.datetime(2014, 1, 17) # Tuesday after MLK Day (Monday is skipped because it's a holiday) In [197]: dt + bday_us Out[197]: Timestamp('2014-01-21 00:00:00') Monthly offsets that respect a certain holiday calendar can be defined in the usual way. In [198]: bmth_us = pd.offsets.CustomBusinessMonthBegin(calendar=USFederalHolidayCalendar()) # Skip new years In [199]: dt = datetime.datetime(2013, 12, 17) In [200]: dt + bmth_us Out[200]: Timestamp('2014-01-02 00:00:00') # Define date index with custom offset In [201]: pd.date_range(start="20100101", end="20120101", freq=bmth_us) Out[201]: DatetimeIndex(['2010-01-04', '2010-02-01', '2010-03-01', '2010-04-01', '2010-05-03', '2010-06-01', '2010-07-01', '2010-08-02', '2010-09-01', '2010-10-01', '2010-11-01', '2010-12-01', '2011-01-03', '2011-02-01', '2011-03-01', '2011-04-01', '2011-05-02', '2011-06-01', '2011-07-01', '2011-08-01', '2011-09-01', '2011-10-03', '2011-11-01', '2011-12-01'], dtype='datetime64[ns]', freq='CBMS') Note The frequency string ‘C’ is used to indicate that a CustomBusinessDay DateOffset is used, it is important to note that since CustomBusinessDay is a parameterised type, instances of CustomBusinessDay may differ and this is not detectable from the ‘C’ frequency string. The user therefore needs to ensure that the ‘C’ frequency string is used consistently within the user’s application. Business hour# The BusinessHour class provides a business hour representation on BusinessDay, allowing to use specific start and end times. By default, BusinessHour uses 9:00 - 17:00 as business hours. Adding BusinessHour will increment Timestamp by hourly frequency. If target Timestamp is out of business hours, move to the next business hour then increment it. If the result exceeds the business hours end, the remaining hours are added to the next business day. In [202]: bh = pd.offsets.BusinessHour() In [203]: bh Out[203]: <BusinessHour: BH=09:00-17:00> # 2014-08-01 is Friday In [204]: pd.Timestamp("2014-08-01 10:00").weekday() Out[204]: 4 In [205]: pd.Timestamp("2014-08-01 10:00") + bh Out[205]: Timestamp('2014-08-01 11:00:00') # Below example is the same as: pd.Timestamp('2014-08-01 09:00') + bh In [206]: pd.Timestamp("2014-08-01 08:00") + bh Out[206]: Timestamp('2014-08-01 10:00:00') # If the results is on the end time, move to the next business day In [207]: pd.Timestamp("2014-08-01 16:00") + bh Out[207]: Timestamp('2014-08-04 09:00:00') # Remainings are added to the next day In [208]: pd.Timestamp("2014-08-01 16:30") + bh Out[208]: Timestamp('2014-08-04 09:30:00') # Adding 2 business hours In [209]: pd.Timestamp("2014-08-01 10:00") + pd.offsets.BusinessHour(2) Out[209]: Timestamp('2014-08-01 12:00:00') # Subtracting 3 business hours In [210]: pd.Timestamp("2014-08-01 10:00") + pd.offsets.BusinessHour(-3) Out[210]: Timestamp('2014-07-31 15:00:00') You can also specify start and end time by keywords. The argument must be a str with an hour:minute representation or a datetime.time instance. Specifying seconds, microseconds and nanoseconds as business hour results in ValueError. In [211]: bh = pd.offsets.BusinessHour(start="11:00", end=datetime.time(20, 0)) In [212]: bh Out[212]: <BusinessHour: BH=11:00-20:00> In [213]: pd.Timestamp("2014-08-01 13:00") + bh Out[213]: Timestamp('2014-08-01 14:00:00') In [214]: pd.Timestamp("2014-08-01 09:00") + bh Out[214]: Timestamp('2014-08-01 12:00:00') In [215]: pd.Timestamp("2014-08-01 18:00") + bh Out[215]: Timestamp('2014-08-01 19:00:00') Passing start time later than end represents midnight business hour. In this case, business hour exceeds midnight and overlap to the next day. Valid business hours are distinguished by whether it started from valid BusinessDay. In [216]: bh = pd.offsets.BusinessHour(start="17:00", end="09:00") In [217]: bh Out[217]: <BusinessHour: BH=17:00-09:00> In [218]: pd.Timestamp("2014-08-01 17:00") + bh Out[218]: Timestamp('2014-08-01 18:00:00') In [219]: pd.Timestamp("2014-08-01 23:00") + bh Out[219]: Timestamp('2014-08-02 00:00:00') # Although 2014-08-02 is Saturday, # it is valid because it starts from 08-01 (Friday). In [220]: pd.Timestamp("2014-08-02 04:00") + bh Out[220]: Timestamp('2014-08-02 05:00:00') # Although 2014-08-04 is Monday, # it is out of business hours because it starts from 08-03 (Sunday). In [221]: pd.Timestamp("2014-08-04 04:00") + bh Out[221]: Timestamp('2014-08-04 18:00:00') Applying BusinessHour.rollforward and rollback to out of business hours results in the next business hour start or previous day’s end. Different from other offsets, BusinessHour.rollforward may output different results from apply by definition. This is because one day’s business hour end is equal to next day’s business hour start. For example, under the default business hours (9:00 - 17:00), there is no gap (0 minutes) between 2014-08-01 17:00 and 2014-08-04 09:00. # This adjusts a Timestamp to business hour edge In [222]: pd.offsets.BusinessHour().rollback(pd.Timestamp("2014-08-02 15:00")) Out[222]: Timestamp('2014-08-01 17:00:00') In [223]: pd.offsets.BusinessHour().rollforward(pd.Timestamp("2014-08-02 15:00")) Out[223]: Timestamp('2014-08-04 09:00:00') # It is the same as BusinessHour() + pd.Timestamp('2014-08-01 17:00'). # And it is the same as BusinessHour() + pd.Timestamp('2014-08-04 09:00') In [224]: pd.offsets.BusinessHour() + pd.Timestamp("2014-08-02 15:00") Out[224]: Timestamp('2014-08-04 10:00:00') # BusinessDay results (for reference) In [225]: pd.offsets.BusinessHour().rollforward(pd.Timestamp("2014-08-02")) Out[225]: Timestamp('2014-08-04 09:00:00') # It is the same as BusinessDay() + pd.Timestamp('2014-08-01') # The result is the same as rollworward because BusinessDay never overlap. In [226]: pd.offsets.BusinessHour() + pd.Timestamp("2014-08-02") Out[226]: Timestamp('2014-08-04 10:00:00') BusinessHour regards Saturday and Sunday as holidays. To use arbitrary holidays, you can use CustomBusinessHour offset, as explained in the following subsection. Custom business hour# The CustomBusinessHour is a mixture of BusinessHour and CustomBusinessDay which allows you to specify arbitrary holidays. CustomBusinessHour works as the same as BusinessHour except that it skips specified custom holidays. In [227]: from pandas.tseries.holiday import USFederalHolidayCalendar In [228]: bhour_us = pd.offsets.CustomBusinessHour(calendar=USFederalHolidayCalendar()) # Friday before MLK Day In [229]: dt = datetime.datetime(2014, 1, 17, 15) In [230]: dt + bhour_us Out[230]: Timestamp('2014-01-17 16:00:00') # Tuesday after MLK Day (Monday is skipped because it's a holiday) In [231]: dt + bhour_us * 2 Out[231]: Timestamp('2014-01-21 09:00:00') You can use keyword arguments supported by either BusinessHour and CustomBusinessDay. In [232]: bhour_mon = pd.offsets.CustomBusinessHour(start="10:00", weekmask="Tue Wed Thu Fri") # Monday is skipped because it's a holiday, business hour starts from 10:00 In [233]: dt + bhour_mon * 2 Out[233]: Timestamp('2014-01-21 10:00:00') Offset aliases# A number of string aliases are given to useful common time series frequencies. We will refer to these aliases as offset aliases. Alias Description B business day frequency C custom business day frequency D calendar day frequency W weekly frequency M month end frequency SM semi-month end frequency (15th and end of month) BM business month end frequency CBM custom business month end frequency MS month start frequency SMS semi-month start frequency (1st and 15th) BMS business month start frequency CBMS custom business month start frequency Q quarter end frequency BQ business quarter end frequency QS quarter start frequency BQS business quarter start frequency A, Y year end frequency BA, BY business year end frequency AS, YS year start frequency BAS, BYS business year start frequency BH business hour frequency H hourly frequency T, min minutely frequency S secondly frequency L, ms milliseconds U, us microseconds N nanoseconds Note When using the offset aliases above, it should be noted that functions such as date_range(), bdate_range(), will only return timestamps that are in the interval defined by start_date and end_date. If the start_date does not correspond to the frequency, the returned timestamps will start at the next valid timestamp, same for end_date, the returned timestamps will stop at the previous valid timestamp. For example, for the offset MS, if the start_date is not the first of the month, the returned timestamps will start with the first day of the next month. If end_date is not the first day of a month, the last returned timestamp will be the first day of the corresponding month. In [234]: dates_lst_1 = pd.date_range("2020-01-06", "2020-04-03", freq="MS") In [235]: dates_lst_1 Out[235]: DatetimeIndex(['2020-02-01', '2020-03-01', '2020-04-01'], dtype='datetime64[ns]', freq='MS') In [236]: dates_lst_2 = pd.date_range("2020-01-01", "2020-04-01", freq="MS") In [237]: dates_lst_2 Out[237]: DatetimeIndex(['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01'], dtype='datetime64[ns]', freq='MS') We can see in the above example date_range() and bdate_range() will only return the valid timestamps between the start_date and end_date. If these are not valid timestamps for the given frequency it will roll to the next value for start_date (respectively previous for the end_date) Combining aliases# As we have seen previously, the alias and the offset instance are fungible in most functions: In [238]: pd.date_range(start, periods=5, freq="B") Out[238]: DatetimeIndex(['2011-01-03', '2011-01-04', '2011-01-05', '2011-01-06', '2011-01-07'], dtype='datetime64[ns]', freq='B') In [239]: pd.date_range(start, periods=5, freq=pd.offsets.BDay()) Out[239]: DatetimeIndex(['2011-01-03', '2011-01-04', '2011-01-05', '2011-01-06', '2011-01-07'], dtype='datetime64[ns]', freq='B') You can combine together day and intraday offsets: In [240]: pd.date_range(start, periods=10, freq="2h20min") Out[240]: DatetimeIndex(['2011-01-01 00:00:00', '2011-01-01 02:20:00', '2011-01-01 04:40:00', '2011-01-01 07:00:00', '2011-01-01 09:20:00', '2011-01-01 11:40:00', '2011-01-01 14:00:00', '2011-01-01 16:20:00', '2011-01-01 18:40:00', '2011-01-01 21:00:00'], dtype='datetime64[ns]', freq='140T') In [241]: pd.date_range(start, periods=10, freq="1D10U") Out[241]: DatetimeIndex([ '2011-01-01 00:00:00', '2011-01-02 00:00:00.000010', '2011-01-03 00:00:00.000020', '2011-01-04 00:00:00.000030', '2011-01-05 00:00:00.000040', '2011-01-06 00:00:00.000050', '2011-01-07 00:00:00.000060', '2011-01-08 00:00:00.000070', '2011-01-09 00:00:00.000080', '2011-01-10 00:00:00.000090'], dtype='datetime64[ns]', freq='86400000010U') Anchored offsets# For some frequencies you can specify an anchoring suffix: Alias Description W-SUN weekly frequency (Sundays). Same as ‘W’ W-MON weekly frequency (Mondays) W-TUE weekly frequency (Tuesdays) W-WED weekly frequency (Wednesdays) W-THU weekly frequency (Thursdays) W-FRI weekly frequency (Fridays) W-SAT weekly frequency (Saturdays) (B)Q(S)-DEC quarterly frequency, year ends in December. Same as ‘Q’ (B)Q(S)-JAN quarterly frequency, year ends in January (B)Q(S)-FEB quarterly frequency, year ends in February (B)Q(S)-MAR quarterly frequency, year ends in March (B)Q(S)-APR quarterly frequency, year ends in April (B)Q(S)-MAY quarterly frequency, year ends in May (B)Q(S)-JUN quarterly frequency, year ends in June (B)Q(S)-JUL quarterly frequency, year ends in July (B)Q(S)-AUG quarterly frequency, year ends in August (B)Q(S)-SEP quarterly frequency, year ends in September (B)Q(S)-OCT quarterly frequency, year ends in October (B)Q(S)-NOV quarterly frequency, year ends in November (B)A(S)-DEC annual frequency, anchored end of December. Same as ‘A’ (B)A(S)-JAN annual frequency, anchored end of January (B)A(S)-FEB annual frequency, anchored end of February (B)A(S)-MAR annual frequency, anchored end of March (B)A(S)-APR annual frequency, anchored end of April (B)A(S)-MAY annual frequency, anchored end of May (B)A(S)-JUN annual frequency, anchored end of June (B)A(S)-JUL annual frequency, anchored end of July (B)A(S)-AUG annual frequency, anchored end of August (B)A(S)-SEP annual frequency, anchored end of September (B)A(S)-OCT annual frequency, anchored end of October (B)A(S)-NOV annual frequency, anchored end of November These can be used as arguments to date_range, bdate_range, constructors for DatetimeIndex, as well as various other timeseries-related functions in pandas. Anchored offset semantics# For those offsets that are anchored to the start or end of specific frequency (MonthEnd, MonthBegin, WeekEnd, etc), the following rules apply to rolling forward and backwards. When n is not 0, if the given date is not on an anchor point, it snapped to the next(previous) anchor point, and moved |n|-1 additional steps forwards or backwards. In [242]: pd.Timestamp("2014-01-02") + pd.offsets.MonthBegin(n=1) Out[242]: Timestamp('2014-02-01 00:00:00') In [243]: pd.Timestamp("2014-01-02") + pd.offsets.MonthEnd(n=1) Out[243]: Timestamp('2014-01-31 00:00:00') In [244]: pd.Timestamp("2014-01-02") - pd.offsets.MonthBegin(n=1) Out[244]: Timestamp('2014-01-01 00:00:00') In [245]: pd.Timestamp("2014-01-02") - pd.offsets.MonthEnd(n=1) Out[245]: Timestamp('2013-12-31 00:00:00') In [246]: pd.Timestamp("2014-01-02") + pd.offsets.MonthBegin(n=4) Out[246]: Timestamp('2014-05-01 00:00:00') In [247]: pd.Timestamp("2014-01-02") - pd.offsets.MonthBegin(n=4) Out[247]: Timestamp('2013-10-01 00:00:00') If the given date is on an anchor point, it is moved |n| points forwards or backwards. In [248]: pd.Timestamp("2014-01-01") + pd.offsets.MonthBegin(n=1) Out[248]: Timestamp('2014-02-01 00:00:00') In [249]: pd.Timestamp("2014-01-31") + pd.offsets.MonthEnd(n=1) Out[249]: Timestamp('2014-02-28 00:00:00') In [250]: pd.Timestamp("2014-01-01") - pd.offsets.MonthBegin(n=1) Out[250]: Timestamp('2013-12-01 00:00:00') In [251]: pd.Timestamp("2014-01-31") - pd.offsets.MonthEnd(n=1) Out[251]: Timestamp('2013-12-31 00:00:00') In [252]: pd.Timestamp("2014-01-01") + pd.offsets.MonthBegin(n=4) Out[252]: Timestamp('2014-05-01 00:00:00') In [253]: pd.Timestamp("2014-01-31") - pd.offsets.MonthBegin(n=4) Out[253]: Timestamp('2013-10-01 00:00:00') For the case when n=0, the date is not moved if on an anchor point, otherwise it is rolled forward to the next anchor point. In [254]: pd.Timestamp("2014-01-02") + pd.offsets.MonthBegin(n=0) Out[254]: Timestamp('2014-02-01 00:00:00') In [255]: pd.Timestamp("2014-01-02") + pd.offsets.MonthEnd(n=0) Out[255]: Timestamp('2014-01-31 00:00:00') In [256]: pd.Timestamp("2014-01-01") + pd.offsets.MonthBegin(n=0) Out[256]: Timestamp('2014-01-01 00:00:00') In [257]: pd.Timestamp("2014-01-31") + pd.offsets.MonthEnd(n=0) Out[257]: Timestamp('2014-01-31 00:00:00') Holidays / holiday calendars# Holidays and calendars provide a simple way to define holiday rules to be used with CustomBusinessDay or in other analysis that requires a predefined set of holidays. The AbstractHolidayCalendar class provides all the necessary methods to return a list of holidays and only rules need to be defined in a specific holiday calendar class. Furthermore, the start_date and end_date class attributes determine over what date range holidays are generated. These should be overwritten on the AbstractHolidayCalendar class to have the range apply to all calendar subclasses. USFederalHolidayCalendar is the only calendar that exists and primarily serves as an example for developing other calendars. For holidays that occur on fixed dates (e.g., US Memorial Day or July 4th) an observance rule determines when that holiday is observed if it falls on a weekend or some other non-observed day. Defined observance rules are: Rule Description nearest_workday move Saturday to Friday and Sunday to Monday sunday_to_monday move Sunday to following Monday next_monday_or_tuesday move Saturday to Monday and Sunday/Monday to Tuesday previous_friday move Saturday and Sunday to previous Friday” next_monday move Saturday and Sunday to following Monday An example of how holidays and holiday calendars are defined: In [258]: from pandas.tseries.holiday import ( .....: Holiday, .....: USMemorialDay, .....: AbstractHolidayCalendar, .....: nearest_workday, .....: MO, .....: ) .....: In [259]: class ExampleCalendar(AbstractHolidayCalendar): .....: rules = [ .....: USMemorialDay, .....: Holiday("July 4th", month=7, day=4, observance=nearest_workday), .....: Holiday( .....: "Columbus Day", .....: month=10, .....: day=1, .....: offset=pd.DateOffset(weekday=MO(2)), .....: ), .....: ] .....: In [260]: cal = ExampleCalendar() In [261]: cal.holidays(datetime.datetime(2012, 1, 1), datetime.datetime(2012, 12, 31)) Out[261]: DatetimeIndex(['2012-05-28', '2012-07-04', '2012-10-08'], dtype='datetime64[ns]', freq=None) hint weekday=MO(2) is same as 2 * Week(weekday=2) Using this calendar, creating an index or doing offset arithmetic skips weekends and holidays (i.e., Memorial Day/July 4th). For example, the below defines a custom business day offset using the ExampleCalendar. Like any other offset, it can be used to create a DatetimeIndex or added to datetime or Timestamp objects. In [262]: pd.date_range( .....: start="7/1/2012", end="7/10/2012", freq=pd.offsets.CDay(calendar=cal) .....: ).to_pydatetime() .....: Out[262]: array([datetime.datetime(2012, 7, 2, 0, 0), datetime.datetime(2012, 7, 3, 0, 0), datetime.datetime(2012, 7, 5, 0, 0), datetime.datetime(2012, 7, 6, 0, 0), datetime.datetime(2012, 7, 9, 0, 0), datetime.datetime(2012, 7, 10, 0, 0)], dtype=object) In [263]: offset = pd.offsets.CustomBusinessDay(calendar=cal) In [264]: datetime.datetime(2012, 5, 25) + offset Out[264]: Timestamp('2012-05-29 00:00:00') In [265]: datetime.datetime(2012, 7, 3) + offset Out[265]: Timestamp('2012-07-05 00:00:00') In [266]: datetime.datetime(2012, 7, 3) + 2 * offset Out[266]: Timestamp('2012-07-06 00:00:00') In [267]: datetime.datetime(2012, 7, 6) + offset Out[267]: Timestamp('2012-07-09 00:00:00') Ranges are defined by the start_date and end_date class attributes of AbstractHolidayCalendar. The defaults are shown below. In [268]: AbstractHolidayCalendar.start_date Out[268]: Timestamp('1970-01-01 00:00:00') In [269]: AbstractHolidayCalendar.end_date Out[269]: Timestamp('2200-12-31 00:00:00') These dates can be overwritten by setting the attributes as datetime/Timestamp/string. In [270]: AbstractHolidayCalendar.start_date = datetime.datetime(2012, 1, 1) In [271]: AbstractHolidayCalendar.end_date = datetime.datetime(2012, 12, 31) In [272]: cal.holidays() Out[272]: DatetimeIndex(['2012-05-28', '2012-07-04', '2012-10-08'], dtype='datetime64[ns]', freq=None) Every calendar class is accessible by name using the get_calendar function which returns a holiday class instance. Any imported calendar class will automatically be available by this function. Also, HolidayCalendarFactory provides an easy interface to create calendars that are combinations of calendars or calendars with additional rules. In [273]: from pandas.tseries.holiday import get_calendar, HolidayCalendarFactory, USLaborDay In [274]: cal = get_calendar("ExampleCalendar") In [275]: cal.rules Out[275]: [Holiday: Memorial Day (month=5, day=31, offset=<DateOffset: weekday=MO(-1)>), Holiday: July 4th (month=7, day=4, observance=<function nearest_workday at 0x7f1e67138ee0>), Holiday: Columbus Day (month=10, day=1, offset=<DateOffset: weekday=MO(+2)>)] In [276]: new_cal = HolidayCalendarFactory("NewExampleCalendar", cal, USLaborDay) In [277]: new_cal.rules Out[277]: [Holiday: Labor Day (month=9, day=1, offset=<DateOffset: weekday=MO(+1)>), Holiday: Memorial Day (month=5, day=31, offset=<DateOffset: weekday=MO(-1)>), Holiday: July 4th (month=7, day=4, observance=<function nearest_workday at 0x7f1e67138ee0>), Holiday: Columbus Day (month=10, day=1, offset=<DateOffset: weekday=MO(+2)>)] Time Series-related instance methods# Shifting / lagging# One may want to shift or lag the values in a time series back and forward in time. The method for this is shift(), which is available on all of the pandas objects. In [278]: ts = pd.Series(range(len(rng)), index=rng) In [279]: ts = ts[:5] In [280]: ts.shift(1) Out[280]: 2012-01-01 NaN 2012-01-02 0.0 2012-01-03 1.0 Freq: D, dtype: float64 The shift method accepts an freq argument which can accept a DateOffset class or other timedelta-like object or also an offset alias. When freq is specified, shift method changes all the dates in the index rather than changing the alignment of the data and the index: In [281]: ts.shift(5, freq="D") Out[281]: 2012-01-06 0 2012-01-07 1 2012-01-08 2 Freq: D, dtype: int64 In [282]: ts.shift(5, freq=pd.offsets.BDay()) Out[282]: 2012-01-06 0 2012-01-09 1 2012-01-10 2 dtype: int64 In [283]: ts.shift(5, freq="BM") Out[283]: 2012-05-31 0 2012-05-31 1 2012-05-31 2 dtype: int64 Note that with when freq is specified, the leading entry is no longer NaN because the data is not being realigned. Frequency conversion# The primary function for changing frequencies is the asfreq() method. For a DatetimeIndex, this is basically just a thin, but convenient wrapper around reindex() which generates a date_range and calls reindex. In [284]: dr = pd.date_range("1/1/2010", periods=3, freq=3 * pd.offsets.BDay()) In [285]: ts = pd.Series(np.random.randn(3), index=dr) In [286]: ts Out[286]: 2010-01-01 1.494522 2010-01-06 -0.778425 2010-01-11 -0.253355 Freq: 3B, dtype: float64 In [287]: ts.asfreq(pd.offsets.BDay()) Out[287]: 2010-01-01 1.494522 2010-01-04 NaN 2010-01-05 NaN 2010-01-06 -0.778425 2010-01-07 NaN 2010-01-08 NaN 2010-01-11 -0.253355 Freq: B, dtype: float64 asfreq provides a further convenience so you can specify an interpolation method for any gaps that may appear after the frequency conversion. In [288]: ts.asfreq(pd.offsets.BDay(), method="pad") Out[288]: 2010-01-01 1.494522 2010-01-04 1.494522 2010-01-05 1.494522 2010-01-06 -0.778425 2010-01-07 -0.778425 2010-01-08 -0.778425 2010-01-11 -0.253355 Freq: B, dtype: float64 Filling forward / backward# Related to asfreq and reindex is fillna(), which is documented in the missing data section. Converting to Python datetimes# DatetimeIndex can be converted to an array of Python native datetime.datetime objects using the to_pydatetime method. Resampling# pandas has a simple, powerful, and efficient functionality for performing resampling operations during frequency conversion (e.g., converting secondly data into 5-minutely data). This is extremely common in, but not limited to, financial applications. resample() is a time-based groupby, followed by a reduction method on each of its groups. See some cookbook examples for some advanced strategies. The resample() method can be used directly from DataFrameGroupBy objects, see the groupby docs. Basics# In [289]: rng = pd.date_range("1/1/2012", periods=100, freq="S") In [290]: ts = pd.Series(np.random.randint(0, 500, len(rng)), index=rng) In [291]: ts.resample("5Min").sum() Out[291]: 2012-01-01 25103 Freq: 5T, dtype: int64 The resample function is very flexible and allows you to specify many different parameters to control the frequency conversion and resampling operation. Any function available via dispatching is available as a method of the returned object, including sum, mean, std, sem, max, min, median, first, last, ohlc: In [292]: ts.resample("5Min").mean() Out[292]: 2012-01-01 251.03 Freq: 5T, dtype: float64 In [293]: ts.resample("5Min").ohlc() Out[293]: open high low close 2012-01-01 308 460 9 205 In [294]: ts.resample("5Min").max() Out[294]: 2012-01-01 460 Freq: 5T, dtype: int64 For downsampling, closed can be set to ‘left’ or ‘right’ to specify which end of the interval is closed: In [295]: ts.resample("5Min", closed="right").mean() Out[295]: 2011-12-31 23:55:00 308.000000 2012-01-01 00:00:00 250.454545 Freq: 5T, dtype: float64 In [296]: ts.resample("5Min", closed="left").mean() Out[296]: 2012-01-01 251.03 Freq: 5T, dtype: float64 Parameters like label are used to manipulate the resulting labels. label specifies whether the result is labeled with the beginning or the end of the interval. In [297]: ts.resample("5Min").mean() # by default label='left' Out[297]: 2012-01-01 251.03 Freq: 5T, dtype: float64 In [298]: ts.resample("5Min", label="left").mean() Out[298]: 2012-01-01 251.03 Freq: 5T, dtype: float64 Warning The default values for label and closed is ‘left’ for all frequency offsets except for ‘M’, ‘A’, ‘Q’, ‘BM’, ‘BA’, ‘BQ’, and ‘W’ which all have a default of ‘right’. This might unintendedly lead to looking ahead, where the value for a later time is pulled back to a previous time as in the following example with the BusinessDay frequency: In [299]: s = pd.date_range("2000-01-01", "2000-01-05").to_series() In [300]: s.iloc[2] = pd.NaT In [301]: s.dt.day_name() Out[301]: 2000-01-01 Saturday 2000-01-02 Sunday 2000-01-03 NaN 2000-01-04 Tuesday 2000-01-05 Wednesday Freq: D, dtype: object # default: label='left', closed='left' In [302]: s.resample("B").last().dt.day_name() Out[302]: 1999-12-31 Sunday 2000-01-03 NaN 2000-01-04 Tuesday 2000-01-05 Wednesday Freq: B, dtype: object Notice how the value for Sunday got pulled back to the previous Friday. To get the behavior where the value for Sunday is pushed to Monday, use instead In [303]: s.resample("B", label="right", closed="right").last().dt.day_name() Out[303]: 2000-01-03 Sunday 2000-01-04 Tuesday 2000-01-05 Wednesday Freq: B, dtype: object The axis parameter can be set to 0 or 1 and allows you to resample the specified axis for a DataFrame. kind can be set to ‘timestamp’ or ‘period’ to convert the resulting index to/from timestamp and time span representations. By default resample retains the input representation. convention can be set to ‘start’ or ‘end’ when resampling period data (detail below). It specifies how low frequency periods are converted to higher frequency periods. Upsampling# For upsampling, you can specify a way to upsample and the limit parameter to interpolate over the gaps that are created: # from secondly to every 250 milliseconds In [304]: ts[:2].resample("250L").asfreq() Out[304]: 2012-01-01 00:00:00.000 308.0 2012-01-01 00:00:00.250 NaN 2012-01-01 00:00:00.500 NaN 2012-01-01 00:00:00.750 NaN 2012-01-01 00:00:01.000 204.0 Freq: 250L, dtype: float64 In [305]: ts[:2].resample("250L").ffill() Out[305]: 2012-01-01 00:00:00.000 308 2012-01-01 00:00:00.250 308 2012-01-01 00:00:00.500 308 2012-01-01 00:00:00.750 308 2012-01-01 00:00:01.000 204 Freq: 250L, dtype: int64 In [306]: ts[:2].resample("250L").ffill(limit=2) Out[306]: 2012-01-01 00:00:00.000 308.0 2012-01-01 00:00:00.250 308.0 2012-01-01 00:00:00.500 308.0 2012-01-01 00:00:00.750 NaN 2012-01-01 00:00:01.000 204.0 Freq: 250L, dtype: float64 Sparse resampling# Sparse timeseries are the ones where you have a lot fewer points relative to the amount of time you are looking to resample. Naively upsampling a sparse series can potentially generate lots of intermediate values. When you don’t want to use a method to fill these values, e.g. fill_method is None, then intermediate values will be filled with NaN. Since resample is a time-based groupby, the following is a method to efficiently resample only the groups that are not all NaN. In [307]: rng = pd.date_range("2014-1-1", periods=100, freq="D") + pd.Timedelta("1s") In [308]: ts = pd.Series(range(100), index=rng) If we want to resample to the full range of the series: In [309]: ts.resample("3T").sum() Out[309]: 2014-01-01 00:00:00 0 2014-01-01 00:03:00 0 2014-01-01 00:06:00 0 2014-01-01 00:09:00 0 2014-01-01 00:12:00 0 .. 2014-04-09 23:48:00 0 2014-04-09 23:51:00 0 2014-04-09 23:54:00 0 2014-04-09 23:57:00 0 2014-04-10 00:00:00 99 Freq: 3T, Length: 47521, dtype: int64 We can instead only resample those groups where we have points as follows: In [310]: from functools import partial In [311]: from pandas.tseries.frequencies import to_offset In [312]: def round(t, freq): .....: freq = to_offset(freq) .....: return pd.Timestamp((t.value // freq.delta.value) * freq.delta.value) .....: In [313]: ts.groupby(partial(round, freq="3T")).sum() Out[313]: 2014-01-01 0 2014-01-02 1 2014-01-03 2 2014-01-04 3 2014-01-05 4 .. 2014-04-06 95 2014-04-07 96 2014-04-08 97 2014-04-09 98 2014-04-10 99 Length: 100, dtype: int64 Aggregation# Similar to the aggregating API, groupby API, and the window API, a Resampler can be selectively resampled. Resampling a DataFrame, the default will be to act on all columns with the same function. In [314]: df = pd.DataFrame( .....: np.random.randn(1000, 3), .....: index=pd.date_range("1/1/2012", freq="S", periods=1000), .....: columns=["A", "B", "C"], .....: ) .....: In [315]: r = df.resample("3T") In [316]: r.mean() Out[316]: A B C 2012-01-01 00:00:00 -0.033823 -0.121514 -0.081447 2012-01-01 00:03:00 0.056909 0.146731 -0.024320 2012-01-01 00:06:00 -0.058837 0.047046 -0.052021 2012-01-01 00:09:00 0.063123 -0.026158 -0.066533 2012-01-01 00:12:00 0.186340 -0.003144 0.074752 2012-01-01 00:15:00 -0.085954 -0.016287 -0.050046 We can select a specific column or columns using standard getitem. In [317]: r["A"].mean() Out[317]: 2012-01-01 00:00:00 -0.033823 2012-01-01 00:03:00 0.056909 2012-01-01 00:06:00 -0.058837 2012-01-01 00:09:00 0.063123 2012-01-01 00:12:00 0.186340 2012-01-01 00:15:00 -0.085954 Freq: 3T, Name: A, dtype: float64 In [318]: r[["A", "B"]].mean() Out[318]: A B 2012-01-01 00:00:00 -0.033823 -0.121514 2012-01-01 00:03:00 0.056909 0.146731 2012-01-01 00:06:00 -0.058837 0.047046 2012-01-01 00:09:00 0.063123 -0.026158 2012-01-01 00:12:00 0.186340 -0.003144 2012-01-01 00:15:00 -0.085954 -0.016287 You can pass a list or dict of functions to do aggregation with, outputting a DataFrame: In [319]: r["A"].agg([np.sum, np.mean, np.std]) Out[319]: sum mean std 2012-01-01 00:00:00 -6.088060 -0.033823 1.043263 2012-01-01 00:03:00 10.243678 0.056909 1.058534 2012-01-01 00:06:00 -10.590584 -0.058837 0.949264 2012-01-01 00:09:00 11.362228 0.063123 1.028096 2012-01-01 00:12:00 33.541257 0.186340 0.884586 2012-01-01 00:15:00 -8.595393 -0.085954 1.035476 On a resampled DataFrame, you can pass a list of functions to apply to each column, which produces an aggregated result with a hierarchical index: In [320]: r.agg([np.sum, np.mean]) Out[320]: A ... C sum mean ... sum mean 2012-01-01 00:00:00 -6.088060 -0.033823 ... -14.660515 -0.081447 2012-01-01 00:03:00 10.243678 0.056909 ... -4.377642 -0.024320 2012-01-01 00:06:00 -10.590584 -0.058837 ... -9.363825 -0.052021 2012-01-01 00:09:00 11.362228 0.063123 ... -11.975895 -0.066533 2012-01-01 00:12:00 33.541257 0.186340 ... 13.455299 0.074752 2012-01-01 00:15:00 -8.595393 -0.085954 ... -5.004580 -0.050046 [6 rows x 6 columns] By passing a dict to aggregate you can apply a different aggregation to the columns of a DataFrame: In [321]: r.agg({"A": np.sum, "B": lambda x: np.std(x, ddof=1)}) Out[321]: A B 2012-01-01 00:00:00 -6.088060 1.001294 2012-01-01 00:03:00 10.243678 1.074597 2012-01-01 00:06:00 -10.590584 0.987309 2012-01-01 00:09:00 11.362228 0.944953 2012-01-01 00:12:00 33.541257 1.095025 2012-01-01 00:15:00 -8.595393 1.035312 The function names can also be strings. In order for a string to be valid it must be implemented on the resampled object: In [322]: r.agg({"A": "sum", "B": "std"}) Out[322]: A B 2012-01-01 00:00:00 -6.088060 1.001294 2012-01-01 00:03:00 10.243678 1.074597 2012-01-01 00:06:00 -10.590584 0.987309 2012-01-01 00:09:00 11.362228 0.944953 2012-01-01 00:12:00 33.541257 1.095025 2012-01-01 00:15:00 -8.595393 1.035312 Furthermore, you can also specify multiple aggregation functions for each column separately. In [323]: r.agg({"A": ["sum", "std"], "B": ["mean", "std"]}) Out[323]: A B sum std mean std 2012-01-01 00:00:00 -6.088060 1.043263 -0.121514 1.001294 2012-01-01 00:03:00 10.243678 1.058534 0.146731 1.074597 2012-01-01 00:06:00 -10.590584 0.949264 0.047046 0.987309 2012-01-01 00:09:00 11.362228 1.028096 -0.026158 0.944953 2012-01-01 00:12:00 33.541257 0.884586 -0.003144 1.095025 2012-01-01 00:15:00 -8.595393 1.035476 -0.016287 1.035312 If a DataFrame does not have a datetimelike index, but instead you want to resample based on datetimelike column in the frame, it can passed to the on keyword. In [324]: df = pd.DataFrame( .....: {"date": pd.date_range("2015-01-01", freq="W", periods=5), "a": np.arange(5)}, .....: index=pd.MultiIndex.from_arrays( .....: [[1, 2, 3, 4, 5], pd.date_range("2015-01-01", freq="W", periods=5)], .....: names=["v", "d"], .....: ), .....: ) .....: In [325]: df Out[325]: date a v d 1 2015-01-04 2015-01-04 0 2 2015-01-11 2015-01-11 1 3 2015-01-18 2015-01-18 2 4 2015-01-25 2015-01-25 3 5 2015-02-01 2015-02-01 4 In [326]: df.resample("M", on="date")[["a"]].sum() Out[326]: a date 2015-01-31 6 2015-02-28 4 Similarly, if you instead want to resample by a datetimelike level of MultiIndex, its name or location can be passed to the level keyword. In [327]: df.resample("M", level="d")[["a"]].sum() Out[327]: a d 2015-01-31 6 2015-02-28 4 Iterating through groups# With the Resampler object in hand, iterating through the grouped data is very natural and functions similarly to itertools.groupby(): In [328]: small = pd.Series( .....: range(6), .....: index=pd.to_datetime( .....: [ .....: "2017-01-01T00:00:00", .....: "2017-01-01T00:30:00", .....: "2017-01-01T00:31:00", .....: "2017-01-01T01:00:00", .....: "2017-01-01T03:00:00", .....: "2017-01-01T03:05:00", .....: ] .....: ), .....: ) .....: In [329]: resampled = small.resample("H") In [330]: for name, group in resampled: .....: print("Group: ", name) .....: print("-" * 27) .....: print(group, end="\n\n") .....: Group: 2017-01-01 00:00:00 --------------------------- 2017-01-01 00:00:00 0 2017-01-01 00:30:00 1 2017-01-01 00:31:00 2 dtype: int64 Group: 2017-01-01 01:00:00 --------------------------- 2017-01-01 01:00:00 3 dtype: int64 Group: 2017-01-01 02:00:00 --------------------------- Series([], dtype: int64) Group: 2017-01-01 03:00:00 --------------------------- 2017-01-01 03:00:00 4 2017-01-01 03:05:00 5 dtype: int64 See Iterating through groups or Resampler.__iter__ for more. Use origin or offset to adjust the start of the bins# New in version 1.1.0. The bins of the grouping are adjusted based on the beginning of the day of the time series starting point. This works well with frequencies that are multiples of a day (like 30D) or that divide a day evenly (like 90s or 1min). This can create inconsistencies with some frequencies that do not meet this criteria. To change this behavior you can specify a fixed Timestamp with the argument origin. For example: In [331]: start, end = "2000-10-01 23:30:00", "2000-10-02 00:30:00" In [332]: middle = "2000-10-02 00:00:00" In [333]: rng = pd.date_range(start, end, freq="7min") In [334]: ts = pd.Series(np.arange(len(rng)) * 3, index=rng) In [335]: ts Out[335]: 2000-10-01 23:30:00 0 2000-10-01 23:37:00 3 2000-10-01 23:44:00 6 2000-10-01 23:51:00 9 2000-10-01 23:58:00 12 2000-10-02 00:05:00 15 2000-10-02 00:12:00 18 2000-10-02 00:19:00 21 2000-10-02 00:26:00 24 Freq: 7T, dtype: int64 Here we can see that, when using origin with its default value ('start_day'), the result after '2000-10-02 00:00:00' are not identical depending on the start of time series: In [336]: ts.resample("17min", origin="start_day").sum() Out[336]: 2000-10-01 23:14:00 0 2000-10-01 23:31:00 9 2000-10-01 23:48:00 21 2000-10-02 00:05:00 54 2000-10-02 00:22:00 24 Freq: 17T, dtype: int64 In [337]: ts[middle:end].resample("17min", origin="start_day").sum() Out[337]: 2000-10-02 00:00:00 33 2000-10-02 00:17:00 45 Freq: 17T, dtype: int64 Here we can see that, when setting origin to 'epoch', the result after '2000-10-02 00:00:00' are identical depending on the start of time series: In [338]: ts.resample("17min", origin="epoch").sum() Out[338]: 2000-10-01 23:18:00 0 2000-10-01 23:35:00 18 2000-10-01 23:52:00 27 2000-10-02 00:09:00 39 2000-10-02 00:26:00 24 Freq: 17T, dtype: int64 In [339]: ts[middle:end].resample("17min", origin="epoch").sum() Out[339]: 2000-10-01 23:52:00 15 2000-10-02 00:09:00 39 2000-10-02 00:26:00 24 Freq: 17T, dtype: int64 If needed you can use a custom timestamp for origin: In [340]: ts.resample("17min", origin="2001-01-01").sum() Out[340]: 2000-10-01 23:30:00 9 2000-10-01 23:47:00 21 2000-10-02 00:04:00 54 2000-10-02 00:21:00 24 Freq: 17T, dtype: int64 In [341]: ts[middle:end].resample("17min", origin=pd.Timestamp("2001-01-01")).sum() Out[341]: 2000-10-02 00:04:00 54 2000-10-02 00:21:00 24 Freq: 17T, dtype: int64 If needed you can just adjust the bins with an offset Timedelta that would be added to the default origin. Those two examples are equivalent for this time series: In [342]: ts.resample("17min", origin="start").sum() Out[342]: 2000-10-01 23:30:00 9 2000-10-01 23:47:00 21 2000-10-02 00:04:00 54 2000-10-02 00:21:00 24 Freq: 17T, dtype: int64 In [343]: ts.resample("17min", offset="23h30min").sum() Out[343]: 2000-10-01 23:30:00 9 2000-10-01 23:47:00 21 2000-10-02 00:04:00 54 2000-10-02 00:21:00 24 Freq: 17T, dtype: int64 Note the use of 'start' for origin on the last example. In that case, origin will be set to the first value of the timeseries. Backward resample# New in version 1.3.0. Instead of adjusting the beginning of bins, sometimes we need to fix the end of the bins to make a backward resample with a given freq. The backward resample sets closed to 'right' by default since the last value should be considered as the edge point for the last bin. We can set origin to 'end'. The value for a specific Timestamp index stands for the resample result from the current Timestamp minus freq to the current Timestamp with a right close. In [344]: ts.resample('17min', origin='end').sum() Out[344]: 2000-10-01 23:35:00 0 2000-10-01 23:52:00 18 2000-10-02 00:09:00 27 2000-10-02 00:26:00 63 Freq: 17T, dtype: int64 Besides, in contrast with the 'start_day' option, end_day is supported. This will set the origin as the ceiling midnight of the largest Timestamp. In [345]: ts.resample('17min', origin='end_day').sum() Out[345]: 2000-10-01 23:38:00 3 2000-10-01 23:55:00 15 2000-10-02 00:12:00 45 2000-10-02 00:29:00 45 Freq: 17T, dtype: int64 The above result uses 2000-10-02 00:29:00 as the last bin’s right edge since the following computation. In [346]: ceil_mid = rng.max().ceil('D') In [347]: freq = pd.offsets.Minute(17) In [348]: bin_res = ceil_mid - freq * ((ceil_mid - rng.max()) // freq) In [349]: bin_res Out[349]: Timestamp('2000-10-02 00:29:00') Time span representation# Regular intervals of time are represented by Period objects in pandas while sequences of Period objects are collected in a PeriodIndex, which can be created with the convenience function period_range. Period# A Period represents a span of time (e.g., a day, a month, a quarter, etc). You can specify the span via freq keyword using a frequency alias like below. Because freq represents a span of Period, it cannot be negative like “-3D”. In [350]: pd.Period("2012", freq="A-DEC") Out[350]: Period('2012', 'A-DEC') In [351]: pd.Period("2012-1-1", freq="D") Out[351]: Period('2012-01-01', 'D') In [352]: pd.Period("2012-1-1 19:00", freq="H") Out[352]: Period('2012-01-01 19:00', 'H') In [353]: pd.Period("2012-1-1 19:00", freq="5H") Out[353]: Period('2012-01-01 19:00', '5H') Adding and subtracting integers from periods shifts the period by its own frequency. Arithmetic is not allowed between Period with different freq (span). In [354]: p = pd.Period("2012", freq="A-DEC") In [355]: p + 1 Out[355]: Period('2013', 'A-DEC') In [356]: p - 3 Out[356]: Period('2009', 'A-DEC') In [357]: p = pd.Period("2012-01", freq="2M") In [358]: p + 2 Out[358]: Period('2012-05', '2M') In [359]: p - 1 Out[359]: Period('2011-11', '2M') In [360]: p == pd.Period("2012-01", freq="3M") Out[360]: False If Period freq is daily or higher (D, H, T, S, L, U, N), offsets and timedelta-like can be added if the result can have the same freq. Otherwise, ValueError will be raised. In [361]: p = pd.Period("2014-07-01 09:00", freq="H") In [362]: p + pd.offsets.Hour(2) Out[362]: Period('2014-07-01 11:00', 'H') In [363]: p + datetime.timedelta(minutes=120) Out[363]: Period('2014-07-01 11:00', 'H') In [364]: p + np.timedelta64(7200, "s") Out[364]: Period('2014-07-01 11:00', 'H') In [1]: p + pd.offsets.Minute(5) Traceback ... ValueError: Input has different freq from Period(freq=H) If Period has other frequencies, only the same offsets can be added. Otherwise, ValueError will be raised. In [365]: p = pd.Period("2014-07", freq="M") In [366]: p + pd.offsets.MonthEnd(3) Out[366]: Period('2014-10', 'M') In [1]: p + pd.offsets.MonthBegin(3) Traceback ... ValueError: Input has different freq from Period(freq=M) Taking the difference of Period instances with the same frequency will return the number of frequency units between them: In [367]: pd.Period("2012", freq="A-DEC") - pd.Period("2002", freq="A-DEC") Out[367]: <10 * YearEnds: month=12> PeriodIndex and period_range# Regular sequences of Period objects can be collected in a PeriodIndex, which can be constructed using the period_range convenience function: In [368]: prng = pd.period_range("1/1/2011", "1/1/2012", freq="M") In [369]: prng Out[369]: PeriodIndex(['2011-01', '2011-02', '2011-03', '2011-04', '2011-05', '2011-06', '2011-07', '2011-08', '2011-09', '2011-10', '2011-11', '2011-12', '2012-01'], dtype='period[M]') The PeriodIndex constructor can also be used directly: In [370]: pd.PeriodIndex(["2011-1", "2011-2", "2011-3"], freq="M") Out[370]: PeriodIndex(['2011-01', '2011-02', '2011-03'], dtype='period[M]') Passing multiplied frequency outputs a sequence of Period which has multiplied span. In [371]: pd.period_range(start="2014-01", freq="3M", periods=4) Out[371]: PeriodIndex(['2014-01', '2014-04', '2014-07', '2014-10'], dtype='period[3M]') If start or end are Period objects, they will be used as anchor endpoints for a PeriodIndex with frequency matching that of the PeriodIndex constructor. In [372]: pd.period_range( .....: start=pd.Period("2017Q1", freq="Q"), end=pd.Period("2017Q2", freq="Q"), freq="M" .....: ) .....: Out[372]: PeriodIndex(['2017-03', '2017-04', '2017-05', '2017-06'], dtype='period[M]') Just like DatetimeIndex, a PeriodIndex can also be used to index pandas objects: In [373]: ps = pd.Series(np.random.randn(len(prng)), prng) In [374]: ps Out[374]: 2011-01 -2.916901 2011-02 0.514474 2011-03 1.346470 2011-04 0.816397 2011-05 2.258648 2011-06 0.494789 2011-07 0.301239 2011-08 0.464776 2011-09 -1.393581 2011-10 0.056780 2011-11 0.197035 2011-12 2.261385 2012-01 -0.329583 Freq: M, dtype: float64 PeriodIndex supports addition and subtraction with the same rule as Period. In [375]: idx = pd.period_range("2014-07-01 09:00", periods=5, freq="H") In [376]: idx Out[376]: PeriodIndex(['2014-07-01 09:00', '2014-07-01 10:00', '2014-07-01 11:00', '2014-07-01 12:00', '2014-07-01 13:00'], dtype='period[H]') In [377]: idx + pd.offsets.Hour(2) Out[377]: PeriodIndex(['2014-07-01 11:00', '2014-07-01 12:00', '2014-07-01 13:00', '2014-07-01 14:00', '2014-07-01 15:00'], dtype='period[H]') In [378]: idx = pd.period_range("2014-07", periods=5, freq="M") In [379]: idx Out[379]: PeriodIndex(['2014-07', '2014-08', '2014-09', '2014-10', '2014-11'], dtype='period[M]') In [380]: idx + pd.offsets.MonthEnd(3) Out[380]: PeriodIndex(['2014-10', '2014-11', '2014-12', '2015-01', '2015-02'], dtype='period[M]') PeriodIndex has its own dtype named period, refer to Period Dtypes. Period dtypes# PeriodIndex has a custom period dtype. This is a pandas extension dtype similar to the timezone aware dtype (datetime64[ns, tz]). The period dtype holds the freq attribute and is represented with period[freq] like period[D] or period[M], using frequency strings. In [381]: pi = pd.period_range("2016-01-01", periods=3, freq="M") In [382]: pi Out[382]: PeriodIndex(['2016-01', '2016-02', '2016-03'], dtype='period[M]') In [383]: pi.dtype Out[383]: period[M] The period dtype can be used in .astype(...). It allows one to change the freq of a PeriodIndex like .asfreq() and convert a DatetimeIndex to PeriodIndex like to_period(): # change monthly freq to daily freq In [384]: pi.astype("period[D]") Out[384]: PeriodIndex(['2016-01-31', '2016-02-29', '2016-03-31'], dtype='period[D]') # convert to DatetimeIndex In [385]: pi.astype("datetime64[ns]") Out[385]: DatetimeIndex(['2016-01-01', '2016-02-01', '2016-03-01'], dtype='datetime64[ns]', freq='MS') # convert to PeriodIndex In [386]: dti = pd.date_range("2011-01-01", freq="M", periods=3) In [387]: dti Out[387]: DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31'], dtype='datetime64[ns]', freq='M') In [388]: dti.astype("period[M]") Out[388]: PeriodIndex(['2011-01', '2011-02', '2011-03'], dtype='period[M]') PeriodIndex partial string indexing# PeriodIndex now supports partial string slicing with non-monotonic indexes. New in version 1.1.0. You can pass in dates and strings to Series and DataFrame with PeriodIndex, in the same manner as DatetimeIndex. For details, refer to DatetimeIndex Partial String Indexing. In [389]: ps["2011-01"] Out[389]: -2.9169013294054507 In [390]: ps[datetime.datetime(2011, 12, 25):] Out[390]: 2011-12 2.261385 2012-01 -0.329583 Freq: M, dtype: float64 In [391]: ps["10/31/2011":"12/31/2011"] Out[391]: 2011-10 0.056780 2011-11 0.197035 2011-12 2.261385 Freq: M, dtype: float64 Passing a string representing a lower frequency than PeriodIndex returns partial sliced data. In [392]: ps["2011"] Out[392]: 2011-01 -2.916901 2011-02 0.514474 2011-03 1.346470 2011-04 0.816397 2011-05 2.258648 2011-06 0.494789 2011-07 0.301239 2011-08 0.464776 2011-09 -1.393581 2011-10 0.056780 2011-11 0.197035 2011-12 2.261385 Freq: M, dtype: float64 In [393]: dfp = pd.DataFrame( .....: np.random.randn(600, 1), .....: columns=["A"], .....: index=pd.period_range("2013-01-01 9:00", periods=600, freq="T"), .....: ) .....: In [394]: dfp Out[394]: A 2013-01-01 09:00 -0.538468 2013-01-01 09:01 -1.365819 2013-01-01 09:02 -0.969051 2013-01-01 09:03 -0.331152 2013-01-01 09:04 -0.245334 ... ... 2013-01-01 18:55 0.522460 2013-01-01 18:56 0.118710 2013-01-01 18:57 0.167517 2013-01-01 18:58 0.922883 2013-01-01 18:59 1.721104 [600 rows x 1 columns] In [395]: dfp.loc["2013-01-01 10H"] Out[395]: A 2013-01-01 10:00 -0.308975 2013-01-01 10:01 0.542520 2013-01-01 10:02 1.061068 2013-01-01 10:03 0.754005 2013-01-01 10:04 0.352933 ... ... 2013-01-01 10:55 -0.865621 2013-01-01 10:56 -1.167818 2013-01-01 10:57 -2.081748 2013-01-01 10:58 -0.527146 2013-01-01 10:59 0.802298 [60 rows x 1 columns] As with DatetimeIndex, the endpoints will be included in the result. The example below slices data starting from 10:00 to 11:59. In [396]: dfp["2013-01-01 10H":"2013-01-01 11H"] Out[396]: A 2013-01-01 10:00 -0.308975 2013-01-01 10:01 0.542520 2013-01-01 10:02 1.061068 2013-01-01 10:03 0.754005 2013-01-01 10:04 0.352933 ... ... 2013-01-01 11:55 -0.590204 2013-01-01 11:56 1.539990 2013-01-01 11:57 -1.224826 2013-01-01 11:58 0.578798 2013-01-01 11:59 -0.685496 [120 rows x 1 columns] Frequency conversion and resampling with PeriodIndex# The frequency of Period and PeriodIndex can be converted via the asfreq method. Let’s start with the fiscal year 2011, ending in December: In [397]: p = pd.Period("2011", freq="A-DEC") In [398]: p Out[398]: Period('2011', 'A-DEC') We can convert it to a monthly frequency. Using the how parameter, we can specify whether to return the starting or ending month: In [399]: p.asfreq("M", how="start") Out[399]: Period('2011-01', 'M') In [400]: p.asfreq("M", how="end") Out[400]: Period('2011-12', 'M') The shorthands ‘s’ and ‘e’ are provided for convenience: In [401]: p.asfreq("M", "s") Out[401]: Period('2011-01', 'M') In [402]: p.asfreq("M", "e") Out[402]: Period('2011-12', 'M') Converting to a “super-period” (e.g., annual frequency is a super-period of quarterly frequency) automatically returns the super-period that includes the input period: In [403]: p = pd.Period("2011-12", freq="M") In [404]: p.asfreq("A-NOV") Out[404]: Period('2012', 'A-NOV') Note that since we converted to an annual frequency that ends the year in November, the monthly period of December 2011 is actually in the 2012 A-NOV period. Period conversions with anchored frequencies are particularly useful for working with various quarterly data common to economics, business, and other fields. Many organizations define quarters relative to the month in which their fiscal year starts and ends. Thus, first quarter of 2011 could start in 2010 or a few months into 2011. Via anchored frequencies, pandas works for all quarterly frequencies Q-JAN through Q-DEC. Q-DEC define regular calendar quarters: In [405]: p = pd.Period("2012Q1", freq="Q-DEC") In [406]: p.asfreq("D", "s") Out[406]: Period('2012-01-01', 'D') In [407]: p.asfreq("D", "e") Out[407]: Period('2012-03-31', 'D') Q-MAR defines fiscal year end in March: In [408]: p = pd.Period("2011Q4", freq="Q-MAR") In [409]: p.asfreq("D", "s") Out[409]: Period('2011-01-01', 'D') In [410]: p.asfreq("D", "e") Out[410]: Period('2011-03-31', 'D') Converting between representations# Timestamped data can be converted to PeriodIndex-ed data using to_period and vice-versa using to_timestamp: In [411]: rng = pd.date_range("1/1/2012", periods=5, freq="M") In [412]: ts = pd.Series(np.random.randn(len(rng)), index=rng) In [413]: ts Out[413]: 2012-01-31 1.931253 2012-02-29 -0.184594 2012-03-31 0.249656 2012-04-30 -0.978151 2012-05-31 -0.873389 Freq: M, dtype: float64 In [414]: ps = ts.to_period() In [415]: ps Out[415]: 2012-01 1.931253 2012-02 -0.184594 2012-03 0.249656 2012-04 -0.978151 2012-05 -0.873389 Freq: M, dtype: float64 In [416]: ps.to_timestamp() Out[416]: 2012-01-01 1.931253 2012-02-01 -0.184594 2012-03-01 0.249656 2012-04-01 -0.978151 2012-05-01 -0.873389 Freq: MS, dtype: float64 Remember that ‘s’ and ‘e’ can be used to return the timestamps at the start or end of the period: In [417]: ps.to_timestamp("D", how="s") Out[417]: 2012-01-01 1.931253 2012-02-01 -0.184594 2012-03-01 0.249656 2012-04-01 -0.978151 2012-05-01 -0.873389 Freq: MS, dtype: float64 Converting between period and timestamp enables some convenient arithmetic functions to be used. In the following example, we convert a quarterly frequency with year ending in November to 9am of the end of the month following the quarter end: In [418]: prng = pd.period_range("1990Q1", "2000Q4", freq="Q-NOV") In [419]: ts = pd.Series(np.random.randn(len(prng)), prng) In [420]: ts.index = (prng.asfreq("M", "e") + 1).asfreq("H", "s") + 9 In [421]: ts.head() Out[421]: 1990-03-01 09:00 -0.109291 1990-06-01 09:00 -0.637235 1990-09-01 09:00 -1.735925 1990-12-01 09:00 2.096946 1991-03-01 09:00 -1.039926 Freq: H, dtype: float64 Representing out-of-bounds spans# If you have data that is outside of the Timestamp bounds, see Timestamp limitations, then you can use a PeriodIndex and/or Series of Periods to do computations. In [422]: span = pd.period_range("1215-01-01", "1381-01-01", freq="D") In [423]: span Out[423]: PeriodIndex(['1215-01-01', '1215-01-02', '1215-01-03', '1215-01-04', '1215-01-05', '1215-01-06', '1215-01-07', '1215-01-08', '1215-01-09', '1215-01-10', ... '1380-12-23', '1380-12-24', '1380-12-25', '1380-12-26', '1380-12-27', '1380-12-28', '1380-12-29', '1380-12-30', '1380-12-31', '1381-01-01'], dtype='period[D]', length=60632) To convert from an int64 based YYYYMMDD representation. In [424]: s = pd.Series([20121231, 20141130, 99991231]) In [425]: s Out[425]: 0 20121231 1 20141130 2 99991231 dtype: int64 In [426]: def conv(x): .....: return pd.Period(year=x // 10000, month=x // 100 % 100, day=x % 100, freq="D") .....: In [427]: s.apply(conv) Out[427]: 0 2012-12-31 1 2014-11-30 2 9999-12-31 dtype: period[D] In [428]: s.apply(conv)[2] Out[428]: Period('9999-12-31', 'D') These can easily be converted to a PeriodIndex: In [429]: span = pd.PeriodIndex(s.apply(conv)) In [430]: span Out[430]: PeriodIndex(['2012-12-31', '2014-11-30', '9999-12-31'], dtype='period[D]') Time zone handling# pandas provides rich support for working with timestamps in different time zones using the pytz and dateutil libraries or datetime.timezone objects from the standard library. Working with time zones# By default, pandas objects are time zone unaware: In [431]: rng = pd.date_range("3/6/2012 00:00", periods=15, freq="D") In [432]: rng.tz is None Out[432]: True To localize these dates to a time zone (assign a particular time zone to a naive date), you can use the tz_localize method or the tz keyword argument in date_range(), Timestamp, or DatetimeIndex. You can either pass pytz or dateutil time zone objects or Olson time zone database strings. Olson time zone strings will return pytz time zone objects by default. To return dateutil time zone objects, append dateutil/ before the string. In pytz you can find a list of common (and less common) time zones using from pytz import common_timezones, all_timezones. dateutil uses the OS time zones so there isn’t a fixed list available. For common zones, the names are the same as pytz. In [433]: import dateutil # pytz In [434]: rng_pytz = pd.date_range("3/6/2012 00:00", periods=3, freq="D", tz="Europe/London") In [435]: rng_pytz.tz Out[435]: <DstTzInfo 'Europe/London' LMT-1 day, 23:59:00 STD> # dateutil In [436]: rng_dateutil = pd.date_range("3/6/2012 00:00", periods=3, freq="D") In [437]: rng_dateutil = rng_dateutil.tz_localize("dateutil/Europe/London") In [438]: rng_dateutil.tz Out[438]: tzfile('/usr/share/zoneinfo/Europe/London') # dateutil - utc special case In [439]: rng_utc = pd.date_range( .....: "3/6/2012 00:00", .....: periods=3, .....: freq="D", .....: tz=dateutil.tz.tzutc(), .....: ) .....: In [440]: rng_utc.tz Out[440]: tzutc() New in version 0.25.0. # datetime.timezone In [441]: rng_utc = pd.date_range( .....: "3/6/2012 00:00", .....: periods=3, .....: freq="D", .....: tz=datetime.timezone.utc, .....: ) .....: In [442]: rng_utc.tz Out[442]: datetime.timezone.utc Note that the UTC time zone is a special case in dateutil and should be constructed explicitly as an instance of dateutil.tz.tzutc. You can also construct other time zones objects explicitly first. In [443]: import pytz # pytz In [444]: tz_pytz = pytz.timezone("Europe/London") In [445]: rng_pytz = pd.date_range("3/6/2012 00:00", periods=3, freq="D") In [446]: rng_pytz = rng_pytz.tz_localize(tz_pytz) In [447]: rng_pytz.tz == tz_pytz Out[447]: True # dateutil In [448]: tz_dateutil = dateutil.tz.gettz("Europe/London") In [449]: rng_dateutil = pd.date_range("3/6/2012 00:00", periods=3, freq="D", tz=tz_dateutil) In [450]: rng_dateutil.tz == tz_dateutil Out[450]: True To convert a time zone aware pandas object from one time zone to another, you can use the tz_convert method. In [451]: rng_pytz.tz_convert("US/Eastern") Out[451]: DatetimeIndex(['2012-03-05 19:00:00-05:00', '2012-03-06 19:00:00-05:00', '2012-03-07 19:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq=None) Note When using pytz time zones, DatetimeIndex will construct a different time zone object than a Timestamp for the same time zone input. A DatetimeIndex can hold a collection of Timestamp objects that may have different UTC offsets and cannot be succinctly represented by one pytz time zone instance while one Timestamp represents one point in time with a specific UTC offset. In [452]: dti = pd.date_range("2019-01-01", periods=3, freq="D", tz="US/Pacific") In [453]: dti.tz Out[453]: <DstTzInfo 'US/Pacific' LMT-1 day, 16:07:00 STD> In [454]: ts = pd.Timestamp("2019-01-01", tz="US/Pacific") In [455]: ts.tz Out[455]: <DstTzInfo 'US/Pacific' PST-1 day, 16:00:00 STD> Warning Be wary of conversions between libraries. For some time zones, pytz and dateutil have different definitions of the zone. This is more of a problem for unusual time zones than for ‘standard’ zones like US/Eastern. Warning Be aware that a time zone definition across versions of time zone libraries may not be considered equal. This may cause problems when working with stored data that is localized using one version and operated on with a different version. See here for how to handle such a situation. Warning For pytz time zones, it is incorrect to pass a time zone object directly into the datetime.datetime constructor (e.g., datetime.datetime(2011, 1, 1, tzinfo=pytz.timezone('US/Eastern')). Instead, the datetime needs to be localized using the localize method on the pytz time zone object. Warning Be aware that for times in the future, correct conversion between time zones (and UTC) cannot be guaranteed by any time zone library because a timezone’s offset from UTC may be changed by the respective government. Warning If you are using dates beyond 2038-01-18, due to current deficiencies in the underlying libraries caused by the year 2038 problem, daylight saving time (DST) adjustments to timezone aware dates will not be applied. If and when the underlying libraries are fixed, the DST transitions will be applied. For example, for two dates that are in British Summer Time (and so would normally be GMT+1), both the following asserts evaluate as true: In [456]: d_2037 = "2037-03-31T010101" In [457]: d_2038 = "2038-03-31T010101" In [458]: DST = "Europe/London" In [459]: assert pd.Timestamp(d_2037, tz=DST) != pd.Timestamp(d_2037, tz="GMT") In [460]: assert pd.Timestamp(d_2038, tz=DST) == pd.Timestamp(d_2038, tz="GMT") Under the hood, all timestamps are stored in UTC. Values from a time zone aware DatetimeIndex or Timestamp will have their fields (day, hour, minute, etc.) localized to the time zone. However, timestamps with the same UTC value are still considered to be equal even if they are in different time zones: In [461]: rng_eastern = rng_utc.tz_convert("US/Eastern") In [462]: rng_berlin = rng_utc.tz_convert("Europe/Berlin") In [463]: rng_eastern[2] Out[463]: Timestamp('2012-03-07 19:00:00-0500', tz='US/Eastern', freq='D') In [464]: rng_berlin[2] Out[464]: Timestamp('2012-03-08 01:00:00+0100', tz='Europe/Berlin', freq='D') In [465]: rng_eastern[2] == rng_berlin[2] Out[465]: True Operations between Series in different time zones will yield UTC Series, aligning the data on the UTC timestamps: In [466]: ts_utc = pd.Series(range(3), pd.date_range("20130101", periods=3, tz="UTC")) In [467]: eastern = ts_utc.tz_convert("US/Eastern") In [468]: berlin = ts_utc.tz_convert("Europe/Berlin") In [469]: result = eastern + berlin In [470]: result Out[470]: 2013-01-01 00:00:00+00:00 0 2013-01-02 00:00:00+00:00 2 2013-01-03 00:00:00+00:00 4 Freq: D, dtype: int64 In [471]: result.index Out[471]: DatetimeIndex(['2013-01-01 00:00:00+00:00', '2013-01-02 00:00:00+00:00', '2013-01-03 00:00:00+00:00'], dtype='datetime64[ns, UTC]', freq='D') To remove time zone information, use tz_localize(None) or tz_convert(None). tz_localize(None) will remove the time zone yielding the local time representation. tz_convert(None) will remove the time zone after converting to UTC time. In [472]: didx = pd.date_range(start="2014-08-01 09:00", freq="H", periods=3, tz="US/Eastern") In [473]: didx Out[473]: DatetimeIndex(['2014-08-01 09:00:00-04:00', '2014-08-01 10:00:00-04:00', '2014-08-01 11:00:00-04:00'], dtype='datetime64[ns, US/Eastern]', freq='H') In [474]: didx.tz_localize(None) Out[474]: DatetimeIndex(['2014-08-01 09:00:00', '2014-08-01 10:00:00', '2014-08-01 11:00:00'], dtype='datetime64[ns]', freq=None) In [475]: didx.tz_convert(None) Out[475]: DatetimeIndex(['2014-08-01 13:00:00', '2014-08-01 14:00:00', '2014-08-01 15:00:00'], dtype='datetime64[ns]', freq='H') # tz_convert(None) is identical to tz_convert('UTC').tz_localize(None) In [476]: didx.tz_convert("UTC").tz_localize(None) Out[476]: DatetimeIndex(['2014-08-01 13:00:00', '2014-08-01 14:00:00', '2014-08-01 15:00:00'], dtype='datetime64[ns]', freq=None) Fold# New in version 1.1.0. For ambiguous times, pandas supports explicitly specifying the keyword-only fold argument. Due to daylight saving time, one wall clock time can occur twice when shifting from summer to winter time; fold describes whether the datetime-like corresponds to the first (0) or the second time (1) the wall clock hits the ambiguous time. Fold is supported only for constructing from naive datetime.datetime (see datetime documentation for details) or from Timestamp or for constructing from components (see below). Only dateutil timezones are supported (see dateutil documentation for dateutil methods that deal with ambiguous datetimes) as pytz timezones do not support fold (see pytz documentation for details on how pytz deals with ambiguous datetimes). To localize an ambiguous datetime with pytz, please use Timestamp.tz_localize(). In general, we recommend to rely on Timestamp.tz_localize() when localizing ambiguous datetimes if you need direct control over how they are handled. In [477]: pd.Timestamp( .....: datetime.datetime(2019, 10, 27, 1, 30, 0, 0), .....: tz="dateutil/Europe/London", .....: fold=0, .....: ) .....: Out[477]: Timestamp('2019-10-27 01:30:00+0100', tz='dateutil//usr/share/zoneinfo/Europe/London') In [478]: pd.Timestamp( .....: year=2019, .....: month=10, .....: day=27, .....: hour=1, .....: minute=30, .....: tz="dateutil/Europe/London", .....: fold=1, .....: ) .....: Out[478]: Timestamp('2019-10-27 01:30:00+0000', tz='dateutil//usr/share/zoneinfo/Europe/London') Ambiguous times when localizing# tz_localize may not be able to determine the UTC offset of a timestamp because daylight savings time (DST) in a local time zone causes some times to occur twice within one day (“clocks fall back”). The following options are available: 'raise': Raises a pytz.AmbiguousTimeError (the default behavior) 'infer': Attempt to determine the correct offset base on the monotonicity of the timestamps 'NaT': Replaces ambiguous times with NaT bool: True represents a DST time, False represents non-DST time. An array-like of bool values is supported for a sequence of times. In [479]: rng_hourly = pd.DatetimeIndex( .....: ["11/06/2011 00:00", "11/06/2011 01:00", "11/06/2011 01:00", "11/06/2011 02:00"] .....: ) .....: This will fail as there are ambiguous times ('11/06/2011 01:00') In [2]: rng_hourly.tz_localize('US/Eastern') AmbiguousTimeError: Cannot infer dst time from Timestamp('2011-11-06 01:00:00'), try using the 'ambiguous' argument Handle these ambiguous times by specifying the following. In [480]: rng_hourly.tz_localize("US/Eastern", ambiguous="infer") Out[480]: DatetimeIndex(['2011-11-06 00:00:00-04:00', '2011-11-06 01:00:00-04:00', '2011-11-06 01:00:00-05:00', '2011-11-06 02:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq=None) In [481]: rng_hourly.tz_localize("US/Eastern", ambiguous="NaT") Out[481]: DatetimeIndex(['2011-11-06 00:00:00-04:00', 'NaT', 'NaT', '2011-11-06 02:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq=None) In [482]: rng_hourly.tz_localize("US/Eastern", ambiguous=[True, True, False, False]) Out[482]: DatetimeIndex(['2011-11-06 00:00:00-04:00', '2011-11-06 01:00:00-04:00', '2011-11-06 01:00:00-05:00', '2011-11-06 02:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq=None) Nonexistent times when localizing# A DST transition may also shift the local time ahead by 1 hour creating nonexistent local times (“clocks spring forward”). The behavior of localizing a timeseries with nonexistent times can be controlled by the nonexistent argument. The following options are available: 'raise': Raises a pytz.NonExistentTimeError (the default behavior) 'NaT': Replaces nonexistent times with NaT 'shift_forward': Shifts nonexistent times forward to the closest real time 'shift_backward': Shifts nonexistent times backward to the closest real time timedelta object: Shifts nonexistent times by the timedelta duration In [483]: dti = pd.date_range(start="2015-03-29 02:30:00", periods=3, freq="H") # 2:30 is a nonexistent time Localization of nonexistent times will raise an error by default. In [2]: dti.tz_localize('Europe/Warsaw') NonExistentTimeError: 2015-03-29 02:30:00 Transform nonexistent times to NaT or shift the times. In [484]: dti Out[484]: DatetimeIndex(['2015-03-29 02:30:00', '2015-03-29 03:30:00', '2015-03-29 04:30:00'], dtype='datetime64[ns]', freq='H') In [485]: dti.tz_localize("Europe/Warsaw", nonexistent="shift_forward") Out[485]: DatetimeIndex(['2015-03-29 03:00:00+02:00', '2015-03-29 03:30:00+02:00', '2015-03-29 04:30:00+02:00'], dtype='datetime64[ns, Europe/Warsaw]', freq=None) In [486]: dti.tz_localize("Europe/Warsaw", nonexistent="shift_backward") Out[486]: DatetimeIndex(['2015-03-29 01:59:59.999999999+01:00', '2015-03-29 03:30:00+02:00', '2015-03-29 04:30:00+02:00'], dtype='datetime64[ns, Europe/Warsaw]', freq=None) In [487]: dti.tz_localize("Europe/Warsaw", nonexistent=pd.Timedelta(1, unit="H")) Out[487]: DatetimeIndex(['2015-03-29 03:30:00+02:00', '2015-03-29 03:30:00+02:00', '2015-03-29 04:30:00+02:00'], dtype='datetime64[ns, Europe/Warsaw]', freq=None) In [488]: dti.tz_localize("Europe/Warsaw", nonexistent="NaT") Out[488]: DatetimeIndex(['NaT', '2015-03-29 03:30:00+02:00', '2015-03-29 04:30:00+02:00'], dtype='datetime64[ns, Europe/Warsaw]', freq=None) Time zone Series operations# A Series with time zone naive values is represented with a dtype of datetime64[ns]. In [489]: s_naive = pd.Series(pd.date_range("20130101", periods=3)) In [490]: s_naive Out[490]: 0 2013-01-01 1 2013-01-02 2 2013-01-03 dtype: datetime64[ns] A Series with a time zone aware values is represented with a dtype of datetime64[ns, tz] where tz is the time zone In [491]: s_aware = pd.Series(pd.date_range("20130101", periods=3, tz="US/Eastern")) In [492]: s_aware Out[492]: 0 2013-01-01 00:00:00-05:00 1 2013-01-02 00:00:00-05:00 2 2013-01-03 00:00:00-05:00 dtype: datetime64[ns, US/Eastern] Both of these Series time zone information can be manipulated via the .dt accessor, see the dt accessor section. For example, to localize and convert a naive stamp to time zone aware. In [493]: s_naive.dt.tz_localize("UTC").dt.tz_convert("US/Eastern") Out[493]: 0 2012-12-31 19:00:00-05:00 1 2013-01-01 19:00:00-05:00 2 2013-01-02 19:00:00-05:00 dtype: datetime64[ns, US/Eastern] Time zone information can also be manipulated using the astype method. This method can convert between different timezone-aware dtypes. # convert to a new time zone In [494]: s_aware.astype("datetime64[ns, CET]") Out[494]: 0 2013-01-01 06:00:00+01:00 1 2013-01-02 06:00:00+01:00 2 2013-01-03 06:00:00+01:00 dtype: datetime64[ns, CET] Note Using Series.to_numpy() on a Series, returns a NumPy array of the data. NumPy does not currently support time zones (even though it is printing in the local time zone!), therefore an object array of Timestamps is returned for time zone aware data: In [495]: s_naive.to_numpy() Out[495]: array(['2013-01-01T00:00:00.000000000', '2013-01-02T00:00:00.000000000', '2013-01-03T00:00:00.000000000'], dtype='datetime64[ns]') In [496]: s_aware.to_numpy() Out[496]: array([Timestamp('2013-01-01 00:00:00-0500', tz='US/Eastern'), Timestamp('2013-01-02 00:00:00-0500', tz='US/Eastern'), Timestamp('2013-01-03 00:00:00-0500', tz='US/Eastern')], dtype=object) By converting to an object array of Timestamps, it preserves the time zone information. For example, when converting back to a Series: In [497]: pd.Series(s_aware.to_numpy()) Out[497]: 0 2013-01-01 00:00:00-05:00 1 2013-01-02 00:00:00-05:00 2 2013-01-03 00:00:00-05:00 dtype: datetime64[ns, US/Eastern] However, if you want an actual NumPy datetime64[ns] array (with the values converted to UTC) instead of an array of objects, you can specify the dtype argument: In [498]: s_aware.to_numpy(dtype="datetime64[ns]") Out[498]: array(['2013-01-01T05:00:00.000000000', '2013-01-02T05:00:00.000000000', '2013-01-03T05:00:00.000000000'], dtype='datetime64[ns]')
689
1,286
Pandas DF, DateOffset, creating new column So I'm working with the JHU covid19 data and they've left their recovered dataset go, they're no longer tracking it, just confirmed cases and deaths. What I'm trying to do here is recreate it. The table is the confirmed cases and deaths for every country for every date sorted by date and my getRecovered function below attempts to pick the date for that row, find the date two weeks before that and for the country of that row, and return a 'Recovered' column, which is the confirmed of two weeks ago - the dead today. Maybe a pointless exercise, but still would like to know how to do it haha. I know it's a big dataset also and there's a lot of operations there, but I've been running it 20 mins now and still going. Did I do something wrong or would it just take this long? Thanks for any help, friends. urls = [ 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv', 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv' ] [wget.download(url) for url in urls] confirmed = pd.read_csv('time_series_covid19_confirmed_global.csv') deaths = pd.read_csv('time_series_covid19_deaths_global.csv') dates = confirmed.columns[4:] confirmed_long_form = confirmed.melt( id_vars =['Province/State', 'Country/Region', 'Lat', 'Long'], value_vars=dates, var_name='Date', value_name='Confirmed' ) deaths_long_form = deaths.melt( id_vars =['Province/State', 'Country/Region', 'Lat', 'Long'], value_vars=dates, var_name='Date', value_name='Deaths' ) full_table = confirmed_long_form.merge( right=deaths_long_form, how='left', on=['Province/State', 'Country/Region', 'Date', 'Lat', 'Long'] ) full_table['Date'] = pd.to_datetime(full_table['Date']) full_table = full_table.sort_values(by='Date', ascending=True) def getRecovered(row): ts = row['Date'] country = row['Country/Region'] ts = pd.Timestamp(ts) do = pd.tseries.offsets.DateOffset(n = 14) newTimeStamp = ts - do oldrow = full_table.loc[(full_table['Date'] == newTimeStamp) & (full_table['Country/Region'] == country)] return oldrow['Confirmed'] - row['Deaths'] full_table['Recovered'] = full_table.apply (lambda row: getRecovered(row), axis=1) full_table
Your function is being applied row by row, which is likely why performance is suffering. Pandas is fastest when you make use of vectorised functions. For example you can use pd.to_datetime(full_table['Date']) to convert the whole date column much faster (see here: Convert DataFrame column type from string to datetime). You can then add the date offset to that column, something like: full_table['Recovery_date'] = pd.to_datetime(full_table['Date']) - pd.tseries.offsets.DateOffset(n = 14) You can then self merge the table on date==recovery_date (plus any other keys) and subtract the numbers.
63,842,108
Exploding columns
<p>I have the following dataset:</p> <pre><code>Date Text 2020/05/12 Include details about your goal 2020/05/12 Describe expected and actual results 2020/05/13 Include any error messages 2020/05/13 The community is here to help you 2020/05/14 Avoid asking opinion-based questions. </code></pre> <p>I cleaned it from punctuation, stopwords, ... in order to prepare it for exploding:</p> <pre><code> stop_words = stopwords.words('english') # punctuation to remove punctuation = string.punctuation.replace(&quot;'&quot;, '') # don't remove apostrophe from strings punc = r'[{}]'.format(punctuation) df.Text = df.Text.str.replace('\d+', '') # remove numbers df.Text =df.Text.str.replace(punc, ' ') # remove punctuation except apostrophe df.Text = df.Text.str.replace('\\s+', ' ') # remove occurrences of more than one whitespace df.Text = df.Text.str.strip() # remove whitespace from beginning and end of string df.Text = df.Text.str.lower() # convert all to lowercase df.dropna(inplace=True) df.Text=df.Text.apply(lambda x: list(word for word in x.split() if word not in stop_words)) # remove words </code></pre> <p>However it works only for the first row, and not for all the rows. Next step would be</p> <pre><code>df_1 = df.explode('Text') </code></pre> <p>Can you please tell me what is wrong?</p> <p>The first row is split as follows:</p> <pre><code>Text New_Text (to show the difference after cleaning the text) Include details about your goal ['include','details','goal'] </code></pre> <p>I have no other rows (so no rows starting with 'Describe...' or 'Avoid...'). In my dateset, I have 1942 rows but only 1 is returned after cleaning the text.</p> <p>Update:</p> <p>Example of output:</p> <pre><code>Date Text 2020/05/12 Include 2020/05/12 details 2020/05/12 goal .... ... </code></pre> <p>Fixed issue (not it should work):</p> <p>I think the below code should allow me to get this result:</p> <pre><code>(pd.melt(test.Text.apply(pd.Series).reset_index(), id_vars=['Date'], value_name='Text') .set_index(['Date']) .drop('variable', axis=1) .dropna() .sort_index() ) </code></pre> <p>To convert Date to an index: <code>test=test.set_index(['Date'])</code></p>
63,842,292
"2020-09-11T06:50:25.903000"
1
null
0
73
python|pandas
<p>The code has been revised again as the question was updated. Your desired output was answered as the date column and word column expanded vertically.</p> <pre><code>import pandas as pd import numpy as np import io data = ''' Date Text 2020/05/12 &quot;Include details about your goal&quot; 2020/05/12 &quot;Describe expected and actual results&quot; 2020/05/13 &quot;Include any error messages&quot; 2020/05/13 &quot;The community is here to help you&quot; 2020/05/14 &quot;Avoid asking opinion-based questions.&quot; ''' test = pd.read_csv(io.StringIO(data), sep='\s+') test.set_index('Date',inplace=True) expand_df = test['Text'].str.split(' ', expand=True) expand_df.reset_index(inplace=True) expand_df = pd.melt(expand_df, id_vars='Date', value_vars=np.arange(6), value_name='text') expand_df.dropna(axis=0, inplace=True, ) expand_df = expand_df[['Date', 'text']] expand_df Date text 0 2020/05/12 Include 1 2020/05/12 Describe 2 2020/05/13 Include 3 2020/05/13 The 4 2020/05/14 Avoid 5 2020/05/12 details 6 2020/05/12 expected 7 2020/05/13 any 8 2020/05/13 community 9 2020/05/14 asking 10 2020/05/12 about 11 2020/05/12 and 12 2020/05/13 error 13 2020/05/13 is 14 2020/05/14 opinion-based 15 2020/05/12 your 16 2020/05/12 actual 17 2020/05/13 messages 18 2020/05/13 here 19 2020/05/14 questions. 20 2020/05/12 goal 21 2020/05/12 results 23 2020/05/13 to 28 2020/05/13 help </code></pre>
"2020-09-11T07:05:45.460000"
1
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.explode.html
The code has been revised again as the question was updated. Your desired output was answered as the date column and word column expanded vertically. import pandas as pd import numpy as np import io data = ''' Date Text 2020/05/12 "Include details about your goal" 2020/05/12 "Describe expected and actual results" 2020/05/13 "Include any error messages" 2020/05/13 "The community is here to help you" 2020/05/14 "Avoid asking opinion-based questions." ''' test = pd.read_csv(io.StringIO(data), sep='\s+') test.set_index('Date',inplace=True) expand_df = test['Text'].str.split(' ', expand=True) expand_df.reset_index(inplace=True) expand_df = pd.melt(expand_df, id_vars='Date', value_vars=np.arange(6), value_name='text') expand_df.dropna(axis=0, inplace=True, ) expand_df = expand_df[['Date', 'text']] expand_df Date text 0 2020/05/12 Include 1 2020/05/12 Describe 2 2020/05/13 Include 3 2020/05/13 The 4 2020/05/14 Avoid 5 2020/05/12 details 6 2020/05/12 expected 7 2020/05/13 any 8 2020/05/13 community 9 2020/05/14 asking 10 2020/05/12 about 11 2020/05/12 and 12 2020/05/13 error 13 2020/05/13 is 14 2020/05/14 opinion-based 15 2020/05/12 your 16 2020/05/12 actual 17 2020/05/13 messages 18 2020/05/13 here 19 2020/05/14 questions. 20 2020/05/12 goal 21 2020/05/12 results 23 2020/05/13 to 28 2020/05/13 help
0
1,380
Exploding columns I have the following dataset: Date Text 2020/05/12 Include details about your goal 2020/05/12 Describe expected and actual results 2020/05/13 Include any error messages 2020/05/13 The community is here to help you 2020/05/14 Avoid asking opinion-based questions. I cleaned it from punctuation, stopwords, ... in order to prepare it for exploding: stop_words = stopwords.words('english') # punctuation to remove punctuation = string.punctuation.replace("'", '') # don't remove apostrophe from strings punc = r'[{}]'.format(punctuation) df.Text = df.Text.str.replace('\d+', '') # remove numbers df.Text =df.Text.str.replace(punc, ' ') # remove punctuation except apostrophe df.Text = df.Text.str.replace('\\s+', ' ') # remove occurrences of more than one whitespace df.Text = df.Text.str.strip() # remove whitespace from beginning and end of string df.Text = df.Text.str.lower() # convert all to lowercase df.dropna(inplace=True) df.Text=df.Text.apply(lambda x: list(word for word in x.split() if word not in stop_words)) # remove words However it works only for the first row, and not for all the rows. Next step would be df_1 = df.explode('Text') Can you please tell me what is wrong? The first row is split as follows: Text New_Text (to show the difference after cleaning the text) Include details about your goal ['include','details','goal'] I have no other rows (so no rows starting with 'Describe...' or 'Avoid...'). In my dateset, I have 1942 rows but only 1 is returned after cleaning the text. Update: Example of output: Date Text 2020/05/12 Include 2020/05/12 details 2020/05/12 goal .... ... Fixed issue (not it should work): I think the below code should allow me to get this result: (pd.melt(test.Text.apply(pd.Series).reset_index(), id_vars=['Date'], value_name='Text') .set_index(['Date']) .drop('variable', axis=1) .dropna() .sort_index() ) To convert Date to an index: test=test.set_index(['Date'])
Exploding columns
The code has been revised again as the question was updated. Your desired output was answered as the date column and word column expanded vertically. import pandas as pd import numpy as np import io data = ''' Date Text 2020/05/12 "Include details about your goal" 2020/05/12 "Describe expected and actual results" 2020/05/13 "Include any error messages" 2020/05/13 "The community is here to help you" 2020/05/14 "Avoid asking opinion-based questions." ''' test = pd.read_csv(io.StringIO(data), sep='\s+') test.set_index('Date',inplace=True) expand_df = test['Text'].str.split(' ', expand=True) expand_df.reset_index(inplace=True) expand_df = pd.melt(expand_df, id_vars='Date', value_vars=np.arange(6), value_name='text') expand_df.dropna(axis=0, inplace=True, ) expand_df = expand_df[['Date', 'text']] expand_df Date text 0 2020/05/12 Include 1 2020/05/12 Describe 2 2020/05/13 Include 3 2020/05/13 The 4 2020/05/14 Avoid 5 2020/05/12 details 6 2020/05/12 expected 7 2020/05/13 any 8 2020/05/13 community 9 2020/05/14 asking 10 2020/05/12 about 11 2020/05/12 and 12 2020/05/13 error 13 2020/05/13 is 14 2020/05/14 opinion-based 15 2020/05/12 your 16 2020/05/12 actual 17 2020/05/13 messages 18 2020/05/13 here 19 2020/05/14 questions. 20 2020/05/12 goal 21 2020/05/12 results 23 2020/05/13 to 28 2020/05/13 help
62,570,120
How Can I optimise my code further using Pandas for the required Problem
<p>i have created a following dataset</p> <pre><code>dataset1 = { 'srid':[1,2,3,1,5], 'custid':[11,12,43,12,34], 'orderdate':[&quot;1/2/2019&quot;,&quot;1/2/2019&quot;,&quot;2/2/2019&quot;,&quot;1/2/2019&quot;,&quot;1/2/2019&quot;], 'Rev':[100,101,102,103,17] } df1 = pd.DataFrame(dataset1) </code></pre> <p>I have to mark every sales representative as : Excellent(where his total revenue for a day is 1.5 times or greater than average revenue of all sales representative for that day) Good (where his total revenue for a day is less than 1.5 times and greater than equal to 1.1 times average revenue of all sales representative for that day) Average (where his total revenue for a day is less than 1.1 times and greater than equal to 0.9 times the average revenue of all sales representative for that day) Poor (where his total revenue for a day is less than 0.9 times the average revenue of all sales representative for that day) for every date present in ‘Dataset 1’ Output Dataset: Sales Representative ID,Order Date, Marking</p> <p>what i tried is:</p> <pre><code>g=df.groupby(df['orderdate']) ans={} for od,od_df in g: # print(od) ans[od]=list() x=od_df[&quot;Rev&quot;].mean() s=set(od_df[&quot;srid&quot;].tolist()) for i in s: p=od_df[od_df[&quot;srid&quot;]==i][&quot;Rev&quot;].sum() val = p/x if val&gt;=1.5: ans[od].append([i,od,&quot;Excellent&quot;]) elif 1.1&lt;=val&lt;1.5: ans[od].append([i,od,&quot;good&quot;]) elif 0.9&lt;=val&lt;1.1: ans[od].append([i,od,&quot;avg&quot;]) else: ans[od].append([i,od,&quot;poor&quot;]) </code></pre> <p>But that is alot to write and will take more time on big dataset how can i optimise it further</p>
62,570,646
"2020-06-25T07:32:49.117000"
3
1
0
73
python|pandas
<p>Simply add a column to be calculated and 'apply' to the result</p> <pre><code>df1['mean'] = df1.loc[:,['orderdate','Rev']].groupby('orderdate').transform('mean') df1['Representative'] = df1['Rev']/df1['mean'] def rep(x): if x &gt;= 1.5: return 'Excellent' elif 1.1&lt;=x&lt;1.5: return 'good' elif 0.9&lt;=x&lt;1.1: return 'avg' else: return 'poor' df1['Marking'] = df1['Representative'].apply(rep) df1 srid custid orderdate Rev mean Representative Marking 0 1 11 1/2/2019 100 80.25 1.246106 good 1 2 12 1/2/2019 101 80.25 1.258567 good 2 3 43 2/2/2019 102 102.00 1.000000 avg 3 1 12 1/2/2019 103 80.25 1.283489 good 4 5 34 1/2/2019 17 80.25 0.211838 poor </code></pre>
"2020-06-25T08:02:40.237000"
1
https://pandas.pydata.org/docs/user_guide/scale.html
Scaling to large datasets# Scaling to large datasets# pandas provides data structures for in-memory analytics, which makes using pandas to analyze datasets that are larger than memory datasets somewhat tricky. Even datasets that are a sizable fraction of memory become unwieldy, as some pandas operations need to make intermediate copies. This document provides a few recommendations for scaling your analysis to larger datasets. Simply add a column to be calculated and 'apply' to the result df1['mean'] = df1.loc[:,['orderdate','Rev']].groupby('orderdate').transform('mean') df1['Representative'] = df1['Rev']/df1['mean'] def rep(x): if x >= 1.5: return 'Excellent' elif 1.1<=x<1.5: return 'good' elif 0.9<=x<1.1: return 'avg' else: return 'poor' df1['Marking'] = df1['Representative'].apply(rep) df1 srid custid orderdate Rev mean Representative Marking 0 1 11 1/2/2019 100 80.25 1.246106 good 1 2 12 1/2/2019 101 80.25 1.258567 good 2 3 43 2/2/2019 102 102.00 1.000000 avg 3 1 12 1/2/2019 103 80.25 1.283489 good 4 5 34 1/2/2019 17 80.25 0.211838 poor It’s a complement to Enhancing performance, which focuses on speeding up analysis for datasets that fit in memory. But first, it’s worth considering not using pandas. pandas isn’t the right tool for all situations. If you’re working with very large datasets and a tool like PostgreSQL fits your needs, then you should probably be using that. Assuming you want or need the expressiveness and power of pandas, let’s carry on. Load less data# Suppose our raw dataset on disk has many columns: id_0 name_0 x_0 y_0 id_1 name_1 x_1 ... name_8 x_8 y_8 id_9 name_9 x_9 y_9 timestamp ... 2000-01-01 00:00:00 1015 Michael -0.399453 0.095427 994 Frank -0.176842 ... Dan -0.315310 0.713892 1025 Victor -0.135779 0.346801 2000-01-01 00:01:00 969 Patricia 0.650773 -0.874275 1003 Laura 0.459153 ... Ursula 0.913244 -0.630308 1047 Wendy -0.886285 0.035852 2000-01-01 00:02:00 1016 Victor -0.721465 -0.584710 1046 Michael 0.524994 ... Ray -0.656593 0.692568 1064 Yvonne 0.070426 0.432047 2000-01-01 00:03:00 939 Alice -0.746004 -0.908008 996 Ingrid -0.414523 ... Jerry -0.958994 0.608210 978 Wendy 0.855949 -0.648988 2000-01-01 00:04:00 1017 Dan 0.919451 -0.803504 1048 Jerry -0.569235 ... Frank -0.577022 -0.409088 994 Bob -0.270132 0.335176 ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 2000-12-30 23:56:00 999 Tim 0.162578 0.512817 973 Kevin -0.403352 ... Tim -0.380415 0.008097 1041 Charlie 0.191477 -0.599519 2000-12-30 23:57:00 970 Laura -0.433586 -0.600289 958 Oliver -0.966577 ... Zelda 0.971274 0.402032 1038 Ursula 0.574016 -0.930992 2000-12-30 23:58:00 1065 Edith 0.232211 -0.454540 971 Tim 0.158484 ... Alice -0.222079 -0.919274 1022 Dan 0.031345 -0.657755 2000-12-30 23:59:00 1019 Ingrid 0.322208 -0.615974 981 Hannah 0.607517 ... Sarah -0.424440 -0.117274 990 George -0.375530 0.563312 2000-12-31 00:00:00 937 Ursula -0.906523 0.943178 1018 Alice -0.564513 ... Jerry 0.236837 0.807650 985 Oliver 0.777642 0.783392 [525601 rows x 40 columns] That can be generated by the following code snippet: In [1]: import pandas as pd In [2]: import numpy as np In [3]: def make_timeseries(start="2000-01-01", end="2000-12-31", freq="1D", seed=None): ...: index = pd.date_range(start=start, end=end, freq=freq, name="timestamp") ...: n = len(index) ...: state = np.random.RandomState(seed) ...: columns = { ...: "name": state.choice(["Alice", "Bob", "Charlie"], size=n), ...: "id": state.poisson(1000, size=n), ...: "x": state.rand(n) * 2 - 1, ...: "y": state.rand(n) * 2 - 1, ...: } ...: df = pd.DataFrame(columns, index=index, columns=sorted(columns)) ...: if df.index[-1] == end: ...: df = df.iloc[:-1] ...: return df ...: In [4]: timeseries = [ ...: make_timeseries(freq="1T", seed=i).rename(columns=lambda x: f"{x}_{i}") ...: for i in range(10) ...: ] ...: In [5]: ts_wide = pd.concat(timeseries, axis=1) In [6]: ts_wide.to_parquet("timeseries_wide.parquet") To load the columns we want, we have two options. Option 1 loads in all the data and then filters to what we need. In [7]: columns = ["id_0", "name_0", "x_0", "y_0"] In [8]: pd.read_parquet("timeseries_wide.parquet")[columns] Out[8]: id_0 name_0 x_0 y_0 timestamp 2000-01-01 00:00:00 977 Alice -0.821225 0.906222 2000-01-01 00:01:00 1018 Bob -0.219182 0.350855 2000-01-01 00:02:00 927 Alice 0.660908 -0.798511 2000-01-01 00:03:00 997 Bob -0.852458 0.735260 2000-01-01 00:04:00 965 Bob 0.717283 0.393391 ... ... ... ... ... 2000-12-30 23:56:00 1037 Bob -0.814321 0.612836 2000-12-30 23:57:00 980 Bob 0.232195 -0.618828 2000-12-30 23:58:00 965 Alice -0.231131 0.026310 2000-12-30 23:59:00 984 Alice 0.942819 0.853128 2000-12-31 00:00:00 1003 Alice 0.201125 -0.136655 [525601 rows x 4 columns] Option 2 only loads the columns we request. In [9]: pd.read_parquet("timeseries_wide.parquet", columns=columns) Out[9]: id_0 name_0 x_0 y_0 timestamp 2000-01-01 00:00:00 977 Alice -0.821225 0.906222 2000-01-01 00:01:00 1018 Bob -0.219182 0.350855 2000-01-01 00:02:00 927 Alice 0.660908 -0.798511 2000-01-01 00:03:00 997 Bob -0.852458 0.735260 2000-01-01 00:04:00 965 Bob 0.717283 0.393391 ... ... ... ... ... 2000-12-30 23:56:00 1037 Bob -0.814321 0.612836 2000-12-30 23:57:00 980 Bob 0.232195 -0.618828 2000-12-30 23:58:00 965 Alice -0.231131 0.026310 2000-12-30 23:59:00 984 Alice 0.942819 0.853128 2000-12-31 00:00:00 1003 Alice 0.201125 -0.136655 [525601 rows x 4 columns] If we were to measure the memory usage of the two calls, we’d see that specifying columns uses about 1/10th the memory in this case. With pandas.read_csv(), you can specify usecols to limit the columns read into memory. Not all file formats that can be read by pandas provide an option to read a subset of columns. Use efficient datatypes# The default pandas data types are not the most memory efficient. This is especially true for text data columns with relatively few unique values (commonly referred to as “low-cardinality” data). By using more efficient data types, you can store larger datasets in memory. In [10]: ts = make_timeseries(freq="30S", seed=0) In [11]: ts.to_parquet("timeseries.parquet") In [12]: ts = pd.read_parquet("timeseries.parquet") In [13]: ts Out[13]: id name x y timestamp 2000-01-01 00:00:00 1041 Alice 0.889987 0.281011 2000-01-01 00:00:30 988 Bob -0.455299 0.488153 2000-01-01 00:01:00 1018 Alice 0.096061 0.580473 2000-01-01 00:01:30 992 Bob 0.142482 0.041665 2000-01-01 00:02:00 960 Bob -0.036235 0.802159 ... ... ... ... ... 2000-12-30 23:58:00 1022 Alice 0.266191 0.875579 2000-12-30 23:58:30 974 Alice -0.009826 0.413686 2000-12-30 23:59:00 1028 Charlie 0.307108 -0.656789 2000-12-30 23:59:30 1002 Alice 0.202602 0.541335 2000-12-31 00:00:00 987 Alice 0.200832 0.615972 [1051201 rows x 4 columns] Now, let’s inspect the data types and memory usage to see where we should focus our attention. In [14]: ts.dtypes Out[14]: id int64 name object x float64 y float64 dtype: object In [15]: ts.memory_usage(deep=True) # memory usage in bytes Out[15]: Index 8409608 id 8409608 name 65176434 x 8409608 y 8409608 dtype: int64 The name column is taking up much more memory than any other. It has just a few unique values, so it’s a good candidate for converting to a pandas.Categorical. With a pandas.Categorical, we store each unique name once and use space-efficient integers to know which specific name is used in each row. In [16]: ts2 = ts.copy() In [17]: ts2["name"] = ts2["name"].astype("category") In [18]: ts2.memory_usage(deep=True) Out[18]: Index 8409608 id 8409608 name 1051495 x 8409608 y 8409608 dtype: int64 We can go a bit further and downcast the numeric columns to their smallest types using pandas.to_numeric(). In [19]: ts2["id"] = pd.to_numeric(ts2["id"], downcast="unsigned") In [20]: ts2[["x", "y"]] = ts2[["x", "y"]].apply(pd.to_numeric, downcast="float") In [21]: ts2.dtypes Out[21]: id uint16 name category x float32 y float32 dtype: object In [22]: ts2.memory_usage(deep=True) Out[22]: Index 8409608 id 2102402 name 1051495 x 4204804 y 4204804 dtype: int64 In [23]: reduction = ts2.memory_usage(deep=True).sum() / ts.memory_usage(deep=True).sum() In [24]: print(f"{reduction:0.2f}") 0.20 In all, we’ve reduced the in-memory footprint of this dataset to 1/5 of its original size. See Categorical data for more on pandas.Categorical and dtypes for an overview of all of pandas’ dtypes. Use chunking# Some workloads can be achieved with chunking: splitting a large problem like “convert this directory of CSVs to parquet” into a bunch of small problems (“convert this individual CSV file into a Parquet file. Now repeat that for each file in this directory.”). As long as each chunk fits in memory, you can work with datasets that are much larger than memory. Note Chunking works well when the operation you’re performing requires zero or minimal coordination between chunks. For more complicated workflows, you’re better off using another library. Suppose we have an even larger “logical dataset” on disk that’s a directory of parquet files. Each file in the directory represents a different year of the entire dataset. In [25]: import pathlib In [26]: N = 12 In [27]: starts = [f"20{i:>02d}-01-01" for i in range(N)] In [28]: ends = [f"20{i:>02d}-12-13" for i in range(N)] In [29]: pathlib.Path("data/timeseries").mkdir(exist_ok=True) In [30]: for i, (start, end) in enumerate(zip(starts, ends)): ....: ts = make_timeseries(start=start, end=end, freq="1T", seed=i) ....: ts.to_parquet(f"data/timeseries/ts-{i:0>2d}.parquet") ....: data └── timeseries ├── ts-00.parquet ├── ts-01.parquet ├── ts-02.parquet ├── ts-03.parquet ├── ts-04.parquet ├── ts-05.parquet ├── ts-06.parquet ├── ts-07.parquet ├── ts-08.parquet ├── ts-09.parquet ├── ts-10.parquet └── ts-11.parquet Now we’ll implement an out-of-core pandas.Series.value_counts(). The peak memory usage of this workflow is the single largest chunk, plus a small series storing the unique value counts up to this point. As long as each individual file fits in memory, this will work for arbitrary-sized datasets. In [31]: %%time ....: files = pathlib.Path("data/timeseries/").glob("ts*.parquet") ....: counts = pd.Series(dtype=int) ....: for path in files: ....: df = pd.read_parquet(path) ....: counts = counts.add(df["name"].value_counts(), fill_value=0) ....: counts.astype(int) ....: CPU times: user 698 ms, sys: 79.6 ms, total: 778 ms Wall time: 768 ms Out[31]: Alice 1994645 Bob 1993692 Charlie 1994875 dtype: int64 Some readers, like pandas.read_csv(), offer parameters to control the chunksize when reading a single file. Manually chunking is an OK option for workflows that don’t require too sophisticated of operations. Some operations, like pandas.DataFrame.groupby(), are much harder to do chunkwise. In these cases, you may be better switching to a different library that implements these out-of-core algorithms for you. Use other libraries# pandas is just one library offering a DataFrame API. Because of its popularity, pandas’ API has become something of a standard that other libraries implement. The pandas documentation maintains a list of libraries implementing a DataFrame API in our ecosystem page. For example, Dask, a parallel computing library, has dask.dataframe, a pandas-like API for working with larger than memory datasets in parallel. Dask can use multiple threads or processes on a single machine, or a cluster of machines to process data in parallel. We’ll import dask.dataframe and notice that the API feels similar to pandas. We can use Dask’s read_parquet function, but provide a globstring of files to read in. In [32]: import dask.dataframe as dd In [33]: ddf = dd.read_parquet("data/timeseries/ts*.parquet", engine="pyarrow") In [34]: ddf Out[34]: Dask DataFrame Structure: id name x y npartitions=12 int64 object float64 float64 ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Dask Name: read-parquet, 1 graph layer Inspecting the ddf object, we see a few things There are familiar attributes like .columns and .dtypes There are familiar methods like .groupby, .sum, etc. There are new attributes like .npartitions and .divisions The partitions and divisions are how Dask parallelizes computation. A Dask DataFrame is made up of many pandas pandas.DataFrame. A single method call on a Dask DataFrame ends up making many pandas method calls, and Dask knows how to coordinate everything to get the result. In [35]: ddf.columns Out[35]: Index(['id', 'name', 'x', 'y'], dtype='object') In [36]: ddf.dtypes Out[36]: id int64 name object x float64 y float64 dtype: object In [37]: ddf.npartitions Out[37]: 12 One major difference: the dask.dataframe API is lazy. If you look at the repr above, you’ll notice that the values aren’t actually printed out; just the column names and dtypes. That’s because Dask hasn’t actually read the data yet. Rather than executing immediately, doing operations build up a task graph. In [38]: ddf Out[38]: Dask DataFrame Structure: id name x y npartitions=12 int64 object float64 float64 ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... Dask Name: read-parquet, 1 graph layer In [39]: ddf["name"] Out[39]: Dask Series Structure: npartitions=12 object ... ... ... ... Name: name, dtype: object Dask Name: getitem, 2 graph layers In [40]: ddf["name"].value_counts() Out[40]: Dask Series Structure: npartitions=1 int64 ... Name: name, dtype: int64 Dask Name: value-counts-agg, 4 graph layers Each of these calls is instant because the result isn’t being computed yet. We’re just building up a list of computation to do when someone needs the result. Dask knows that the return type of a pandas.Series.value_counts is a pandas pandas.Series with a certain dtype and a certain name. So the Dask version returns a Dask Series with the same dtype and the same name. To get the actual result you can call .compute(). In [41]: %time ddf["name"].value_counts().compute() CPU times: user 767 ms, sys: 44.4 ms, total: 811 ms Wall time: 788 ms Out[41]: Charlie 1994875 Alice 1994645 Bob 1993692 Name: name, dtype: int64 At that point, you get back the same thing you’d get with pandas, in this case a concrete pandas pandas.Series with the count of each name. Calling .compute causes the full task graph to be executed. This includes reading the data, selecting the columns, and doing the value_counts. The execution is done in parallel where possible, and Dask tries to keep the overall memory footprint small. You can work with datasets that are much larger than memory, as long as each partition (a regular pandas pandas.DataFrame) fits in memory. By default, dask.dataframe operations use a threadpool to do operations in parallel. We can also connect to a cluster to distribute the work on many machines. In this case we’ll connect to a local “cluster” made up of several processes on this single machine. >>> from dask.distributed import Client, LocalCluster >>> cluster = LocalCluster() >>> client = Client(cluster) >>> client <Client: 'tcp://127.0.0.1:53349' processes=4 threads=8, memory=17.18 GB> Once this client is created, all of Dask’s computation will take place on the cluster (which is just processes in this case). Dask implements the most used parts of the pandas API. For example, we can do a familiar groupby aggregation. In [42]: %time ddf.groupby("name")[["x", "y"]].mean().compute().head() CPU times: user 1.24 s, sys: 91.7 ms, total: 1.33 s Wall time: 1.2 s Out[42]: x y name Alice -0.000224 -0.000194 Bob -0.000746 0.000349 Charlie 0.000604 0.000250 The grouping and aggregation is done out-of-core and in parallel. When Dask knows the divisions of a dataset, certain optimizations are possible. When reading parquet datasets written by dask, the divisions will be known automatically. In this case, since we created the parquet files manually, we need to supply the divisions manually. In [43]: N = 12 In [44]: starts = [f"20{i:>02d}-01-01" for i in range(N)] In [45]: ends = [f"20{i:>02d}-12-13" for i in range(N)] In [46]: divisions = tuple(pd.to_datetime(starts)) + (pd.Timestamp(ends[-1]),) In [47]: ddf.divisions = divisions In [48]: ddf Out[48]: Dask DataFrame Structure: id name x y npartitions=12 2000-01-01 int64 object float64 float64 2001-01-01 ... ... ... ... ... ... ... ... ... 2011-01-01 ... ... ... ... 2011-12-13 ... ... ... ... Dask Name: read-parquet, 1 graph layer Now we can do things like fast random access with .loc. In [49]: ddf.loc["2002-01-01 12:01":"2002-01-01 12:05"].compute() Out[49]: id name x y timestamp 2002-01-01 12:01:00 971 Bob -0.659481 0.556184 2002-01-01 12:02:00 1015 Charlie 0.120131 -0.609522 2002-01-01 12:03:00 991 Bob -0.357816 0.811362 2002-01-01 12:04:00 984 Alice -0.608760 0.034187 2002-01-01 12:05:00 998 Charlie 0.551662 -0.461972 Dask knows to just look in the 3rd partition for selecting values in 2002. It doesn’t need to look at any other data. Many workflows involve a large amount of data and processing it in a way that reduces the size to something that fits in memory. In this case, we’ll resample to daily frequency and take the mean. Once we’ve taken the mean, we know the results will fit in memory, so we can safely call compute without running out of memory. At that point it’s just a regular pandas object. In [50]: ddf[["x", "y"]].resample("1D").mean().cumsum().compute().plot() Out[50]: <AxesSubplot: xlabel='timestamp'> These Dask examples have all be done using multiple processes on a single machine. Dask can be deployed on a cluster to scale up to even larger datasets. You see more dask examples at https://examples.dask.org.
432
1,187
How Can I optimise my code further using Pandas for the required Problem i have created a following dataset dataset1 = { 'srid':[1,2,3,1,5], 'custid':[11,12,43,12,34], 'orderdate':["1/2/2019","1/2/2019","2/2/2019","1/2/2019","1/2/2019"], 'Rev':[100,101,102,103,17] } df1 = pd.DataFrame(dataset1) I have to mark every sales representative as : Excellent(where his total revenue for a day is 1.5 times or greater than average revenue of all sales representative for that day) Good (where his total revenue for a day is less than 1.5 times and greater than equal to 1.1 times average revenue of all sales representative for that day) Average (where his total revenue for a day is less than 1.1 times and greater than equal to 0.9 times the average revenue of all sales representative for that day) Poor (where his total revenue for a day is less than 0.9 times the average revenue of all sales representative for that day) for every date present in ‘Dataset 1’ Output Dataset: Sales Representative ID,Order Date, Marking what i tried is: g=df.groupby(df['orderdate']) ans={} for od,od_df in g: # print(od) ans[od]=list() x=od_df["Rev"].mean() s=set(od_df["srid"].tolist()) for i in s: p=od_df[od_df["srid"]==i]["Rev"].sum() val = p/x if val>=1.5: ans[od].append([i,od,"Excellent"]) elif 1.1<=val<1.5: ans[od].append([i,od,"good"]) elif 0.9<=val<1.1: ans[od].append([i,od,"avg"]) else: ans[od].append([i,od,"poor"]) But that is alot to write and will take more time on big dataset how can i optimise it further
Simply add a column to be calculated and 'apply' to the result df1['mean'] = df1.loc[:,['orderdate','Rev']].groupby('orderdate').transform('mean') df1['Representative'] = df1['Rev']/df1['mean'] def rep(x): if x >= 1.5: return 'Excellent' elif 1.1<=x<1.5: return 'good' elif 0.9<=x<1.1: return 'avg' else: return 'poor' df1['Marking'] = df1['Representative'].apply(rep) df1 srid custid orderdate Rev mean Representative Marking 0 1 11 1/2/2019 100 80.25 1.246106 good 1 2 12 1/2/2019 101 80.25 1.258567 good 2 3 43 2/2/2019 102 102.00 1.000000 avg 3 1 12 1/2/2019 103 80.25 1.283489 good 4 5 34 1/2/2019 17 80.25 0.211838 poor
62,032,932
How to split pandas record with one large timedelta into multiple records with smaller ones?
<p>I have a dataframe with 3 columns: timedeltas (duration) of time slot, datetime of slot start and datetime informing when record was created. Timedeltas are all multipliers of 15 minutes:</p> <pre><code>Index duration slot_start creation_time 1. 15 minutes some datetime 1 some datetime 3 2. 45 minutes some datetime 2 some datetime 4 </code></pre> <p>What I want to achieve is:</p> <pre><code>Index duration slot_start creation_time 1. 15 minutes some datetime 1 some datetime 3 2. 15 minutes some datetime 2 some datetime 4 3. 15 minutes some datetime 2 + 15 minutes some datetime 4 4. 15 minutes some datetime 2 + 30 minutes some datetime 4 </code></pre> <p>Is there any tool for such operation? How to achieve it easily and time efficiently on very large dataframes?</p>
62,033,434
"2020-05-26T23:15:59.780000"
1
0
2
76
python|pandas
<p>Try this:</p> <pre><code>unit = pd.Timedelta(minutes=15) s = pd.to_timedelta(df['duration']).div(unit) \ .apply(lambda n: unit * np.arange(n)) \ .rename('offset') \ .explode() df = df.join(s) df['slot_start'] = df['slot_start'] + df['offset'] </code></pre>
"2020-05-27T00:10:52.427000"
1
https://pandas.pydata.org/docs/user_guide/io.html
IO tools (text, CSV, HDF5, …)# IO tools (text, CSV, HDF5, …)# Try this: unit = pd.Timedelta(minutes=15) s = pd.to_timedelta(df['duration']).div(unit) \ .apply(lambda n: unit * np.arange(n)) \ .rename('offset') \ .explode() df = df.join(s) df['slot_start'] = df['slot_start'] + df['offset'] The pandas I/O API is a set of top level reader functions accessed like pandas.read_csv() that generally return a pandas object. The corresponding writer functions are object methods that are accessed like DataFrame.to_csv(). Below is a table containing available readers and writers. Format Type Data Description Reader Writer text CSV read_csv to_csv text Fixed-Width Text File read_fwf text JSON read_json to_json text HTML read_html to_html text LaTeX Styler.to_latex text XML read_xml to_xml text Local clipboard read_clipboard to_clipboard binary MS Excel read_excel to_excel binary OpenDocument read_excel binary HDF5 Format read_hdf to_hdf binary Feather Format read_feather to_feather binary Parquet Format read_parquet to_parquet binary ORC Format read_orc to_orc binary Stata read_stata to_stata binary SAS read_sas binary SPSS read_spss binary Python Pickle Format read_pickle to_pickle SQL SQL read_sql to_sql SQL Google BigQuery read_gbq to_gbq Here is an informal performance comparison for some of these IO methods. Note For examples that use the StringIO class, make sure you import it with from io import StringIO for Python 3. CSV & text files# The workhorse function for reading text files (a.k.a. flat files) is read_csv(). See the cookbook for some advanced strategies. Parsing options# read_csv() accepts the following common arguments: Basic# filepath_or_buffervariousEither a path to a file (a str, pathlib.Path, or py:py._path.local.LocalPath), URL (including http, ftp, and S3 locations), or any object with a read() method (such as an open file or StringIO). sepstr, defaults to ',' for read_csv(), \t for read_table()Delimiter to use. If sep is None, the C engine cannot automatically detect the separator, but the Python parsing engine can, meaning the latter will be used and automatically detect the separator by Python’s builtin sniffer tool, csv.Sniffer. In addition, separators longer than 1 character and different from '\s+' will be interpreted as regular expressions and will also force the use of the Python parsing engine. Note that regex delimiters are prone to ignoring quoted data. Regex example: '\\r\\t'. delimiterstr, default NoneAlternative argument name for sep. delim_whitespaceboolean, default FalseSpecifies whether or not whitespace (e.g. ' ' or '\t') will be used as the delimiter. Equivalent to setting sep='\s+'. If this option is set to True, nothing should be passed in for the delimiter parameter. Column and index locations and names# headerint or list of ints, default 'infer'Row number(s) to use as the column names, and the start of the data. Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first line of the file, if column names are passed explicitly then the behavior is identical to header=None. Explicitly pass header=0 to be able to replace existing names. The header can be a list of ints that specify row locations for a MultiIndex on the columns e.g. [0,1,3]. Intervening rows that are not specified will be skipped (e.g. 2 in this example is skipped). Note that this parameter ignores commented lines and empty lines if skip_blank_lines=True, so header=0 denotes the first line of data rather than the first line of the file. namesarray-like, default NoneList of column names to use. If file contains no header row, then you should explicitly pass header=None. Duplicates in this list are not allowed. index_colint, str, sequence of int / str, or False, optional, default NoneColumn(s) to use as the row labels of the DataFrame, either given as string name or column index. If a sequence of int / str is given, a MultiIndex is used. Note index_col=False can be used to force pandas to not use the first column as the index, e.g. when you have a malformed file with delimiters at the end of each line. The default value of None instructs pandas to guess. If the number of fields in the column header row is equal to the number of fields in the body of the data file, then a default index is used. If it is larger, then the first columns are used as index so that the remaining number of fields in the body are equal to the number of fields in the header. The first row after the header is used to determine the number of columns, which will go into the index. If the subsequent rows contain less columns than the first row, they are filled with NaN. This can be avoided through usecols. This ensures that the columns are taken as is and the trailing data are ignored. usecolslist-like or callable, default NoneReturn a subset of the columns. If list-like, all elements must either be positional (i.e. integer indices into the document columns) or strings that correspond to column names provided either by the user in names or inferred from the document header row(s). If names are given, the document header row(s) are not taken into account. For example, a valid list-like usecols parameter would be [0, 1, 2] or ['foo', 'bar', 'baz']. Element order is ignored, so usecols=[0, 1] is the same as [1, 0]. To instantiate a DataFrame from data with element order preserved use pd.read_csv(data, usecols=['foo', 'bar'])[['foo', 'bar']] for columns in ['foo', 'bar'] order or pd.read_csv(data, usecols=['foo', 'bar'])[['bar', 'foo']] for ['bar', 'foo'] order. If callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True: In [1]: import pandas as pd In [2]: from io import StringIO In [3]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [4]: pd.read_csv(StringIO(data)) Out[4]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [5]: pd.read_csv(StringIO(data), usecols=lambda x: x.upper() in ["COL1", "COL3"]) Out[5]: col1 col3 0 a 1 1 a 2 2 c 3 Using this parameter results in much faster parsing time and lower memory usage when using the c engine. The Python engine loads the data first before deciding which columns to drop. squeezeboolean, default FalseIf the parsed data only contains one column then return a Series. Deprecated since version 1.4.0: Append .squeeze("columns") to the call to {func_name} to squeeze the data. prefixstr, default NonePrefix to add to column numbers when no header, e.g. ‘X’ for X0, X1, … Deprecated since version 1.4.0: Use a list comprehension on the DataFrame’s columns after calling read_csv. In [6]: data = "col1,col2,col3\na,b,1" In [7]: df = pd.read_csv(StringIO(data)) In [8]: df.columns = [f"pre_{col}" for col in df.columns] In [9]: df Out[9]: pre_col1 pre_col2 pre_col3 0 a b 1 mangle_dupe_colsboolean, default TrueDuplicate columns will be specified as ‘X’, ‘X.1’…’X.N’, rather than ‘X’…’X’. Passing in False will cause data to be overwritten if there are duplicate names in the columns. Deprecated since version 1.5.0: The argument was never implemented, and a new argument where the renaming pattern can be specified will be added instead. General parsing configuration# dtypeType name or dict of column -> type, default NoneData type for data or columns. E.g. {'a': np.float64, 'b': np.int32, 'c': 'Int64'} Use str or object together with suitable na_values settings to preserve and not interpret dtype. If converters are specified, they will be applied INSTEAD of dtype conversion. New in version 1.5.0: Support for defaultdict was added. Specify a defaultdict as input where the default determines the dtype of the columns which are not explicitly listed. engine{'c', 'python', 'pyarrow'}Parser engine to use. The C and pyarrow engines are faster, while the python engine is currently more feature-complete. Multithreading is currently only supported by the pyarrow engine. New in version 1.4.0: The “pyarrow” engine was added as an experimental engine, and some features are unsupported, or may not work correctly, with this engine. convertersdict, default NoneDict of functions for converting values in certain columns. Keys can either be integers or column labels. true_valueslist, default NoneValues to consider as True. false_valueslist, default NoneValues to consider as False. skipinitialspaceboolean, default FalseSkip spaces after delimiter. skiprowslist-like or integer, default NoneLine numbers to skip (0-indexed) or number of lines to skip (int) at the start of the file. If callable, the callable function will be evaluated against the row indices, returning True if the row should be skipped and False otherwise: In [10]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [11]: pd.read_csv(StringIO(data)) Out[11]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [12]: pd.read_csv(StringIO(data), skiprows=lambda x: x % 2 != 0) Out[12]: col1 col2 col3 0 a b 2 skipfooterint, default 0Number of lines at bottom of file to skip (unsupported with engine=’c’). nrowsint, default NoneNumber of rows of file to read. Useful for reading pieces of large files. low_memoryboolean, default TrueInternally process the file in chunks, resulting in lower memory use while parsing, but possibly mixed type inference. To ensure no mixed types either set False, or specify the type with the dtype parameter. Note that the entire file is read into a single DataFrame regardless, use the chunksize or iterator parameter to return the data in chunks. (Only valid with C parser) memory_mapboolean, default FalseIf a filepath is provided for filepath_or_buffer, map the file object directly onto memory and access the data directly from there. Using this option can improve performance because there is no longer any I/O overhead. NA and missing data handling# na_valuesscalar, str, list-like, or dict, default NoneAdditional strings to recognize as NA/NaN. If dict passed, specific per-column NA values. See na values const below for a list of the values interpreted as NaN by default. keep_default_naboolean, default TrueWhether or not to include the default NaN values when parsing the data. Depending on whether na_values is passed in, the behavior is as follows: If keep_default_na is True, and na_values are specified, na_values is appended to the default NaN values used for parsing. If keep_default_na is True, and na_values are not specified, only the default NaN values are used for parsing. If keep_default_na is False, and na_values are specified, only the NaN values specified na_values are used for parsing. If keep_default_na is False, and na_values are not specified, no strings will be parsed as NaN. Note that if na_filter is passed in as False, the keep_default_na and na_values parameters will be ignored. na_filterboolean, default TrueDetect missing value markers (empty strings and the value of na_values). In data without any NAs, passing na_filter=False can improve the performance of reading a large file. verboseboolean, default FalseIndicate number of NA values placed in non-numeric columns. skip_blank_linesboolean, default TrueIf True, skip over blank lines rather than interpreting as NaN values. Datetime handling# parse_datesboolean or list of ints or names or list of lists or dict, default False. If True -> try parsing the index. If [1, 2, 3] -> try parsing columns 1, 2, 3 each as a separate date column. If [[1, 3]] -> combine columns 1 and 3 and parse as a single date column. If {'foo': [1, 3]} -> parse columns 1, 3 as date and call result ‘foo’. Note A fast-path exists for iso8601-formatted dates. infer_datetime_formatboolean, default FalseIf True and parse_dates is enabled for a column, attempt to infer the datetime format to speed up the processing. keep_date_colboolean, default FalseIf True and parse_dates specifies combining multiple columns then keep the original columns. date_parserfunction, default NoneFunction to use for converting a sequence of string columns to an array of datetime instances. The default uses dateutil.parser.parser to do the conversion. pandas will try to call date_parser in three different ways, advancing to the next if an exception occurs: 1) Pass one or more arrays (as defined by parse_dates) as arguments; 2) concatenate (row-wise) the string values from the columns defined by parse_dates into a single array and pass that; and 3) call date_parser once for each row using one or more strings (corresponding to the columns defined by parse_dates) as arguments. dayfirstboolean, default FalseDD/MM format dates, international and European format. cache_datesboolean, default TrueIf True, use a cache of unique, converted dates to apply the datetime conversion. May produce significant speed-up when parsing duplicate date strings, especially ones with timezone offsets. New in version 0.25.0. Iteration# iteratorboolean, default FalseReturn TextFileReader object for iteration or getting chunks with get_chunk(). chunksizeint, default NoneReturn TextFileReader object for iteration. See iterating and chunking below. Quoting, compression, and file format# compression{'infer', 'gzip', 'bz2', 'zip', 'xz', 'zstd', None, dict}, default 'infer'For on-the-fly decompression of on-disk data. If ‘infer’, then use gzip, bz2, zip, xz, or zstandard if filepath_or_buffer is path-like ending in ‘.gz’, ‘.bz2’, ‘.zip’, ‘.xz’, ‘.zst’, respectively, and no decompression otherwise. If using ‘zip’, the ZIP file must contain only one data file to be read in. Set to None for no decompression. Can also be a dict with key 'method' set to one of {'zip', 'gzip', 'bz2', 'zstd'} and other key-value pairs are forwarded to zipfile.ZipFile, gzip.GzipFile, bz2.BZ2File, or zstandard.ZstdDecompressor. As an example, the following could be passed for faster compression and to create a reproducible gzip archive: compression={'method': 'gzip', 'compresslevel': 1, 'mtime': 1}. Changed in version 1.1.0: dict option extended to support gzip and bz2. Changed in version 1.2.0: Previous versions forwarded dict entries for ‘gzip’ to gzip.open. thousandsstr, default NoneThousands separator. decimalstr, default '.'Character to recognize as decimal point. E.g. use ',' for European data. float_precisionstring, default NoneSpecifies which converter the C engine should use for floating-point values. The options are None for the ordinary converter, high for the high-precision converter, and round_trip for the round-trip converter. lineterminatorstr (length 1), default NoneCharacter to break file into lines. Only valid with C parser. quotecharstr (length 1)The character used to denote the start and end of a quoted item. Quoted items can include the delimiter and it will be ignored. quotingint or csv.QUOTE_* instance, default 0Control field quoting behavior per csv.QUOTE_* constants. Use one of QUOTE_MINIMAL (0), QUOTE_ALL (1), QUOTE_NONNUMERIC (2) or QUOTE_NONE (3). doublequoteboolean, default TrueWhen quotechar is specified and quoting is not QUOTE_NONE, indicate whether or not to interpret two consecutive quotechar elements inside a field as a single quotechar element. escapecharstr (length 1), default NoneOne-character string used to escape delimiter when quoting is QUOTE_NONE. commentstr, default NoneIndicates remainder of line should not be parsed. If found at the beginning of a line, the line will be ignored altogether. This parameter must be a single character. Like empty lines (as long as skip_blank_lines=True), fully commented lines are ignored by the parameter header but not by skiprows. For example, if comment='#', parsing ‘#empty\na,b,c\n1,2,3’ with header=0 will result in ‘a,b,c’ being treated as the header. encodingstr, default NoneEncoding to use for UTF when reading/writing (e.g. 'utf-8'). List of Python standard encodings. dialectstr or csv.Dialect instance, default NoneIf provided, this parameter will override values (default or not) for the following parameters: delimiter, doublequote, escapechar, skipinitialspace, quotechar, and quoting. If it is necessary to override values, a ParserWarning will be issued. See csv.Dialect documentation for more details. Error handling# error_bad_linesboolean, optional, default NoneLines with too many fields (e.g. a csv line with too many commas) will by default cause an exception to be raised, and no DataFrame will be returned. If False, then these “bad lines” will dropped from the DataFrame that is returned. See bad lines below. Deprecated since version 1.3.0: The on_bad_lines parameter should be used instead to specify behavior upon encountering a bad line instead. warn_bad_linesboolean, optional, default NoneIf error_bad_lines is False, and warn_bad_lines is True, a warning for each “bad line” will be output. Deprecated since version 1.3.0: The on_bad_lines parameter should be used instead to specify behavior upon encountering a bad line instead. on_bad_lines(‘error’, ‘warn’, ‘skip’), default ‘error’Specifies what to do upon encountering a bad line (a line with too many fields). Allowed values are : ‘error’, raise an ParserError when a bad line is encountered. ‘warn’, print a warning when a bad line is encountered and skip that line. ‘skip’, skip bad lines without raising or warning when they are encountered. New in version 1.3.0. Specifying column data types# You can indicate the data type for the whole DataFrame or individual columns: In [13]: import numpy as np In [14]: data = "a,b,c,d\n1,2,3,4\n5,6,7,8\n9,10,11" In [15]: print(data) a,b,c,d 1,2,3,4 5,6,7,8 9,10,11 In [16]: df = pd.read_csv(StringIO(data), dtype=object) In [17]: df Out[17]: a b c d 0 1 2 3 4 1 5 6 7 8 2 9 10 11 NaN In [18]: df["a"][0] Out[18]: '1' In [19]: df = pd.read_csv(StringIO(data), dtype={"b": object, "c": np.float64, "d": "Int64"}) In [20]: df.dtypes Out[20]: a int64 b object c float64 d Int64 dtype: object Fortunately, pandas offers more than one way to ensure that your column(s) contain only one dtype. If you’re unfamiliar with these concepts, you can see here to learn more about dtypes, and here to learn more about object conversion in pandas. For instance, you can use the converters argument of read_csv(): In [21]: data = "col_1\n1\n2\n'A'\n4.22" In [22]: df = pd.read_csv(StringIO(data), converters={"col_1": str}) In [23]: df Out[23]: col_1 0 1 1 2 2 'A' 3 4.22 In [24]: df["col_1"].apply(type).value_counts() Out[24]: <class 'str'> 4 Name: col_1, dtype: int64 Or you can use the to_numeric() function to coerce the dtypes after reading in the data, In [25]: df2 = pd.read_csv(StringIO(data)) In [26]: df2["col_1"] = pd.to_numeric(df2["col_1"], errors="coerce") In [27]: df2 Out[27]: col_1 0 1.00 1 2.00 2 NaN 3 4.22 In [28]: df2["col_1"].apply(type).value_counts() Out[28]: <class 'float'> 4 Name: col_1, dtype: int64 which will convert all valid parsing to floats, leaving the invalid parsing as NaN. Ultimately, how you deal with reading in columns containing mixed dtypes depends on your specific needs. In the case above, if you wanted to NaN out the data anomalies, then to_numeric() is probably your best option. However, if you wanted for all the data to be coerced, no matter the type, then using the converters argument of read_csv() would certainly be worth trying. Note In some cases, reading in abnormal data with columns containing mixed dtypes will result in an inconsistent dataset. If you rely on pandas to infer the dtypes of your columns, the parsing engine will go and infer the dtypes for different chunks of the data, rather than the whole dataset at once. Consequently, you can end up with column(s) with mixed dtypes. For example, In [29]: col_1 = list(range(500000)) + ["a", "b"] + list(range(500000)) In [30]: df = pd.DataFrame({"col_1": col_1}) In [31]: df.to_csv("foo.csv") In [32]: mixed_df = pd.read_csv("foo.csv") In [33]: mixed_df["col_1"].apply(type).value_counts() Out[33]: <class 'int'> 737858 <class 'str'> 262144 Name: col_1, dtype: int64 In [34]: mixed_df["col_1"].dtype Out[34]: dtype('O') will result with mixed_df containing an int dtype for certain chunks of the column, and str for others due to the mixed dtypes from the data that was read in. It is important to note that the overall column will be marked with a dtype of object, which is used for columns with mixed dtypes. Specifying categorical dtype# Categorical columns can be parsed directly by specifying dtype='category' or dtype=CategoricalDtype(categories, ordered). In [35]: data = "col1,col2,col3\na,b,1\na,b,2\nc,d,3" In [36]: pd.read_csv(StringIO(data)) Out[36]: col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 In [37]: pd.read_csv(StringIO(data)).dtypes Out[37]: col1 object col2 object col3 int64 dtype: object In [38]: pd.read_csv(StringIO(data), dtype="category").dtypes Out[38]: col1 category col2 category col3 category dtype: object Individual columns can be parsed as a Categorical using a dict specification: In [39]: pd.read_csv(StringIO(data), dtype={"col1": "category"}).dtypes Out[39]: col1 category col2 object col3 int64 dtype: object Specifying dtype='category' will result in an unordered Categorical whose categories are the unique values observed in the data. For more control on the categories and order, create a CategoricalDtype ahead of time, and pass that for that column’s dtype. In [40]: from pandas.api.types import CategoricalDtype In [41]: dtype = CategoricalDtype(["d", "c", "b", "a"], ordered=True) In [42]: pd.read_csv(StringIO(data), dtype={"col1": dtype}).dtypes Out[42]: col1 category col2 object col3 int64 dtype: object When using dtype=CategoricalDtype, “unexpected” values outside of dtype.categories are treated as missing values. In [43]: dtype = CategoricalDtype(["a", "b", "d"]) # No 'c' In [44]: pd.read_csv(StringIO(data), dtype={"col1": dtype}).col1 Out[44]: 0 a 1 a 2 NaN Name: col1, dtype: category Categories (3, object): ['a', 'b', 'd'] This matches the behavior of Categorical.set_categories(). Note With dtype='category', the resulting categories will always be parsed as strings (object dtype). If the categories are numeric they can be converted using the to_numeric() function, or as appropriate, another converter such as to_datetime(). When dtype is a CategoricalDtype with homogeneous categories ( all numeric, all datetimes, etc.), the conversion is done automatically. In [45]: df = pd.read_csv(StringIO(data), dtype="category") In [46]: df.dtypes Out[46]: col1 category col2 category col3 category dtype: object In [47]: df["col3"] Out[47]: 0 1 1 2 2 3 Name: col3, dtype: category Categories (3, object): ['1', '2', '3'] In [48]: new_categories = pd.to_numeric(df["col3"].cat.categories) In [49]: df["col3"] = df["col3"].cat.rename_categories(new_categories) In [50]: df["col3"] Out[50]: 0 1 1 2 2 3 Name: col3, dtype: category Categories (3, int64): [1, 2, 3] Naming and using columns# Handling column names# A file may or may not have a header row. pandas assumes the first row should be used as the column names: In [51]: data = "a,b,c\n1,2,3\n4,5,6\n7,8,9" In [52]: print(data) a,b,c 1,2,3 4,5,6 7,8,9 In [53]: pd.read_csv(StringIO(data)) Out[53]: a b c 0 1 2 3 1 4 5 6 2 7 8 9 By specifying the names argument in conjunction with header you can indicate other names to use and whether or not to throw away the header row (if any): In [54]: print(data) a,b,c 1,2,3 4,5,6 7,8,9 In [55]: pd.read_csv(StringIO(data), names=["foo", "bar", "baz"], header=0) Out[55]: foo bar baz 0 1 2 3 1 4 5 6 2 7 8 9 In [56]: pd.read_csv(StringIO(data), names=["foo", "bar", "baz"], header=None) Out[56]: foo bar baz 0 a b c 1 1 2 3 2 4 5 6 3 7 8 9 If the header is in a row other than the first, pass the row number to header. This will skip the preceding rows: In [57]: data = "skip this skip it\na,b,c\n1,2,3\n4,5,6\n7,8,9" In [58]: pd.read_csv(StringIO(data), header=1) Out[58]: a b c 0 1 2 3 1 4 5 6 2 7 8 9 Note Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first non-blank line of the file, if column names are passed explicitly then the behavior is identical to header=None. Duplicate names parsing# Deprecated since version 1.5.0: mangle_dupe_cols was never implemented, and a new argument where the renaming pattern can be specified will be added instead. If the file or header contains duplicate names, pandas will by default distinguish between them so as to prevent overwriting data: In [59]: data = "a,b,a\n0,1,2\n3,4,5" In [60]: pd.read_csv(StringIO(data)) Out[60]: a b a.1 0 0 1 2 1 3 4 5 There is no more duplicate data because mangle_dupe_cols=True by default, which modifies a series of duplicate columns ‘X’, …, ‘X’ to become ‘X’, ‘X.1’, …, ‘X.N’. Filtering columns (usecols)# The usecols argument allows you to select any subset of the columns in a file, either using the column names, position numbers or a callable: In [61]: data = "a,b,c,d\n1,2,3,foo\n4,5,6,bar\n7,8,9,baz" In [62]: pd.read_csv(StringIO(data)) Out[62]: a b c d 0 1 2 3 foo 1 4 5 6 bar 2 7 8 9 baz In [63]: pd.read_csv(StringIO(data), usecols=["b", "d"]) Out[63]: b d 0 2 foo 1 5 bar 2 8 baz In [64]: pd.read_csv(StringIO(data), usecols=[0, 2, 3]) Out[64]: a c d 0 1 3 foo 1 4 6 bar 2 7 9 baz In [65]: pd.read_csv(StringIO(data), usecols=lambda x: x.upper() in ["A", "C"]) Out[65]: a c 0 1 3 1 4 6 2 7 9 The usecols argument can also be used to specify which columns not to use in the final result: In [66]: pd.read_csv(StringIO(data), usecols=lambda x: x not in ["a", "c"]) Out[66]: b d 0 2 foo 1 5 bar 2 8 baz In this case, the callable is specifying that we exclude the “a” and “c” columns from the output. Comments and empty lines# Ignoring line comments and empty lines# If the comment parameter is specified, then completely commented lines will be ignored. By default, completely blank lines will be ignored as well. In [67]: data = "\na,b,c\n \n# commented line\n1,2,3\n\n4,5,6" In [68]: print(data) a,b,c # commented line 1,2,3 4,5,6 In [69]: pd.read_csv(StringIO(data), comment="#") Out[69]: a b c 0 1 2 3 1 4 5 6 If skip_blank_lines=False, then read_csv will not ignore blank lines: In [70]: data = "a,b,c\n\n1,2,3\n\n\n4,5,6" In [71]: pd.read_csv(StringIO(data), skip_blank_lines=False) Out[71]: a b c 0 NaN NaN NaN 1 1.0 2.0 3.0 2 NaN NaN NaN 3 NaN NaN NaN 4 4.0 5.0 6.0 Warning The presence of ignored lines might create ambiguities involving line numbers; the parameter header uses row numbers (ignoring commented/empty lines), while skiprows uses line numbers (including commented/empty lines): In [72]: data = "#comment\na,b,c\nA,B,C\n1,2,3" In [73]: pd.read_csv(StringIO(data), comment="#", header=1) Out[73]: A B C 0 1 2 3 In [74]: data = "A,B,C\n#comment\na,b,c\n1,2,3" In [75]: pd.read_csv(StringIO(data), comment="#", skiprows=2) Out[75]: a b c 0 1 2 3 If both header and skiprows are specified, header will be relative to the end of skiprows. For example: In [76]: data = ( ....: "# empty\n" ....: "# second empty line\n" ....: "# third emptyline\n" ....: "X,Y,Z\n" ....: "1,2,3\n" ....: "A,B,C\n" ....: "1,2.,4.\n" ....: "5.,NaN,10.0\n" ....: ) ....: In [77]: print(data) # empty # second empty line # third emptyline X,Y,Z 1,2,3 A,B,C 1,2.,4. 5.,NaN,10.0 In [78]: pd.read_csv(StringIO(data), comment="#", skiprows=4, header=1) Out[78]: A B C 0 1.0 2.0 4.0 1 5.0 NaN 10.0 Comments# Sometimes comments or meta data may be included in a file: In [79]: print(open("tmp.csv").read()) ID,level,category Patient1,123000,x # really unpleasant Patient2,23000,y # wouldn't take his medicine Patient3,1234018,z # awesome By default, the parser includes the comments in the output: In [80]: df = pd.read_csv("tmp.csv") In [81]: df Out[81]: ID level category 0 Patient1 123000 x # really unpleasant 1 Patient2 23000 y # wouldn't take his medicine 2 Patient3 1234018 z # awesome We can suppress the comments using the comment keyword: In [82]: df = pd.read_csv("tmp.csv", comment="#") In [83]: df Out[83]: ID level category 0 Patient1 123000 x 1 Patient2 23000 y 2 Patient3 1234018 z Dealing with Unicode data# The encoding argument should be used for encoded unicode data, which will result in byte strings being decoded to unicode in the result: In [84]: from io import BytesIO In [85]: data = b"word,length\n" b"Tr\xc3\xa4umen,7\n" b"Gr\xc3\xbc\xc3\x9fe,5" In [86]: data = data.decode("utf8").encode("latin-1") In [87]: df = pd.read_csv(BytesIO(data), encoding="latin-1") In [88]: df Out[88]: word length 0 Träumen 7 1 Grüße 5 In [89]: df["word"][1] Out[89]: 'Grüße' Some formats which encode all characters as multiple bytes, like UTF-16, won’t parse correctly at all without specifying the encoding. Full list of Python standard encodings. Index columns and trailing delimiters# If a file has one more column of data than the number of column names, the first column will be used as the DataFrame’s row names: In [90]: data = "a,b,c\n4,apple,bat,5.7\n8,orange,cow,10" In [91]: pd.read_csv(StringIO(data)) Out[91]: a b c 4 apple bat 5.7 8 orange cow 10.0 In [92]: data = "index,a,b,c\n4,apple,bat,5.7\n8,orange,cow,10" In [93]: pd.read_csv(StringIO(data), index_col=0) Out[93]: a b c index 4 apple bat 5.7 8 orange cow 10.0 Ordinarily, you can achieve this behavior using the index_col option. There are some exception cases when a file has been prepared with delimiters at the end of each data line, confusing the parser. To explicitly disable the index column inference and discard the last column, pass index_col=False: In [94]: data = "a,b,c\n4,apple,bat,\n8,orange,cow," In [95]: print(data) a,b,c 4,apple,bat, 8,orange,cow, In [96]: pd.read_csv(StringIO(data)) Out[96]: a b c 4 apple bat NaN 8 orange cow NaN In [97]: pd.read_csv(StringIO(data), index_col=False) Out[97]: a b c 0 4 apple bat 1 8 orange cow If a subset of data is being parsed using the usecols option, the index_col specification is based on that subset, not the original data. In [98]: data = "a,b,c\n4,apple,bat,\n8,orange,cow," In [99]: print(data) a,b,c 4,apple,bat, 8,orange,cow, In [100]: pd.read_csv(StringIO(data), usecols=["b", "c"]) Out[100]: b c 4 bat NaN 8 cow NaN In [101]: pd.read_csv(StringIO(data), usecols=["b", "c"], index_col=0) Out[101]: b c 4 bat NaN 8 cow NaN Date Handling# Specifying date columns# To better facilitate working with datetime data, read_csv() uses the keyword arguments parse_dates and date_parser to allow users to specify a variety of columns and date/time formats to turn the input text data into datetime objects. The simplest case is to just pass in parse_dates=True: In [102]: with open("foo.csv", mode="w") as f: .....: f.write("date,A,B,C\n20090101,a,1,2\n20090102,b,3,4\n20090103,c,4,5") .....: # Use a column as an index, and parse it as dates. In [103]: df = pd.read_csv("foo.csv", index_col=0, parse_dates=True) In [104]: df Out[104]: A B C date 2009-01-01 a 1 2 2009-01-02 b 3 4 2009-01-03 c 4 5 # These are Python datetime objects In [105]: df.index Out[105]: DatetimeIndex(['2009-01-01', '2009-01-02', '2009-01-03'], dtype='datetime64[ns]', name='date', freq=None) It is often the case that we may want to store date and time data separately, or store various date fields separately. the parse_dates keyword can be used to specify a combination of columns to parse the dates and/or times from. You can specify a list of column lists to parse_dates, the resulting date columns will be prepended to the output (so as to not affect the existing column order) and the new column names will be the concatenation of the component column names: In [106]: data = ( .....: "KORD,19990127, 19:00:00, 18:56:00, 0.8100\n" .....: "KORD,19990127, 20:00:00, 19:56:00, 0.0100\n" .....: "KORD,19990127, 21:00:00, 20:56:00, -0.5900\n" .....: "KORD,19990127, 21:00:00, 21:18:00, -0.9900\n" .....: "KORD,19990127, 22:00:00, 21:56:00, -0.5900\n" .....: "KORD,19990127, 23:00:00, 22:56:00, -0.5900" .....: ) .....: In [107]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [108]: df = pd.read_csv("tmp.csv", header=None, parse_dates=[[1, 2], [1, 3]]) In [109]: df Out[109]: 1_2 1_3 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 By default the parser removes the component date columns, but you can choose to retain them via the keep_date_col keyword: In [110]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=[[1, 2], [1, 3]], keep_date_col=True .....: ) .....: In [111]: df Out[111]: 1_2 1_3 0 ... 2 3 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD ... 19:00:00 18:56:00 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD ... 20:00:00 19:56:00 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD ... 21:00:00 20:56:00 -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD ... 21:00:00 21:18:00 -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD ... 22:00:00 21:56:00 -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD ... 23:00:00 22:56:00 -0.59 [6 rows x 7 columns] Note that if you wish to combine multiple columns into a single date column, a nested list must be used. In other words, parse_dates=[1, 2] indicates that the second and third columns should each be parsed as separate date columns while parse_dates=[[1, 2]] means the two columns should be parsed into a single column. You can also use a dict to specify custom name columns: In [112]: date_spec = {"nominal": [1, 2], "actual": [1, 3]} In [113]: df = pd.read_csv("tmp.csv", header=None, parse_dates=date_spec) In [114]: df Out[114]: nominal actual 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 It is important to remember that if multiple text columns are to be parsed into a single date column, then a new column is prepended to the data. The index_col specification is based off of this new set of columns rather than the original data columns: In [115]: date_spec = {"nominal": [1, 2], "actual": [1, 3]} In [116]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=date_spec, index_col=0 .....: ) # index is the nominal column .....: In [117]: df Out[117]: actual 0 4 nominal 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 Note If a column or index contains an unparsable date, the entire column or index will be returned unaltered as an object data type. For non-standard datetime parsing, use to_datetime() after pd.read_csv. Note read_csv has a fast_path for parsing datetime strings in iso8601 format, e.g “2000-01-01T00:01:02+00:00” and similar variations. If you can arrange for your data to store datetimes in this format, load times will be significantly faster, ~20x has been observed. Date parsing functions# Finally, the parser allows you to specify a custom date_parser function to take full advantage of the flexibility of the date parsing API: In [118]: df = pd.read_csv( .....: "tmp.csv", header=None, parse_dates=date_spec, date_parser=pd.to_datetime .....: ) .....: In [119]: df Out[119]: nominal actual 0 4 0 1999-01-27 19:00:00 1999-01-27 18:56:00 KORD 0.81 1 1999-01-27 20:00:00 1999-01-27 19:56:00 KORD 0.01 2 1999-01-27 21:00:00 1999-01-27 20:56:00 KORD -0.59 3 1999-01-27 21:00:00 1999-01-27 21:18:00 KORD -0.99 4 1999-01-27 22:00:00 1999-01-27 21:56:00 KORD -0.59 5 1999-01-27 23:00:00 1999-01-27 22:56:00 KORD -0.59 pandas will try to call the date_parser function in three different ways. If an exception is raised, the next one is tried: date_parser is first called with one or more arrays as arguments, as defined using parse_dates (e.g., date_parser(['2013', '2013'], ['1', '2'])). If #1 fails, date_parser is called with all the columns concatenated row-wise into a single array (e.g., date_parser(['2013 1', '2013 2'])). Note that performance-wise, you should try these methods of parsing dates in order: Try to infer the format using infer_datetime_format=True (see section below). If you know the format, use pd.to_datetime(): date_parser=lambda x: pd.to_datetime(x, format=...). If you have a really non-standard format, use a custom date_parser function. For optimal performance, this should be vectorized, i.e., it should accept arrays as arguments. Parsing a CSV with mixed timezones# pandas cannot natively represent a column or index with mixed timezones. If your CSV file contains columns with a mixture of timezones, the default result will be an object-dtype column with strings, even with parse_dates. In [120]: content = """\ .....: a .....: 2000-01-01T00:00:00+05:00 .....: 2000-01-01T00:00:00+06:00""" .....: In [121]: df = pd.read_csv(StringIO(content), parse_dates=["a"]) In [122]: df["a"] Out[122]: 0 2000-01-01 00:00:00+05:00 1 2000-01-01 00:00:00+06:00 Name: a, dtype: object To parse the mixed-timezone values as a datetime column, pass a partially-applied to_datetime() with utc=True as the date_parser. In [123]: df = pd.read_csv( .....: StringIO(content), .....: parse_dates=["a"], .....: date_parser=lambda col: pd.to_datetime(col, utc=True), .....: ) .....: In [124]: df["a"] Out[124]: 0 1999-12-31 19:00:00+00:00 1 1999-12-31 18:00:00+00:00 Name: a, dtype: datetime64[ns, UTC] Inferring datetime format# If you have parse_dates enabled for some or all of your columns, and your datetime strings are all formatted the same way, you may get a large speed up by setting infer_datetime_format=True. If set, pandas will attempt to guess the format of your datetime strings, and then use a faster means of parsing the strings. 5-10x parsing speeds have been observed. pandas will fallback to the usual parsing if either the format cannot be guessed or the format that was guessed cannot properly parse the entire column of strings. So in general, infer_datetime_format should not have any negative consequences if enabled. Here are some examples of datetime strings that can be guessed (All representing December 30th, 2011 at 00:00:00): “20111230” “2011/12/30” “20111230 00:00:00” “12/30/2011 00:00:00” “30/Dec/2011 00:00:00” “30/December/2011 00:00:00” Note that infer_datetime_format is sensitive to dayfirst. With dayfirst=True, it will guess “01/12/2011” to be December 1st. With dayfirst=False (default) it will guess “01/12/2011” to be January 12th. # Try to infer the format for the index column In [125]: df = pd.read_csv( .....: "foo.csv", .....: index_col=0, .....: parse_dates=True, .....: infer_datetime_format=True, .....: ) .....: In [126]: df Out[126]: A B C date 2009-01-01 a 1 2 2009-01-02 b 3 4 2009-01-03 c 4 5 International date formats# While US date formats tend to be MM/DD/YYYY, many international formats use DD/MM/YYYY instead. For convenience, a dayfirst keyword is provided: In [127]: data = "date,value,cat\n1/6/2000,5,a\n2/6/2000,10,b\n3/6/2000,15,c" In [128]: print(data) date,value,cat 1/6/2000,5,a 2/6/2000,10,b 3/6/2000,15,c In [129]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [130]: pd.read_csv("tmp.csv", parse_dates=[0]) Out[130]: date value cat 0 2000-01-06 5 a 1 2000-02-06 10 b 2 2000-03-06 15 c In [131]: pd.read_csv("tmp.csv", dayfirst=True, parse_dates=[0]) Out[131]: date value cat 0 2000-06-01 5 a 1 2000-06-02 10 b 2 2000-06-03 15 c Writing CSVs to binary file objects# New in version 1.2.0. df.to_csv(..., mode="wb") allows writing a CSV to a file object opened binary mode. In most cases, it is not necessary to specify mode as Pandas will auto-detect whether the file object is opened in text or binary mode. In [132]: import io In [133]: data = pd.DataFrame([0, 1, 2]) In [134]: buffer = io.BytesIO() In [135]: data.to_csv(buffer, encoding="utf-8", compression="gzip") Specifying method for floating-point conversion# The parameter float_precision can be specified in order to use a specific floating-point converter during parsing with the C engine. The options are the ordinary converter, the high-precision converter, and the round-trip converter (which is guaranteed to round-trip values after writing to a file). For example: In [136]: val = "0.3066101993807095471566981359501369297504425048828125" In [137]: data = "a,b,c\n1,2,{0}".format(val) In [138]: abs( .....: pd.read_csv( .....: StringIO(data), .....: engine="c", .....: float_precision=None, .....: )["c"][0] - float(val) .....: ) .....: Out[138]: 5.551115123125783e-17 In [139]: abs( .....: pd.read_csv( .....: StringIO(data), .....: engine="c", .....: float_precision="high", .....: )["c"][0] - float(val) .....: ) .....: Out[139]: 5.551115123125783e-17 In [140]: abs( .....: pd.read_csv(StringIO(data), engine="c", float_precision="round_trip")["c"][0] .....: - float(val) .....: ) .....: Out[140]: 0.0 Thousand separators# For large numbers that have been written with a thousands separator, you can set the thousands keyword to a string of length 1 so that integers will be parsed correctly: By default, numbers with a thousands separator will be parsed as strings: In [141]: data = ( .....: "ID|level|category\n" .....: "Patient1|123,000|x\n" .....: "Patient2|23,000|y\n" .....: "Patient3|1,234,018|z" .....: ) .....: In [142]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [143]: df = pd.read_csv("tmp.csv", sep="|") In [144]: df Out[144]: ID level category 0 Patient1 123,000 x 1 Patient2 23,000 y 2 Patient3 1,234,018 z In [145]: df.level.dtype Out[145]: dtype('O') The thousands keyword allows integers to be parsed correctly: In [146]: df = pd.read_csv("tmp.csv", sep="|", thousands=",") In [147]: df Out[147]: ID level category 0 Patient1 123000 x 1 Patient2 23000 y 2 Patient3 1234018 z In [148]: df.level.dtype Out[148]: dtype('int64') NA values# To control which values are parsed as missing values (which are signified by NaN), specify a string in na_values. If you specify a list of strings, then all values in it are considered to be missing values. If you specify a number (a float, like 5.0 or an integer like 5), the corresponding equivalent values will also imply a missing value (in this case effectively [5.0, 5] are recognized as NaN). To completely override the default values that are recognized as missing, specify keep_default_na=False. The default NaN recognized values are ['-1.#IND', '1.#QNAN', '1.#IND', '-1.#QNAN', '#N/A N/A', '#N/A', 'N/A', 'n/a', 'NA', '<NA>', '#NA', 'NULL', 'null', 'NaN', '-NaN', 'nan', '-nan', '']. Let us consider some examples: pd.read_csv("path_to_file.csv", na_values=[5]) In the example above 5 and 5.0 will be recognized as NaN, in addition to the defaults. A string will first be interpreted as a numerical 5, then as a NaN. pd.read_csv("path_to_file.csv", keep_default_na=False, na_values=[""]) Above, only an empty field will be recognized as NaN. pd.read_csv("path_to_file.csv", keep_default_na=False, na_values=["NA", "0"]) Above, both NA and 0 as strings are NaN. pd.read_csv("path_to_file.csv", na_values=["Nope"]) The default values, in addition to the string "Nope" are recognized as NaN. Infinity# inf like values will be parsed as np.inf (positive infinity), and -inf as -np.inf (negative infinity). These will ignore the case of the value, meaning Inf, will also be parsed as np.inf. Returning Series# Using the squeeze keyword, the parser will return output with a single column as a Series: Deprecated since version 1.4.0: Users should append .squeeze("columns") to the DataFrame returned by read_csv instead. In [149]: data = "level\nPatient1,123000\nPatient2,23000\nPatient3,1234018" In [150]: with open("tmp.csv", "w") as fh: .....: fh.write(data) .....: In [151]: print(open("tmp.csv").read()) level Patient1,123000 Patient2,23000 Patient3,1234018 In [152]: output = pd.read_csv("tmp.csv", squeeze=True) In [153]: output Out[153]: Patient1 123000 Patient2 23000 Patient3 1234018 Name: level, dtype: int64 In [154]: type(output) Out[154]: pandas.core.series.Series Boolean values# The common values True, False, TRUE, and FALSE are all recognized as boolean. Occasionally you might want to recognize other values as being boolean. To do this, use the true_values and false_values options as follows: In [155]: data = "a,b,c\n1,Yes,2\n3,No,4" In [156]: print(data) a,b,c 1,Yes,2 3,No,4 In [157]: pd.read_csv(StringIO(data)) Out[157]: a b c 0 1 Yes 2 1 3 No 4 In [158]: pd.read_csv(StringIO(data), true_values=["Yes"], false_values=["No"]) Out[158]: a b c 0 1 True 2 1 3 False 4 Handling “bad” lines# Some files may have malformed lines with too few fields or too many. Lines with too few fields will have NA values filled in the trailing fields. Lines with too many fields will raise an error by default: In [159]: data = "a,b,c\n1,2,3\n4,5,6,7\n8,9,10" In [160]: pd.read_csv(StringIO(data)) --------------------------------------------------------------------------- ParserError Traceback (most recent call last) Cell In[160], line 1 ----> 1 pd.read_csv(StringIO(data)) File ~/work/pandas/pandas/pandas/util/_decorators.py:211, in deprecate_kwarg.<locals>._deprecate_kwarg.<locals>.wrapper(*args, **kwargs) 209 else: 210 kwargs[new_arg_name] = new_arg_value --> 211 return func(*args, **kwargs) File ~/work/pandas/pandas/pandas/util/_decorators.py:331, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs) 325 if len(args) > num_allow_args: 326 warnings.warn( 327 msg.format(arguments=_format_argument_list(allow_args)), 328 FutureWarning, 329 stacklevel=find_stack_level(), 330 ) --> 331 return func(*args, **kwargs) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:950, in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, error_bad_lines, warn_bad_lines, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options) 935 kwds_defaults = _refine_defaults_read( 936 dialect, 937 delimiter, (...) 946 defaults={"delimiter": ","}, 947 ) 948 kwds.update(kwds_defaults) --> 950 return _read(filepath_or_buffer, kwds) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:611, in _read(filepath_or_buffer, kwds) 608 return parser 610 with parser: --> 611 return parser.read(nrows) File ~/work/pandas/pandas/pandas/io/parsers/readers.py:1778, in TextFileReader.read(self, nrows) 1771 nrows = validate_integer("nrows", nrows) 1772 try: 1773 # error: "ParserBase" has no attribute "read" 1774 ( 1775 index, 1776 columns, 1777 col_dict, -> 1778 ) = self._engine.read( # type: ignore[attr-defined] 1779 nrows 1780 ) 1781 except Exception: 1782 self.close() File ~/work/pandas/pandas/pandas/io/parsers/c_parser_wrapper.py:230, in CParserWrapper.read(self, nrows) 228 try: 229 if self.low_memory: --> 230 chunks = self._reader.read_low_memory(nrows) 231 # destructive to chunks 232 data = _concatenate_chunks(chunks) File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:808, in pandas._libs.parsers.TextReader.read_low_memory() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:866, in pandas._libs.parsers.TextReader._read_rows() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:852, in pandas._libs.parsers.TextReader._tokenize_rows() File ~/work/pandas/pandas/pandas/_libs/parsers.pyx:1973, in pandas._libs.parsers.raise_parser_error() ParserError: Error tokenizing data. C error: Expected 3 fields in line 3, saw 4 You can elect to skip bad lines: In [29]: pd.read_csv(StringIO(data), on_bad_lines="warn") Skipping line 3: expected 3 fields, saw 4 Out[29]: a b c 0 1 2 3 1 8 9 10 Or pass a callable function to handle the bad line if engine="python". The bad line will be a list of strings that was split by the sep: In [29]: external_list = [] In [30]: def bad_lines_func(line): ...: external_list.append(line) ...: return line[-3:] In [31]: pd.read_csv(StringIO(data), on_bad_lines=bad_lines_func, engine="python") Out[31]: a b c 0 1 2 3 1 5 6 7 2 8 9 10 In [32]: external_list Out[32]: [4, 5, 6, 7] .. versionadded:: 1.4.0 You can also use the usecols parameter to eliminate extraneous column data that appear in some lines but not others: In [33]: pd.read_csv(StringIO(data), usecols=[0, 1, 2]) Out[33]: a b c 0 1 2 3 1 4 5 6 2 8 9 10 In case you want to keep all data including the lines with too many fields, you can specify a sufficient number of names. This ensures that lines with not enough fields are filled with NaN. In [34]: pd.read_csv(StringIO(data), names=['a', 'b', 'c', 'd']) Out[34]: a b c d 0 1 2 3 NaN 1 4 5 6 7 2 8 9 10 NaN Dialect# The dialect keyword gives greater flexibility in specifying the file format. By default it uses the Excel dialect but you can specify either the dialect name or a csv.Dialect instance. Suppose you had data with unenclosed quotes: In [161]: data = "label1,label2,label3\n" 'index1,"a,c,e\n' "index2,b,d,f" In [162]: print(data) label1,label2,label3 index1,"a,c,e index2,b,d,f By default, read_csv uses the Excel dialect and treats the double quote as the quote character, which causes it to fail when it finds a newline before it finds the closing double quote. We can get around this using dialect: In [163]: import csv In [164]: dia = csv.excel() In [165]: dia.quoting = csv.QUOTE_NONE In [166]: pd.read_csv(StringIO(data), dialect=dia) Out[166]: label1 label2 label3 index1 "a c e index2 b d f All of the dialect options can be specified separately by keyword arguments: In [167]: data = "a,b,c~1,2,3~4,5,6" In [168]: pd.read_csv(StringIO(data), lineterminator="~") Out[168]: a b c 0 1 2 3 1 4 5 6 Another common dialect option is skipinitialspace, to skip any whitespace after a delimiter: In [169]: data = "a, b, c\n1, 2, 3\n4, 5, 6" In [170]: print(data) a, b, c 1, 2, 3 4, 5, 6 In [171]: pd.read_csv(StringIO(data), skipinitialspace=True) Out[171]: a b c 0 1 2 3 1 4 5 6 The parsers make every attempt to “do the right thing” and not be fragile. Type inference is a pretty big deal. If a column can be coerced to integer dtype without altering the contents, the parser will do so. Any non-numeric columns will come through as object dtype as with the rest of pandas objects. Quoting and Escape Characters# Quotes (and other escape characters) in embedded fields can be handled in any number of ways. One way is to use backslashes; to properly parse this data, you should pass the escapechar option: In [172]: data = 'a,b\n"hello, \\"Bob\\", nice to see you",5' In [173]: print(data) a,b "hello, \"Bob\", nice to see you",5 In [174]: pd.read_csv(StringIO(data), escapechar="\\") Out[174]: a b 0 hello, "Bob", nice to see you 5 Files with fixed width columns# While read_csv() reads delimited data, the read_fwf() function works with data files that have known and fixed column widths. The function parameters to read_fwf are largely the same as read_csv with two extra parameters, and a different usage of the delimiter parameter: colspecs: A list of pairs (tuples) giving the extents of the fixed-width fields of each line as half-open intervals (i.e., [from, to[ ). String value ‘infer’ can be used to instruct the parser to try detecting the column specifications from the first 100 rows of the data. Default behavior, if not specified, is to infer. widths: A list of field widths which can be used instead of ‘colspecs’ if the intervals are contiguous. delimiter: Characters to consider as filler characters in the fixed-width file. Can be used to specify the filler character of the fields if it is not spaces (e.g., ‘~’). Consider a typical fixed-width data file: In [175]: data1 = ( .....: "id8141 360.242940 149.910199 11950.7\n" .....: "id1594 444.953632 166.985655 11788.4\n" .....: "id1849 364.136849 183.628767 11806.2\n" .....: "id1230 413.836124 184.375703 11916.8\n" .....: "id1948 502.953953 173.237159 12468.3" .....: ) .....: In [176]: with open("bar.csv", "w") as f: .....: f.write(data1) .....: In order to parse this file into a DataFrame, we simply need to supply the column specifications to the read_fwf function along with the file name: # Column specifications are a list of half-intervals In [177]: colspecs = [(0, 6), (8, 20), (21, 33), (34, 43)] In [178]: df = pd.read_fwf("bar.csv", colspecs=colspecs, header=None, index_col=0) In [179]: df Out[179]: 1 2 3 0 id8141 360.242940 149.910199 11950.7 id1594 444.953632 166.985655 11788.4 id1849 364.136849 183.628767 11806.2 id1230 413.836124 184.375703 11916.8 id1948 502.953953 173.237159 12468.3 Note how the parser automatically picks column names X.<column number> when header=None argument is specified. Alternatively, you can supply just the column widths for contiguous columns: # Widths are a list of integers In [180]: widths = [6, 14, 13, 10] In [181]: df = pd.read_fwf("bar.csv", widths=widths, header=None) In [182]: df Out[182]: 0 1 2 3 0 id8141 360.242940 149.910199 11950.7 1 id1594 444.953632 166.985655 11788.4 2 id1849 364.136849 183.628767 11806.2 3 id1230 413.836124 184.375703 11916.8 4 id1948 502.953953 173.237159 12468.3 The parser will take care of extra white spaces around the columns so it’s ok to have extra separation between the columns in the file. By default, read_fwf will try to infer the file’s colspecs by using the first 100 rows of the file. It can do it only in cases when the columns are aligned and correctly separated by the provided delimiter (default delimiter is whitespace). In [183]: df = pd.read_fwf("bar.csv", header=None, index_col=0) In [184]: df Out[184]: 1 2 3 0 id8141 360.242940 149.910199 11950.7 id1594 444.953632 166.985655 11788.4 id1849 364.136849 183.628767 11806.2 id1230 413.836124 184.375703 11916.8 id1948 502.953953 173.237159 12468.3 read_fwf supports the dtype parameter for specifying the types of parsed columns to be different from the inferred type. In [185]: pd.read_fwf("bar.csv", header=None, index_col=0).dtypes Out[185]: 1 float64 2 float64 3 float64 dtype: object In [186]: pd.read_fwf("bar.csv", header=None, dtype={2: "object"}).dtypes Out[186]: 0 object 1 float64 2 object 3 float64 dtype: object Indexes# Files with an “implicit” index column# Consider a file with one less entry in the header than the number of data column: In [187]: data = "A,B,C\n20090101,a,1,2\n20090102,b,3,4\n20090103,c,4,5" In [188]: print(data) A,B,C 20090101,a,1,2 20090102,b,3,4 20090103,c,4,5 In [189]: with open("foo.csv", "w") as f: .....: f.write(data) .....: In this special case, read_csv assumes that the first column is to be used as the index of the DataFrame: In [190]: pd.read_csv("foo.csv") Out[190]: A B C 20090101 a 1 2 20090102 b 3 4 20090103 c 4 5 Note that the dates weren’t automatically parsed. In that case you would need to do as before: In [191]: df = pd.read_csv("foo.csv", parse_dates=True) In [192]: df.index Out[192]: DatetimeIndex(['2009-01-01', '2009-01-02', '2009-01-03'], dtype='datetime64[ns]', freq=None) Reading an index with a MultiIndex# Suppose you have data indexed by two columns: In [193]: data = 'year,indiv,zit,xit\n1977,"A",1.2,.6\n1977,"B",1.5,.5' In [194]: print(data) year,indiv,zit,xit 1977,"A",1.2,.6 1977,"B",1.5,.5 In [195]: with open("mindex_ex.csv", mode="w") as f: .....: f.write(data) .....: The index_col argument to read_csv can take a list of column numbers to turn multiple columns into a MultiIndex for the index of the returned object: In [196]: df = pd.read_csv("mindex_ex.csv", index_col=[0, 1]) In [197]: df Out[197]: zit xit year indiv 1977 A 1.2 0.6 B 1.5 0.5 In [198]: df.loc[1977] Out[198]: zit xit indiv A 1.2 0.6 B 1.5 0.5 Reading columns with a MultiIndex# By specifying list of row locations for the header argument, you can read in a MultiIndex for the columns. Specifying non-consecutive rows will skip the intervening rows. In [199]: from pandas._testing import makeCustomDataframe as mkdf In [200]: df = mkdf(5, 3, r_idx_nlevels=2, c_idx_nlevels=4) In [201]: df.to_csv("mi.csv") In [202]: print(open("mi.csv").read()) C0,,C_l0_g0,C_l0_g1,C_l0_g2 C1,,C_l1_g0,C_l1_g1,C_l1_g2 C2,,C_l2_g0,C_l2_g1,C_l2_g2 C3,,C_l3_g0,C_l3_g1,C_l3_g2 R0,R1,,, R_l0_g0,R_l1_g0,R0C0,R0C1,R0C2 R_l0_g1,R_l1_g1,R1C0,R1C1,R1C2 R_l0_g2,R_l1_g2,R2C0,R2C1,R2C2 R_l0_g3,R_l1_g3,R3C0,R3C1,R3C2 R_l0_g4,R_l1_g4,R4C0,R4C1,R4C2 In [203]: pd.read_csv("mi.csv", header=[0, 1, 2, 3], index_col=[0, 1]) Out[203]: C0 C_l0_g0 C_l0_g1 C_l0_g2 C1 C_l1_g0 C_l1_g1 C_l1_g2 C2 C_l2_g0 C_l2_g1 C_l2_g2 C3 C_l3_g0 C_l3_g1 C_l3_g2 R0 R1 R_l0_g0 R_l1_g0 R0C0 R0C1 R0C2 R_l0_g1 R_l1_g1 R1C0 R1C1 R1C2 R_l0_g2 R_l1_g2 R2C0 R2C1 R2C2 R_l0_g3 R_l1_g3 R3C0 R3C1 R3C2 R_l0_g4 R_l1_g4 R4C0 R4C1 R4C2 read_csv is also able to interpret a more common format of multi-columns indices. In [204]: data = ",a,a,a,b,c,c\n,q,r,s,t,u,v\none,1,2,3,4,5,6\ntwo,7,8,9,10,11,12" In [205]: print(data) ,a,a,a,b,c,c ,q,r,s,t,u,v one,1,2,3,4,5,6 two,7,8,9,10,11,12 In [206]: with open("mi2.csv", "w") as fh: .....: fh.write(data) .....: In [207]: pd.read_csv("mi2.csv", header=[0, 1], index_col=0) Out[207]: a b c q r s t u v one 1 2 3 4 5 6 two 7 8 9 10 11 12 Note If an index_col is not specified (e.g. you don’t have an index, or wrote it with df.to_csv(..., index=False), then any names on the columns index will be lost. Automatically “sniffing” the delimiter# read_csv is capable of inferring delimited (not necessarily comma-separated) files, as pandas uses the csv.Sniffer class of the csv module. For this, you have to specify sep=None. In [208]: df = pd.DataFrame(np.random.randn(10, 4)) In [209]: df.to_csv("tmp.csv", sep="|") In [210]: df.to_csv("tmp2.csv", sep=":") In [211]: pd.read_csv("tmp2.csv", sep=None, engine="python") Out[211]: Unnamed: 0 0 1 2 3 0 0 0.469112 -0.282863 -1.509059 -1.135632 1 1 1.212112 -0.173215 0.119209 -1.044236 2 2 -0.861849 -2.104569 -0.494929 1.071804 3 3 0.721555 -0.706771 -1.039575 0.271860 4 4 -0.424972 0.567020 0.276232 -1.087401 5 5 -0.673690 0.113648 -1.478427 0.524988 6 6 0.404705 0.577046 -1.715002 -1.039268 7 7 -0.370647 -1.157892 -1.344312 0.844885 8 8 1.075770 -0.109050 1.643563 -1.469388 9 9 0.357021 -0.674600 -1.776904 -0.968914 Reading multiple files to create a single DataFrame# It’s best to use concat() to combine multiple files. See the cookbook for an example. Iterating through files chunk by chunk# Suppose you wish to iterate through a (potentially very large) file lazily rather than reading the entire file into memory, such as the following: In [212]: df = pd.DataFrame(np.random.randn(10, 4)) In [213]: df.to_csv("tmp.csv", sep="|") In [214]: table = pd.read_csv("tmp.csv", sep="|") In [215]: table Out[215]: Unnamed: 0 0 1 2 3 0 0 -1.294524 0.413738 0.276662 -0.472035 1 1 -0.013960 -0.362543 -0.006154 -0.923061 2 2 0.895717 0.805244 -1.206412 2.565646 3 3 1.431256 1.340309 -1.170299 -0.226169 4 4 0.410835 0.813850 0.132003 -0.827317 5 5 -0.076467 -1.187678 1.130127 -1.436737 6 6 -1.413681 1.607920 1.024180 0.569605 7 7 0.875906 -2.211372 0.974466 -2.006747 8 8 -0.410001 -0.078638 0.545952 -1.219217 9 9 -1.226825 0.769804 -1.281247 -0.727707 By specifying a chunksize to read_csv, the return value will be an iterable object of type TextFileReader: In [216]: with pd.read_csv("tmp.csv", sep="|", chunksize=4) as reader: .....: reader .....: for chunk in reader: .....: print(chunk) .....: Unnamed: 0 0 1 2 3 0 0 -1.294524 0.413738 0.276662 -0.472035 1 1 -0.013960 -0.362543 -0.006154 -0.923061 2 2 0.895717 0.805244 -1.206412 2.565646 3 3 1.431256 1.340309 -1.170299 -0.226169 Unnamed: 0 0 1 2 3 4 4 0.410835 0.813850 0.132003 -0.827317 5 5 -0.076467 -1.187678 1.130127 -1.436737 6 6 -1.413681 1.607920 1.024180 0.569605 7 7 0.875906 -2.211372 0.974466 -2.006747 Unnamed: 0 0 1 2 3 8 8 -0.410001 -0.078638 0.545952 -1.219217 9 9 -1.226825 0.769804 -1.281247 -0.727707 Changed in version 1.2: read_csv/json/sas return a context-manager when iterating through a file. Specifying iterator=True will also return the TextFileReader object: In [217]: with pd.read_csv("tmp.csv", sep="|", iterator=True) as reader: .....: reader.get_chunk(5) .....: Specifying the parser engine# Pandas currently supports three engines, the C engine, the python engine, and an experimental pyarrow engine (requires the pyarrow package). In general, the pyarrow engine is fastest on larger workloads and is equivalent in speed to the C engine on most other workloads. The python engine tends to be slower than the pyarrow and C engines on most workloads. However, the pyarrow engine is much less robust than the C engine, which lacks a few features compared to the Python engine. Where possible, pandas uses the C parser (specified as engine='c'), but it may fall back to Python if C-unsupported options are specified. Currently, options unsupported by the C and pyarrow engines include: sep other than a single character (e.g. regex separators) skipfooter sep=None with delim_whitespace=False Specifying any of the above options will produce a ParserWarning unless the python engine is selected explicitly using engine='python'. Options that are unsupported by the pyarrow engine which are not covered by the list above include: float_precision chunksize comment nrows thousands memory_map dialect warn_bad_lines error_bad_lines on_bad_lines delim_whitespace quoting lineterminator converters decimal iterator dayfirst infer_datetime_format verbose skipinitialspace low_memory Specifying these options with engine='pyarrow' will raise a ValueError. Reading/writing remote files# You can pass in a URL to read or write remote files to many of pandas’ IO functions - the following example shows reading a CSV file: df = pd.read_csv("https://download.bls.gov/pub/time.series/cu/cu.item", sep="\t") New in version 1.3.0. A custom header can be sent alongside HTTP(s) requests by passing a dictionary of header key value mappings to the storage_options keyword argument as shown below: headers = {"User-Agent": "pandas"} df = pd.read_csv( "https://download.bls.gov/pub/time.series/cu/cu.item", sep="\t", storage_options=headers ) All URLs which are not local files or HTTP(s) are handled by fsspec, if installed, and its various filesystem implementations (including Amazon S3, Google Cloud, SSH, FTP, webHDFS…). Some of these implementations will require additional packages to be installed, for example S3 URLs require the s3fs library: df = pd.read_json("s3://pandas-test/adatafile.json") When dealing with remote storage systems, you might need extra configuration with environment variables or config files in special locations. For example, to access data in your S3 bucket, you will need to define credentials in one of the several ways listed in the S3Fs documentation. The same is true for several of the storage backends, and you should follow the links at fsimpl1 for implementations built into fsspec and fsimpl2 for those not included in the main fsspec distribution. You can also pass parameters directly to the backend driver. For example, if you do not have S3 credentials, you can still access public data by specifying an anonymous connection, such as New in version 1.2.0. pd.read_csv( "s3://ncei-wcsd-archive/data/processed/SH1305/18kHz/SaKe2013" "-D20130523-T080854_to_SaKe2013-D20130523-T085643.csv", storage_options={"anon": True}, ) fsspec also allows complex URLs, for accessing data in compressed archives, local caching of files, and more. To locally cache the above example, you would modify the call to pd.read_csv( "simplecache::s3://ncei-wcsd-archive/data/processed/SH1305/18kHz/" "SaKe2013-D20130523-T080854_to_SaKe2013-D20130523-T085643.csv", storage_options={"s3": {"anon": True}}, ) where we specify that the “anon” parameter is meant for the “s3” part of the implementation, not to the caching implementation. Note that this caches to a temporary directory for the duration of the session only, but you can also specify a permanent store. Writing out data# Writing to CSV format# The Series and DataFrame objects have an instance method to_csv which allows storing the contents of the object as a comma-separated-values file. The function takes a number of arguments. Only the first is required. path_or_buf: A string path to the file to write or a file object. If a file object it must be opened with newline='' sep : Field delimiter for the output file (default “,”) na_rep: A string representation of a missing value (default ‘’) float_format: Format string for floating point numbers columns: Columns to write (default None) header: Whether to write out the column names (default True) index: whether to write row (index) names (default True) index_label: Column label(s) for index column(s) if desired. If None (default), and header and index are True, then the index names are used. (A sequence should be given if the DataFrame uses MultiIndex). mode : Python write mode, default ‘w’ encoding: a string representing the encoding to use if the contents are non-ASCII, for Python versions prior to 3 lineterminator: Character sequence denoting line end (default os.linesep) quoting: Set quoting rules as in csv module (default csv.QUOTE_MINIMAL). Note that if you have set a float_format then floats are converted to strings and csv.QUOTE_NONNUMERIC will treat them as non-numeric quotechar: Character used to quote fields (default ‘”’) doublequote: Control quoting of quotechar in fields (default True) escapechar: Character used to escape sep and quotechar when appropriate (default None) chunksize: Number of rows to write at a time date_format: Format string for datetime objects Writing a formatted string# The DataFrame object has an instance method to_string which allows control over the string representation of the object. All arguments are optional: buf default None, for example a StringIO object columns default None, which columns to write col_space default None, minimum width of each column. na_rep default NaN, representation of NA value formatters default None, a dictionary (by column) of functions each of which takes a single argument and returns a formatted string float_format default None, a function which takes a single (float) argument and returns a formatted string; to be applied to floats in the DataFrame. sparsify default True, set to False for a DataFrame with a hierarchical index to print every MultiIndex key at each row. index_names default True, will print the names of the indices index default True, will print the index (ie, row labels) header default True, will print the column labels justify default left, will print column headers left- or right-justified The Series object also has a to_string method, but with only the buf, na_rep, float_format arguments. There is also a length argument which, if set to True, will additionally output the length of the Series. JSON# Read and write JSON format files and strings. Writing JSON# A Series or DataFrame can be converted to a valid JSON string. Use to_json with optional parameters: path_or_buf : the pathname or buffer to write the output This can be None in which case a JSON string is returned orient : Series: default is index allowed values are {split, records, index} DataFrame: default is columns allowed values are {split, records, index, columns, values, table} The format of the JSON string split dict like {index -> [index], columns -> [columns], data -> [values]} records list like [{column -> value}, … , {column -> value}] index dict like {index -> {column -> value}} columns dict like {column -> {index -> value}} values just the values array table adhering to the JSON Table Schema date_format : string, type of date conversion, ‘epoch’ for timestamp, ‘iso’ for ISO8601. double_precision : The number of decimal places to use when encoding floating point values, default 10. force_ascii : force encoded string to be ASCII, default True. date_unit : The time unit to encode to, governs timestamp and ISO8601 precision. One of ‘s’, ‘ms’, ‘us’ or ‘ns’ for seconds, milliseconds, microseconds and nanoseconds respectively. Default ‘ms’. default_handler : The handler to call if an object cannot otherwise be converted to a suitable format for JSON. Takes a single argument, which is the object to convert, and returns a serializable object. lines : If records orient, then will write each record per line as json. Note NaN’s, NaT’s and None will be converted to null and datetime objects will be converted based on the date_format and date_unit parameters. In [218]: dfj = pd.DataFrame(np.random.randn(5, 2), columns=list("AB")) In [219]: json = dfj.to_json() In [220]: json Out[220]: '{"A":{"0":-0.1213062281,"1":0.6957746499,"2":0.9597255933,"3":-0.6199759194,"4":-0.7323393705},"B":{"0":-0.0978826728,"1":0.3417343559,"2":-1.1103361029,"3":0.1497483186,"4":0.6877383895}}' Orient options# There are a number of different options for the format of the resulting JSON file / string. Consider the following DataFrame and Series: In [221]: dfjo = pd.DataFrame( .....: dict(A=range(1, 4), B=range(4, 7), C=range(7, 10)), .....: columns=list("ABC"), .....: index=list("xyz"), .....: ) .....: In [222]: dfjo Out[222]: A B C x 1 4 7 y 2 5 8 z 3 6 9 In [223]: sjo = pd.Series(dict(x=15, y=16, z=17), name="D") In [224]: sjo Out[224]: x 15 y 16 z 17 Name: D, dtype: int64 Column oriented (the default for DataFrame) serializes the data as nested JSON objects with column labels acting as the primary index: In [225]: dfjo.to_json(orient="columns") Out[225]: '{"A":{"x":1,"y":2,"z":3},"B":{"x":4,"y":5,"z":6},"C":{"x":7,"y":8,"z":9}}' # Not available for Series Index oriented (the default for Series) similar to column oriented but the index labels are now primary: In [226]: dfjo.to_json(orient="index") Out[226]: '{"x":{"A":1,"B":4,"C":7},"y":{"A":2,"B":5,"C":8},"z":{"A":3,"B":6,"C":9}}' In [227]: sjo.to_json(orient="index") Out[227]: '{"x":15,"y":16,"z":17}' Record oriented serializes the data to a JSON array of column -> value records, index labels are not included. This is useful for passing DataFrame data to plotting libraries, for example the JavaScript library d3.js: In [228]: dfjo.to_json(orient="records") Out[228]: '[{"A":1,"B":4,"C":7},{"A":2,"B":5,"C":8},{"A":3,"B":6,"C":9}]' In [229]: sjo.to_json(orient="records") Out[229]: '[15,16,17]' Value oriented is a bare-bones option which serializes to nested JSON arrays of values only, column and index labels are not included: In [230]: dfjo.to_json(orient="values") Out[230]: '[[1,4,7],[2,5,8],[3,6,9]]' # Not available for Series Split oriented serializes to a JSON object containing separate entries for values, index and columns. Name is also included for Series: In [231]: dfjo.to_json(orient="split") Out[231]: '{"columns":["A","B","C"],"index":["x","y","z"],"data":[[1,4,7],[2,5,8],[3,6,9]]}' In [232]: sjo.to_json(orient="split") Out[232]: '{"name":"D","index":["x","y","z"],"data":[15,16,17]}' Table oriented serializes to the JSON Table Schema, allowing for the preservation of metadata including but not limited to dtypes and index names. Note Any orient option that encodes to a JSON object will not preserve the ordering of index and column labels during round-trip serialization. If you wish to preserve label ordering use the split option as it uses ordered containers. Date handling# Writing in ISO date format: In [233]: dfd = pd.DataFrame(np.random.randn(5, 2), columns=list("AB")) In [234]: dfd["date"] = pd.Timestamp("20130101") In [235]: dfd = dfd.sort_index(axis=1, ascending=False) In [236]: json = dfd.to_json(date_format="iso") In [237]: json Out[237]: '{"date":{"0":"2013-01-01T00:00:00.000","1":"2013-01-01T00:00:00.000","2":"2013-01-01T00:00:00.000","3":"2013-01-01T00:00:00.000","4":"2013-01-01T00:00:00.000"},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Writing in ISO date format, with microseconds: In [238]: json = dfd.to_json(date_format="iso", date_unit="us") In [239]: json Out[239]: '{"date":{"0":"2013-01-01T00:00:00.000000","1":"2013-01-01T00:00:00.000000","2":"2013-01-01T00:00:00.000000","3":"2013-01-01T00:00:00.000000","4":"2013-01-01T00:00:00.000000"},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Epoch timestamps, in seconds: In [240]: json = dfd.to_json(date_format="epoch", date_unit="s") In [241]: json Out[241]: '{"date":{"0":1356998400,"1":1356998400,"2":1356998400,"3":1356998400,"4":1356998400},"B":{"0":0.403309524,"1":0.3016244523,"2":-1.3698493577,"3":1.4626960492,"4":-0.8265909164},"A":{"0":0.1764443426,"1":-0.1549507744,"2":-2.1798606054,"3":-0.9542078401,"4":-1.7431609117}}' Writing to a file, with a date index and a date column: In [242]: dfj2 = dfj.copy() In [243]: dfj2["date"] = pd.Timestamp("20130101") In [244]: dfj2["ints"] = list(range(5)) In [245]: dfj2["bools"] = True In [246]: dfj2.index = pd.date_range("20130101", periods=5) In [247]: dfj2.to_json("test.json") In [248]: with open("test.json") as fh: .....: print(fh.read()) .....: {"A":{"1356998400000":-0.1213062281,"1357084800000":0.6957746499,"1357171200000":0.9597255933,"1357257600000":-0.6199759194,"1357344000000":-0.7323393705},"B":{"1356998400000":-0.0978826728,"1357084800000":0.3417343559,"1357171200000":-1.1103361029,"1357257600000":0.1497483186,"1357344000000":0.6877383895},"date":{"1356998400000":1356998400000,"1357084800000":1356998400000,"1357171200000":1356998400000,"1357257600000":1356998400000,"1357344000000":1356998400000},"ints":{"1356998400000":0,"1357084800000":1,"1357171200000":2,"1357257600000":3,"1357344000000":4},"bools":{"1356998400000":true,"1357084800000":true,"1357171200000":true,"1357257600000":true,"1357344000000":true}} Fallback behavior# If the JSON serializer cannot handle the container contents directly it will fall back in the following manner: if the dtype is unsupported (e.g. np.complex_) then the default_handler, if provided, will be called for each value, otherwise an exception is raised. if an object is unsupported it will attempt the following: check if the object has defined a toDict method and call it. A toDict method should return a dict which will then be JSON serialized. invoke the default_handler if one was provided. convert the object to a dict by traversing its contents. However this will often fail with an OverflowError or give unexpected results. In general the best approach for unsupported objects or dtypes is to provide a default_handler. For example: >>> DataFrame([1.0, 2.0, complex(1.0, 2.0)]).to_json() # raises RuntimeError: Unhandled numpy dtype 15 can be dealt with by specifying a simple default_handler: In [249]: pd.DataFrame([1.0, 2.0, complex(1.0, 2.0)]).to_json(default_handler=str) Out[249]: '{"0":{"0":"(1+0j)","1":"(2+0j)","2":"(1+2j)"}}' Reading JSON# Reading a JSON string to pandas object can take a number of parameters. The parser will try to parse a DataFrame if typ is not supplied or is None. To explicitly force Series parsing, pass typ=series filepath_or_buffer : a VALID JSON string or file handle / StringIO. The string could be a URL. Valid URL schemes include http, ftp, S3, and file. For file URLs, a host is expected. For instance, a local file could be file ://localhost/path/to/table.json typ : type of object to recover (series or frame), default ‘frame’ orient : Series : default is index allowed values are {split, records, index} DataFrame default is columns allowed values are {split, records, index, columns, values, table} The format of the JSON string split dict like {index -> [index], columns -> [columns], data -> [values]} records list like [{column -> value}, … , {column -> value}] index dict like {index -> {column -> value}} columns dict like {column -> {index -> value}} values just the values array table adhering to the JSON Table Schema dtype : if True, infer dtypes, if a dict of column to dtype, then use those, if False, then don’t infer dtypes at all, default is True, apply only to the data. convert_axes : boolean, try to convert the axes to the proper dtypes, default is True convert_dates : a list of columns to parse for dates; If True, then try to parse date-like columns, default is True. keep_default_dates : boolean, default True. If parsing dates, then parse the default date-like columns. numpy : direct decoding to NumPy arrays. default is False; Supports numeric data only, although labels may be non-numeric. Also note that the JSON ordering MUST be the same for each term if numpy=True. precise_float : boolean, default False. Set to enable usage of higher precision (strtod) function when decoding string to double values. Default (False) is to use fast but less precise builtin functionality. date_unit : string, the timestamp unit to detect if converting dates. Default None. By default the timestamp precision will be detected, if this is not desired then pass one of ‘s’, ‘ms’, ‘us’ or ‘ns’ to force timestamp precision to seconds, milliseconds, microseconds or nanoseconds respectively. lines : reads file as one json object per line. encoding : The encoding to use to decode py3 bytes. chunksize : when used in combination with lines=True, return a JsonReader which reads in chunksize lines per iteration. The parser will raise one of ValueError/TypeError/AssertionError if the JSON is not parseable. If a non-default orient was used when encoding to JSON be sure to pass the same option here so that decoding produces sensible results, see Orient Options for an overview. Data conversion# The default of convert_axes=True, dtype=True, and convert_dates=True will try to parse the axes, and all of the data into appropriate types, including dates. If you need to override specific dtypes, pass a dict to dtype. convert_axes should only be set to False if you need to preserve string-like numbers (e.g. ‘1’, ‘2’) in an axes. Note Large integer values may be converted to dates if convert_dates=True and the data and / or column labels appear ‘date-like’. The exact threshold depends on the date_unit specified. ‘date-like’ means that the column label meets one of the following criteria: it ends with '_at' it ends with '_time' it begins with 'timestamp' it is 'modified' it is 'date' Warning When reading JSON data, automatic coercing into dtypes has some quirks: an index can be reconstructed in a different order from serialization, that is, the returned order is not guaranteed to be the same as before serialization a column that was float data will be converted to integer if it can be done safely, e.g. a column of 1. bool columns will be converted to integer on reconstruction Thus there are times where you may want to specify specific dtypes via the dtype keyword argument. Reading from a JSON string: In [250]: pd.read_json(json) Out[250]: date B A 0 2013-01-01 0.403310 0.176444 1 2013-01-01 0.301624 -0.154951 2 2013-01-01 -1.369849 -2.179861 3 2013-01-01 1.462696 -0.954208 4 2013-01-01 -0.826591 -1.743161 Reading from a file: In [251]: pd.read_json("test.json") Out[251]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True Don’t convert any data (but still convert axes and dates): In [252]: pd.read_json("test.json", dtype=object).dtypes Out[252]: A object B object date object ints object bools object dtype: object Specify dtypes for conversion: In [253]: pd.read_json("test.json", dtype={"A": "float32", "bools": "int8"}).dtypes Out[253]: A float32 B float64 date datetime64[ns] ints int64 bools int8 dtype: object Preserve string indices: In [254]: si = pd.DataFrame( .....: np.zeros((4, 4)), columns=list(range(4)), index=[str(i) for i in range(4)] .....: ) .....: In [255]: si Out[255]: 0 1 2 3 0 0.0 0.0 0.0 0.0 1 0.0 0.0 0.0 0.0 2 0.0 0.0 0.0 0.0 3 0.0 0.0 0.0 0.0 In [256]: si.index Out[256]: Index(['0', '1', '2', '3'], dtype='object') In [257]: si.columns Out[257]: Int64Index([0, 1, 2, 3], dtype='int64') In [258]: json = si.to_json() In [259]: sij = pd.read_json(json, convert_axes=False) In [260]: sij Out[260]: 0 1 2 3 0 0 0 0 0 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 In [261]: sij.index Out[261]: Index(['0', '1', '2', '3'], dtype='object') In [262]: sij.columns Out[262]: Index(['0', '1', '2', '3'], dtype='object') Dates written in nanoseconds need to be read back in nanoseconds: In [263]: json = dfj2.to_json(date_unit="ns") # Try to parse timestamps as milliseconds -> Won't Work In [264]: dfju = pd.read_json(json, date_unit="ms") In [265]: dfju Out[265]: A B date ints bools 1356998400000000000 -0.121306 -0.097883 1356998400000000000 0 True 1357084800000000000 0.695775 0.341734 1356998400000000000 1 True 1357171200000000000 0.959726 -1.110336 1356998400000000000 2 True 1357257600000000000 -0.619976 0.149748 1356998400000000000 3 True 1357344000000000000 -0.732339 0.687738 1356998400000000000 4 True # Let pandas detect the correct precision In [266]: dfju = pd.read_json(json) In [267]: dfju Out[267]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True # Or specify that all timestamps are in nanoseconds In [268]: dfju = pd.read_json(json, date_unit="ns") In [269]: dfju Out[269]: A B date ints bools 2013-01-01 -0.121306 -0.097883 2013-01-01 0 True 2013-01-02 0.695775 0.341734 2013-01-01 1 True 2013-01-03 0.959726 -1.110336 2013-01-01 2 True 2013-01-04 -0.619976 0.149748 2013-01-01 3 True 2013-01-05 -0.732339 0.687738 2013-01-01 4 True The Numpy parameter# Note This param has been deprecated as of version 1.0.0 and will raise a FutureWarning. This supports numeric data only. Index and columns labels may be non-numeric, e.g. strings, dates etc. If numpy=True is passed to read_json an attempt will be made to sniff an appropriate dtype during deserialization and to subsequently decode directly to NumPy arrays, bypassing the need for intermediate Python objects. This can provide speedups if you are deserialising a large amount of numeric data: In [270]: randfloats = np.random.uniform(-100, 1000, 10000) In [271]: randfloats.shape = (1000, 10) In [272]: dffloats = pd.DataFrame(randfloats, columns=list("ABCDEFGHIJ")) In [273]: jsonfloats = dffloats.to_json() In [274]: %timeit pd.read_json(jsonfloats) 7.91 ms +- 77.3 us per loop (mean +- std. dev. of 7 runs, 100 loops each) In [275]: %timeit pd.read_json(jsonfloats, numpy=True) 5.71 ms +- 333 us per loop (mean +- std. dev. of 7 runs, 100 loops each) The speedup is less noticeable for smaller datasets: In [276]: jsonfloats = dffloats.head(100).to_json() In [277]: %timeit pd.read_json(jsonfloats) 4.46 ms +- 25.9 us per loop (mean +- std. dev. of 7 runs, 100 loops each) In [278]: %timeit pd.read_json(jsonfloats, numpy=True) 4.09 ms +- 32.3 us per loop (mean +- std. dev. of 7 runs, 100 loops each) Warning Direct NumPy decoding makes a number of assumptions and may fail or produce unexpected output if these assumptions are not satisfied: data is numeric. data is uniform. The dtype is sniffed from the first value decoded. A ValueError may be raised, or incorrect output may be produced if this condition is not satisfied. labels are ordered. Labels are only read from the first container, it is assumed that each subsequent row / column has been encoded in the same order. This should be satisfied if the data was encoded using to_json but may not be the case if the JSON is from another source. Normalization# pandas provides a utility function to take a dict or list of dicts and normalize this semi-structured data into a flat table. In [279]: data = [ .....: {"id": 1, "name": {"first": "Coleen", "last": "Volk"}}, .....: {"name": {"given": "Mark", "family": "Regner"}}, .....: {"id": 2, "name": "Faye Raker"}, .....: ] .....: In [280]: pd.json_normalize(data) Out[280]: id name.first name.last name.given name.family name 0 1.0 Coleen Volk NaN NaN NaN 1 NaN NaN NaN Mark Regner NaN 2 2.0 NaN NaN NaN NaN Faye Raker In [281]: data = [ .....: { .....: "state": "Florida", .....: "shortname": "FL", .....: "info": {"governor": "Rick Scott"}, .....: "county": [ .....: {"name": "Dade", "population": 12345}, .....: {"name": "Broward", "population": 40000}, .....: {"name": "Palm Beach", "population": 60000}, .....: ], .....: }, .....: { .....: "state": "Ohio", .....: "shortname": "OH", .....: "info": {"governor": "John Kasich"}, .....: "county": [ .....: {"name": "Summit", "population": 1234}, .....: {"name": "Cuyahoga", "population": 1337}, .....: ], .....: }, .....: ] .....: In [282]: pd.json_normalize(data, "county", ["state", "shortname", ["info", "governor"]]) Out[282]: name population state shortname info.governor 0 Dade 12345 Florida FL Rick Scott 1 Broward 40000 Florida FL Rick Scott 2 Palm Beach 60000 Florida FL Rick Scott 3 Summit 1234 Ohio OH John Kasich 4 Cuyahoga 1337 Ohio OH John Kasich The max_level parameter provides more control over which level to end normalization. With max_level=1 the following snippet normalizes until 1st nesting level of the provided dict. In [283]: data = [ .....: { .....: "CreatedBy": {"Name": "User001"}, .....: "Lookup": { .....: "TextField": "Some text", .....: "UserField": {"Id": "ID001", "Name": "Name001"}, .....: }, .....: "Image": {"a": "b"}, .....: } .....: ] .....: In [284]: pd.json_normalize(data, max_level=1) Out[284]: CreatedBy.Name Lookup.TextField Lookup.UserField Image.a 0 User001 Some text {'Id': 'ID001', 'Name': 'Name001'} b Line delimited json# pandas is able to read and write line-delimited json files that are common in data processing pipelines using Hadoop or Spark. For line-delimited json files, pandas can also return an iterator which reads in chunksize lines at a time. This can be useful for large files or to read from a stream. In [285]: jsonl = """ .....: {"a": 1, "b": 2} .....: {"a": 3, "b": 4} .....: """ .....: In [286]: df = pd.read_json(jsonl, lines=True) In [287]: df Out[287]: a b 0 1 2 1 3 4 In [288]: df.to_json(orient="records", lines=True) Out[288]: '{"a":1,"b":2}\n{"a":3,"b":4}\n' # reader is an iterator that returns ``chunksize`` lines each iteration In [289]: with pd.read_json(StringIO(jsonl), lines=True, chunksize=1) as reader: .....: reader .....: for chunk in reader: .....: print(chunk) .....: Empty DataFrame Columns: [] Index: [] a b 0 1 2 a b 1 3 4 Table schema# Table Schema is a spec for describing tabular datasets as a JSON object. The JSON includes information on the field names, types, and other attributes. You can use the orient table to build a JSON string with two fields, schema and data. In [290]: df = pd.DataFrame( .....: { .....: "A": [1, 2, 3], .....: "B": ["a", "b", "c"], .....: "C": pd.date_range("2016-01-01", freq="d", periods=3), .....: }, .....: index=pd.Index(range(3), name="idx"), .....: ) .....: In [291]: df Out[291]: A B C idx 0 1 a 2016-01-01 1 2 b 2016-01-02 2 3 c 2016-01-03 In [292]: df.to_json(orient="table", date_format="iso") Out[292]: '{"schema":{"fields":[{"name":"idx","type":"integer"},{"name":"A","type":"integer"},{"name":"B","type":"string"},{"name":"C","type":"datetime"}],"primaryKey":["idx"],"pandas_version":"1.4.0"},"data":[{"idx":0,"A":1,"B":"a","C":"2016-01-01T00:00:00.000"},{"idx":1,"A":2,"B":"b","C":"2016-01-02T00:00:00.000"},{"idx":2,"A":3,"B":"c","C":"2016-01-03T00:00:00.000"}]}' The schema field contains the fields key, which itself contains a list of column name to type pairs, including the Index or MultiIndex (see below for a list of types). The schema field also contains a primaryKey field if the (Multi)index is unique. The second field, data, contains the serialized data with the records orient. The index is included, and any datetimes are ISO 8601 formatted, as required by the Table Schema spec. The full list of types supported are described in the Table Schema spec. This table shows the mapping from pandas types: pandas type Table Schema type int64 integer float64 number bool boolean datetime64[ns] datetime timedelta64[ns] duration categorical any object str A few notes on the generated table schema: The schema object contains a pandas_version field. This contains the version of pandas’ dialect of the schema, and will be incremented with each revision. All dates are converted to UTC when serializing. Even timezone naive values, which are treated as UTC with an offset of 0. In [293]: from pandas.io.json import build_table_schema In [294]: s = pd.Series(pd.date_range("2016", periods=4)) In [295]: build_table_schema(s) Out[295]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'datetime'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} datetimes with a timezone (before serializing), include an additional field tz with the time zone name (e.g. 'US/Central'). In [296]: s_tz = pd.Series(pd.date_range("2016", periods=12, tz="US/Central")) In [297]: build_table_schema(s_tz) Out[297]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'datetime', 'tz': 'US/Central'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} Periods are converted to timestamps before serialization, and so have the same behavior of being converted to UTC. In addition, periods will contain and additional field freq with the period’s frequency, e.g. 'A-DEC'. In [298]: s_per = pd.Series(1, index=pd.period_range("2016", freq="A-DEC", periods=4)) In [299]: build_table_schema(s_per) Out[299]: {'fields': [{'name': 'index', 'type': 'datetime', 'freq': 'A-DEC'}, {'name': 'values', 'type': 'integer'}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} Categoricals use the any type and an enum constraint listing the set of possible values. Additionally, an ordered field is included: In [300]: s_cat = pd.Series(pd.Categorical(["a", "b", "a"])) In [301]: build_table_schema(s_cat) Out[301]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'any', 'constraints': {'enum': ['a', 'b']}, 'ordered': False}], 'primaryKey': ['index'], 'pandas_version': '1.4.0'} A primaryKey field, containing an array of labels, is included if the index is unique: In [302]: s_dupe = pd.Series([1, 2], index=[1, 1]) In [303]: build_table_schema(s_dupe) Out[303]: {'fields': [{'name': 'index', 'type': 'integer'}, {'name': 'values', 'type': 'integer'}], 'pandas_version': '1.4.0'} The primaryKey behavior is the same with MultiIndexes, but in this case the primaryKey is an array: In [304]: s_multi = pd.Series(1, index=pd.MultiIndex.from_product([("a", "b"), (0, 1)])) In [305]: build_table_schema(s_multi) Out[305]: {'fields': [{'name': 'level_0', 'type': 'string'}, {'name': 'level_1', 'type': 'integer'}, {'name': 'values', 'type': 'integer'}], 'primaryKey': FrozenList(['level_0', 'level_1']), 'pandas_version': '1.4.0'} The default naming roughly follows these rules: For series, the object.name is used. If that’s none, then the name is values For DataFrames, the stringified version of the column name is used For Index (not MultiIndex), index.name is used, with a fallback to index if that is None. For MultiIndex, mi.names is used. If any level has no name, then level_<i> is used. read_json also accepts orient='table' as an argument. This allows for the preservation of metadata such as dtypes and index names in a round-trippable manner. In [306]: df = pd.DataFrame( .....: { .....: "foo": [1, 2, 3, 4], .....: "bar": ["a", "b", "c", "d"], .....: "baz": pd.date_range("2018-01-01", freq="d", periods=4), .....: "qux": pd.Categorical(["a", "b", "c", "c"]), .....: }, .....: index=pd.Index(range(4), name="idx"), .....: ) .....: In [307]: df Out[307]: foo bar baz qux idx 0 1 a 2018-01-01 a 1 2 b 2018-01-02 b 2 3 c 2018-01-03 c 3 4 d 2018-01-04 c In [308]: df.dtypes Out[308]: foo int64 bar object baz datetime64[ns] qux category dtype: object In [309]: df.to_json("test.json", orient="table") In [310]: new_df = pd.read_json("test.json", orient="table") In [311]: new_df Out[311]: foo bar baz qux idx 0 1 a 2018-01-01 a 1 2 b 2018-01-02 b 2 3 c 2018-01-03 c 3 4 d 2018-01-04 c In [312]: new_df.dtypes Out[312]: foo int64 bar object baz datetime64[ns] qux category dtype: object Please note that the literal string ‘index’ as the name of an Index is not round-trippable, nor are any names beginning with 'level_' within a MultiIndex. These are used by default in DataFrame.to_json() to indicate missing values and the subsequent read cannot distinguish the intent. In [313]: df.index.name = "index" In [314]: df.to_json("test.json", orient="table") In [315]: new_df = pd.read_json("test.json", orient="table") In [316]: print(new_df.index.name) None When using orient='table' along with user-defined ExtensionArray, the generated schema will contain an additional extDtype key in the respective fields element. This extra key is not standard but does enable JSON roundtrips for extension types (e.g. read_json(df.to_json(orient="table"), orient="table")). The extDtype key carries the name of the extension, if you have properly registered the ExtensionDtype, pandas will use said name to perform a lookup into the registry and re-convert the serialized data into your custom dtype. HTML# Reading HTML content# Warning We highly encourage you to read the HTML Table Parsing gotchas below regarding the issues surrounding the BeautifulSoup4/html5lib/lxml parsers. The top-level read_html() function can accept an HTML string/file/URL and will parse HTML tables into list of pandas DataFrames. Let’s look at a few examples. Note read_html returns a list of DataFrame objects, even if there is only a single table contained in the HTML content. Read a URL with no options: In [320]: "https://www.fdic.gov/resources/resolutions/bank-failures/failed-bank-list" In [321]: pd.read_html(url) Out[321]: [ Bank NameBank CityCity StateSt ... Acquiring InstitutionAI Closing DateClosing FundFund 0 Almena State Bank Almena KS ... Equity Bank October 23, 2020 10538 1 First City Bank of Florida Fort Walton Beach FL ... United Fidelity Bank, fsb October 16, 2020 10537 2 The First State Bank Barboursville WV ... MVB Bank, Inc. April 3, 2020 10536 3 Ericson State Bank Ericson NE ... Farmers and Merchants Bank February 14, 2020 10535 4 City National Bank of New Jersey Newark NJ ... Industrial Bank November 1, 2019 10534 .. ... ... ... ... ... ... ... 558 Superior Bank, FSB Hinsdale IL ... Superior Federal, FSB July 27, 2001 6004 559 Malta National Bank Malta OH ... North Valley Bank May 3, 2001 4648 560 First Alliance Bank & Trust Co. Manchester NH ... Southern New Hampshire Bank & Trust February 2, 2001 4647 561 National State Bank of Metropolis Metropolis IL ... Banterra Bank of Marion December 14, 2000 4646 562 Bank of Honolulu Honolulu HI ... Bank of the Orient October 13, 2000 4645 [563 rows x 7 columns]] Note The data from the above URL changes every Monday so the resulting data above may be slightly different. Read in the content of the file from the above URL and pass it to read_html as a string: In [317]: html_str = """ .....: <table> .....: <tr> .....: <th>A</th> .....: <th colspan="1">B</th> .....: <th rowspan="1">C</th> .....: </tr> .....: <tr> .....: <td>a</td> .....: <td>b</td> .....: <td>c</td> .....: </tr> .....: </table> .....: """ .....: In [318]: with open("tmp.html", "w") as f: .....: f.write(html_str) .....: In [319]: df = pd.read_html("tmp.html") In [320]: df[0] Out[320]: A B C 0 a b c You can even pass in an instance of StringIO if you so desire: In [321]: dfs = pd.read_html(StringIO(html_str)) In [322]: dfs[0] Out[322]: A B C 0 a b c Note The following examples are not run by the IPython evaluator due to the fact that having so many network-accessing functions slows down the documentation build. If you spot an error or an example that doesn’t run, please do not hesitate to report it over on pandas GitHub issues page. Read a URL and match a table that contains specific text: match = "Metcalf Bank" df_list = pd.read_html(url, match=match) Specify a header row (by default <th> or <td> elements located within a <thead> are used to form the column index, if multiple rows are contained within <thead> then a MultiIndex is created); if specified, the header row is taken from the data minus the parsed header elements (<th> elements). dfs = pd.read_html(url, header=0) Specify an index column: dfs = pd.read_html(url, index_col=0) Specify a number of rows to skip: dfs = pd.read_html(url, skiprows=0) Specify a number of rows to skip using a list (range works as well): dfs = pd.read_html(url, skiprows=range(2)) Specify an HTML attribute: dfs1 = pd.read_html(url, attrs={"id": "table"}) dfs2 = pd.read_html(url, attrs={"class": "sortable"}) print(np.array_equal(dfs1[0], dfs2[0])) # Should be True Specify values that should be converted to NaN: dfs = pd.read_html(url, na_values=["No Acquirer"]) Specify whether to keep the default set of NaN values: dfs = pd.read_html(url, keep_default_na=False) Specify converters for columns. This is useful for numerical text data that has leading zeros. By default columns that are numerical are cast to numeric types and the leading zeros are lost. To avoid this, we can convert these columns to strings. url_mcc = "https://en.wikipedia.org/wiki/Mobile_country_code" dfs = pd.read_html( url_mcc, match="Telekom Albania", header=0, converters={"MNC": str}, ) Use some combination of the above: dfs = pd.read_html(url, match="Metcalf Bank", index_col=0) Read in pandas to_html output (with some loss of floating point precision): df = pd.DataFrame(np.random.randn(2, 2)) s = df.to_html(float_format="{0:.40g}".format) dfin = pd.read_html(s, index_col=0) The lxml backend will raise an error on a failed parse if that is the only parser you provide. If you only have a single parser you can provide just a string, but it is considered good practice to pass a list with one string if, for example, the function expects a sequence of strings. You may use: dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor=["lxml"]) Or you could pass flavor='lxml' without a list: dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor="lxml") However, if you have bs4 and html5lib installed and pass None or ['lxml', 'bs4'] then the parse will most likely succeed. Note that as soon as a parse succeeds, the function will return. dfs = pd.read_html(url, "Metcalf Bank", index_col=0, flavor=["lxml", "bs4"]) Links can be extracted from cells along with the text using extract_links="all". In [323]: html_table = """ .....: <table> .....: <tr> .....: <th>GitHub</th> .....: </tr> .....: <tr> .....: <td><a href="https://github.com/pandas-dev/pandas">pandas</a></td> .....: </tr> .....: </table> .....: """ .....: In [324]: df = pd.read_html( .....: html_table, .....: extract_links="all" .....: )[0] .....: In [325]: df Out[325]: (GitHub, None) 0 (pandas, https://github.com/pandas-dev/pandas) In [326]: df[("GitHub", None)] Out[326]: 0 (pandas, https://github.com/pandas-dev/pandas) Name: (GitHub, None), dtype: object In [327]: df[("GitHub", None)].str[1] Out[327]: 0 https://github.com/pandas-dev/pandas Name: (GitHub, None), dtype: object New in version 1.5.0. Writing to HTML files# DataFrame objects have an instance method to_html which renders the contents of the DataFrame as an HTML table. The function arguments are as in the method to_string described above. Note Not all of the possible options for DataFrame.to_html are shown here for brevity’s sake. See to_html() for the full set of options. Note In an HTML-rendering supported environment like a Jupyter Notebook, display(HTML(...))` will render the raw HTML into the environment. In [328]: from IPython.display import display, HTML In [329]: df = pd.DataFrame(np.random.randn(2, 2)) In [330]: df Out[330]: 0 1 0 0.070319 1.773907 1 0.253908 0.414581 In [331]: html = df.to_html() In [332]: print(html) # raw html <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <th>1</th> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> In [333]: display(HTML(html)) <IPython.core.display.HTML object> The columns argument will limit the columns shown: In [334]: html = df.to_html(columns=[0]) In [335]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> </tr> <tr> <th>1</th> <td>0.253908</td> </tr> </tbody> </table> In [336]: display(HTML(html)) <IPython.core.display.HTML object> float_format takes a Python callable to control the precision of floating point values: In [337]: html = df.to_html(float_format="{0:.10f}".format) In [338]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.0703192665</td> <td>1.7739074228</td> </tr> <tr> <th>1</th> <td>0.2539083433</td> <td>0.4145805920</td> </tr> </tbody> </table> In [339]: display(HTML(html)) <IPython.core.display.HTML object> bold_rows will make the row labels bold by default, but you can turn that off: In [340]: html = df.to_html(bold_rows=False) In [341]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <td>1</td> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> In [342]: display(HTML(html)) <IPython.core.display.HTML object> The classes argument provides the ability to give the resulting HTML table CSS classes. Note that these classes are appended to the existing 'dataframe' class. In [343]: print(df.to_html(classes=["awesome_table_class", "even_more_awesome_class"])) <table border="1" class="dataframe awesome_table_class even_more_awesome_class"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>0.070319</td> <td>1.773907</td> </tr> <tr> <th>1</th> <td>0.253908</td> <td>0.414581</td> </tr> </tbody> </table> The render_links argument provides the ability to add hyperlinks to cells that contain URLs. In [344]: url_df = pd.DataFrame( .....: { .....: "name": ["Python", "pandas"], .....: "url": ["https://www.python.org/", "https://pandas.pydata.org"], .....: } .....: ) .....: In [345]: html = url_df.to_html(render_links=True) In [346]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>name</th> <th>url</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>Python</td> <td><a href="https://www.python.org/" target="_blank">https://www.python.org/</a></td> </tr> <tr> <th>1</th> <td>pandas</td> <td><a href="https://pandas.pydata.org" target="_blank">https://pandas.pydata.org</a></td> </tr> </tbody> </table> In [347]: display(HTML(html)) <IPython.core.display.HTML object> Finally, the escape argument allows you to control whether the “<”, “>” and “&” characters escaped in the resulting HTML (by default it is True). So to get the HTML without escaped characters pass escape=False In [348]: df = pd.DataFrame({"a": list("&<>"), "b": np.random.randn(3)}) Escaped: In [349]: html = df.to_html() In [350]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>a</th> <th>b</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>&amp;</td> <td>0.842321</td> </tr> <tr> <th>1</th> <td>&lt;</td> <td>0.211337</td> </tr> <tr> <th>2</th> <td>&gt;</td> <td>-1.055427</td> </tr> </tbody> </table> In [351]: display(HTML(html)) <IPython.core.display.HTML object> Not escaped: In [352]: html = df.to_html(escape=False) In [353]: print(html) <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>a</th> <th>b</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>&</td> <td>0.842321</td> </tr> <tr> <th>1</th> <td><</td> <td>0.211337</td> </tr> <tr> <th>2</th> <td>></td> <td>-1.055427</td> </tr> </tbody> </table> In [354]: display(HTML(html)) <IPython.core.display.HTML object> Note Some browsers may not show a difference in the rendering of the previous two HTML tables. HTML Table Parsing Gotchas# There are some versioning issues surrounding the libraries that are used to parse HTML tables in the top-level pandas io function read_html. Issues with lxml Benefits lxml is very fast. lxml requires Cython to install correctly. Drawbacks lxml does not make any guarantees about the results of its parse unless it is given strictly valid markup. In light of the above, we have chosen to allow you, the user, to use the lxml backend, but this backend will use html5lib if lxml fails to parse It is therefore highly recommended that you install both BeautifulSoup4 and html5lib, so that you will still get a valid result (provided everything else is valid) even if lxml fails. Issues with BeautifulSoup4 using lxml as a backend The above issues hold here as well since BeautifulSoup4 is essentially just a wrapper around a parser backend. Issues with BeautifulSoup4 using html5lib as a backend Benefits html5lib is far more lenient than lxml and consequently deals with real-life markup in a much saner way rather than just, e.g., dropping an element without notifying you. html5lib generates valid HTML5 markup from invalid markup automatically. This is extremely important for parsing HTML tables, since it guarantees a valid document. However, that does NOT mean that it is “correct”, since the process of fixing markup does not have a single definition. html5lib is pure Python and requires no additional build steps beyond its own installation. Drawbacks The biggest drawback to using html5lib is that it is slow as molasses. However consider the fact that many tables on the web are not big enough for the parsing algorithm runtime to matter. It is more likely that the bottleneck will be in the process of reading the raw text from the URL over the web, i.e., IO (input-output). For very large tables, this might not be true. LaTeX# New in version 1.3.0. Currently there are no methods to read from LaTeX, only output methods. Writing to LaTeX files# Note DataFrame and Styler objects currently have a to_latex method. We recommend using the Styler.to_latex() method over DataFrame.to_latex() due to the former’s greater flexibility with conditional styling, and the latter’s possible future deprecation. Review the documentation for Styler.to_latex, which gives examples of conditional styling and explains the operation of its keyword arguments. For simple application the following pattern is sufficient. In [355]: df = pd.DataFrame([[1, 2], [3, 4]], index=["a", "b"], columns=["c", "d"]) In [356]: print(df.style.to_latex()) \begin{tabular}{lrr} & c & d \\ a & 1 & 2 \\ b & 3 & 4 \\ \end{tabular} To format values before output, chain the Styler.format method. In [357]: print(df.style.format("€ {}").to_latex()) \begin{tabular}{lrr} & c & d \\ a & € 1 & € 2 \\ b & € 3 & € 4 \\ \end{tabular} XML# Reading XML# New in version 1.3.0. The top-level read_xml() function can accept an XML string/file/URL and will parse nodes and attributes into a pandas DataFrame. Note Since there is no standard XML structure where design types can vary in many ways, read_xml works best with flatter, shallow versions. If an XML document is deeply nested, use the stylesheet feature to transform XML into a flatter version. Let’s look at a few examples. Read an XML string: In [358]: xml = """<?xml version="1.0" encoding="UTF-8"?> .....: <bookstore> .....: <book category="cooking"> .....: <title lang="en">Everyday Italian</title> .....: <author>Giada De Laurentiis</author> .....: <year>2005</year> .....: <price>30.00</price> .....: </book> .....: <book category="children"> .....: <title lang="en">Harry Potter</title> .....: <author>J K. Rowling</author> .....: <year>2005</year> .....: <price>29.99</price> .....: </book> .....: <book category="web"> .....: <title lang="en">Learning XML</title> .....: <author>Erik T. Ray</author> .....: <year>2003</year> .....: <price>39.95</price> .....: </book> .....: </bookstore>""" .....: In [359]: df = pd.read_xml(xml) In [360]: df Out[360]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Read a URL with no options: In [361]: df = pd.read_xml("https://www.w3schools.com/xml/books.xml") In [362]: df Out[362]: category title author year price cover 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 None 1 children Harry Potter J K. Rowling 2005 29.99 None 2 web XQuery Kick Start Vaidyanathan Nagarajan 2003 49.99 None 3 web Learning XML Erik T. Ray 2003 39.95 paperback Read in the content of the “books.xml” file and pass it to read_xml as a string: In [363]: file_path = "books.xml" In [364]: with open(file_path, "w") as f: .....: f.write(xml) .....: In [365]: with open(file_path, "r") as f: .....: df = pd.read_xml(f.read()) .....: In [366]: df Out[366]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Read in the content of the “books.xml” as instance of StringIO or BytesIO and pass it to read_xml: In [367]: with open(file_path, "r") as f: .....: sio = StringIO(f.read()) .....: In [368]: df = pd.read_xml(sio) In [369]: df Out[369]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 In [370]: with open(file_path, "rb") as f: .....: bio = BytesIO(f.read()) .....: In [371]: df = pd.read_xml(bio) In [372]: df Out[372]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 2 web Learning XML Erik T. Ray 2003 39.95 Even read XML from AWS S3 buckets such as NIH NCBI PMC Article Datasets providing Biomedical and Life Science Jorurnals: In [373]: df = pd.read_xml( .....: "s3://pmc-oa-opendata/oa_comm/xml/all/PMC1236943.xml", .....: xpath=".//journal-meta", .....: ) .....: In [374]: df Out[374]: journal-id journal-title issn publisher 0 Cardiovasc Ultrasound Cardiovascular Ultrasound 1476-7120 NaN With lxml as default parser, you access the full-featured XML library that extends Python’s ElementTree API. One powerful tool is ability to query nodes selectively or conditionally with more expressive XPath: In [375]: df = pd.read_xml(file_path, xpath="//book[year=2005]") In [376]: df Out[376]: category title author year price 0 cooking Everyday Italian Giada De Laurentiis 2005 30.00 1 children Harry Potter J K. Rowling 2005 29.99 Specify only elements or only attributes to parse: In [377]: df = pd.read_xml(file_path, elems_only=True) In [378]: df Out[378]: title author year price 0 Everyday Italian Giada De Laurentiis 2005 30.00 1 Harry Potter J K. Rowling 2005 29.99 2 Learning XML Erik T. Ray 2003 39.95 In [379]: df = pd.read_xml(file_path, attrs_only=True) In [380]: df Out[380]: category 0 cooking 1 children 2 web XML documents can have namespaces with prefixes and default namespaces without prefixes both of which are denoted with a special attribute xmlns. In order to parse by node under a namespace context, xpath must reference a prefix. For example, below XML contains a namespace with prefix, doc, and URI at https://example.com. In order to parse doc:row nodes, namespaces must be used. In [381]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <doc:data xmlns:doc="https://example.com"> .....: <doc:row> .....: <doc:shape>square</doc:shape> .....: <doc:degrees>360</doc:degrees> .....: <doc:sides>4.0</doc:sides> .....: </doc:row> .....: <doc:row> .....: <doc:shape>circle</doc:shape> .....: <doc:degrees>360</doc:degrees> .....: <doc:sides/> .....: </doc:row> .....: <doc:row> .....: <doc:shape>triangle</doc:shape> .....: <doc:degrees>180</doc:degrees> .....: <doc:sides>3.0</doc:sides> .....: </doc:row> .....: </doc:data>""" .....: In [382]: df = pd.read_xml(xml, .....: xpath="//doc:row", .....: namespaces={"doc": "https://example.com"}) .....: In [383]: df Out[383]: shape degrees sides 0 square 360 4.0 1 circle 360 NaN 2 triangle 180 3.0 Similarly, an XML document can have a default namespace without prefix. Failing to assign a temporary prefix will return no nodes and raise a ValueError. But assigning any temporary name to correct URI allows parsing by nodes. In [384]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <data xmlns="https://example.com"> .....: <row> .....: <shape>square</shape> .....: <degrees>360</degrees> .....: <sides>4.0</sides> .....: </row> .....: <row> .....: <shape>circle</shape> .....: <degrees>360</degrees> .....: <sides/> .....: </row> .....: <row> .....: <shape>triangle</shape> .....: <degrees>180</degrees> .....: <sides>3.0</sides> .....: </row> .....: </data>""" .....: In [385]: df = pd.read_xml(xml, .....: xpath="//pandas:row", .....: namespaces={"pandas": "https://example.com"}) .....: In [386]: df Out[386]: shape degrees sides 0 square 360 4.0 1 circle 360 NaN 2 triangle 180 3.0 However, if XPath does not reference node names such as default, /*, then namespaces is not required. With lxml as parser, you can flatten nested XML documents with an XSLT script which also can be string/file/URL types. As background, XSLT is a special-purpose language written in a special XML file that can transform original XML documents into other XML, HTML, even text (CSV, JSON, etc.) using an XSLT processor. For example, consider this somewhat nested structure of Chicago “L” Rides where station and rides elements encapsulate data in their own sections. With below XSLT, lxml can transform original nested document into a flatter output (as shown below for demonstration) for easier parse into DataFrame: In [387]: xml = """<?xml version='1.0' encoding='utf-8'?> .....: <response> .....: <row> .....: <station id="40850" name="Library"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>864.2</avg_weekday_rides> .....: <avg_saturday_rides>534</avg_saturday_rides> .....: <avg_sunday_holiday_rides>417.2</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: <row> .....: <station id="41700" name="Washington/Wabash"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>2707.4</avg_weekday_rides> .....: <avg_saturday_rides>1909.8</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1438.6</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: <row> .....: <station id="40380" name="Clark/Lake"/> .....: <month>2020-09-01T00:00:00</month> .....: <rides> .....: <avg_weekday_rides>2949.6</avg_weekday_rides> .....: <avg_saturday_rides>1657</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1453.8</avg_sunday_holiday_rides> .....: </rides> .....: </row> .....: </response>""" .....: In [388]: xsl = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> .....: <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/> .....: <xsl:strip-space elements="*"/> .....: <xsl:template match="/response"> .....: <xsl:copy> .....: <xsl:apply-templates select="row"/> .....: </xsl:copy> .....: </xsl:template> .....: <xsl:template match="row"> .....: <xsl:copy> .....: <station_id><xsl:value-of select="station/@id"/></station_id> .....: <station_name><xsl:value-of select="station/@name"/></station_name> .....: <xsl:copy-of select="month|rides/*"/> .....: </xsl:copy> .....: </xsl:template> .....: </xsl:stylesheet>""" .....: In [389]: output = """<?xml version='1.0' encoding='utf-8'?> .....: <response> .....: <row> .....: <station_id>40850</station_id> .....: <station_name>Library</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>864.2</avg_weekday_rides> .....: <avg_saturday_rides>534</avg_saturday_rides> .....: <avg_sunday_holiday_rides>417.2</avg_sunday_holiday_rides> .....: </row> .....: <row> .....: <station_id>41700</station_id> .....: <station_name>Washington/Wabash</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>2707.4</avg_weekday_rides> .....: <avg_saturday_rides>1909.8</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1438.6</avg_sunday_holiday_rides> .....: </row> .....: <row> .....: <station_id>40380</station_id> .....: <station_name>Clark/Lake</station_name> .....: <month>2020-09-01T00:00:00</month> .....: <avg_weekday_rides>2949.6</avg_weekday_rides> .....: <avg_saturday_rides>1657</avg_saturday_rides> .....: <avg_sunday_holiday_rides>1453.8</avg_sunday_holiday_rides> .....: </row> .....: </response>""" .....: In [390]: df = pd.read_xml(xml, stylesheet=xsl) In [391]: df Out[391]: station_id station_name ... avg_saturday_rides avg_sunday_holiday_rides 0 40850 Library ... 534.0 417.2 1 41700 Washington/Wabash ... 1909.8 1438.6 2 40380 Clark/Lake ... 1657.0 1453.8 [3 rows x 6 columns] For very large XML files that can range in hundreds of megabytes to gigabytes, pandas.read_xml() supports parsing such sizeable files using lxml’s iterparse and etree’s iterparse which are memory-efficient methods to iterate through an XML tree and extract specific elements and attributes. without holding entire tree in memory. New in version 1.5.0. To use this feature, you must pass a physical XML file path into read_xml and use the iterparse argument. Files should not be compressed or point to online sources but stored on local disk. Also, iterparse should be a dictionary where the key is the repeating nodes in document (which become the rows) and the value is a list of any element or attribute that is a descendant (i.e., child, grandchild) of repeating node. Since XPath is not used in this method, descendants do not need to share same relationship with one another. Below shows example of reading in Wikipedia’s very large (12 GB+) latest article data dump. In [1]: df = pd.read_xml( ... "/path/to/downloaded/enwikisource-latest-pages-articles.xml", ... iterparse = {"page": ["title", "ns", "id"]} ... ) ... df Out[2]: title ns id 0 Gettysburg Address 0 21450 1 Main Page 0 42950 2 Declaration by United Nations 0 8435 3 Constitution of the United States of America 0 8435 4 Declaration of Independence (Israel) 0 17858 ... ... ... ... 3578760 Page:Black cat 1897 07 v2 n10.pdf/17 104 219649 3578761 Page:Black cat 1897 07 v2 n10.pdf/43 104 219649 3578762 Page:Black cat 1897 07 v2 n10.pdf/44 104 219649 3578763 The History of Tom Jones, a Foundling/Book IX 0 12084291 3578764 Page:Shakespeare of Stratford (1926) Yale.djvu/91 104 21450 [3578765 rows x 3 columns] Writing XML# New in version 1.3.0. DataFrame objects have an instance method to_xml which renders the contents of the DataFrame as an XML document. Note This method does not support special properties of XML including DTD, CData, XSD schemas, processing instructions, comments, and others. Only namespaces at the root level is supported. However, stylesheet allows design changes after initial output. Let’s look at a few examples. Write an XML without options: In [392]: geom_df = pd.DataFrame( .....: { .....: "shape": ["square", "circle", "triangle"], .....: "degrees": [360, 360, 180], .....: "sides": [4, np.nan, 3], .....: } .....: ) .....: In [393]: print(geom_df.to_xml()) <?xml version='1.0' encoding='utf-8'?> <data> <row> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </row> <row> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </row> <row> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Write an XML with new root and row name: In [394]: print(geom_df.to_xml(root_name="geometry", row_name="objects")) <?xml version='1.0' encoding='utf-8'?> <geometry> <objects> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </objects> <objects> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </objects> <objects> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </objects> </geometry> Write an attribute-centric XML: In [395]: print(geom_df.to_xml(attr_cols=geom_df.columns.tolist())) <?xml version='1.0' encoding='utf-8'?> <data> <row index="0" shape="square" degrees="360" sides="4.0"/> <row index="1" shape="circle" degrees="360"/> <row index="2" shape="triangle" degrees="180" sides="3.0"/> </data> Write a mix of elements and attributes: In [396]: print( .....: geom_df.to_xml( .....: index=False, .....: attr_cols=['shape'], .....: elem_cols=['degrees', 'sides']) .....: ) .....: <?xml version='1.0' encoding='utf-8'?> <data> <row shape="square"> <degrees>360</degrees> <sides>4.0</sides> </row> <row shape="circle"> <degrees>360</degrees> <sides/> </row> <row shape="triangle"> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Any DataFrames with hierarchical columns will be flattened for XML element names with levels delimited by underscores: In [397]: ext_geom_df = pd.DataFrame( .....: { .....: "type": ["polygon", "other", "polygon"], .....: "shape": ["square", "circle", "triangle"], .....: "degrees": [360, 360, 180], .....: "sides": [4, np.nan, 3], .....: } .....: ) .....: In [398]: pvt_df = ext_geom_df.pivot_table(index='shape', .....: columns='type', .....: values=['degrees', 'sides'], .....: aggfunc='sum') .....: In [399]: pvt_df Out[399]: degrees sides type other polygon other polygon shape circle 360.0 NaN 0.0 NaN square NaN 360.0 NaN 4.0 triangle NaN 180.0 NaN 3.0 In [400]: print(pvt_df.to_xml()) <?xml version='1.0' encoding='utf-8'?> <data> <row> <shape>circle</shape> <degrees_other>360.0</degrees_other> <degrees_polygon/> <sides_other>0.0</sides_other> <sides_polygon/> </row> <row> <shape>square</shape> <degrees_other/> <degrees_polygon>360.0</degrees_polygon> <sides_other/> <sides_polygon>4.0</sides_polygon> </row> <row> <shape>triangle</shape> <degrees_other/> <degrees_polygon>180.0</degrees_polygon> <sides_other/> <sides_polygon>3.0</sides_polygon> </row> </data> Write an XML with default namespace: In [401]: print(geom_df.to_xml(namespaces={"": "https://example.com"})) <?xml version='1.0' encoding='utf-8'?> <data xmlns="https://example.com"> <row> <index>0</index> <shape>square</shape> <degrees>360</degrees> <sides>4.0</sides> </row> <row> <index>1</index> <shape>circle</shape> <degrees>360</degrees> <sides/> </row> <row> <index>2</index> <shape>triangle</shape> <degrees>180</degrees> <sides>3.0</sides> </row> </data> Write an XML with namespace prefix: In [402]: print( .....: geom_df.to_xml(namespaces={"doc": "https://example.com"}, .....: prefix="doc") .....: ) .....: <?xml version='1.0' encoding='utf-8'?> <doc:data xmlns:doc="https://example.com"> <doc:row> <doc:index>0</doc:index> <doc:shape>square</doc:shape> <doc:degrees>360</doc:degrees> <doc:sides>4.0</doc:sides> </doc:row> <doc:row> <doc:index>1</doc:index> <doc:shape>circle</doc:shape> <doc:degrees>360</doc:degrees> <doc:sides/> </doc:row> <doc:row> <doc:index>2</doc:index> <doc:shape>triangle</doc:shape> <doc:degrees>180</doc:degrees> <doc:sides>3.0</doc:sides> </doc:row> </doc:data> Write an XML without declaration or pretty print: In [403]: print( .....: geom_df.to_xml(xml_declaration=False, .....: pretty_print=False) .....: ) .....: <data><row><index>0</index><shape>square</shape><degrees>360</degrees><sides>4.0</sides></row><row><index>1</index><shape>circle</shape><degrees>360</degrees><sides/></row><row><index>2</index><shape>triangle</shape><degrees>180</degrees><sides>3.0</sides></row></data> Write an XML and transform with stylesheet: In [404]: xsl = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> .....: <xsl:output method="xml" omit-xml-declaration="no" indent="yes"/> .....: <xsl:strip-space elements="*"/> .....: <xsl:template match="/data"> .....: <geometry> .....: <xsl:apply-templates select="row"/> .....: </geometry> .....: </xsl:template> .....: <xsl:template match="row"> .....: <object index="{index}"> .....: <xsl:if test="shape!='circle'"> .....: <xsl:attribute name="type">polygon</xsl:attribute> .....: </xsl:if> .....: <xsl:copy-of select="shape"/> .....: <property> .....: <xsl:copy-of select="degrees|sides"/> .....: </property> .....: </object> .....: </xsl:template> .....: </xsl:stylesheet>""" .....: In [405]: print(geom_df.to_xml(stylesheet=xsl)) <?xml version="1.0"?> <geometry> <object index="0" type="polygon"> <shape>square</shape> <property> <degrees>360</degrees> <sides>4.0</sides> </property> </object> <object index="1"> <shape>circle</shape> <property> <degrees>360</degrees> <sides/> </property> </object> <object index="2" type="polygon"> <shape>triangle</shape> <property> <degrees>180</degrees> <sides>3.0</sides> </property> </object> </geometry> XML Final Notes# All XML documents adhere to W3C specifications. Both etree and lxml parsers will fail to parse any markup document that is not well-formed or follows XML syntax rules. Do be aware HTML is not an XML document unless it follows XHTML specs. However, other popular markup types including KML, XAML, RSS, MusicML, MathML are compliant XML schemas. For above reason, if your application builds XML prior to pandas operations, use appropriate DOM libraries like etree and lxml to build the necessary document and not by string concatenation or regex adjustments. Always remember XML is a special text file with markup rules. With very large XML files (several hundred MBs to GBs), XPath and XSLT can become memory-intensive operations. Be sure to have enough available RAM for reading and writing to large XML files (roughly about 5 times the size of text). Because XSLT is a programming language, use it with caution since such scripts can pose a security risk in your environment and can run large or infinite recursive operations. Always test scripts on small fragments before full run. The etree parser supports all functionality of both read_xml and to_xml except for complex XPath and any XSLT. Though limited in features, etree is still a reliable and capable parser and tree builder. Its performance may trail lxml to a certain degree for larger files but relatively unnoticeable on small to medium size files. Excel files# The read_excel() method can read Excel 2007+ (.xlsx) files using the openpyxl Python module. Excel 2003 (.xls) files can be read using xlrd. Binary Excel (.xlsb) files can be read using pyxlsb. The to_excel() instance method is used for saving a DataFrame to Excel. Generally the semantics are similar to working with csv data. See the cookbook for some advanced strategies. Warning The xlwt package for writing old-style .xls excel files is no longer maintained. The xlrd package is now only for reading old-style .xls files. Before pandas 1.3.0, the default argument engine=None to read_excel() would result in using the xlrd engine in many cases, including new Excel 2007+ (.xlsx) files. pandas will now default to using the openpyxl engine. It is strongly encouraged to install openpyxl to read Excel 2007+ (.xlsx) files. Please do not report issues when using ``xlrd`` to read ``.xlsx`` files. This is no longer supported, switch to using openpyxl instead. Attempting to use the xlwt engine will raise a FutureWarning unless the option io.excel.xls.writer is set to "xlwt". While this option is now deprecated and will also raise a FutureWarning, it can be globally set and the warning suppressed. Users are recommended to write .xlsx files using the openpyxl engine instead. Reading Excel files# In the most basic use-case, read_excel takes a path to an Excel file, and the sheet_name indicating which sheet to parse. # Returns a DataFrame pd.read_excel("path_to_file.xls", sheet_name="Sheet1") ExcelFile class# To facilitate working with multiple sheets from the same file, the ExcelFile class can be used to wrap the file and can be passed into read_excel There will be a performance benefit for reading multiple sheets as the file is read into memory only once. xlsx = pd.ExcelFile("path_to_file.xls") df = pd.read_excel(xlsx, "Sheet1") The ExcelFile class can also be used as a context manager. with pd.ExcelFile("path_to_file.xls") as xls: df1 = pd.read_excel(xls, "Sheet1") df2 = pd.read_excel(xls, "Sheet2") The sheet_names property will generate a list of the sheet names in the file. The primary use-case for an ExcelFile is parsing multiple sheets with different parameters: data = {} # For when Sheet1's format differs from Sheet2 with pd.ExcelFile("path_to_file.xls") as xls: data["Sheet1"] = pd.read_excel(xls, "Sheet1", index_col=None, na_values=["NA"]) data["Sheet2"] = pd.read_excel(xls, "Sheet2", index_col=1) Note that if the same parsing parameters are used for all sheets, a list of sheet names can simply be passed to read_excel with no loss in performance. # using the ExcelFile class data = {} with pd.ExcelFile("path_to_file.xls") as xls: data["Sheet1"] = pd.read_excel(xls, "Sheet1", index_col=None, na_values=["NA"]) data["Sheet2"] = pd.read_excel(xls, "Sheet2", index_col=None, na_values=["NA"]) # equivalent using the read_excel function data = pd.read_excel( "path_to_file.xls", ["Sheet1", "Sheet2"], index_col=None, na_values=["NA"] ) ExcelFile can also be called with a xlrd.book.Book object as a parameter. This allows the user to control how the excel file is read. For example, sheets can be loaded on demand by calling xlrd.open_workbook() with on_demand=True. import xlrd xlrd_book = xlrd.open_workbook("path_to_file.xls", on_demand=True) with pd.ExcelFile(xlrd_book) as xls: df1 = pd.read_excel(xls, "Sheet1") df2 = pd.read_excel(xls, "Sheet2") Specifying sheets# Note The second argument is sheet_name, not to be confused with ExcelFile.sheet_names. Note An ExcelFile’s attribute sheet_names provides access to a list of sheets. The arguments sheet_name allows specifying the sheet or sheets to read. The default value for sheet_name is 0, indicating to read the first sheet Pass a string to refer to the name of a particular sheet in the workbook. Pass an integer to refer to the index of a sheet. Indices follow Python convention, beginning at 0. Pass a list of either strings or integers, to return a dictionary of specified sheets. Pass a None to return a dictionary of all available sheets. # Returns a DataFrame pd.read_excel("path_to_file.xls", "Sheet1", index_col=None, na_values=["NA"]) Using the sheet index: # Returns a DataFrame pd.read_excel("path_to_file.xls", 0, index_col=None, na_values=["NA"]) Using all default values: # Returns a DataFrame pd.read_excel("path_to_file.xls") Using None to get all sheets: # Returns a dictionary of DataFrames pd.read_excel("path_to_file.xls", sheet_name=None) Using a list to get multiple sheets: # Returns the 1st and 4th sheet, as a dictionary of DataFrames. pd.read_excel("path_to_file.xls", sheet_name=["Sheet1", 3]) read_excel can read more than one sheet, by setting sheet_name to either a list of sheet names, a list of sheet positions, or None to read all sheets. Sheets can be specified by sheet index or sheet name, using an integer or string, respectively. Reading a MultiIndex# read_excel can read a MultiIndex index, by passing a list of columns to index_col and a MultiIndex column by passing a list of rows to header. If either the index or columns have serialized level names those will be read in as well by specifying the rows/columns that make up the levels. For example, to read in a MultiIndex index without names: In [406]: df = pd.DataFrame( .....: {"a": [1, 2, 3, 4], "b": [5, 6, 7, 8]}, .....: index=pd.MultiIndex.from_product([["a", "b"], ["c", "d"]]), .....: ) .....: In [407]: df.to_excel("path_to_file.xlsx") In [408]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1]) In [409]: df Out[409]: a b a c 1 5 d 2 6 b c 3 7 d 4 8 If the index has level names, they will parsed as well, using the same parameters. In [410]: df.index = df.index.set_names(["lvl1", "lvl2"]) In [411]: df.to_excel("path_to_file.xlsx") In [412]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1]) In [413]: df Out[413]: a b lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 If the source file has both MultiIndex index and columns, lists specifying each should be passed to index_col and header: In [414]: df.columns = pd.MultiIndex.from_product([["a"], ["b", "d"]], names=["c1", "c2"]) In [415]: df.to_excel("path_to_file.xlsx") In [416]: df = pd.read_excel("path_to_file.xlsx", index_col=[0, 1], header=[0, 1]) In [417]: df Out[417]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 Missing values in columns specified in index_col will be forward filled to allow roundtripping with to_excel for merged_cells=True. To avoid forward filling the missing values use set_index after reading the data instead of index_col. Parsing specific columns# It is often the case that users will insert columns to do temporary computations in Excel and you may not want to read in those columns. read_excel takes a usecols keyword to allow you to specify a subset of columns to parse. Changed in version 1.0.0. Passing in an integer for usecols will no longer work. Please pass in a list of ints from 0 to usecols inclusive instead. You can specify a comma-delimited set of Excel columns and ranges as a string: pd.read_excel("path_to_file.xls", "Sheet1", usecols="A,C:E") If usecols is a list of integers, then it is assumed to be the file column indices to be parsed. pd.read_excel("path_to_file.xls", "Sheet1", usecols=[0, 2, 3]) Element order is ignored, so usecols=[0, 1] is the same as [1, 0]. If usecols is a list of strings, it is assumed that each string corresponds to a column name provided either by the user in names or inferred from the document header row(s). Those strings define which columns will be parsed: pd.read_excel("path_to_file.xls", "Sheet1", usecols=["foo", "bar"]) Element order is ignored, so usecols=['baz', 'joe'] is the same as ['joe', 'baz']. If usecols is callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True. pd.read_excel("path_to_file.xls", "Sheet1", usecols=lambda x: x.isalpha()) Parsing dates# Datetime-like values are normally automatically converted to the appropriate dtype when reading the excel file. But if you have a column of strings that look like dates (but are not actually formatted as dates in excel), you can use the parse_dates keyword to parse those strings to datetimes: pd.read_excel("path_to_file.xls", "Sheet1", parse_dates=["date_strings"]) Cell converters# It is possible to transform the contents of Excel cells via the converters option. For instance, to convert a column to boolean: pd.read_excel("path_to_file.xls", "Sheet1", converters={"MyBools": bool}) This options handles missing values and treats exceptions in the converters as missing data. Transformations are applied cell by cell rather than to the column as a whole, so the array dtype is not guaranteed. For instance, a column of integers with missing values cannot be transformed to an array with integer dtype, because NaN is strictly a float. You can manually mask missing data to recover integer dtype: def cfun(x): return int(x) if x else -1 pd.read_excel("path_to_file.xls", "Sheet1", converters={"MyInts": cfun}) Dtype specifications# As an alternative to converters, the type for an entire column can be specified using the dtype keyword, which takes a dictionary mapping column names to types. To interpret data with no type inference, use the type str or object. pd.read_excel("path_to_file.xls", dtype={"MyInts": "int64", "MyText": str}) Writing Excel files# Writing Excel files to disk# To write a DataFrame object to a sheet of an Excel file, you can use the to_excel instance method. The arguments are largely the same as to_csv described above, the first argument being the name of the excel file, and the optional second argument the name of the sheet to which the DataFrame should be written. For example: df.to_excel("path_to_file.xlsx", sheet_name="Sheet1") Files with a .xls extension will be written using xlwt and those with a .xlsx extension will be written using xlsxwriter (if available) or openpyxl. The DataFrame will be written in a way that tries to mimic the REPL output. The index_label will be placed in the second row instead of the first. You can place it in the first row by setting the merge_cells option in to_excel() to False: df.to_excel("path_to_file.xlsx", index_label="label", merge_cells=False) In order to write separate DataFrames to separate sheets in a single Excel file, one can pass an ExcelWriter. with pd.ExcelWriter("path_to_file.xlsx") as writer: df1.to_excel(writer, sheet_name="Sheet1") df2.to_excel(writer, sheet_name="Sheet2") Writing Excel files to memory# pandas supports writing Excel files to buffer-like objects such as StringIO or BytesIO using ExcelWriter. from io import BytesIO bio = BytesIO() # By setting the 'engine' in the ExcelWriter constructor. writer = pd.ExcelWriter(bio, engine="xlsxwriter") df.to_excel(writer, sheet_name="Sheet1") # Save the workbook writer.save() # Seek to the beginning and read to copy the workbook to a variable in memory bio.seek(0) workbook = bio.read() Note engine is optional but recommended. Setting the engine determines the version of workbook produced. Setting engine='xlrd' will produce an Excel 2003-format workbook (xls). Using either 'openpyxl' or 'xlsxwriter' will produce an Excel 2007-format workbook (xlsx). If omitted, an Excel 2007-formatted workbook is produced. Excel writer engines# Deprecated since version 1.2.0: As the xlwt package is no longer maintained, the xlwt engine will be removed from a future version of pandas. This is the only engine in pandas that supports writing to .xls files. pandas chooses an Excel writer via two methods: the engine keyword argument the filename extension (via the default specified in config options) By default, pandas uses the XlsxWriter for .xlsx, openpyxl for .xlsm, and xlwt for .xls files. If you have multiple engines installed, you can set the default engine through setting the config options io.excel.xlsx.writer and io.excel.xls.writer. pandas will fall back on openpyxl for .xlsx files if Xlsxwriter is not available. To specify which writer you want to use, you can pass an engine keyword argument to to_excel and to ExcelWriter. The built-in engines are: openpyxl: version 2.4 or higher is required xlsxwriter xlwt # By setting the 'engine' in the DataFrame 'to_excel()' methods. df.to_excel("path_to_file.xlsx", sheet_name="Sheet1", engine="xlsxwriter") # By setting the 'engine' in the ExcelWriter constructor. writer = pd.ExcelWriter("path_to_file.xlsx", engine="xlsxwriter") # Or via pandas configuration. from pandas import options # noqa: E402 options.io.excel.xlsx.writer = "xlsxwriter" df.to_excel("path_to_file.xlsx", sheet_name="Sheet1") Style and formatting# The look and feel of Excel worksheets created from pandas can be modified using the following parameters on the DataFrame’s to_excel method. float_format : Format string for floating point numbers (default None). freeze_panes : A tuple of two integers representing the bottommost row and rightmost column to freeze. Each of these parameters is one-based, so (1, 1) will freeze the first row and first column (default None). Using the Xlsxwriter engine provides many options for controlling the format of an Excel worksheet created with the to_excel method. Excellent examples can be found in the Xlsxwriter documentation here: https://xlsxwriter.readthedocs.io/working_with_pandas.html OpenDocument Spreadsheets# New in version 0.25. The read_excel() method can also read OpenDocument spreadsheets using the odfpy module. The semantics and features for reading OpenDocument spreadsheets match what can be done for Excel files using engine='odf'. # Returns a DataFrame pd.read_excel("path_to_file.ods", engine="odf") Note Currently pandas only supports reading OpenDocument spreadsheets. Writing is not implemented. Binary Excel (.xlsb) files# New in version 1.0.0. The read_excel() method can also read binary Excel files using the pyxlsb module. The semantics and features for reading binary Excel files mostly match what can be done for Excel files using engine='pyxlsb'. pyxlsb does not recognize datetime types in files and will return floats instead. # Returns a DataFrame pd.read_excel("path_to_file.xlsb", engine="pyxlsb") Note Currently pandas only supports reading binary Excel files. Writing is not implemented. Clipboard# A handy way to grab data is to use the read_clipboard() method, which takes the contents of the clipboard buffer and passes them to the read_csv method. For instance, you can copy the following text to the clipboard (CTRL-C on many operating systems): A B C x 1 4 p y 2 5 q z 3 6 r And then import the data directly to a DataFrame by calling: >>> clipdf = pd.read_clipboard() >>> clipdf A B C x 1 4 p y 2 5 q z 3 6 r The to_clipboard method can be used to write the contents of a DataFrame to the clipboard. Following which you can paste the clipboard contents into other applications (CTRL-V on many operating systems). Here we illustrate writing a DataFrame into clipboard and reading it back. >>> df = pd.DataFrame( ... {"A": [1, 2, 3], "B": [4, 5, 6], "C": ["p", "q", "r"]}, index=["x", "y", "z"] ... ) >>> df A B C x 1 4 p y 2 5 q z 3 6 r >>> df.to_clipboard() >>> pd.read_clipboard() A B C x 1 4 p y 2 5 q z 3 6 r We can see that we got the same content back, which we had earlier written to the clipboard. Note You may need to install xclip or xsel (with PyQt5, PyQt4 or qtpy) on Linux to use these methods. Pickling# All pandas objects are equipped with to_pickle methods which use Python’s cPickle module to save data structures to disk using the pickle format. In [418]: df Out[418]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 In [419]: df.to_pickle("foo.pkl") The read_pickle function in the pandas namespace can be used to load any pickled pandas object (or any other pickled object) from file: In [420]: pd.read_pickle("foo.pkl") Out[420]: c1 a c2 b d lvl1 lvl2 a c 1 5 d 2 6 b c 3 7 d 4 8 Warning Loading pickled data received from untrusted sources can be unsafe. See: https://docs.python.org/3/library/pickle.html Warning read_pickle() is only guaranteed backwards compatible back to pandas version 0.20.3 Compressed pickle files# read_pickle(), DataFrame.to_pickle() and Series.to_pickle() can read and write compressed pickle files. The compression types of gzip, bz2, xz, zstd are supported for reading and writing. The zip file format only supports reading and must contain only one data file to be read. The compression type can be an explicit parameter or be inferred from the file extension. If ‘infer’, then use gzip, bz2, zip, xz, zstd if filename ends in '.gz', '.bz2', '.zip', '.xz', or '.zst', respectively. The compression parameter can also be a dict in order to pass options to the compression protocol. It must have a 'method' key set to the name of the compression protocol, which must be one of {'zip', 'gzip', 'bz2', 'xz', 'zstd'}. All other key-value pairs are passed to the underlying compression library. In [421]: df = pd.DataFrame( .....: { .....: "A": np.random.randn(1000), .....: "B": "foo", .....: "C": pd.date_range("20130101", periods=1000, freq="s"), .....: } .....: ) .....: In [422]: df Out[422]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] Using an explicit compression type: In [423]: df.to_pickle("data.pkl.compress", compression="gzip") In [424]: rt = pd.read_pickle("data.pkl.compress", compression="gzip") In [425]: rt Out[425]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] Inferring compression type from the extension: In [426]: df.to_pickle("data.pkl.xz", compression="infer") In [427]: rt = pd.read_pickle("data.pkl.xz", compression="infer") In [428]: rt Out[428]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] The default is to ‘infer’: In [429]: df.to_pickle("data.pkl.gz") In [430]: rt = pd.read_pickle("data.pkl.gz") In [431]: rt Out[431]: A B C 0 -0.828876 foo 2013-01-01 00:00:00 1 -0.110383 foo 2013-01-01 00:00:01 2 2.357598 foo 2013-01-01 00:00:02 3 -1.620073 foo 2013-01-01 00:00:03 4 0.440903 foo 2013-01-01 00:00:04 .. ... ... ... 995 -1.177365 foo 2013-01-01 00:16:35 996 1.236988 foo 2013-01-01 00:16:36 997 0.743946 foo 2013-01-01 00:16:37 998 -0.533097 foo 2013-01-01 00:16:38 999 -0.140850 foo 2013-01-01 00:16:39 [1000 rows x 3 columns] In [432]: df["A"].to_pickle("s1.pkl.bz2") In [433]: rt = pd.read_pickle("s1.pkl.bz2") In [434]: rt Out[434]: 0 -0.828876 1 -0.110383 2 2.357598 3 -1.620073 4 0.440903 ... 995 -1.177365 996 1.236988 997 0.743946 998 -0.533097 999 -0.140850 Name: A, Length: 1000, dtype: float64 Passing options to the compression protocol in order to speed up compression: In [435]: df.to_pickle("data.pkl.gz", compression={"method": "gzip", "compresslevel": 1}) msgpack# pandas support for msgpack has been removed in version 1.0.0. It is recommended to use pickle instead. Alternatively, you can also the Arrow IPC serialization format for on-the-wire transmission of pandas objects. For documentation on pyarrow, see here. HDF5 (PyTables)# HDFStore is a dict-like object which reads and writes pandas using the high performance HDF5 format using the excellent PyTables library. See the cookbook for some advanced strategies Warning pandas uses PyTables for reading and writing HDF5 files, which allows serializing object-dtype data with pickle. Loading pickled data received from untrusted sources can be unsafe. See: https://docs.python.org/3/library/pickle.html for more. In [436]: store = pd.HDFStore("store.h5") In [437]: print(store) <class 'pandas.io.pytables.HDFStore'> File path: store.h5 Objects can be written to the file just like adding key-value pairs to a dict: In [438]: index = pd.date_range("1/1/2000", periods=8) In [439]: s = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"]) In [440]: df = pd.DataFrame(np.random.randn(8, 3), index=index, columns=["A", "B", "C"]) # store.put('s', s) is an equivalent method In [441]: store["s"] = s In [442]: store["df"] = df In [443]: store Out[443]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 In a current or later Python session, you can retrieve stored objects: # store.get('df') is an equivalent method In [444]: store["df"] Out[444]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 # dotted (attribute) access provides get as well In [445]: store.df Out[445]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Deletion of the object specified by the key: # store.remove('df') is an equivalent method In [446]: del store["df"] In [447]: store Out[447]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 Closing a Store and using a context manager: In [448]: store.close() In [449]: store Out[449]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 In [450]: store.is_open Out[450]: False # Working with, and automatically closing the store using a context manager In [451]: with pd.HDFStore("store.h5") as store: .....: store.keys() .....: Read/write API# HDFStore supports a top-level API using read_hdf for reading and to_hdf for writing, similar to how read_csv and to_csv work. In [452]: df_tl = pd.DataFrame({"A": list(range(5)), "B": list(range(5))}) In [453]: df_tl.to_hdf("store_tl.h5", "table", append=True) In [454]: pd.read_hdf("store_tl.h5", "table", where=["index>2"]) Out[454]: A B 3 3 3 4 4 4 HDFStore will by default not drop rows that are all missing. This behavior can be changed by setting dropna=True. In [455]: df_with_missing = pd.DataFrame( .....: { .....: "col1": [0, np.nan, 2], .....: "col2": [1, np.nan, np.nan], .....: } .....: ) .....: In [456]: df_with_missing Out[456]: col1 col2 0 0.0 1.0 1 NaN NaN 2 2.0 NaN In [457]: df_with_missing.to_hdf("file.h5", "df_with_missing", format="table", mode="w") In [458]: pd.read_hdf("file.h5", "df_with_missing") Out[458]: col1 col2 0 0.0 1.0 1 NaN NaN 2 2.0 NaN In [459]: df_with_missing.to_hdf( .....: "file.h5", "df_with_missing", format="table", mode="w", dropna=True .....: ) .....: In [460]: pd.read_hdf("file.h5", "df_with_missing") Out[460]: col1 col2 0 0.0 1.0 2 2.0 NaN Fixed format# The examples above show storing using put, which write the HDF5 to PyTables in a fixed array format, called the fixed format. These types of stores are not appendable once written (though you can simply remove them and rewrite). Nor are they queryable; they must be retrieved in their entirety. They also do not support dataframes with non-unique column names. The fixed format stores offer very fast writing and slightly faster reading than table stores. This format is specified by default when using put or to_hdf or by format='fixed' or format='f'. Warning A fixed format will raise a TypeError if you try to retrieve using a where: >>> pd.DataFrame(np.random.randn(10, 2)).to_hdf("test_fixed.h5", "df") >>> pd.read_hdf("test_fixed.h5", "df", where="index>5") TypeError: cannot pass a where specification when reading a fixed format. this store must be selected in its entirety Table format# HDFStore supports another PyTables format on disk, the table format. Conceptually a table is shaped very much like a DataFrame, with rows and columns. A table may be appended to in the same or other sessions. In addition, delete and query type operations are supported. This format is specified by format='table' or format='t' to append or put or to_hdf. This format can be set as an option as well pd.set_option('io.hdf.default_format','table') to enable put/append/to_hdf to by default store in the table format. In [461]: store = pd.HDFStore("store.h5") In [462]: df1 = df[0:4] In [463]: df2 = df[4:] # append data (creates a table automatically) In [464]: store.append("df", df1) In [465]: store.append("df", df2) In [466]: store Out[466]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # select the entire object In [467]: store.select("df") Out[467]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 # the type of stored data In [468]: store.root.df._v_attrs.pandas_type Out[468]: 'frame_table' Note You can also create a table by passing format='table' or format='t' to a put operation. Hierarchical keys# Keys to a store can be specified as a string. These can be in a hierarchical path-name like format (e.g. foo/bar/bah), which will generate a hierarchy of sub-stores (or Groups in PyTables parlance). Keys can be specified without the leading ‘/’ and are always absolute (e.g. ‘foo’ refers to ‘/foo’). Removal operations can remove everything in the sub-store and below, so be careful. In [469]: store.put("foo/bar/bah", df) In [470]: store.append("food/orange", df) In [471]: store.append("food/apple", df) In [472]: store Out[472]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # a list of keys are returned In [473]: store.keys() Out[473]: ['/df', '/food/apple', '/food/orange', '/foo/bar/bah'] # remove all nodes under this level In [474]: store.remove("food") In [475]: store Out[475]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 You can walk through the group hierarchy using the walk method which will yield a tuple for each group key along with the relative keys of its contents. In [476]: for (path, subgroups, subkeys) in store.walk(): .....: for subgroup in subgroups: .....: print("GROUP: {}/{}".format(path, subgroup)) .....: for subkey in subkeys: .....: key = "/".join([path, subkey]) .....: print("KEY: {}".format(key)) .....: print(store.get(key)) .....: GROUP: /foo KEY: /df A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 GROUP: /foo/bar KEY: /foo/bar/bah A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Warning Hierarchical keys cannot be retrieved as dotted (attribute) access as described above for items stored under the root node. In [8]: store.foo.bar.bah AttributeError: 'HDFStore' object has no attribute 'foo' # you can directly access the actual PyTables node but using the root node In [9]: store.root.foo.bar.bah Out[9]: /foo/bar/bah (Group) '' children := ['block0_items' (Array), 'block0_values' (Array), 'axis0' (Array), 'axis1' (Array)] Instead, use explicit string based keys: In [477]: store["foo/bar/bah"] Out[477]: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Storing types# Storing mixed types in a table# Storing mixed-dtype data is supported. Strings are stored as a fixed-width using the maximum size of the appended column. Subsequent attempts at appending longer strings will raise a ValueError. Passing min_itemsize={`values`: size} as a parameter to append will set a larger minimum for the string columns. Storing floats, strings, ints, bools, datetime64 are currently supported. For string columns, passing nan_rep = 'nan' to append will change the default nan representation on disk (which converts to/from np.nan), this defaults to nan. In [478]: df_mixed = pd.DataFrame( .....: { .....: "A": np.random.randn(8), .....: "B": np.random.randn(8), .....: "C": np.array(np.random.randn(8), dtype="float32"), .....: "string": "string", .....: "int": 1, .....: "bool": True, .....: "datetime64": pd.Timestamp("20010102"), .....: }, .....: index=list(range(8)), .....: ) .....: In [479]: df_mixed.loc[df_mixed.index[3:5], ["A", "B", "string", "datetime64"]] = np.nan In [480]: store.append("df_mixed", df_mixed, min_itemsize={"values": 50}) In [481]: df_mixed1 = store.select("df_mixed") In [482]: df_mixed1 Out[482]: A B C string int bool datetime64 0 1.778161 -0.898283 -0.263043 string 1 True 2001-01-02 1 -0.913867 -0.218499 -0.639244 string 1 True 2001-01-02 2 -0.030004 1.408028 -0.866305 string 1 True 2001-01-02 3 NaN NaN -0.225250 NaN 1 True NaT 4 NaN NaN -0.890978 NaN 1 True NaT 5 0.081323 0.520995 -0.553839 string 1 True 2001-01-02 6 -0.268494 0.620028 -2.762875 string 1 True 2001-01-02 7 0.168016 0.159416 -1.244763 string 1 True 2001-01-02 In [483]: df_mixed1.dtypes.value_counts() Out[483]: float64 2 float32 1 object 1 int64 1 bool 1 datetime64[ns] 1 dtype: int64 # we have provided a minimum string column size In [484]: store.root.df_mixed.table Out[484]: /df_mixed/table (Table(8,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(2,), dflt=0.0, pos=1), "values_block_1": Float32Col(shape=(1,), dflt=0.0, pos=2), "values_block_2": StringCol(itemsize=50, shape=(1,), dflt=b'', pos=3), "values_block_3": Int64Col(shape=(1,), dflt=0, pos=4), "values_block_4": BoolCol(shape=(1,), dflt=False, pos=5), "values_block_5": Int64Col(shape=(1,), dflt=0, pos=6)} byteorder := 'little' chunkshape := (689,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False} Storing MultiIndex DataFrames# Storing MultiIndex DataFrames as tables is very similar to storing/selecting from homogeneous index DataFrames. In [485]: index = pd.MultiIndex( .....: levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], .....: codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], .....: names=["foo", "bar"], .....: ) .....: In [486]: df_mi = pd.DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"]) In [487]: df_mi Out[487]: A B C foo bar foo one -1.280289 0.692545 -0.536722 two 1.005707 0.296917 0.139796 three -1.083889 0.811865 1.648435 bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 baz two 0.183573 0.145277 0.308146 three -1.043530 -0.708145 1.430905 qux one -0.850136 0.813949 1.508891 two -1.556154 0.187597 1.176488 three -1.246093 -0.002726 -0.444249 In [488]: store.append("df_mi", df_mi) In [489]: store.select("df_mi") Out[489]: A B C foo bar foo one -1.280289 0.692545 -0.536722 two 1.005707 0.296917 0.139796 three -1.083889 0.811865 1.648435 bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 baz two 0.183573 0.145277 0.308146 three -1.043530 -0.708145 1.430905 qux one -0.850136 0.813949 1.508891 two -1.556154 0.187597 1.176488 three -1.246093 -0.002726 -0.444249 # the levels are automatically included as data columns In [490]: store.select("df_mi", "foo=bar") Out[490]: A B C foo bar bar one -0.164377 -0.402227 1.618922 two -1.424723 -0.023232 0.948196 Note The index keyword is reserved and cannot be use as a level name. Querying# Querying a table# select and delete operations have an optional criterion that can be specified to select/delete only a subset of the data. This allows one to have a very large on-disk table and retrieve only a portion of the data. A query is specified using the Term class under the hood, as a boolean expression. index and columns are supported indexers of DataFrames. if data_columns are specified, these can be used as additional indexers. level name in a MultiIndex, with default name level_0, level_1, … if not provided. Valid comparison operators are: =, ==, !=, >, >=, <, <= Valid boolean expressions are combined with: | : or & : and ( and ) : for grouping These rules are similar to how boolean expressions are used in pandas for indexing. Note = will be automatically expanded to the comparison operator == ~ is the not operator, but can only be used in very limited circumstances If a list/tuple of expressions is passed they will be combined via & The following are valid expressions: 'index >= date' "columns = ['A', 'D']" "columns in ['A', 'D']" 'columns = A' 'columns == A' "~(columns = ['A', 'B'])" 'index > df.index[3] & string = "bar"' '(index > df.index[3] & index <= df.index[6]) | string = "bar"' "ts >= Timestamp('2012-02-01')" "major_axis>=20130101" The indexers are on the left-hand side of the sub-expression: columns, major_axis, ts The right-hand side of the sub-expression (after a comparison operator) can be: functions that will be evaluated, e.g. Timestamp('2012-02-01') strings, e.g. "bar" date-like, e.g. 20130101, or "20130101" lists, e.g. "['A', 'B']" variables that are defined in the local names space, e.g. date Note Passing a string to a query by interpolating it into the query expression is not recommended. Simply assign the string of interest to a variable and use that variable in an expression. For example, do this string = "HolyMoly'" store.select("df", "index == string") instead of this string = "HolyMoly'" store.select('df', f'index == {string}') The latter will not work and will raise a SyntaxError.Note that there’s a single quote followed by a double quote in the string variable. If you must interpolate, use the '%r' format specifier store.select("df", "index == %r" % string) which will quote string. Here are some examples: In [491]: dfq = pd.DataFrame( .....: np.random.randn(10, 4), .....: columns=list("ABCD"), .....: index=pd.date_range("20130101", periods=10), .....: ) .....: In [492]: store.append("dfq", dfq, format="table", data_columns=True) Use boolean expressions, with in-line function evaluation. In [493]: store.select("dfq", "index>pd.Timestamp('20130104') & columns=['A', 'B']") Out[493]: A B 2013-01-05 1.366810 1.073372 2013-01-06 2.119746 -2.628174 2013-01-07 0.337920 -0.634027 2013-01-08 1.053434 1.109090 2013-01-09 -0.772942 -0.269415 2013-01-10 0.048562 -0.285920 Use inline column reference. In [494]: store.select("dfq", where="A>0 or C>0") Out[494]: A B C D 2013-01-01 0.856838 1.491776 0.001283 0.701816 2013-01-02 -1.097917 0.102588 0.661740 0.443531 2013-01-03 0.559313 -0.459055 -1.222598 -0.455304 2013-01-05 1.366810 1.073372 -0.994957 0.755314 2013-01-06 2.119746 -2.628174 -0.089460 -0.133636 2013-01-07 0.337920 -0.634027 0.421107 0.604303 2013-01-08 1.053434 1.109090 -0.367891 -0.846206 2013-01-10 0.048562 -0.285920 1.334100 0.194462 The columns keyword can be supplied to select a list of columns to be returned, this is equivalent to passing a 'columns=list_of_columns_to_filter': In [495]: store.select("df", "columns=['A', 'B']") Out[495]: A B 2000-01-01 -0.398501 -0.677311 2000-01-02 -1.167564 -0.593353 2000-01-03 -0.131959 0.089012 2000-01-04 0.169405 -1.358046 2000-01-05 0.492195 0.076693 2000-01-06 -0.285283 -1.210529 2000-01-07 0.941577 -0.342447 2000-01-08 0.052607 2.093214 start and stop parameters can be specified to limit the total search space. These are in terms of the total number of rows in a table. Note select will raise a ValueError if the query expression has an unknown variable reference. Usually this means that you are trying to select on a column that is not a data_column. select will raise a SyntaxError if the query expression is not valid. Query timedelta64[ns]# You can store and query using the timedelta64[ns] type. Terms can be specified in the format: <float>(<unit>), where float may be signed (and fractional), and unit can be D,s,ms,us,ns for the timedelta. Here’s an example: In [496]: from datetime import timedelta In [497]: dftd = pd.DataFrame( .....: { .....: "A": pd.Timestamp("20130101"), .....: "B": [ .....: pd.Timestamp("20130101") + timedelta(days=i, seconds=10) .....: for i in range(10) .....: ], .....: } .....: ) .....: In [498]: dftd["C"] = dftd["A"] - dftd["B"] In [499]: dftd Out[499]: A B C 0 2013-01-01 2013-01-01 00:00:10 -1 days +23:59:50 1 2013-01-01 2013-01-02 00:00:10 -2 days +23:59:50 2 2013-01-01 2013-01-03 00:00:10 -3 days +23:59:50 3 2013-01-01 2013-01-04 00:00:10 -4 days +23:59:50 4 2013-01-01 2013-01-05 00:00:10 -5 days +23:59:50 5 2013-01-01 2013-01-06 00:00:10 -6 days +23:59:50 6 2013-01-01 2013-01-07 00:00:10 -7 days +23:59:50 7 2013-01-01 2013-01-08 00:00:10 -8 days +23:59:50 8 2013-01-01 2013-01-09 00:00:10 -9 days +23:59:50 9 2013-01-01 2013-01-10 00:00:10 -10 days +23:59:50 In [500]: store.append("dftd", dftd, data_columns=True) In [501]: store.select("dftd", "C<'-3.5D'") Out[501]: A B C 4 2013-01-01 2013-01-05 00:00:10 -5 days +23:59:50 5 2013-01-01 2013-01-06 00:00:10 -6 days +23:59:50 6 2013-01-01 2013-01-07 00:00:10 -7 days +23:59:50 7 2013-01-01 2013-01-08 00:00:10 -8 days +23:59:50 8 2013-01-01 2013-01-09 00:00:10 -9 days +23:59:50 9 2013-01-01 2013-01-10 00:00:10 -10 days +23:59:50 Query MultiIndex# Selecting from a MultiIndex can be achieved by using the name of the level. In [502]: df_mi.index.names Out[502]: FrozenList(['foo', 'bar']) In [503]: store.select("df_mi", "foo=baz and bar=two") Out[503]: A B C foo bar baz two 0.183573 0.145277 0.308146 If the MultiIndex levels names are None, the levels are automatically made available via the level_n keyword with n the level of the MultiIndex you want to select from. In [504]: index = pd.MultiIndex( .....: levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], .....: codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], .....: ) .....: In [505]: df_mi_2 = pd.DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"]) In [506]: df_mi_2 Out[506]: A B C foo one -0.646538 1.210676 -0.315409 two 1.528366 0.376542 0.174490 three 1.247943 -0.742283 0.710400 bar one 0.434128 -1.246384 1.139595 two 1.388668 -0.413554 -0.666287 baz two 0.010150 -0.163820 -0.115305 three 0.216467 0.633720 0.473945 qux one -0.155446 1.287082 0.320201 two -1.256989 0.874920 0.765944 three 0.025557 -0.729782 -0.127439 In [507]: store.append("df_mi_2", df_mi_2) # the levels are automatically included as data columns with keyword level_n In [508]: store.select("df_mi_2", "level_0=foo and level_1=two") Out[508]: A B C foo two 1.528366 0.376542 0.17449 Indexing# You can create/modify an index for a table with create_table_index after data is already in the table (after and append/put operation). Creating a table index is highly encouraged. This will speed your queries a great deal when you use a select with the indexed dimension as the where. Note Indexes are automagically created on the indexables and any data columns you specify. This behavior can be turned off by passing index=False to append. # we have automagically already created an index (in the first section) In [509]: i = store.root.df.table.cols.index.index In [510]: i.optlevel, i.kind Out[510]: (6, 'medium') # change an index by passing new parameters In [511]: store.create_table_index("df", optlevel=9, kind="full") In [512]: i = store.root.df.table.cols.index.index In [513]: i.optlevel, i.kind Out[513]: (9, 'full') Oftentimes when appending large amounts of data to a store, it is useful to turn off index creation for each append, then recreate at the end. In [514]: df_1 = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [515]: df_2 = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [516]: st = pd.HDFStore("appends.h5", mode="w") In [517]: st.append("df", df_1, data_columns=["B"], index=False) In [518]: st.append("df", df_2, data_columns=["B"], index=False) In [519]: st.get_storer("df").table Out[519]: /df/table (Table(20,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2)} byteorder := 'little' chunkshape := (2730,) Then create the index when finished appending. In [520]: st.create_table_index("df", columns=["B"], optlevel=9, kind="full") In [521]: st.get_storer("df").table Out[521]: /df/table (Table(20,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2)} byteorder := 'little' chunkshape := (2730,) autoindex := True colindexes := { "B": Index(9, fullshuffle, zlib(1)).is_csi=True} In [522]: st.close() See here for how to create a completely-sorted-index (CSI) on an existing store. Query via data columns# You can designate (and index) certain columns that you want to be able to perform queries (other than the indexable columns, which you can always query). For instance say you want to perform this common operation, on-disk, and return just the frame that matches this query. You can specify data_columns = True to force all columns to be data_columns. In [523]: df_dc = df.copy() In [524]: df_dc["string"] = "foo" In [525]: df_dc.loc[df_dc.index[4:6], "string"] = np.nan In [526]: df_dc.loc[df_dc.index[7:9], "string"] = "bar" In [527]: df_dc["string2"] = "cool" In [528]: df_dc.loc[df_dc.index[1:3], ["B", "C"]] = 1.0 In [529]: df_dc Out[529]: A B C string string2 2000-01-01 -0.398501 -0.677311 -0.874991 foo cool 2000-01-02 -1.167564 1.000000 1.000000 foo cool 2000-01-03 -0.131959 1.000000 1.000000 foo cool 2000-01-04 0.169405 -1.358046 -0.105563 foo cool 2000-01-05 0.492195 0.076693 0.213685 NaN cool 2000-01-06 -0.285283 -1.210529 -1.408386 NaN cool 2000-01-07 0.941577 -0.342447 0.222031 foo cool 2000-01-08 0.052607 2.093214 1.064908 bar cool # on-disk operations In [530]: store.append("df_dc", df_dc, data_columns=["B", "C", "string", "string2"]) In [531]: store.select("df_dc", where="B > 0") Out[531]: A B C string string2 2000-01-02 -1.167564 1.000000 1.000000 foo cool 2000-01-03 -0.131959 1.000000 1.000000 foo cool 2000-01-05 0.492195 0.076693 0.213685 NaN cool 2000-01-08 0.052607 2.093214 1.064908 bar cool # getting creative In [532]: store.select("df_dc", "B > 0 & C > 0 & string == foo") Out[532]: A B C string string2 2000-01-02 -1.167564 1.0 1.0 foo cool 2000-01-03 -0.131959 1.0 1.0 foo cool # this is in-memory version of this type of selection In [533]: df_dc[(df_dc.B > 0) & (df_dc.C > 0) & (df_dc.string == "foo")] Out[533]: A B C string string2 2000-01-02 -1.167564 1.0 1.0 foo cool 2000-01-03 -0.131959 1.0 1.0 foo cool # we have automagically created this index and the B/C/string/string2 # columns are stored separately as ``PyTables`` columns In [534]: store.root.df_dc.table Out[534]: /df_dc/table (Table(8,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1), "B": Float64Col(shape=(), dflt=0.0, pos=2), "C": Float64Col(shape=(), dflt=0.0, pos=3), "string": StringCol(itemsize=3, shape=(), dflt=b'', pos=4), "string2": StringCol(itemsize=4, shape=(), dflt=b'', pos=5)} byteorder := 'little' chunkshape := (1680,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False, "B": Index(6, mediumshuffle, zlib(1)).is_csi=False, "C": Index(6, mediumshuffle, zlib(1)).is_csi=False, "string": Index(6, mediumshuffle, zlib(1)).is_csi=False, "string2": Index(6, mediumshuffle, zlib(1)).is_csi=False} There is some performance degradation by making lots of columns into data columns, so it is up to the user to designate these. In addition, you cannot change data columns (nor indexables) after the first append/put operation (Of course you can simply read in the data and create a new table!). Iterator# You can pass iterator=True or chunksize=number_in_a_chunk to select and select_as_multiple to return an iterator on the results. The default is 50,000 rows returned in a chunk. In [535]: for df in store.select("df", chunksize=3): .....: print(df) .....: A B C 2000-01-01 -0.398501 -0.677311 -0.874991 2000-01-02 -1.167564 -0.593353 0.146262 2000-01-03 -0.131959 0.089012 0.667450 A B C 2000-01-04 0.169405 -1.358046 -0.105563 2000-01-05 0.492195 0.076693 0.213685 2000-01-06 -0.285283 -1.210529 -1.408386 A B C 2000-01-07 0.941577 -0.342447 0.222031 2000-01-08 0.052607 2.093214 1.064908 Note You can also use the iterator with read_hdf which will open, then automatically close the store when finished iterating. for df in pd.read_hdf("store.h5", "df", chunksize=3): print(df) Note, that the chunksize keyword applies to the source rows. So if you are doing a query, then the chunksize will subdivide the total rows in the table and the query applied, returning an iterator on potentially unequal sized chunks. Here is a recipe for generating a query and using it to create equal sized return chunks. In [536]: dfeq = pd.DataFrame({"number": np.arange(1, 11)}) In [537]: dfeq Out[537]: number 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 In [538]: store.append("dfeq", dfeq, data_columns=["number"]) In [539]: def chunks(l, n): .....: return [l[i: i + n] for i in range(0, len(l), n)] .....: In [540]: evens = [2, 4, 6, 8, 10] In [541]: coordinates = store.select_as_coordinates("dfeq", "number=evens") In [542]: for c in chunks(coordinates, 2): .....: print(store.select("dfeq", where=c)) .....: number 1 2 3 4 number 5 6 7 8 number 9 10 Advanced queries# Select a single column# To retrieve a single indexable or data column, use the method select_column. This will, for example, enable you to get the index very quickly. These return a Series of the result, indexed by the row number. These do not currently accept the where selector. In [543]: store.select_column("df_dc", "index") Out[543]: 0 2000-01-01 1 2000-01-02 2 2000-01-03 3 2000-01-04 4 2000-01-05 5 2000-01-06 6 2000-01-07 7 2000-01-08 Name: index, dtype: datetime64[ns] In [544]: store.select_column("df_dc", "string") Out[544]: 0 foo 1 foo 2 foo 3 foo 4 NaN 5 NaN 6 foo 7 bar Name: string, dtype: object Selecting coordinates# Sometimes you want to get the coordinates (a.k.a the index locations) of your query. This returns an Int64Index of the resulting locations. These coordinates can also be passed to subsequent where operations. In [545]: df_coord = pd.DataFrame( .....: np.random.randn(1000, 2), index=pd.date_range("20000101", periods=1000) .....: ) .....: In [546]: store.append("df_coord", df_coord) In [547]: c = store.select_as_coordinates("df_coord", "index > 20020101") In [548]: c Out[548]: Int64Index([732, 733, 734, 735, 736, 737, 738, 739, 740, 741, ... 990, 991, 992, 993, 994, 995, 996, 997, 998, 999], dtype='int64', length=268) In [549]: store.select("df_coord", where=c) Out[549]: 0 1 2002-01-02 0.009035 0.921784 2002-01-03 -1.476563 -1.376375 2002-01-04 1.266731 2.173681 2002-01-05 0.147621 0.616468 2002-01-06 0.008611 2.136001 ... ... ... 2002-09-22 0.781169 -0.791687 2002-09-23 -0.764810 -2.000933 2002-09-24 -0.345662 0.393915 2002-09-25 -0.116661 0.834638 2002-09-26 -1.341780 0.686366 [268 rows x 2 columns] Selecting using a where mask# Sometime your query can involve creating a list of rows to select. Usually this mask would be a resulting index from an indexing operation. This example selects the months of a datetimeindex which are 5. In [550]: df_mask = pd.DataFrame( .....: np.random.randn(1000, 2), index=pd.date_range("20000101", periods=1000) .....: ) .....: In [551]: store.append("df_mask", df_mask) In [552]: c = store.select_column("df_mask", "index") In [553]: where = c[pd.DatetimeIndex(c).month == 5].index In [554]: store.select("df_mask", where=where) Out[554]: 0 1 2000-05-01 -0.386742 -0.977433 2000-05-02 -0.228819 0.471671 2000-05-03 0.337307 1.840494 2000-05-04 0.050249 0.307149 2000-05-05 -0.802947 -0.946730 ... ... ... 2002-05-27 1.605281 1.741415 2002-05-28 -0.804450 -0.715040 2002-05-29 -0.874851 0.037178 2002-05-30 -0.161167 -1.294944 2002-05-31 -0.258463 -0.731969 [93 rows x 2 columns] Storer object# If you want to inspect the stored object, retrieve via get_storer. You could use this programmatically to say get the number of rows in an object. In [555]: store.get_storer("df_dc").nrows Out[555]: 8 Multiple table queries# The methods append_to_multiple and select_as_multiple can perform appending/selecting from multiple tables at once. The idea is to have one table (call it the selector table) that you index most/all of the columns, and perform your queries. The other table(s) are data tables with an index matching the selector table’s index. You can then perform a very fast query on the selector table, yet get lots of data back. This method is similar to having a very wide table, but enables more efficient queries. The append_to_multiple method splits a given single DataFrame into multiple tables according to d, a dictionary that maps the table names to a list of ‘columns’ you want in that table. If None is used in place of a list, that table will have the remaining unspecified columns of the given DataFrame. The argument selector defines which table is the selector table (which you can make queries from). The argument dropna will drop rows from the input DataFrame to ensure tables are synchronized. This means that if a row for one of the tables being written to is entirely np.NaN, that row will be dropped from all tables. If dropna is False, THE USER IS RESPONSIBLE FOR SYNCHRONIZING THE TABLES. Remember that entirely np.Nan rows are not written to the HDFStore, so if you choose to call dropna=False, some tables may have more rows than others, and therefore select_as_multiple may not work or it may return unexpected results. In [556]: df_mt = pd.DataFrame( .....: np.random.randn(8, 6), .....: index=pd.date_range("1/1/2000", periods=8), .....: columns=["A", "B", "C", "D", "E", "F"], .....: ) .....: In [557]: df_mt["foo"] = "bar" In [558]: df_mt.loc[df_mt.index[1], ("A", "B")] = np.nan # you can also create the tables individually In [559]: store.append_to_multiple( .....: {"df1_mt": ["A", "B"], "df2_mt": None}, df_mt, selector="df1_mt" .....: ) .....: In [560]: store Out[560]: <class 'pandas.io.pytables.HDFStore'> File path: store.h5 # individual tables were created In [561]: store.select("df1_mt") Out[561]: A B 2000-01-01 0.079529 -1.459471 2000-01-02 NaN NaN 2000-01-03 -0.423113 2.314361 2000-01-04 0.756744 -0.792372 2000-01-05 -0.184971 0.170852 2000-01-06 0.678830 0.633974 2000-01-07 0.034973 0.974369 2000-01-08 -2.110103 0.243062 In [562]: store.select("df2_mt") Out[562]: C D E F foo 2000-01-01 -0.596306 -0.910022 -1.057072 -0.864360 bar 2000-01-02 0.477849 0.283128 -2.045700 -0.338206 bar 2000-01-03 -0.033100 -0.965461 -0.001079 -0.351689 bar 2000-01-04 -0.513555 -1.484776 -0.796280 -0.182321 bar 2000-01-05 -0.872407 -1.751515 0.934334 0.938818 bar 2000-01-06 -1.398256 1.347142 -0.029520 0.082738 bar 2000-01-07 -0.755544 0.380786 -1.634116 1.293610 bar 2000-01-08 1.453064 0.500558 -0.574475 0.694324 bar # as a multiple In [563]: store.select_as_multiple( .....: ["df1_mt", "df2_mt"], .....: where=["A>0", "B>0"], .....: selector="df1_mt", .....: ) .....: Out[563]: A B C D E F foo 2000-01-06 0.678830 0.633974 -1.398256 1.347142 -0.029520 0.082738 bar 2000-01-07 0.034973 0.974369 -0.755544 0.380786 -1.634116 1.293610 bar Delete from a table# You can delete from a table selectively by specifying a where. In deleting rows, it is important to understand the PyTables deletes rows by erasing the rows, then moving the following data. Thus deleting can potentially be a very expensive operation depending on the orientation of your data. To get optimal performance, it’s worthwhile to have the dimension you are deleting be the first of the indexables. Data is ordered (on the disk) in terms of the indexables. Here’s a simple use case. You store panel-type data, with dates in the major_axis and ids in the minor_axis. The data is then interleaved like this: date_1 id_1 id_2 . id_n date_2 id_1 . id_n It should be clear that a delete operation on the major_axis will be fairly quick, as one chunk is removed, then the following data moved. On the other hand a delete operation on the minor_axis will be very expensive. In this case it would almost certainly be faster to rewrite the table using a where that selects all but the missing data. Warning Please note that HDF5 DOES NOT RECLAIM SPACE in the h5 files automatically. Thus, repeatedly deleting (or removing nodes) and adding again, WILL TEND TO INCREASE THE FILE SIZE. To repack and clean the file, use ptrepack. Notes & caveats# Compression# PyTables allows the stored data to be compressed. This applies to all kinds of stores, not just tables. Two parameters are used to control compression: complevel and complib. complevel specifies if and how hard data is to be compressed. complevel=0 and complevel=None disables compression and 0<complevel<10 enables compression. complib specifies which compression library to use. If nothing is specified the default library zlib is used. A compression library usually optimizes for either good compression rates or speed and the results will depend on the type of data. Which type of compression to choose depends on your specific needs and data. The list of supported compression libraries: zlib: The default compression library. A classic in terms of compression, achieves good compression rates but is somewhat slow. lzo: Fast compression and decompression. bzip2: Good compression rates. blosc: Fast compression and decompression. Support for alternative blosc compressors: blosc:blosclz This is the default compressor for blosc blosc:lz4: A compact, very popular and fast compressor. blosc:lz4hc: A tweaked version of LZ4, produces better compression ratios at the expense of speed. blosc:snappy: A popular compressor used in many places. blosc:zlib: A classic; somewhat slower than the previous ones, but achieving better compression ratios. blosc:zstd: An extremely well balanced codec; it provides the best compression ratios among the others above, and at reasonably fast speed. If complib is defined as something other than the listed libraries a ValueError exception is issued. Note If the library specified with the complib option is missing on your platform, compression defaults to zlib without further ado. Enable compression for all objects within the file: store_compressed = pd.HDFStore( "store_compressed.h5", complevel=9, complib="blosc:blosclz" ) Or on-the-fly compression (this only applies to tables) in stores where compression is not enabled: store.append("df", df, complib="zlib", complevel=5) ptrepack# PyTables offers better write performance when tables are compressed after they are written, as opposed to turning on compression at the very beginning. You can use the supplied PyTables utility ptrepack. In addition, ptrepack can change compression levels after the fact. ptrepack --chunkshape=auto --propindexes --complevel=9 --complib=blosc in.h5 out.h5 Furthermore ptrepack in.h5 out.h5 will repack the file to allow you to reuse previously deleted space. Alternatively, one can simply remove the file and write again, or use the copy method. Caveats# Warning HDFStore is not-threadsafe for writing. The underlying PyTables only supports concurrent reads (via threading or processes). If you need reading and writing at the same time, you need to serialize these operations in a single thread in a single process. You will corrupt your data otherwise. See the (GH2397) for more information. If you use locks to manage write access between multiple processes, you may want to use fsync() before releasing write locks. For convenience you can use store.flush(fsync=True) to do this for you. Once a table is created columns (DataFrame) are fixed; only exactly the same columns can be appended Be aware that timezones (e.g., pytz.timezone('US/Eastern')) are not necessarily equal across timezone versions. So if data is localized to a specific timezone in the HDFStore using one version of a timezone library and that data is updated with another version, the data will be converted to UTC since these timezones are not considered equal. Either use the same version of timezone library or use tz_convert with the updated timezone definition. Warning PyTables will show a NaturalNameWarning if a column name cannot be used as an attribute selector. Natural identifiers contain only letters, numbers, and underscores, and may not begin with a number. Other identifiers cannot be used in a where clause and are generally a bad idea. DataTypes# HDFStore will map an object dtype to the PyTables underlying dtype. This means the following types are known to work: Type Represents missing values floating : float64, float32, float16 np.nan integer : int64, int32, int8, uint64,uint32, uint8 boolean datetime64[ns] NaT timedelta64[ns] NaT categorical : see the section below object : strings np.nan unicode columns are not supported, and WILL FAIL. Categorical data# You can write data that contains category dtypes to a HDFStore. Queries work the same as if it was an object array. However, the category dtyped data is stored in a more efficient manner. In [564]: dfcat = pd.DataFrame( .....: {"A": pd.Series(list("aabbcdba")).astype("category"), "B": np.random.randn(8)} .....: ) .....: In [565]: dfcat Out[565]: A B 0 a -1.608059 1 a 0.851060 2 b -0.736931 3 b 0.003538 4 c -1.422611 5 d 2.060901 6 b 0.993899 7 a -1.371768 In [566]: dfcat.dtypes Out[566]: A category B float64 dtype: object In [567]: cstore = pd.HDFStore("cats.h5", mode="w") In [568]: cstore.append("dfcat", dfcat, format="table", data_columns=["A"]) In [569]: result = cstore.select("dfcat", where="A in ['b', 'c']") In [570]: result Out[570]: A B 2 b -0.736931 3 b 0.003538 4 c -1.422611 6 b 0.993899 In [571]: result.dtypes Out[571]: A category B float64 dtype: object String columns# min_itemsize The underlying implementation of HDFStore uses a fixed column width (itemsize) for string columns. A string column itemsize is calculated as the maximum of the length of data (for that column) that is passed to the HDFStore, in the first append. Subsequent appends, may introduce a string for a column larger than the column can hold, an Exception will be raised (otherwise you could have a silent truncation of these columns, leading to loss of information). In the future we may relax this and allow a user-specified truncation to occur. Pass min_itemsize on the first table creation to a-priori specify the minimum length of a particular string column. min_itemsize can be an integer, or a dict mapping a column name to an integer. You can pass values as a key to allow all indexables or data_columns to have this min_itemsize. Passing a min_itemsize dict will cause all passed columns to be created as data_columns automatically. Note If you are not passing any data_columns, then the min_itemsize will be the maximum of the length of any string passed In [572]: dfs = pd.DataFrame({"A": "foo", "B": "bar"}, index=list(range(5))) In [573]: dfs Out[573]: A B 0 foo bar 1 foo bar 2 foo bar 3 foo bar 4 foo bar # A and B have a size of 30 In [574]: store.append("dfs", dfs, min_itemsize=30) In [575]: store.get_storer("dfs").table Out[575]: /dfs/table (Table(5,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": StringCol(itemsize=30, shape=(2,), dflt=b'', pos=1)} byteorder := 'little' chunkshape := (963,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False} # A is created as a data_column with a size of 30 # B is size is calculated In [576]: store.append("dfs2", dfs, min_itemsize={"A": 30}) In [577]: store.get_storer("dfs2").table Out[577]: /dfs2/table (Table(5,)) '' description := { "index": Int64Col(shape=(), dflt=0, pos=0), "values_block_0": StringCol(itemsize=3, shape=(1,), dflt=b'', pos=1), "A": StringCol(itemsize=30, shape=(), dflt=b'', pos=2)} byteorder := 'little' chunkshape := (1598,) autoindex := True colindexes := { "index": Index(6, mediumshuffle, zlib(1)).is_csi=False, "A": Index(6, mediumshuffle, zlib(1)).is_csi=False} nan_rep String columns will serialize a np.nan (a missing value) with the nan_rep string representation. This defaults to the string value nan. You could inadvertently turn an actual nan value into a missing value. In [578]: dfss = pd.DataFrame({"A": ["foo", "bar", "nan"]}) In [579]: dfss Out[579]: A 0 foo 1 bar 2 nan In [580]: store.append("dfss", dfss) In [581]: store.select("dfss") Out[581]: A 0 foo 1 bar 2 NaN # here you need to specify a different nan rep In [582]: store.append("dfss2", dfss, nan_rep="_nan_") In [583]: store.select("dfss2") Out[583]: A 0 foo 1 bar 2 nan External compatibility# HDFStore writes table format objects in specific formats suitable for producing loss-less round trips to pandas objects. For external compatibility, HDFStore can read native PyTables format tables. It is possible to write an HDFStore object that can easily be imported into R using the rhdf5 library (Package website). Create a table format store like this: In [584]: df_for_r = pd.DataFrame( .....: { .....: "first": np.random.rand(100), .....: "second": np.random.rand(100), .....: "class": np.random.randint(0, 2, (100,)), .....: }, .....: index=range(100), .....: ) .....: In [585]: df_for_r.head() Out[585]: first second class 0 0.013480 0.504941 0 1 0.690984 0.898188 1 2 0.510113 0.618748 1 3 0.357698 0.004972 0 4 0.451658 0.012065 1 In [586]: store_export = pd.HDFStore("export.h5") In [587]: store_export.append("df_for_r", df_for_r, data_columns=df_dc.columns) In [588]: store_export Out[588]: <class 'pandas.io.pytables.HDFStore'> File path: export.h5 In R this file can be read into a data.frame object using the rhdf5 library. The following example function reads the corresponding column names and data values from the values and assembles them into a data.frame: # Load values and column names for all datasets from corresponding nodes and # insert them into one data.frame object. library(rhdf5) loadhdf5data <- function(h5File) { listing <- h5ls(h5File) # Find all data nodes, values are stored in *_values and corresponding column # titles in *_items data_nodes <- grep("_values", listing$name) name_nodes <- grep("_items", listing$name) data_paths = paste(listing$group[data_nodes], listing$name[data_nodes], sep = "/") name_paths = paste(listing$group[name_nodes], listing$name[name_nodes], sep = "/") columns = list() for (idx in seq(data_paths)) { # NOTE: matrices returned by h5read have to be transposed to obtain # required Fortran order! data <- data.frame(t(h5read(h5File, data_paths[idx]))) names <- t(h5read(h5File, name_paths[idx])) entry <- data.frame(data) colnames(entry) <- names columns <- append(columns, entry) } data <- data.frame(columns) return(data) } Now you can import the DataFrame into R: > data = loadhdf5data("transfer.hdf5") > head(data) first second class 1 0.4170220047 0.3266449 0 2 0.7203244934 0.5270581 0 3 0.0001143748 0.8859421 1 4 0.3023325726 0.3572698 1 5 0.1467558908 0.9085352 1 6 0.0923385948 0.6233601 1 Note The R function lists the entire HDF5 file’s contents and assembles the data.frame object from all matching nodes, so use this only as a starting point if you have stored multiple DataFrame objects to a single HDF5 file. Performance# tables format come with a writing performance penalty as compared to fixed stores. The benefit is the ability to append/delete and query (potentially very large amounts of data). Write times are generally longer as compared with regular stores. Query times can be quite fast, especially on an indexed axis. You can pass chunksize=<int> to append, specifying the write chunksize (default is 50000). This will significantly lower your memory usage on writing. You can pass expectedrows=<int> to the first append, to set the TOTAL number of rows that PyTables will expect. This will optimize read/write performance. Duplicate rows can be written to tables, but are filtered out in selection (with the last items being selected; thus a table is unique on major, minor pairs) A PerformanceWarning will be raised if you are attempting to store types that will be pickled by PyTables (rather than stored as endemic types). See Here for more information and some solutions. Feather# Feather provides binary columnar serialization for data frames. It is designed to make reading and writing data frames efficient, and to make sharing data across data analysis languages easy. Feather is designed to faithfully serialize and de-serialize DataFrames, supporting all of the pandas dtypes, including extension dtypes such as categorical and datetime with tz. Several caveats: The format will NOT write an Index, or MultiIndex for the DataFrame and will raise an error if a non-default one is provided. You can .reset_index() to store the index or .reset_index(drop=True) to ignore it. Duplicate column names and non-string columns names are not supported Actual Python objects in object dtype columns are not supported. These will raise a helpful error message on an attempt at serialization. See the Full Documentation. In [589]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(3, 6).astype("u1"), .....: "d": np.arange(4.0, 7.0, dtype="float64"), .....: "e": [True, False, True], .....: "f": pd.Categorical(list("abc")), .....: "g": pd.date_range("20130101", periods=3), .....: "h": pd.date_range("20130101", periods=3, tz="US/Eastern"), .....: "i": pd.date_range("20130101", periods=3, freq="ns"), .....: } .....: ) .....: In [590]: df Out[590]: a b c ... g h i 0 a 1 3 ... 2013-01-01 2013-01-01 00:00:00-05:00 2013-01-01 00:00:00.000000000 1 b 2 4 ... 2013-01-02 2013-01-02 00:00:00-05:00 2013-01-01 00:00:00.000000001 2 c 3 5 ... 2013-01-03 2013-01-03 00:00:00-05:00 2013-01-01 00:00:00.000000002 [3 rows x 9 columns] In [591]: df.dtypes Out[591]: a object b int64 c uint8 d float64 e bool f category g datetime64[ns] h datetime64[ns, US/Eastern] i datetime64[ns] dtype: object Write to a feather file. In [592]: df.to_feather("example.feather") Read from a feather file. In [593]: result = pd.read_feather("example.feather") In [594]: result Out[594]: a b c ... g h i 0 a 1 3 ... 2013-01-01 2013-01-01 00:00:00-05:00 2013-01-01 00:00:00.000000000 1 b 2 4 ... 2013-01-02 2013-01-02 00:00:00-05:00 2013-01-01 00:00:00.000000001 2 c 3 5 ... 2013-01-03 2013-01-03 00:00:00-05:00 2013-01-01 00:00:00.000000002 [3 rows x 9 columns] # we preserve dtypes In [595]: result.dtypes Out[595]: a object b int64 c uint8 d float64 e bool f category g datetime64[ns] h datetime64[ns, US/Eastern] i datetime64[ns] dtype: object Parquet# Apache Parquet provides a partitioned binary columnar serialization for data frames. It is designed to make reading and writing data frames efficient, and to make sharing data across data analysis languages easy. Parquet can use a variety of compression techniques to shrink the file size as much as possible while still maintaining good read performance. Parquet is designed to faithfully serialize and de-serialize DataFrame s, supporting all of the pandas dtypes, including extension dtypes such as datetime with tz. Several caveats. Duplicate column names and non-string columns names are not supported. The pyarrow engine always writes the index to the output, but fastparquet only writes non-default indexes. This extra column can cause problems for non-pandas consumers that are not expecting it. You can force including or omitting indexes with the index argument, regardless of the underlying engine. Index level names, if specified, must be strings. In the pyarrow engine, categorical dtypes for non-string types can be serialized to parquet, but will de-serialize as their primitive dtype. The pyarrow engine preserves the ordered flag of categorical dtypes with string types. fastparquet does not preserve the ordered flag. Non supported types include Interval and actual Python object types. These will raise a helpful error message on an attempt at serialization. Period type is supported with pyarrow >= 0.16.0. The pyarrow engine preserves extension data types such as the nullable integer and string data type (requiring pyarrow >= 0.16.0, and requiring the extension type to implement the needed protocols, see the extension types documentation). You can specify an engine to direct the serialization. This can be one of pyarrow, or fastparquet, or auto. If the engine is NOT specified, then the pd.options.io.parquet.engine option is checked; if this is also auto, then pyarrow is tried, and falling back to fastparquet. See the documentation for pyarrow and fastparquet. Note These engines are very similar and should read/write nearly identical parquet format files. pyarrow>=8.0.0 supports timedelta data, fastparquet>=0.1.4 supports timezone aware datetimes. These libraries differ by having different underlying dependencies (fastparquet by using numba, while pyarrow uses a c-library). In [596]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(3, 6).astype("u1"), .....: "d": np.arange(4.0, 7.0, dtype="float64"), .....: "e": [True, False, True], .....: "f": pd.date_range("20130101", periods=3), .....: "g": pd.date_range("20130101", periods=3, tz="US/Eastern"), .....: "h": pd.Categorical(list("abc")), .....: "i": pd.Categorical(list("abc"), ordered=True), .....: } .....: ) .....: In [597]: df Out[597]: a b c d e f g h i 0 a 1 3 4.0 True 2013-01-01 2013-01-01 00:00:00-05:00 a a 1 b 2 4 5.0 False 2013-01-02 2013-01-02 00:00:00-05:00 b b 2 c 3 5 6.0 True 2013-01-03 2013-01-03 00:00:00-05:00 c c In [598]: df.dtypes Out[598]: a object b int64 c uint8 d float64 e bool f datetime64[ns] g datetime64[ns, US/Eastern] h category i category dtype: object Write to a parquet file. In [599]: df.to_parquet("example_pa.parquet", engine="pyarrow") In [600]: df.to_parquet("example_fp.parquet", engine="fastparquet") Read from a parquet file. In [601]: result = pd.read_parquet("example_fp.parquet", engine="fastparquet") In [602]: result = pd.read_parquet("example_pa.parquet", engine="pyarrow") In [603]: result.dtypes Out[603]: a object b int64 c uint8 d float64 e bool f datetime64[ns] g datetime64[ns, US/Eastern] h category i category dtype: object Read only certain columns of a parquet file. In [604]: result = pd.read_parquet( .....: "example_fp.parquet", .....: engine="fastparquet", .....: columns=["a", "b"], .....: ) .....: In [605]: result = pd.read_parquet( .....: "example_pa.parquet", .....: engine="pyarrow", .....: columns=["a", "b"], .....: ) .....: In [606]: result.dtypes Out[606]: a object b int64 dtype: object Handling indexes# Serializing a DataFrame to parquet may include the implicit index as one or more columns in the output file. Thus, this code: In [607]: df = pd.DataFrame({"a": [1, 2], "b": [3, 4]}) In [608]: df.to_parquet("test.parquet", engine="pyarrow") creates a parquet file with three columns if you use pyarrow for serialization: a, b, and __index_level_0__. If you’re using fastparquet, the index may or may not be written to the file. This unexpected extra column causes some databases like Amazon Redshift to reject the file, because that column doesn’t exist in the target table. If you want to omit a dataframe’s indexes when writing, pass index=False to to_parquet(): In [609]: df.to_parquet("test.parquet", index=False) This creates a parquet file with just the two expected columns, a and b. If your DataFrame has a custom index, you won’t get it back when you load this file into a DataFrame. Passing index=True will always write the index, even if that’s not the underlying engine’s default behavior. Partitioning Parquet files# Parquet supports partitioning of data based on the values of one or more columns. In [610]: df = pd.DataFrame({"a": [0, 0, 1, 1], "b": [0, 1, 0, 1]}) In [611]: df.to_parquet(path="test", engine="pyarrow", partition_cols=["a"], compression=None) The path specifies the parent directory to which data will be saved. The partition_cols are the column names by which the dataset will be partitioned. Columns are partitioned in the order they are given. The partition splits are determined by the unique values in the partition columns. The above example creates a partitioned dataset that may look like: test ├── a=0 │ ├── 0bac803e32dc42ae83fddfd029cbdebc.parquet │ └── ... └── a=1 ├── e6ab24a4f45147b49b54a662f0c412a3.parquet └── ... ORC# New in version 1.0.0. Similar to the parquet format, the ORC Format is a binary columnar serialization for data frames. It is designed to make reading data frames efficient. pandas provides both the reader and the writer for the ORC format, read_orc() and to_orc(). This requires the pyarrow library. Warning It is highly recommended to install pyarrow using conda due to some issues occurred by pyarrow. to_orc() requires pyarrow>=7.0.0. read_orc() and to_orc() are not supported on Windows yet, you can find valid environments on install optional dependencies. For supported dtypes please refer to supported ORC features in Arrow. Currently timezones in datetime columns are not preserved when a dataframe is converted into ORC files. In [612]: df = pd.DataFrame( .....: { .....: "a": list("abc"), .....: "b": list(range(1, 4)), .....: "c": np.arange(4.0, 7.0, dtype="float64"), .....: "d": [True, False, True], .....: "e": pd.date_range("20130101", periods=3), .....: } .....: ) .....: In [613]: df Out[613]: a b c d e 0 a 1 4.0 True 2013-01-01 1 b 2 5.0 False 2013-01-02 2 c 3 6.0 True 2013-01-03 In [614]: df.dtypes Out[614]: a object b int64 c float64 d bool e datetime64[ns] dtype: object Write to an orc file. In [615]: df.to_orc("example_pa.orc", engine="pyarrow") Read from an orc file. In [616]: result = pd.read_orc("example_pa.orc") In [617]: result.dtypes Out[617]: a object b int64 c float64 d bool e datetime64[ns] dtype: object Read only certain columns of an orc file. In [618]: result = pd.read_orc( .....: "example_pa.orc", .....: columns=["a", "b"], .....: ) .....: In [619]: result.dtypes Out[619]: a object b int64 dtype: object SQL queries# The pandas.io.sql module provides a collection of query wrappers to both facilitate data retrieval and to reduce dependency on DB-specific API. Database abstraction is provided by SQLAlchemy if installed. In addition you will need a driver library for your database. Examples of such drivers are psycopg2 for PostgreSQL or pymysql for MySQL. For SQLite this is included in Python’s standard library by default. You can find an overview of supported drivers for each SQL dialect in the SQLAlchemy docs. If SQLAlchemy is not installed, a fallback is only provided for sqlite (and for mysql for backwards compatibility, but this is deprecated and will be removed in a future version). This mode requires a Python database adapter which respect the Python DB-API. See also some cookbook examples for some advanced strategies. The key functions are: read_sql_table(table_name, con[, schema, ...]) Read SQL database table into a DataFrame. read_sql_query(sql, con[, index_col, ...]) Read SQL query into a DataFrame. read_sql(sql, con[, index_col, ...]) Read SQL query or database table into a DataFrame. DataFrame.to_sql(name, con[, schema, ...]) Write records stored in a DataFrame to a SQL database. Note The function read_sql() is a convenience wrapper around read_sql_table() and read_sql_query() (and for backward compatibility) and will delegate to specific function depending on the provided input (database table name or sql query). Table names do not need to be quoted if they have special characters. In the following example, we use the SQlite SQL database engine. You can use a temporary SQLite database where data are stored in “memory”. To connect with SQLAlchemy you use the create_engine() function to create an engine object from database URI. You only need to create the engine once per database you are connecting to. For more information on create_engine() and the URI formatting, see the examples below and the SQLAlchemy documentation In [620]: from sqlalchemy import create_engine # Create your engine. In [621]: engine = create_engine("sqlite:///:memory:") If you want to manage your own connections you can pass one of those instead. The example below opens a connection to the database using a Python context manager that automatically closes the connection after the block has completed. See the SQLAlchemy docs for an explanation of how the database connection is handled. with engine.connect() as conn, conn.begin(): data = pd.read_sql_table("data", conn) Warning When you open a connection to a database you are also responsible for closing it. Side effects of leaving a connection open may include locking the database or other breaking behaviour. Writing DataFrames# Assuming the following data is in a DataFrame data, we can insert it into the database using to_sql(). id Date Col_1 Col_2 Col_3 26 2012-10-18 X 25.7 True 42 2012-10-19 Y -12.4 False 63 2012-10-20 Z 5.73 True In [622]: import datetime In [623]: c = ["id", "Date", "Col_1", "Col_2", "Col_3"] In [624]: d = [ .....: (26, datetime.datetime(2010, 10, 18), "X", 27.5, True), .....: (42, datetime.datetime(2010, 10, 19), "Y", -12.5, False), .....: (63, datetime.datetime(2010, 10, 20), "Z", 5.73, True), .....: ] .....: In [625]: data = pd.DataFrame(d, columns=c) In [626]: data Out[626]: id Date Col_1 Col_2 Col_3 0 26 2010-10-18 X 27.50 True 1 42 2010-10-19 Y -12.50 False 2 63 2010-10-20 Z 5.73 True In [627]: data.to_sql("data", engine) Out[627]: 3 With some databases, writing large DataFrames can result in errors due to packet size limitations being exceeded. This can be avoided by setting the chunksize parameter when calling to_sql. For example, the following writes data to the database in batches of 1000 rows at a time: In [628]: data.to_sql("data_chunked", engine, chunksize=1000) Out[628]: 3 SQL data types# to_sql() will try to map your data to an appropriate SQL data type based on the dtype of the data. When you have columns of dtype object, pandas will try to infer the data type. You can always override the default type by specifying the desired SQL type of any of the columns by using the dtype argument. This argument needs a dictionary mapping column names to SQLAlchemy types (or strings for the sqlite3 fallback mode). For example, specifying to use the sqlalchemy String type instead of the default Text type for string columns: In [629]: from sqlalchemy.types import String In [630]: data.to_sql("data_dtype", engine, dtype={"Col_1": String}) Out[630]: 3 Note Due to the limited support for timedelta’s in the different database flavors, columns with type timedelta64 will be written as integer values as nanoseconds to the database and a warning will be raised. Note Columns of category dtype will be converted to the dense representation as you would get with np.asarray(categorical) (e.g. for string categories this gives an array of strings). Because of this, reading the database table back in does not generate a categorical. Datetime data types# Using SQLAlchemy, to_sql() is capable of writing datetime data that is timezone naive or timezone aware. However, the resulting data stored in the database ultimately depends on the supported data type for datetime data of the database system being used. The following table lists supported data types for datetime data for some common databases. Other database dialects may have different data types for datetime data. Database SQL Datetime Types Timezone Support SQLite TEXT No MySQL TIMESTAMP or DATETIME No PostgreSQL TIMESTAMP or TIMESTAMP WITH TIME ZONE Yes When writing timezone aware data to databases that do not support timezones, the data will be written as timezone naive timestamps that are in local time with respect to the timezone. read_sql_table() is also capable of reading datetime data that is timezone aware or naive. When reading TIMESTAMP WITH TIME ZONE types, pandas will convert the data to UTC. Insertion method# The parameter method controls the SQL insertion clause used. Possible values are: None: Uses standard SQL INSERT clause (one per row). 'multi': Pass multiple values in a single INSERT clause. It uses a special SQL syntax not supported by all backends. This usually provides better performance for analytic databases like Presto and Redshift, but has worse performance for traditional SQL backend if the table contains many columns. For more information check the SQLAlchemy documentation. callable with signature (pd_table, conn, keys, data_iter): This can be used to implement a more performant insertion method based on specific backend dialect features. Example of a callable using PostgreSQL COPY clause: # Alternative to_sql() *method* for DBs that support COPY FROM import csv from io import StringIO def psql_insert_copy(table, conn, keys, data_iter): """ Execute SQL statement inserting data Parameters ---------- table : pandas.io.sql.SQLTable conn : sqlalchemy.engine.Engine or sqlalchemy.engine.Connection keys : list of str Column names data_iter : Iterable that iterates the values to be inserted """ # gets a DBAPI connection that can provide a cursor dbapi_conn = conn.connection with dbapi_conn.cursor() as cur: s_buf = StringIO() writer = csv.writer(s_buf) writer.writerows(data_iter) s_buf.seek(0) columns = ', '.join(['"{}"'.format(k) for k in keys]) if table.schema: table_name = '{}.{}'.format(table.schema, table.name) else: table_name = table.name sql = 'COPY {} ({}) FROM STDIN WITH CSV'.format( table_name, columns) cur.copy_expert(sql=sql, file=s_buf) Reading tables# read_sql_table() will read a database table given the table name and optionally a subset of columns to read. Note In order to use read_sql_table(), you must have the SQLAlchemy optional dependency installed. In [631]: pd.read_sql_table("data", engine) Out[631]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 X 27.50 True 1 1 42 2010-10-19 Y -12.50 False 2 2 63 2010-10-20 Z 5.73 True Note Note that pandas infers column dtypes from query outputs, and not by looking up data types in the physical database schema. For example, assume userid is an integer column in a table. Then, intuitively, select userid ... will return integer-valued series, while select cast(userid as text) ... will return object-valued (str) series. Accordingly, if the query output is empty, then all resulting columns will be returned as object-valued (since they are most general). If you foresee that your query will sometimes generate an empty result, you may want to explicitly typecast afterwards to ensure dtype integrity. You can also specify the name of the column as the DataFrame index, and specify a subset of columns to be read. In [632]: pd.read_sql_table("data", engine, index_col="id") Out[632]: index Date Col_1 Col_2 Col_3 id 26 0 2010-10-18 X 27.50 True 42 1 2010-10-19 Y -12.50 False 63 2 2010-10-20 Z 5.73 True In [633]: pd.read_sql_table("data", engine, columns=["Col_1", "Col_2"]) Out[633]: Col_1 Col_2 0 X 27.50 1 Y -12.50 2 Z 5.73 And you can explicitly force columns to be parsed as dates: In [634]: pd.read_sql_table("data", engine, parse_dates=["Date"]) Out[634]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 X 27.50 True 1 1 42 2010-10-19 Y -12.50 False 2 2 63 2010-10-20 Z 5.73 True If needed you can explicitly specify a format string, or a dict of arguments to pass to pandas.to_datetime(): pd.read_sql_table("data", engine, parse_dates={"Date": "%Y-%m-%d"}) pd.read_sql_table( "data", engine, parse_dates={"Date": {"format": "%Y-%m-%d %H:%M:%S"}}, ) You can check if a table exists using has_table() Schema support# Reading from and writing to different schema’s is supported through the schema keyword in the read_sql_table() and to_sql() functions. Note however that this depends on the database flavor (sqlite does not have schema’s). For example: df.to_sql("table", engine, schema="other_schema") pd.read_sql_table("table", engine, schema="other_schema") Querying# You can query using raw SQL in the read_sql_query() function. In this case you must use the SQL variant appropriate for your database. When using SQLAlchemy, you can also pass SQLAlchemy Expression language constructs, which are database-agnostic. In [635]: pd.read_sql_query("SELECT * FROM data", engine) Out[635]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 00:00:00.000000 X 27.50 1 1 1 42 2010-10-19 00:00:00.000000 Y -12.50 0 2 2 63 2010-10-20 00:00:00.000000 Z 5.73 1 Of course, you can specify a more “complex” query. In [636]: pd.read_sql_query("SELECT id, Col_1, Col_2 FROM data WHERE id = 42;", engine) Out[636]: id Col_1 Col_2 0 42 Y -12.5 The read_sql_query() function supports a chunksize argument. Specifying this will return an iterator through chunks of the query result: In [637]: df = pd.DataFrame(np.random.randn(20, 3), columns=list("abc")) In [638]: df.to_sql("data_chunks", engine, index=False) Out[638]: 20 In [639]: for chunk in pd.read_sql_query("SELECT * FROM data_chunks", engine, chunksize=5): .....: print(chunk) .....: a b c 0 0.070470 0.901320 0.937577 1 0.295770 1.420548 -0.005283 2 -1.518598 -0.730065 0.226497 3 -2.061465 0.632115 0.853619 4 2.719155 0.139018 0.214557 a b c 0 -1.538924 -0.366973 -0.748801 1 -0.478137 -1.559153 -3.097759 2 -2.320335 -0.221090 0.119763 3 0.608228 1.064810 -0.780506 4 -2.736887 0.143539 1.170191 a b c 0 -1.573076 0.075792 -1.722223 1 -0.774650 0.803627 0.221665 2 0.584637 0.147264 1.057825 3 -0.284136 0.912395 1.552808 4 0.189376 -0.109830 0.539341 a b c 0 0.592591 -0.155407 -1.356475 1 0.833837 1.524249 1.606722 2 -0.029487 -0.051359 1.700152 3 0.921484 -0.926347 0.979818 4 0.182380 -0.186376 0.049820 You can also run a plain query without creating a DataFrame with execute(). This is useful for queries that don’t return values, such as INSERT. This is functionally equivalent to calling execute on the SQLAlchemy engine or db connection object. Again, you must use the SQL syntax variant appropriate for your database. from pandas.io import sql sql.execute("SELECT * FROM table_name", engine) sql.execute( "INSERT INTO table_name VALUES(?, ?, ?)", engine, params=[("id", 1, 12.2, True)] ) Engine connection examples# To connect with SQLAlchemy you use the create_engine() function to create an engine object from database URI. You only need to create the engine once per database you are connecting to. from sqlalchemy import create_engine engine = create_engine("postgresql://scott:[email protected]:5432/mydatabase") engine = create_engine("mysql+mysqldb://scott:[email protected]/foo") engine = create_engine("oracle://scott:[email protected]:1521/sidname") engine = create_engine("mssql+pyodbc://mydsn") # sqlite://<nohostname>/<path> # where <path> is relative: engine = create_engine("sqlite:///foo.db") # or absolute, starting with a slash: engine = create_engine("sqlite:////absolute/path/to/foo.db") For more information see the examples the SQLAlchemy documentation Advanced SQLAlchemy queries# You can use SQLAlchemy constructs to describe your query. Use sqlalchemy.text() to specify query parameters in a backend-neutral way In [640]: import sqlalchemy as sa In [641]: pd.read_sql( .....: sa.text("SELECT * FROM data where Col_1=:col1"), engine, params={"col1": "X"} .....: ) .....: Out[641]: index id Date Col_1 Col_2 Col_3 0 0 26 2010-10-18 00:00:00.000000 X 27.5 1 If you have an SQLAlchemy description of your database you can express where conditions using SQLAlchemy expressions In [642]: metadata = sa.MetaData() In [643]: data_table = sa.Table( .....: "data", .....: metadata, .....: sa.Column("index", sa.Integer), .....: sa.Column("Date", sa.DateTime), .....: sa.Column("Col_1", sa.String), .....: sa.Column("Col_2", sa.Float), .....: sa.Column("Col_3", sa.Boolean), .....: ) .....: In [644]: pd.read_sql(sa.select([data_table]).where(data_table.c.Col_3 is True), engine) Out[644]: Empty DataFrame Columns: [index, Date, Col_1, Col_2, Col_3] Index: [] You can combine SQLAlchemy expressions with parameters passed to read_sql() using sqlalchemy.bindparam() In [645]: import datetime as dt In [646]: expr = sa.select([data_table]).where(data_table.c.Date > sa.bindparam("date")) In [647]: pd.read_sql(expr, engine, params={"date": dt.datetime(2010, 10, 18)}) Out[647]: index Date Col_1 Col_2 Col_3 0 1 2010-10-19 Y -12.50 False 1 2 2010-10-20 Z 5.73 True Sqlite fallback# The use of sqlite is supported without using SQLAlchemy. This mode requires a Python database adapter which respect the Python DB-API. You can create connections like so: import sqlite3 con = sqlite3.connect(":memory:") And then issue the following queries: data.to_sql("data", con) pd.read_sql_query("SELECT * FROM data", con) Google BigQuery# Warning Starting in 0.20.0, pandas has split off Google BigQuery support into the separate package pandas-gbq. You can pip install pandas-gbq to get it. The pandas-gbq package provides functionality to read/write from Google BigQuery. pandas integrates with this external package. if pandas-gbq is installed, you can use the pandas methods pd.read_gbq and DataFrame.to_gbq, which will call the respective functions from pandas-gbq. Full documentation can be found here. Stata format# Writing to stata format# The method to_stata() will write a DataFrame into a .dta file. The format version of this file is always 115 (Stata 12). In [648]: df = pd.DataFrame(np.random.randn(10, 2), columns=list("AB")) In [649]: df.to_stata("stata.dta") Stata data files have limited data type support; only strings with 244 or fewer characters, int8, int16, int32, float32 and float64 can be stored in .dta files. Additionally, Stata reserves certain values to represent missing data. Exporting a non-missing value that is outside of the permitted range in Stata for a particular data type will retype the variable to the next larger size. For example, int8 values are restricted to lie between -127 and 100 in Stata, and so variables with values above 100 will trigger a conversion to int16. nan values in floating points data types are stored as the basic missing data type (. in Stata). Note It is not possible to export missing data values for integer data types. The Stata writer gracefully handles other data types including int64, bool, uint8, uint16, uint32 by casting to the smallest supported type that can represent the data. For example, data with a type of uint8 will be cast to int8 if all values are less than 100 (the upper bound for non-missing int8 data in Stata), or, if values are outside of this range, the variable is cast to int16. Warning Conversion from int64 to float64 may result in a loss of precision if int64 values are larger than 2**53. Warning StataWriter and to_stata() only support fixed width strings containing up to 244 characters, a limitation imposed by the version 115 dta file format. Attempting to write Stata dta files with strings longer than 244 characters raises a ValueError. Reading from Stata format# The top-level function read_stata will read a dta file and return either a DataFrame or a StataReader that can be used to read the file incrementally. In [650]: pd.read_stata("stata.dta") Out[650]: index A B 0 0 -1.690072 0.405144 1 1 -1.511309 -1.531396 2 2 0.572698 -1.106845 3 3 -1.185859 0.174564 4 4 0.603797 -1.796129 5 5 -0.791679 1.173795 6 6 -0.277710 1.859988 7 7 -0.258413 1.251808 8 8 1.443262 0.441553 9 9 1.168163 -2.054946 Specifying a chunksize yields a StataReader instance that can be used to read chunksize lines from the file at a time. The StataReader object can be used as an iterator. In [651]: with pd.read_stata("stata.dta", chunksize=3) as reader: .....: for df in reader: .....: print(df.shape) .....: (3, 3) (3, 3) (3, 3) (1, 3) For more fine-grained control, use iterator=True and specify chunksize with each call to read(). In [652]: with pd.read_stata("stata.dta", iterator=True) as reader: .....: chunk1 = reader.read(5) .....: chunk2 = reader.read(5) .....: Currently the index is retrieved as a column. The parameter convert_categoricals indicates whether value labels should be read and used to create a Categorical variable from them. Value labels can also be retrieved by the function value_labels, which requires read() to be called before use. The parameter convert_missing indicates whether missing value representations in Stata should be preserved. If False (the default), missing values are represented as np.nan. If True, missing values are represented using StataMissingValue objects, and columns containing missing values will have object data type. Note read_stata() and StataReader support .dta formats 113-115 (Stata 10-12), 117 (Stata 13), and 118 (Stata 14). Note Setting preserve_dtypes=False will upcast to the standard pandas data types: int64 for all integer types and float64 for floating point data. By default, the Stata data types are preserved when importing. Categorical data# Categorical data can be exported to Stata data files as value labeled data. The exported data consists of the underlying category codes as integer data values and the categories as value labels. Stata does not have an explicit equivalent to a Categorical and information about whether the variable is ordered is lost when exporting. Warning Stata only supports string value labels, and so str is called on the categories when exporting data. Exporting Categorical variables with non-string categories produces a warning, and can result a loss of information if the str representations of the categories are not unique. Labeled data can similarly be imported from Stata data files as Categorical variables using the keyword argument convert_categoricals (True by default). The keyword argument order_categoricals (True by default) determines whether imported Categorical variables are ordered. Note When importing categorical data, the values of the variables in the Stata data file are not preserved since Categorical variables always use integer data types between -1 and n-1 where n is the number of categories. If the original values in the Stata data file are required, these can be imported by setting convert_categoricals=False, which will import original data (but not the variable labels). The original values can be matched to the imported categorical data since there is a simple mapping between the original Stata data values and the category codes of imported Categorical variables: missing values are assigned code -1, and the smallest original value is assigned 0, the second smallest is assigned 1 and so on until the largest original value is assigned the code n-1. Note Stata supports partially labeled series. These series have value labels for some but not all data values. Importing a partially labeled series will produce a Categorical with string categories for the values that are labeled and numeric categories for values with no label. SAS formats# The top-level function read_sas() can read (but not write) SAS XPORT (.xpt) and (since v0.18.0) SAS7BDAT (.sas7bdat) format files. SAS files only contain two value types: ASCII text and floating point values (usually 8 bytes but sometimes truncated). For xport files, there is no automatic type conversion to integers, dates, or categoricals. For SAS7BDAT files, the format codes may allow date variables to be automatically converted to dates. By default the whole file is read and returned as a DataFrame. Specify a chunksize or use iterator=True to obtain reader objects (XportReader or SAS7BDATReader) for incrementally reading the file. The reader objects also have attributes that contain additional information about the file and its variables. Read a SAS7BDAT file: df = pd.read_sas("sas_data.sas7bdat") Obtain an iterator and read an XPORT file 100,000 lines at a time: def do_something(chunk): pass with pd.read_sas("sas_xport.xpt", chunk=100000) as rdr: for chunk in rdr: do_something(chunk) The specification for the xport file format is available from the SAS web site. No official documentation is available for the SAS7BDAT format. SPSS formats# New in version 0.25.0. The top-level function read_spss() can read (but not write) SPSS SAV (.sav) and ZSAV (.zsav) format files. SPSS files contain column names. By default the whole file is read, categorical columns are converted into pd.Categorical, and a DataFrame with all columns is returned. Specify the usecols parameter to obtain a subset of columns. Specify convert_categoricals=False to avoid converting categorical columns into pd.Categorical. Read an SPSS file: df = pd.read_spss("spss_data.sav") Extract a subset of columns contained in usecols from an SPSS file and avoid converting categorical columns into pd.Categorical: df = pd.read_spss( "spss_data.sav", usecols=["foo", "bar"], convert_categoricals=False, ) More information about the SAV and ZSAV file formats is available here. Other file formats# pandas itself only supports IO with a limited set of file formats that map cleanly to its tabular data model. For reading and writing other file formats into and from pandas, we recommend these packages from the broader community. netCDF# xarray provides data structures inspired by the pandas DataFrame for working with multi-dimensional datasets, with a focus on the netCDF file format and easy conversion to and from pandas. Performance considerations# This is an informal comparison of various IO methods, using pandas 0.24.2. Timings are machine dependent and small differences should be ignored. In [1]: sz = 1000000 In [2]: df = pd.DataFrame({'A': np.random.randn(sz), 'B': [1] * sz}) In [3]: df.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 1000000 entries, 0 to 999999 Data columns (total 2 columns): A 1000000 non-null float64 B 1000000 non-null int64 dtypes: float64(1), int64(1) memory usage: 15.3 MB The following test functions will be used below to compare the performance of several IO methods: import numpy as np import os sz = 1000000 df = pd.DataFrame({"A": np.random.randn(sz), "B": [1] * sz}) sz = 1000000 np.random.seed(42) df = pd.DataFrame({"A": np.random.randn(sz), "B": [1] * sz}) def test_sql_write(df): if os.path.exists("test.sql"): os.remove("test.sql") sql_db = sqlite3.connect("test.sql") df.to_sql(name="test_table", con=sql_db) sql_db.close() def test_sql_read(): sql_db = sqlite3.connect("test.sql") pd.read_sql_query("select * from test_table", sql_db) sql_db.close() def test_hdf_fixed_write(df): df.to_hdf("test_fixed.hdf", "test", mode="w") def test_hdf_fixed_read(): pd.read_hdf("test_fixed.hdf", "test") def test_hdf_fixed_write_compress(df): df.to_hdf("test_fixed_compress.hdf", "test", mode="w", complib="blosc") def test_hdf_fixed_read_compress(): pd.read_hdf("test_fixed_compress.hdf", "test") def test_hdf_table_write(df): df.to_hdf("test_table.hdf", "test", mode="w", format="table") def test_hdf_table_read(): pd.read_hdf("test_table.hdf", "test") def test_hdf_table_write_compress(df): df.to_hdf( "test_table_compress.hdf", "test", mode="w", complib="blosc", format="table" ) def test_hdf_table_read_compress(): pd.read_hdf("test_table_compress.hdf", "test") def test_csv_write(df): df.to_csv("test.csv", mode="w") def test_csv_read(): pd.read_csv("test.csv", index_col=0) def test_feather_write(df): df.to_feather("test.feather") def test_feather_read(): pd.read_feather("test.feather") def test_pickle_write(df): df.to_pickle("test.pkl") def test_pickle_read(): pd.read_pickle("test.pkl") def test_pickle_write_compress(df): df.to_pickle("test.pkl.compress", compression="xz") def test_pickle_read_compress(): pd.read_pickle("test.pkl.compress", compression="xz") def test_parquet_write(df): df.to_parquet("test.parquet") def test_parquet_read(): pd.read_parquet("test.parquet") When writing, the top three functions in terms of speed are test_feather_write, test_hdf_fixed_write and test_hdf_fixed_write_compress. In [4]: %timeit test_sql_write(df) 3.29 s ± 43.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [5]: %timeit test_hdf_fixed_write(df) 19.4 ms ± 560 µs per loop (mean ± std. dev. of 7 runs, 1 loop each) In [6]: %timeit test_hdf_fixed_write_compress(df) 19.6 ms ± 308 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [7]: %timeit test_hdf_table_write(df) 449 ms ± 5.61 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [8]: %timeit test_hdf_table_write_compress(df) 448 ms ± 11.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [9]: %timeit test_csv_write(df) 3.66 s ± 26.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [10]: %timeit test_feather_write(df) 9.75 ms ± 117 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [11]: %timeit test_pickle_write(df) 30.1 ms ± 229 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [12]: %timeit test_pickle_write_compress(df) 4.29 s ± 15.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [13]: %timeit test_parquet_write(df) 67.6 ms ± 706 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) When reading, the top three functions in terms of speed are test_feather_read, test_pickle_read and test_hdf_fixed_read. In [14]: %timeit test_sql_read() 1.77 s ± 17.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [15]: %timeit test_hdf_fixed_read() 19.4 ms ± 436 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [16]: %timeit test_hdf_fixed_read_compress() 19.5 ms ± 222 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [17]: %timeit test_hdf_table_read() 38.6 ms ± 857 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [18]: %timeit test_hdf_table_read_compress() 38.8 ms ± 1.49 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) In [19]: %timeit test_csv_read() 452 ms ± 9.04 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [20]: %timeit test_feather_read() 12.4 ms ± 99.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [21]: %timeit test_pickle_read() 18.4 ms ± 191 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [22]: %timeit test_pickle_read_compress() 915 ms ± 7.48 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [23]: %timeit test_parquet_read() 24.4 ms ± 146 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) The files test.pkl.compress, test.parquet and test.feather took the least space on disk (in bytes). 29519500 Oct 10 06:45 test.csv 16000248 Oct 10 06:45 test.feather 8281983 Oct 10 06:49 test.parquet 16000857 Oct 10 06:47 test.pkl 7552144 Oct 10 06:48 test.pkl.compress 34816000 Oct 10 06:42 test.sql 24009288 Oct 10 06:43 test_fixed.hdf 24009288 Oct 10 06:43 test_fixed_compress.hdf 24458940 Oct 10 06:44 test_table.hdf 24458940 Oct 10 06:44 test_table_compress.hdf
64
311
How to split pandas record with one large timedelta into multiple records with smaller ones? I have a dataframe with 3 columns: timedeltas (duration) of time slot, datetime of slot start and datetime informing when record was created. Timedeltas are all multipliers of 15 minutes: Index duration slot_start creation_time 1. 15 minutes some datetime 1 some datetime 3 2. 45 minutes some datetime 2 some datetime 4 What I want to achieve is: Index duration slot_start creation_time 1. 15 minutes some datetime 1 some datetime 3 2. 15 minutes some datetime 2 some datetime 4 3. 15 minutes some datetime 2 + 15 minutes some datetime 4 4. 15 minutes some datetime 2 + 30 minutes some datetime 4 Is there any tool for such operation? How to achieve it easily and time efficiently on very large dataframes?
in seconds / [CLS] Python split pandas record minutes some datetime Index duration slot [SEP] avg _ saturday _ rides >..... : < avg _ sunday _ holiday _ rides > 1438. 6 < / avg _ sunday _ holiday _ rides >..... : < / rides >..... : < / row >..... : < row >..... : < station id = " 40380 " name = " Clark / Lake " / >..... : < month > 2020 - 09 - 01T00 : 00 : 00 < / month >..... : < rides >..... : < avg _ weekday _ rides > 2949. 6 /
Try this: unit = pd.Timedelta(minutes=15) s = pd.to_timedelta(df['duration']).div(unit) \ .apply(lambda n: unit * np.arange(n)) \ .rename('offset') \ .explode() df = df.join(s) df['slot_start'] = df['slot_start'] + df['offset']
62,803,668
Calculate medians of data-frame column based on referencing 2 other columns
<p>I have a dataframe <code>df</code>:</p> <p><code>a = [['A',1,2,3], ['A',0,4,5], ['A',0,6,7],['A',4,6,2],['B',7,3,7],['B',1,6,8],['B',6,2,6],['B',2,5,1],['C',4,2,3],['C',3,9,2],['C',2,7,1],['C',2,3,7]]</code></p> <p><code>df = pd.DataFrame(a, columns=['name','one', 'two', 'three'])</code></p> <p>that looks like:</p> <pre><code> name one two three 0 A 1 2 3 1 A 0 4 5 2 A 0 6 7 3 A 4 6 2 4 B 7 3 7 5 B 1 6 8 6 B 6 2 6 7 B 2 5 1 8 C 4 2 3 9 C 3 9 2 10 C 2 7 1 11 C 2 3 7 </code></pre> <p>How can I create a new column that is the medians of column <code>three</code> where the <code>name</code> values are the same? So my desired output would look like:</p> <pre><code> name one two three median 0 A 1 2 3 4.0 1 A 0 4 5 4.0 2 A 0 6 7 4.0 3 A 4 6 2 4.0 4 B 7 3 7 6.5 5 B 1 6 8 6.5 6 B 6 2 6 6.5 7 B 2 5 1 6.5 8 C 4 2 3 2.5 9 C 3 9 2 2.5 10 C 2 7 1 2.5 11 C 2 3 7 2.5 </code></pre> <p>so for example where <code>name</code> = <code>A</code> the median of 3,5,7 and 2 is calculated to equal 4.0 and entered into the median column where name is equal to <code>A</code>.</p>
62,803,688
"2020-07-08T21:08:32.860000"
1
null
0
102
python|pandas
<p>You can do <code>groupby().transform</code>:</p> <pre><code>df['median'] = df.groupby('name')['three'].transform('median') </code></pre> <p>Output:</p> <pre><code> name one two three median 0 A 1 2 3 4.0 1 A 0 4 5 4.0 2 A 0 6 7 4.0 3 A 4 6 2 4.0 4 B 7 3 7 6.5 5 B 1 6 8 6.5 6 B 6 2 6 6.5 7 B 2 5 1 6.5 8 C 4 2 3 2.5 9 C 3 9 2 2.5 10 C 2 7 1 2.5 11 C 2 3 7 2.5 </code></pre>
"2020-07-08T21:10:11.440000"
1
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.boxplot.html
pandas.DataFrame.boxplot# pandas.DataFrame.boxplot# DataFrame.boxplot(column=None, by=None, ax=None, fontsize=None, rot=0, grid=True, figsize=None, layout=None, return_type=None, backend=None, **kwargs)[source]# You can do groupby().transform: df['median'] = df.groupby('name')['three'].transform('median') Output: name one two three median 0 A 1 2 3 4.0 1 A 0 4 5 4.0 2 A 0 6 7 4.0 3 A 4 6 2 4.0 4 B 7 3 7 6.5 5 B 1 6 8 6.5 6 B 6 2 6 6.5 7 B 2 5 1 6.5 8 C 4 2 3 2.5 9 C 3 9 2 2.5 10 C 2 7 1 2.5 11 C 2 3 7 2.5 Make a box plot from DataFrame columns. Make a box-and-whisker plot from DataFrame columns, optionally grouped by some other columns. A box plot is a method for graphically depicting groups of numerical data through their quartiles. The box extends from the Q1 to Q3 quartile values of the data, with a line at the median (Q2). The whiskers extend from the edges of box to show the range of the data. By default, they extend no more than 1.5 * IQR (IQR = Q3 - Q1) from the edges of the box, ending at the farthest data point within that interval. Outliers are plotted as separate dots. For further details see Wikipedia’s entry for boxplot. Parameters columnstr or list of str, optionalColumn name or list of names, or vector. Can be any valid input to pandas.DataFrame.groupby(). bystr or array-like, optionalColumn in the DataFrame to pandas.DataFrame.groupby(). One box-plot will be done per value of columns in by. axobject of class matplotlib.axes.Axes, optionalThe matplotlib axes to be used by boxplot. fontsizefloat or strTick label font size in points or as a string (e.g., large). rotint or float, default 0The rotation angle of labels (in degrees) with respect to the screen coordinate system. gridbool, default TrueSetting this to True will show the grid. figsizeA tuple (width, height) in inchesThe size of the figure to create in matplotlib. layouttuple (rows, columns), optionalFor example, (3, 5) will display the subplots using 3 columns and 5 rows, starting from the top-left. return_type{‘axes’, ‘dict’, ‘both’} or None, default ‘axes’The kind of object to return. The default is axes. ‘axes’ returns the matplotlib axes the boxplot is drawn on. ‘dict’ returns a dictionary whose values are the matplotlib Lines of the boxplot. ‘both’ returns a namedtuple with the axes and dict. when grouping with by, a Series mapping columns to return_type is returned. If return_type is None, a NumPy array of axes with the same shape as layout is returned. backendstr, default NoneBackend to use instead of the backend specified in the option plotting.backend. For instance, ‘matplotlib’. Alternatively, to specify the plotting.backend for the whole session, set pd.options.plotting.backend. New in version 1.0.0. **kwargsAll other plotting keyword arguments to be passed to matplotlib.pyplot.boxplot(). Returns resultSee Notes. See also Series.plot.histMake a histogram. matplotlib.pyplot.boxplotMatplotlib equivalent plot. Notes The return type depends on the return_type parameter: ‘axes’ : object of class matplotlib.axes.Axes ‘dict’ : dict of matplotlib.lines.Line2D objects ‘both’ : a namedtuple with structure (ax, lines) For data grouped with by, return a Series of the above or a numpy array: Series array (for return_type = None) Use return_type='dict' when you want to tweak the appearance of the lines after plotting. In this case a dict containing the Lines making up the boxes, caps, fliers, medians, and whiskers is returned. Examples Boxplots can be created for every column in the dataframe by df.boxplot() or indicating the columns to be used: >>> np.random.seed(1234) >>> df = pd.DataFrame(np.random.randn(10, 4), ... columns=['Col1', 'Col2', 'Col3', 'Col4']) >>> boxplot = df.boxplot(column=['Col1', 'Col2', 'Col3']) Boxplots of variables distributions grouped by the values of a third variable can be created using the option by. For instance: >>> df = pd.DataFrame(np.random.randn(10, 2), ... columns=['Col1', 'Col2']) >>> df['X'] = pd.Series(['A', 'A', 'A', 'A', 'A', ... 'B', 'B', 'B', 'B', 'B']) >>> boxplot = df.boxplot(by='X') A list of strings (i.e. ['X', 'Y']) can be passed to boxplot in order to group the data by combination of the variables in the x-axis: >>> df = pd.DataFrame(np.random.randn(10, 3), ... columns=['Col1', 'Col2', 'Col3']) >>> df['X'] = pd.Series(['A', 'A', 'A', 'A', 'A', ... 'B', 'B', 'B', 'B', 'B']) >>> df['Y'] = pd.Series(['A', 'B', 'A', 'B', 'A', ... 'B', 'A', 'B', 'A', 'B']) >>> boxplot = df.boxplot(column=['Col1', 'Col2'], by=['X', 'Y']) The layout of boxplot can be adjusted giving a tuple to layout: >>> boxplot = df.boxplot(column=['Col1', 'Col2'], by='X', ... layout=(2, 1)) Additional formatting can be done to the boxplot, like suppressing the grid (grid=False), rotating the labels in the x-axis (i.e. rot=45) or changing the fontsize (i.e. fontsize=15): >>> boxplot = df.boxplot(grid=False, rot=45, fontsize=15) The parameter return_type can be used to select the type of element returned by boxplot. When return_type='axes' is selected, the matplotlib axes on which the boxplot is drawn are returned: >>> boxplot = df.boxplot(column=['Col1', 'Col2'], return_type='axes') >>> type(boxplot) <class 'matplotlib.axes._subplots.AxesSubplot'> When grouping with by, a Series mapping columns to return_type is returned: >>> boxplot = df.boxplot(column=['Col1', 'Col2'], by='X', ... return_type='axes') >>> type(boxplot) <class 'pandas.core.series.Series'> If return_type is None, a NumPy array of axes with the same shape as layout is returned: >>> boxplot = df.boxplot(column=['Col1', 'Col2'], by='X', ... return_type=None) >>> type(boxplot) <class 'numpy.ndarray'>
216
749
Calculate medians of data-frame column based on referencing 2 other columns I have a dataframe df: a = [['A',1,2,3], ['A',0,4,5], ['A',0,6,7],['A',4,6,2],['B',7,3,7],['B',1,6,8],['B',6,2,6],['B',2,5,1],['C',4,2,3],['C',3,9,2],['C',2,7,1],['C',2,3,7]] df = pd.DataFrame(a, columns=['name','one', 'two', 'three']) that looks like: name one two three 0 A 1 2 3 1 A 0 4 5 2 A 0 6 7 3 A 4 6 2 4 B 7 3 7 5 B 1 6 8 6 B 6 2 6 7 B 2 5 1 8 C 4 2 3 9 C 3 9 2 10 C 2 7 1 11 C 2 3 7 How can I create a new column that is the medians of column three where the name values are the same? So my desired output would look like: name one two three median 0 A 1 2 3 4.0 1 A 0 4 5 4.0 2 A 0 6 7 4.0 3 A 4 6 2 4.0 4 B 7 3 7 6.5 5 B 1 6 8 6.5 6 B 6 2 6 6.5 7 B 2 5 1 6.5 8 C 4 2 3 2.5 9 C 3 9 2 2.5 10 C 2 7 1 2.5 11 C 2 3 7 2.5 so for example where name = A the median of 3,5,7 and 2 is calculated to equal 4.0 and entered into the median column where name is equal to A.
transform / /
You can do groupby().transform: df['median'] = df.groupby('name')['three'].transform('median') Output: name one two three median 0 A 1 2 3 4.0 1 A 0 4 5 4.0 2 A 0 6 7 4.0 3 A 4 6 2 4.0 4 B 7 3 7 6.5 5 B 1 6 8 6.5 6 B 6 2 6 6.5 7 B 2 5 1 6.5 8 C 4 2 3 2.5 9 C 3 9 2 2.5 10 C 2 7 1 2.5 11 C 2 3 7 2.5
61,755,118
in python , how to calculate MAX and MIN for 3 or more dataframes
<p>How to calculate MAX and MIN for 3 or more dataframes<br> I can do calculate the difference between prices simply by adding this line of code</p> <pre><code>diff['list2-list1'] = diff['price2'] - diff['price1'] </code></pre> <p>but it not work to calculate MIN with </p> <pre><code>diff['min'] = (df1,df2.df3).min() </code></pre> <p>or </p> <pre><code>diff['min'] = (diff['price2'],diff['price1'],diff['price3']).min() </code></pre> <p>or </p> <pre><code>diff['min'] = (diff['price2'],diff['price1'],diff['price3']).idxmin() </code></pre> <p>and do not print result of <code>if</code> in new column when latest list (list3) have minimum value</p> <pre><code>if diff['min'] == diff['price3'] diff['Lowest now?'] = "yes" </code></pre> <p>The python code I have</p> <pre><code>import pandas import numpy as np import csv from csv_diff import load_csv, compare df1 = pandas.read_csv('list1.csv') df1['version'] = 'list1' df2 = pandas.read_csv('list2.csv') df2['version'] = 'list2' df3 = pandas.read_csv('list3.csv') df3['version'] = 'list3' # keep only columns 'version', 'ean', 'price' diff = df1.append([df2,df3])[['version', 'ean','price']] # keep only duplicated eans, which will only occur # for eans in both original lists diff = diff[diff['ean'].duplicated(keep=False)] # perform a pivot https://pandas.pydata.org/pandas-docs/stable/user_guide/reshaping.html diff = diff.pivot_table(index='ean', columns='version', values='price', aggfunc='first') # back to a normal dataframe diff = diff.reset_index() diff.columns.name = None # rename columns and keep only what we want diff = diff.rename(columns={'list1': 'price1', 'list2': 'price2', 'list3': 'price3'})[['ean', 'price1', 'price2','price3']] diff['list2-list1'] = diff['price2'] - diff['price1'] diff['list3-list2'] = diff['price3'] - diff['price2'] diff['min'] = (df1,df2).min() if diff['min'] == diff['price3'] diff['Lowest now?'] = "yes" diff.to_csv('diff.csv') </code></pre> <p><strong>more information</strong> <br/></p> <p>headers of list1,lsit2,list3 are the same</p> <pre><code>price,ean,unit </code></pre> <p>example of list1</p> <pre><code>price,ean,unit 143.80,2724316972629,0 125.00,2724456127521,0 158.00,2724280705919,0 19.99,2724342954019,0 20.00,2724321942662,0 212.00,2724559841560,0 1322.98,2724829673686 </code></pre> <p>example of list2</p> <pre><code>price,ean,unit 55.80,2724316972629,0 15.00,2724456127521,0 66.00,2724559841560,0 1622.98,2724829673686,0 </code></pre> <p>example of list3</p> <pre><code>price,ean,unit 139.99,2724342954019,0 240.00,2724321942662,0 252.00,2724559841560,0 1422.98,2724829673686,0 </code></pre>
61,756,926
"2020-05-12T15:02:49.847000"
2
null
0
143
python|pandas
<p>There you go:</p> <pre><code>data = pd.concat([df1, df2, df3], axis=1).fillna(0).astype('float') data['minimum_price'] = data['price'].min(1) data['maximum_price'] = data['price'].max(1) </code></pre> <p>Out:</p> <pre><code> price ean units price ean units price ean units minimum_price maximum_price 0 143.80 2.724317e+12 0.0 55.80 2.724317e+12 0.0 139.99 2.724343e+12 0.0 55.80 143.80 1 125.00 2.724456e+12 0.0 15.00 2.724456e+12 0.0 240.00 2.724322e+12 0.0 15.00 240.00 2 158.00 2.724281e+12 0.0 66.00 2.724560e+12 0.0 252.00 2.724560e+12 0.0 66.00 252.00 3 19.99 2.724343e+12 0.0 1622.98 2.724830e+12 0.0 1422.98 2.724830e+12 0.0 19.99 1622.98 4 20.00 2.724322e+12 0.0 0.00 0.000000e+00 0.0 0.00 0.000000e+00 0.0 0.00 20.00 5 212.00 2.724560e+12 0.0 0.00 0.000000e+00 0.0 0.00 0.000000e+00 0.0 0.00 212.00 6 1322.98 2.724830e+12 0.0 0.00 0.000000e+00 0.0 0.00 0.000000e+00 0.0 0.00 1322.98 </code></pre>
"2020-05-12T16:30:39.700000"
1
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.max.html
pandas.DataFrame.max# There you go: data = pd.concat([df1, df2, df3], axis=1).fillna(0).astype('float') data['minimum_price'] = data['price'].min(1) data['maximum_price'] = data['price'].max(1) Out: price ean units price ean units price ean units minimum_price maximum_price 0 143.80 2.724317e+12 0.0 55.80 2.724317e+12 0.0 139.99 2.724343e+12 0.0 55.80 143.80 1 125.00 2.724456e+12 0.0 15.00 2.724456e+12 0.0 240.00 2.724322e+12 0.0 15.00 240.00 2 158.00 2.724281e+12 0.0 66.00 2.724560e+12 0.0 252.00 2.724560e+12 0.0 66.00 252.00 3 19.99 2.724343e+12 0.0 1622.98 2.724830e+12 0.0 1422.98 2.724830e+12 0.0 19.99 1622.98 4 20.00 2.724322e+12 0.0 0.00 0.000000e+00 0.0 0.00 0.000000e+00 0.0 0.00 20.00 5 212.00 2.724560e+12 0.0 0.00 0.000000e+00 0.0 0.00 0.000000e+00 0.0 0.00 212.00 6 1322.98 2.724830e+12 0.0 0.00 0.000000e+00 0.0 0.00 0.000000e+00 0.0 0.00 1322.98 pandas.DataFrame.max# DataFrame.max(axis=_NoDefault.no_default, skipna=True, level=None, numeric_only=None, **kwargs)[source]# Return the maximum of the values over the requested axis. If you want the index of the maximum, use idxmax. This is the equivalent of the numpy.ndarray method argmax. Parameters axis{index (0), columns (1)}Axis for the function to be applied on. For Series this parameter is unused and defaults to 0. skipnabool, default TrueExclude NA/null values when computing the result. levelint or level name, default NoneIf the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series. Deprecated since version 1.3.0: The level keyword is deprecated. Use groupby instead. numeric_onlybool, default NoneInclude only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series. Deprecated since version 1.5.0: Specifying numeric_only=None is deprecated. The default value will be False in a future version of pandas. **kwargsAdditional keyword arguments to be passed to the function. Returns Series or DataFrame (if level specified) See also Series.sumReturn the sum. Series.minReturn the minimum. Series.maxReturn the maximum. Series.idxminReturn the index of the minimum. Series.idxmaxReturn the index of the maximum. DataFrame.sumReturn the sum over the requested axis. DataFrame.minReturn the minimum over the requested axis. DataFrame.maxReturn the maximum over the requested axis. DataFrame.idxminReturn the index of the minimum over the requested axis. DataFrame.idxmaxReturn the index of the maximum over the requested axis. Examples >>> idx = pd.MultiIndex.from_arrays([ ... ['warm', 'warm', 'cold', 'cold'], ... ['dog', 'falcon', 'fish', 'spider']], ... names=['blooded', 'animal']) >>> s = pd.Series([4, 2, 0, 8], name='legs', index=idx) >>> s blooded animal warm dog 4 falcon 2 cold fish 0 spider 8 Name: legs, dtype: int64 >>> s.max() 8
23
1,177
in python , how to calculate MAX and MIN for 3 or more dataframes How to calculate MAX and MIN for 3 or more dataframes I can do calculate the difference between prices simply by adding this line of code diff['list2-list1'] = diff['price2'] - diff['price1'] but it not work to calculate MIN with diff['min'] = (df1,df2.df3).min() or diff['min'] = (diff['price2'],diff['price1'],diff['price3']).min() or diff['min'] = (diff['price2'],diff['price1'],diff['price3']).idxmin() and do not print result of if in new column when latest list (list3) have minimum value if diff['min'] == diff['price3'] diff['Lowest now?'] = "yes" The python code I have import pandas import numpy as np import csv from csv_diff import load_csv, compare df1 = pandas.read_csv('list1.csv') df1['version'] = 'list1' df2 = pandas.read_csv('list2.csv') df2['version'] = 'list2' df3 = pandas.read_csv('list3.csv') df3['version'] = 'list3' # keep only columns 'version', 'ean', 'price' diff = df1.append([df2,df3])[['version', 'ean','price']] # keep only duplicated eans, which will only occur # for eans in both original lists diff = diff[diff['ean'].duplicated(keep=False)] # perform a pivot https://pandas.pydata.org/pandas-docs/stable/user_guide/reshaping.html diff = diff.pivot_table(index='ean', columns='version', values='price', aggfunc='first') # back to a normal dataframe diff = diff.reset_index() diff.columns.name = None # rename columns and keep only what we want diff = diff.rename(columns={'list1': 'price1', 'list2': 'price2', 'list3': 'price3'})[['ean', 'price1', 'price2','price3']] diff['list2-list1'] = diff['price2'] - diff['price1'] diff['list3-list2'] = diff['price3'] - diff['price2'] diff['min'] = (df1,df2).min() if diff['min'] == diff['price3'] diff['Lowest now?'] = "yes" diff.to_csv('diff.csv') more information headers of list1,lsit2,list3 are the same price,ean,unit example of list1 price,ean,unit 143.80,2724316972629,0 125.00,2724456127521,0 158.00,2724280705919,0 19.99,2724342954019,0 20.00,2724321942662,0 212.00,2724559841560,0 1322.98,2724829673686 example of list2 price,ean,unit 55.80,2724316972629,0 15.00,2724456127521,0 66.00,2724559841560,0 1622.98,2724829673686,0 example of list3 price,ean,unit 139.99,2724342954019,0 240.00,2724321942662,0 252.00,2724559841560,0 1422.98,2724829673686,0
There you go: data = pd.concat([df1, df2, df3], axis=1).fillna(0).astype('float') data['minimum_price'] = data['price'].min(1) data['maximum_price'] = data['price'].max(1) Out: price ean units price ean units price ean units minimum_price maximum_price 0 143.80 2.724317e+12 0.0 55.80 2.724317e+12 0.0 139.99 2.724343e+12 0.0 55.80 143.80 1 125.00 2.724456e+12 0.0 15.00 2.724456e+12 0.0 240.00 2.724322e+12 0.0 15.00 240.00 2 158.00 2.724281e+12 0.0 66.00 2.724560e+12 0.0 252.00 2.724560e+12 0.0 66.00 252.00 3 19.99 2.724343e+12 0.0 1622.98 2.724830e+12 0.0 1422.98 2.724830e+12 0.0 19.99 1622.98 4 20.00 2.724322e+12 0.0 0.00 0.000000e+00 0.0 0.00 0.000000e+00 0.0 0.00 20.00 5 212.00 2.724560e+12 0.0 0.00 0.000000e+00 0.0 0.00 0.000000e+00 0.0 0.00 212.00 6 1322.98 2.724830e+12 0.0 0.00 0.000000e+00 0.0 0.00 0.000000e+00 0.0 0.00 1322.98
69,604,634
Improve splitting and exploding a string column at comma and pivot it from long to wide in Pandas
<p>I'm wondering if there's a better way to split a string column into multiple rows by 'comma' and send the column from long to wide. My current approach is only working to some extent.</p> <pre><code>data = {'id':[&quot;ab3e3&quot;, &quot;psdds2&quot;, &quot;pas13&quot;, &quot;ccdf2&quot;, &quot;dsda1&quot;], 'fruit':[&quot;apple, organge&quot;, &quot;others&quot;, &quot;dragon fruit, organge&quot;, &quot;watermelon&quot;, &quot;others&quot;]} df = pd.DataFrame(data) lst_col = 'fruit' x = df.assign(**{lst_col:df[lst_col].str.split(',')}) dt = x.explode('fruit') dt['counts'] = 1 dt.pivot(index='id', columns='fruit', values = 'counts') id fruit 0 ab3e3 apple, organge 1 psdds2 others 2 pas13 dragon fruit, organge 3 ccdf2 watermelon 4 dsda1 others </code></pre> <p>Expected output:</p> <pre><code>id organge apple dragon fruit others watermelon ab3e3 1 1 0 0 0 ccdf2 0 0 0 0 1 dsda1 0 0 0 1 0 pas13 1 0 1 0 0 psdds2 0 0 0 1 0 </code></pre>
69,604,744
"2021-10-17T13:06:47.533000"
2
null
0
160
python|pandas
<p>Use <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.get_dummies.html" rel="nofollow noreferrer"><code>.str.get_dummies()</code></a> to get a dummy table from the <code>fruit</code> column. Then, <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.join.html" rel="nofollow noreferrer"><code>join</code></a> <code>df</code> with this dummy table, as follows:</p> <pre><code>df.drop('fruit', axis=1).join(df['fruit'].str.get_dummies(', ')) </code></pre> <p><strong>Result:</strong></p> <pre><code> id apple dragon fruit organge others watermelon 0 ab3e3 1 0 1 0 0 1 psdds2 0 0 0 1 0 2 pas13 0 1 1 0 0 3 ccdf2 0 0 0 0 1 4 dsda1 0 0 0 1 0 </code></pre>
"2021-10-17T13:23:04.237000"
1
https://pandas.pydata.org/docs/user_guide/reshaping.html
Reshaping and pivot tables# Reshaping and pivot tables# Reshaping by pivoting DataFrame objects# Data is often stored in so-called “stacked” or “record” format: Use .str.get_dummies() to get a dummy table from the fruit column. Then, join df with this dummy table, as follows: df.drop('fruit', axis=1).join(df['fruit'].str.get_dummies(', ')) Result: id apple dragon fruit organge others watermelon 0 ab3e3 1 0 1 0 0 1 psdds2 0 0 0 1 0 2 pas13 0 1 1 0 0 3 ccdf2 0 0 0 0 1 4 dsda1 0 0 0 1 0 In [1]: import pandas._testing as tm In [2]: def unpivot(frame): ...: N, K = frame.shape ...: data = { ...: "value": frame.to_numpy().ravel("F"), ...: "variable": np.asarray(frame.columns).repeat(N), ...: "date": np.tile(np.asarray(frame.index), K), ...: } ...: return pd.DataFrame(data, columns=["date", "variable", "value"]) ...: In [3]: df = unpivot(tm.makeTimeDataFrame(3)) In [4]: df Out[4]: date variable value 0 2000-01-03 A 0.469112 1 2000-01-04 A -0.282863 2 2000-01-05 A -1.509059 3 2000-01-03 B -1.135632 4 2000-01-04 B 1.212112 5 2000-01-05 B -0.173215 6 2000-01-03 C 0.119209 7 2000-01-04 C -1.044236 8 2000-01-05 C -0.861849 9 2000-01-03 D -2.104569 10 2000-01-04 D -0.494929 11 2000-01-05 D 1.071804 To select out everything for variable A we could do: In [5]: filtered = df[df["variable"] == "A"] In [6]: filtered Out[6]: date variable value 0 2000-01-03 A 0.469112 1 2000-01-04 A -0.282863 2 2000-01-05 A -1.509059 But suppose we wish to do time series operations with the variables. A better representation would be where the columns are the unique variables and an index of dates identifies individual observations. To reshape the data into this form, we use the DataFrame.pivot() method (also implemented as a top level function pivot()): In [7]: pivoted = df.pivot(index="date", columns="variable", values="value") In [8]: pivoted Out[8]: variable A B C D date 2000-01-03 0.469112 -1.135632 0.119209 -2.104569 2000-01-04 -0.282863 1.212112 -1.044236 -0.494929 2000-01-05 -1.509059 -0.173215 -0.861849 1.071804 If the values argument is omitted, and the input DataFrame has more than one column of values which are not used as column or index inputs to pivot(), then the resulting “pivoted” DataFrame will have hierarchical columns whose topmost level indicates the respective value column: In [9]: df["value2"] = df["value"] * 2 In [10]: pivoted = df.pivot(index="date", columns="variable") In [11]: pivoted Out[11]: value ... value2 variable A B C ... B C D date ... 2000-01-03 0.469112 -1.135632 0.119209 ... -2.271265 0.238417 -4.209138 2000-01-04 -0.282863 1.212112 -1.044236 ... 2.424224 -2.088472 -0.989859 2000-01-05 -1.509059 -0.173215 -0.861849 ... -0.346429 -1.723698 2.143608 [3 rows x 8 columns] You can then select subsets from the pivoted DataFrame: In [12]: pivoted["value2"] Out[12]: variable A B C D date 2000-01-03 0.938225 -2.271265 0.238417 -4.209138 2000-01-04 -0.565727 2.424224 -2.088472 -0.989859 2000-01-05 -3.018117 -0.346429 -1.723698 2.143608 Note that this returns a view on the underlying data in the case where the data are homogeneously-typed. Note pivot() will error with a ValueError: Index contains duplicate entries, cannot reshape if the index/column pair is not unique. In this case, consider using pivot_table() which is a generalization of pivot that can handle duplicate values for one index/column pair. Reshaping by stacking and unstacking# Closely related to the pivot() method are the related stack() and unstack() methods available on Series and DataFrame. These methods are designed to work together with MultiIndex objects (see the section on hierarchical indexing). Here are essentially what these methods do: stack(): “pivot” a level of the (possibly hierarchical) column labels, returning a DataFrame with an index with a new inner-most level of row labels. unstack(): (inverse operation of stack()) “pivot” a level of the (possibly hierarchical) row index to the column axis, producing a reshaped DataFrame with a new inner-most level of column labels. The clearest way to explain is by example. Let’s take a prior example data set from the hierarchical indexing section: In [13]: tuples = list( ....: zip( ....: *[ ....: ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"], ....: ["one", "two", "one", "two", "one", "two", "one", "two"], ....: ] ....: ) ....: ) ....: In [14]: index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"]) In [15]: df = pd.DataFrame(np.random.randn(8, 2), index=index, columns=["A", "B"]) In [16]: df2 = df[:4] In [17]: df2 Out[17]: A B first second bar one 0.721555 -0.706771 two -1.039575 0.271860 baz one -0.424972 0.567020 two 0.276232 -1.087401 The stack() function “compresses” a level in the DataFrame columns to produce either: A Series, in the case of a simple column Index. A DataFrame, in the case of a MultiIndex in the columns. If the columns have a MultiIndex, you can choose which level to stack. The stacked level becomes the new lowest level in a MultiIndex on the columns: In [18]: stacked = df2.stack() In [19]: stacked Out[19]: first second bar one A 0.721555 B -0.706771 two A -1.039575 B 0.271860 baz one A -0.424972 B 0.567020 two A 0.276232 B -1.087401 dtype: float64 With a “stacked” DataFrame or Series (having a MultiIndex as the index), the inverse operation of stack() is unstack(), which by default unstacks the last level: In [20]: stacked.unstack() Out[20]: A B first second bar one 0.721555 -0.706771 two -1.039575 0.271860 baz one -0.424972 0.567020 two 0.276232 -1.087401 In [21]: stacked.unstack(1) Out[21]: second one two first bar A 0.721555 -1.039575 B -0.706771 0.271860 baz A -0.424972 0.276232 B 0.567020 -1.087401 In [22]: stacked.unstack(0) Out[22]: first bar baz second one A 0.721555 -0.424972 B -0.706771 0.567020 two A -1.039575 0.276232 B 0.271860 -1.087401 If the indexes have names, you can use the level names instead of specifying the level numbers: In [23]: stacked.unstack("second") Out[23]: second one two first bar A 0.721555 -1.039575 B -0.706771 0.271860 baz A -0.424972 0.276232 B 0.567020 -1.087401 Notice that the stack() and unstack() methods implicitly sort the index levels involved. Hence a call to stack() and then unstack(), or vice versa, will result in a sorted copy of the original DataFrame or Series: In [24]: index = pd.MultiIndex.from_product([[2, 1], ["a", "b"]]) In [25]: df = pd.DataFrame(np.random.randn(4), index=index, columns=["A"]) In [26]: df Out[26]: A 2 a -0.370647 b -1.157892 1 a -1.344312 b 0.844885 In [27]: all(df.unstack().stack() == df.sort_index()) Out[27]: True The above code will raise a TypeError if the call to sort_index() is removed. Multiple levels# You may also stack or unstack more than one level at a time by passing a list of levels, in which case the end result is as if each level in the list were processed individually. In [28]: columns = pd.MultiIndex.from_tuples( ....: [ ....: ("A", "cat", "long"), ....: ("B", "cat", "long"), ....: ("A", "dog", "short"), ....: ("B", "dog", "short"), ....: ], ....: names=["exp", "animal", "hair_length"], ....: ) ....: In [29]: df = pd.DataFrame(np.random.randn(4, 4), columns=columns) In [30]: df Out[30]: exp A B A B animal cat cat dog dog hair_length long long short short 0 1.075770 -0.109050 1.643563 -1.469388 1 0.357021 -0.674600 -1.776904 -0.968914 2 -1.294524 0.413738 0.276662 -0.472035 3 -0.013960 -0.362543 -0.006154 -0.923061 In [31]: df.stack(level=["animal", "hair_length"]) Out[31]: exp A B animal hair_length 0 cat long 1.075770 -0.109050 dog short 1.643563 -1.469388 1 cat long 0.357021 -0.674600 dog short -1.776904 -0.968914 2 cat long -1.294524 0.413738 dog short 0.276662 -0.472035 3 cat long -0.013960 -0.362543 dog short -0.006154 -0.923061 The list of levels can contain either level names or level numbers (but not a mixture of the two). # df.stack(level=['animal', 'hair_length']) # from above is equivalent to: In [32]: df.stack(level=[1, 2]) Out[32]: exp A B animal hair_length 0 cat long 1.075770 -0.109050 dog short 1.643563 -1.469388 1 cat long 0.357021 -0.674600 dog short -1.776904 -0.968914 2 cat long -1.294524 0.413738 dog short 0.276662 -0.472035 3 cat long -0.013960 -0.362543 dog short -0.006154 -0.923061 Missing data# These functions are intelligent about handling missing data and do not expect each subgroup within the hierarchical index to have the same set of labels. They also can handle the index being unsorted (but you can make it sorted by calling sort_index(), of course). Here is a more complex example: In [33]: columns = pd.MultiIndex.from_tuples( ....: [ ....: ("A", "cat"), ....: ("B", "dog"), ....: ("B", "cat"), ....: ("A", "dog"), ....: ], ....: names=["exp", "animal"], ....: ) ....: In [34]: index = pd.MultiIndex.from_product( ....: [("bar", "baz", "foo", "qux"), ("one", "two")], names=["first", "second"] ....: ) ....: In [35]: df = pd.DataFrame(np.random.randn(8, 4), index=index, columns=columns) In [36]: df2 = df.iloc[[0, 1, 2, 4, 5, 7]] In [37]: df2 Out[37]: exp A B A animal cat dog cat dog first second bar one 0.895717 0.805244 -1.206412 2.565646 two 1.431256 1.340309 -1.170299 -0.226169 baz one 0.410835 0.813850 0.132003 -0.827317 foo one -1.413681 1.607920 1.024180 0.569605 two 0.875906 -2.211372 0.974466 -2.006747 qux two -1.226825 0.769804 -1.281247 -0.727707 As mentioned above, stack() can be called with a level argument to select which level in the columns to stack: In [38]: df2.stack("exp") Out[38]: animal cat dog first second exp bar one A 0.895717 2.565646 B -1.206412 0.805244 two A 1.431256 -0.226169 B -1.170299 1.340309 baz one A 0.410835 -0.827317 B 0.132003 0.813850 foo one A -1.413681 0.569605 B 1.024180 1.607920 two A 0.875906 -2.006747 B 0.974466 -2.211372 qux two A -1.226825 -0.727707 B -1.281247 0.769804 In [39]: df2.stack("animal") Out[39]: exp A B first second animal bar one cat 0.895717 -1.206412 dog 2.565646 0.805244 two cat 1.431256 -1.170299 dog -0.226169 1.340309 baz one cat 0.410835 0.132003 dog -0.827317 0.813850 foo one cat -1.413681 1.024180 dog 0.569605 1.607920 two cat 0.875906 0.974466 dog -2.006747 -2.211372 qux two cat -1.226825 -1.281247 dog -0.727707 0.769804 Unstacking can result in missing values if subgroups do not have the same set of labels. By default, missing values will be replaced with the default fill value for that data type, NaN for float, NaT for datetimelike, etc. For integer types, by default data will converted to float and missing values will be set to NaN. In [40]: df3 = df.iloc[[0, 1, 4, 7], [1, 2]] In [41]: df3 Out[41]: exp B animal dog cat first second bar one 0.805244 -1.206412 two 1.340309 -1.170299 foo one 1.607920 1.024180 qux two 0.769804 -1.281247 In [42]: df3.unstack() Out[42]: exp B animal dog cat second one two one two first bar 0.805244 1.340309 -1.206412 -1.170299 foo 1.607920 NaN 1.024180 NaN qux NaN 0.769804 NaN -1.281247 Alternatively, unstack takes an optional fill_value argument, for specifying the value of missing data. In [43]: df3.unstack(fill_value=-1e9) Out[43]: exp B animal dog cat second one two one two first bar 8.052440e-01 1.340309e+00 -1.206412e+00 -1.170299e+00 foo 1.607920e+00 -1.000000e+09 1.024180e+00 -1.000000e+09 qux -1.000000e+09 7.698036e-01 -1.000000e+09 -1.281247e+00 With a MultiIndex# Unstacking when the columns are a MultiIndex is also careful about doing the right thing: In [44]: df[:3].unstack(0) Out[44]: exp A B ... A animal cat dog ... cat dog first bar baz bar ... baz bar baz second ... one 0.895717 0.410835 0.805244 ... 0.132003 2.565646 -0.827317 two 1.431256 NaN 1.340309 ... NaN -0.226169 NaN [2 rows x 8 columns] In [45]: df2.unstack(1) Out[45]: exp A B ... A animal cat dog ... cat dog second one two one ... two one two first ... bar 0.895717 1.431256 0.805244 ... -1.170299 2.565646 -0.226169 baz 0.410835 NaN 0.813850 ... NaN -0.827317 NaN foo -1.413681 0.875906 1.607920 ... 0.974466 0.569605 -2.006747 qux NaN -1.226825 NaN ... -1.281247 NaN -0.727707 [4 rows x 8 columns] Reshaping by melt# The top-level melt() function and the corresponding DataFrame.melt() are useful to massage a DataFrame into a format where one or more columns are identifier variables, while all other columns, considered measured variables, are “unpivoted” to the row axis, leaving just two non-identifier columns, “variable” and “value”. The names of those columns can be customized by supplying the var_name and value_name parameters. For instance, In [46]: cheese = pd.DataFrame( ....: { ....: "first": ["John", "Mary"], ....: "last": ["Doe", "Bo"], ....: "height": [5.5, 6.0], ....: "weight": [130, 150], ....: } ....: ) ....: In [47]: cheese Out[47]: first last height weight 0 John Doe 5.5 130 1 Mary Bo 6.0 150 In [48]: cheese.melt(id_vars=["first", "last"]) Out[48]: first last variable value 0 John Doe height 5.5 1 Mary Bo height 6.0 2 John Doe weight 130.0 3 Mary Bo weight 150.0 In [49]: cheese.melt(id_vars=["first", "last"], var_name="quantity") Out[49]: first last quantity value 0 John Doe height 5.5 1 Mary Bo height 6.0 2 John Doe weight 130.0 3 Mary Bo weight 150.0 When transforming a DataFrame using melt(), the index will be ignored. The original index values can be kept around by setting the ignore_index parameter to False (default is True). This will however duplicate them. New in version 1.1.0. In [50]: index = pd.MultiIndex.from_tuples([("person", "A"), ("person", "B")]) In [51]: cheese = pd.DataFrame( ....: { ....: "first": ["John", "Mary"], ....: "last": ["Doe", "Bo"], ....: "height": [5.5, 6.0], ....: "weight": [130, 150], ....: }, ....: index=index, ....: ) ....: In [52]: cheese Out[52]: first last height weight person A John Doe 5.5 130 B Mary Bo 6.0 150 In [53]: cheese.melt(id_vars=["first", "last"]) Out[53]: first last variable value 0 John Doe height 5.5 1 Mary Bo height 6.0 2 John Doe weight 130.0 3 Mary Bo weight 150.0 In [54]: cheese.melt(id_vars=["first", "last"], ignore_index=False) Out[54]: first last variable value person A John Doe height 5.5 B Mary Bo height 6.0 A John Doe weight 130.0 B Mary Bo weight 150.0 Another way to transform is to use the wide_to_long() panel data convenience function. It is less flexible than melt(), but more user-friendly. In [55]: dft = pd.DataFrame( ....: { ....: "A1970": {0: "a", 1: "b", 2: "c"}, ....: "A1980": {0: "d", 1: "e", 2: "f"}, ....: "B1970": {0: 2.5, 1: 1.2, 2: 0.7}, ....: "B1980": {0: 3.2, 1: 1.3, 2: 0.1}, ....: "X": dict(zip(range(3), np.random.randn(3))), ....: } ....: ) ....: In [56]: dft["id"] = dft.index In [57]: dft Out[57]: A1970 A1980 B1970 B1980 X id 0 a d 2.5 3.2 -0.121306 0 1 b e 1.2 1.3 -0.097883 1 2 c f 0.7 0.1 0.695775 2 In [58]: pd.wide_to_long(dft, ["A", "B"], i="id", j="year") Out[58]: X A B id year 0 1970 -0.121306 a 2.5 1 1970 -0.097883 b 1.2 2 1970 0.695775 c 0.7 0 1980 -0.121306 d 3.2 1 1980 -0.097883 e 1.3 2 1980 0.695775 f 0.1 Combining with stats and GroupBy# It should be no shock that combining pivot() / stack() / unstack() with GroupBy and the basic Series and DataFrame statistical functions can produce some very expressive and fast data manipulations. In [59]: df Out[59]: exp A B A animal cat dog cat dog first second bar one 0.895717 0.805244 -1.206412 2.565646 two 1.431256 1.340309 -1.170299 -0.226169 baz one 0.410835 0.813850 0.132003 -0.827317 two -0.076467 -1.187678 1.130127 -1.436737 foo one -1.413681 1.607920 1.024180 0.569605 two 0.875906 -2.211372 0.974466 -2.006747 qux one -0.410001 -0.078638 0.545952 -1.219217 two -1.226825 0.769804 -1.281247 -0.727707 In [60]: df.stack().mean(1).unstack() Out[60]: animal cat dog first second bar one -0.155347 1.685445 two 0.130479 0.557070 baz one 0.271419 -0.006733 two 0.526830 -1.312207 foo one -0.194750 1.088763 two 0.925186 -2.109060 qux one 0.067976 -0.648927 two -1.254036 0.021048 # same result, another way In [61]: df.groupby(level=1, axis=1).mean() Out[61]: animal cat dog first second bar one -0.155347 1.685445 two 0.130479 0.557070 baz one 0.271419 -0.006733 two 0.526830 -1.312207 foo one -0.194750 1.088763 two 0.925186 -2.109060 qux one 0.067976 -0.648927 two -1.254036 0.021048 In [62]: df.stack().groupby(level=1).mean() Out[62]: exp A B second one 0.071448 0.455513 two -0.424186 -0.204486 In [63]: df.mean().unstack(0) Out[63]: exp A B animal cat 0.060843 0.018596 dog -0.413580 0.232430 Pivot tables# While pivot() provides general purpose pivoting with various data types (strings, numerics, etc.), pandas also provides pivot_table() for pivoting with aggregation of numeric data. The function pivot_table() can be used to create spreadsheet-style pivot tables. See the cookbook for some advanced strategies. It takes a number of arguments: data: a DataFrame object. values: a column or a list of columns to aggregate. index: a column, Grouper, array which has the same length as data, or list of them. Keys to group by on the pivot table index. If an array is passed, it is being used as the same manner as column values. columns: a column, Grouper, array which has the same length as data, or list of them. Keys to group by on the pivot table column. If an array is passed, it is being used as the same manner as column values. aggfunc: function to use for aggregation, defaulting to numpy.mean. Consider a data set like this: In [64]: import datetime In [65]: df = pd.DataFrame( ....: { ....: "A": ["one", "one", "two", "three"] * 6, ....: "B": ["A", "B", "C"] * 8, ....: "C": ["foo", "foo", "foo", "bar", "bar", "bar"] * 4, ....: "D": np.random.randn(24), ....: "E": np.random.randn(24), ....: "F": [datetime.datetime(2013, i, 1) for i in range(1, 13)] ....: + [datetime.datetime(2013, i, 15) for i in range(1, 13)], ....: } ....: ) ....: In [66]: df Out[66]: A B C D E F 0 one A foo 0.341734 -0.317441 2013-01-01 1 one B foo 0.959726 -1.236269 2013-02-01 2 two C foo -1.110336 0.896171 2013-03-01 3 three A bar -0.619976 -0.487602 2013-04-01 4 one B bar 0.149748 -0.082240 2013-05-01 .. ... .. ... ... ... ... 19 three B foo 0.690579 -2.213588 2013-08-15 20 one C foo 0.995761 1.063327 2013-09-15 21 one A bar 2.396780 1.266143 2013-10-15 22 two B bar 0.014871 0.299368 2013-11-15 23 three C bar 3.357427 -0.863838 2013-12-15 [24 rows x 6 columns] We can produce pivot tables from this data very easily: In [67]: pd.pivot_table(df, values="D", index=["A", "B"], columns=["C"]) Out[67]: C bar foo A B one A 1.120915 -0.514058 B -0.338421 0.002759 C -0.538846 0.699535 three A -1.181568 NaN B NaN 0.433512 C 0.588783 NaN two A NaN 1.000985 B 0.158248 NaN C NaN 0.176180 In [68]: pd.pivot_table(df, values="D", index=["B"], columns=["A", "C"], aggfunc=np.sum) Out[68]: A one three two C bar foo bar foo bar foo B A 2.241830 -1.028115 -2.363137 NaN NaN 2.001971 B -0.676843 0.005518 NaN 0.867024 0.316495 NaN C -1.077692 1.399070 1.177566 NaN NaN 0.352360 In [69]: pd.pivot_table( ....: df, values=["D", "E"], ....: index=["B"], ....: columns=["A", "C"], ....: aggfunc=np.sum, ....: ) ....: Out[69]: D ... E A one three ... three two C bar foo bar ... foo bar foo B ... A 2.241830 -1.028115 -2.363137 ... NaN NaN 0.128491 B -0.676843 0.005518 NaN ... -2.128743 -0.194294 NaN C -1.077692 1.399070 1.177566 ... NaN NaN 0.872482 [3 rows x 12 columns] The result object is a DataFrame having potentially hierarchical indexes on the rows and columns. If the values column name is not given, the pivot table will include all of the data in an additional level of hierarchy in the columns: In [70]: pd.pivot_table(df[["A", "B", "C", "D", "E"]], index=["A", "B"], columns=["C"]) Out[70]: D E C bar foo bar foo A B one A 1.120915 -0.514058 1.393057 -0.021605 B -0.338421 0.002759 0.684140 -0.551692 C -0.538846 0.699535 -0.988442 0.747859 three A -1.181568 NaN 0.961289 NaN B NaN 0.433512 NaN -1.064372 C 0.588783 NaN -0.131830 NaN two A NaN 1.000985 NaN 0.064245 B 0.158248 NaN -0.097147 NaN C NaN 0.176180 NaN 0.436241 Also, you can use Grouper for index and columns keywords. For detail of Grouper, see Grouping with a Grouper specification. In [71]: pd.pivot_table(df, values="D", index=pd.Grouper(freq="M", key="F"), columns="C") Out[71]: C bar foo F 2013-01-31 NaN -0.514058 2013-02-28 NaN 0.002759 2013-03-31 NaN 0.176180 2013-04-30 -1.181568 NaN 2013-05-31 -0.338421 NaN 2013-06-30 -0.538846 NaN 2013-07-31 NaN 1.000985 2013-08-31 NaN 0.433512 2013-09-30 NaN 0.699535 2013-10-31 1.120915 NaN 2013-11-30 0.158248 NaN 2013-12-31 0.588783 NaN You can render a nice output of the table omitting the missing values by calling to_string() if you wish: In [72]: table = pd.pivot_table(df, index=["A", "B"], columns=["C"], values=["D", "E"]) In [73]: print(table.to_string(na_rep="")) D E C bar foo bar foo A B one A 1.120915 -0.514058 1.393057 -0.021605 B -0.338421 0.002759 0.684140 -0.551692 C -0.538846 0.699535 -0.988442 0.747859 three A -1.181568 0.961289 B 0.433512 -1.064372 C 0.588783 -0.131830 two A 1.000985 0.064245 B 0.158248 -0.097147 C 0.176180 0.436241 Note that pivot_table() is also available as an instance method on DataFrame,i.e. DataFrame.pivot_table(). Adding margins# If you pass margins=True to pivot_table(), special All columns and rows will be added with partial group aggregates across the categories on the rows and columns: In [74]: table = df.pivot_table( ....: index=["A", "B"], ....: columns="C", ....: values=["D", "E"], ....: margins=True, ....: aggfunc=np.std ....: ) ....: In [75]: table Out[75]: D E C bar foo All bar foo All A B one A 1.804346 1.210272 1.569879 0.179483 0.418374 0.858005 B 0.690376 1.353355 0.898998 1.083825 0.968138 1.101401 C 0.273641 0.418926 0.771139 1.689271 0.446140 1.422136 three A 0.794212 NaN 0.794212 2.049040 NaN 2.049040 B NaN 0.363548 0.363548 NaN 1.625237 1.625237 C 3.915454 NaN 3.915454 1.035215 NaN 1.035215 two A NaN 0.442998 0.442998 NaN 0.447104 0.447104 B 0.202765 NaN 0.202765 0.560757 NaN 0.560757 C NaN 1.819408 1.819408 NaN 0.650439 0.650439 All 1.556686 0.952552 1.246608 1.250924 0.899904 1.059389 Additionally, you can call DataFrame.stack() to display a pivoted DataFrame as having a multi-level index: In [76]: table.stack() Out[76]: D E A B C one A All 1.569879 0.858005 bar 1.804346 0.179483 foo 1.210272 0.418374 B All 0.898998 1.101401 bar 0.690376 1.083825 ... ... ... two C All 1.819408 0.650439 foo 1.819408 0.650439 All All 1.246608 1.059389 bar 1.556686 1.250924 foo 0.952552 0.899904 [24 rows x 2 columns] Cross tabulations# Use crosstab() to compute a cross-tabulation of two (or more) factors. By default crosstab() computes a frequency table of the factors unless an array of values and an aggregation function are passed. It takes a number of arguments index: array-like, values to group by in the rows. columns: array-like, values to group by in the columns. values: array-like, optional, array of values to aggregate according to the factors. aggfunc: function, optional, If no values array is passed, computes a frequency table. rownames: sequence, default None, must match number of row arrays passed. colnames: sequence, default None, if passed, must match number of column arrays passed. margins: boolean, default False, Add row/column margins (subtotals) normalize: boolean, {‘all’, ‘index’, ‘columns’}, or {0,1}, default False. Normalize by dividing all values by the sum of values. Any Series passed will have their name attributes used unless row or column names for the cross-tabulation are specified For example: In [77]: foo, bar, dull, shiny, one, two = "foo", "bar", "dull", "shiny", "one", "two" In [78]: a = np.array([foo, foo, bar, bar, foo, foo], dtype=object) In [79]: b = np.array([one, one, two, one, two, one], dtype=object) In [80]: c = np.array([dull, dull, shiny, dull, dull, shiny], dtype=object) In [81]: pd.crosstab(a, [b, c], rownames=["a"], colnames=["b", "c"]) Out[81]: b one two c dull shiny dull shiny a bar 1 0 0 1 foo 2 1 1 0 If crosstab() receives only two Series, it will provide a frequency table. In [82]: df = pd.DataFrame( ....: {"A": [1, 2, 2, 2, 2], "B": [3, 3, 4, 4, 4], "C": [1, 1, np.nan, 1, 1]} ....: ) ....: In [83]: df Out[83]: A B C 0 1 3 1.0 1 2 3 1.0 2 2 4 NaN 3 2 4 1.0 4 2 4 1.0 In [84]: pd.crosstab(df["A"], df["B"]) Out[84]: B 3 4 A 1 1 0 2 1 3 crosstab() can also be implemented to Categorical data. In [85]: foo = pd.Categorical(["a", "b"], categories=["a", "b", "c"]) In [86]: bar = pd.Categorical(["d", "e"], categories=["d", "e", "f"]) In [87]: pd.crosstab(foo, bar) Out[87]: col_0 d e row_0 a 1 0 b 0 1 If you want to include all of data categories even if the actual data does not contain any instances of a particular category, you should set dropna=False. For example: In [88]: pd.crosstab(foo, bar, dropna=False) Out[88]: col_0 d e f row_0 a 1 0 0 b 0 1 0 c 0 0 0 Normalization# Frequency tables can also be normalized to show percentages rather than counts using the normalize argument: In [89]: pd.crosstab(df["A"], df["B"], normalize=True) Out[89]: B 3 4 A 1 0.2 0.0 2 0.2 0.6 normalize can also normalize values within each row or within each column: In [90]: pd.crosstab(df["A"], df["B"], normalize="columns") Out[90]: B 3 4 A 1 0.5 0.0 2 0.5 1.0 crosstab() can also be passed a third Series and an aggregation function (aggfunc) that will be applied to the values of the third Series within each group defined by the first two Series: In [91]: pd.crosstab(df["A"], df["B"], values=df["C"], aggfunc=np.sum) Out[91]: B 3 4 A 1 1.0 NaN 2 1.0 2.0 Adding margins# Finally, one can also add margins or normalize this output. In [92]: pd.crosstab( ....: df["A"], df["B"], values=df["C"], aggfunc=np.sum, normalize=True, margins=True ....: ) ....: Out[92]: B 3 4 All A 1 0.25 0.0 0.25 2 0.25 0.5 0.75 All 0.50 0.5 1.00 Tiling# The cut() function computes groupings for the values of the input array and is often used to transform continuous variables to discrete or categorical variables: In [93]: ages = np.array([10, 15, 13, 12, 23, 25, 28, 59, 60]) In [94]: pd.cut(ages, bins=3) Out[94]: [(9.95, 26.667], (9.95, 26.667], (9.95, 26.667], (9.95, 26.667], (9.95, 26.667], (9.95, 26.667], (26.667, 43.333], (43.333, 60.0], (43.333, 60.0]] Categories (3, interval[float64, right]): [(9.95, 26.667] < (26.667, 43.333] < (43.333, 60.0]] If the bins keyword is an integer, then equal-width bins are formed. Alternatively we can specify custom bin-edges: In [95]: c = pd.cut(ages, bins=[0, 18, 35, 70]) In [96]: c Out[96]: [(0, 18], (0, 18], (0, 18], (0, 18], (18, 35], (18, 35], (18, 35], (35, 70], (35, 70]] Categories (3, interval[int64, right]): [(0, 18] < (18, 35] < (35, 70]] If the bins keyword is an IntervalIndex, then these will be used to bin the passed data.: pd.cut([25, 20, 50], bins=c.categories) Computing indicator / dummy variables# To convert a categorical variable into a “dummy” or “indicator” DataFrame, for example a column in a DataFrame (a Series) which has k distinct values, can derive a DataFrame containing k columns of 1s and 0s using get_dummies(): In [97]: df = pd.DataFrame({"key": list("bbacab"), "data1": range(6)}) In [98]: pd.get_dummies(df["key"]) Out[98]: a b c 0 0 1 0 1 0 1 0 2 1 0 0 3 0 0 1 4 1 0 0 5 0 1 0 Sometimes it’s useful to prefix the column names, for example when merging the result with the original DataFrame: In [99]: dummies = pd.get_dummies(df["key"], prefix="key") In [100]: dummies Out[100]: key_a key_b key_c 0 0 1 0 1 0 1 0 2 1 0 0 3 0 0 1 4 1 0 0 5 0 1 0 In [101]: df[["data1"]].join(dummies) Out[101]: data1 key_a key_b key_c 0 0 0 1 0 1 1 0 1 0 2 2 1 0 0 3 3 0 0 1 4 4 1 0 0 5 5 0 1 0 This function is often used along with discretization functions like cut(): In [102]: values = np.random.randn(10) In [103]: values Out[103]: array([ 0.4082, -1.0481, -0.0257, -0.9884, 0.0941, 1.2627, 1.29 , 0.0824, -0.0558, 0.5366]) In [104]: bins = [0, 0.2, 0.4, 0.6, 0.8, 1] In [105]: pd.get_dummies(pd.cut(values, bins)) Out[105]: (0.0, 0.2] (0.2, 0.4] (0.4, 0.6] (0.6, 0.8] (0.8, 1.0] 0 0 0 1 0 0 1 0 0 0 0 0 2 0 0 0 0 0 3 0 0 0 0 0 4 1 0 0 0 0 5 0 0 0 0 0 6 0 0 0 0 0 7 1 0 0 0 0 8 0 0 0 0 0 9 0 0 1 0 0 See also Series.str.get_dummies. get_dummies() also accepts a DataFrame. By default all categorical variables (categorical in the statistical sense, those with object or categorical dtype) are encoded as dummy variables. In [106]: df = pd.DataFrame({"A": ["a", "b", "a"], "B": ["c", "c", "b"], "C": [1, 2, 3]}) In [107]: pd.get_dummies(df) Out[107]: C A_a A_b B_b B_c 0 1 1 0 0 1 1 2 0 1 0 1 2 3 1 0 1 0 All non-object columns are included untouched in the output. You can control the columns that are encoded with the columns keyword. In [108]: pd.get_dummies(df, columns=["A"]) Out[108]: B C A_a A_b 0 c 1 1 0 1 c 2 0 1 2 b 3 1 0 Notice that the B column is still included in the output, it just hasn’t been encoded. You can drop B before calling get_dummies if you don’t want to include it in the output. As with the Series version, you can pass values for the prefix and prefix_sep. By default the column name is used as the prefix, and _ as the prefix separator. You can specify prefix and prefix_sep in 3 ways: string: Use the same value for prefix or prefix_sep for each column to be encoded. list: Must be the same length as the number of columns being encoded. dict: Mapping column name to prefix. In [109]: simple = pd.get_dummies(df, prefix="new_prefix") In [110]: simple Out[110]: C new_prefix_a new_prefix_b new_prefix_b new_prefix_c 0 1 1 0 0 1 1 2 0 1 0 1 2 3 1 0 1 0 In [111]: from_list = pd.get_dummies(df, prefix=["from_A", "from_B"]) In [112]: from_list Out[112]: C from_A_a from_A_b from_B_b from_B_c 0 1 1 0 0 1 1 2 0 1 0 1 2 3 1 0 1 0 In [113]: from_dict = pd.get_dummies(df, prefix={"B": "from_B", "A": "from_A"}) In [114]: from_dict Out[114]: C from_A_a from_A_b from_B_b from_B_c 0 1 1 0 0 1 1 2 0 1 0 1 2 3 1 0 1 0 Sometimes it will be useful to only keep k-1 levels of a categorical variable to avoid collinearity when feeding the result to statistical models. You can switch to this mode by turn on drop_first. In [115]: s = pd.Series(list("abcaa")) In [116]: pd.get_dummies(s) Out[116]: a b c 0 1 0 0 1 0 1 0 2 0 0 1 3 1 0 0 4 1 0 0 In [117]: pd.get_dummies(s, drop_first=True) Out[117]: b c 0 0 0 1 1 0 2 0 1 3 0 0 4 0 0 When a column contains only one level, it will be omitted in the result. In [118]: df = pd.DataFrame({"A": list("aaaaa"), "B": list("ababc")}) In [119]: pd.get_dummies(df) Out[119]: A_a B_a B_b B_c 0 1 1 0 0 1 1 0 1 0 2 1 1 0 0 3 1 0 1 0 4 1 0 0 1 In [120]: pd.get_dummies(df, drop_first=True) Out[120]: B_b B_c 0 0 0 1 1 0 2 0 0 3 1 0 4 0 1 By default new columns will have np.uint8 dtype. To choose another dtype, use the dtype argument: In [121]: df = pd.DataFrame({"A": list("abc"), "B": [1.1, 2.2, 3.3]}) In [122]: pd.get_dummies(df, dtype=bool).dtypes Out[122]: B float64 A_a bool A_b bool A_c bool dtype: object New in version 1.5.0. To convert a “dummy” or “indicator” DataFrame, into a categorical DataFrame, for example k columns of a DataFrame containing 1s and 0s can derive a DataFrame which has k distinct values using from_dummies(): In [123]: df = pd.DataFrame({"prefix_a": [0, 1, 0], "prefix_b": [1, 0, 1]}) In [124]: df Out[124]: prefix_a prefix_b 0 0 1 1 1 0 2 0 1 In [125]: pd.from_dummies(df, sep="_") Out[125]: prefix 0 b 1 a 2 b Dummy coded data only requires k - 1 categories to be included, in this case the k th category is the default category, implied by not being assigned any of the other k - 1 categories, can be passed via default_category. In [126]: df = pd.DataFrame({"prefix_a": [0, 1, 0]}) In [127]: df Out[127]: prefix_a 0 0 1 1 2 0 In [128]: pd.from_dummies(df, sep="_", default_category="b") Out[128]: prefix 0 b 1 a 2 b Factorizing values# To encode 1-d values as an enumerated type use factorize(): In [129]: x = pd.Series(["A", "A", np.nan, "B", 3.14, np.inf]) In [130]: x Out[130]: 0 A 1 A 2 NaN 3 B 4 3.14 5 inf dtype: object In [131]: labels, uniques = pd.factorize(x) In [132]: labels Out[132]: array([ 0, 0, -1, 1, 2, 3]) In [133]: uniques Out[133]: Index(['A', 'B', 3.14, inf], dtype='object') Note that factorize() is similar to numpy.unique, but differs in its handling of NaN: Note The following numpy.unique will fail under Python 3 with a TypeError because of an ordering bug. See also here. In [134]: ser = pd.Series(['A', 'A', np.nan, 'B', 3.14, np.inf]) In [135]: pd.factorize(ser, sort=True) Out[135]: (array([ 2, 2, -1, 3, 0, 1]), Index([3.14, inf, 'A', 'B'], dtype='object')) In [136]: np.unique(ser, return_inverse=True)[::-1] --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[136], line 1 ----> 1 np.unique(ser, return_inverse=True)[::-1] File <__array_function__ internals>:180, in unique(*args, **kwargs) File ~/micromamba/envs/test/lib/python3.8/site-packages/numpy/lib/arraysetops.py:274, in unique(ar, return_index, return_inverse, return_counts, axis, equal_nan) 272 ar = np.asanyarray(ar) 273 if axis is None: --> 274 ret = _unique1d(ar, return_index, return_inverse, return_counts, 275 equal_nan=equal_nan) 276 return _unpack_tuple(ret) 278 # axis was specified and not None File ~/micromamba/envs/test/lib/python3.8/site-packages/numpy/lib/arraysetops.py:333, in _unique1d(ar, return_index, return_inverse, return_counts, equal_nan) 330 optional_indices = return_index or return_inverse 332 if optional_indices: --> 333 perm = ar.argsort(kind='mergesort' if return_index else 'quicksort') 334 aux = ar[perm] 335 else: TypeError: '<' not supported between instances of 'float' and 'str' Note If you just want to handle one column as a categorical variable (like R’s factor), you can use df["cat_col"] = pd.Categorical(df["col"]) or df["cat_col"] = df["col"].astype("category"). For full docs on Categorical, see the Categorical introduction and the API documentation. Examples# In this section, we will review frequently asked questions and examples. The column names and relevant column values are named to correspond with how this DataFrame will be pivoted in the answers below. In [137]: np.random.seed([3, 1415]) In [138]: n = 20 In [139]: cols = np.array(["key", "row", "item", "col"]) In [140]: df = cols + pd.DataFrame( .....: (np.random.randint(5, size=(n, 4)) // [2, 1, 2, 1]).astype(str) .....: ) .....: In [141]: df.columns = cols In [142]: df = df.join(pd.DataFrame(np.random.rand(n, 2).round(2)).add_prefix("val")) In [143]: df Out[143]: key row item col val0 val1 0 key0 row3 item1 col3 0.81 0.04 1 key1 row2 item1 col2 0.44 0.07 2 key1 row0 item1 col0 0.77 0.01 3 key0 row4 item0 col2 0.15 0.59 4 key1 row0 item2 col1 0.81 0.64 .. ... ... ... ... ... ... 15 key0 row3 item1 col1 0.31 0.23 16 key0 row0 item2 col3 0.86 0.01 17 key0 row4 item0 col3 0.64 0.21 18 key2 row2 item2 col0 0.13 0.45 19 key0 row2 item0 col4 0.37 0.70 [20 rows x 6 columns] Pivoting with single aggregations# Suppose we wanted to pivot df such that the col values are columns, row values are the index, and the mean of val0 are the values? In particular, the resulting DataFrame should look like: col col0 col1 col2 col3 col4 row row0 0.77 0.605 NaN 0.860 0.65 row2 0.13 NaN 0.395 0.500 0.25 row3 NaN 0.310 NaN 0.545 NaN row4 NaN 0.100 0.395 0.760 0.24 This solution uses pivot_table(). Also note that aggfunc='mean' is the default. It is included here to be explicit. In [144]: df.pivot_table(values="val0", index="row", columns="col", aggfunc="mean") Out[144]: col col0 col1 col2 col3 col4 row row0 0.77 0.605 NaN 0.860 0.65 row2 0.13 NaN 0.395 0.500 0.25 row3 NaN 0.310 NaN 0.545 NaN row4 NaN 0.100 0.395 0.760 0.24 Note that we can also replace the missing values by using the fill_value parameter. In [145]: df.pivot_table( .....: values="val0", .....: index="row", .....: columns="col", .....: aggfunc="mean", .....: fill_value=0, .....: ) .....: Out[145]: col col0 col1 col2 col3 col4 row row0 0.77 0.605 0.000 0.860 0.65 row2 0.13 0.000 0.395 0.500 0.25 row3 0.00 0.310 0.000 0.545 0.00 row4 0.00 0.100 0.395 0.760 0.24 Also note that we can pass in other aggregation functions as well. For example, we can also pass in sum. In [146]: df.pivot_table( .....: values="val0", .....: index="row", .....: columns="col", .....: aggfunc="sum", .....: fill_value=0, .....: ) .....: Out[146]: col col0 col1 col2 col3 col4 row row0 0.77 1.21 0.00 0.86 0.65 row2 0.13 0.00 0.79 0.50 0.50 row3 0.00 0.31 0.00 1.09 0.00 row4 0.00 0.10 0.79 1.52 0.24 Another aggregation we can do is calculate the frequency in which the columns and rows occur together a.k.a. “cross tabulation”. To do this, we can pass size to the aggfunc parameter. In [147]: df.pivot_table(index="row", columns="col", fill_value=0, aggfunc="size") Out[147]: col col0 col1 col2 col3 col4 row row0 1 2 0 1 1 row2 1 0 2 1 2 row3 0 1 0 2 0 row4 0 1 2 2 1 Pivoting with multiple aggregations# We can also perform multiple aggregations. For example, to perform both a sum and mean, we can pass in a list to the aggfunc argument. In [148]: df.pivot_table( .....: values="val0", .....: index="row", .....: columns="col", .....: aggfunc=["mean", "sum"], .....: ) .....: Out[148]: mean sum col col0 col1 col2 col3 col4 col0 col1 col2 col3 col4 row row0 0.77 0.605 NaN 0.860 0.65 0.77 1.21 NaN 0.86 0.65 row2 0.13 NaN 0.395 0.500 0.25 0.13 NaN 0.79 0.50 0.50 row3 NaN 0.310 NaN 0.545 NaN NaN 0.31 NaN 1.09 NaN row4 NaN 0.100 0.395 0.760 0.24 NaN 0.10 0.79 1.52 0.24 Note to aggregate over multiple value columns, we can pass in a list to the values parameter. In [149]: df.pivot_table( .....: values=["val0", "val1"], .....: index="row", .....: columns="col", .....: aggfunc=["mean"], .....: ) .....: Out[149]: mean val0 val1 col col0 col1 col2 col3 col4 col0 col1 col2 col3 col4 row row0 0.77 0.605 NaN 0.860 0.65 0.01 0.745 NaN 0.010 0.02 row2 0.13 NaN 0.395 0.500 0.25 0.45 NaN 0.34 0.440 0.79 row3 NaN 0.310 NaN 0.545 NaN NaN 0.230 NaN 0.075 NaN row4 NaN 0.100 0.395 0.760 0.24 NaN 0.070 0.42 0.300 0.46 Note to subdivide over multiple columns we can pass in a list to the columns parameter. In [150]: df.pivot_table( .....: values=["val0"], .....: index="row", .....: columns=["item", "col"], .....: aggfunc=["mean"], .....: ) .....: Out[150]: mean val0 item item0 item1 item2 col col2 col3 col4 col0 col1 col2 col3 col4 col0 col1 col3 col4 row row0 NaN NaN NaN 0.77 NaN NaN NaN NaN NaN 0.605 0.86 0.65 row2 0.35 NaN 0.37 NaN NaN 0.44 NaN NaN 0.13 NaN 0.50 0.13 row3 NaN NaN NaN NaN 0.31 NaN 0.81 NaN NaN NaN 0.28 NaN row4 0.15 0.64 NaN NaN 0.10 0.64 0.88 0.24 NaN NaN NaN NaN Exploding a list-like column# New in version 0.25.0. Sometimes the values in a column are list-like. In [151]: keys = ["panda1", "panda2", "panda3"] In [152]: values = [["eats", "shoots"], ["shoots", "leaves"], ["eats", "leaves"]] In [153]: df = pd.DataFrame({"keys": keys, "values": values}) In [154]: df Out[154]: keys values 0 panda1 [eats, shoots] 1 panda2 [shoots, leaves] 2 panda3 [eats, leaves] We can ‘explode’ the values column, transforming each list-like to a separate row, by using explode(). This will replicate the index values from the original row: In [155]: df["values"].explode() Out[155]: 0 eats 0 shoots 1 shoots 1 leaves 2 eats 2 leaves Name: values, dtype: object You can also explode the column in the DataFrame. In [156]: df.explode("values") Out[156]: keys values 0 panda1 eats 0 panda1 shoots 1 panda2 shoots 1 panda2 leaves 2 panda3 eats 2 panda3 leaves Series.explode() will replace empty lists with np.nan and preserve scalar entries. The dtype of the resulting Series is always object. In [157]: s = pd.Series([[1, 2, 3], "foo", [], ["a", "b"]]) In [158]: s Out[158]: 0 [1, 2, 3] 1 foo 2 [] 3 [a, b] dtype: object In [159]: s.explode() Out[159]: 0 1 0 2 0 3 1 foo 2 NaN 3 a 3 b dtype: object Here is a typical usecase. You have comma separated strings in a column and want to expand this. In [160]: df = pd.DataFrame([{"var1": "a,b,c", "var2": 1}, {"var1": "d,e,f", "var2": 2}]) In [161]: df Out[161]: var1 var2 0 a,b,c 1 1 d,e,f 2 Creating a long form DataFrame is now straightforward using explode and chained operations In [162]: df.assign(var1=df.var1.str.split(",")).explode("var1") Out[162]: var1 var2 0 a 1 0 b 1 0 c 1 1 d 2 1 e 2 1 f 2
165
716
Improve splitting and exploding a string column at comma and pivot it from long to wide in Pandas I'm wondering if there's a better way to split a string column into multiple rows by 'comma' and send the column from long to wide. My current approach is only working to some extent. data = {'id':["ab3e3", "psdds2", "pas13", "ccdf2", "dsda1"], 'fruit':["apple, organge", "others", "dragon fruit, organge", "watermelon", "others"]} df = pd.DataFrame(data) lst_col = 'fruit' x = df.assign(**{lst_col:df[lst_col].str.split(',')}) dt = x.explode('fruit') dt['counts'] = 1 dt.pivot(index='id', columns='fruit', values = 'counts') id fruit 0 ab3e3 apple, organge 1 psdds2 others 2 pas13 dragon fruit, organge 3 ccdf2 watermelon 4 dsda1 others Expected output: id organge apple dragon fruit others watermelon ab3e3 1 1 0 0 0 ccdf2 0 0 0 0 1 dsda1 0 0 0 1 0 pas13 1 0 1 0 0 psdds2 0 0 0 1 0
Use .str.get_dummies() to get a dummy table from the fruit column. Then, join df with this dummy table, as follows: df.drop('fruit', axis=1).join(df['fruit'].str.get_dummies(', ')) Result: id apple dragon fruit organge others watermelon 0 ab3e3 1 0 1 0 0 1 psdds2 0 0 0 1 0 2 pas13 0 1 1 0 0 3 ccdf2 0 0 0 0 1 4 dsda1 0 0 0 1 0
63,746,507
Convert regression tree output to pandas table
<p>This code fits a regression tree in python. I want to convert this text based output to a table format.</p> <p>Have looked into this ( <a href="https://stackoverflow.com/questions/53399214/convert-a-decision-tree-to-a-table">Convert a decision tree to a table</a> ) however the given solution doesn't work.</p> <pre><code>import pandas as pd import numpy as np from sklearn.tree import DecisionTreeRegressor from sklearn import tree dataset = np.array( [['Asset Flip', 100, 1000], ['Text Based', 500, 3000], ['Visual Novel', 1500, 5000], ['2D Pixel Art', 3500, 8000], ['2D Vector Art', 5000, 6500], ['Strategy', 6000, 7000], ['First Person Shooter', 8000, 15000], ['Simulator', 9500, 20000], ['Racing', 12000, 21000], ['RPG', 14000, 25000], ['Sandbox', 15500, 27000], ['Open-World', 16500, 30000], ['MMOFPS', 25000, 52000], ['MMORPG', 30000, 80000] ]) X = dataset[:, 1:2].astype(int) y = dataset[:, 2].astype(int) regressor = DecisionTreeRegressor(random_state = 0) regressor.fit(X, y) text_rule = tree.export_text(regressor ) print(text_rule) </code></pre> <p>Output I am getting is like this</p> <pre><code>print(text_rule) |--- feature_0 &lt;= 20750.00 | |--- feature_0 &lt;= 7000.00 | | |--- feature_0 &lt;= 1000.00 | | | |--- feature_0 &lt;= 300.00 | | | | |--- value: [1000.00] | | | |--- feature_0 &gt; 300.00 | | | | |--- value: [3000.00] | | |--- feature_0 &gt; 1000.00 | | | |--- feature_0 &lt;= 2500.00 | | | | |--- value: [5000.00] | | | |--- feature_0 &gt; 2500.00 | | | | |--- feature_0 &lt;= 4250.00 | | | | | |--- value: [8000.00] | | | | |--- feature_0 &gt; 4250.00 | | | | | |--- feature_0 &lt;= 5500.00 | | | | | | |--- value: [6500.00] | | | | | |--- feature_0 &gt; 5500.00 | | | | | | |--- value: [7000.00] | |--- feature_0 &gt; 7000.00 | | |--- feature_0 &lt;= 13000.00 | | | |--- feature_0 &lt;= 8750.00 | | | | |--- value: [15000.00] | | | |--- feature_0 &gt; 8750.00 | | | | |--- feature_0 &lt;= 10750.00 | | | | | |--- value: [20000.00] | | | | |--- feature_0 &gt; 10750.00 | | | | | |--- value: [21000.00] | | |--- feature_0 &gt; 13000.00 | | | |--- feature_0 &lt;= 16000.00 | | | | |--- feature_0 &lt;= 14750.00 | | | | | |--- value: [25000.00] | | | | |--- feature_0 &gt; 14750.00 | | | | | |--- value: [27000.00] | | | |--- feature_0 &gt; 16000.00 | | | | |--- value: [30000.00] |--- feature_0 &gt; 20750.00 | |--- feature_0 &lt;= 27500.00 | | |--- value: [52000.00] | |--- feature_0 &gt; 27500.00 | | |--- value: [80000.00] </code></pre> <p>I want to convert this rule in a pandas table something similar to the following form. How to do this ?</p> <p><a href="https://i.stack.imgur.com/gt8Or.png" rel="noreferrer"><img src="https://i.stack.imgur.com/gt8Or.png" alt="enter image description here" /></a></p> <p>Plot version of the rule is something like this ( for reference ). Please note in table I have showed the left most part of the rule.</p> <p><a href="https://i.stack.imgur.com/Z9NuJ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Z9NuJ.png" alt="enter image description here" /></a></p>
63,748,009
"2020-09-04T18:42:12.840000"
2
3
5
695
python|pandas
<p>Modifying the the code from the <a href="https://stackoverflow.com/a/53400587/3087542">linked answer</a>:</p> <pre class="lang-py prettyprint-override"><code>import sklearn import pandas as pd def tree_to_df(reg_tree, feature_names): tree_ = reg_tree.tree_ feature_name = [ feature_names[i] if i != sklearn.tree._tree.TREE_UNDEFINED else &quot;undefined!&quot; for i in tree_.feature ] def recurse(node, row, ret): if tree_.feature[node] != sklearn.tree._tree.TREE_UNDEFINED: name = feature_name[node] threshold = tree_.threshold[node] # Add rule to row and search left branch row[-1].append(name + &quot; &lt;= &quot; + str(threshold)) recurse(tree_.children_left[node], row, ret) # Add rule to row and search right branch row[-1].append(name + &quot; &gt; &quot; + str(threshold)) recurse(tree_.children_right[node], row, ret) else: # Add output rules and start a new row label = tree_.value[node] ret.append(&quot;return &quot; + str(label[0][0])) row.append([]) # Initialize rules = [[]] vals = [] # Call recursive function with initial values recurse(0, rules, vals) # Convert to table and output df = pd.DataFrame(rules).dropna(how='all') df['Return'] = pd.Series(vals) return df </code></pre> <p>This will return a pandas dataframe:</p> <pre><code> 0 1 2 3 Return 0 feature &lt;= 20750.0 feature &lt;= 7000.0 feature &lt;= 1000.0 feature &lt;= 300.0 return 1000.0 1 feature &gt; 300.0 None None None return 3000.0 2 feature &gt; 1000.0 feature &lt;= 2500.0 None None return 5000.0 3 feature &gt; 2500.0 feature &lt;= 4250.0 None None return 8000.0 4 feature &gt; 4250.0 feature &lt;= 5500.0 None None return 6500.0 5 feature &gt; 5500.0 None None None return 7000.0 6 feature &gt; 7000.0 feature &lt;= 13000.0 feature &lt;= 8750.0 None return 15000.0 7 feature &gt; 8750.0 feature &lt;= 10750.0 None None return 20000.0 8 feature &gt; 10750.0 None None None return 21000.0 9 feature &gt; 13000.0 feature &lt;= 16000.0 feature &lt;= 14750.0 None return 25000.0 10 feature &gt; 14750.0 None None None return 27000.0 11 feature &gt; 16000.0 None None None return 30000.0 12 feature &gt; 20750.0 feature &lt;= 27500.0 None None return 52000.0 13 feature &gt; 27500.0 None None None return 80000.0 </code></pre>
"2020-09-04T21:03:27.077000"
1
https://pandas.pydata.org/docs/user_guide/style.html
Modifying the the code from the linked answer: import sklearn import pandas as pd def tree_to_df(reg_tree, feature_names): tree_ = reg_tree.tree_ feature_name = [ feature_names[i] if i != sklearn.tree._tree.TREE_UNDEFINED else "undefined!" for i in tree_.feature ] def recurse(node, row, ret): if tree_.feature[node] != sklearn.tree._tree.TREE_UNDEFINED: name = feature_name[node] threshold = tree_.threshold[node] # Add rule to row and search left branch row[-1].append(name + " <= " + str(threshold)) recurse(tree_.children_left[node], row, ret) # Add rule to row and search right branch row[-1].append(name + " > " + str(threshold)) recurse(tree_.children_right[node], row, ret) else: # Add output rules and start a new row label = tree_.value[node] ret.append("return " + str(label[0][0])) row.append([]) # Initialize rules = [[]] vals = [] # Call recursive function with initial values recurse(0, rules, vals) # Convert to table and output df = pd.DataFrame(rules).dropna(how='all') df['Return'] = pd.Series(vals) return df This will return a pandas dataframe: 0 1 2 3 Return 0 feature <= 20750.0 feature <= 7000.0 feature <= 1000.0 feature <= 300.0 return 1000.0 1 feature > 300.0 None None None return 3000.0 2 feature > 1000.0 feature <= 2500.0 None None return 5000.0 3 feature > 2500.0 feature <= 4250.0 None None return 8000.0 4 feature > 4250.0 feature <= 5500.0 None None return 6500.0 5 feature > 5500.0 None None None return 7000.0 6 feature > 7000.0 feature <= 13000.0 feature <= 8750.0 None return 15000.0 7 feature > 8750.0 feature <= 10750.0 None None return 20000.0 8 feature > 10750.0 None None None return 21000.0 9 feature > 13000.0 feature <= 16000.0 feature <= 14750.0 None return 25000.0 10 feature > 14750.0 None None None return 27000.0 11 feature > 16000.0 None None None return 30000.0 12 feature > 20750.0 feature <= 27500.0 None None return 52000.0 13 feature > 27500.0 None None None return 80000.0
0
2,772
Convert regression tree output to pandas table This code fits a regression tree in python. I want to convert this text based output to a table format. Have looked into this ( Convert a decision tree to a table ) however the given solution doesn't work. import pandas as pd import numpy as np from sklearn.tree import DecisionTreeRegressor from sklearn import tree dataset = np.array( [['Asset Flip', 100, 1000], ['Text Based', 500, 3000], ['Visual Novel', 1500, 5000], ['2D Pixel Art', 3500, 8000], ['2D Vector Art', 5000, 6500], ['Strategy', 6000, 7000], ['First Person Shooter', 8000, 15000], ['Simulator', 9500, 20000], ['Racing', 12000, 21000], ['RPG', 14000, 25000], ['Sandbox', 15500, 27000], ['Open-World', 16500, 30000], ['MMOFPS', 25000, 52000], ['MMORPG', 30000, 80000] ]) X = dataset[:, 1:2].astype(int) y = dataset[:, 2].astype(int) regressor = DecisionTreeRegressor(random_state = 0) regressor.fit(X, y) text_rule = tree.export_text(regressor ) print(text_rule) Output I am getting is like this print(text_rule) |--- feature_0 <= 20750.00 | |--- feature_0 <= 7000.00 | | |--- feature_0 <= 1000.00 | | | |--- feature_0 <= 300.00 | | | | |--- value: [1000.00] | | | |--- feature_0 > 300.00 | | | | |--- value: [3000.00] | | |--- feature_0 > 1000.00 | | | |--- feature_0 <= 2500.00 | | | | |--- value: [5000.00] | | | |--- feature_0 > 2500.00 | | | | |--- feature_0 <= 4250.00 | | | | | |--- value: [8000.00] | | | | |--- feature_0 > 4250.00 | | | | | |--- feature_0 <= 5500.00 | | | | | | |--- value: [6500.00] | | | | | |--- feature_0 > 5500.00 | | | | | | |--- value: [7000.00] | |--- feature_0 > 7000.00 | | |--- feature_0 <= 13000.00 | | | |--- feature_0 <= 8750.00 | | | | |--- value: [15000.00] | | | |--- feature_0 > 8750.00 | | | | |--- feature_0 <= 10750.00 | | | | | |--- value: [20000.00] | | | | |--- feature_0 > 10750.00 | | | | | |--- value: [21000.00] | | |--- feature_0 > 13000.00 | | | |--- feature_0 <= 16000.00 | | | | |--- feature_0 <= 14750.00 | | | | | |--- value: [25000.00] | | | | |--- feature_0 > 14750.00 | | | | | |--- value: [27000.00] | | | |--- feature_0 > 16000.00 | | | | |--- value: [30000.00] |--- feature_0 > 20750.00 | |--- feature_0 <= 27500.00 | | |--- value: [52000.00] | |--- feature_0 > 27500.00 | | |--- value: [80000.00] I want to convert this rule in a pandas table something similar to the following form. How to do this ? Plot version of the rule is something like this ( for reference ). Please note in table I have showed the left most part of the rule.
Modifying the the code from the linked answer: import sklearn import pandas as pd def tree_to_df(reg_tree, feature_names): tree_ = reg_tree.tree_ feature_name = [ feature_names[i] if i != sklearn.tree._tree.TREE_UNDEFINED else "undefined!" for i in tree_.feature ] def recurse(node, row, ret): if tree_.feature[node] != sklearn.tree._tree.TREE_UNDEFINED: name = feature_name[node] threshold = tree_.threshold[node] # Add rule to row and search left branch row[-1].append(name + " <= " + str(threshold)) recurse(tree_.children_left[node], row, ret) # Add rule to row and search right branch row[-1].append(name + " > " + str(threshold)) recurse(tree_.children_right[node], row, ret) else: # Add output rules and start a new row label = tree_.value[node] ret.append("return " + str(label[0][0])) row.append([]) # Initialize rules = [[]] vals = [] # Call recursive function with initial values recurse(0, rules, vals) # Convert to table and output df = pd.DataFrame(rules).dropna(how='all') df['Return'] = pd.Series(vals) return df This will return a pandas dataframe: 0 1 2 3 Return 0 feature <= 20750.0 feature <= 7000.0 feature <= 1000.0 feature <= 300.0 return 1000.0 1 feature > 300.0 None None None return 3000.0 2 feature > 1000.0 feature <= 2500.0 None None return 5000.0 3 feature > 2500.0 feature <= 4250.0 None None return 8000.0 4 feature > 4250.0 feature <= 5500.0 None None return 6500.0 5 feature > 5500.0 None None None return 7000.0 6 feature > 7000.0 feature <= 13000.0 feature <= 8750.0 None return 15000.0 7 feature > 8750.0 feature <= 10750.0 None None return 20000.0 8 feature > 10750.0 None None None return 21000.0 9 feature > 13000.0 feature <= 16000.0 feature <= 14750.0 None return 25000.0 10 feature > 14750.0 None None None return 27000.0 11 feature > 16000.0 None None None return 30000.0 12 feature > 20750.0 feature <= 27500.0 None None return 52000.0 13 feature > 27500.0 None None None return 80000.0
61,854,984
Python Pandas resampling specific hours over different days and day ranges
<p>I have data records for different entities, and for each entity some count recorded in a specific hour during the day for a whole month. For example:</p> <pre><code> entity_id time counts 0 175 2019-03-01 05:00:00 3 1 175 2019-03-01 06:00:00 4 2 175 2019-03-01 07:00:00 6 3 175 2019-03-01 08:00:00 6 4 175 2019-03-01 09:00:00 7 5 178 2019-03-01 05:00:00 8 6 178 2019-03-01 06:00:00 4 7 178 2019-03-01 07:00:00 5 8 178 2019-03-01 08:00:00 6 9 200 2019-03-01 05:00:00 7 10 200 2019-03-01 08:00:00 3 11 175 2019-03-03 05:00:00 3 12 175 2019-03-03 07:00:00 6 13 175 2019-03-03 08:00:00 6 14 175 2019-03-03 09:00:00 7 15 178 2019-03-03 05:00:00 8 16 178 2019-03-03 06:00:00 4 17 178 2019-03-03 07:00:00 5 18 178 2019-03-03 08:00:00 6 19 200 2019-03-03 05:00:00 7 20 200 2019-03-03 08:00:00 3 21 200 2019-03-03 09:00:00 7 ... </code></pre> <p>I want to be able to aggregate for each entity the mean of the counts in several ranges of hours in different days of the week throughout the month. E.g.:</p> <ul> <li>The mean for Morning (6-10AM) on Sundays</li> <li>The mean for Morning (6-10AM) on Sundays-Thursdays</li> <li>The mean for Noon (11AM-1PM) on Sundays-Thursdays </li> <li>The mean for Noon (11AM-1PM) on Fri-Sat </li> <li>The mean for Evening (6PM-9PM) on Fri </li> <li>etc.</li> </ul> <p>So I wish to get a df like this (partial example):</p> <pre><code> entity_id day_in_week time_in_day counts_mean 0 175 sun eve 5 1 175 sun-thu noon 6 2 178 sun eve 5 3 178 sat eve 5 4 200 sun-thu morning 2 ... </code></pre> <p>I managed to get this partially done by iterating over the data, slicing and extracting different elements, but I assume there's a much more efficient way.</p> <p>I started with <a href="https://stackoverflow.com/questions/36977103/python-pandas-resampling-only-specific-hours">this issue</a>, but I still had too many for loops. Any ideas how to optimize the performance?</p>
61,858,222
"2020-05-17T16:17:19.663000"
2
null
0
217
python|pandas
<p>The idea of my solution is based on an auxiliary DataFrame with definitions of ranges, for which means are to be computed (<em>day_in_week</em>, <em>time_in_day</em> and respective <em>CustomBusinessHour</em> for the above attributes).</p> <p>Creation of this DataFrame (I called it <em>calendars</em>) starts from <em>day_in_week</em>, <em>time_in_day</em> columns:</p> <pre><code>calendars = pd.DataFrame([ ['sun', 'morning'], ['sun-thu', 'morning'], ['sun-thu', 'noon'], ['fri-sat', 'noon'], ['fri', 'eve']], columns=['day_in_week', 'time_in_day']) </code></pre> <p>If you want more such definitions, add them here.</p> <p>Then, to add corresponding <em>CustomBusinessHour</em> objects:</p> <ol> <li><p>Define a function to get hour limits:</p> <pre><code>def getHourLimits(name): if name == 'morning': return '06:00', '10:00' elif name == 'noon': return '11:00', '13:00' elif name == 'eve': return '18:00', '21:00' else: return '8:00', '16:00' </code></pre></li> <li><p>Define a function to get week mask (start hour and end hour):</p> <pre><code>def getWeekMask(name): parts = name.split('-') if len(parts) &gt; 1: fullWeek = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] ind1 = fullWeek.index(parts[0].capitalize()) ind2 = fullWeek.index(parts[1].capitalize()) return ' '.join(fullWeek[ind1 : ind2 + 1]) else: return parts[0].capitalize() </code></pre></li> <li><p>Define a function generating a <em>CustomBusinessHour</em> object:</p> <pre><code>def getCBH(row): wkMask = getWeekMask(row.day_in_week) hStart, hEnd = getHourLimits(row.time_in_day) return pd.offsets.CustomBusinessHour(weekmask=wkMask, start=hStart, end=hEnd) </code></pre></li> <li><p>Add <em>CustomBusinessHour</em> objects to <em>calendars</em>:</p> <pre><code>calendars['CBH'] = calendars.apply(getCBH, axis=1) </code></pre></li> </ol> <p>Then define a function computing all required means, for the given entity Id:</p> <pre><code>def getSums(entId): outRows = [] wrk = df[df.entity_id.eq(entId)] # Filter for entity Id for _, row in calendars.iterrows(): dd = row.day_in_week hh = row.time_in_day cbh = row.CBH # Filter for the current calendar cnts = wrk[wrk.time.apply(lambda val: cbh.is_on_offset(val))] cnt = cnts.counts.mean() if pd.notnull(cnt): outRows.append(pd.Series([entId, dd, hh, cnt], index=['entity_id', 'day_in_week', 'time_in_day', 'counts_mean'])) return pd.DataFrame(outRows) </code></pre> <p>As you can see, the result contains only non-null means.</p> <p>And to generate the result, run:</p> <pre><code>pd.concat([getSums(entId) for entId in df.entity_id.unique()], ignore_index=True) </code></pre> <p>For your data sample (containing only readings from morning hours), the result is:</p> <pre><code> entity_id day_in_week time_in_day counts_mean 0 175 sun morning 6.333333 1 175 sun-thu morning 6.333333 2 178 sun morning 5.000000 3 178 sun-thu morning 5.000000 4 200 sun morning 5.000000 5 200 sun-thu morning 5.000000 </code></pre>
"2020-05-17T20:18:55.020000"
1
https://pandas.pydata.org/docs/user_guide/timeseries.html
The idea of my solution is based on an auxiliary DataFrame with definitions of ranges, for which means are to be computed (day_in_week, time_in_day and respective CustomBusinessHour for the above attributes). Creation of this DataFrame (I called it calendars) starts from day_in_week, time_in_day columns: calendars = pd.DataFrame([ ['sun', 'morning'], ['sun-thu', 'morning'], ['sun-thu', 'noon'], ['fri-sat', 'noon'], ['fri', 'eve']], columns=['day_in_week', 'time_in_day']) If you want more such definitions, add them here. Then, to add corresponding CustomBusinessHour objects: Define a function to get hour limits: def getHourLimits(name): if name == 'morning': return '06:00', '10:00' elif name == 'noon': return '11:00', '13:00' elif name == 'eve': return '18:00', '21:00' else: return '8:00', '16:00' Define a function to get week mask (start hour and end hour): def getWeekMask(name): parts = name.split('-') if len(parts) > 1: fullWeek = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] ind1 = fullWeek.index(parts[0].capitalize()) ind2 = fullWeek.index(parts[1].capitalize()) return ' '.join(fullWeek[ind1 : ind2 + 1]) else: return parts[0].capitalize() Define a function generating a CustomBusinessHour object: def getCBH(row): wkMask = getWeekMask(row.day_in_week) hStart, hEnd = getHourLimits(row.time_in_day) return pd.offsets.CustomBusinessHour(weekmask=wkMask, start=hStart, end=hEnd) Add CustomBusinessHour objects to calendars: calendars['CBH'] = calendars.apply(getCBH, axis=1) Then define a function computing all required means, for the given entity Id: def getSums(entId): outRows = [] wrk = df[df.entity_id.eq(entId)] # Filter for entity Id for _, row in calendars.iterrows(): dd = row.day_in_week hh = row.time_in_day cbh = row.CBH # Filter for the current calendar cnts = wrk[wrk.time.apply(lambda val: cbh.is_on_offset(val))] cnt = cnts.counts.mean() if pd.notnull(cnt): outRows.append(pd.Series([entId, dd, hh, cnt], index=['entity_id', 'day_in_week', 'time_in_day', 'counts_mean'])) return pd.DataFrame(outRows) As you can see, the result contains only non-null means. And to generate the result, run: pd.concat([getSums(entId) for entId in df.entity_id.unique()], ignore_index=True) For your data sample (containing only readings from morning hours), the result is: entity_id day_in_week time_in_day counts_mean 0 175 sun morning 6.333333 1 175 sun-thu morning 6.333333 2 178 sun morning 5.000000 3 178 sun-thu morning 5.000000 4 200 sun morning 5.000000 5 200 sun-thu morning 5.000000
0
2,901
Python Pandas resampling specific hours over different days and day ranges I have data records for different entities, and for each entity some count recorded in a specific hour during the day for a whole month. For example: entity_id time counts 0 175 2019-03-01 05:00:00 3 1 175 2019-03-01 06:00:00 4 2 175 2019-03-01 07:00:00 6 3 175 2019-03-01 08:00:00 6 4 175 2019-03-01 09:00:00 7 5 178 2019-03-01 05:00:00 8 6 178 2019-03-01 06:00:00 4 7 178 2019-03-01 07:00:00 5 8 178 2019-03-01 08:00:00 6 9 200 2019-03-01 05:00:00 7 10 200 2019-03-01 08:00:00 3 11 175 2019-03-03 05:00:00 3 12 175 2019-03-03 07:00:00 6 13 175 2019-03-03 08:00:00 6 14 175 2019-03-03 09:00:00 7 15 178 2019-03-03 05:00:00 8 16 178 2019-03-03 06:00:00 4 17 178 2019-03-03 07:00:00 5 18 178 2019-03-03 08:00:00 6 19 200 2019-03-03 05:00:00 7 20 200 2019-03-03 08:00:00 3 21 200 2019-03-03 09:00:00 7 ... I want to be able to aggregate for each entity the mean of the counts in several ranges of hours in different days of the week throughout the month. E.g.: The mean for Morning (6-10AM) on Sundays The mean for Morning (6-10AM) on Sundays-Thursdays The mean for Noon (11AM-1PM) on Sundays-Thursdays The mean for Noon (11AM-1PM) on Fri-Sat The mean for Evening (6PM-9PM) on Fri etc. So I wish to get a df like this (partial example): entity_id day_in_week time_in_day counts_mean 0 175 sun eve 5 1 175 sun-thu noon 6 2 178 sun eve 5 3 178 sat eve 5 4 200 sun-thu morning 2 ... I managed to get this partially done by iterating over the data, slicing and extracting different elements, but I assume there's a much more efficient way. I started with this issue, but I still had too many for loops. Any ideas how to optimize the performance?
The idea of my solution is based on an auxiliary DataFrame with definitions of ranges, for which means are to be computed (day_in_week, time_in_day and respective CustomBusinessHour for the above attributes). Creation of this DataFrame (I called it calendars) starts from day_in_week, time_in_day columns: calendars = pd.DataFrame([ ['sun', 'morning'], ['sun-thu', 'morning'], ['sun-thu', 'noon'], ['fri-sat', 'noon'], ['fri', 'eve']], columns=['day_in_week', 'time_in_day']) If you want more such definitions, add them here. Then, to add corresponding CustomBusinessHour objects: Define a function to get hour limits: def getHourLimits(name): if name == 'morning': return '06:00', '10:00' elif name == 'noon': return '11:00', '13:00' elif name == 'eve': return '18:00', '21:00' else: return '8:00', '16:00' Define a function to get week mask (start hour and end hour): def getWeekMask(name): parts = name.split('-') if len(parts) > 1: fullWeek = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] ind1 = fullWeek.index(parts[0].capitalize()) ind2 = fullWeek.index(parts[1].capitalize()) return ' '.join(fullWeek[ind1 : ind2 + 1]) else: return parts[0].capitalize() Define a function generating a CustomBusinessHour object: def getCBH(row): wkMask = getWeekMask(row.day_in_week) hStart, hEnd = getHourLimits(row.time_in_day) return pd.offsets.CustomBusinessHour(weekmask=wkMask, start=hStart, end=hEnd) Add CustomBusinessHour objects to calendars: calendars['CBH'] = calendars.apply(getCBH, axis=1) Then define a function computing all required means, for the given entity Id: def getSums(entId): outRows = [] wrk = df[df.entity_id.eq(entId)] # Filter for entity Id for _, row in calendars.iterrows(): dd = row.day_in_week hh = row.time_in_day cbh = row.CBH # Filter for the current calendar cnts = wrk[wrk.time.apply(lambda val: cbh.is_on_offset(val))] cnt = cnts.counts.mean() if pd.notnull(cnt): outRows.append(pd.Series([entId, dd, hh, cnt], index=['entity_id', 'day_in_week', 'time_in_day', 'counts_mean'])) return pd.DataFrame(outRows) As you can see, the result contains only non-null means. And to generate the result, run: pd.concat([getSums(entId) for entId in df.entity_id.unique()], ignore_index=True) For your data sample (containing only readings from morning hours), the result is: entity_id day_in_week time_in_day counts_mean 0 175 sun morning 6.333333 1 175 sun-thu morning 6.333333 2 178 sun morning 5.000000 3 178 sun-thu morning 5.000000 4 200 sun morning 5.000000 5 200 sun-thu morning 5.000000
66,344,769
How to rename columns by appending the row value to the column name in Pandas?
<p>I have a dataframe as such:</p> <pre><code>**A** **B** **C** **Name** Hello No Why - 2 5 303 Sir </code></pre> <p>And I want to rename the columns by appending the row value to the column name:</p> <pre><code>A_Hello B_No C_Why Name 2 5 303 Sir </code></pre> <p>(That row will be removed after I change the column names)</p>
66,344,954
"2021-02-24T04:35:05.493000"
2
null
0
258
python|pandas
<p>Here is a more general solution that would apply to a much larger dataframe without having to manually rename the columns:</p> <pre><code>df.columns = [col.replace('*','')+ '_' +df.iloc[0,idx] for idx, col in enumerate(df.columns)] df = df[1:] print(df) #output: A_Hello B_No C_Why Name_- 1 2 5 303 Sir </code></pre>
"2021-02-24T05:01:20.670000"
2
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rename.html
pandas.DataFrame.rename# pandas.DataFrame.rename# DataFrame.rename(mapper=None, *, index=None, columns=None, axis=None, copy=None, inplace=False, level=None, errors='ignore')[source]# Here is a more general solution that would apply to a much larger dataframe without having to manually rename the columns: df.columns = [col.replace('*','')+ '_' +df.iloc[0,idx] for idx, col in enumerate(df.columns)] df = df[1:] print(df) #output: A_Hello B_No C_Why Name_- 1 2 5 303 Sir Alter axes labels. Function / dict values must be unique (1-to-1). Labels not contained in a dict / Series will be left as-is. Extra labels listed don’t throw an error. See the user guide for more. Parameters mapperdict-like or functionDict-like or function transformations to apply to that axis’ values. Use either mapper and axis to specify the axis to target with mapper, or index and columns. indexdict-like or functionAlternative to specifying axis (mapper, axis=0 is equivalent to index=mapper). columnsdict-like or functionAlternative to specifying axis (mapper, axis=1 is equivalent to columns=mapper). axis{0 or ‘index’, 1 or ‘columns’}, default 0Axis to target with mapper. Can be either the axis name (‘index’, ‘columns’) or number (0, 1). The default is ‘index’. copybool, default TrueAlso copy underlying data. inplacebool, default FalseWhether to modify the DataFrame rather than creating a new one. If True then value of copy is ignored. levelint or level name, default NoneIn case of a MultiIndex, only rename labels in the specified level. errors{‘ignore’, ‘raise’}, default ‘ignore’If ‘raise’, raise a KeyError when a dict-like mapper, index, or columns contains labels that are not present in the Index being transformed. If ‘ignore’, existing keys will be renamed and extra keys will be ignored. Returns DataFrame or NoneDataFrame with the renamed axis labels or None if inplace=True. Raises KeyErrorIf any of the labels is not found in the selected axis and “errors=’raise’”. See also DataFrame.rename_axisSet the name of the axis. Examples DataFrame.rename supports two calling conventions (index=index_mapper, columns=columns_mapper, ...) (mapper, axis={'index', 'columns'}, ...) We highly recommend using keyword arguments to clarify your intent. Rename columns using a mapping: >>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) >>> df.rename(columns={"A": "a", "B": "c"}) a c 0 1 4 1 2 5 2 3 6 Rename index using a mapping: >>> df.rename(index={0: "x", 1: "y", 2: "z"}) A B x 1 4 y 2 5 z 3 6 Cast index labels to a different type: >>> df.index RangeIndex(start=0, stop=3, step=1) >>> df.rename(index=str).index Index(['0', '1', '2'], dtype='object') >>> df.rename(columns={"A": "a", "B": "b", "C": "c"}, errors="raise") Traceback (most recent call last): KeyError: ['C'] not found in axis Using axis-style parameters: >>> df.rename(str.lower, axis='columns') a b 0 1 4 1 2 5 2 3 6 >>> df.rename({1: 2, 2: 4}, axis='index') A B 0 1 4 2 2 5 4 3 6
188
493
How to rename columns by appending the row value to the column name in Pandas? I have a dataframe as such: **A** **B** **C** **Name** Hello No Why - 2 5 303 Sir And I want to rename the columns by appending the row value to the column name: A_Hello B_No C_Why Name 2 5 303 Sir (That row will be removed after I change the column names)
A B 0 1 4 2 2 5 4 3 6 /
Here is a more general solution that would apply to a much larger dataframe without having to manually rename the columns: df.columns = [col.replace('*','')+ '_' +df.iloc[0,idx] for idx, col in enumerate(df.columns)] df = df[1:] print(df) #output: A_Hello B_No C_Why Name_- 1 2 5 303 Sir
70,014,461
pandas find intersection point of two lines in dataframe
<p>Tow lines with its points:</p> <pre><code>df1=pd.DataFrame({'y':[262,306,360,360,380,380],'x':[0,30,75,174,199,837]}) df2=pd.DataFrame({'y':[410,400,170,170,100],'x':[0,8.8,39.8,80.6,83.7]}) </code></pre> <p><a href="https://i.stack.imgur.com/dlHQO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dlHQO.png" alt="enter image description here" /></a></p> <p>How to find the intersection point of these two lines and put the point in dataframe?</p>
70,022,316
"2021-11-18T04:10:37.823000"
1
0
1
521
python|pandas
<ol> <li>Get all lines' equations <code>y = mx + beta</code> between consecutive data points in each dataframe</li> <li>For each of the above lines <code>(m, beta)</code>, also keep the boundaries <code>(x,y)</code></li> <li>Find intersection points <code>x = (beta2 - beta1) / (m1 - m2)</code> between lines of the two dataframes</li> <li>Accept only the intersection points that are between the described boundaries</li> </ol> <pre><code>import pandas as pd import numpy as np import matplotlib.pyplot as plt # our dataframes df1 = pd.DataFrame( {&quot;y&quot;: [262, 306, 360, 360, 380, 380], &quot;x&quot;: [0, 30, 75, 174, 199, 837]} ) df2 = pd.DataFrame({&quot;y&quot;: [410, 400, 170, 170, 100], &quot;x&quot;: [0, 8.8, 39.8, 80.6, 83.7]}) def find_intersection(first_line, second_line): &quot;&quot;&quot; this functions takes the variables m and beta of two lines and returns the intersection point coordinates x, y &quot;&quot;&quot; x = (second_line[1] - first_line[1]) / (first_line[0] - second_line[0]) y = first_line[0] * x + first_line[1] return x, y def find_m_beta(x1, y1, x2, y2): &quot;&quot;&quot; this functions calculates m and beta of a line given two coordinates x1,y1 and x2,y2 &quot;&quot;&quot; if x2 != x1: m = (y2 - y1) / (x2 - x1) else: m = 0 beta = y2 - m * x2 return m, beta def find_m_beta_bounds(df): &quot;&quot;&quot; this function finds the equations of all lines that can be created by the given dataframe. It only calculates lines of points that are consequent. For example: index x y 0 0 1 1 1 2 2 3 4 Given the above dataframe, the function will find two equations: namely m and beta of the line between points with indexes 0 and 1 and m and beta of the line between points with indexes 1 and 2. It will also return the boundaries of these lines &quot;&quot;&quot; data_points = df.to_numpy() vars = [] bounds_x, bounds_y = [], [] # find m, beta and bounds for each line in df for idx, item in enumerate(data_points): if idx == len(data_points) - 1: break x1 = item[1] y1 = item[0] x2 = data_points[idx + 1][1] y2 = data_points[idx + 1][0] m, beta = find_m_beta(x1, y1, x2, y2) vars.append([m, beta]) bounds_x.append([min([x1, x2]), max([x1, x2])]) bounds_y.append([min([y1, y2]), max([y1, y2])]) return vars, bounds_x, bounds_y # get vars (m,beta) and bounds (x,y) for each line in df1 and in df2 # where y = mx +beta vars_df1, bounds_x_df1, bounds_y_df1 = find_m_beta_bounds(df1) vars_df2, bounds_x_df2, bounds_y_df2 = find_m_beta_bounds(df2) # find interesections of all lines of df1 and df2 all_intersections_x, all_intersections_y = [], [] for idx, item in enumerate(vars_df1): for idx_, item_ in enumerate(vars_df2): x, y = find_intersection(item, item_) # accept intersection only if (x,y) are in bounds of both investigated lines if ( x &gt;= bounds_x_df1[idx][0] and x &lt;= bounds_x_df1[idx][1] and y &gt;= bounds_y_df1[idx][0] and y &lt;= bounds_y_df1[idx][1] and x &gt;= bounds_x_df2[idx_][0] and x &lt;= bounds_x_df2[idx_][1] and y &gt;= bounds_y_df2[idx_][0] and y &lt;= bounds_y_df2[idx_][1] ): all_intersections_x.append(x) all_intersections_y.append(y) # plot fig = plt.figure(figsize=(10, 6)) ax = plt.gca() ax.plot(df1[&quot;x&quot;], df1[&quot;y&quot;], color=&quot;red&quot;) ax.plot(df2[&quot;x&quot;], df2[&quot;y&quot;], color=&quot;green&quot;) ax.scatter(all_intersections_x, all_intersections_y) plt.show() </code></pre> <p>Result <a href="https://i.stack.imgur.com/Fg40i.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Fg40i.png" alt="Result" /></a></p>
"2021-11-18T15:14:46.090000"
2
https://pandas.pydata.org/docs/reference/api/pandas.Index.intersection.html
Get all lines' equations y = mx + beta between consecutive data points in each dataframe For each of the above lines (m, beta), also keep the boundaries (x,y) Find intersection points x = (beta2 - beta1) / (m1 - m2) between lines of the two dataframes Accept only the intersection points that are between the described boundaries import pandas as pd import numpy as np import matplotlib.pyplot as plt # our dataframes df1 = pd.DataFrame( {"y": [262, 306, 360, 360, 380, 380], "x": [0, 30, 75, 174, 199, 837]} ) df2 = pd.DataFrame({"y": [410, 400, 170, 170, 100], "x": [0, 8.8, 39.8, 80.6, 83.7]}) def find_intersection(first_line, second_line): """ this functions takes the variables m and beta of two lines and returns the intersection point coordinates x, y """ x = (second_line[1] - first_line[1]) / (first_line[0] - second_line[0]) y = first_line[0] * x + first_line[1] return x, y def find_m_beta(x1, y1, x2, y2): """ this functions calculates m and beta of a line given two coordinates x1,y1 and x2,y2 """ if x2 != x1: m = (y2 - y1) / (x2 - x1) else: m = 0 beta = y2 - m * x2 return m, beta def find_m_beta_bounds(df): """ this function finds the equations of all lines that can be created by the given dataframe. It only calculates lines of points that are consequent. For example: index x y 0 0 1 1 1 2 2 3 4 Given the above dataframe, the function will find two equations: namely m and beta of the line between points with indexes 0 and 1 and m and beta of the line between points with indexes 1 and 2. It will also return the boundaries of these lines """ data_points = df.to_numpy() vars = [] bounds_x, bounds_y = [], [] # find m, beta and bounds for each line in df for idx, item in enumerate(data_points): if idx == len(data_points) - 1: break x1 = item[1] y1 = item[0] x2 = data_points[idx + 1][1] y2 = data_points[idx + 1][0] m, beta = find_m_beta(x1, y1, x2, y2) vars.append([m, beta]) bounds_x.append([min([x1, x2]), max([x1, x2])]) bounds_y.append([min([y1, y2]), max([y1, y2])]) return vars, bounds_x, bounds_y # get vars (m,beta) and bounds (x,y) for each line in df1 and in df2 # where y = mx +beta vars_df1, bounds_x_df1, bounds_y_df1 = find_m_beta_bounds(df1) vars_df2, bounds_x_df2, bounds_y_df2 = find_m_beta_bounds(df2) # find interesections of all lines of df1 and df2 all_intersections_x, all_intersections_y = [], [] for idx, item in enumerate(vars_df1): for idx_, item_ in enumerate(vars_df2): x, y = find_intersection(item, item_) # accept intersection only if (x,y) are in bounds of both investigated lines if ( x >= bounds_x_df1[idx][0] and x <= bounds_x_df1[idx][1] and y >= bounds_y_df1[idx][0] and y <= bounds_y_df1[idx][1] and x >= bounds_x_df2[idx_][0] and x <= bounds_x_df2[idx_][1] and y >= bounds_y_df2[idx_][0] and y <= bounds_y_df2[idx_][1] ): all_intersections_x.append(x) all_intersections_y.append(y) # plot fig = plt.figure(figsize=(10, 6)) ax = plt.gca() ax.plot(df1["x"], df1["y"], color="red") ax.plot(df2["x"], df2["y"], color="green") ax.scatter(all_intersections_x, all_intersections_y) plt.show() Result
0
3,528
pandas find intersection point of two lines in dataframe Tow lines with its points: df1=pd.DataFrame({'y':[262,306,360,360,380,380],'x':[0,30,75,174,199,837]}) df2=pd.DataFrame({'y':[410,400,170,170,100],'x':[0,8.8,39.8,80.6,83.7]}) How to find the intersection point of these two lines and put the point in dataframe?
/ all _ intersections _ x. append ( x ) all _ intersections _ y. append ( y ) # plot fig = plt. figure ( figsize = ( 10, 6 ) ) ax = plt. gca ( ) ax. plot ( df1 [ " x " ], df1 [ " y " ], color = " red " ) ax. plot ( df2 [ " x " ], df2 [ " y " ], color = " green " ) ax. scatter ( all _ intersections _ x, all _ intersections _ y ) plt. show /
Get all lines' equations y = mx + beta between consecutive data points in each dataframe For each of the above lines (m, beta), also keep the boundaries (x,y) Find intersection points x = (beta2 - beta1) / (m1 - m2) between lines of the two dataframes Accept only the intersection points that are between the described boundaries import pandas as pd import numpy as np import matplotlib.pyplot as plt # our dataframes df1 = pd.DataFrame( {"y": [262, 306, 360, 360, 380, 380], "x": [0, 30, 75, 174, 199, 837]} ) df2 = pd.DataFrame({"y": [410, 400, 170, 170, 100], "x": [0, 8.8, 39.8, 80.6, 83.7]}) def find_intersection(first_line, second_line): """ this functions takes the variables m and beta of two lines and returns the intersection point coordinates x, y """ x = (second_line[1] - first_line[1]) / (first_line[0] - second_line[0]) y = first_line[0] * x + first_line[1] return x, y def find_m_beta(x1, y1, x2, y2): """ this functions calculates m and beta of a line given two coordinates x1,y1 and x2,y2 """ if x2 != x1: m = (y2 - y1) / (x2 - x1) else: m = 0 beta = y2 - m * x2 return m, beta def find_m_beta_bounds(df): """ this function finds the equations of all lines that can be created by the given dataframe. It only calculates lines of points that are consequent. For example: index x y 0 0 1 1 1 2 2 3 4 Given the above dataframe, the function will find two equations: namely m and beta of the line between points with indexes 0 and 1 and m and beta of the line between points with indexes 1 and 2. It will also return the boundaries of these lines """ data_points = df.to_numpy() vars = [] bounds_x, bounds_y = [], [] # find m, beta and bounds for each line in df for idx, item in enumerate(data_points): if idx == len(data_points) - 1: break x1 = item[1] y1 = item[0] x2 = data_points[idx + 1][1] y2 = data_points[idx + 1][0] m, beta = find_m_beta(x1, y1, x2, y2) vars.append([m, beta]) bounds_x.append([min([x1, x2]), max([x1, x2])]) bounds_y.append([min([y1, y2]), max([y1, y2])]) return vars, bounds_x, bounds_y # get vars (m,beta) and bounds (x,y) for each line in df1 and in df2 # where y = mx +beta vars_df1, bounds_x_df1, bounds_y_df1 = find_m_beta_bounds(df1) vars_df2, bounds_x_df2, bounds_y_df2 = find_m_beta_bounds(df2) # find interesections of all lines of df1 and df2 all_intersections_x, all_intersections_y = [], [] for idx, item in enumerate(vars_df1): for idx_, item_ in enumerate(vars_df2): x, y = find_intersection(item, item_) # accept intersection only if (x,y) are in bounds of both investigated lines if ( x >= bounds_x_df1[idx][0] and x <= bounds_x_df1[idx][1] and y >= bounds_y_df1[idx][0] and y <= bounds_y_df1[idx][1] and x >= bounds_x_df2[idx_][0] and x <= bounds_x_df2[idx_][1] and y >= bounds_y_df2[idx_][0] and y <= bounds_y_df2[idx_][1] ): all_intersections_x.append(x) all_intersections_y.append(y) # plot fig = plt.figure(figsize=(10, 6)) ax = plt.gca() ax.plot(df1["x"], df1["y"], color="red") ax.plot(df2["x"], df2["y"], color="green") ax.scatter(all_intersections_x, all_intersections_y) plt.show() Result
66,942,772
Including only ids where there is a 2 flags for one id (PANDAS)
<p>I have the following dataframe called <code>df</code> and i want to subset the dataframe to only ids where there is a 1 and a 0 for the column <code>is_signup</code>. The following example would remove <code>id</code> = 1 because it only has a 1 (and not 0).</p> <pre><code>id tag is_signup 1 Button 1 1 Circle 1 2 Button 1 2 Circle 0 2 Diamond 1 3 Circle 0 3 Button 1 </code></pre> <p>expected output:</p> <pre><code>id tag is_signup 2 Button 1 2 Circle 0 2 Diamond 1 3 Circle 0 3 Button 1 </code></pre> <p>How can I do this? I think a groupby would be helpful? but not sure how to formally do it</p>
66,942,921
"2021-04-04T15:18:15.710000"
2
null
2
32
python|pandas
<p>You can also use a <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.core.groupby.DataFrameGroupBy.filter.html" rel="nofollow noreferrer"><strong><code>groupby-filter</code></strong></a> to check <code>is_signup.nunique()</code> directly:</p> <pre class="lang-py prettyprint-override"><code>df.groupby('id').filter(lambda x: x.is_signup.nunique() == 2) # id tag is_signup # 2 2 Button 1 # 3 2 Circle 0 # 4 2 Diamond 1 # 5 3 Circle 0 # 6 3 Button 1 </code></pre>
"2021-04-04T15:32:42.927000"
2
https://pandas.pydata.org/docs/reference/api/pandas.Series.replace.html
pandas.Series.replace# pandas.Series.replace# Series.replace(to_replace=None, value=_NoDefault.no_default, *, inplace=False, limit=None, regex=False, method=_NoDefault.no_default)[source]# You can also use a groupby-filter to check is_signup.nunique() directly: df.groupby('id').filter(lambda x: x.is_signup.nunique() == 2) # id tag is_signup # 2 2 Button 1 # 3 2 Circle 0 # 4 2 Diamond 1 # 5 3 Circle 0 # 6 3 Button 1 Replace values given in to_replace with value. Values of the Series are replaced with other values dynamically. This differs from updating with .loc or .iloc, which require you to specify a location to update with some value. Parameters to_replacestr, regex, list, dict, Series, int, float, or NoneHow to find the values that will be replaced. numeric, str or regex: numeric: numeric values equal to to_replace will be replaced with value str: string exactly matching to_replace will be replaced with value regex: regexs matching to_replace will be replaced with value list of str, regex, or numeric: First, if to_replace and value are both lists, they must be the same length. Second, if regex=True then all of the strings in both lists will be interpreted as regexs otherwise they will match directly. This doesn’t matter much for value since there are only a few possible substitution regexes you can use. str, regex and numeric rules apply as above. dict: Dicts can be used to specify different replacement values for different existing values. For example, {'a': 'b', 'y': 'z'} replaces the value ‘a’ with ‘b’ and ‘y’ with ‘z’. To use a dict in this way, the optional value parameter should not be given. For a DataFrame a dict can specify that different values should be replaced in different columns. For example, {'a': 1, 'b': 'z'} looks for the value 1 in column ‘a’ and the value ‘z’ in column ‘b’ and replaces these values with whatever is specified in value. The value parameter should not be None in this case. You can treat this as a special case of passing two lists except that you are specifying the column to search in. For a DataFrame nested dictionaries, e.g., {'a': {'b': np.nan}}, are read as follows: look in column ‘a’ for the value ‘b’ and replace it with NaN. The optional value parameter should not be specified to use a nested dict in this way. You can nest regular expressions as well. Note that column names (the top-level dictionary keys in a nested dictionary) cannot be regular expressions. None: This means that the regex argument must be a string, compiled regular expression, or list, dict, ndarray or Series of such elements. If value is also None then this must be a nested dictionary or Series. See the examples section for examples of each of these. valuescalar, dict, list, str, regex, default NoneValue to replace any values matching to_replace with. For a DataFrame a dict of values can be used to specify which value to use for each column (columns not in the dict will not be filled). Regular expressions, strings and lists or dicts of such objects are also allowed. inplacebool, default FalseIf True, performs operation inplace and returns None. limitint, default NoneMaximum size gap to forward or backward fill. regexbool or same types as to_replace, default FalseWhether to interpret to_replace and/or value as regular expressions. If this is True then to_replace must be a string. Alternatively, this could be a regular expression or a list, dict, or array of regular expressions in which case to_replace must be None. method{‘pad’, ‘ffill’, ‘bfill’}The method to use when for replacement, when to_replace is a scalar, list or tuple and value is None. Changed in version 0.23.0: Added to DataFrame. Returns SeriesObject after replacement. Raises AssertionError If regex is not a bool and to_replace is not None. TypeError If to_replace is not a scalar, array-like, dict, or None If to_replace is a dict and value is not a list, dict, ndarray, or Series If to_replace is None and regex is not compilable into a regular expression or is a list, dict, ndarray, or Series. When replacing multiple bool or datetime64 objects and the arguments to to_replace does not match the type of the value being replaced ValueError If a list or an ndarray is passed to to_replace and value but they are not the same length. See also Series.fillnaFill NA values. Series.whereReplace values based on boolean condition. Series.str.replaceSimple string replacement. Notes Regex substitution is performed under the hood with re.sub. The rules for substitution for re.sub are the same. Regular expressions will only substitute on strings, meaning you cannot provide, for example, a regular expression matching floating point numbers and expect the columns in your frame that have a numeric dtype to be matched. However, if those floating point numbers are strings, then you can do this. This method has a lot of options. You are encouraged to experiment and play with this method to gain intuition about how it works. When dict is used as the to_replace value, it is like key(s) in the dict are the to_replace part and value(s) in the dict are the value parameter. Examples Scalar `to_replace` and `value` >>> s = pd.Series([1, 2, 3, 4, 5]) >>> s.replace(1, 5) 0 5 1 2 2 3 3 4 4 5 dtype: int64 >>> df = pd.DataFrame({'A': [0, 1, 2, 3, 4], ... 'B': [5, 6, 7, 8, 9], ... 'C': ['a', 'b', 'c', 'd', 'e']}) >>> df.replace(0, 5) A B C 0 5 5 a 1 1 6 b 2 2 7 c 3 3 8 d 4 4 9 e List-like `to_replace` >>> df.replace([0, 1, 2, 3], 4) A B C 0 4 5 a 1 4 6 b 2 4 7 c 3 4 8 d 4 4 9 e >>> df.replace([0, 1, 2, 3], [4, 3, 2, 1]) A B C 0 4 5 a 1 3 6 b 2 2 7 c 3 1 8 d 4 4 9 e >>> s.replace([1, 2], method='bfill') 0 3 1 3 2 3 3 4 4 5 dtype: int64 dict-like `to_replace` >>> df.replace({0: 10, 1: 100}) A B C 0 10 5 a 1 100 6 b 2 2 7 c 3 3 8 d 4 4 9 e >>> df.replace({'A': 0, 'B': 5}, 100) A B C 0 100 100 a 1 1 6 b 2 2 7 c 3 3 8 d 4 4 9 e >>> df.replace({'A': {0: 100, 4: 400}}) A B C 0 100 5 a 1 1 6 b 2 2 7 c 3 3 8 d 4 400 9 e Regular expression `to_replace` >>> df = pd.DataFrame({'A': ['bat', 'foo', 'bait'], ... 'B': ['abc', 'bar', 'xyz']}) >>> df.replace(to_replace=r'^ba.$', value='new', regex=True) A B 0 new abc 1 foo new 2 bait xyz >>> df.replace({'A': r'^ba.$'}, {'A': 'new'}, regex=True) A B 0 new abc 1 foo bar 2 bait xyz >>> df.replace(regex=r'^ba.$', value='new') A B 0 new abc 1 foo new 2 bait xyz >>> df.replace(regex={r'^ba.$': 'new', 'foo': 'xyz'}) A B 0 new abc 1 xyz new 2 bait xyz >>> df.replace(regex=[r'^ba.$', 'foo'], value='new') A B 0 new abc 1 new new 2 bait xyz Compare the behavior of s.replace({'a': None}) and s.replace('a', None) to understand the peculiarities of the to_replace parameter: >>> s = pd.Series([10, 'a', 'a', 'b', 'a']) When one uses a dict as the to_replace value, it is like the value(s) in the dict are equal to the value parameter. s.replace({'a': None}) is equivalent to s.replace(to_replace={'a': None}, value=None, method=None): >>> s.replace({'a': None}) 0 10 1 None 2 None 3 b 4 None dtype: object When value is not explicitly passed and to_replace is a scalar, list or tuple, replace uses the method parameter (default ‘pad’) to do the replacement. So this is why the ‘a’ values are being replaced by 10 in rows 1 and 2 and ‘b’ in row 4 in this case. >>> s.replace('a') 0 10 1 10 2 10 3 b 4 b dtype: object On the other hand, if None is explicitly passed for value, it will be respected: >>> s.replace('a', None) 0 10 1 None 2 None 3 b 4 None dtype: object Changed in version 1.4.0: Previously the explicit None was silently ignored.
193
497
Including only ids where there is a 2 flags for one id (PANDAS) I have the following dataframe called df and i want to subset the dataframe to only ids where there is a 1 and a 0 for the column is_signup. The following example would remove id = 1 because it only has a 1 (and not 0). id tag is_signup 1 Button 1 1 Circle 1 2 Button 1 2 Circle 0 2 Diamond 1 3 Circle 0 3 Button 1 expected output: id tag is_signup 2 Button 1 2 Circle 0 2 Diamond 1 3 Circle 0 3 Button 1 How can I do this? I think a groupby would be helpful? but not sure how to formally do it
You can also use a groupby-filter to check is_signup.nunique() directly: df.groupby('id').filter(lambda x: x.is_signup.nunique() == 2) # id tag is_signup # 2 2 Button 1 # 3 2 Circle 0 # 4 2 Diamond 1 # 5 3 Circle 0 # 6 3 Button 1
67,688,257
2 condition in pandas dataFrame indexes
<p>I have this rows filtering:</p> <pre><code>idx = dataset.data.groupby(['a', 'b'])['c'].transform(max) == dataset.data['c'] dataset.data = dataset.data[idx] </code></pre> <p>For this dataset:</p> <pre><code>a | b | c | d 0 | 0 | 1 | True 0 | 0 | 2 | True 0 | 1 | 3 | True 0 | 2 | 4 | False 0 | 2 | 5 | False </code></pre> <p>I'll get:</p> <pre><code>a | b | c | d 0 | 0 | 1 | True 0 | 1 | 3 | True 0 | 2 | 5 | False </code></pre> <p>I want to add for this condition that removes only rows that their field 'd' is false, so in the above example I'll get:</p> <pre><code>a | b | c | d 0 | 0 | 1 | True 0 | 0 | 2 | True 0 | 1 | 3 | True 0 | 2 | 5 | False </code></pre> <p>Can someone help me add it, please?</p> <p>Thanks!</p>
67,688,581
"2021-05-25T12:48:20.777000"
1
null
1
37
python|pandas
<p>IIUC, keep rows where <code>'c'</code> is the <code>max</code> <strong>or</strong> <code>d</code> is <code>True</code>:</p> <pre><code>import pandas as pd df = pd.DataFrame({ 'a': [0, 0, 0, 0, 0], 'b': [0, 0, 1, 2, 2], 'c': [1, 2, 3, 4, 5], 'd': [True, True, True, False, False] }) # Max value of C c1 = df.groupby(['a', 'b'])['c'].transform(max) == df['c'] # D is True c2 = df.d # Or together idx = c1 | c2 print(df[idx]) </code></pre> <p><code>df[idx]</code>:</p> <pre><code> a b c d 0 0 0 1 True 1 0 0 2 True 2 0 1 3 True 4 0 2 5 False </code></pre> <hr/> <p>The one-liner:</p> <pre><code>df[df.groupby(['a', 'b'])['c'].transform(max).eq(df['c']) | df['d']] </code></pre>
"2021-05-25T13:05:20.223000"
2
https://pandas.pydata.org/docs/user_guide/indexing.html
Indexing and selecting data# Indexing and selecting data# The axis labeling information in pandas objects serves many purposes: Identifies data (i.e. provides metadata) using known indicators, important for analysis, visualization, and interactive console display. Enables automatic and explicit data alignment. Allows intuitive getting and setting of subsets of the data set. In this section, we will focus on the final point: namely, how to slice, dice, and generally get and set subsets of pandas objects. The primary focus will be on Series and DataFrame as they have received more development attention in this area. Note IIUC, keep rows where 'c' is the max or d is True: import pandas as pd df = pd.DataFrame({ 'a': [0, 0, 0, 0, 0], 'b': [0, 0, 1, 2, 2], 'c': [1, 2, 3, 4, 5], 'd': [True, True, True, False, False] }) # Max value of C c1 = df.groupby(['a', 'b'])['c'].transform(max) == df['c'] # D is True c2 = df.d # Or together idx = c1 | c2 print(df[idx]) df[idx]: a b c d 0 0 0 1 True 1 0 0 2 True 2 0 1 3 True 4 0 2 5 False The one-liner: df[df.groupby(['a', 'b'])['c'].transform(max).eq(df['c']) | df['d']] The Python and NumPy indexing operators [] and attribute operator . provide quick and easy access to pandas data structures across a wide range of use cases. This makes interactive work intuitive, as there’s little new to learn if you already know how to deal with Python dictionaries and NumPy arrays. However, since the type of the data to be accessed isn’t known in advance, directly using standard operators has some optimization limits. For production code, we recommended that you take advantage of the optimized pandas data access methods exposed in this chapter. Warning Whether a copy or a reference is returned for a setting operation, may depend on the context. This is sometimes called chained assignment and should be avoided. See Returning a View versus Copy. See the MultiIndex / Advanced Indexing for MultiIndex and more advanced indexing documentation. See the cookbook for some advanced strategies. Different choices for indexing# Object selection has had a number of user-requested additions in order to support more explicit location based indexing. pandas now supports three types of multi-axis indexing. .loc is primarily label based, but may also be used with a boolean array. .loc will raise KeyError when the items are not found. Allowed inputs are: A single label, e.g. 5 or 'a' (Note that 5 is interpreted as a label of the index. This use is not an integer position along the index.). A list or array of labels ['a', 'b', 'c']. A slice object with labels 'a':'f' (Note that contrary to usual Python slices, both the start and the stop are included, when present in the index! See Slicing with labels and Endpoints are inclusive.) A boolean array (any NA values will be treated as False). A callable function with one argument (the calling Series or DataFrame) and that returns valid output for indexing (one of the above). See more at Selection by Label. .iloc is primarily integer position based (from 0 to length-1 of the axis), but may also be used with a boolean array. .iloc will raise IndexError if a requested indexer is out-of-bounds, except slice indexers which allow out-of-bounds indexing. (this conforms with Python/NumPy slice semantics). Allowed inputs are: An integer e.g. 5. A list or array of integers [4, 3, 0]. A slice object with ints 1:7. A boolean array (any NA values will be treated as False). A callable function with one argument (the calling Series or DataFrame) and that returns valid output for indexing (one of the above). See more at Selection by Position, Advanced Indexing and Advanced Hierarchical. .loc, .iloc, and also [] indexing can accept a callable as indexer. See more at Selection By Callable. Getting values from an object with multi-axes selection uses the following notation (using .loc as an example, but the following applies to .iloc as well). Any of the axes accessors may be the null slice :. Axes left out of the specification are assumed to be :, e.g. p.loc['a'] is equivalent to p.loc['a', :]. Object Type Indexers Series s.loc[indexer] DataFrame df.loc[row_indexer,column_indexer] Basics# As mentioned when introducing the data structures in the last section, the primary function of indexing with [] (a.k.a. __getitem__ for those familiar with implementing class behavior in Python) is selecting out lower-dimensional slices. The following table shows return type values when indexing pandas objects with []: Object Type Selection Return Value Type Series series[label] scalar value DataFrame frame[colname] Series corresponding to colname Here we construct a simple time series data set to use for illustrating the indexing functionality: In [1]: dates = pd.date_range('1/1/2000', periods=8) In [2]: df = pd.DataFrame(np.random.randn(8, 4), ...: index=dates, columns=['A', 'B', 'C', 'D']) ...: In [3]: df Out[3]: A B C D 2000-01-01 0.469112 -0.282863 -1.509059 -1.135632 2000-01-02 1.212112 -0.173215 0.119209 -1.044236 2000-01-03 -0.861849 -2.104569 -0.494929 1.071804 2000-01-04 0.721555 -0.706771 -1.039575 0.271860 2000-01-05 -0.424972 0.567020 0.276232 -1.087401 2000-01-06 -0.673690 0.113648 -1.478427 0.524988 2000-01-07 0.404705 0.577046 -1.715002 -1.039268 2000-01-08 -0.370647 -1.157892 -1.344312 0.844885 Note None of the indexing functionality is time series specific unless specifically stated. Thus, as per above, we have the most basic indexing using []: In [4]: s = df['A'] In [5]: s[dates[5]] Out[5]: -0.6736897080883706 You can pass a list of columns to [] to select columns in that order. If a column is not contained in the DataFrame, an exception will be raised. Multiple columns can also be set in this manner: In [6]: df Out[6]: A B C D 2000-01-01 0.469112 -0.282863 -1.509059 -1.135632 2000-01-02 1.212112 -0.173215 0.119209 -1.044236 2000-01-03 -0.861849 -2.104569 -0.494929 1.071804 2000-01-04 0.721555 -0.706771 -1.039575 0.271860 2000-01-05 -0.424972 0.567020 0.276232 -1.087401 2000-01-06 -0.673690 0.113648 -1.478427 0.524988 2000-01-07 0.404705 0.577046 -1.715002 -1.039268 2000-01-08 -0.370647 -1.157892 -1.344312 0.844885 In [7]: df[['B', 'A']] = df[['A', 'B']] In [8]: df Out[8]: A B C D 2000-01-01 -0.282863 0.469112 -1.509059 -1.135632 2000-01-02 -0.173215 1.212112 0.119209 -1.044236 2000-01-03 -2.104569 -0.861849 -0.494929 1.071804 2000-01-04 -0.706771 0.721555 -1.039575 0.271860 2000-01-05 0.567020 -0.424972 0.276232 -1.087401 2000-01-06 0.113648 -0.673690 -1.478427 0.524988 2000-01-07 0.577046 0.404705 -1.715002 -1.039268 2000-01-08 -1.157892 -0.370647 -1.344312 0.844885 You may find this useful for applying a transform (in-place) to a subset of the columns. Warning pandas aligns all AXES when setting Series and DataFrame from .loc, and .iloc. This will not modify df because the column alignment is before value assignment. In [9]: df[['A', 'B']] Out[9]: A B 2000-01-01 -0.282863 0.469112 2000-01-02 -0.173215 1.212112 2000-01-03 -2.104569 -0.861849 2000-01-04 -0.706771 0.721555 2000-01-05 0.567020 -0.424972 2000-01-06 0.113648 -0.673690 2000-01-07 0.577046 0.404705 2000-01-08 -1.157892 -0.370647 In [10]: df.loc[:, ['B', 'A']] = df[['A', 'B']] In [11]: df[['A', 'B']] Out[11]: A B 2000-01-01 -0.282863 0.469112 2000-01-02 -0.173215 1.212112 2000-01-03 -2.104569 -0.861849 2000-01-04 -0.706771 0.721555 2000-01-05 0.567020 -0.424972 2000-01-06 0.113648 -0.673690 2000-01-07 0.577046 0.404705 2000-01-08 -1.157892 -0.370647 The correct way to swap column values is by using raw values: In [12]: df.loc[:, ['B', 'A']] = df[['A', 'B']].to_numpy() In [13]: df[['A', 'B']] Out[13]: A B 2000-01-01 0.469112 -0.282863 2000-01-02 1.212112 -0.173215 2000-01-03 -0.861849 -2.104569 2000-01-04 0.721555 -0.706771 2000-01-05 -0.424972 0.567020 2000-01-06 -0.673690 0.113648 2000-01-07 0.404705 0.577046 2000-01-08 -0.370647 -1.157892 Attribute access# You may access an index on a Series or column on a DataFrame directly as an attribute: In [14]: sa = pd.Series([1, 2, 3], index=list('abc')) In [15]: dfa = df.copy() In [16]: sa.b Out[16]: 2 In [17]: dfa.A Out[17]: 2000-01-01 0.469112 2000-01-02 1.212112 2000-01-03 -0.861849 2000-01-04 0.721555 2000-01-05 -0.424972 2000-01-06 -0.673690 2000-01-07 0.404705 2000-01-08 -0.370647 Freq: D, Name: A, dtype: float64 In [18]: sa.a = 5 In [19]: sa Out[19]: a 5 b 2 c 3 dtype: int64 In [20]: dfa.A = list(range(len(dfa.index))) # ok if A already exists In [21]: dfa Out[21]: A B C D 2000-01-01 0 -0.282863 -1.509059 -1.135632 2000-01-02 1 -0.173215 0.119209 -1.044236 2000-01-03 2 -2.104569 -0.494929 1.071804 2000-01-04 3 -0.706771 -1.039575 0.271860 2000-01-05 4 0.567020 0.276232 -1.087401 2000-01-06 5 0.113648 -1.478427 0.524988 2000-01-07 6 0.577046 -1.715002 -1.039268 2000-01-08 7 -1.157892 -1.344312 0.844885 In [22]: dfa['A'] = list(range(len(dfa.index))) # use this form to create a new column In [23]: dfa Out[23]: A B C D 2000-01-01 0 -0.282863 -1.509059 -1.135632 2000-01-02 1 -0.173215 0.119209 -1.044236 2000-01-03 2 -2.104569 -0.494929 1.071804 2000-01-04 3 -0.706771 -1.039575 0.271860 2000-01-05 4 0.567020 0.276232 -1.087401 2000-01-06 5 0.113648 -1.478427 0.524988 2000-01-07 6 0.577046 -1.715002 -1.039268 2000-01-08 7 -1.157892 -1.344312 0.844885 Warning You can use this access only if the index element is a valid Python identifier, e.g. s.1 is not allowed. See here for an explanation of valid identifiers. The attribute will not be available if it conflicts with an existing method name, e.g. s.min is not allowed, but s['min'] is possible. Similarly, the attribute will not be available if it conflicts with any of the following list: index, major_axis, minor_axis, items. In any of these cases, standard indexing will still work, e.g. s['1'], s['min'], and s['index'] will access the corresponding element or column. If you are using the IPython environment, you may also use tab-completion to see these accessible attributes. You can also assign a dict to a row of a DataFrame: In [24]: x = pd.DataFrame({'x': [1, 2, 3], 'y': [3, 4, 5]}) In [25]: x.iloc[1] = {'x': 9, 'y': 99} In [26]: x Out[26]: x y 0 1 3 1 9 99 2 3 5 You can use attribute access to modify an existing element of a Series or column of a DataFrame, but be careful; if you try to use attribute access to create a new column, it creates a new attribute rather than a new column. In 0.21.0 and later, this will raise a UserWarning: In [1]: df = pd.DataFrame({'one': [1., 2., 3.]}) In [2]: df.two = [4, 5, 6] UserWarning: Pandas doesn't allow Series to be assigned into nonexistent columns - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute_access In [3]: df Out[3]: one 0 1.0 1 2.0 2 3.0 Slicing ranges# The most robust and consistent way of slicing ranges along arbitrary axes is described in the Selection by Position section detailing the .iloc method. For now, we explain the semantics of slicing using the [] operator. With Series, the syntax works exactly as with an ndarray, returning a slice of the values and the corresponding labels: In [27]: s[:5] Out[27]: 2000-01-01 0.469112 2000-01-02 1.212112 2000-01-03 -0.861849 2000-01-04 0.721555 2000-01-05 -0.424972 Freq: D, Name: A, dtype: float64 In [28]: s[::2] Out[28]: 2000-01-01 0.469112 2000-01-03 -0.861849 2000-01-05 -0.424972 2000-01-07 0.404705 Freq: 2D, Name: A, dtype: float64 In [29]: s[::-1] Out[29]: 2000-01-08 -0.370647 2000-01-07 0.404705 2000-01-06 -0.673690 2000-01-05 -0.424972 2000-01-04 0.721555 2000-01-03 -0.861849 2000-01-02 1.212112 2000-01-01 0.469112 Freq: -1D, Name: A, dtype: float64 Note that setting works as well: In [30]: s2 = s.copy() In [31]: s2[:5] = 0 In [32]: s2 Out[32]: 2000-01-01 0.000000 2000-01-02 0.000000 2000-01-03 0.000000 2000-01-04 0.000000 2000-01-05 0.000000 2000-01-06 -0.673690 2000-01-07 0.404705 2000-01-08 -0.370647 Freq: D, Name: A, dtype: float64 With DataFrame, slicing inside of [] slices the rows. This is provided largely as a convenience since it is such a common operation. In [33]: df[:3] Out[33]: A B C D 2000-01-01 0.469112 -0.282863 -1.509059 -1.135632 2000-01-02 1.212112 -0.173215 0.119209 -1.044236 2000-01-03 -0.861849 -2.104569 -0.494929 1.071804 In [34]: df[::-1] Out[34]: A B C D 2000-01-08 -0.370647 -1.157892 -1.344312 0.844885 2000-01-07 0.404705 0.577046 -1.715002 -1.039268 2000-01-06 -0.673690 0.113648 -1.478427 0.524988 2000-01-05 -0.424972 0.567020 0.276232 -1.087401 2000-01-04 0.721555 -0.706771 -1.039575 0.271860 2000-01-03 -0.861849 -2.104569 -0.494929 1.071804 2000-01-02 1.212112 -0.173215 0.119209 -1.044236 2000-01-01 0.469112 -0.282863 -1.509059 -1.135632 Selection by label# Warning Whether a copy or a reference is returned for a setting operation, may depend on the context. This is sometimes called chained assignment and should be avoided. See Returning a View versus Copy. Warning .loc is strict when you present slicers that are not compatible (or convertible) with the index type. For example using integers in a DatetimeIndex. These will raise a TypeError. In [35]: dfl = pd.DataFrame(np.random.randn(5, 4), ....: columns=list('ABCD'), ....: index=pd.date_range('20130101', periods=5)) ....: In [36]: dfl Out[36]: A B C D 2013-01-01 1.075770 -0.109050 1.643563 -1.469388 2013-01-02 0.357021 -0.674600 -1.776904 -0.968914 2013-01-03 -1.294524 0.413738 0.276662 -0.472035 2013-01-04 -0.013960 -0.362543 -0.006154 -0.923061 2013-01-05 0.895717 0.805244 -1.206412 2.565646 In [4]: dfl.loc[2:3] TypeError: cannot do slice indexing on <class 'pandas.tseries.index.DatetimeIndex'> with these indexers [2] of <type 'int'> String likes in slicing can be convertible to the type of the index and lead to natural slicing. In [37]: dfl.loc['20130102':'20130104'] Out[37]: A B C D 2013-01-02 0.357021 -0.674600 -1.776904 -0.968914 2013-01-03 -1.294524 0.413738 0.276662 -0.472035 2013-01-04 -0.013960 -0.362543 -0.006154 -0.923061 Warning Changed in version 1.0.0. pandas will raise a KeyError if indexing with a list with missing labels. See list-like Using loc with missing keys in a list is Deprecated. pandas provides a suite of methods in order to have purely label based indexing. This is a strict inclusion based protocol. Every label asked for must be in the index, or a KeyError will be raised. When slicing, both the start bound AND the stop bound are included, if present in the index. Integers are valid labels, but they refer to the label and not the position. The .loc attribute is the primary access method. The following are valid inputs: A single label, e.g. 5 or 'a' (Note that 5 is interpreted as a label of the index. This use is not an integer position along the index.). A list or array of labels ['a', 'b', 'c']. A slice object with labels 'a':'f' (Note that contrary to usual Python slices, both the start and the stop are included, when present in the index! See Slicing with labels. A boolean array. A callable, see Selection By Callable. In [38]: s1 = pd.Series(np.random.randn(6), index=list('abcdef')) In [39]: s1 Out[39]: a 1.431256 b 1.340309 c -1.170299 d -0.226169 e 0.410835 f 0.813850 dtype: float64 In [40]: s1.loc['c':] Out[40]: c -1.170299 d -0.226169 e 0.410835 f 0.813850 dtype: float64 In [41]: s1.loc['b'] Out[41]: 1.3403088497993827 Note that setting works as well: In [42]: s1.loc['c':] = 0 In [43]: s1 Out[43]: a 1.431256 b 1.340309 c 0.000000 d 0.000000 e 0.000000 f 0.000000 dtype: float64 With a DataFrame: In [44]: df1 = pd.DataFrame(np.random.randn(6, 4), ....: index=list('abcdef'), ....: columns=list('ABCD')) ....: In [45]: df1 Out[45]: A B C D a 0.132003 -0.827317 -0.076467 -1.187678 b 1.130127 -1.436737 -1.413681 1.607920 c 1.024180 0.569605 0.875906 -2.211372 d 0.974466 -2.006747 -0.410001 -0.078638 e 0.545952 -1.219217 -1.226825 0.769804 f -1.281247 -0.727707 -0.121306 -0.097883 In [46]: df1.loc[['a', 'b', 'd'], :] Out[46]: A B C D a 0.132003 -0.827317 -0.076467 -1.187678 b 1.130127 -1.436737 -1.413681 1.607920 d 0.974466 -2.006747 -0.410001 -0.078638 Accessing via label slices: In [47]: df1.loc['d':, 'A':'C'] Out[47]: A B C d 0.974466 -2.006747 -0.410001 e 0.545952 -1.219217 -1.226825 f -1.281247 -0.727707 -0.121306 For getting a cross section using a label (equivalent to df.xs('a')): In [48]: df1.loc['a'] Out[48]: A 0.132003 B -0.827317 C -0.076467 D -1.187678 Name: a, dtype: float64 For getting values with a boolean array: In [49]: df1.loc['a'] > 0 Out[49]: A True B False C False D False Name: a, dtype: bool In [50]: df1.loc[:, df1.loc['a'] > 0] Out[50]: A a 0.132003 b 1.130127 c 1.024180 d 0.974466 e 0.545952 f -1.281247 NA values in a boolean array propagate as False: Changed in version 1.0.2. In [51]: mask = pd.array([True, False, True, False, pd.NA, False], dtype="boolean") In [52]: mask Out[52]: <BooleanArray> [True, False, True, False, <NA>, False] Length: 6, dtype: boolean In [53]: df1[mask] Out[53]: A B C D a 0.132003 -0.827317 -0.076467 -1.187678 c 1.024180 0.569605 0.875906 -2.211372 For getting a value explicitly: # this is also equivalent to ``df1.at['a','A']`` In [54]: df1.loc['a', 'A'] Out[54]: 0.13200317033032932 Slicing with labels# When using .loc with slices, if both the start and the stop labels are present in the index, then elements located between the two (including them) are returned: In [55]: s = pd.Series(list('abcde'), index=[0, 3, 2, 5, 4]) In [56]: s.loc[3:5] Out[56]: 3 b 2 c 5 d dtype: object If at least one of the two is absent, but the index is sorted, and can be compared against start and stop labels, then slicing will still work as expected, by selecting labels which rank between the two: In [57]: s.sort_index() Out[57]: 0 a 2 c 3 b 4 e 5 d dtype: object In [58]: s.sort_index().loc[1:6] Out[58]: 2 c 3 b 4 e 5 d dtype: object However, if at least one of the two is absent and the index is not sorted, an error will be raised (since doing otherwise would be computationally expensive, as well as potentially ambiguous for mixed type indexes). For instance, in the above example, s.loc[1:6] would raise KeyError. For the rationale behind this behavior, see Endpoints are inclusive. In [59]: s = pd.Series(list('abcdef'), index=[0, 3, 2, 5, 4, 2]) In [60]: s.loc[3:5] Out[60]: 3 b 2 c 5 d dtype: object Also, if the index has duplicate labels and either the start or the stop label is duplicated, an error will be raised. For instance, in the above example, s.loc[2:5] would raise a KeyError. For more information about duplicate labels, see Duplicate Labels. Selection by position# Warning Whether a copy or a reference is returned for a setting operation, may depend on the context. This is sometimes called chained assignment and should be avoided. See Returning a View versus Copy. pandas provides a suite of methods in order to get purely integer based indexing. The semantics follow closely Python and NumPy slicing. These are 0-based indexing. When slicing, the start bound is included, while the upper bound is excluded. Trying to use a non-integer, even a valid label will raise an IndexError. The .iloc attribute is the primary access method. The following are valid inputs: An integer e.g. 5. A list or array of integers [4, 3, 0]. A slice object with ints 1:7. A boolean array. A callable, see Selection By Callable. In [61]: s1 = pd.Series(np.random.randn(5), index=list(range(0, 10, 2))) In [62]: s1 Out[62]: 0 0.695775 2 0.341734 4 0.959726 6 -1.110336 8 -0.619976 dtype: float64 In [63]: s1.iloc[:3] Out[63]: 0 0.695775 2 0.341734 4 0.959726 dtype: float64 In [64]: s1.iloc[3] Out[64]: -1.110336102891167 Note that setting works as well: In [65]: s1.iloc[:3] = 0 In [66]: s1 Out[66]: 0 0.000000 2 0.000000 4 0.000000 6 -1.110336 8 -0.619976 dtype: float64 With a DataFrame: In [67]: df1 = pd.DataFrame(np.random.randn(6, 4), ....: index=list(range(0, 12, 2)), ....: columns=list(range(0, 8, 2))) ....: In [68]: df1 Out[68]: 0 2 4 6 0 0.149748 -0.732339 0.687738 0.176444 2 0.403310 -0.154951 0.301624 -2.179861 4 -1.369849 -0.954208 1.462696 -1.743161 6 -0.826591 -0.345352 1.314232 0.690579 8 0.995761 2.396780 0.014871 3.357427 10 -0.317441 -1.236269 0.896171 -0.487602 Select via integer slicing: In [69]: df1.iloc[:3] Out[69]: 0 2 4 6 0 0.149748 -0.732339 0.687738 0.176444 2 0.403310 -0.154951 0.301624 -2.179861 4 -1.369849 -0.954208 1.462696 -1.743161 In [70]: df1.iloc[1:5, 2:4] Out[70]: 4 6 2 0.301624 -2.179861 4 1.462696 -1.743161 6 1.314232 0.690579 8 0.014871 3.357427 Select via integer list: In [71]: df1.iloc[[1, 3, 5], [1, 3]] Out[71]: 2 6 2 -0.154951 -2.179861 6 -0.345352 0.690579 10 -1.236269 -0.487602 In [72]: df1.iloc[1:3, :] Out[72]: 0 2 4 6 2 0.403310 -0.154951 0.301624 -2.179861 4 -1.369849 -0.954208 1.462696 -1.743161 In [73]: df1.iloc[:, 1:3] Out[73]: 2 4 0 -0.732339 0.687738 2 -0.154951 0.301624 4 -0.954208 1.462696 6 -0.345352 1.314232 8 2.396780 0.014871 10 -1.236269 0.896171 # this is also equivalent to ``df1.iat[1,1]`` In [74]: df1.iloc[1, 1] Out[74]: -0.1549507744249032 For getting a cross section using an integer position (equiv to df.xs(1)): In [75]: df1.iloc[1] Out[75]: 0 0.403310 2 -0.154951 4 0.301624 6 -2.179861 Name: 2, dtype: float64 Out of range slice indexes are handled gracefully just as in Python/NumPy. # these are allowed in Python/NumPy. In [76]: x = list('abcdef') In [77]: x Out[77]: ['a', 'b', 'c', 'd', 'e', 'f'] In [78]: x[4:10] Out[78]: ['e', 'f'] In [79]: x[8:10] Out[79]: [] In [80]: s = pd.Series(x) In [81]: s Out[81]: 0 a 1 b 2 c 3 d 4 e 5 f dtype: object In [82]: s.iloc[4:10] Out[82]: 4 e 5 f dtype: object In [83]: s.iloc[8:10] Out[83]: Series([], dtype: object) Note that using slices that go out of bounds can result in an empty axis (e.g. an empty DataFrame being returned). In [84]: dfl = pd.DataFrame(np.random.randn(5, 2), columns=list('AB')) In [85]: dfl Out[85]: A B 0 -0.082240 -2.182937 1 0.380396 0.084844 2 0.432390 1.519970 3 -0.493662 0.600178 4 0.274230 0.132885 In [86]: dfl.iloc[:, 2:3] Out[86]: Empty DataFrame Columns: [] Index: [0, 1, 2, 3, 4] In [87]: dfl.iloc[:, 1:3] Out[87]: B 0 -2.182937 1 0.084844 2 1.519970 3 0.600178 4 0.132885 In [88]: dfl.iloc[4:6] Out[88]: A B 4 0.27423 0.132885 A single indexer that is out of bounds will raise an IndexError. A list of indexers where any element is out of bounds will raise an IndexError. >>> dfl.iloc[[4, 5, 6]] IndexError: positional indexers are out-of-bounds >>> dfl.iloc[:, 4] IndexError: single positional indexer is out-of-bounds Selection by callable# .loc, .iloc, and also [] indexing can accept a callable as indexer. The callable must be a function with one argument (the calling Series or DataFrame) that returns valid output for indexing. In [89]: df1 = pd.DataFrame(np.random.randn(6, 4), ....: index=list('abcdef'), ....: columns=list('ABCD')) ....: In [90]: df1 Out[90]: A B C D a -0.023688 2.410179 1.450520 0.206053 b -0.251905 -2.213588 1.063327 1.266143 c 0.299368 -0.863838 0.408204 -1.048089 d -0.025747 -0.988387 0.094055 1.262731 e 1.289997 0.082423 -0.055758 0.536580 f -0.489682 0.369374 -0.034571 -2.484478 In [91]: df1.loc[lambda df: df['A'] > 0, :] Out[91]: A B C D c 0.299368 -0.863838 0.408204 -1.048089 e 1.289997 0.082423 -0.055758 0.536580 In [92]: df1.loc[:, lambda df: ['A', 'B']] Out[92]: A B a -0.023688 2.410179 b -0.251905 -2.213588 c 0.299368 -0.863838 d -0.025747 -0.988387 e 1.289997 0.082423 f -0.489682 0.369374 In [93]: df1.iloc[:, lambda df: [0, 1]] Out[93]: A B a -0.023688 2.410179 b -0.251905 -2.213588 c 0.299368 -0.863838 d -0.025747 -0.988387 e 1.289997 0.082423 f -0.489682 0.369374 In [94]: df1[lambda df: df.columns[0]] Out[94]: a -0.023688 b -0.251905 c 0.299368 d -0.025747 e 1.289997 f -0.489682 Name: A, dtype: float64 You can use callable indexing in Series. In [95]: df1['A'].loc[lambda s: s > 0] Out[95]: c 0.299368 e 1.289997 Name: A, dtype: float64 Using these methods / indexers, you can chain data selection operations without using a temporary variable. In [96]: bb = pd.read_csv('data/baseball.csv', index_col='id') In [97]: (bb.groupby(['year', 'team']).sum(numeric_only=True) ....: .loc[lambda df: df['r'] > 100]) ....: Out[97]: stint g ab r h X2b ... so ibb hbp sh sf gidp year team ... 2007 CIN 6 379 745 101 203 35 ... 127.0 14.0 1.0 1.0 15.0 18.0 DET 5 301 1062 162 283 54 ... 176.0 3.0 10.0 4.0 8.0 28.0 HOU 4 311 926 109 218 47 ... 212.0 3.0 9.0 16.0 6.0 17.0 LAN 11 413 1021 153 293 61 ... 141.0 8.0 9.0 3.0 8.0 29.0 NYN 13 622 1854 240 509 101 ... 310.0 24.0 23.0 18.0 15.0 48.0 SFN 5 482 1305 198 337 67 ... 188.0 51.0 8.0 16.0 6.0 41.0 TEX 2 198 729 115 200 40 ... 140.0 4.0 5.0 2.0 8.0 16.0 TOR 4 459 1408 187 378 96 ... 265.0 16.0 12.0 4.0 16.0 38.0 [8 rows x 18 columns] Combining positional and label-based indexing# If you wish to get the 0th and the 2nd elements from the index in the ‘A’ column, you can do: In [98]: dfd = pd.DataFrame({'A': [1, 2, 3], ....: 'B': [4, 5, 6]}, ....: index=list('abc')) ....: In [99]: dfd Out[99]: A B a 1 4 b 2 5 c 3 6 In [100]: dfd.loc[dfd.index[[0, 2]], 'A'] Out[100]: a 1 c 3 Name: A, dtype: int64 This can also be expressed using .iloc, by explicitly getting locations on the indexers, and using positional indexing to select things. In [101]: dfd.iloc[[0, 2], dfd.columns.get_loc('A')] Out[101]: a 1 c 3 Name: A, dtype: int64 For getting multiple indexers, using .get_indexer: In [102]: dfd.iloc[[0, 2], dfd.columns.get_indexer(['A', 'B'])] Out[102]: A B a 1 4 c 3 6 Indexing with list with missing labels is deprecated# Warning Changed in version 1.0.0. Using .loc or [] with a list with one or more missing labels will no longer reindex, in favor of .reindex. In prior versions, using .loc[list-of-labels] would work as long as at least 1 of the keys was found (otherwise it would raise a KeyError). This behavior was changed and will now raise a KeyError if at least one label is missing. The recommended alternative is to use .reindex(). For example. In [103]: s = pd.Series([1, 2, 3]) In [104]: s Out[104]: 0 1 1 2 2 3 dtype: int64 Selection with all keys found is unchanged. In [105]: s.loc[[1, 2]] Out[105]: 1 2 2 3 dtype: int64 Previous behavior In [4]: s.loc[[1, 2, 3]] Out[4]: 1 2.0 2 3.0 3 NaN dtype: float64 Current behavior In [4]: s.loc[[1, 2, 3]] Passing list-likes to .loc with any non-matching elements will raise KeyError in the future, you can use .reindex() as an alternative. See the documentation here: https://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate-loc-reindex-listlike Out[4]: 1 2.0 2 3.0 3 NaN dtype: float64 Reindexing# The idiomatic way to achieve selecting potentially not-found elements is via .reindex(). See also the section on reindexing. In [106]: s.reindex([1, 2, 3]) Out[106]: 1 2.0 2 3.0 3 NaN dtype: float64 Alternatively, if you want to select only valid keys, the following is idiomatic and efficient; it is guaranteed to preserve the dtype of the selection. In [107]: labels = [1, 2, 3] In [108]: s.loc[s.index.intersection(labels)] Out[108]: 1 2 2 3 dtype: int64 Having a duplicated index will raise for a .reindex(): In [109]: s = pd.Series(np.arange(4), index=['a', 'a', 'b', 'c']) In [110]: labels = ['c', 'd'] In [17]: s.reindex(labels) ValueError: cannot reindex on an axis with duplicate labels Generally, you can intersect the desired labels with the current axis, and then reindex. In [111]: s.loc[s.index.intersection(labels)].reindex(labels) Out[111]: c 3.0 d NaN dtype: float64 However, this would still raise if your resulting index is duplicated. In [41]: labels = ['a', 'd'] In [42]: s.loc[s.index.intersection(labels)].reindex(labels) ValueError: cannot reindex on an axis with duplicate labels Selecting random samples# A random selection of rows or columns from a Series or DataFrame with the sample() method. The method will sample rows by default, and accepts a specific number of rows/columns to return, or a fraction of rows. In [112]: s = pd.Series([0, 1, 2, 3, 4, 5]) # When no arguments are passed, returns 1 row. In [113]: s.sample() Out[113]: 4 4 dtype: int64 # One may specify either a number of rows: In [114]: s.sample(n=3) Out[114]: 0 0 4 4 1 1 dtype: int64 # Or a fraction of the rows: In [115]: s.sample(frac=0.5) Out[115]: 5 5 3 3 1 1 dtype: int64 By default, sample will return each row at most once, but one can also sample with replacement using the replace option: In [116]: s = pd.Series([0, 1, 2, 3, 4, 5]) # Without replacement (default): In [117]: s.sample(n=6, replace=False) Out[117]: 0 0 1 1 5 5 3 3 2 2 4 4 dtype: int64 # With replacement: In [118]: s.sample(n=6, replace=True) Out[118]: 0 0 4 4 3 3 2 2 4 4 4 4 dtype: int64 By default, each row has an equal probability of being selected, but if you want rows to have different probabilities, you can pass the sample function sampling weights as weights. These weights can be a list, a NumPy array, or a Series, but they must be of the same length as the object you are sampling. Missing values will be treated as a weight of zero, and inf values are not allowed. If weights do not sum to 1, they will be re-normalized by dividing all weights by the sum of the weights. For example: In [119]: s = pd.Series([0, 1, 2, 3, 4, 5]) In [120]: example_weights = [0, 0, 0.2, 0.2, 0.2, 0.4] In [121]: s.sample(n=3, weights=example_weights) Out[121]: 5 5 4 4 3 3 dtype: int64 # Weights will be re-normalized automatically In [122]: example_weights2 = [0.5, 0, 0, 0, 0, 0] In [123]: s.sample(n=1, weights=example_weights2) Out[123]: 0 0 dtype: int64 When applied to a DataFrame, you can use a column of the DataFrame as sampling weights (provided you are sampling rows and not columns) by simply passing the name of the column as a string. In [124]: df2 = pd.DataFrame({'col1': [9, 8, 7, 6], .....: 'weight_column': [0.5, 0.4, 0.1, 0]}) .....: In [125]: df2.sample(n=3, weights='weight_column') Out[125]: col1 weight_column 1 8 0.4 0 9 0.5 2 7 0.1 sample also allows users to sample columns instead of rows using the axis argument. In [126]: df3 = pd.DataFrame({'col1': [1, 2, 3], 'col2': [2, 3, 4]}) In [127]: df3.sample(n=1, axis=1) Out[127]: col1 0 1 1 2 2 3 Finally, one can also set a seed for sample’s random number generator using the random_state argument, which will accept either an integer (as a seed) or a NumPy RandomState object. In [128]: df4 = pd.DataFrame({'col1': [1, 2, 3], 'col2': [2, 3, 4]}) # With a given seed, the sample will always draw the same rows. In [129]: df4.sample(n=2, random_state=2) Out[129]: col1 col2 2 3 4 1 2 3 In [130]: df4.sample(n=2, random_state=2) Out[130]: col1 col2 2 3 4 1 2 3 Setting with enlargement# The .loc/[] operations can perform enlargement when setting a non-existent key for that axis. In the Series case this is effectively an appending operation. In [131]: se = pd.Series([1, 2, 3]) In [132]: se Out[132]: 0 1 1 2 2 3 dtype: int64 In [133]: se[5] = 5. In [134]: se Out[134]: 0 1.0 1 2.0 2 3.0 5 5.0 dtype: float64 A DataFrame can be enlarged on either axis via .loc. In [135]: dfi = pd.DataFrame(np.arange(6).reshape(3, 2), .....: columns=['A', 'B']) .....: In [136]: dfi Out[136]: A B 0 0 1 1 2 3 2 4 5 In [137]: dfi.loc[:, 'C'] = dfi.loc[:, 'A'] In [138]: dfi Out[138]: A B C 0 0 1 0 1 2 3 2 2 4 5 4 This is like an append operation on the DataFrame. In [139]: dfi.loc[3] = 5 In [140]: dfi Out[140]: A B C 0 0 1 0 1 2 3 2 2 4 5 4 3 5 5 5 Fast scalar value getting and setting# Since indexing with [] must handle a lot of cases (single-label access, slicing, boolean indexing, etc.), it has a bit of overhead in order to figure out what you’re asking for. If you only want to access a scalar value, the fastest way is to use the at and iat methods, which are implemented on all of the data structures. Similarly to loc, at provides label based scalar lookups, while, iat provides integer based lookups analogously to iloc In [141]: s.iat[5] Out[141]: 5 In [142]: df.at[dates[5], 'A'] Out[142]: -0.6736897080883706 In [143]: df.iat[3, 0] Out[143]: 0.7215551622443669 You can also set using these same indexers. In [144]: df.at[dates[5], 'E'] = 7 In [145]: df.iat[3, 0] = 7 at may enlarge the object in-place as above if the indexer is missing. In [146]: df.at[dates[-1] + pd.Timedelta('1 day'), 0] = 7 In [147]: df Out[147]: A B C D E 0 2000-01-01 0.469112 -0.282863 -1.509059 -1.135632 NaN NaN 2000-01-02 1.212112 -0.173215 0.119209 -1.044236 NaN NaN 2000-01-03 -0.861849 -2.104569 -0.494929 1.071804 NaN NaN 2000-01-04 7.000000 -0.706771 -1.039575 0.271860 NaN NaN 2000-01-05 -0.424972 0.567020 0.276232 -1.087401 NaN NaN 2000-01-06 -0.673690 0.113648 -1.478427 0.524988 7.0 NaN 2000-01-07 0.404705 0.577046 -1.715002 -1.039268 NaN NaN 2000-01-08 -0.370647 -1.157892 -1.344312 0.844885 NaN NaN 2000-01-09 NaN NaN NaN NaN NaN 7.0 Boolean indexing# Another common operation is the use of boolean vectors to filter the data. The operators are: | for or, & for and, and ~ for not. These must be grouped by using parentheses, since by default Python will evaluate an expression such as df['A'] > 2 & df['B'] < 3 as df['A'] > (2 & df['B']) < 3, while the desired evaluation order is (df['A'] > 2) & (df['B'] < 3). Using a boolean vector to index a Series works exactly as in a NumPy ndarray: In [148]: s = pd.Series(range(-3, 4)) In [149]: s Out[149]: 0 -3 1 -2 2 -1 3 0 4 1 5 2 6 3 dtype: int64 In [150]: s[s > 0] Out[150]: 4 1 5 2 6 3 dtype: int64 In [151]: s[(s < -1) | (s > 0.5)] Out[151]: 0 -3 1 -2 4 1 5 2 6 3 dtype: int64 In [152]: s[~(s < 0)] Out[152]: 3 0 4 1 5 2 6 3 dtype: int64 You may select rows from a DataFrame using a boolean vector the same length as the DataFrame’s index (for example, something derived from one of the columns of the DataFrame): In [153]: df[df['A'] > 0] Out[153]: A B C D E 0 2000-01-01 0.469112 -0.282863 -1.509059 -1.135632 NaN NaN 2000-01-02 1.212112 -0.173215 0.119209 -1.044236 NaN NaN 2000-01-04 7.000000 -0.706771 -1.039575 0.271860 NaN NaN 2000-01-07 0.404705 0.577046 -1.715002 -1.039268 NaN NaN List comprehensions and the map method of Series can also be used to produce more complex criteria: In [154]: df2 = pd.DataFrame({'a': ['one', 'one', 'two', 'three', 'two', 'one', 'six'], .....: 'b': ['x', 'y', 'y', 'x', 'y', 'x', 'x'], .....: 'c': np.random.randn(7)}) .....: # only want 'two' or 'three' In [155]: criterion = df2['a'].map(lambda x: x.startswith('t')) In [156]: df2[criterion] Out[156]: a b c 2 two y 0.041290 3 three x 0.361719 4 two y -0.238075 # equivalent but slower In [157]: df2[[x.startswith('t') for x in df2['a']]] Out[157]: a b c 2 two y 0.041290 3 three x 0.361719 4 two y -0.238075 # Multiple criteria In [158]: df2[criterion & (df2['b'] == 'x')] Out[158]: a b c 3 three x 0.361719 With the choice methods Selection by Label, Selection by Position, and Advanced Indexing you may select along more than one axis using boolean vectors combined with other indexing expressions. In [159]: df2.loc[criterion & (df2['b'] == 'x'), 'b':'c'] Out[159]: b c 3 x 0.361719 Warning iloc supports two kinds of boolean indexing. If the indexer is a boolean Series, an error will be raised. For instance, in the following example, df.iloc[s.values, 1] is ok. The boolean indexer is an array. But df.iloc[s, 1] would raise ValueError. In [160]: df = pd.DataFrame([[1, 2], [3, 4], [5, 6]], .....: index=list('abc'), .....: columns=['A', 'B']) .....: In [161]: s = (df['A'] > 2) In [162]: s Out[162]: a False b True c True Name: A, dtype: bool In [163]: df.loc[s, 'B'] Out[163]: b 4 c 6 Name: B, dtype: int64 In [164]: df.iloc[s.values, 1] Out[164]: b 4 c 6 Name: B, dtype: int64 Indexing with isin# Consider the isin() method of Series, which returns a boolean vector that is true wherever the Series elements exist in the passed list. This allows you to select rows where one or more columns have values you want: In [165]: s = pd.Series(np.arange(5), index=np.arange(5)[::-1], dtype='int64') In [166]: s Out[166]: 4 0 3 1 2 2 1 3 0 4 dtype: int64 In [167]: s.isin([2, 4, 6]) Out[167]: 4 False 3 False 2 True 1 False 0 True dtype: bool In [168]: s[s.isin([2, 4, 6])] Out[168]: 2 2 0 4 dtype: int64 The same method is available for Index objects and is useful for the cases when you don’t know which of the sought labels are in fact present: In [169]: s[s.index.isin([2, 4, 6])] Out[169]: 4 0 2 2 dtype: int64 # compare it to the following In [170]: s.reindex([2, 4, 6]) Out[170]: 2 2.0 4 0.0 6 NaN dtype: float64 In addition to that, MultiIndex allows selecting a separate level to use in the membership check: In [171]: s_mi = pd.Series(np.arange(6), .....: index=pd.MultiIndex.from_product([[0, 1], ['a', 'b', 'c']])) .....: In [172]: s_mi Out[172]: 0 a 0 b 1 c 2 1 a 3 b 4 c 5 dtype: int64 In [173]: s_mi.iloc[s_mi.index.isin([(1, 'a'), (2, 'b'), (0, 'c')])] Out[173]: 0 c 2 1 a 3 dtype: int64 In [174]: s_mi.iloc[s_mi.index.isin(['a', 'c', 'e'], level=1)] Out[174]: 0 a 0 c 2 1 a 3 c 5 dtype: int64 DataFrame also has an isin() method. When calling isin, pass a set of values as either an array or dict. If values is an array, isin returns a DataFrame of booleans that is the same shape as the original DataFrame, with True wherever the element is in the sequence of values. In [175]: df = pd.DataFrame({'vals': [1, 2, 3, 4], 'ids': ['a', 'b', 'f', 'n'], .....: 'ids2': ['a', 'n', 'c', 'n']}) .....: In [176]: values = ['a', 'b', 1, 3] In [177]: df.isin(values) Out[177]: vals ids ids2 0 True True True 1 False True False 2 True False False 3 False False False Oftentimes you’ll want to match certain values with certain columns. Just make values a dict where the key is the column, and the value is a list of items you want to check for. In [178]: values = {'ids': ['a', 'b'], 'vals': [1, 3]} In [179]: df.isin(values) Out[179]: vals ids ids2 0 True True False 1 False True False 2 True False False 3 False False False To return the DataFrame of booleans where the values are not in the original DataFrame, use the ~ operator: In [180]: values = {'ids': ['a', 'b'], 'vals': [1, 3]} In [181]: ~df.isin(values) Out[181]: vals ids ids2 0 False False True 1 True False True 2 False True True 3 True True True Combine DataFrame’s isin with the any() and all() methods to quickly select subsets of your data that meet a given criteria. To select a row where each column meets its own criterion: In [182]: values = {'ids': ['a', 'b'], 'ids2': ['a', 'c'], 'vals': [1, 3]} In [183]: row_mask = df.isin(values).all(1) In [184]: df[row_mask] Out[184]: vals ids ids2 0 1 a a The where() Method and Masking# Selecting values from a Series with a boolean vector generally returns a subset of the data. To guarantee that selection output has the same shape as the original data, you can use the where method in Series and DataFrame. To return only the selected rows: In [185]: s[s > 0] Out[185]: 3 1 2 2 1 3 0 4 dtype: int64 To return a Series of the same shape as the original: In [186]: s.where(s > 0) Out[186]: 4 NaN 3 1.0 2 2.0 1 3.0 0 4.0 dtype: float64 Selecting values from a DataFrame with a boolean criterion now also preserves input data shape. where is used under the hood as the implementation. The code below is equivalent to df.where(df < 0). In [187]: df[df < 0] Out[187]: A B C D 2000-01-01 -2.104139 -1.309525 NaN NaN 2000-01-02 -0.352480 NaN -1.192319 NaN 2000-01-03 -0.864883 NaN -0.227870 NaN 2000-01-04 NaN -1.222082 NaN -1.233203 2000-01-05 NaN -0.605656 -1.169184 NaN 2000-01-06 NaN -0.948458 NaN -0.684718 2000-01-07 -2.670153 -0.114722 NaN -0.048048 2000-01-08 NaN NaN -0.048788 -0.808838 In addition, where takes an optional other argument for replacement of values where the condition is False, in the returned copy. In [188]: df.where(df < 0, -df) Out[188]: A B C D 2000-01-01 -2.104139 -1.309525 -0.485855 -0.245166 2000-01-02 -0.352480 -0.390389 -1.192319 -1.655824 2000-01-03 -0.864883 -0.299674 -0.227870 -0.281059 2000-01-04 -0.846958 -1.222082 -0.600705 -1.233203 2000-01-05 -0.669692 -0.605656 -1.169184 -0.342416 2000-01-06 -0.868584 -0.948458 -2.297780 -0.684718 2000-01-07 -2.670153 -0.114722 -0.168904 -0.048048 2000-01-08 -0.801196 -1.392071 -0.048788 -0.808838 You may wish to set values based on some boolean criteria. This can be done intuitively like so: In [189]: s2 = s.copy() In [190]: s2[s2 < 0] = 0 In [191]: s2 Out[191]: 4 0 3 1 2 2 1 3 0 4 dtype: int64 In [192]: df2 = df.copy() In [193]: df2[df2 < 0] = 0 In [194]: df2 Out[194]: A B C D 2000-01-01 0.000000 0.000000 0.485855 0.245166 2000-01-02 0.000000 0.390389 0.000000 1.655824 2000-01-03 0.000000 0.299674 0.000000 0.281059 2000-01-04 0.846958 0.000000 0.600705 0.000000 2000-01-05 0.669692 0.000000 0.000000 0.342416 2000-01-06 0.868584 0.000000 2.297780 0.000000 2000-01-07 0.000000 0.000000 0.168904 0.000000 2000-01-08 0.801196 1.392071 0.000000 0.000000 By default, where returns a modified copy of the data. There is an optional parameter inplace so that the original data can be modified without creating a copy: In [195]: df_orig = df.copy() In [196]: df_orig.where(df > 0, -df, inplace=True) In [197]: df_orig Out[197]: A B C D 2000-01-01 2.104139 1.309525 0.485855 0.245166 2000-01-02 0.352480 0.390389 1.192319 1.655824 2000-01-03 0.864883 0.299674 0.227870 0.281059 2000-01-04 0.846958 1.222082 0.600705 1.233203 2000-01-05 0.669692 0.605656 1.169184 0.342416 2000-01-06 0.868584 0.948458 2.297780 0.684718 2000-01-07 2.670153 0.114722 0.168904 0.048048 2000-01-08 0.801196 1.392071 0.048788 0.808838 Note The signature for DataFrame.where() differs from numpy.where(). Roughly df1.where(m, df2) is equivalent to np.where(m, df1, df2). In [198]: df.where(df < 0, -df) == np.where(df < 0, df, -df) Out[198]: A B C D 2000-01-01 True True True True 2000-01-02 True True True True 2000-01-03 True True True True 2000-01-04 True True True True 2000-01-05 True True True True 2000-01-06 True True True True 2000-01-07 True True True True 2000-01-08 True True True True Alignment Furthermore, where aligns the input boolean condition (ndarray or DataFrame), such that partial selection with setting is possible. This is analogous to partial setting via .loc (but on the contents rather than the axis labels). In [199]: df2 = df.copy() In [200]: df2[df2[1:4] > 0] = 3 In [201]: df2 Out[201]: A B C D 2000-01-01 -2.104139 -1.309525 0.485855 0.245166 2000-01-02 -0.352480 3.000000 -1.192319 3.000000 2000-01-03 -0.864883 3.000000 -0.227870 3.000000 2000-01-04 3.000000 -1.222082 3.000000 -1.233203 2000-01-05 0.669692 -0.605656 -1.169184 0.342416 2000-01-06 0.868584 -0.948458 2.297780 -0.684718 2000-01-07 -2.670153 -0.114722 0.168904 -0.048048 2000-01-08 0.801196 1.392071 -0.048788 -0.808838 Where can also accept axis and level parameters to align the input when performing the where. In [202]: df2 = df.copy() In [203]: df2.where(df2 > 0, df2['A'], axis='index') Out[203]: A B C D 2000-01-01 -2.104139 -2.104139 0.485855 0.245166 2000-01-02 -0.352480 0.390389 -0.352480 1.655824 2000-01-03 -0.864883 0.299674 -0.864883 0.281059 2000-01-04 0.846958 0.846958 0.600705 0.846958 2000-01-05 0.669692 0.669692 0.669692 0.342416 2000-01-06 0.868584 0.868584 2.297780 0.868584 2000-01-07 -2.670153 -2.670153 0.168904 -2.670153 2000-01-08 0.801196 1.392071 0.801196 0.801196 This is equivalent to (but faster than) the following. In [204]: df2 = df.copy() In [205]: df.apply(lambda x, y: x.where(x > 0, y), y=df['A']) Out[205]: A B C D 2000-01-01 -2.104139 -2.104139 0.485855 0.245166 2000-01-02 -0.352480 0.390389 -0.352480 1.655824 2000-01-03 -0.864883 0.299674 -0.864883 0.281059 2000-01-04 0.846958 0.846958 0.600705 0.846958 2000-01-05 0.669692 0.669692 0.669692 0.342416 2000-01-06 0.868584 0.868584 2.297780 0.868584 2000-01-07 -2.670153 -2.670153 0.168904 -2.670153 2000-01-08 0.801196 1.392071 0.801196 0.801196 where can accept a callable as condition and other arguments. The function must be with one argument (the calling Series or DataFrame) and that returns valid output as condition and other argument. In [206]: df3 = pd.DataFrame({'A': [1, 2, 3], .....: 'B': [4, 5, 6], .....: 'C': [7, 8, 9]}) .....: In [207]: df3.where(lambda x: x > 4, lambda x: x + 10) Out[207]: A B C 0 11 14 7 1 12 5 8 2 13 6 9 Mask# mask() is the inverse boolean operation of where. In [208]: s.mask(s >= 0) Out[208]: 4 NaN 3 NaN 2 NaN 1 NaN 0 NaN dtype: float64 In [209]: df.mask(df >= 0) Out[209]: A B C D 2000-01-01 -2.104139 -1.309525 NaN NaN 2000-01-02 -0.352480 NaN -1.192319 NaN 2000-01-03 -0.864883 NaN -0.227870 NaN 2000-01-04 NaN -1.222082 NaN -1.233203 2000-01-05 NaN -0.605656 -1.169184 NaN 2000-01-06 NaN -0.948458 NaN -0.684718 2000-01-07 -2.670153 -0.114722 NaN -0.048048 2000-01-08 NaN NaN -0.048788 -0.808838 Setting with enlargement conditionally using numpy()# An alternative to where() is to use numpy.where(). Combined with setting a new column, you can use it to enlarge a DataFrame where the values are determined conditionally. Consider you have two choices to choose from in the following DataFrame. And you want to set a new column color to ‘green’ when the second column has ‘Z’. You can do the following: In [210]: df = pd.DataFrame({'col1': list('ABBC'), 'col2': list('ZZXY')}) In [211]: df['color'] = np.where(df['col2'] == 'Z', 'green', 'red') In [212]: df Out[212]: col1 col2 color 0 A Z green 1 B Z green 2 B X red 3 C Y red If you have multiple conditions, you can use numpy.select() to achieve that. Say corresponding to three conditions there are three choice of colors, with a fourth color as a fallback, you can do the following. In [213]: conditions = [ .....: (df['col2'] == 'Z') & (df['col1'] == 'A'), .....: (df['col2'] == 'Z') & (df['col1'] == 'B'), .....: (df['col1'] == 'B') .....: ] .....: In [214]: choices = ['yellow', 'blue', 'purple'] In [215]: df['color'] = np.select(conditions, choices, default='black') In [216]: df Out[216]: col1 col2 color 0 A Z yellow 1 B Z blue 2 B X purple 3 C Y black The query() Method# DataFrame objects have a query() method that allows selection using an expression. You can get the value of the frame where column b has values between the values of columns a and c. For example: In [217]: n = 10 In [218]: df = pd.DataFrame(np.random.rand(n, 3), columns=list('abc')) In [219]: df Out[219]: a b c 0 0.438921 0.118680 0.863670 1 0.138138 0.577363 0.686602 2 0.595307 0.564592 0.520630 3 0.913052 0.926075 0.616184 4 0.078718 0.854477 0.898725 5 0.076404 0.523211 0.591538 6 0.792342 0.216974 0.564056 7 0.397890 0.454131 0.915716 8 0.074315 0.437913 0.019794 9 0.559209 0.502065 0.026437 # pure python In [220]: df[(df['a'] < df['b']) & (df['b'] < df['c'])] Out[220]: a b c 1 0.138138 0.577363 0.686602 4 0.078718 0.854477 0.898725 5 0.076404 0.523211 0.591538 7 0.397890 0.454131 0.915716 # query In [221]: df.query('(a < b) & (b < c)') Out[221]: a b c 1 0.138138 0.577363 0.686602 4 0.078718 0.854477 0.898725 5 0.076404 0.523211 0.591538 7 0.397890 0.454131 0.915716 Do the same thing but fall back on a named index if there is no column with the name a. In [222]: df = pd.DataFrame(np.random.randint(n / 2, size=(n, 2)), columns=list('bc')) In [223]: df.index.name = 'a' In [224]: df Out[224]: b c a 0 0 4 1 0 1 2 3 4 3 4 3 4 1 4 5 0 3 6 0 1 7 3 4 8 2 3 9 1 1 In [225]: df.query('a < b and b < c') Out[225]: b c a 2 3 4 If instead you don’t want to or cannot name your index, you can use the name index in your query expression: In [226]: df = pd.DataFrame(np.random.randint(n, size=(n, 2)), columns=list('bc')) In [227]: df Out[227]: b c 0 3 1 1 3 0 2 5 6 3 5 2 4 7 4 5 0 1 6 2 5 7 0 1 8 6 0 9 7 9 In [228]: df.query('index < b < c') Out[228]: b c 2 5 6 Note If the name of your index overlaps with a column name, the column name is given precedence. For example, In [229]: df = pd.DataFrame({'a': np.random.randint(5, size=5)}) In [230]: df.index.name = 'a' In [231]: df.query('a > 2') # uses the column 'a', not the index Out[231]: a a 1 3 3 3 You can still use the index in a query expression by using the special identifier ‘index’: In [232]: df.query('index > 2') Out[232]: a a 3 3 4 2 If for some reason you have a column named index, then you can refer to the index as ilevel_0 as well, but at this point you should consider renaming your columns to something less ambiguous. MultiIndex query() Syntax# You can also use the levels of a DataFrame with a MultiIndex as if they were columns in the frame: In [233]: n = 10 In [234]: colors = np.random.choice(['red', 'green'], size=n) In [235]: foods = np.random.choice(['eggs', 'ham'], size=n) In [236]: colors Out[236]: array(['red', 'red', 'red', 'green', 'green', 'green', 'green', 'green', 'green', 'green'], dtype='<U5') In [237]: foods Out[237]: array(['ham', 'ham', 'eggs', 'eggs', 'eggs', 'ham', 'ham', 'eggs', 'eggs', 'eggs'], dtype='<U4') In [238]: index = pd.MultiIndex.from_arrays([colors, foods], names=['color', 'food']) In [239]: df = pd.DataFrame(np.random.randn(n, 2), index=index) In [240]: df Out[240]: 0 1 color food red ham 0.194889 -0.381994 ham 0.318587 2.089075 eggs -0.728293 -0.090255 green eggs -0.748199 1.318931 eggs -2.029766 0.792652 ham 0.461007 -0.542749 ham -0.305384 -0.479195 eggs 0.095031 -0.270099 eggs -0.707140 -0.773882 eggs 0.229453 0.304418 In [241]: df.query('color == "red"') Out[241]: 0 1 color food red ham 0.194889 -0.381994 ham 0.318587 2.089075 eggs -0.728293 -0.090255 If the levels of the MultiIndex are unnamed, you can refer to them using special names: In [242]: df.index.names = [None, None] In [243]: df Out[243]: 0 1 red ham 0.194889 -0.381994 ham 0.318587 2.089075 eggs -0.728293 -0.090255 green eggs -0.748199 1.318931 eggs -2.029766 0.792652 ham 0.461007 -0.542749 ham -0.305384 -0.479195 eggs 0.095031 -0.270099 eggs -0.707140 -0.773882 eggs 0.229453 0.304418 In [244]: df.query('ilevel_0 == "red"') Out[244]: 0 1 red ham 0.194889 -0.381994 ham 0.318587 2.089075 eggs -0.728293 -0.090255 The convention is ilevel_0, which means “index level 0” for the 0th level of the index. query() Use Cases# A use case for query() is when you have a collection of DataFrame objects that have a subset of column names (or index levels/names) in common. You can pass the same query to both frames without having to specify which frame you’re interested in querying In [245]: df = pd.DataFrame(np.random.rand(n, 3), columns=list('abc')) In [246]: df Out[246]: a b c 0 0.224283 0.736107 0.139168 1 0.302827 0.657803 0.713897 2 0.611185 0.136624 0.984960 3 0.195246 0.123436 0.627712 4 0.618673 0.371660 0.047902 5 0.480088 0.062993 0.185760 6 0.568018 0.483467 0.445289 7 0.309040 0.274580 0.587101 8 0.258993 0.477769 0.370255 9 0.550459 0.840870 0.304611 In [247]: df2 = pd.DataFrame(np.random.rand(n + 2, 3), columns=df.columns) In [248]: df2 Out[248]: a b c 0 0.357579 0.229800 0.596001 1 0.309059 0.957923 0.965663 2 0.123102 0.336914 0.318616 3 0.526506 0.323321 0.860813 4 0.518736 0.486514 0.384724 5 0.190804 0.505723 0.614533 6 0.891939 0.623977 0.676639 7 0.480559 0.378528 0.460858 8 0.420223 0.136404 0.141295 9 0.732206 0.419540 0.604675 10 0.604466 0.848974 0.896165 11 0.589168 0.920046 0.732716 In [249]: expr = '0.0 <= a <= c <= 0.5' In [250]: map(lambda frame: frame.query(expr), [df, df2]) Out[250]: <map at 0x7f1ea0d8e580> query() Python versus pandas Syntax Comparison# Full numpy-like syntax: In [251]: df = pd.DataFrame(np.random.randint(n, size=(n, 3)), columns=list('abc')) In [252]: df Out[252]: a b c 0 7 8 9 1 1 0 7 2 2 7 2 3 6 2 2 4 2 6 3 5 3 8 2 6 1 7 2 7 5 1 5 8 9 8 0 9 1 5 0 In [253]: df.query('(a < b) & (b < c)') Out[253]: a b c 0 7 8 9 In [254]: df[(df['a'] < df['b']) & (df['b'] < df['c'])] Out[254]: a b c 0 7 8 9 Slightly nicer by removing the parentheses (comparison operators bind tighter than & and |): In [255]: df.query('a < b & b < c') Out[255]: a b c 0 7 8 9 Use English instead of symbols: In [256]: df.query('a < b and b < c') Out[256]: a b c 0 7 8 9 Pretty close to how you might write it on paper: In [257]: df.query('a < b < c') Out[257]: a b c 0 7 8 9 The in and not in operators# query() also supports special use of Python’s in and not in comparison operators, providing a succinct syntax for calling the isin method of a Series or DataFrame. # get all rows where columns "a" and "b" have overlapping values In [258]: df = pd.DataFrame({'a': list('aabbccddeeff'), 'b': list('aaaabbbbcccc'), .....: 'c': np.random.randint(5, size=12), .....: 'd': np.random.randint(9, size=12)}) .....: In [259]: df Out[259]: a b c d 0 a a 2 6 1 a a 4 7 2 b a 1 6 3 b a 2 1 4 c b 3 6 5 c b 0 2 6 d b 3 3 7 d b 2 1 8 e c 4 3 9 e c 2 0 10 f c 0 6 11 f c 1 2 In [260]: df.query('a in b') Out[260]: a b c d 0 a a 2 6 1 a a 4 7 2 b a 1 6 3 b a 2 1 4 c b 3 6 5 c b 0 2 # How you'd do it in pure Python In [261]: df[df['a'].isin(df['b'])] Out[261]: a b c d 0 a a 2 6 1 a a 4 7 2 b a 1 6 3 b a 2 1 4 c b 3 6 5 c b 0 2 In [262]: df.query('a not in b') Out[262]: a b c d 6 d b 3 3 7 d b 2 1 8 e c 4 3 9 e c 2 0 10 f c 0 6 11 f c 1 2 # pure Python In [263]: df[~df['a'].isin(df['b'])] Out[263]: a b c d 6 d b 3 3 7 d b 2 1 8 e c 4 3 9 e c 2 0 10 f c 0 6 11 f c 1 2 You can combine this with other expressions for very succinct queries: # rows where cols a and b have overlapping values # and col c's values are less than col d's In [264]: df.query('a in b and c < d') Out[264]: a b c d 0 a a 2 6 1 a a 4 7 2 b a 1 6 4 c b 3 6 5 c b 0 2 # pure Python In [265]: df[df['b'].isin(df['a']) & (df['c'] < df['d'])] Out[265]: a b c d 0 a a 2 6 1 a a 4 7 2 b a 1 6 4 c b 3 6 5 c b 0 2 10 f c 0 6 11 f c 1 2 Note Note that in and not in are evaluated in Python, since numexpr has no equivalent of this operation. However, only the in/not in expression itself is evaluated in vanilla Python. For example, in the expression df.query('a in b + c + d') (b + c + d) is evaluated by numexpr and then the in operation is evaluated in plain Python. In general, any operations that can be evaluated using numexpr will be. Special use of the == operator with list objects# Comparing a list of values to a column using ==/!= works similarly to in/not in. In [266]: df.query('b == ["a", "b", "c"]') Out[266]: a b c d 0 a a 2 6 1 a a 4 7 2 b a 1 6 3 b a 2 1 4 c b 3 6 5 c b 0 2 6 d b 3 3 7 d b 2 1 8 e c 4 3 9 e c 2 0 10 f c 0 6 11 f c 1 2 # pure Python In [267]: df[df['b'].isin(["a", "b", "c"])] Out[267]: a b c d 0 a a 2 6 1 a a 4 7 2 b a 1 6 3 b a 2 1 4 c b 3 6 5 c b 0 2 6 d b 3 3 7 d b 2 1 8 e c 4 3 9 e c 2 0 10 f c 0 6 11 f c 1 2 In [268]: df.query('c == [1, 2]') Out[268]: a b c d 0 a a 2 6 2 b a 1 6 3 b a 2 1 7 d b 2 1 9 e c 2 0 11 f c 1 2 In [269]: df.query('c != [1, 2]') Out[269]: a b c d 1 a a 4 7 4 c b 3 6 5 c b 0 2 6 d b 3 3 8 e c 4 3 10 f c 0 6 # using in/not in In [270]: df.query('[1, 2] in c') Out[270]: a b c d 0 a a 2 6 2 b a 1 6 3 b a 2 1 7 d b 2 1 9 e c 2 0 11 f c 1 2 In [271]: df.query('[1, 2] not in c') Out[271]: a b c d 1 a a 4 7 4 c b 3 6 5 c b 0 2 6 d b 3 3 8 e c 4 3 10 f c 0 6 # pure Python In [272]: df[df['c'].isin([1, 2])] Out[272]: a b c d 0 a a 2 6 2 b a 1 6 3 b a 2 1 7 d b 2 1 9 e c 2 0 11 f c 1 2 Boolean operators# You can negate boolean expressions with the word not or the ~ operator. In [273]: df = pd.DataFrame(np.random.rand(n, 3), columns=list('abc')) In [274]: df['bools'] = np.random.rand(len(df)) > 0.5 In [275]: df.query('~bools') Out[275]: a b c bools 2 0.697753 0.212799 0.329209 False 7 0.275396 0.691034 0.826619 False 8 0.190649 0.558748 0.262467 False In [276]: df.query('not bools') Out[276]: a b c bools 2 0.697753 0.212799 0.329209 False 7 0.275396 0.691034 0.826619 False 8 0.190649 0.558748 0.262467 False In [277]: df.query('not bools') == df[~df['bools']] Out[277]: a b c bools 2 True True True True 7 True True True True 8 True True True True Of course, expressions can be arbitrarily complex too: # short query syntax In [278]: shorter = df.query('a < b < c and (not bools) or bools > 2') # equivalent in pure Python In [279]: longer = df[(df['a'] < df['b']) .....: & (df['b'] < df['c']) .....: & (~df['bools']) .....: | (df['bools'] > 2)] .....: In [280]: shorter Out[280]: a b c bools 7 0.275396 0.691034 0.826619 False In [281]: longer Out[281]: a b c bools 7 0.275396 0.691034 0.826619 False In [282]: shorter == longer Out[282]: a b c bools 7 True True True True Performance of query()# DataFrame.query() using numexpr is slightly faster than Python for large frames. Note You will only see the performance benefits of using the numexpr engine with DataFrame.query() if your frame has more than approximately 200,000 rows. This plot was created using a DataFrame with 3 columns each containing floating point values generated using numpy.random.randn(). Duplicate data# If you want to identify and remove duplicate rows in a DataFrame, there are two methods that will help: duplicated and drop_duplicates. Each takes as an argument the columns to use to identify duplicated rows. duplicated returns a boolean vector whose length is the number of rows, and which indicates whether a row is duplicated. drop_duplicates removes duplicate rows. By default, the first observed row of a duplicate set is considered unique, but each method has a keep parameter to specify targets to be kept. keep='first' (default): mark / drop duplicates except for the first occurrence. keep='last': mark / drop duplicates except for the last occurrence. keep=False: mark / drop all duplicates. In [283]: df2 = pd.DataFrame({'a': ['one', 'one', 'two', 'two', 'two', 'three', 'four'], .....: 'b': ['x', 'y', 'x', 'y', 'x', 'x', 'x'], .....: 'c': np.random.randn(7)}) .....: In [284]: df2 Out[284]: a b c 0 one x -1.067137 1 one y 0.309500 2 two x -0.211056 3 two y -1.842023 4 two x -0.390820 5 three x -1.964475 6 four x 1.298329 In [285]: df2.duplicated('a') Out[285]: 0 False 1 True 2 False 3 True 4 True 5 False 6 False dtype: bool In [286]: df2.duplicated('a', keep='last') Out[286]: 0 True 1 False 2 True 3 True 4 False 5 False 6 False dtype: bool In [287]: df2.duplicated('a', keep=False) Out[287]: 0 True 1 True 2 True 3 True 4 True 5 False 6 False dtype: bool In [288]: df2.drop_duplicates('a') Out[288]: a b c 0 one x -1.067137 2 two x -0.211056 5 three x -1.964475 6 four x 1.298329 In [289]: df2.drop_duplicates('a', keep='last') Out[289]: a b c 1 one y 0.309500 4 two x -0.390820 5 three x -1.964475 6 four x 1.298329 In [290]: df2.drop_duplicates('a', keep=False) Out[290]: a b c 5 three x -1.964475 6 four x 1.298329 Also, you can pass a list of columns to identify duplications. In [291]: df2.duplicated(['a', 'b']) Out[291]: 0 False 1 False 2 False 3 False 4 True 5 False 6 False dtype: bool In [292]: df2.drop_duplicates(['a', 'b']) Out[292]: a b c 0 one x -1.067137 1 one y 0.309500 2 two x -0.211056 3 two y -1.842023 5 three x -1.964475 6 four x 1.298329 To drop duplicates by index value, use Index.duplicated then perform slicing. The same set of options are available for the keep parameter. In [293]: df3 = pd.DataFrame({'a': np.arange(6), .....: 'b': np.random.randn(6)}, .....: index=['a', 'a', 'b', 'c', 'b', 'a']) .....: In [294]: df3 Out[294]: a b a 0 1.440455 a 1 2.456086 b 2 1.038402 c 3 -0.894409 b 4 0.683536 a 5 3.082764 In [295]: df3.index.duplicated() Out[295]: array([False, True, False, False, True, True]) In [296]: df3[~df3.index.duplicated()] Out[296]: a b a 0 1.440455 b 2 1.038402 c 3 -0.894409 In [297]: df3[~df3.index.duplicated(keep='last')] Out[297]: a b c 3 -0.894409 b 4 0.683536 a 5 3.082764 In [298]: df3[~df3.index.duplicated(keep=False)] Out[298]: a b c 3 -0.894409 Dictionary-like get() method# Each of Series or DataFrame have a get method which can return a default value. In [299]: s = pd.Series([1, 2, 3], index=['a', 'b', 'c']) In [300]: s.get('a') # equivalent to s['a'] Out[300]: 1 In [301]: s.get('x', default=-1) Out[301]: -1 Looking up values by index/column labels# Sometimes you want to extract a set of values given a sequence of row labels and column labels, this can be achieved by pandas.factorize and NumPy indexing. For instance: In [302]: df = pd.DataFrame({'col': ["A", "A", "B", "B"], .....: 'A': [80, 23, np.nan, 22], .....: 'B': [80, 55, 76, 67]}) .....: In [303]: df Out[303]: col A B 0 A 80.0 80 1 A 23.0 55 2 B NaN 76 3 B 22.0 67 In [304]: idx, cols = pd.factorize(df['col']) In [305]: df.reindex(cols, axis=1).to_numpy()[np.arange(len(df)), idx] Out[305]: array([80., 23., 76., 67.]) Formerly this could be achieved with the dedicated DataFrame.lookup method which was deprecated in version 1.2.0. Index objects# The pandas Index class and its subclasses can be viewed as implementing an ordered multiset. Duplicates are allowed. However, if you try to convert an Index object with duplicate entries into a set, an exception will be raised. Index also provides the infrastructure necessary for lookups, data alignment, and reindexing. The easiest way to create an Index directly is to pass a list or other sequence to Index: In [306]: index = pd.Index(['e', 'd', 'a', 'b']) In [307]: index Out[307]: Index(['e', 'd', 'a', 'b'], dtype='object') In [308]: 'd' in index Out[308]: True You can also pass a name to be stored in the index: In [309]: index = pd.Index(['e', 'd', 'a', 'b'], name='something') In [310]: index.name Out[310]: 'something' The name, if set, will be shown in the console display: In [311]: index = pd.Index(list(range(5)), name='rows') In [312]: columns = pd.Index(['A', 'B', 'C'], name='cols') In [313]: df = pd.DataFrame(np.random.randn(5, 3), index=index, columns=columns) In [314]: df Out[314]: cols A B C rows 0 1.295989 -1.051694 1.340429 1 -2.366110 0.428241 0.387275 2 0.433306 0.929548 0.278094 3 2.154730 -0.315628 0.264223 4 1.126818 1.132290 -0.353310 In [315]: df['A'] Out[315]: rows 0 1.295989 1 -2.366110 2 0.433306 3 2.154730 4 1.126818 Name: A, dtype: float64 Setting metadata# Indexes are “mostly immutable”, but it is possible to set and change their name attribute. You can use the rename, set_names to set these attributes directly, and they default to returning a copy. See Advanced Indexing for usage of MultiIndexes. In [316]: ind = pd.Index([1, 2, 3]) In [317]: ind.rename("apple") Out[317]: Int64Index([1, 2, 3], dtype='int64', name='apple') In [318]: ind Out[318]: Int64Index([1, 2, 3], dtype='int64') In [319]: ind.set_names(["apple"], inplace=True) In [320]: ind.name = "bob" In [321]: ind Out[321]: Int64Index([1, 2, 3], dtype='int64', name='bob') set_names, set_levels, and set_codes also take an optional level argument In [322]: index = pd.MultiIndex.from_product([range(3), ['one', 'two']], names=['first', 'second']) In [323]: index Out[323]: MultiIndex([(0, 'one'), (0, 'two'), (1, 'one'), (1, 'two'), (2, 'one'), (2, 'two')], names=['first', 'second']) In [324]: index.levels[1] Out[324]: Index(['one', 'two'], dtype='object', name='second') In [325]: index.set_levels(["a", "b"], level=1) Out[325]: MultiIndex([(0, 'a'), (0, 'b'), (1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')], names=['first', 'second']) Set operations on Index objects# The two main operations are union and intersection. Difference is provided via the .difference() method. In [326]: a = pd.Index(['c', 'b', 'a']) In [327]: b = pd.Index(['c', 'e', 'd']) In [328]: a.difference(b) Out[328]: Index(['a', 'b'], dtype='object') Also available is the symmetric_difference operation, which returns elements that appear in either idx1 or idx2, but not in both. This is equivalent to the Index created by idx1.difference(idx2).union(idx2.difference(idx1)), with duplicates dropped. In [329]: idx1 = pd.Index([1, 2, 3, 4]) In [330]: idx2 = pd.Index([2, 3, 4, 5]) In [331]: idx1.symmetric_difference(idx2) Out[331]: Int64Index([1, 5], dtype='int64') Note The resulting index from a set operation will be sorted in ascending order. When performing Index.union() between indexes with different dtypes, the indexes must be cast to a common dtype. Typically, though not always, this is object dtype. The exception is when performing a union between integer and float data. In this case, the integer values are converted to float In [332]: idx1 = pd.Index([0, 1, 2]) In [333]: idx2 = pd.Index([0.5, 1.5]) In [334]: idx1.union(idx2) Out[334]: Float64Index([0.0, 0.5, 1.0, 1.5, 2.0], dtype='float64') Missing values# Important Even though Index can hold missing values (NaN), it should be avoided if you do not want any unexpected results. For example, some operations exclude missing values implicitly. Index.fillna fills missing values with specified scalar value. In [335]: idx1 = pd.Index([1, np.nan, 3, 4]) In [336]: idx1 Out[336]: Float64Index([1.0, nan, 3.0, 4.0], dtype='float64') In [337]: idx1.fillna(2) Out[337]: Float64Index([1.0, 2.0, 3.0, 4.0], dtype='float64') In [338]: idx2 = pd.DatetimeIndex([pd.Timestamp('2011-01-01'), .....: pd.NaT, .....: pd.Timestamp('2011-01-03')]) .....: In [339]: idx2 Out[339]: DatetimeIndex(['2011-01-01', 'NaT', '2011-01-03'], dtype='datetime64[ns]', freq=None) In [340]: idx2.fillna(pd.Timestamp('2011-01-02')) Out[340]: DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03'], dtype='datetime64[ns]', freq=None) Set / reset index# Occasionally you will load or create a data set into a DataFrame and want to add an index after you’ve already done so. There are a couple of different ways. Set an index# DataFrame has a set_index() method which takes a column name (for a regular Index) or a list of column names (for a MultiIndex). To create a new, re-indexed DataFrame: In [341]: data Out[341]: a b c d 0 bar one z 1.0 1 bar two y 2.0 2 foo one x 3.0 3 foo two w 4.0 In [342]: indexed1 = data.set_index('c') In [343]: indexed1 Out[343]: a b d c z bar one 1.0 y bar two 2.0 x foo one 3.0 w foo two 4.0 In [344]: indexed2 = data.set_index(['a', 'b']) In [345]: indexed2 Out[345]: c d a b bar one z 1.0 two y 2.0 foo one x 3.0 two w 4.0 The append keyword option allow you to keep the existing index and append the given columns to a MultiIndex: In [346]: frame = data.set_index('c', drop=False) In [347]: frame = frame.set_index(['a', 'b'], append=True) In [348]: frame Out[348]: c d c a b z bar one z 1.0 y bar two y 2.0 x foo one x 3.0 w foo two w 4.0 Other options in set_index allow you not drop the index columns or to add the index in-place (without creating a new object): In [349]: data.set_index('c', drop=False) Out[349]: a b c d c z bar one z 1.0 y bar two y 2.0 x foo one x 3.0 w foo two w 4.0 In [350]: data.set_index(['a', 'b'], inplace=True) In [351]: data Out[351]: c d a b bar one z 1.0 two y 2.0 foo one x 3.0 two w 4.0 Reset the index# As a convenience, there is a new function on DataFrame called reset_index() which transfers the index values into the DataFrame’s columns and sets a simple integer index. This is the inverse operation of set_index(). In [352]: data Out[352]: c d a b bar one z 1.0 two y 2.0 foo one x 3.0 two w 4.0 In [353]: data.reset_index() Out[353]: a b c d 0 bar one z 1.0 1 bar two y 2.0 2 foo one x 3.0 3 foo two w 4.0 The output is more similar to a SQL table or a record array. The names for the columns derived from the index are the ones stored in the names attribute. You can use the level keyword to remove only a portion of the index: In [354]: frame Out[354]: c d c a b z bar one z 1.0 y bar two y 2.0 x foo one x 3.0 w foo two w 4.0 In [355]: frame.reset_index(level=1) Out[355]: a c d c b z one bar z 1.0 y two bar y 2.0 x one foo x 3.0 w two foo w 4.0 reset_index takes an optional parameter drop which if true simply discards the index, instead of putting index values in the DataFrame’s columns. Adding an ad hoc index# If you create an index yourself, you can just assign it to the index field: data.index = index Returning a view versus a copy# When setting values in a pandas object, care must be taken to avoid what is called chained indexing. Here is an example. In [356]: dfmi = pd.DataFrame([list('abcd'), .....: list('efgh'), .....: list('ijkl'), .....: list('mnop')], .....: columns=pd.MultiIndex.from_product([['one', 'two'], .....: ['first', 'second']])) .....: In [357]: dfmi Out[357]: one two first second first second 0 a b c d 1 e f g h 2 i j k l 3 m n o p Compare these two access methods: In [358]: dfmi['one']['second'] Out[358]: 0 b 1 f 2 j 3 n Name: second, dtype: object In [359]: dfmi.loc[:, ('one', 'second')] Out[359]: 0 b 1 f 2 j 3 n Name: (one, second), dtype: object These both yield the same results, so which should you use? It is instructive to understand the order of operations on these and why method 2 (.loc) is much preferred over method 1 (chained []). dfmi['one'] selects the first level of the columns and returns a DataFrame that is singly-indexed. Then another Python operation dfmi_with_one['second'] selects the series indexed by 'second'. This is indicated by the variable dfmi_with_one because pandas sees these operations as separate events. e.g. separate calls to __getitem__, so it has to treat them as linear operations, they happen one after another. Contrast this to df.loc[:,('one','second')] which passes a nested tuple of (slice(None),('one','second')) to a single call to __getitem__. This allows pandas to deal with this as a single entity. Furthermore this order of operations can be significantly faster, and allows one to index both axes if so desired. Why does assignment fail when using chained indexing?# The problem in the previous section is just a performance issue. What’s up with the SettingWithCopy warning? We don’t usually throw warnings around when you do something that might cost a few extra milliseconds! But it turns out that assigning to the product of chained indexing has inherently unpredictable results. To see this, think about how the Python interpreter executes this code: dfmi.loc[:, ('one', 'second')] = value # becomes dfmi.loc.__setitem__((slice(None), ('one', 'second')), value) But this code is handled differently: dfmi['one']['second'] = value # becomes dfmi.__getitem__('one').__setitem__('second', value) See that __getitem__ in there? Outside of simple cases, it’s very hard to predict whether it will return a view or a copy (it depends on the memory layout of the array, about which pandas makes no guarantees), and therefore whether the __setitem__ will modify dfmi or a temporary object that gets thrown out immediately afterward. That’s what SettingWithCopy is warning you about! Note You may be wondering whether we should be concerned about the loc property in the first example. But dfmi.loc is guaranteed to be dfmi itself with modified indexing behavior, so dfmi.loc.__getitem__ / dfmi.loc.__setitem__ operate on dfmi directly. Of course, dfmi.loc.__getitem__(idx) may be a view or a copy of dfmi. Sometimes a SettingWithCopy warning will arise at times when there’s no obvious chained indexing going on. These are the bugs that SettingWithCopy is designed to catch! pandas is probably trying to warn you that you’ve done this: def do_something(df): foo = df[['bar', 'baz']] # Is foo a view? A copy? Nobody knows! # ... many lines here ... # We don't know whether this will modify df or not! foo['quux'] = value return foo Yikes! Evaluation order matters# When you use chained indexing, the order and type of the indexing operation partially determine whether the result is a slice into the original object, or a copy of the slice. pandas has the SettingWithCopyWarning because assigning to a copy of a slice is frequently not intentional, but a mistake caused by chained indexing returning a copy where a slice was expected. If you would like pandas to be more or less trusting about assignment to a chained indexing expression, you can set the option mode.chained_assignment to one of these values: 'warn', the default, means a SettingWithCopyWarning is printed. 'raise' means pandas will raise a SettingWithCopyError you have to deal with. None will suppress the warnings entirely. In [360]: dfb = pd.DataFrame({'a': ['one', 'one', 'two', .....: 'three', 'two', 'one', 'six'], .....: 'c': np.arange(7)}) .....: # This will show the SettingWithCopyWarning # but the frame values will be set In [361]: dfb['c'][dfb['a'].str.startswith('o')] = 42 This however is operating on a copy and will not work. >>> pd.set_option('mode.chained_assignment','warn') >>> dfb[dfb['a'].str.startswith('o')]['c'] = 42 Traceback (most recent call last) ... SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_index,col_indexer] = value instead A chained assignment can also crop up in setting in a mixed dtype frame. Note These setting rules apply to all of .loc/.iloc. The following is the recommended access method using .loc for multiple items (using mask) and a single item using a fixed index: In [362]: dfc = pd.DataFrame({'a': ['one', 'one', 'two', .....: 'three', 'two', 'one', 'six'], .....: 'c': np.arange(7)}) .....: In [363]: dfd = dfc.copy() # Setting multiple items using a mask In [364]: mask = dfd['a'].str.startswith('o') In [365]: dfd.loc[mask, 'c'] = 42 In [366]: dfd Out[366]: a c 0 one 42 1 one 42 2 two 2 3 three 3 4 two 4 5 one 42 6 six 6 # Setting a single item In [367]: dfd = dfc.copy() In [368]: dfd.loc[2, 'a'] = 11 In [369]: dfd Out[369]: a c 0 one 0 1 one 1 2 11 2 3 three 3 4 two 4 5 one 5 6 six 6 The following can work at times, but it is not guaranteed to, and therefore should be avoided: In [370]: dfd = dfc.copy() In [371]: dfd['a'][2] = 111 In [372]: dfd Out[372]: a c 0 one 0 1 one 1 2 111 2 3 three 3 4 two 4 5 one 5 6 six 6 Last, the subsequent example will not work at all, and so should be avoided: >>> pd.set_option('mode.chained_assignment','raise') >>> dfd.loc[0]['a'] = 1111 Traceback (most recent call last) ... SettingWithCopyError: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_index,col_indexer] = value instead Warning The chained assignment warnings / exceptions are aiming to inform the user of a possibly invalid assignment. There may be false positives; situations where a chained assignment is inadvertently reported.
632
1,176
2 condition in pandas dataFrame indexes I have this rows filtering: idx = dataset.data.groupby(['a', 'b'])['c'].transform(max) == dataset.data['c'] dataset.data = dataset.data[idx] For this dataset: a | b | c | d 0 | 0 | 1 | True 0 | 0 | 2 | True 0 | 1 | 3 | True 0 | 2 | 4 | False 0 | 2 | 5 | False I'll get: a | b | c | d 0 | 0 | 1 | True 0 | 1 | 3 | True 0 | 2 | 5 | False I want to add for this condition that removes only rows that their field 'd' is false, so in the above example I'll get: a | b | c | d 0 | 0 | 1 | True 0 | 0 | 2 | True 0 | 1 | 3 | True 0 | 2 | 5 | False Can someone help me add it, please? Thanks!
[CLS] Python pandas dataFrame indexes True pre [SEP] - 0. 988387 e 1. 289997 0. 082423 f - 0. 489682 0. 369374 In [ 94 ] : df1 [ lambda df : df. columns [ 0 ] ] Out [ 94 ] : a - 0. 023688 b - 0. 251905 c 0. 299368 d - 0. 025747 e 1. 289997 f - 0. 489682 Name : A, dtype : float64 You can use callable indexing in Series. In [ 95 ] : df1 [ ' A ' ]. loc [ lambda s : s > 0 ] Out [ 95 ] : c 0. 299368 e 1. 289997 Name : A, dtype : float64 Using these methods / indexers, you can chain data selection operations without using a temporary variable. In [ 96 ] : bb = pd. read _ csv ( ' data / baseball. csv ', index _ col = ' id ' ) In [ 97 ] : ( bb. groupby ( [ ' year ', ' team ' ] ). sum ( numeric _ only = True /
IIUC, keep rows where 'c' is the max or d is True: import pandas as pd df = pd.DataFrame({ 'a': [0, 0, 0, 0, 0], 'b': [0, 0, 1, 2, 2], 'c': [1, 2, 3, 4, 5], 'd': [True, True, True, False, False] }) # Max value of C c1 = df.groupby(['a', 'b'])['c'].transform(max) == df['c'] # D is True c2 = df.d # Or together idx = c1 | c2 print(df[idx]) df[idx]: a b c d 0 0 0 1 True 1 0 0 2 True 2 0 1 3 True 4 0 2 5 False The one-liner: df[df.groupby(['a', 'b'])['c'].transform(max).eq(df['c']) | df['d']]
61,450,091
Python appending a list of elements to a row of an empty dataframe
<p>My list has many elements. I want to save them to a newly created dataframe. I want to append each element of this list as a column element to the dataframe. </p> <p>My code: </p> <pre><code>oplist = [[2,3,4,10,12,3,4],-0.4,1.23456E-6] # list saves all output values opdf = pd.Dataframe() #creating an empty dataframe opdf.append(oplist) </code></pre> <p>Present output: </p> <pre><code>TypeError: object of type 'numpy.float64' has no len() </code></pre> <p>Expected output: </p> <pre><code>opdf = 0 1 2 # unnamed columns 0 [2,3,4,10,12,3,4] -0.4 1.23456E-6 # first row </code></pre>
61,450,242
"2020-04-27T00:49:44.287000"
1
null
-1
40
python|pandas
<p>I do not know what exactly you are looking for but this gives you the output you have there</p> <pre><code>opdf = pd.DataFrame() #creating an empty dataframe opdf.append(pd.Series(oplist), ignore_index=True) </code></pre>
"2020-04-27T01:13:13.263000"
2
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.append.html
pandas.DataFrame.append# pandas.DataFrame.append# DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=False)[source]# Append rows of other to the end of caller, returning a new object. I do not know what exactly you are looking for but this gives you the output you have there opdf = pd.DataFrame() #creating an empty dataframe opdf.append(pd.Series(oplist), ignore_index=True) Deprecated since version 1.4.0: Use concat() instead. For further details see Deprecated DataFrame.append and Series.append Columns in other that are not in the caller are added as new columns. Parameters otherDataFrame or Series/dict-like object, or list of theseThe data to append. ignore_indexbool, default FalseIf True, the resulting axis will be labeled 0, 1, …, n - 1. verify_integritybool, default FalseIf True, raise ValueError on creating index with duplicates. sortbool, default FalseSort columns if the columns of self and other are not aligned. Changed in version 1.0.0: Changed to not sort by default. Returns DataFrameA new DataFrame consisting of the rows of caller and the rows of other. See also concatGeneral function to concatenate DataFrame or Series objects. Notes If a list of dict/series is passed and the keys are all contained in the DataFrame’s index, the order of the columns in the resulting DataFrame will be unchanged. Iteratively appending rows to a DataFrame can be more computationally intensive than a single concatenate. A better solution is to append those rows to a list and then concatenate the list with the original DataFrame all at once. Examples >>> df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'), index=['x', 'y']) >>> df A B x 1 2 y 3 4 >>> df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'), index=['x', 'y']) >>> df.append(df2) A B x 1 2 y 3 4 x 5 6 y 7 8 With ignore_index set to True: >>> df.append(df2, ignore_index=True) A B 0 1 2 1 3 4 2 5 6 3 7 8 The following, while not recommended methods for generating DataFrames, show two ways to generate a DataFrame from multiple data sources. Less efficient: >>> df = pd.DataFrame(columns=['A']) >>> for i in range(5): ... df = df.append({'A': i}, ignore_index=True) >>> df A 0 0 1 1 2 2 3 3 4 4 More efficient: >>> pd.concat([pd.DataFrame([i], columns=['A']) for i in range(5)], ... ignore_index=True) A 0 0 1 1 2 2 3 3 4 4
210
403
Python appending a list of elements to a row of an empty dataframe My list has many elements. I want to save them to a newly created dataframe. I want to append each element of this list as a column element to the dataframe. My code: oplist = [[2,3,4,10,12,3,4],-0.4,1.23456E-6] # list saves all output values opdf = pd.Dataframe() #creating an empty dataframe opdf.append(oplist) Present output: TypeError: object of type 'numpy.float64' has no len() Expected output: opdf = 0 1 2 # unnamed columns 0 [2,3,4,10,12,3,4] -0.4 1.23456E-6 # first row
I do not know what exactly you are looking for but this gives you the output you have there opdf = pd.DataFrame() #creating an empty dataframe opdf.append(pd.Series(oplist), ignore_index=True)
68,778,109
Check the difference in one column based on duplicated values in other two column
<p>I have an example data frame like this.</p> <pre><code>animal color price rabit red 10 turtle green 15 rabit red 12 turtle green 15 turtle green 10 sheep white 5 sheep white 5 </code></pre> <p>If the values in animal and color are the same like row 1 and row 3 but price is different, return the rows with different price. Results shoule be something like this:</p> <pre><code>animal color price rabit red 10 rabit red 12 turtle green 15 turtle green 10 </code></pre> <p>Thank you.</p>
68,778,330
"2021-08-13T20:37:12.700000"
2
null
0
58
python|pandas
<p>Try this -</p> <p>Make sure to use the <code>drop_duplicates()</code> first, before using <code>pd.duplicated</code> with <code>keep=False</code>, for your intended purpose.</p> <pre><code>out = df.drop_duplicates() out = out[out.duplicated(subset=['animal','color'], keep=False)] print(out) </code></pre> <pre><code> animal color price 0 rabit red 10 1 turtle green 15 2 rabit red 12 4 turtle green 10 </code></pre> <hr /> <p><strong>NOTE!!:</strong> If you use <code>drop_duplicates()</code> as the second step, you get the unexpected output as below.</p> <pre><code>out = df[df.duplicated(subset=['animal','color'], keep=False)] out = out.drop_duplicates() print(out) </code></pre> <pre><code> animal color price 0 rabit red 10 1 turtle green 15 2 rabit red 12 4 turtle green 10 5 sheep white 5 </code></pre> <hr /> <h3>Why does this happen?</h3> <p>Intuitively, <code>drop_duplicates()</code> removes duplicates based on all the columns together and <code>df.duplicated(keep=False)</code> marks all duplicates based on the 2 given columns.</p> <p><strong>Scenario 1:</strong> Remove all rows that have the same 2 columns and price, then keep the duplicates based on 2 columns &gt; This means you will be left with rows with different prices but duplicate 2 columns.</p> <p><strong>Scenario 2:</strong> Keep all rows that are duplicated based on 2 columns (which would not remove anything in this example). Then drop duplicate rows where the price is the same. Ends up being the same as just dropping duplicate rows across all columns.</p>
"2021-08-13T21:05:19.267000"
2
https://pandas.pydata.org/docs/dev/user_guide/merging.html
Try this - Make sure to use the drop_duplicates() first, before using pd.duplicated with keep=False, for your intended purpose. out = df.drop_duplicates() out = out[out.duplicated(subset=['animal','color'], keep=False)] print(out) animal color price 0 rabit red 10 1 turtle green 15 2 rabit red 12 4 turtle green 10 NOTE!!: If you use drop_duplicates() as the second step, you get the unexpected output as below. out = df[df.duplicated(subset=['animal','color'], keep=False)] out = out.drop_duplicates() print(out) animal color price 0 rabit red 10 1 turtle green 15 2 rabit red 12 4 turtle green 10 5 sheep white 5 Why does this happen? Intuitively, drop_duplicates() removes duplicates based on all the columns together and df.duplicated(keep=False) marks all duplicates based on the 2 given columns. Scenario 1: Remove all rows that have the same 2 columns and price, then keep the duplicates based on 2 columns > This means you will be left with rows with different prices but duplicate 2 columns. Scenario 2: Keep all rows that are duplicated based on 2 columns (which would not remove anything in this example). Then drop duplicate rows where the price is the same. Ends up being the same as just dropping duplicate rows across all columns.
0
1,329
Check the difference in one column based on duplicated values in other two column I have an example data frame like this. animal color price rabit red 10 turtle green 15 rabit red 12 turtle green 15 turtle green 10 sheep white 5 sheep white 5 If the values in animal and color are the same like row 1 and row 3 but price is different, return the rows with different price. Results shoule be something like this: animal color price rabit red 10 rabit red 12 turtle green 15 turtle green 10 Thank you.
Check the difference in one column based on duplicated values in other two column
Try this - Make sure to use the drop_duplicates() first, before using pd.duplicated with keep=False, for your intended purpose. out = df.drop_duplicates() out = out[out.duplicated(subset=['animal','color'], keep=False)] print(out) animal color price 0 rabit red 10 1 turtle green 15 2 rabit red 12 4 turtle green 10 NOTE!!: If you use drop_duplicates() as the second step, you get the unexpected output as below. out = df[df.duplicated(subset=['animal','color'], keep=False)] out = out.drop_duplicates() print(out) animal color price 0 rabit red 10 1 turtle green 15 2 rabit red 12 4 turtle green 10 5 sheep white 5 Why does this happen? Intuitively, drop_duplicates() removes duplicates based on all the columns together and df.duplicated(keep=False) marks all duplicates based on the 2 given columns. Scenario 1: Remove all rows that have the same 2 columns and price, then keep the duplicates based on 2 columns > This means you will be left with rows with different prices but duplicate 2 columns. Scenario 2: Keep all rows that are duplicated based on 2 columns (which would not remove anything in this example). Then drop duplicate rows where the price is the same. Ends up being the same as just dropping duplicate rows across all columns.
66,259,724
How to check if groups of data are connected in python
<p>Imagine we have a data set like this:</p> <pre><code>import pandas as pd import numpy as np df = pd.DataFrame({'ID':[1,1,1,2,2,2,3,3,3,4,4,4], 'Number':[234, 43, 455, 112, 45, 234, 982, 41, 112, 46, 109, 4]}) </code></pre> <p>Now, if you run the code above, you'll see that ID's 1,2 and 3 are all 'connected', i.e. ID 1 is connected to ID 2 via Number == 234, and ID 2 is connected to ID 3 via Number == 112. But ID 4 is not connected to these other 3 ID's. So I would like to have a df_final like this</p> <pre><code>df_final = pd.DataFrame({'ID':[1,1,1,2,2,2,3,3,3,4,4,4], 'Number':[234, 43, 455, 112, 45, 234, 982, 41, 112, 46, 109, 4], 'Group':[1,1,1,1,1,1,1,1,1,2,2,2]}) </code></pre> <p>My data set is large, so I need some code that is as efficient as possible. I defined an 'is_a_neighbor' function to check if there is overlap between two ID's. It returns 'True' if two ID's are connected (i.e. have an overlap). I suppose I'll need to embed it in a loop somehow. Here's the function:</p> <pre><code>def is_a_neighbor(id_1, id_2): val = np.isin(df[df['ID'] == id_1][['Number']].to_numpy(), df[df['ID'] == id_2][['Number']].to_numpy()).any() return val </code></pre> <p>and a test run</p> <pre><code># Test is_a_neighbor function id_1 = 1 id_2 = 2 val = is_a_neighbor(id_1, id_2) val </code></pre> <p>Which will return 'True'. Thanks for any help/guidance on this one.</p>
66,261,253
"2021-02-18T12:12:39.313000"
2
1
1
78
python|pandas
<p>The following code should work:</p> <pre class="lang-py prettyprint-override"><code>import pandas as pd import networkx as nx df = pd.DataFrame({'ID':[1,1,1,2,2,2,3,3,3,4,4,4], 'Number':[234, 43, 455, 112, 45, 234, 982, 41, 112, 46, 109, 4]}) df.ID = df.ID.astype(str) g = nx.from_pandas_edgelist(df,'ID','Number') connected_components = nx.connected_components(g) d = {y:i for i, x in enumerate(connected_components) for y in x} df['group'] = df.ID.replace(d) df.ID = df.ID.astype(int) </code></pre> <p>The reason why we convert the <code>ID</code> column to a string is because we want <code>1</code> in the <code>ID</code> column to be distinct from <code>1</code> in the <code>Number</code> column.</p> <p>If we didn't do that the following example would show only one group:</p> <div class="s-table-container"> <table class="s-table"> <thead> <tr> <th>ID</th> <th>Number</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>3</td> </tr> <tr> <td>2</td> <td>3</td> </tr> <tr> <td>3</td> <td>4</td> </tr> </tbody> </table> </div> <p>To expand on the speed of the code, the following test resulted in an average time 3.4 seconds:</p> <pre class="lang-py prettyprint-override"><code>import pandas as pd import numpy as np import networkx as nx import timeit test_df = pd.DataFrame({'ID': np.random.randint(0, 10000, 10000), 'Number': np.random.randint(0, 10000, 10000)}) def add_group(df): df.ID = df.ID.astype(str) g = nx.from_pandas_edgelist(df,'ID','Number') connected_components = nx.connected_components(g) d = {y:i for i, x in enumerate(connected_components) for y in x} df['group'] = df.ID.replace(d) df.ID = df.ID.astype(int) return df timeit.timeit(lambda : add_group(test_df), number=100) </code></pre>
"2021-02-18T13:45:31.573000"
2
https://pandas.pydata.org/docs/user_guide/groupby.html
The following code should work: import pandas as pd import networkx as nx df = pd.DataFrame({'ID':[1,1,1,2,2,2,3,3,3,4,4,4], 'Number':[234, 43, 455, 112, 45, 234, 982, 41, 112, 46, 109, 4]}) df.ID = df.ID.astype(str) g = nx.from_pandas_edgelist(df,'ID','Number') connected_components = nx.connected_components(g) d = {y:i for i, x in enumerate(connected_components) for y in x} df['group'] = df.ID.replace(d) df.ID = df.ID.astype(int) The reason why we convert the ID column to a string is because we want 1 in the ID column to be distinct from 1 in the Number column. If we didn't do that the following example would show only one group: ID Number 1 3 2 3 3 4 To expand on the speed of the code, the following test resulted in an average time 3.4 seconds: import pandas as pd import numpy as np import networkx as nx import timeit test_df = pd.DataFrame({'ID': np.random.randint(0, 10000, 10000), 'Number': np.random.randint(0, 10000, 10000)}) def add_group(df): df.ID = df.ID.astype(str) g = nx.from_pandas_edgelist(df,'ID','Number') connected_components = nx.connected_components(g) d = {y:i for i, x in enumerate(connected_components) for y in x} df['group'] = df.ID.replace(d) df.ID = df.ID.astype(int) return df timeit.timeit(lambda : add_group(test_df), number=100)
0
1,369
How to check if groups of data are connected in python Imagine we have a data set like this: import pandas as pd import numpy as np df = pd.DataFrame({'ID':[1,1,1,2,2,2,3,3,3,4,4,4], 'Number':[234, 43, 455, 112, 45, 234, 982, 41, 112, 46, 109, 4]}) Now, if you run the code above, you'll see that ID's 1,2 and 3 are all 'connected', i.e. ID 1 is connected to ID 2 via Number == 234, and ID 2 is connected to ID 3 via Number == 112. But ID 4 is not connected to these other 3 ID's. So I would like to have a df_final like this df_final = pd.DataFrame({'ID':[1,1,1,2,2,2,3,3,3,4,4,4], 'Number':[234, 43, 455, 112, 45, 234, 982, 41, 112, 46, 109, 4], 'Group':[1,1,1,1,1,1,1,1,1,2,2,2]}) My data set is large, so I need some code that is as efficient as possible. I defined an 'is_a_neighbor' function to check if there is overlap between two ID's. It returns 'True' if two ID's are connected (i.e. have an overlap). I suppose I'll need to embed it in a loop somehow. Here's the function: def is_a_neighbor(id_1, id_2): val = np.isin(df[df['ID'] == id_1][['Number']].to_numpy(), df[df['ID'] == id_2][['Number']].to_numpy()).any() return val and a test run # Test is_a_neighbor function id_1 = 1 id_2 = 2 val = is_a_neighbor(id_1, id_2) val Which will return 'True'. Thanks for any help/guidance on this one.
The following code should work: import pandas as pd import networkx as nx df = pd.DataFrame({'ID':[1,1,1,2,2,2,3,3,3,4,4,4], 'Number':[234, 43, 455, 112, 45, 234, 982, 41, 112, 46, 109, 4]}) df.ID = df.ID.astype(str) g = nx.from_pandas_edgelist(df,'ID','Number') connected_components = nx.connected_components(g) d = {y:i for i, x in enumerate(connected_components) for y in x} df['group'] = df.ID.replace(d) df.ID = df.ID.astype(int) The reason why we convert the ID column to a string is because we want 1 in the ID column to be distinct from 1 in the Number column. If we didn't do that the following example would show only one group: ID Number 1 3 2 3 3 4 To expand on the speed of the code, the following test resulted in an average time 3.4 seconds: import pandas as pd import numpy as np import networkx as nx import timeit test_df = pd.DataFrame({'ID': np.random.randint(0, 10000, 10000), 'Number': np.random.randint(0, 10000, 10000)}) def add_group(df): df.ID = df.ID.astype(str) g = nx.from_pandas_edgelist(df,'ID','Number') connected_components = nx.connected_components(g) d = {y:i for i, x in enumerate(connected_components) for y in x} df['group'] = df.ID.replace(d) df.ID = df.ID.astype(int) return df timeit.timeit(lambda : add_group(test_df), number=100)
60,131,565
copy specific column value to another column that matches specific string
<p>I'm using Python lib pandas. For all the rows in a data frame in which a specific column matches a specific string, I want to copy that value from the column at the left to the current column.</p> <p>For example, for all rows with the column City with the value 'not available', I want to copy the value from the column at the left, let's say Country, to the current column City.</p> <pre><code>import pandas as pd df = pd.DataFrame({'Country': ['France','England','U.S.A','Spain'], 'City': ['Paris','London','not available','not available']}) print(df) Country City 0 France Paris 1 England London 2 U.S.A not available 3 Spain not available </code></pre>
60,132,696
"2020-02-08T21:56:34.137000"
1
1
0
617
python|pandas
<p>There are a lot of ways to achieve this. One is using the loc property:</p> <pre><code>import pandas as pd df = pd.DataFrame({'Country': ['France','England','U.S.A','Spain'], 'City': ['Paris','London','N/A','N/A']}) print(df) Country City 0 France Paris 1 England London 2 U.S.A N/A 3 Spain N/A </code></pre> <p>Then:</p> <pre><code>df.loc[df['City'] == 'N/A', 'City'] = df['Country'] Country City 0 France Paris 1 England London 2 U.S.A U.S.A 3 Spain Spain </code></pre> <p>For more complex recipes check the <a href="https://pandas.pydata.org/pandas-docs/stable/user_guide/cookbook.html" rel="nofollow noreferrer">Cookbook</a></p>
"2020-02-09T01:11:27.597000"
2
https://pandas.pydata.org/docs/reference/api/pandas.merge.html
pandas.merge# pandas.merge# pandas.merge(left, right, how='inner', on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=False, suffixes=('_x', '_y'), copy=True, indicator=False, validate=None)[source]# Merge DataFrame or named Series objects with a database-style join. A named Series object is treated as a DataFrame with a single named column. The join is done on columns or indexes. If joining columns on There are a lot of ways to achieve this. One is using the loc property: import pandas as pd df = pd.DataFrame({'Country': ['France','England','U.S.A','Spain'], 'City': ['Paris','London','N/A','N/A']}) print(df) Country City 0 France Paris 1 England London 2 U.S.A N/A 3 Spain N/A Then: df.loc[df['City'] == 'N/A', 'City'] = df['Country'] Country City 0 France Paris 1 England London 2 U.S.A U.S.A 3 Spain Spain For more complex recipes check the Cookbook columns, the DataFrame indexes will be ignored. Otherwise if joining indexes on indexes or indexes on a column or columns, the index will be passed on. When performing a cross merge, no column specifications to merge on are allowed. Warning If both key columns contain rows where the key is a null value, those rows will be matched against each other. This is different from usual SQL join behaviour and can lead to unexpected results. Parameters leftDataFrame rightDataFrame or named SeriesObject to merge with. how{‘left’, ‘right’, ‘outer’, ‘inner’, ‘cross’}, default ‘inner’Type of merge to be performed. left: use only keys from left frame, similar to a SQL left outer join; preserve key order. right: use only keys from right frame, similar to a SQL right outer join; preserve key order. outer: use union of keys from both frames, similar to a SQL full outer join; sort keys lexicographically. inner: use intersection of keys from both frames, similar to a SQL inner join; preserve the order of the left keys. cross: creates the cartesian product from both frames, preserves the order of the left keys. New in version 1.2.0. onlabel or listColumn or index level names to join on. These must be found in both DataFrames. If on is None and not merging on indexes then this defaults to the intersection of the columns in both DataFrames. left_onlabel or list, or array-likeColumn or index level names to join on in the left DataFrame. Can also be an array or list of arrays of the length of the left DataFrame. These arrays are treated as if they are columns. right_onlabel or list, or array-likeColumn or index level names to join on in the right DataFrame. Can also be an array or list of arrays of the length of the right DataFrame. These arrays are treated as if they are columns. left_indexbool, default FalseUse the index from the left DataFrame as the join key(s). If it is a MultiIndex, the number of keys in the other DataFrame (either the index or a number of columns) must match the number of levels. right_indexbool, default FalseUse the index from the right DataFrame as the join key. Same caveats as left_index. sortbool, default FalseSort the join keys lexicographically in the result DataFrame. If False, the order of the join keys depends on the join type (how keyword). suffixeslist-like, default is (“_x”, “_y”)A length-2 sequence where each element is optionally a string indicating the suffix to add to overlapping column names in left and right respectively. Pass a value of None instead of a string to indicate that the column name from left or right should be left as-is, with no suffix. At least one of the values must not be None. copybool, default TrueIf False, avoid copy if possible. indicatorbool or str, default FalseIf True, adds a column to the output DataFrame called “_merge” with information on the source of each row. The column can be given a different name by providing a string argument. The column will have a Categorical type with the value of “left_only” for observations whose merge key only appears in the left DataFrame, “right_only” for observations whose merge key only appears in the right DataFrame, and “both” if the observation’s merge key is found in both DataFrames. validatestr, optionalIf specified, checks if merge is of specified type. “one_to_one” or “1:1”: check if merge keys are unique in both left and right datasets. “one_to_many” or “1:m”: check if merge keys are unique in left dataset. “many_to_one” or “m:1”: check if merge keys are unique in right dataset. “many_to_many” or “m:m”: allowed, but does not result in checks. Returns DataFrameA DataFrame of the two merged objects. See also merge_orderedMerge with optional filling/interpolation. merge_asofMerge on nearest keys. DataFrame.joinSimilar method using indices. Notes Support for specifying index levels as the on, left_on, and right_on parameters was added in version 0.23.0 Support for merging named Series objects was added in version 0.24.0 Examples >>> df1 = pd.DataFrame({'lkey': ['foo', 'bar', 'baz', 'foo'], ... 'value': [1, 2, 3, 5]}) >>> df2 = pd.DataFrame({'rkey': ['foo', 'bar', 'baz', 'foo'], ... 'value': [5, 6, 7, 8]}) >>> df1 lkey value 0 foo 1 1 bar 2 2 baz 3 3 foo 5 >>> df2 rkey value 0 foo 5 1 bar 6 2 baz 7 3 foo 8 Merge df1 and df2 on the lkey and rkey columns. The value columns have the default suffixes, _x and _y, appended. >>> df1.merge(df2, left_on='lkey', right_on='rkey') lkey value_x rkey value_y 0 foo 1 foo 5 1 foo 1 foo 8 2 foo 5 foo 5 3 foo 5 foo 8 4 bar 2 bar 6 5 baz 3 baz 7 Merge DataFrames df1 and df2 with specified left and right suffixes appended to any overlapping columns. >>> df1.merge(df2, left_on='lkey', right_on='rkey', ... suffixes=('_left', '_right')) lkey value_left rkey value_right 0 foo 1 foo 5 1 foo 1 foo 8 2 foo 5 foo 5 3 foo 5 foo 8 4 bar 2 bar 6 5 baz 3 baz 7 Merge DataFrames df1 and df2, but raise an exception if the DataFrames have any overlapping columns. >>> df1.merge(df2, left_on='lkey', right_on='rkey', suffixes=(False, False)) Traceback (most recent call last): ... ValueError: columns overlap but no suffix specified: Index(['value'], dtype='object') >>> df1 = pd.DataFrame({'a': ['foo', 'bar'], 'b': [1, 2]}) >>> df2 = pd.DataFrame({'a': ['foo', 'baz'], 'c': [3, 4]}) >>> df1 a b 0 foo 1 1 bar 2 >>> df2 a c 0 foo 3 1 baz 4 >>> df1.merge(df2, how='inner', on='a') a b c 0 foo 1 3 >>> df1.merge(df2, how='left', on='a') a b c 0 foo 1 3.0 1 bar 2 NaN >>> df1 = pd.DataFrame({'left': ['foo', 'bar']}) >>> df2 = pd.DataFrame({'right': [7, 8]}) >>> df1 left 0 foo 1 bar >>> df2 right 0 7 1 8 >>> df1.merge(df2, how='cross') left right 0 foo 7 1 foo 8 2 bar 7 3 bar 8
439
976
copy specific column value to another column that matches specific string I'm using Python lib pandas. For all the rows in a data frame in which a specific column matches a specific string, I want to copy that value from the column at the left to the current column. For example, for all rows with the column City with the value 'not available', I want to copy the value from the column at the left, let's say Country, to the current column City. import pandas as pd df = pd.DataFrame({'Country': ['France','England','U.S.A','Spain'], 'City': ['Paris','London','not available','not available']}) print(df) Country City 0 France Paris 1 England London 2 U.S.A not available 3 Spain not available
There are a lot of ways to achieve this. One is using the loc property: import pandas as pd df = pd.DataFrame({'Country': ['France','England','U.S.A','Spain'], 'City': ['Paris','London','N/A','N/A']}) print(df) Country City 0 France Paris 1 England London 2 U.S.A N/A 3 Spain N/A Then: df.loc[df['City'] == 'N/A', 'City'] = df['Country'] Country City 0 France Paris 1 England London 2 U.S.A U.S.A 3 Spain Spain For more complex recipes check the Cookbook
63,988,804
How to infer frequency from an index where a few observations are missing?
<p>Using <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.date_range.html" rel="nofollow noreferrer">pd.date_range</a> like <code>dr = pd.date_range('2020', freq='15min', periods=n_obs)</code> will produce this DateTimeIndex with a 15 minute interval or frequency:</p> <pre><code>DatetimeIndex(['2020-01-01 00:00:00', '2020-01-01 00:15:00', '2020-01-01 00:30:00', '2020-01-01 00:45:00', '2020-01-01 01:00:00'], dtype='datetime64[ns]', freq='15T') </code></pre> <p>You can use this to set up a pandas dataframe like:</p> <pre><code>import pandas as pd import numpy as np # data np.random.seed(10) n_obs = 10 daterange = pd.date_range('2020', freq='15min', periods=n_obs) values = np.random.uniform(low=-1, high=1, size=n_obs).tolist() df = pd.DataFrame({'time':daterange, 'value':values}) df = df.set_index('time') </code></pre> <p>And now you can use <code>pd.infer_freq(df.index)</code> to retreive the frequency <code>'15T'</code> again for further calculations. Taking a closer look at <code>help(pd.infer_freq())</code> lets us know that <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.infer_freq.html" rel="nofollow noreferrer">pd.infer_freq</a> will:</p> <blockquote> <pre><code>Infer the most likely frequency given the input index. If the frequency is uncertain, a warning will be printed. </code></pre> </blockquote> <p>My understanding of this would be that it would be possible to retrieve <code>'15T'</code> if a few observations were missing, leading to an irregular time index. But when I remove a few of the observations using:</p> <pre><code>dropped = df.index[[1,3]] df = df.drop(dropped) </code></pre> <p>Then <code>pd.infer_freq(df.index)</code> returns <code>None</code>. This also happens if we set <code>n_obs = 100</code>. So it would seem that I was hoping for a bit too much when I thought that <code>[...] infer the most likely frequency [...]</code> meant that <code>pd.infer_freq()</code> could infer that this was in fact an index with a 15 minute frequency with only a few missing values. Is there any other approach I could use to programmatically infer an index frequency from a somewhat irregular time series using pandas?</p>
63,989,486
"2020-09-21T08:37:51.137000"
1
null
5
879
python|pandas
<p>You could compute the minimum time difference of values in the index (here <code>min_delta</code>), try to find 3 consecutive values in the index, each with this minimum time difference between them, and then call <code>infer_freq</code> on these consecutive values of the index:</p> <pre class="lang-py prettyprint-override"><code>diffs = (df.index[1:] - df.index[:-1]) min_delta = diffs.min() mask = (diffs == min_delta)[:-1] &amp; (diffs[:-1] == diffs[1:]) pos = np.where(mask)[0][0] print(pd.infer_freq(idx[pos: pos + 3])) </code></pre> <p>This retrieves &quot;15T&quot;.</p>
"2020-09-21T09:24:51.937000"
2
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.asfreq.html
pandas.DataFrame.asfreq# pandas.DataFrame.asfreq# DataFrame.asfreq(freq, method=None, how=None, normalize=False, fill_value=None)[source]# Convert time series to specified frequency. Returns the original data conformed to a new index with the specified frequency. If the index of this DataFrame is a PeriodIndex, the new index You could compute the minimum time difference of values in the index (here min_delta), try to find 3 consecutive values in the index, each with this minimum time difference between them, and then call infer_freq on these consecutive values of the index: diffs = (df.index[1:] - df.index[:-1]) min_delta = diffs.min() mask = (diffs == min_delta)[:-1] & (diffs[:-1] == diffs[1:]) pos = np.where(mask)[0][0] print(pd.infer_freq(idx[pos: pos + 3])) This retrieves "15T". is the result of transforming the original index with PeriodIndex.asfreq (so the original index will map one-to-one to the new index). Otherwise, the new index will be equivalent to pd.date_range(start, end, freq=freq) where start and end are, respectively, the first and last entries in the original index (see pandas.date_range()). The values corresponding to any timesteps in the new index which were not present in the original index will be null (NaN), unless a method for filling such unknowns is provided (see the method parameter below). The resample() method is more appropriate if an operation on each group of timesteps (such as an aggregate) is necessary to represent the data at the new frequency. Parameters freqDateOffset or strFrequency DateOffset or string. method{‘backfill’/’bfill’, ‘pad’/’ffill’}, default NoneMethod to use for filling holes in reindexed Series (note this does not fill NaNs that already were present): ‘pad’ / ‘ffill’: propagate last valid observation forward to next valid ‘backfill’ / ‘bfill’: use NEXT valid observation to fill. how{‘start’, ‘end’}, default endFor PeriodIndex only (see PeriodIndex.asfreq). normalizebool, default FalseWhether to reset output index to midnight. fill_valuescalar, optionalValue to use for missing values, applied during upsampling (note this does not fill NaNs that already were present). Returns DataFrameDataFrame object reindexed to the specified frequency. See also reindexConform DataFrame to new index with optional filling logic. Notes To learn more about the frequency strings, please see this link. Examples Start by creating a series with 4 one minute timestamps. >>> index = pd.date_range('1/1/2000', periods=4, freq='T') >>> series = pd.Series([0.0, None, 2.0, 3.0], index=index) >>> df = pd.DataFrame({'s': series}) >>> df s 2000-01-01 00:00:00 0.0 2000-01-01 00:01:00 NaN 2000-01-01 00:02:00 2.0 2000-01-01 00:03:00 3.0 Upsample the series into 30 second bins. >>> df.asfreq(freq='30S') s 2000-01-01 00:00:00 0.0 2000-01-01 00:00:30 NaN 2000-01-01 00:01:00 NaN 2000-01-01 00:01:30 NaN 2000-01-01 00:02:00 2.0 2000-01-01 00:02:30 NaN 2000-01-01 00:03:00 3.0 Upsample again, providing a fill value. >>> df.asfreq(freq='30S', fill_value=9.0) s 2000-01-01 00:00:00 0.0 2000-01-01 00:00:30 9.0 2000-01-01 00:01:00 NaN 2000-01-01 00:01:30 9.0 2000-01-01 00:02:00 2.0 2000-01-01 00:02:30 9.0 2000-01-01 00:03:00 3.0 Upsample again, providing a method. >>> df.asfreq(freq='30S', method='bfill') s 2000-01-01 00:00:00 0.0 2000-01-01 00:00:30 NaN 2000-01-01 00:01:00 NaN 2000-01-01 00:01:30 2.0 2000-01-01 00:02:00 2.0 2000-01-01 00:02:30 3.0 2000-01-01 00:03:00 3.0
331
798
How to infer frequency from an index where a few observations are missing? Using pd.date_range like dr = pd.date_range('2020', freq='15min', periods=n_obs) will produce this DateTimeIndex with a 15 minute interval or frequency: DatetimeIndex(['2020-01-01 00:00:00', '2020-01-01 00:15:00', '2020-01-01 00:30:00', '2020-01-01 00:45:00', '2020-01-01 01:00:00'], dtype='datetime64[ns]', freq='15T') You can use this to set up a pandas dataframe like: import pandas as pd import numpy as np # data np.random.seed(10) n_obs = 10 daterange = pd.date_range('2020', freq='15min', periods=n_obs) values = np.random.uniform(low=-1, high=1, size=n_obs).tolist() df = pd.DataFrame({'time':daterange, 'value':values}) df = df.set_index('time') And now you can use pd.infer_freq(df.index) to retreive the frequency '15T' again for further calculations. Taking a closer look at help(pd.infer_freq()) lets us know that pd.infer_freq will: Infer the most likely frequency given the input index. If the frequency is uncertain, a warning will be printed. My understanding of this would be that it would be possible to retrieve '15T' if a few observations were missing, leading to an irregular time index. But when I remove a few of the observations using: dropped = df.index[[1,3]] df = df.drop(dropped) Then pd.infer_freq(df.index) returns None. This also happens if we set n_obs = 100. So it would seem that I was hoping for a bit too much when I thought that [...] infer the most likely frequency [...] meant that pd.infer_freq() could infer that this was in fact an index with a 15 minute frequency with only a few missing values. Is there any other approach I could use to programmatically infer an index frequency from a somewhat irregular time series using pandas?
You could compute the minimum time difference of values in the index (here min_delta), try to find 3 consecutive values in the index, each with this minimum time difference between them, and then call infer_freq on these consecutive values of the index: diffs = (df.index[1:] - df.index[:-1]) min_delta = diffs.min() mask = (diffs == min_delta)[:-1] & (diffs[:-1] == diffs[1:]) pos = np.where(mask)[0][0] print(pd.infer_freq(idx[pos: pos + 3])) This retrieves "15T".
62,509,730
group by rank continuous date by pandas
<p>I refer this <a href="https://stackoverflow.com/questions/53265362/pandas-groupby-rank-date-time">post</a> . But My goal is something different.</p> <p><strong>Example</strong></p> <pre><code>ID TIME 01 2018-07-11 01 2018-07-12 01 2018-07-13 01 2018-07-15 01 2018-07-16 01 2018-07-17 02 2019-09-11 02 2019-09-12 02 2019-09-15 02 2019-09-16 </code></pre> <p>Notice: For each id, the date is unique.</p> <p><strong>Expected</strong></p> <pre><code>ID TIME RANK 01 2018-07-11 1 01 2018-07-12 2 01 2018-07-13 3 01 2018-07-15 1 01 2018-07-16 2 01 2018-07-17 3 02 2019-09-11 1 02 2019-09-12 2 02 2019-09-15 1 02 2019-09-16 2 </code></pre> <p>For each id, the rank of continuous date does not change.IF not, the rank restarts.</p> <p><strong>Goal</strong></p> <p>How to get the result.</p> <p><strong>Try</strong></p> <p><code>df.groupby('ID')['TIME'].rank(ascending=True)</code> failed</p>
62,509,858
"2020-06-22T07:51:11.767000"
2
null
1
128
pandas
<p>First we check the difference between the dates, which are <code>&gt; 1 day</code>. Then we groupby on <code>ID</code> and the <code>cumsum</code> of these differences and <code>cumulative count</code> each group`</p> <pre><code># df['TIME'] = pd.to_datetime(df['TIME']) s = df['TIME'].diff().fillna(pd.Timedelta(days=1)).ne(pd.Timedelta(days=1)) df['RANK'] = s.groupby([df['ID'], s.cumsum()]).cumcount().add(1) ID TIME RANK 0 1 2018-07-11 1 1 1 2018-07-12 2 2 1 2018-07-13 3 3 1 2018-07-15 1 4 1 2018-07-16 2 5 1 2018-07-17 3 6 2 2019-09-11 1 7 2 2019-09-12 2 8 2 2019-09-15 1 9 2 2019-09-16 2 </code></pre>
"2020-06-22T07:58:55.983000"
2
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rank.html
pandas.DataFrame.rank# pandas.DataFrame.rank# DataFrame.rank(axis=0, method='average', numeric_only=_NoDefault.no_default, na_option='keep', ascending=True, pct=False)[source]# Compute numerical data ranks (1 through n) along axis. By default, equal values are assigned a rank that is the average of the ranks of those values. Parameters axis{0 or ‘index’, 1 or ‘columns’}, default 0Index to direct ranking. First we check the difference between the dates, which are > 1 day. Then we groupby on ID and the cumsum of these differences and cumulative count each group` # df['TIME'] = pd.to_datetime(df['TIME']) s = df['TIME'].diff().fillna(pd.Timedelta(days=1)).ne(pd.Timedelta(days=1)) df['RANK'] = s.groupby([df['ID'], s.cumsum()]).cumcount().add(1) ID TIME RANK 0 1 2018-07-11 1 1 1 2018-07-12 2 2 1 2018-07-13 3 3 1 2018-07-15 1 4 1 2018-07-16 2 5 1 2018-07-17 3 6 2 2019-09-11 1 7 2 2019-09-12 2 8 2 2019-09-15 1 9 2 2019-09-16 2 For Series this parameter is unused and defaults to 0. method{‘average’, ‘min’, ‘max’, ‘first’, ‘dense’}, default ‘average’How to rank the group of records that have the same value (i.e. ties): average: average rank of the group min: lowest rank in the group max: highest rank in the group first: ranks assigned in order they appear in the array dense: like ‘min’, but rank always increases by 1 between groups. numeric_onlybool, optionalFor DataFrame objects, rank only numeric columns if set to True. na_option{‘keep’, ‘top’, ‘bottom’}, default ‘keep’How to rank NaN values: keep: assign NaN rank to NaN values top: assign lowest rank to NaN values bottom: assign highest rank to NaN values ascendingbool, default TrueWhether or not the elements should be ranked in ascending order. pctbool, default FalseWhether or not to display the returned rankings in percentile form. Returns same type as callerReturn a Series or DataFrame with data ranks as values. See also core.groupby.GroupBy.rankRank of values within each group. Examples >>> df = pd.DataFrame(data={'Animal': ['cat', 'penguin', 'dog', ... 'spider', 'snake'], ... 'Number_legs': [4, 2, 4, 8, np.nan]}) >>> df Animal Number_legs 0 cat 4.0 1 penguin 2.0 2 dog 4.0 3 spider 8.0 4 snake NaN Ties are assigned the mean of the ranks (by default) for the group. >>> s = pd.Series(range(5), index=list("abcde")) >>> s["d"] = s["b"] >>> s.rank() a 1.0 b 2.5 c 4.0 d 2.5 e 5.0 dtype: float64 The following example shows how the method behaves with the above parameters: default_rank: this is the default behaviour obtained without using any parameter. max_rank: setting method = 'max' the records that have the same values are ranked using the highest rank (e.g.: since ‘cat’ and ‘dog’ are both in the 2nd and 3rd position, rank 3 is assigned.) NA_bottom: choosing na_option = 'bottom', if there are records with NaN values they are placed at the bottom of the ranking. pct_rank: when setting pct = True, the ranking is expressed as percentile rank. >>> df['default_rank'] = df['Number_legs'].rank() >>> df['max_rank'] = df['Number_legs'].rank(method='max') >>> df['NA_bottom'] = df['Number_legs'].rank(na_option='bottom') >>> df['pct_rank'] = df['Number_legs'].rank(pct=True) >>> df Animal Number_legs default_rank max_rank NA_bottom pct_rank 0 cat 4.0 2.5 3.0 2.5 0.625 1 penguin 2.0 1.0 1.0 1.0 0.250 2 dog 4.0 2.5 3.0 2.5 0.625 3 spider 8.0 4.0 4.0 4.0 1.000 4 snake NaN NaN NaN 5.0 NaN
414
1,011
group by rank continuous date by pandas I refer this post . But My goal is something different. Example ID TIME 01 2018-07-11 01 2018-07-12 01 2018-07-13 01 2018-07-15 01 2018-07-16 01 2018-07-17 02 2019-09-11 02 2019-09-12 02 2019-09-15 02 2019-09-16 Notice: For each id, the date is unique. Expected ID TIME RANK 01 2018-07-11 1 01 2018-07-12 2 01 2018-07-13 3 01 2018-07-15 1 01 2018-07-16 2 01 2018-07-17 3 02 2019-09-11 1 02 2019-09-12 2 02 2019-09-15 1 02 2019-09-16 2 For each id, the rank of continuous date does not change.IF not, the rank restarts. Goal How to get the result. Try df.groupby('ID')['TIME'].rank(ascending=True) failed
First we check the difference between the dates, which are > 1 day. Then we groupby on ID and the cumsum of these differences and cumulative count each group` # df['TIME'] = pd.to_datetime(df['TIME']) s = df['TIME'].diff().fillna(pd.Timedelta(days=1)).ne(pd.Timedelta(days=1)) df['RANK'] = s.groupby([df['ID'], s.cumsum()]).cumcount().add(1) ID TIME RANK 0 1 2018-07-11 1 1 1 2018-07-12 2 2 1 2018-07-13 3 3 1 2018-07-15 1 4 1 2018-07-16 2 5 1 2018-07-17 3 6 2 2019-09-11 1 7 2 2019-09-12 2 8 2 2019-09-15 1 9 2 2019-09-16 2
65,266,706
Does Pandas Have an Alternative to This Syntax I'm Currently Using?
<p>I want to filter my <code>df</code> down to only those rows who have a value in column <code>A</code> which appears less frequently than some threshold. I currently am using a trick with two <code>value_counts()</code>. To explain what I mean:</p> <pre><code>df = pd.DataFrame([[1, 2, 3], [1, 4, 5], [6, 7, 8]], columns=['A', 'B', 'C']) ''' A B C 0 1 2 3 1 1 4 5 2 6 7 8 ''' </code></pre> <p>I want to remove any row whose value in the <code>A</code> column appears <code>&lt; 2</code> times in the column <code>A</code>. I currently do this:</p> <pre><code>df = df[df['A'].isin(df.A.value_counts()[df.A.value_counts() &gt;= 2].index)] </code></pre> <p>Does Pandas have a method to do this which is cleaner than having to call <code>value_counts()</code> twice?</p>
65,266,832
"2020-12-12T15:34:23.227000"
1
null
0
29
python|pandas
<p>It's probably easiest to filter by group size, where the groups are done on column A.</p> <pre><code>df.groupby('A').filter(lambda x: len(x) &gt;=2) </code></pre>
"2020-12-12T15:46:01.353000"
3
https://pandas.pydata.org/docs/getting_started/intro_tutorials/01_table_oriented.html
What kind of data does pandas handle?# What kind of data does pandas handle?# I want to start using pandas In [1]: import pandas as pd To load the pandas package and start working with it, import the package. The community agreed alias for pandas is pd, so loading It's probably easiest to filter by group size, where the groups are done on column A. df.groupby('A').filter(lambda x: len(x) >=2) pandas as pd is assumed standard practice for all of the pandas documentation. pandas data table representation# I want to store passenger data of the Titanic. For a number of passengers, I know the name (characters), age (integers) and sex (male/female) data. In [2]: df = pd.DataFrame( ...: { ...: "Name": [ ...: "Braund, Mr. Owen Harris", ...: "Allen, Mr. William Henry", ...: "Bonnell, Miss. Elizabeth", ...: ], ...: "Age": [22, 35, 58], ...: "Sex": ["male", "male", "female"], ...: } ...: ) ...: In [3]: df Out[3]: Name Age Sex 0 Braund, Mr. Owen Harris 22 male 1 Allen, Mr. William Henry 35 male 2 Bonnell, Miss. Elizabeth 58 female To manually store data in a table, create a DataFrame. When using a Python dictionary of lists, the dictionary keys will be used as column headers and the values in each list as columns of the DataFrame. A DataFrame is a 2-dimensional data structure that can store data of different types (including characters, integers, floating point values, categorical data and more) in columns. It is similar to a spreadsheet, a SQL table or the data.frame in R. The table has 3 columns, each of them with a column label. The column labels are respectively Name, Age and Sex. The column Name consists of textual data with each value a string, the column Age are numbers and the column Sex is textual data. In spreadsheet software, the table representation of our data would look very similar: Each column in a DataFrame is a Series# I’m just interested in working with the data in the column Age In [4]: df["Age"] Out[4]: 0 22 1 35 2 58 Name: Age, dtype: int64 When selecting a single column of a pandas DataFrame, the result is a pandas Series. To select the column, use the column label in between square brackets []. Note If you are familiar to Python dictionaries, the selection of a single column is very similar to selection of dictionary values based on the key. You can create a Series from scratch as well: In [5]: ages = pd.Series([22, 35, 58], name="Age") In [6]: ages Out[6]: 0 22 1 35 2 58 Name: Age, dtype: int64 A pandas Series has no column labels, as it is just a single column of a DataFrame. A Series does have row labels. Do something with a DataFrame or Series# I want to know the maximum Age of the passengers We can do this on the DataFrame by selecting the Age column and applying max(): In [7]: df["Age"].max() Out[7]: 58 Or to the Series: In [8]: ages.max() Out[8]: 58 As illustrated by the max() method, you can do things with a DataFrame or Series. pandas provides a lot of functionalities, each of them a method you can apply to a DataFrame or Series. As methods are functions, do not forget to use parentheses (). I’m interested in some basic statistics of the numerical data of my data table In [9]: df.describe() Out[9]: Age count 3.000000 mean 38.333333 std 18.230012 min 22.000000 25% 28.500000 50% 35.000000 75% 46.500000 max 58.000000 The describe() method provides a quick overview of the numerical data in a DataFrame. As the Name and Sex columns are textual data, these are by default not taken into account by the describe() method. Many pandas operations return a DataFrame or a Series. The describe() method is an example of a pandas operation returning a pandas Series or a pandas DataFrame. To user guideCheck more options on describe in the user guide section about aggregations with describe Note This is just a starting point. Similar to spreadsheet software, pandas represents data as a table with columns and rows. Apart from the representation, also the data manipulations and calculations you would do in spreadsheet software are supported by pandas. Continue reading the next tutorials to get started! REMEMBER Import the package, aka import pandas as pd A table of data is stored as a pandas DataFrame Each column in a DataFrame is a Series You can do things by applying a method to a DataFrame or Series To user guideA more extended explanation to DataFrame and Series is provided in the introduction to data structures.
270
401
Does Pandas Have an Alternative to This Syntax I'm Currently Using? I want to filter my df down to only those rows who have a value in column A which appears less frequently than some threshold. I currently am using a trick with two value_counts(). To explain what I mean: df = pd.DataFrame([[1, 2, 3], [1, 4, 5], [6, 7, 8]], columns=['A', 'B', 'C']) ''' A B C 0 1 2 3 1 1 4 5 2 6 7 8 ''' I want to remove any row whose value in the A column appears < 2 times in the column A. I currently do this: df = df[df['A'].isin(df.A.value_counts()[df.A.value_counts() >= 2].index)] Does Pandas have a method to do this which is cleaner than having to call value_counts() twice?
It's probably easiest to filter by group size, where the groups are done on column A. df.groupby('A').filter(lambda x: len(x) >=2)
61,207,344
Merging table by group in pandas
<p>I have a dataset with human genes. I want to check all possible combination of alleles. So my df:</p> <pre><code>Human Gene AllelA AllelB 1 gene1 C C 1 gene2 A T 1 gene3 G G 1 gene4 T A 2 gene1 C C 2 gene2 G G 3 gene4 C C 4 gene1 A T 5 gene4 C C </code></pre> <p>And now I need to check all ways so I think the better way will be pivot thah table to (first I merging gene to one column Allele1Allele2, so in Gene1 for Human1 it will be one column contains: CC):</p> <pre><code>humanNumber Gene1 Gene2 Gene3 Gene4 </code></pre> <p>And then just do some magic with merge columns. But now I have a problem with pivot it. When I tried I got error telling that my index is not unique (indeed it isn't), so I tried to group by the table, but it's completly unclear for my to pivot it.</p> <p>I know - and I did - I can do that by loop in pure python, but first: it's long (80k people x 2|3|4 genes) and second: I think pandas can do that better. I just can't applay the pivot function. Tried in different way, using different indexes... but still problems.</p> <p>Expectation:</p> <pre><code> humanNumber Gene1 Gene2 Gene3 Gene4 1 CC AT GG TA etc. </code></pre> <p>Can someone help me do that right?</p> <p>P.S. whole dataframe is filled by strings</p>
61,207,612
"2020-04-14T12:09:53.613000"
1
null
2
45
python|pandas
<p>Problem are duplicates by <code>Human</code> and <code>Gene</code>, you can check them:</p> <pre><code>print (df) Human Gene AllelA AllelB 0 1 gene1 C C &lt;- added dupe row for test 1 1 gene1 C C 2 1 gene2 A T 3 1 gene3 G G 4 1 gene4 T A 5 2 gene1 C C 6 2 gene2 G G 7 3 gene4 C C 8 4 gene1 A T 9 5 gene4 C C print (df[df.duplicated(['Human','Gene'], keep=False)]) Human Gene AllelA AllelB 0 1 gene1 C C 1 1 gene1 C C </code></pre> <p>If possible, you can remove them by <a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.drop_duplicates.html" rel="nofollow noreferrer"><code>DataFrame.drop_duplicates</code></a>:</p> <pre><code>df = (df.drop_duplicates(['Human','Gene']) .assign(Alle = lambda x: df['AllelA'] + df['AllelB']) .pivot('Human','Gene','Alle')) print (df) Gene gene1 gene2 gene3 gene4 Human 1 CC AT GG TA 2 CC GG NaN NaN 3 NaN NaN NaN CC 4 AT NaN NaN NaN 5 NaN NaN NaN CC </code></pre>
"2020-04-14T12:23:58.840000"
3
https://pandas.pydata.org/docs/user_guide/groupby.html
Group by: split-apply-combine# Problem are duplicates by Human and Gene, you can check them: print (df) Human Gene AllelA AllelB 0 1 gene1 C C <- added dupe row for test 1 1 gene1 C C 2 1 gene2 A T 3 1 gene3 G G 4 1 gene4 T A 5 2 gene1 C C 6 2 gene2 G G 7 3 gene4 C C 8 4 gene1 A T 9 5 gene4 C C print (df[df.duplicated(['Human','Gene'], keep=False)]) Human Gene AllelA AllelB 0 1 gene1 C C 1 1 gene1 C C If possible, you can remove them by DataFrame.drop_duplicates: df = (df.drop_duplicates(['Human','Gene']) .assign(Alle = lambda x: df['AllelA'] + df['AllelB']) .pivot('Human','Gene','Alle')) print (df) Gene gene1 gene2 gene3 gene4 Human 1 CC AT GG TA 2 CC GG NaN NaN 3 NaN NaN NaN CC 4 AT NaN NaN NaN 5 NaN NaN NaN CC Group by: split-apply-combine# By “group by” we are referring to a process involving one or more of the following steps: Splitting the data into groups based on some criteria. Applying a function to each group independently. Combining the results into a data structure. Out of these, the split step is the most straightforward. In fact, in many situations we may wish to split the data set into groups and do something with those groups. In the apply step, we might wish to do one of the following: Aggregation: compute a summary statistic (or statistics) for each group. Some examples: Compute group sums or means. Compute group sizes / counts. Transformation: perform some group-specific computations and return a like-indexed object. Some examples: Standardize data (zscore) within a group. Filling NAs within groups with a value derived from each group. Filtration: discard some groups, according to a group-wise computation that evaluates True or False. Some examples: Discard data that belongs to groups with only a few members. Filter out data based on the group sum or mean. Some combination of the above: GroupBy will examine the results of the apply step and try to return a sensibly combined result if it doesn’t fit into either of the above two categories. Since the set of object instance methods on pandas data structures are generally rich and expressive, we often simply want to invoke, say, a DataFrame function on each group. The name GroupBy should be quite familiar to those who have used a SQL-based tool (or itertools), in which you can write code like: SELECT Column1, Column2, mean(Column3), sum(Column4) FROM SomeTable GROUP BY Column1, Column2 We aim to make operations like this natural and easy to express using pandas. We’ll address each area of GroupBy functionality then provide some non-trivial examples / use cases. See the cookbook for some advanced strategies. Splitting an object into groups# pandas objects can be split on any of their axes. The abstract definition of grouping is to provide a mapping of labels to group names. To create a GroupBy object (more on what the GroupBy object is later), you may do the following: In [1]: df = pd.DataFrame( ...: [ ...: ("bird", "Falconiformes", 389.0), ...: ("bird", "Psittaciformes", 24.0), ...: ("mammal", "Carnivora", 80.2), ...: ("mammal", "Primates", np.nan), ...: ("mammal", "Carnivora", 58), ...: ], ...: index=["falcon", "parrot", "lion", "monkey", "leopard"], ...: columns=("class", "order", "max_speed"), ...: ) ...: In [2]: df Out[2]: class order max_speed falcon bird Falconiformes 389.0 parrot bird Psittaciformes 24.0 lion mammal Carnivora 80.2 monkey mammal Primates NaN leopard mammal Carnivora 58.0 # default is axis=0 In [3]: grouped = df.groupby("class") In [4]: grouped = df.groupby("order", axis="columns") In [5]: grouped = df.groupby(["class", "order"]) The mapping can be specified many different ways: A Python function, to be called on each of the axis labels. A list or NumPy array of the same length as the selected axis. A dict or Series, providing a label -> group name mapping. For DataFrame objects, a string indicating either a column name or an index level name to be used to group. df.groupby('A') is just syntactic sugar for df.groupby(df['A']). A list of any of the above things. Collectively we refer to the grouping objects as the keys. For example, consider the following DataFrame: Note A string passed to groupby may refer to either a column or an index level. If a string matches both a column name and an index level name, a ValueError will be raised. In [6]: df = pd.DataFrame( ...: { ...: "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"], ...: "B": ["one", "one", "two", "three", "two", "two", "one", "three"], ...: "C": np.random.randn(8), ...: "D": np.random.randn(8), ...: } ...: ) ...: In [7]: df Out[7]: A B C D 0 foo one 0.469112 -0.861849 1 bar one -0.282863 -2.104569 2 foo two -1.509059 -0.494929 3 bar three -1.135632 1.071804 4 foo two 1.212112 0.721555 5 bar two -0.173215 -0.706771 6 foo one 0.119209 -1.039575 7 foo three -1.044236 0.271860 On a DataFrame, we obtain a GroupBy object by calling groupby(). We could naturally group by either the A or B columns, or both: In [8]: grouped = df.groupby("A") In [9]: grouped = df.groupby(["A", "B"]) If we also have a MultiIndex on columns A and B, we can group by all but the specified columns In [10]: df2 = df.set_index(["A", "B"]) In [11]: grouped = df2.groupby(level=df2.index.names.difference(["B"])) In [12]: grouped.sum() Out[12]: C D A bar -1.591710 -1.739537 foo -0.752861 -1.402938 These will split the DataFrame on its index (rows). We could also split by the columns: In [13]: def get_letter_type(letter): ....: if letter.lower() in 'aeiou': ....: return 'vowel' ....: else: ....: return 'consonant' ....: In [14]: grouped = df.groupby(get_letter_type, axis=1) pandas Index objects support duplicate values. If a non-unique index is used as the group key in a groupby operation, all values for the same index value will be considered to be in one group and thus the output of aggregation functions will only contain unique index values: In [15]: lst = [1, 2, 3, 1, 2, 3] In [16]: s = pd.Series([1, 2, 3, 10, 20, 30], lst) In [17]: grouped = s.groupby(level=0) In [18]: grouped.first() Out[18]: 1 1 2 2 3 3 dtype: int64 In [19]: grouped.last() Out[19]: 1 10 2 20 3 30 dtype: int64 In [20]: grouped.sum() Out[20]: 1 11 2 22 3 33 dtype: int64 Note that no splitting occurs until it’s needed. Creating the GroupBy object only verifies that you’ve passed a valid mapping. Note Many kinds of complicated data manipulations can be expressed in terms of GroupBy operations (though can’t be guaranteed to be the most efficient). You can get quite creative with the label mapping functions. GroupBy sorting# By default the group keys are sorted during the groupby operation. You may however pass sort=False for potential speedups: In [21]: df2 = pd.DataFrame({"X": ["B", "B", "A", "A"], "Y": [1, 2, 3, 4]}) In [22]: df2.groupby(["X"]).sum() Out[22]: Y X A 7 B 3 In [23]: df2.groupby(["X"], sort=False).sum() Out[23]: Y X B 3 A 7 Note that groupby will preserve the order in which observations are sorted within each group. For example, the groups created by groupby() below are in the order they appeared in the original DataFrame: In [24]: df3 = pd.DataFrame({"X": ["A", "B", "A", "B"], "Y": [1, 4, 3, 2]}) In [25]: df3.groupby(["X"]).get_group("A") Out[25]: X Y 0 A 1 2 A 3 In [26]: df3.groupby(["X"]).get_group("B") Out[26]: X Y 1 B 4 3 B 2 New in version 1.1.0. GroupBy dropna# By default NA values are excluded from group keys during the groupby operation. However, in case you want to include NA values in group keys, you could pass dropna=False to achieve it. In [27]: df_list = [[1, 2, 3], [1, None, 4], [2, 1, 3], [1, 2, 2]] In [28]: df_dropna = pd.DataFrame(df_list, columns=["a", "b", "c"]) In [29]: df_dropna Out[29]: a b c 0 1 2.0 3 1 1 NaN 4 2 2 1.0 3 3 1 2.0 2 # Default ``dropna`` is set to True, which will exclude NaNs in keys In [30]: df_dropna.groupby(by=["b"], dropna=True).sum() Out[30]: a c b 1.0 2 3 2.0 2 5 # In order to allow NaN in keys, set ``dropna`` to False In [31]: df_dropna.groupby(by=["b"], dropna=False).sum() Out[31]: a c b 1.0 2 3 2.0 2 5 NaN 1 4 The default setting of dropna argument is True which means NA are not included in group keys. GroupBy object attributes# The groups attribute is a dict whose keys are the computed unique groups and corresponding values being the axis labels belonging to each group. In the above example we have: In [32]: df.groupby("A").groups Out[32]: {'bar': [1, 3, 5], 'foo': [0, 2, 4, 6, 7]} In [33]: df.groupby(get_letter_type, axis=1).groups Out[33]: {'consonant': ['B', 'C', 'D'], 'vowel': ['A']} Calling the standard Python len function on the GroupBy object just returns the length of the groups dict, so it is largely just a convenience: In [34]: grouped = df.groupby(["A", "B"]) In [35]: grouped.groups Out[35]: {('bar', 'one'): [1], ('bar', 'three'): [3], ('bar', 'two'): [5], ('foo', 'one'): [0, 6], ('foo', 'three'): [7], ('foo', 'two'): [2, 4]} In [36]: len(grouped) Out[36]: 6 GroupBy will tab complete column names (and other attributes): In [37]: df Out[37]: height weight gender 2000-01-01 42.849980 157.500553 male 2000-01-02 49.607315 177.340407 male 2000-01-03 56.293531 171.524640 male 2000-01-04 48.421077 144.251986 female 2000-01-05 46.556882 152.526206 male 2000-01-06 68.448851 168.272968 female 2000-01-07 70.757698 136.431469 male 2000-01-08 58.909500 176.499753 female 2000-01-09 76.435631 174.094104 female 2000-01-10 45.306120 177.540920 male In [38]: gb = df.groupby("gender") In [39]: gb.<TAB> # noqa: E225, E999 gb.agg gb.boxplot gb.cummin gb.describe gb.filter gb.get_group gb.height gb.last gb.median gb.ngroups gb.plot gb.rank gb.std gb.transform gb.aggregate gb.count gb.cumprod gb.dtype gb.first gb.groups gb.hist gb.max gb.min gb.nth gb.prod gb.resample gb.sum gb.var gb.apply gb.cummax gb.cumsum gb.fillna gb.gender gb.head gb.indices gb.mean gb.name gb.ohlc gb.quantile gb.size gb.tail gb.weight GroupBy with MultiIndex# With hierarchically-indexed data, it’s quite natural to group by one of the levels of the hierarchy. Let’s create a Series with a two-level MultiIndex. In [40]: arrays = [ ....: ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"], ....: ["one", "two", "one", "two", "one", "two", "one", "two"], ....: ] ....: In [41]: index = pd.MultiIndex.from_arrays(arrays, names=["first", "second"]) In [42]: s = pd.Series(np.random.randn(8), index=index) In [43]: s Out[43]: first second bar one -0.919854 two -0.042379 baz one 1.247642 two -0.009920 foo one 0.290213 two 0.495767 qux one 0.362949 two 1.548106 dtype: float64 We can then group by one of the levels in s. In [44]: grouped = s.groupby(level=0) In [45]: grouped.sum() Out[45]: first bar -0.962232 baz 1.237723 foo 0.785980 qux 1.911055 dtype: float64 If the MultiIndex has names specified, these can be passed instead of the level number: In [46]: s.groupby(level="second").sum() Out[46]: second one 0.980950 two 1.991575 dtype: float64 Grouping with multiple levels is supported. In [47]: s Out[47]: first second third bar doo one -1.131345 two -0.089329 baz bee one 0.337863 two -0.945867 foo bop one -0.932132 two 1.956030 qux bop one 0.017587 two -0.016692 dtype: float64 In [48]: s.groupby(level=["first", "second"]).sum() Out[48]: first second bar doo -1.220674 baz bee -0.608004 foo bop 1.023898 qux bop 0.000895 dtype: float64 Index level names may be supplied as keys. In [49]: s.groupby(["first", "second"]).sum() Out[49]: first second bar doo -1.220674 baz bee -0.608004 foo bop 1.023898 qux bop 0.000895 dtype: float64 More on the sum function and aggregation later. Grouping DataFrame with Index levels and columns# A DataFrame may be grouped by a combination of columns and index levels by specifying the column names as strings and the index levels as pd.Grouper objects. In [50]: arrays = [ ....: ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"], ....: ["one", "two", "one", "two", "one", "two", "one", "two"], ....: ] ....: In [51]: index = pd.MultiIndex.from_arrays(arrays, names=["first", "second"]) In [52]: df = pd.DataFrame({"A": [1, 1, 1, 1, 2, 2, 3, 3], "B": np.arange(8)}, index=index) In [53]: df Out[53]: A B first second bar one 1 0 two 1 1 baz one 1 2 two 1 3 foo one 2 4 two 2 5 qux one 3 6 two 3 7 The following example groups df by the second index level and the A column. In [54]: df.groupby([pd.Grouper(level=1), "A"]).sum() Out[54]: B second A one 1 2 2 4 3 6 two 1 4 2 5 3 7 Index levels may also be specified by name. In [55]: df.groupby([pd.Grouper(level="second"), "A"]).sum() Out[55]: B second A one 1 2 2 4 3 6 two 1 4 2 5 3 7 Index level names may be specified as keys directly to groupby. In [56]: df.groupby(["second", "A"]).sum() Out[56]: B second A one 1 2 2 4 3 6 two 1 4 2 5 3 7 DataFrame column selection in GroupBy# Once you have created the GroupBy object from a DataFrame, you might want to do something different for each of the columns. Thus, using [] similar to getting a column from a DataFrame, you can do: In [57]: df = pd.DataFrame( ....: { ....: "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"], ....: "B": ["one", "one", "two", "three", "two", "two", "one", "three"], ....: "C": np.random.randn(8), ....: "D": np.random.randn(8), ....: } ....: ) ....: In [58]: df Out[58]: A B C D 0 foo one -0.575247 1.346061 1 bar one 0.254161 1.511763 2 foo two -1.143704 1.627081 3 bar three 0.215897 -0.990582 4 foo two 1.193555 -0.441652 5 bar two -0.077118 1.211526 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 In [59]: grouped = df.groupby(["A"]) In [60]: grouped_C = grouped["C"] In [61]: grouped_D = grouped["D"] This is mainly syntactic sugar for the alternative and much more verbose: In [62]: df["C"].groupby(df["A"]) Out[62]: <pandas.core.groupby.generic.SeriesGroupBy object at 0x7f1ea100a490> Additionally this method avoids recomputing the internal grouping information derived from the passed key. Iterating through groups# With the GroupBy object in hand, iterating through the grouped data is very natural and functions similarly to itertools.groupby(): In [63]: grouped = df.groupby('A') In [64]: for name, group in grouped: ....: print(name) ....: print(group) ....: bar A B C D 1 bar one 0.254161 1.511763 3 bar three 0.215897 -0.990582 5 bar two -0.077118 1.211526 foo A B C D 0 foo one -0.575247 1.346061 2 foo two -1.143704 1.627081 4 foo two 1.193555 -0.441652 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 In the case of grouping by multiple keys, the group name will be a tuple: In [65]: for name, group in df.groupby(['A', 'B']): ....: print(name) ....: print(group) ....: ('bar', 'one') A B C D 1 bar one 0.254161 1.511763 ('bar', 'three') A B C D 3 bar three 0.215897 -0.990582 ('bar', 'two') A B C D 5 bar two -0.077118 1.211526 ('foo', 'one') A B C D 0 foo one -0.575247 1.346061 6 foo one -0.408530 0.268520 ('foo', 'three') A B C D 7 foo three -0.862495 0.02458 ('foo', 'two') A B C D 2 foo two -1.143704 1.627081 4 foo two 1.193555 -0.441652 See Iterating through groups. Selecting a group# A single group can be selected using get_group(): In [66]: grouped.get_group("bar") Out[66]: A B C D 1 bar one 0.254161 1.511763 3 bar three 0.215897 -0.990582 5 bar two -0.077118 1.211526 Or for an object grouped on multiple columns: In [67]: df.groupby(["A", "B"]).get_group(("bar", "one")) Out[67]: A B C D 1 bar one 0.254161 1.511763 Aggregation# Once the GroupBy object has been created, several methods are available to perform a computation on the grouped data. These operations are similar to the aggregating API, window API, and resample API. An obvious one is aggregation via the aggregate() or equivalently agg() method: In [68]: grouped = df.groupby("A") In [69]: grouped[["C", "D"]].aggregate(np.sum) Out[69]: C D A bar 0.392940 1.732707 foo -1.796421 2.824590 In [70]: grouped = df.groupby(["A", "B"]) In [71]: grouped.aggregate(np.sum) Out[71]: C D A B bar one 0.254161 1.511763 three 0.215897 -0.990582 two -0.077118 1.211526 foo one -0.983776 1.614581 three -0.862495 0.024580 two 0.049851 1.185429 As you can see, the result of the aggregation will have the group names as the new index along the grouped axis. In the case of multiple keys, the result is a MultiIndex by default, though this can be changed by using the as_index option: In [72]: grouped = df.groupby(["A", "B"], as_index=False) In [73]: grouped.aggregate(np.sum) Out[73]: A B C D 0 bar one 0.254161 1.511763 1 bar three 0.215897 -0.990582 2 bar two -0.077118 1.211526 3 foo one -0.983776 1.614581 4 foo three -0.862495 0.024580 5 foo two 0.049851 1.185429 In [74]: df.groupby("A", as_index=False)[["C", "D"]].sum() Out[74]: A C D 0 bar 0.392940 1.732707 1 foo -1.796421 2.824590 Note that you could use the reset_index DataFrame function to achieve the same result as the column names are stored in the resulting MultiIndex: In [75]: df.groupby(["A", "B"]).sum().reset_index() Out[75]: A B C D 0 bar one 0.254161 1.511763 1 bar three 0.215897 -0.990582 2 bar two -0.077118 1.211526 3 foo one -0.983776 1.614581 4 foo three -0.862495 0.024580 5 foo two 0.049851 1.185429 Another simple aggregation example is to compute the size of each group. This is included in GroupBy as the size method. It returns a Series whose index are the group names and whose values are the sizes of each group. In [76]: grouped.size() Out[76]: A B size 0 bar one 1 1 bar three 1 2 bar two 1 3 foo one 2 4 foo three 1 5 foo two 2 In [77]: grouped.describe() Out[77]: C ... D count mean std min ... 25% 50% 75% max 0 1.0 0.254161 NaN 0.254161 ... 1.511763 1.511763 1.511763 1.511763 1 1.0 0.215897 NaN 0.215897 ... -0.990582 -0.990582 -0.990582 -0.990582 2 1.0 -0.077118 NaN -0.077118 ... 1.211526 1.211526 1.211526 1.211526 3 2.0 -0.491888 0.117887 -0.575247 ... 0.537905 0.807291 1.076676 1.346061 4 1.0 -0.862495 NaN -0.862495 ... 0.024580 0.024580 0.024580 0.024580 5 2.0 0.024925 1.652692 -1.143704 ... 0.075531 0.592714 1.109898 1.627081 [6 rows x 16 columns] Another aggregation example is to compute the number of unique values of each group. This is similar to the value_counts function, except that it only counts unique values. In [78]: ll = [['foo', 1], ['foo', 2], ['foo', 2], ['bar', 1], ['bar', 1]] In [79]: df4 = pd.DataFrame(ll, columns=["A", "B"]) In [80]: df4 Out[80]: A B 0 foo 1 1 foo 2 2 foo 2 3 bar 1 4 bar 1 In [81]: df4.groupby("A")["B"].nunique() Out[81]: A bar 1 foo 2 Name: B, dtype: int64 Note Aggregation functions will not return the groups that you are aggregating over if they are named columns, when as_index=True, the default. The grouped columns will be the indices of the returned object. Passing as_index=False will return the groups that you are aggregating over, if they are named columns. Aggregating functions are the ones that reduce the dimension of the returned objects. Some common aggregating functions are tabulated below: Function Description mean() Compute mean of groups sum() Compute sum of group values size() Compute group sizes count() Compute count of group std() Standard deviation of groups var() Compute variance of groups sem() Standard error of the mean of groups describe() Generates descriptive statistics first() Compute first of group values last() Compute last of group values nth() Take nth value, or a subset if n is a list min() Compute min of group values max() Compute max of group values The aggregating functions above will exclude NA values. Any function which reduces a Series to a scalar value is an aggregation function and will work, a trivial example is df.groupby('A').agg(lambda ser: 1). Note that nth() can act as a reducer or a filter, see here. Applying multiple functions at once# With grouped Series you can also pass a list or dict of functions to do aggregation with, outputting a DataFrame: In [82]: grouped = df.groupby("A") In [83]: grouped["C"].agg([np.sum, np.mean, np.std]) Out[83]: sum mean std A bar 0.392940 0.130980 0.181231 foo -1.796421 -0.359284 0.912265 On a grouped DataFrame, you can pass a list of functions to apply to each column, which produces an aggregated result with a hierarchical index: In [84]: grouped[["C", "D"]].agg([np.sum, np.mean, np.std]) Out[84]: C D sum mean std sum mean std A bar 0.392940 0.130980 0.181231 1.732707 0.577569 1.366330 foo -1.796421 -0.359284 0.912265 2.824590 0.564918 0.884785 The resulting aggregations are named for the functions themselves. If you need to rename, then you can add in a chained operation for a Series like this: In [85]: ( ....: grouped["C"] ....: .agg([np.sum, np.mean, np.std]) ....: .rename(columns={"sum": "foo", "mean": "bar", "std": "baz"}) ....: ) ....: Out[85]: foo bar baz A bar 0.392940 0.130980 0.181231 foo -1.796421 -0.359284 0.912265 For a grouped DataFrame, you can rename in a similar manner: In [86]: ( ....: grouped[["C", "D"]].agg([np.sum, np.mean, np.std]).rename( ....: columns={"sum": "foo", "mean": "bar", "std": "baz"} ....: ) ....: ) ....: Out[86]: C D foo bar baz foo bar baz A bar 0.392940 0.130980 0.181231 1.732707 0.577569 1.366330 foo -1.796421 -0.359284 0.912265 2.824590 0.564918 0.884785 Note In general, the output column names should be unique. You can’t apply the same function (or two functions with the same name) to the same column. In [87]: grouped["C"].agg(["sum", "sum"]) Out[87]: sum sum A bar 0.392940 0.392940 foo -1.796421 -1.796421 pandas does allow you to provide multiple lambdas. In this case, pandas will mangle the name of the (nameless) lambda functions, appending _<i> to each subsequent lambda. In [88]: grouped["C"].agg([lambda x: x.max() - x.min(), lambda x: x.median() - x.mean()]) Out[88]: <lambda_0> <lambda_1> A bar 0.331279 0.084917 foo 2.337259 -0.215962 Named aggregation# New in version 0.25.0. To support column-specific aggregation with control over the output column names, pandas accepts the special syntax in GroupBy.agg(), known as “named aggregation”, where The keywords are the output column names The values are tuples whose first element is the column to select and the second element is the aggregation to apply to that column. pandas provides the pandas.NamedAgg namedtuple with the fields ['column', 'aggfunc'] to make it clearer what the arguments are. As usual, the aggregation can be a callable or a string alias. In [89]: animals = pd.DataFrame( ....: { ....: "kind": ["cat", "dog", "cat", "dog"], ....: "height": [9.1, 6.0, 9.5, 34.0], ....: "weight": [7.9, 7.5, 9.9, 198.0], ....: } ....: ) ....: In [90]: animals Out[90]: kind height weight 0 cat 9.1 7.9 1 dog 6.0 7.5 2 cat 9.5 9.9 3 dog 34.0 198.0 In [91]: animals.groupby("kind").agg( ....: min_height=pd.NamedAgg(column="height", aggfunc="min"), ....: max_height=pd.NamedAgg(column="height", aggfunc="max"), ....: average_weight=pd.NamedAgg(column="weight", aggfunc=np.mean), ....: ) ....: Out[91]: min_height max_height average_weight kind cat 9.1 9.5 8.90 dog 6.0 34.0 102.75 pandas.NamedAgg is just a namedtuple. Plain tuples are allowed as well. In [92]: animals.groupby("kind").agg( ....: min_height=("height", "min"), ....: max_height=("height", "max"), ....: average_weight=("weight", np.mean), ....: ) ....: Out[92]: min_height max_height average_weight kind cat 9.1 9.5 8.90 dog 6.0 34.0 102.75 If your desired output column names are not valid Python keywords, construct a dictionary and unpack the keyword arguments In [93]: animals.groupby("kind").agg( ....: **{ ....: "total weight": pd.NamedAgg(column="weight", aggfunc=sum) ....: } ....: ) ....: Out[93]: total weight kind cat 17.8 dog 205.5 Additional keyword arguments are not passed through to the aggregation functions. Only pairs of (column, aggfunc) should be passed as **kwargs. If your aggregation functions requires additional arguments, partially apply them with functools.partial(). Note For Python 3.5 and earlier, the order of **kwargs in a functions was not preserved. This means that the output column ordering would not be consistent. To ensure consistent ordering, the keys (and so output columns) will always be sorted for Python 3.5. Named aggregation is also valid for Series groupby aggregations. In this case there’s no column selection, so the values are just the functions. In [94]: animals.groupby("kind").height.agg( ....: min_height="min", ....: max_height="max", ....: ) ....: Out[94]: min_height max_height kind cat 9.1 9.5 dog 6.0 34.0 Applying different functions to DataFrame columns# By passing a dict to aggregate you can apply a different aggregation to the columns of a DataFrame: In [95]: grouped.agg({"C": np.sum, "D": lambda x: np.std(x, ddof=1)}) Out[95]: C D A bar 0.392940 1.366330 foo -1.796421 0.884785 The function names can also be strings. In order for a string to be valid it must be either implemented on GroupBy or available via dispatching: In [96]: grouped.agg({"C": "sum", "D": "std"}) Out[96]: C D A bar 0.392940 1.366330 foo -1.796421 0.884785 Cython-optimized aggregation functions# Some common aggregations, currently only sum, mean, std, and sem, have optimized Cython implementations: In [97]: df.groupby("A")[["C", "D"]].sum() Out[97]: C D A bar 0.392940 1.732707 foo -1.796421 2.824590 In [98]: df.groupby(["A", "B"]).mean() Out[98]: C D A B bar one 0.254161 1.511763 three 0.215897 -0.990582 two -0.077118 1.211526 foo one -0.491888 0.807291 three -0.862495 0.024580 two 0.024925 0.592714 Of course sum and mean are implemented on pandas objects, so the above code would work even without the special versions via dispatching (see below). Aggregations with User-Defined Functions# Users can also provide their own functions for custom aggregations. When aggregating with a User-Defined Function (UDF), the UDF should not mutate the provided Series, see Mutating with User Defined Function (UDF) methods for more information. In [99]: animals.groupby("kind")[["height"]].agg(lambda x: set(x)) Out[99]: height kind cat {9.1, 9.5} dog {34.0, 6.0} The resulting dtype will reflect that of the aggregating function. If the results from different groups have different dtypes, then a common dtype will be determined in the same way as DataFrame construction. In [100]: animals.groupby("kind")[["height"]].agg(lambda x: x.astype(int).sum()) Out[100]: height kind cat 18 dog 40 Transformation# The transform method returns an object that is indexed the same as the one being grouped. The transform function must: Return a result that is either the same size as the group chunk or broadcastable to the size of the group chunk (e.g., a scalar, grouped.transform(lambda x: x.iloc[-1])). Operate column-by-column on the group chunk. The transform is applied to the first group chunk using chunk.apply. Not perform in-place operations on the group chunk. Group chunks should be treated as immutable, and changes to a group chunk may produce unexpected results. For example, when using fillna, inplace must be False (grouped.transform(lambda x: x.fillna(inplace=False))). (Optionally) operates on the entire group chunk. If this is supported, a fast path is used starting from the second chunk. Deprecated since version 1.5.0: When using .transform on a grouped DataFrame and the transformation function returns a DataFrame, currently pandas does not align the result’s index with the input’s index. This behavior is deprecated and alignment will be performed in a future version of pandas. You can apply .to_numpy() to the result of the transformation function to avoid alignment. Similar to Aggregations with User-Defined Functions, the resulting dtype will reflect that of the transformation function. If the results from different groups have different dtypes, then a common dtype will be determined in the same way as DataFrame construction. Suppose we wished to standardize the data within each group: In [101]: index = pd.date_range("10/1/1999", periods=1100) In [102]: ts = pd.Series(np.random.normal(0.5, 2, 1100), index) In [103]: ts = ts.rolling(window=100, min_periods=100).mean().dropna() In [104]: ts.head() Out[104]: 2000-01-08 0.779333 2000-01-09 0.778852 2000-01-10 0.786476 2000-01-11 0.782797 2000-01-12 0.798110 Freq: D, dtype: float64 In [105]: ts.tail() Out[105]: 2002-09-30 0.660294 2002-10-01 0.631095 2002-10-02 0.673601 2002-10-03 0.709213 2002-10-04 0.719369 Freq: D, dtype: float64 In [106]: transformed = ts.groupby(lambda x: x.year).transform( .....: lambda x: (x - x.mean()) / x.std() .....: ) .....: We would expect the result to now have mean 0 and standard deviation 1 within each group, which we can easily check: # Original Data In [107]: grouped = ts.groupby(lambda x: x.year) In [108]: grouped.mean() Out[108]: 2000 0.442441 2001 0.526246 2002 0.459365 dtype: float64 In [109]: grouped.std() Out[109]: 2000 0.131752 2001 0.210945 2002 0.128753 dtype: float64 # Transformed Data In [110]: grouped_trans = transformed.groupby(lambda x: x.year) In [111]: grouped_trans.mean() Out[111]: 2000 -4.870756e-16 2001 -1.545187e-16 2002 4.136282e-16 dtype: float64 In [112]: grouped_trans.std() Out[112]: 2000 1.0 2001 1.0 2002 1.0 dtype: float64 We can also visually compare the original and transformed data sets. In [113]: compare = pd.DataFrame({"Original": ts, "Transformed": transformed}) In [114]: compare.plot() Out[114]: <AxesSubplot: > Transformation functions that have lower dimension outputs are broadcast to match the shape of the input array. In [115]: ts.groupby(lambda x: x.year).transform(lambda x: x.max() - x.min()) Out[115]: 2000-01-08 0.623893 2000-01-09 0.623893 2000-01-10 0.623893 2000-01-11 0.623893 2000-01-12 0.623893 ... 2002-09-30 0.558275 2002-10-01 0.558275 2002-10-02 0.558275 2002-10-03 0.558275 2002-10-04 0.558275 Freq: D, Length: 1001, dtype: float64 Alternatively, the built-in methods could be used to produce the same outputs. In [116]: max_ts = ts.groupby(lambda x: x.year).transform("max") In [117]: min_ts = ts.groupby(lambda x: x.year).transform("min") In [118]: max_ts - min_ts Out[118]: 2000-01-08 0.623893 2000-01-09 0.623893 2000-01-10 0.623893 2000-01-11 0.623893 2000-01-12 0.623893 ... 2002-09-30 0.558275 2002-10-01 0.558275 2002-10-02 0.558275 2002-10-03 0.558275 2002-10-04 0.558275 Freq: D, Length: 1001, dtype: float64 Another common data transform is to replace missing data with the group mean. In [119]: data_df Out[119]: A B C 0 1.539708 -1.166480 0.533026 1 1.302092 -0.505754 NaN 2 -0.371983 1.104803 -0.651520 3 -1.309622 1.118697 -1.161657 4 -1.924296 0.396437 0.812436 .. ... ... ... 995 -0.093110 0.683847 -0.774753 996 -0.185043 1.438572 NaN 997 -0.394469 -0.642343 0.011374 998 -1.174126 1.857148 NaN 999 0.234564 0.517098 0.393534 [1000 rows x 3 columns] In [120]: countries = np.array(["US", "UK", "GR", "JP"]) In [121]: key = countries[np.random.randint(0, 4, 1000)] In [122]: grouped = data_df.groupby(key) # Non-NA count in each group In [123]: grouped.count() Out[123]: A B C GR 209 217 189 JP 240 255 217 UK 216 231 193 US 239 250 217 In [124]: transformed = grouped.transform(lambda x: x.fillna(x.mean())) We can verify that the group means have not changed in the transformed data and that the transformed data contains no NAs. In [125]: grouped_trans = transformed.groupby(key) In [126]: grouped.mean() # original group means Out[126]: A B C GR -0.098371 -0.015420 0.068053 JP 0.069025 0.023100 -0.077324 UK 0.034069 -0.052580 -0.116525 US 0.058664 -0.020399 0.028603 In [127]: grouped_trans.mean() # transformation did not change group means Out[127]: A B C GR -0.098371 -0.015420 0.068053 JP 0.069025 0.023100 -0.077324 UK 0.034069 -0.052580 -0.116525 US 0.058664 -0.020399 0.028603 In [128]: grouped.count() # original has some missing data points Out[128]: A B C GR 209 217 189 JP 240 255 217 UK 216 231 193 US 239 250 217 In [129]: grouped_trans.count() # counts after transformation Out[129]: A B C GR 228 228 228 JP 267 267 267 UK 247 247 247 US 258 258 258 In [130]: grouped_trans.size() # Verify non-NA count equals group size Out[130]: GR 228 JP 267 UK 247 US 258 dtype: int64 Note Some functions will automatically transform the input when applied to a GroupBy object, but returning an object of the same shape as the original. Passing as_index=False will not affect these transformation methods. For example: fillna, ffill, bfill, shift.. In [131]: grouped.ffill() Out[131]: A B C 0 1.539708 -1.166480 0.533026 1 1.302092 -0.505754 0.533026 2 -0.371983 1.104803 -0.651520 3 -1.309622 1.118697 -1.161657 4 -1.924296 0.396437 0.812436 .. ... ... ... 995 -0.093110 0.683847 -0.774753 996 -0.185043 1.438572 -0.774753 997 -0.394469 -0.642343 0.011374 998 -1.174126 1.857148 -0.774753 999 0.234564 0.517098 0.393534 [1000 rows x 3 columns] Window and resample operations# It is possible to use resample(), expanding() and rolling() as methods on groupbys. The example below will apply the rolling() method on the samples of the column B based on the groups of column A. In [132]: df_re = pd.DataFrame({"A": [1] * 10 + [5] * 10, "B": np.arange(20)}) In [133]: df_re Out[133]: A B 0 1 0 1 1 1 2 1 2 3 1 3 4 1 4 .. .. .. 15 5 15 16 5 16 17 5 17 18 5 18 19 5 19 [20 rows x 2 columns] In [134]: df_re.groupby("A").rolling(4).B.mean() Out[134]: A 1 0 NaN 1 NaN 2 NaN 3 1.5 4 2.5 ... 5 15 13.5 16 14.5 17 15.5 18 16.5 19 17.5 Name: B, Length: 20, dtype: float64 The expanding() method will accumulate a given operation (sum() in the example) for all the members of each particular group. In [135]: df_re.groupby("A").expanding().sum() Out[135]: B A 1 0 0.0 1 1.0 2 3.0 3 6.0 4 10.0 ... ... 5 15 75.0 16 91.0 17 108.0 18 126.0 19 145.0 [20 rows x 1 columns] Suppose you want to use the resample() method to get a daily frequency in each group of your dataframe and wish to complete the missing values with the ffill() method. In [136]: df_re = pd.DataFrame( .....: { .....: "date": pd.date_range(start="2016-01-01", periods=4, freq="W"), .....: "group": [1, 1, 2, 2], .....: "val": [5, 6, 7, 8], .....: } .....: ).set_index("date") .....: In [137]: df_re Out[137]: group val date 2016-01-03 1 5 2016-01-10 1 6 2016-01-17 2 7 2016-01-24 2 8 In [138]: df_re.groupby("group").resample("1D").ffill() Out[138]: group val group date 1 2016-01-03 1 5 2016-01-04 1 5 2016-01-05 1 5 2016-01-06 1 5 2016-01-07 1 5 ... ... ... 2 2016-01-20 2 7 2016-01-21 2 7 2016-01-22 2 7 2016-01-23 2 7 2016-01-24 2 8 [16 rows x 2 columns] Filtration# The filter method returns a subset of the original object. Suppose we want to take only elements that belong to groups with a group sum greater than 2. In [139]: sf = pd.Series([1, 1, 2, 3, 3, 3]) In [140]: sf.groupby(sf).filter(lambda x: x.sum() > 2) Out[140]: 3 3 4 3 5 3 dtype: int64 The argument of filter must be a function that, applied to the group as a whole, returns True or False. Another useful operation is filtering out elements that belong to groups with only a couple members. In [141]: dff = pd.DataFrame({"A": np.arange(8), "B": list("aabbbbcc")}) In [142]: dff.groupby("B").filter(lambda x: len(x) > 2) Out[142]: A B 2 2 b 3 3 b 4 4 b 5 5 b Alternatively, instead of dropping the offending groups, we can return a like-indexed objects where the groups that do not pass the filter are filled with NaNs. In [143]: dff.groupby("B").filter(lambda x: len(x) > 2, dropna=False) Out[143]: A B 0 NaN NaN 1 NaN NaN 2 2.0 b 3 3.0 b 4 4.0 b 5 5.0 b 6 NaN NaN 7 NaN NaN For DataFrames with multiple columns, filters should explicitly specify a column as the filter criterion. In [144]: dff["C"] = np.arange(8) In [145]: dff.groupby("B").filter(lambda x: len(x["C"]) > 2) Out[145]: A B C 2 2 b 2 3 3 b 3 4 4 b 4 5 5 b 5 Note Some functions when applied to a groupby object will act as a filter on the input, returning a reduced shape of the original (and potentially eliminating groups), but with the index unchanged. Passing as_index=False will not affect these transformation methods. For example: head, tail. In [146]: dff.groupby("B").head(2) Out[146]: A B C 0 0 a 0 1 1 a 1 2 2 b 2 3 3 b 3 6 6 c 6 7 7 c 7 Dispatching to instance methods# When doing an aggregation or transformation, you might just want to call an instance method on each data group. This is pretty easy to do by passing lambda functions: In [147]: grouped = df.groupby("A") In [148]: grouped.agg(lambda x: x.std()) Out[148]: C D A bar 0.181231 1.366330 foo 0.912265 0.884785 But, it’s rather verbose and can be untidy if you need to pass additional arguments. Using a bit of metaprogramming cleverness, GroupBy now has the ability to “dispatch” method calls to the groups: In [149]: grouped.std() Out[149]: C D A bar 0.181231 1.366330 foo 0.912265 0.884785 What is actually happening here is that a function wrapper is being generated. When invoked, it takes any passed arguments and invokes the function with any arguments on each group (in the above example, the std function). The results are then combined together much in the style of agg and transform (it actually uses apply to infer the gluing, documented next). This enables some operations to be carried out rather succinctly: In [150]: tsdf = pd.DataFrame( .....: np.random.randn(1000, 3), .....: index=pd.date_range("1/1/2000", periods=1000), .....: columns=["A", "B", "C"], .....: ) .....: In [151]: tsdf.iloc[::2] = np.nan In [152]: grouped = tsdf.groupby(lambda x: x.year) In [153]: grouped.fillna(method="pad") Out[153]: A B C 2000-01-01 NaN NaN NaN 2000-01-02 -0.353501 -0.080957 -0.876864 2000-01-03 -0.353501 -0.080957 -0.876864 2000-01-04 0.050976 0.044273 -0.559849 2000-01-05 0.050976 0.044273 -0.559849 ... ... ... ... 2002-09-22 0.005011 0.053897 -1.026922 2002-09-23 0.005011 0.053897 -1.026922 2002-09-24 -0.456542 -1.849051 1.559856 2002-09-25 -0.456542 -1.849051 1.559856 2002-09-26 1.123162 0.354660 1.128135 [1000 rows x 3 columns] In this example, we chopped the collection of time series into yearly chunks then independently called fillna on the groups. The nlargest and nsmallest methods work on Series style groupbys: In [154]: s = pd.Series([9, 8, 7, 5, 19, 1, 4.2, 3.3]) In [155]: g = pd.Series(list("abababab")) In [156]: gb = s.groupby(g) In [157]: gb.nlargest(3) Out[157]: a 4 19.0 0 9.0 2 7.0 b 1 8.0 3 5.0 7 3.3 dtype: float64 In [158]: gb.nsmallest(3) Out[158]: a 6 4.2 2 7.0 0 9.0 b 5 1.0 7 3.3 3 5.0 dtype: float64 Flexible apply# Some operations on the grouped data might not fit into either the aggregate or transform categories. Or, you may simply want GroupBy to infer how to combine the results. For these, use the apply function, which can be substituted for both aggregate and transform in many standard use cases. However, apply can handle some exceptional use cases. Note apply can act as a reducer, transformer, or filter function, depending on exactly what is passed to it. It can depend on the passed function and exactly what you are grouping. Thus the grouped column(s) may be included in the output as well as set the indices. In [159]: df Out[159]: A B C D 0 foo one -0.575247 1.346061 1 bar one 0.254161 1.511763 2 foo two -1.143704 1.627081 3 bar three 0.215897 -0.990582 4 foo two 1.193555 -0.441652 5 bar two -0.077118 1.211526 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 In [160]: grouped = df.groupby("A") # could also just call .describe() In [161]: grouped["C"].apply(lambda x: x.describe()) Out[161]: A bar count 3.000000 mean 0.130980 std 0.181231 min -0.077118 25% 0.069390 ... foo min -1.143704 25% -0.862495 50% -0.575247 75% -0.408530 max 1.193555 Name: C, Length: 16, dtype: float64 The dimension of the returned result can also change: In [162]: grouped = df.groupby('A')['C'] In [163]: def f(group): .....: return pd.DataFrame({'original': group, .....: 'demeaned': group - group.mean()}) .....: apply on a Series can operate on a returned value from the applied function, that is itself a series, and possibly upcast the result to a DataFrame: In [164]: def f(x): .....: return pd.Series([x, x ** 2], index=["x", "x^2"]) .....: In [165]: s = pd.Series(np.random.rand(5)) In [166]: s Out[166]: 0 0.321438 1 0.493496 2 0.139505 3 0.910103 4 0.194158 dtype: float64 In [167]: s.apply(f) Out[167]: x x^2 0 0.321438 0.103323 1 0.493496 0.243538 2 0.139505 0.019462 3 0.910103 0.828287 4 0.194158 0.037697 Control grouped column(s) placement with group_keys# Note If group_keys=True is specified when calling groupby(), functions passed to apply that return like-indexed outputs will have the group keys added to the result index. Previous versions of pandas would add the group keys only when the result from the applied function had a different index than the input. If group_keys is not specified, the group keys will not be added for like-indexed outputs. In the future this behavior will change to always respect group_keys, which defaults to True. Changed in version 1.5.0. To control whether the grouped column(s) are included in the indices, you can use the argument group_keys. Compare In [168]: df.groupby("A", group_keys=True).apply(lambda x: x) Out[168]: A B C D A bar 1 bar one 0.254161 1.511763 3 bar three 0.215897 -0.990582 5 bar two -0.077118 1.211526 foo 0 foo one -0.575247 1.346061 2 foo two -1.143704 1.627081 4 foo two 1.193555 -0.441652 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 with In [169]: df.groupby("A", group_keys=False).apply(lambda x: x) Out[169]: A B C D 0 foo one -0.575247 1.346061 1 bar one 0.254161 1.511763 2 foo two -1.143704 1.627081 3 bar three 0.215897 -0.990582 4 foo two 1.193555 -0.441652 5 bar two -0.077118 1.211526 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 Similar to Aggregations with User-Defined Functions, the resulting dtype will reflect that of the apply function. If the results from different groups have different dtypes, then a common dtype will be determined in the same way as DataFrame construction. Numba Accelerated Routines# New in version 1.1. If Numba is installed as an optional dependency, the transform and aggregate methods support engine='numba' and engine_kwargs arguments. See enhancing performance with Numba for general usage of the arguments and performance considerations. The function signature must start with values, index exactly as the data belonging to each group will be passed into values, and the group index will be passed into index. Warning When using engine='numba', there will be no “fall back” behavior internally. The group data and group index will be passed as NumPy arrays to the JITed user defined function, and no alternative execution attempts will be tried. Other useful features# Automatic exclusion of “nuisance” columns# Again consider the example DataFrame we’ve been looking at: In [170]: df Out[170]: A B C D 0 foo one -0.575247 1.346061 1 bar one 0.254161 1.511763 2 foo two -1.143704 1.627081 3 bar three 0.215897 -0.990582 4 foo two 1.193555 -0.441652 5 bar two -0.077118 1.211526 6 foo one -0.408530 0.268520 7 foo three -0.862495 0.024580 Suppose we wish to compute the standard deviation grouped by the A column. There is a slight problem, namely that we don’t care about the data in column B. We refer to this as a “nuisance” column. You can avoid nuisance columns by specifying numeric_only=True: In [171]: df.groupby("A").std(numeric_only=True) Out[171]: C D A bar 0.181231 1.366330 foo 0.912265 0.884785 Note that df.groupby('A').colname.std(). is more efficient than df.groupby('A').std().colname, so if the result of an aggregation function is only interesting over one column (here colname), it may be filtered before applying the aggregation function. Note Any object column, also if it contains numerical values such as Decimal objects, is considered as a “nuisance” columns. They are excluded from aggregate functions automatically in groupby. If you do wish to include decimal or object columns in an aggregation with other non-nuisance data types, you must do so explicitly. Warning The automatic dropping of nuisance columns has been deprecated and will be removed in a future version of pandas. If columns are included that cannot be operated on, pandas will instead raise an error. In order to avoid this, either select the columns you wish to operate on or specify numeric_only=True. In [172]: from decimal import Decimal In [173]: df_dec = pd.DataFrame( .....: { .....: "id": [1, 2, 1, 2], .....: "int_column": [1, 2, 3, 4], .....: "dec_column": [ .....: Decimal("0.50"), .....: Decimal("0.15"), .....: Decimal("0.25"), .....: Decimal("0.40"), .....: ], .....: } .....: ) .....: # Decimal columns can be sum'd explicitly by themselves... In [174]: df_dec.groupby(["id"])[["dec_column"]].sum() Out[174]: dec_column id 1 0.75 2 0.55 # ...but cannot be combined with standard data types or they will be excluded In [175]: df_dec.groupby(["id"])[["int_column", "dec_column"]].sum() Out[175]: int_column id 1 4 2 6 # Use .agg function to aggregate over standard and "nuisance" data types # at the same time In [176]: df_dec.groupby(["id"]).agg({"int_column": "sum", "dec_column": "sum"}) Out[176]: int_column dec_column id 1 4 0.75 2 6 0.55 Handling of (un)observed Categorical values# When using a Categorical grouper (as a single grouper, or as part of multiple groupers), the observed keyword controls whether to return a cartesian product of all possible groupers values (observed=False) or only those that are observed groupers (observed=True). Show all values: In [177]: pd.Series([1, 1, 1]).groupby( .....: pd.Categorical(["a", "a", "a"], categories=["a", "b"]), observed=False .....: ).count() .....: Out[177]: a 3 b 0 dtype: int64 Show only the observed values: In [178]: pd.Series([1, 1, 1]).groupby( .....: pd.Categorical(["a", "a", "a"], categories=["a", "b"]), observed=True .....: ).count() .....: Out[178]: a 3 dtype: int64 The returned dtype of the grouped will always include all of the categories that were grouped. In [179]: s = ( .....: pd.Series([1, 1, 1]) .....: .groupby(pd.Categorical(["a", "a", "a"], categories=["a", "b"]), observed=False) .....: .count() .....: ) .....: In [180]: s.index.dtype Out[180]: CategoricalDtype(categories=['a', 'b'], ordered=False) NA and NaT group handling# If there are any NaN or NaT values in the grouping key, these will be automatically excluded. In other words, there will never be an “NA group” or “NaT group”. This was not the case in older versions of pandas, but users were generally discarding the NA group anyway (and supporting it was an implementation headache). Grouping with ordered factors# Categorical variables represented as instance of pandas’s Categorical class can be used as group keys. If so, the order of the levels will be preserved: In [181]: data = pd.Series(np.random.randn(100)) In [182]: factor = pd.qcut(data, [0, 0.25, 0.5, 0.75, 1.0]) In [183]: data.groupby(factor).mean() Out[183]: (-2.645, -0.523] -1.362896 (-0.523, 0.0296] -0.260266 (0.0296, 0.654] 0.361802 (0.654, 2.21] 1.073801 dtype: float64 Grouping with a grouper specification# You may need to specify a bit more data to properly group. You can use the pd.Grouper to provide this local control. In [184]: import datetime In [185]: df = pd.DataFrame( .....: { .....: "Branch": "A A A A A A A B".split(), .....: "Buyer": "Carl Mark Carl Carl Joe Joe Joe Carl".split(), .....: "Quantity": [1, 3, 5, 1, 8, 1, 9, 3], .....: "Date": [ .....: datetime.datetime(2013, 1, 1, 13, 0), .....: datetime.datetime(2013, 1, 1, 13, 5), .....: datetime.datetime(2013, 10, 1, 20, 0), .....: datetime.datetime(2013, 10, 2, 10, 0), .....: datetime.datetime(2013, 10, 1, 20, 0), .....: datetime.datetime(2013, 10, 2, 10, 0), .....: datetime.datetime(2013, 12, 2, 12, 0), .....: datetime.datetime(2013, 12, 2, 14, 0), .....: ], .....: } .....: ) .....: In [186]: df Out[186]: Branch Buyer Quantity Date 0 A Carl 1 2013-01-01 13:00:00 1 A Mark 3 2013-01-01 13:05:00 2 A Carl 5 2013-10-01 20:00:00 3 A Carl 1 2013-10-02 10:00:00 4 A Joe 8 2013-10-01 20:00:00 5 A Joe 1 2013-10-02 10:00:00 6 A Joe 9 2013-12-02 12:00:00 7 B Carl 3 2013-12-02 14:00:00 Groupby a specific column with the desired frequency. This is like resampling. In [187]: df.groupby([pd.Grouper(freq="1M", key="Date"), "Buyer"])[["Quantity"]].sum() Out[187]: Quantity Date Buyer 2013-01-31 Carl 1 Mark 3 2013-10-31 Carl 6 Joe 9 2013-12-31 Carl 3 Joe 9 You have an ambiguous specification in that you have a named index and a column that could be potential groupers. In [188]: df = df.set_index("Date") In [189]: df["Date"] = df.index + pd.offsets.MonthEnd(2) In [190]: df.groupby([pd.Grouper(freq="6M", key="Date"), "Buyer"])[["Quantity"]].sum() Out[190]: Quantity Date Buyer 2013-02-28 Carl 1 Mark 3 2014-02-28 Carl 9 Joe 18 In [191]: df.groupby([pd.Grouper(freq="6M", level="Date"), "Buyer"])[["Quantity"]].sum() Out[191]: Quantity Date Buyer 2013-01-31 Carl 1 Mark 3 2014-01-31 Carl 9 Joe 18 Taking the first rows of each group# Just like for a DataFrame or Series you can call head and tail on a groupby: In [192]: df = pd.DataFrame([[1, 2], [1, 4], [5, 6]], columns=["A", "B"]) In [193]: df Out[193]: A B 0 1 2 1 1 4 2 5 6 In [194]: g = df.groupby("A") In [195]: g.head(1) Out[195]: A B 0 1 2 2 5 6 In [196]: g.tail(1) Out[196]: A B 1 1 4 2 5 6 This shows the first or last n rows from each group. Taking the nth row of each group# To select from a DataFrame or Series the nth item, use nth(). This is a reduction method, and will return a single row (or no row) per group if you pass an int for n: In [197]: df = pd.DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=["A", "B"]) In [198]: g = df.groupby("A") In [199]: g.nth(0) Out[199]: B A 1 NaN 5 6.0 In [200]: g.nth(-1) Out[200]: B A 1 4.0 5 6.0 In [201]: g.nth(1) Out[201]: B A 1 4.0 If you want to select the nth not-null item, use the dropna kwarg. For a DataFrame this should be either 'any' or 'all' just like you would pass to dropna: # nth(0) is the same as g.first() In [202]: g.nth(0, dropna="any") Out[202]: B A 1 4.0 5 6.0 In [203]: g.first() Out[203]: B A 1 4.0 5 6.0 # nth(-1) is the same as g.last() In [204]: g.nth(-1, dropna="any") # NaNs denote group exhausted when using dropna Out[204]: B A 1 4.0 5 6.0 In [205]: g.last() Out[205]: B A 1 4.0 5 6.0 In [206]: g.B.nth(0, dropna="all") Out[206]: A 1 4.0 5 6.0 Name: B, dtype: float64 As with other methods, passing as_index=False, will achieve a filtration, which returns the grouped row. In [207]: df = pd.DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=["A", "B"]) In [208]: g = df.groupby("A", as_index=False) In [209]: g.nth(0) Out[209]: A B 0 1 NaN 2 5 6.0 In [210]: g.nth(-1) Out[210]: A B 1 1 4.0 2 5 6.0 You can also select multiple rows from each group by specifying multiple nth values as a list of ints. In [211]: business_dates = pd.date_range(start="4/1/2014", end="6/30/2014", freq="B") In [212]: df = pd.DataFrame(1, index=business_dates, columns=["a", "b"]) # get the first, 4th, and last date index for each month In [213]: df.groupby([df.index.year, df.index.month]).nth([0, 3, -1]) Out[213]: a b 2014 4 1 1 4 1 1 4 1 1 5 1 1 5 1 1 5 1 1 6 1 1 6 1 1 6 1 1 Enumerate group items# To see the order in which each row appears within its group, use the cumcount method: In [214]: dfg = pd.DataFrame(list("aaabba"), columns=["A"]) In [215]: dfg Out[215]: A 0 a 1 a 2 a 3 b 4 b 5 a In [216]: dfg.groupby("A").cumcount() Out[216]: 0 0 1 1 2 2 3 0 4 1 5 3 dtype: int64 In [217]: dfg.groupby("A").cumcount(ascending=False) Out[217]: 0 3 1 2 2 1 3 1 4 0 5 0 dtype: int64 Enumerate groups# To see the ordering of the groups (as opposed to the order of rows within a group given by cumcount) you can use ngroup(). Note that the numbers given to the groups match the order in which the groups would be seen when iterating over the groupby object, not the order they are first observed. In [218]: dfg = pd.DataFrame(list("aaabba"), columns=["A"]) In [219]: dfg Out[219]: A 0 a 1 a 2 a 3 b 4 b 5 a In [220]: dfg.groupby("A").ngroup() Out[220]: 0 0 1 0 2 0 3 1 4 1 5 0 dtype: int64 In [221]: dfg.groupby("A").ngroup(ascending=False) Out[221]: 0 1 1 1 2 1 3 0 4 0 5 1 dtype: int64 Plotting# Groupby also works with some plotting methods. For example, suppose we suspect that some features in a DataFrame may differ by group, in this case, the values in column 1 where the group is “B” are 3 higher on average. In [222]: np.random.seed(1234) In [223]: df = pd.DataFrame(np.random.randn(50, 2)) In [224]: df["g"] = np.random.choice(["A", "B"], size=50) In [225]: df.loc[df["g"] == "B", 1] += 3 We can easily visualize this with a boxplot: In [226]: df.groupby("g").boxplot() Out[226]: A AxesSubplot(0.1,0.15;0.363636x0.75) B AxesSubplot(0.536364,0.15;0.363636x0.75) dtype: object The result of calling boxplot is a dictionary whose keys are the values of our grouping column g (“A” and “B”). The values of the resulting dictionary can be controlled by the return_type keyword of boxplot. See the visualization documentation for more. Warning For historical reasons, df.groupby("g").boxplot() is not equivalent to df.boxplot(by="g"). See here for an explanation. Piping function calls# Similar to the functionality provided by DataFrame and Series, functions that take GroupBy objects can be chained together using a pipe method to allow for a cleaner, more readable syntax. To read about .pipe in general terms, see here. Combining .groupby and .pipe is often useful when you need to reuse GroupBy objects. As an example, imagine having a DataFrame with columns for stores, products, revenue and quantity sold. We’d like to do a groupwise calculation of prices (i.e. revenue/quantity) per store and per product. We could do this in a multi-step operation, but expressing it in terms of piping can make the code more readable. First we set the data: In [227]: n = 1000 In [228]: df = pd.DataFrame( .....: { .....: "Store": np.random.choice(["Store_1", "Store_2"], n), .....: "Product": np.random.choice(["Product_1", "Product_2"], n), .....: "Revenue": (np.random.random(n) * 50 + 10).round(2), .....: "Quantity": np.random.randint(1, 10, size=n), .....: } .....: ) .....: In [229]: df.head(2) Out[229]: Store Product Revenue Quantity 0 Store_2 Product_1 26.12 1 1 Store_2 Product_1 28.86 1 Now, to find prices per store/product, we can simply do: In [230]: ( .....: df.groupby(["Store", "Product"]) .....: .pipe(lambda grp: grp.Revenue.sum() / grp.Quantity.sum()) .....: .unstack() .....: .round(2) .....: ) .....: Out[230]: Product Product_1 Product_2 Store Store_1 6.82 7.05 Store_2 6.30 6.64 Piping can also be expressive when you want to deliver a grouped object to some arbitrary function, for example: In [231]: def mean(groupby): .....: return groupby.mean() .....: In [232]: df.groupby(["Store", "Product"]).pipe(mean) Out[232]: Revenue Quantity Store Product Store_1 Product_1 34.622727 5.075758 Product_2 35.482815 5.029630 Store_2 Product_1 32.972837 5.237589 Product_2 34.684360 5.224000 where mean takes a GroupBy object and finds the mean of the Revenue and Quantity columns respectively for each Store-Product combination. The mean function can be any function that takes in a GroupBy object; the .pipe will pass the GroupBy object as a parameter into the function you specify. Examples# Regrouping by factor# Regroup columns of a DataFrame according to their sum, and sum the aggregated ones. In [233]: df = pd.DataFrame({"a": [1, 0, 0], "b": [0, 1, 0], "c": [1, 0, 0], "d": [2, 3, 4]}) In [234]: df Out[234]: a b c d 0 1 0 1 2 1 0 1 0 3 2 0 0 0 4 In [235]: df.groupby(df.sum(), axis=1).sum() Out[235]: 1 9 0 2 2 1 1 3 2 0 4 Multi-column factorization# By using ngroup(), we can extract information about the groups in a way similar to factorize() (as described further in the reshaping API) but which applies naturally to multiple columns of mixed type and different sources. This can be useful as an intermediate categorical-like step in processing, when the relationships between the group rows are more important than their content, or as input to an algorithm which only accepts the integer encoding. (For more information about support in pandas for full categorical data, see the Categorical introduction and the API documentation.) In [236]: dfg = pd.DataFrame({"A": [1, 1, 2, 3, 2], "B": list("aaaba")}) In [237]: dfg Out[237]: A B 0 1 a 1 1 a 2 2 a 3 3 b 4 2 a In [238]: dfg.groupby(["A", "B"]).ngroup() Out[238]: 0 0 1 0 2 1 3 2 4 1 dtype: int64 In [239]: dfg.groupby(["A", [0, 0, 0, 1, 1]]).ngroup() Out[239]: 0 0 1 0 2 1 3 3 4 2 dtype: int64 Groupby by indexer to ‘resample’ data# Resampling produces new hypothetical samples (resamples) from already existing observed data or from a model that generates data. These new samples are similar to the pre-existing samples. In order to resample to work on indices that are non-datetimelike, the following procedure can be utilized. In the following examples, df.index // 5 returns a binary array which is used to determine what gets selected for the groupby operation. Note The below example shows how we can downsample by consolidation of samples into fewer samples. Here by using df.index // 5, we are aggregating the samples in bins. By applying std() function, we aggregate the information contained in many samples into a small subset of values which is their standard deviation thereby reducing the number of samples. In [240]: df = pd.DataFrame(np.random.randn(10, 2)) In [241]: df Out[241]: 0 1 0 -0.793893 0.321153 1 0.342250 1.618906 2 -0.975807 1.918201 3 -0.810847 -1.405919 4 -1.977759 0.461659 5 0.730057 -1.316938 6 -0.751328 0.528290 7 -0.257759 -1.081009 8 0.505895 -1.701948 9 -1.006349 0.020208 In [242]: df.index // 5 Out[242]: Int64Index([0, 0, 0, 0, 0, 1, 1, 1, 1, 1], dtype='int64') In [243]: df.groupby(df.index // 5).std() Out[243]: 0 1 0 0.823647 1.312912 1 0.760109 0.942941 Returning a Series to propagate names# Group DataFrame columns, compute a set of metrics and return a named Series. The Series name is used as the name for the column index. This is especially useful in conjunction with reshaping operations such as stacking in which the column index name will be used as the name of the inserted column: In [244]: df = pd.DataFrame( .....: { .....: "a": [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], .....: "b": [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1], .....: "c": [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], .....: "d": [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1], .....: } .....: ) .....: In [245]: def compute_metrics(x): .....: result = {"b_sum": x["b"].sum(), "c_mean": x["c"].mean()} .....: return pd.Series(result, name="metrics") .....: In [246]: result = df.groupby("a").apply(compute_metrics) In [247]: result Out[247]: metrics b_sum c_mean a 0 2.0 0.5 1 2.0 0.5 2 2.0 0.5 In [248]: result.stack() Out[248]: a metrics 0 b_sum 2.0 c_mean 0.5 1 b_sum 2.0 c_mean 0.5 2 b_sum 2.0 c_mean 0.5 dtype: float64
32
1,038
Merging table by group in pandas I have a dataset with human genes. I want to check all possible combination of alleles. So my df: Human Gene AllelA AllelB 1 gene1 C C 1 gene2 A T 1 gene3 G G 1 gene4 T A 2 gene1 C C 2 gene2 G G 3 gene4 C C 4 gene1 A T 5 gene4 C C And now I need to check all ways so I think the better way will be pivot thah table to (first I merging gene to one column Allele1Allele2, so in Gene1 for Human1 it will be one column contains: CC): humanNumber Gene1 Gene2 Gene3 Gene4 And then just do some magic with merge columns. But now I have a problem with pivot it. When I tried I got error telling that my index is not unique (indeed it isn't), so I tried to group by the table, but it's completly unclear for my to pivot it. I know - and I did - I can do that by loop in pure python, but first: it's long (80k people x 2|3|4 genes) and second: I think pandas can do that better. I just can't applay the pivot function. Tried in different way, using different indexes... but still problems. Expectation: humanNumber Gene1 Gene2 Gene3 Gene4 1 CC AT GG TA etc. Can someone help me do that right? P.S. whole dataframe is filled by strings
Problem are duplicates by Human and Gene, you can check them: print (df) Human Gene AllelA AllelB 0 1 gene1 C C <- added dupe row for test 1 1 gene1 C C 2 1 gene2 A T 3 1 gene3 G G 4 1 gene4 T A 5 2 gene1 C C 6 2 gene2 G G 7 3 gene4 C C 8 4 gene1 A T 9 5 gene4 C C print (df[df.duplicated(['Human','Gene'], keep=False)]) Human Gene AllelA AllelB 0 1 gene1 C C 1 1 gene1 C C If possible, you can remove them by DataFrame.drop_duplicates: df = (df.drop_duplicates(['Human','Gene']) .assign(Alle = lambda x: df['AllelA'] + df['AllelB']) .pivot('Human','Gene','Alle')) print (df) Gene gene1 gene2 gene3 gene4 Human 1 CC AT GG TA 2 CC GG NaN NaN 3 NaN NaN NaN CC 4 AT NaN NaN NaN 5 NaN NaN NaN CC
67,097,110
Convert elements in lists to separate rows
<p>My data frame has approximately 30 columns. Some of these columns have lists of items, for instance</p> <pre><code> Student Subject \ 0 J.M. [mathematics, history, literature] 1 M.G. [physics, mathematics, geography, history] 2 L.D. [latin, literature, mathematics] Score # + other 27 columns 0 [10, 8, 8.5] 1 [5, 4, 8, 8.5] 2 [4, 5, 5] </code></pre> <p>How can I convert elements in lists to separate rows to have subjects and scores in rows and not in lists?</p> <p>Generally,</p> <pre><code>Student Subject Score student 1 student 1's subject 1 student 1' score for subject 1 student 1 student 1's subject 2 student 1' score for subject 2 </code></pre>
67,097,208
"2021-04-14T18:22:51.170000"
1
null
1
530
python|pandas
<p>Assuming <code>df</code> to be:</p> <pre><code>In [2893]: df = pd.DataFrame({'Student':['J.M.', 'M.G.', 'L.D.'], 'Subject':[['mathematics', 'history', 'literature'], ['physics', 'mathematics', 'geography', 'history'], ['latin', 'literature', 'mathematics']], 'Score':[[10, 8, 8.5], [5, 4, 8, 8.5], [4, ...: 5, 5]]}) In [2894]: df Out[2894]: Student Subject Score 0 J.M. [mathematics, history, literature] [10, 8, 8.5] 1 M.G. [physics, mathematics, geography, history] [5, 4, 8, 8.5] 2 L.D. [latin, literature, mathematics] [4, 5, 5] </code></pre> <p>Use <code>df.explode</code> with <code>df.apply</code>:</p> <pre><code>In [2898]: df = df.apply(pd.Series.explode) In [2899]: df Out[2899]: Student Subject Score 0 J.M. mathematics 10 1 J.M. history 8 2 J.M. literature 8.5 3 M.G. physics 5 4 M.G. mathematics 4 5 M.G. geography 8 6 M.G. history 8.5 7 L.D. latin 4 8 L.D. literature 5 9 L.D. mathematics 5 </code></pre>
"2021-04-14T18:30:27.353000"
5
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.explode.html
pandas.DataFrame.explode# pandas.DataFrame.explode# DataFrame.explode(column, ignore_index=False)[source]# Transform each element of a list-like to a row, replicating index values. New in version 0.25.0. Parameters Assuming df to be: In [2893]: df = pd.DataFrame({'Student':['J.M.', 'M.G.', 'L.D.'], 'Subject':[['mathematics', 'history', 'literature'], ['physics', 'mathematics', 'geography', 'history'], ['latin', 'literature', 'mathematics']], 'Score':[[10, 8, 8.5], [5, 4, 8, 8.5], [4, ...: 5, 5]]}) In [2894]: df Out[2894]: Student Subject Score 0 J.M. [mathematics, history, literature] [10, 8, 8.5] 1 M.G. [physics, mathematics, geography, history] [5, 4, 8, 8.5] 2 L.D. [latin, literature, mathematics] [4, 5, 5] Use df.explode with df.apply: In [2898]: df = df.apply(pd.Series.explode) In [2899]: df Out[2899]: Student Subject Score 0 J.M. mathematics 10 1 J.M. history 8 2 J.M. literature 8.5 3 M.G. physics 5 4 M.G. mathematics 4 5 M.G. geography 8 6 M.G. history 8.5 7 L.D. latin 4 8 L.D. literature 5 9 L.D. mathematics 5 columnIndexLabelColumn(s) to explode. For multiple columns, specify a non-empty list with each element be str or tuple, and all specified columns their list-like data on same row of the frame must have matching length. New in version 1.3.0: Multi-column explode ignore_indexbool, default FalseIf True, the resulting index will be labeled 0, 1, …, n - 1. New in version 1.1.0. Returns DataFrameExploded lists to rows of the subset columns; index will be duplicated for these rows. Raises ValueError If columns of the frame are not unique. If specified columns to explode is empty list. If specified columns to explode have not matching count of elements rowwise in the frame. See also DataFrame.unstackPivot a level of the (necessarily hierarchical) index labels. DataFrame.meltUnpivot a DataFrame from wide format to long format. Series.explodeExplode a DataFrame from list-like columns to long format. Notes This routine will explode list-likes including lists, tuples, sets, Series, and np.ndarray. The result dtype of the subset rows will be object. Scalars will be returned unchanged, and empty list-likes will result in a np.nan for that row. In addition, the ordering of rows in the output will be non-deterministic when exploding sets. Reference the user guide for more examples. Examples >>> df = pd.DataFrame({'A': [[0, 1, 2], 'foo', [], [3, 4]], ... 'B': 1, ... 'C': [['a', 'b', 'c'], np.nan, [], ['d', 'e']]}) >>> df A B C 0 [0, 1, 2] 1 [a, b, c] 1 foo 1 NaN 2 [] 1 [] 3 [3, 4] 1 [d, e] Single-column explode. >>> df.explode('A') A B C 0 0 1 [a, b, c] 0 1 1 [a, b, c] 0 2 1 [a, b, c] 1 foo 1 NaN 2 NaN 1 [] 3 3 1 [d, e] 3 4 1 [d, e] Multi-column explode. >>> df.explode(list('AC')) A B C 0 0 1 a 0 1 1 b 0 2 1 c 1 foo 1 NaN 2 NaN 1 NaN 3 3 1 d 3 4 1 e
222
1,245
Convert elements in lists to separate rows My data frame has approximately 30 columns. Some of these columns have lists of items, for instance Student Subject \ 0 J.M. [mathematics, history, literature] 1 M.G. [physics, mathematics, geography, history] 2 L.D. [latin, literature, mathematics] Score # + other 27 columns 0 [10, 8, 8.5] 1 [5, 4, 8, 8.5] 2 [4, 5, 5] How can I convert elements in lists to separate rows to have subjects and scores in rows and not in lists? Generally, Student Subject Score student 1 student 1's subject 1 student 1' score for subject 1 student 1 student 1's subject 2 student 1' score for subject 2
explode /
Assuming df to be: In [2893]: df = pd.DataFrame({'Student':['J.M.', 'M.G.', 'L.D.'], 'Subject':[['mathematics', 'history', 'literature'], ['physics', 'mathematics', 'geography', 'history'], ['latin', 'literature', 'mathematics']], 'Score':[[10, 8, 8.5], [5, 4, 8, 8.5], [4, ...: 5, 5]]}) In [2894]: df Out[2894]: Student Subject Score 0 J.M. [mathematics, history, literature] [10, 8, 8.5] 1 M.G. [physics, mathematics, geography, history] [5, 4, 8, 8.5] 2 L.D. [latin, literature, mathematics] [4, 5, 5] Use df.explode with df.apply: In [2898]: df = df.apply(pd.Series.explode) In [2899]: df Out[2899]: Student Subject Score 0 J.M. mathematics 10 1 J.M. history 8 2 J.M. literature 8.5 3 M.G. physics 5 4 M.G. mathematics 4 5 M.G. geography 8 6 M.G. history 8.5 7 L.D. latin 4 8 L.D. literature 5 9 L.D. mathematics 5
68,170,724
How to access a dict column field in pandas
<p>Having the follow dataframe</p> <pre><code>df = pd.DataFrame([[30, 20, {'some_data': 30}]], columns=['a', 'b', 'c']) </code></pre> <p>I would like to create a new column with the value of <code>some_data</code> value. I was thinking something like:</p> <pre><code>df['new_column'] = df['c']['some_data'] </code></pre> <p>Is there a simple way to do it? In reality the dict would be more complex, I will have to get a nested value.</p> <p>EDIT 1:</p> <p>Here is a example where I have nested data, it's closer to real problem.</p> <pre><code>df = pd.DataFrame([[30, 20, {'some_data': [{'other_data': 0}]}]], columns=['a', 'b', 'c']) # I would like to do something like: df['new_column'] = df['c']['some_data'][0]['other_data'] </code></pre>
68,170,736
"2021-06-28T22:41:43.553000"
3
null
2
1,170
python|pandas
<p>Use the <code>.str</code> accessor:</p> <pre><code>df.c.str['some_data'] #0 30 #Name: c, dtype: int64 </code></pre> <hr /> <p>You can further chain <code>.str</code> for nested data access, given:</p> <pre><code>df = pd.DataFrame([[30, 20, {'some_data': [{'other_data': 0}]}]], columns=['a', 'b', 'c']) df # a b c #0 30 20 {'some_data': [{'other_data': 0}]} </code></pre> <p>To access nested <code>other_data</code> field, you can do:</p> <pre><code>df.c.str['some_data'].str[0].str['other_data'] #0 0 #Name: c, dtype: int64 </code></pre>
"2021-06-28T22:43:39.653000"
5
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.from_dict.html
pandas.DataFrame.from_dict# pandas.DataFrame.from_dict# classmethod DataFrame.from_dict(data, orient='columns', dtype=None, columns=None)[source]# Construct DataFrame from dict of array-like or dicts. Creates DataFrame object from dictionary by columns or by index allowing dtype specification. Parameters datadictOf the form {field : array-like} or {field : dict}. orient{‘columns’, ‘index’, ‘tight’}, default ‘columns’The “orientation” of the data. If the keys of the passed dict Use the .str accessor: df.c.str['some_data'] #0 30 #Name: c, dtype: int64 You can further chain .str for nested data access, given: df = pd.DataFrame([[30, 20, {'some_data': [{'other_data': 0}]}]], columns=['a', 'b', 'c']) df # a b c #0 30 20 {'some_data': [{'other_data': 0}]} To access nested other_data field, you can do: df.c.str['some_data'].str[0].str['other_data'] #0 0 #Name: c, dtype: int64 should be the columns of the resulting DataFrame, pass ‘columns’ (default). Otherwise if the keys should be rows, pass ‘index’. If ‘tight’, assume a dict with keys [‘index’, ‘columns’, ‘data’, ‘index_names’, ‘column_names’]. New in version 1.4.0: ‘tight’ as an allowed value for the orient argument dtypedtype, default NoneData type to force, otherwise infer. columnslist, default NoneColumn labels to use when orient='index'. Raises a ValueError if used with orient='columns' or orient='tight'. Returns DataFrame See also DataFrame.from_recordsDataFrame from structured ndarray, sequence of tuples or dicts, or DataFrame. DataFrameDataFrame object creation using constructor. DataFrame.to_dictConvert the DataFrame to a dictionary. Examples By default the keys of the dict become the DataFrame columns: >>> data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']} >>> pd.DataFrame.from_dict(data) col_1 col_2 0 3 a 1 2 b 2 1 c 3 0 d Specify orient='index' to create the DataFrame using dictionary keys as rows: >>> data = {'row_1': [3, 2, 1, 0], 'row_2': ['a', 'b', 'c', 'd']} >>> pd.DataFrame.from_dict(data, orient='index') 0 1 2 3 row_1 3 2 1 0 row_2 a b c d When using the ‘index’ orientation, the column names can be specified manually: >>> pd.DataFrame.from_dict(data, orient='index', ... columns=['A', 'B', 'C', 'D']) A B C D row_1 3 2 1 0 row_2 a b c d Specify orient='tight' to create the DataFrame using a ‘tight’ format: >>> data = {'index': [('a', 'b'), ('a', 'c')], ... 'columns': [('x', 1), ('y', 2)], ... 'data': [[1, 3], [2, 4]], ... 'index_names': ['n1', 'n2'], ... 'column_names': ['z1', 'z2']} >>> pd.DataFrame.from_dict(data, orient='tight') z1 x y z2 1 2 n1 n2 a b 1 3 c 2 4
489
941
How to access a dict column field in pandas Having the follow dataframe df = pd.DataFrame([[30, 20, {'some_data': 30}]], columns=['a', 'b', 'c']) I would like to create a new column with the value of some_data value. I was thinking something like: df['new_column'] = df['c']['some_data'] Is there a simple way to do it? In reality the dict would be more complex, I will have to get a nested value. EDIT 1: Here is a example where I have nested data, it's closer to real problem. df = pd.DataFrame([[30, 20, {'some_data': [{'other_data': 0}]}]], columns=['a', 'b', 'c']) # I would like to do something like: df['new_column'] = df['c']['some_data'][0]['other_data']
Use the .str accessor: df.c.str['some_data'] #0 30 #Name: c, dtype: int64 You can further chain .str for nested data access, given: df = pd.DataFrame([[30, 20, {'some_data': [{'other_data': 0}]}]], columns=['a', 'b', 'c']) df # a b c #0 30 20 {'some_data': [{'other_data': 0}]} To access nested other_data field, you can do: df.c.str['some_data'].str[0].str['other_data'] #0 0 #Name: c, dtype: int64
61,348,604
Combine dataframe with column value matching
<p>How can I do for below expected result with python code?</p> <pre class="lang-none prettyprint-override"><code>[DF1] [DF2] **Name** **Configure** **Name** MD0001 2G MD0001 MD0002 3G MD0001 MD0003 4G MD0001 MD0002 MD0002 MD0003 MD0003 MD0003 </code></pre> <p>Expected result:</p> <pre class="lang-none prettyprint-override"><code>Name Configure MD0001 2G MD0001 2G MD0001 2G MD0002 3G MD0002 3G MD0003 4G MD0003 4G MD0003 4G </code></pre> <p>Here is my code so far:</p> <pre><code>data_xlsx = pd.read_excel(data,skiprows=1,sheet_name='KPI') new_xlsx = pd.read_excel(new, skiprows=1,sheet_name='KPI') data_df = pd.Dataframe[data_xlsx ] new_xlsx = pd.Dataframe[new_xlsx ] </code></pre>
61,348,870
"2020-04-21T16:41:15.720000"
1
null
-1
27
python|pandas
<p>You should use merge or join. This is pandas' way of accomplishing SQL-like joins.</p> <pre><code># mock up the test dataframes df1 = pd.DataFrame.from_records( columns=["Name", "Configure"], data = [ ("MD0001", "2G"), ("MD0002", "3G"), ("MD0003", "4G") ] ) df2 = pd.DataFrame.from_records( columns = ["Name"], data = [ ("MD0001",), ("MD0001",), ("MD0001",), ("MD0002",), ("MD0002",), ("MD0003",), ("MD0003",), ("MD0003",) ] ) # index df1 on Name df1 = df1.set_index("Name") # then join it to df2 df2.join(df1['Configure'], on="Name") </code></pre> <p>Output:</p> <pre><code> Name Configure 0 MD0001 2G 1 MD0001 2G 2 MD0001 2G 3 MD0002 3G 4 MD0002 3G 5 MD0003 4G 6 MD0003 4G 7 MD0003 4G </code></pre>
"2020-04-21T16:54:50.493000"
0
https://pandas.pydata.org/docs/dev/user_guide/merging.html
Merge, join, concatenate and compare# Merge, join, concatenate and compare# pandas provides various facilities for easily combining together Series or You should use merge or join. This is pandas' way of accomplishing SQL-like joins. # mock up the test dataframes df1 = pd.DataFrame.from_records( columns=["Name", "Configure"], data = [ ("MD0001", "2G"), ("MD0002", "3G"), ("MD0003", "4G") ] ) df2 = pd.DataFrame.from_records( columns = ["Name"], data = [ ("MD0001",), ("MD0001",), ("MD0001",), ("MD0002",), ("MD0002",), ("MD0003",), ("MD0003",), ("MD0003",) ] ) # index df1 on Name df1 = df1.set_index("Name") # then join it to df2 df2.join(df1['Configure'], on="Name") Output: Name Configure 0 MD0001 2G 1 MD0001 2G 2 MD0001 2G 3 MD0002 3G 4 MD0002 3G 5 MD0003 4G 6 MD0003 4G 7 MD0003 4G DataFrame with various kinds of set logic for the indexes and relational algebra functionality in the case of join / merge-type operations. In addition, pandas also provides utilities to compare two Series or DataFrame and summarize their differences. Concatenating objects# The concat() function (in the main pandas namespace) does all of the heavy lifting of performing concatenation operations along an axis while performing optional set logic (union or intersection) of the indexes (if any) on the other axes. Note that I say “if any” because there is only a single possible axis of concatenation for Series. Before diving into all of the details of concat and what it can do, here is a simple example: In [1]: df1 = pd.DataFrame( ...: { ...: "A": ["A0", "A1", "A2", "A3"], ...: "B": ["B0", "B1", "B2", "B3"], ...: "C": ["C0", "C1", "C2", "C3"], ...: "D": ["D0", "D1", "D2", "D3"], ...: }, ...: index=[0, 1, 2, 3], ...: ) ...: In [2]: df2 = pd.DataFrame( ...: { ...: "A": ["A4", "A5", "A6", "A7"], ...: "B": ["B4", "B5", "B6", "B7"], ...: "C": ["C4", "C5", "C6", "C7"], ...: "D": ["D4", "D5", "D6", "D7"], ...: }, ...: index=[4, 5, 6, 7], ...: ) ...: In [3]: df3 = pd.DataFrame( ...: { ...: "A": ["A8", "A9", "A10", "A11"], ...: "B": ["B8", "B9", "B10", "B11"], ...: "C": ["C8", "C9", "C10", "C11"], ...: "D": ["D8", "D9", "D10", "D11"], ...: }, ...: index=[8, 9, 10, 11], ...: ) ...: In [4]: frames = [df1, df2, df3] In [5]: result = pd.concat(frames) Like its sibling function on ndarrays, numpy.concatenate, pandas.concat takes a list or dict of homogeneously-typed objects and concatenates them with some configurable handling of “what to do with the other axes”: pd.concat( objs, axis=0, join="outer", ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, copy=True, ) objs : a sequence or mapping of Series or DataFrame objects. If a dict is passed, the sorted keys will be used as the keys argument, unless it is passed, in which case the values will be selected (see below). Any None objects will be dropped silently unless they are all None in which case a ValueError will be raised. axis : {0, 1, …}, default 0. The axis to concatenate along. join : {‘inner’, ‘outer’}, default ‘outer’. How to handle indexes on other axis(es). Outer for union and inner for intersection. ignore_index : boolean, default False. If True, do not use the index values on the concatenation axis. The resulting axis will be labeled 0, …, n - 1. This is useful if you are concatenating objects where the concatenation axis does not have meaningful indexing information. Note the index values on the other axes are still respected in the join. keys : sequence, default None. Construct hierarchical index using the passed keys as the outermost level. If multiple levels passed, should contain tuples. levels : list of sequences, default None. Specific levels (unique values) to use for constructing a MultiIndex. Otherwise they will be inferred from the keys. names : list, default None. Names for the levels in the resulting hierarchical index. verify_integrity : boolean, default False. Check whether the new concatenated axis contains duplicates. This can be very expensive relative to the actual data concatenation. copy : boolean, default True. If False, do not copy data unnecessarily. Without a little bit of context many of these arguments don’t make much sense. Let’s revisit the above example. Suppose we wanted to associate specific keys with each of the pieces of the chopped up DataFrame. We can do this using the keys argument: In [6]: result = pd.concat(frames, keys=["x", "y", "z"]) As you can see (if you’ve read the rest of the documentation), the resulting object’s index has a hierarchical index. This means that we can now select out each chunk by key: In [7]: result.loc["y"] Out[7]: A B C D 4 A4 B4 C4 D4 5 A5 B5 C5 D5 6 A6 B6 C6 D6 7 A7 B7 C7 D7 It’s not a stretch to see how this can be very useful. More detail on this functionality below. Note It is worth noting that concat() makes a full copy of the data, and that constantly reusing this function can create a significant performance hit. If you need to use the operation over several datasets, use a list comprehension. frames = [ process_your_file(f) for f in files ] result = pd.concat(frames) Note When concatenating DataFrames with named axes, pandas will attempt to preserve these index/column names whenever possible. In the case where all inputs share a common name, this name will be assigned to the result. When the input names do not all agree, the result will be unnamed. The same is true for MultiIndex, but the logic is applied separately on a level-by-level basis. Set logic on the other axes# When gluing together multiple DataFrames, you have a choice of how to handle the other axes (other than the one being concatenated). This can be done in the following two ways: Take the union of them all, join='outer'. This is the default option as it results in zero information loss. Take the intersection, join='inner'. Here is an example of each of these methods. First, the default join='outer' behavior: In [8]: df4 = pd.DataFrame( ...: { ...: "B": ["B2", "B3", "B6", "B7"], ...: "D": ["D2", "D3", "D6", "D7"], ...: "F": ["F2", "F3", "F6", "F7"], ...: }, ...: index=[2, 3, 6, 7], ...: ) ...: In [9]: result = pd.concat([df1, df4], axis=1) Here is the same thing with join='inner': In [10]: result = pd.concat([df1, df4], axis=1, join="inner") Lastly, suppose we just wanted to reuse the exact index from the original DataFrame: In [11]: result = pd.concat([df1, df4], axis=1).reindex(df1.index) Similarly, we could index before the concatenation: In [12]: pd.concat([df1, df4.reindex(df1.index)], axis=1) Out[12]: A B C D B D F 0 A0 B0 C0 D0 NaN NaN NaN 1 A1 B1 C1 D1 NaN NaN NaN 2 A2 B2 C2 D2 B2 D2 F2 3 A3 B3 C3 D3 B3 D3 F3 Ignoring indexes on the concatenation axis# For DataFrame objects which don’t have a meaningful index, you may wish to append them and ignore the fact that they may have overlapping indexes. To do this, use the ignore_index argument: In [13]: result = pd.concat([df1, df4], ignore_index=True, sort=False) Concatenating with mixed ndims# You can concatenate a mix of Series and DataFrame objects. The Series will be transformed to DataFrame with the column name as the name of the Series. In [14]: s1 = pd.Series(["X0", "X1", "X2", "X3"], name="X") In [15]: result = pd.concat([df1, s1], axis=1) Note Since we’re concatenating a Series to a DataFrame, we could have achieved the same result with DataFrame.assign(). To concatenate an arbitrary number of pandas objects (DataFrame or Series), use concat. If unnamed Series are passed they will be numbered consecutively. In [16]: s2 = pd.Series(["_0", "_1", "_2", "_3"]) In [17]: result = pd.concat([df1, s2, s2, s2], axis=1) Passing ignore_index=True will drop all name references. In [18]: result = pd.concat([df1, s1], axis=1, ignore_index=True) More concatenating with group keys# A fairly common use of the keys argument is to override the column names when creating a new DataFrame based on existing Series. Notice how the default behaviour consists on letting the resulting DataFrame inherit the parent Series’ name, when these existed. In [19]: s3 = pd.Series([0, 1, 2, 3], name="foo") In [20]: s4 = pd.Series([0, 1, 2, 3]) In [21]: s5 = pd.Series([0, 1, 4, 5]) In [22]: pd.concat([s3, s4, s5], axis=1) Out[22]: foo 0 1 0 0 0 0 1 1 1 1 2 2 2 4 3 3 3 5 Through the keys argument we can override the existing column names. In [23]: pd.concat([s3, s4, s5], axis=1, keys=["red", "blue", "yellow"]) Out[23]: red blue yellow 0 0 0 0 1 1 1 1 2 2 2 4 3 3 3 5 Let’s consider a variation of the very first example presented: In [24]: result = pd.concat(frames, keys=["x", "y", "z"]) You can also pass a dict to concat in which case the dict keys will be used for the keys argument (unless other keys are specified): In [25]: pieces = {"x": df1, "y": df2, "z": df3} In [26]: result = pd.concat(pieces) In [27]: result = pd.concat(pieces, keys=["z", "y"]) The MultiIndex created has levels that are constructed from the passed keys and the index of the DataFrame pieces: In [28]: result.index.levels Out[28]: FrozenList([['z', 'y'], [4, 5, 6, 7, 8, 9, 10, 11]]) If you wish to specify other levels (as will occasionally be the case), you can do so using the levels argument: In [29]: result = pd.concat( ....: pieces, keys=["x", "y", "z"], levels=[["z", "y", "x", "w"]], names=["group_key"] ....: ) ....: In [30]: result.index.levels Out[30]: FrozenList([['z', 'y', 'x', 'w'], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]) This is fairly esoteric, but it is actually necessary for implementing things like GroupBy where the order of a categorical variable is meaningful. Appending rows to a DataFrame# If you have a series that you want to append as a single row to a DataFrame, you can convert the row into a DataFrame and use concat In [31]: s2 = pd.Series(["X0", "X1", "X2", "X3"], index=["A", "B", "C", "D"]) In [32]: result = pd.concat([df1, s2.to_frame().T], ignore_index=True) You should use ignore_index with this method to instruct DataFrame to discard its index. If you wish to preserve the index, you should construct an appropriately-indexed DataFrame and append or concatenate those objects. Database-style DataFrame or named Series joining/merging# pandas has full-featured, high performance in-memory join operations idiomatically very similar to relational databases like SQL. These methods perform significantly better (in some cases well over an order of magnitude better) than other open source implementations (like base::merge.data.frame in R). The reason for this is careful algorithmic design and the internal layout of the data in DataFrame. See the cookbook for some advanced strategies. Users who are familiar with SQL but new to pandas might be interested in a comparison with SQL. pandas provides a single function, merge(), as the entry point for all standard database join operations between DataFrame or named Series objects: pd.merge( left, right, how="inner", on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=True, suffixes=("_x", "_y"), copy=True, indicator=False, validate=None, ) left: A DataFrame or named Series object. right: Another DataFrame or named Series object. on: Column or index level names to join on. Must be found in both the left and right DataFrame and/or Series objects. If not passed and left_index and right_index are False, the intersection of the columns in the DataFrames and/or Series will be inferred to be the join keys. left_on: Columns or index levels from the left DataFrame or Series to use as keys. Can either be column names, index level names, or arrays with length equal to the length of the DataFrame or Series. right_on: Columns or index levels from the right DataFrame or Series to use as keys. Can either be column names, index level names, or arrays with length equal to the length of the DataFrame or Series. left_index: If True, use the index (row labels) from the left DataFrame or Series as its join key(s). In the case of a DataFrame or Series with a MultiIndex (hierarchical), the number of levels must match the number of join keys from the right DataFrame or Series. right_index: Same usage as left_index for the right DataFrame or Series how: One of 'left', 'right', 'outer', 'inner', 'cross'. Defaults to inner. See below for more detailed description of each method. sort: Sort the result DataFrame by the join keys in lexicographical order. Defaults to True, setting to False will improve performance substantially in many cases. suffixes: A tuple of string suffixes to apply to overlapping columns. Defaults to ('_x', '_y'). copy: Always copy data (default True) from the passed DataFrame or named Series objects, even when reindexing is not necessary. Cannot be avoided in many cases but may improve performance / memory usage. The cases where copying can be avoided are somewhat pathological but this option is provided nonetheless. indicator: Add a column to the output DataFrame called _merge with information on the source of each row. _merge is Categorical-type and takes on a value of left_only for observations whose merge key only appears in 'left' DataFrame or Series, right_only for observations whose merge key only appears in 'right' DataFrame or Series, and both if the observation’s merge key is found in both. validate : string, default None. If specified, checks if merge is of specified type. “one_to_one” or “1:1”: checks if merge keys are unique in both left and right datasets. “one_to_many” or “1:m”: checks if merge keys are unique in left dataset. “many_to_one” or “m:1”: checks if merge keys are unique in right dataset. “many_to_many” or “m:m”: allowed, but does not result in checks. Note Support for specifying index levels as the on, left_on, and right_on parameters was added in version 0.23.0. Support for merging named Series objects was added in version 0.24.0. The return type will be the same as left. If left is a DataFrame or named Series and right is a subclass of DataFrame, the return type will still be DataFrame. merge is a function in the pandas namespace, and it is also available as a DataFrame instance method merge(), with the calling DataFrame being implicitly considered the left object in the join. The related join() method, uses merge internally for the index-on-index (by default) and column(s)-on-index join. If you are joining on index only, you may wish to use DataFrame.join to save yourself some typing. Brief primer on merge methods (relational algebra)# Experienced users of relational databases like SQL will be familiar with the terminology used to describe join operations between two SQL-table like structures (DataFrame objects). There are several cases to consider which are very important to understand: one-to-one joins: for example when joining two DataFrame objects on their indexes (which must contain unique values). many-to-one joins: for example when joining an index (unique) to one or more columns in a different DataFrame. many-to-many joins: joining columns on columns. Note When joining columns on columns (potentially a many-to-many join), any indexes on the passed DataFrame objects will be discarded. It is worth spending some time understanding the result of the many-to-many join case. In SQL / standard relational algebra, if a key combination appears more than once in both tables, the resulting table will have the Cartesian product of the associated data. Here is a very basic example with one unique key combination: In [33]: left = pd.DataFrame( ....: { ....: "key": ["K0", "K1", "K2", "K3"], ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: } ....: ) ....: In [34]: right = pd.DataFrame( ....: { ....: "key": ["K0", "K1", "K2", "K3"], ....: "C": ["C0", "C1", "C2", "C3"], ....: "D": ["D0", "D1", "D2", "D3"], ....: } ....: ) ....: In [35]: result = pd.merge(left, right, on="key") Here is a more complicated example with multiple join keys. Only the keys appearing in left and right are present (the intersection), since how='inner' by default. In [36]: left = pd.DataFrame( ....: { ....: "key1": ["K0", "K0", "K1", "K2"], ....: "key2": ["K0", "K1", "K0", "K1"], ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: } ....: ) ....: In [37]: right = pd.DataFrame( ....: { ....: "key1": ["K0", "K1", "K1", "K2"], ....: "key2": ["K0", "K0", "K0", "K0"], ....: "C": ["C0", "C1", "C2", "C3"], ....: "D": ["D0", "D1", "D2", "D3"], ....: } ....: ) ....: In [38]: result = pd.merge(left, right, on=["key1", "key2"]) The how argument to merge specifies how to determine which keys are to be included in the resulting table. If a key combination does not appear in either the left or right tables, the values in the joined table will be NA. Here is a summary of the how options and their SQL equivalent names: Merge method SQL Join Name Description left LEFT OUTER JOIN Use keys from left frame only right RIGHT OUTER JOIN Use keys from right frame only outer FULL OUTER JOIN Use union of keys from both frames inner INNER JOIN Use intersection of keys from both frames cross CROSS JOIN Create the cartesian product of rows of both frames In [39]: result = pd.merge(left, right, how="left", on=["key1", "key2"]) In [40]: result = pd.merge(left, right, how="right", on=["key1", "key2"]) In [41]: result = pd.merge(left, right, how="outer", on=["key1", "key2"]) In [42]: result = pd.merge(left, right, how="inner", on=["key1", "key2"]) In [43]: result = pd.merge(left, right, how="cross") You can merge a mult-indexed Series and a DataFrame, if the names of the MultiIndex correspond to the columns from the DataFrame. Transform the Series to a DataFrame using Series.reset_index() before merging, as shown in the following example. In [44]: df = pd.DataFrame({"Let": ["A", "B", "C"], "Num": [1, 2, 3]}) In [45]: df Out[45]: Let Num 0 A 1 1 B 2 2 C 3 In [46]: ser = pd.Series( ....: ["a", "b", "c", "d", "e", "f"], ....: index=pd.MultiIndex.from_arrays( ....: [["A", "B", "C"] * 2, [1, 2, 3, 4, 5, 6]], names=["Let", "Num"] ....: ), ....: ) ....: In [47]: ser Out[47]: Let Num A 1 a B 2 b C 3 c A 4 d B 5 e C 6 f dtype: object In [48]: pd.merge(df, ser.reset_index(), on=["Let", "Num"]) Out[48]: Let Num 0 0 A 1 a 1 B 2 b 2 C 3 c Here is another example with duplicate join keys in DataFrames: In [49]: left = pd.DataFrame({"A": [1, 2], "B": [2, 2]}) In [50]: right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]}) In [51]: result = pd.merge(left, right, on="B", how="outer") Warning Joining / merging on duplicate keys can cause a returned frame that is the multiplication of the row dimensions, which may result in memory overflow. It is the user’ s responsibility to manage duplicate values in keys before joining large DataFrames. Checking for duplicate keys# Users can use the validate argument to automatically check whether there are unexpected duplicates in their merge keys. Key uniqueness is checked before merge operations and so should protect against memory overflows. Checking key uniqueness is also a good way to ensure user data structures are as expected. In the following example, there are duplicate values of B in the right DataFrame. As this is not a one-to-one merge – as specified in the validate argument – an exception will be raised. In [52]: left = pd.DataFrame({"A": [1, 2], "B": [1, 2]}) In [53]: right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]}) In [53]: result = pd.merge(left, right, on="B", how="outer", validate="one_to_one") ... MergeError: Merge keys are not unique in right dataset; not a one-to-one merge If the user is aware of the duplicates in the right DataFrame but wants to ensure there are no duplicates in the left DataFrame, one can use the validate='one_to_many' argument instead, which will not raise an exception. In [54]: pd.merge(left, right, on="B", how="outer", validate="one_to_many") Out[54]: A_x B A_y 0 1 1 NaN 1 2 2 4.0 2 2 2 5.0 3 2 2 6.0 The merge indicator# merge() accepts the argument indicator. If True, a Categorical-type column called _merge will be added to the output object that takes on values: Observation Origin _merge value Merge key only in 'left' frame left_only Merge key only in 'right' frame right_only Merge key in both frames both In [55]: df1 = pd.DataFrame({"col1": [0, 1], "col_left": ["a", "b"]}) In [56]: df2 = pd.DataFrame({"col1": [1, 2, 2], "col_right": [2, 2, 2]}) In [57]: pd.merge(df1, df2, on="col1", how="outer", indicator=True) Out[57]: col1 col_left col_right _merge 0 0 a NaN left_only 1 1 b 2.0 both 2 2 NaN 2.0 right_only 3 2 NaN 2.0 right_only The indicator argument will also accept string arguments, in which case the indicator function will use the value of the passed string as the name for the indicator column. In [58]: pd.merge(df1, df2, on="col1", how="outer", indicator="indicator_column") Out[58]: col1 col_left col_right indicator_column 0 0 a NaN left_only 1 1 b 2.0 both 2 2 NaN 2.0 right_only 3 2 NaN 2.0 right_only Merge dtypes# Merging will preserve the dtype of the join keys. In [59]: left = pd.DataFrame({"key": [1], "v1": [10]}) In [60]: left Out[60]: key v1 0 1 10 In [61]: right = pd.DataFrame({"key": [1, 2], "v1": [20, 30]}) In [62]: right Out[62]: key v1 0 1 20 1 2 30 We are able to preserve the join keys: In [63]: pd.merge(left, right, how="outer") Out[63]: key v1 0 1 10 1 1 20 2 2 30 In [64]: pd.merge(left, right, how="outer").dtypes Out[64]: key int64 v1 int64 dtype: object Of course if you have missing values that are introduced, then the resulting dtype will be upcast. In [65]: pd.merge(left, right, how="outer", on="key") Out[65]: key v1_x v1_y 0 1 10.0 20 1 2 NaN 30 In [66]: pd.merge(left, right, how="outer", on="key").dtypes Out[66]: key int64 v1_x float64 v1_y int64 dtype: object Merging will preserve category dtypes of the mergands. See also the section on categoricals. The left frame. In [67]: from pandas.api.types import CategoricalDtype In [68]: X = pd.Series(np.random.choice(["foo", "bar"], size=(10,))) In [69]: X = X.astype(CategoricalDtype(categories=["foo", "bar"])) In [70]: left = pd.DataFrame( ....: {"X": X, "Y": np.random.choice(["one", "two", "three"], size=(10,))} ....: ) ....: In [71]: left Out[71]: X Y 0 bar one 1 foo one 2 foo three 3 bar three 4 foo one 5 bar one 6 bar three 7 bar three 8 bar three 9 foo three In [72]: left.dtypes Out[72]: X category Y object dtype: object The right frame. In [73]: right = pd.DataFrame( ....: { ....: "X": pd.Series(["foo", "bar"], dtype=CategoricalDtype(["foo", "bar"])), ....: "Z": [1, 2], ....: } ....: ) ....: In [74]: right Out[74]: X Z 0 foo 1 1 bar 2 In [75]: right.dtypes Out[75]: X category Z int64 dtype: object The merged result: In [76]: result = pd.merge(left, right, how="outer") In [77]: result Out[77]: X Y Z 0 bar one 2 1 bar three 2 2 bar one 2 3 bar three 2 4 bar three 2 5 bar three 2 6 foo one 1 7 foo three 1 8 foo one 1 9 foo three 1 In [78]: result.dtypes Out[78]: X category Y object Z int64 dtype: object Note The category dtypes must be exactly the same, meaning the same categories and the ordered attribute. Otherwise the result will coerce to the categories’ dtype. Note Merging on category dtypes that are the same can be quite performant compared to object dtype merging. Joining on index# DataFrame.join() is a convenient method for combining the columns of two potentially differently-indexed DataFrames into a single result DataFrame. Here is a very basic example: In [79]: left = pd.DataFrame( ....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=["K0", "K1", "K2"] ....: ) ....: In [80]: right = pd.DataFrame( ....: {"C": ["C0", "C2", "C3"], "D": ["D0", "D2", "D3"]}, index=["K0", "K2", "K3"] ....: ) ....: In [81]: result = left.join(right) In [82]: result = left.join(right, how="outer") The same as above, but with how='inner'. In [83]: result = left.join(right, how="inner") The data alignment here is on the indexes (row labels). This same behavior can be achieved using merge plus additional arguments instructing it to use the indexes: In [84]: result = pd.merge(left, right, left_index=True, right_index=True, how="outer") In [85]: result = pd.merge(left, right, left_index=True, right_index=True, how="inner") Joining key columns on an index# join() takes an optional on argument which may be a column or multiple column names, which specifies that the passed DataFrame is to be aligned on that column in the DataFrame. These two function calls are completely equivalent: left.join(right, on=key_or_keys) pd.merge( left, right, left_on=key_or_keys, right_index=True, how="left", sort=False ) Obviously you can choose whichever form you find more convenient. For many-to-one joins (where one of the DataFrame’s is already indexed by the join key), using join may be more convenient. Here is a simple example: In [86]: left = pd.DataFrame( ....: { ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: "key": ["K0", "K1", "K0", "K1"], ....: } ....: ) ....: In [87]: right = pd.DataFrame({"C": ["C0", "C1"], "D": ["D0", "D1"]}, index=["K0", "K1"]) In [88]: result = left.join(right, on="key") In [89]: result = pd.merge( ....: left, right, left_on="key", right_index=True, how="left", sort=False ....: ) ....: To join on multiple keys, the passed DataFrame must have a MultiIndex: In [90]: left = pd.DataFrame( ....: { ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: "key1": ["K0", "K0", "K1", "K2"], ....: "key2": ["K0", "K1", "K0", "K1"], ....: } ....: ) ....: In [91]: index = pd.MultiIndex.from_tuples( ....: [("K0", "K0"), ("K1", "K0"), ("K2", "K0"), ("K2", "K1")] ....: ) ....: In [92]: right = pd.DataFrame( ....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=index ....: ) ....: Now this can be joined by passing the two key column names: In [93]: result = left.join(right, on=["key1", "key2"]) The default for DataFrame.join is to perform a left join (essentially a “VLOOKUP” operation, for Excel users), which uses only the keys found in the calling DataFrame. Other join types, for example inner join, can be just as easily performed: In [94]: result = left.join(right, on=["key1", "key2"], how="inner") As you can see, this drops any rows where there was no match. Joining a single Index to a MultiIndex# You can join a singly-indexed DataFrame with a level of a MultiIndexed DataFrame. The level will match on the name of the index of the singly-indexed frame against a level name of the MultiIndexed frame. In [95]: left = pd.DataFrame( ....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, ....: index=pd.Index(["K0", "K1", "K2"], name="key"), ....: ) ....: In [96]: index = pd.MultiIndex.from_tuples( ....: [("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")], ....: names=["key", "Y"], ....: ) ....: In [97]: right = pd.DataFrame( ....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, ....: index=index, ....: ) ....: In [98]: result = left.join(right, how="inner") This is equivalent but less verbose and more memory efficient / faster than this. In [99]: result = pd.merge( ....: left.reset_index(), right.reset_index(), on=["key"], how="inner" ....: ).set_index(["key","Y"]) ....: Joining with two MultiIndexes# This is supported in a limited way, provided that the index for the right argument is completely used in the join, and is a subset of the indices in the left argument, as in this example: In [100]: leftindex = pd.MultiIndex.from_product( .....: [list("abc"), list("xy"), [1, 2]], names=["abc", "xy", "num"] .....: ) .....: In [101]: left = pd.DataFrame({"v1": range(12)}, index=leftindex) In [102]: left Out[102]: v1 abc xy num a x 1 0 2 1 y 1 2 2 3 b x 1 4 2 5 y 1 6 2 7 c x 1 8 2 9 y 1 10 2 11 In [103]: rightindex = pd.MultiIndex.from_product( .....: [list("abc"), list("xy")], names=["abc", "xy"] .....: ) .....: In [104]: right = pd.DataFrame({"v2": [100 * i for i in range(1, 7)]}, index=rightindex) In [105]: right Out[105]: v2 abc xy a x 100 y 200 b x 300 y 400 c x 500 y 600 In [106]: left.join(right, on=["abc", "xy"], how="inner") Out[106]: v1 v2 abc xy num a x 1 0 100 2 1 100 y 1 2 200 2 3 200 b x 1 4 300 2 5 300 y 1 6 400 2 7 400 c x 1 8 500 2 9 500 y 1 10 600 2 11 600 If that condition is not satisfied, a join with two multi-indexes can be done using the following code. In [107]: leftindex = pd.MultiIndex.from_tuples( .....: [("K0", "X0"), ("K0", "X1"), ("K1", "X2")], names=["key", "X"] .....: ) .....: In [108]: left = pd.DataFrame( .....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=leftindex .....: ) .....: In [109]: rightindex = pd.MultiIndex.from_tuples( .....: [("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")], names=["key", "Y"] .....: ) .....: In [110]: right = pd.DataFrame( .....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=rightindex .....: ) .....: In [111]: result = pd.merge( .....: left.reset_index(), right.reset_index(), on=["key"], how="inner" .....: ).set_index(["key", "X", "Y"]) .....: Merging on a combination of columns and index levels# Strings passed as the on, left_on, and right_on parameters may refer to either column names or index level names. This enables merging DataFrame instances on a combination of index levels and columns without resetting indexes. In [112]: left_index = pd.Index(["K0", "K0", "K1", "K2"], name="key1") In [113]: left = pd.DataFrame( .....: { .....: "A": ["A0", "A1", "A2", "A3"], .....: "B": ["B0", "B1", "B2", "B3"], .....: "key2": ["K0", "K1", "K0", "K1"], .....: }, .....: index=left_index, .....: ) .....: In [114]: right_index = pd.Index(["K0", "K1", "K2", "K2"], name="key1") In [115]: right = pd.DataFrame( .....: { .....: "C": ["C0", "C1", "C2", "C3"], .....: "D": ["D0", "D1", "D2", "D3"], .....: "key2": ["K0", "K0", "K0", "K1"], .....: }, .....: index=right_index, .....: ) .....: In [116]: result = left.merge(right, on=["key1", "key2"]) Note When DataFrames are merged on a string that matches an index level in both frames, the index level is preserved as an index level in the resulting DataFrame. Note When DataFrames are merged using only some of the levels of a MultiIndex, the extra levels will be dropped from the resulting merge. In order to preserve those levels, use reset_index on those level names to move those levels to columns prior to doing the merge. Note If a string matches both a column name and an index level name, then a warning is issued and the column takes precedence. This will result in an ambiguity error in a future version. Overlapping value columns# The merge suffixes argument takes a tuple of list of strings to append to overlapping column names in the input DataFrames to disambiguate the result columns: In [117]: left = pd.DataFrame({"k": ["K0", "K1", "K2"], "v": [1, 2, 3]}) In [118]: right = pd.DataFrame({"k": ["K0", "K0", "K3"], "v": [4, 5, 6]}) In [119]: result = pd.merge(left, right, on="k") In [120]: result = pd.merge(left, right, on="k", suffixes=("_l", "_r")) DataFrame.join() has lsuffix and rsuffix arguments which behave similarly. In [121]: left = left.set_index("k") In [122]: right = right.set_index("k") In [123]: result = left.join(right, lsuffix="_l", rsuffix="_r") Joining multiple DataFrames# A list or tuple of DataFrames can also be passed to join() to join them together on their indexes. In [124]: right2 = pd.DataFrame({"v": [7, 8, 9]}, index=["K1", "K1", "K2"]) In [125]: result = left.join([right, right2]) Merging together values within Series or DataFrame columns# Another fairly common situation is to have two like-indexed (or similarly indexed) Series or DataFrame objects and wanting to “patch” values in one object from values for matching indices in the other. Here is an example: In [126]: df1 = pd.DataFrame( .....: [[np.nan, 3.0, 5.0], [-4.6, np.nan, np.nan], [np.nan, 7.0, np.nan]] .....: ) .....: In [127]: df2 = pd.DataFrame([[-42.6, np.nan, -8.2], [-5.0, 1.6, 4]], index=[1, 2]) For this, use the combine_first() method: In [128]: result = df1.combine_first(df2) Note that this method only takes values from the right DataFrame if they are missing in the left DataFrame. A related method, update(), alters non-NA values in place: In [129]: df1.update(df2) Timeseries friendly merging# Merging ordered data# A merge_ordered() function allows combining time series and other ordered data. In particular it has an optional fill_method keyword to fill/interpolate missing data: In [130]: left = pd.DataFrame( .....: {"k": ["K0", "K1", "K1", "K2"], "lv": [1, 2, 3, 4], "s": ["a", "b", "c", "d"]} .....: ) .....: In [131]: right = pd.DataFrame({"k": ["K1", "K2", "K4"], "rv": [1, 2, 3]}) In [132]: pd.merge_ordered(left, right, fill_method="ffill", left_by="s") Out[132]: k lv s rv 0 K0 1.0 a NaN 1 K1 1.0 a 1.0 2 K2 1.0 a 2.0 3 K4 1.0 a 3.0 4 K1 2.0 b 1.0 5 K2 2.0 b 2.0 6 K4 2.0 b 3.0 7 K1 3.0 c 1.0 8 K2 3.0 c 2.0 9 K4 3.0 c 3.0 10 K1 NaN d 1.0 11 K2 4.0 d 2.0 12 K4 4.0 d 3.0 Merging asof# A merge_asof() is similar to an ordered left-join except that we match on nearest key rather than equal keys. For each row in the left DataFrame, we select the last row in the right DataFrame whose on key is less than the left’s key. Both DataFrames must be sorted by the key. Optionally an asof merge can perform a group-wise merge. This matches the by key equally, in addition to the nearest match on the on key. For example; we might have trades and quotes and we want to asof merge them. In [133]: trades = pd.DataFrame( .....: { .....: "time": pd.to_datetime( .....: [ .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.038", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.048", .....: ] .....: ), .....: "ticker": ["MSFT", "MSFT", "GOOG", "GOOG", "AAPL"], .....: "price": [51.95, 51.95, 720.77, 720.92, 98.00], .....: "quantity": [75, 155, 100, 100, 100], .....: }, .....: columns=["time", "ticker", "price", "quantity"], .....: ) .....: In [134]: quotes = pd.DataFrame( .....: { .....: "time": pd.to_datetime( .....: [ .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.030", .....: "20160525 13:30:00.041", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.049", .....: "20160525 13:30:00.072", .....: "20160525 13:30:00.075", .....: ] .....: ), .....: "ticker": ["GOOG", "MSFT", "MSFT", "MSFT", "GOOG", "AAPL", "GOOG", "MSFT"], .....: "bid": [720.50, 51.95, 51.97, 51.99, 720.50, 97.99, 720.50, 52.01], .....: "ask": [720.93, 51.96, 51.98, 52.00, 720.93, 98.01, 720.88, 52.03], .....: }, .....: columns=["time", "ticker", "bid", "ask"], .....: ) .....: In [135]: trades Out[135]: time ticker price quantity 0 2016-05-25 13:30:00.023 MSFT 51.95 75 1 2016-05-25 13:30:00.038 MSFT 51.95 155 2 2016-05-25 13:30:00.048 GOOG 720.77 100 3 2016-05-25 13:30:00.048 GOOG 720.92 100 4 2016-05-25 13:30:00.048 AAPL 98.00 100 In [136]: quotes Out[136]: time ticker bid ask 0 2016-05-25 13:30:00.023 GOOG 720.50 720.93 1 2016-05-25 13:30:00.023 MSFT 51.95 51.96 2 2016-05-25 13:30:00.030 MSFT 51.97 51.98 3 2016-05-25 13:30:00.041 MSFT 51.99 52.00 4 2016-05-25 13:30:00.048 GOOG 720.50 720.93 5 2016-05-25 13:30:00.049 AAPL 97.99 98.01 6 2016-05-25 13:30:00.072 GOOG 720.50 720.88 7 2016-05-25 13:30:00.075 MSFT 52.01 52.03 By default we are taking the asof of the quotes. In [137]: pd.merge_asof(trades, quotes, on="time", by="ticker") Out[137]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 51.95 51.96 1 2016-05-25 13:30:00.038 MSFT 51.95 155 51.97 51.98 2 2016-05-25 13:30:00.048 GOOG 720.77 100 720.50 720.93 3 2016-05-25 13:30:00.048 GOOG 720.92 100 720.50 720.93 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN We only asof within 2ms between the quote time and the trade time. In [138]: pd.merge_asof(trades, quotes, on="time", by="ticker", tolerance=pd.Timedelta("2ms")) Out[138]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 51.95 51.96 1 2016-05-25 13:30:00.038 MSFT 51.95 155 NaN NaN 2 2016-05-25 13:30:00.048 GOOG 720.77 100 720.50 720.93 3 2016-05-25 13:30:00.048 GOOG 720.92 100 720.50 720.93 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN We only asof within 10ms between the quote time and the trade time and we exclude exact matches on time. Note that though we exclude the exact matches (of the quotes), prior quotes do propagate to that point in time. In [139]: pd.merge_asof( .....: trades, .....: quotes, .....: on="time", .....: by="ticker", .....: tolerance=pd.Timedelta("10ms"), .....: allow_exact_matches=False, .....: ) .....: Out[139]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 NaN NaN 1 2016-05-25 13:30:00.038 MSFT 51.95 155 51.97 51.98 2 2016-05-25 13:30:00.048 GOOG 720.77 100 NaN NaN 3 2016-05-25 13:30:00.048 GOOG 720.92 100 NaN NaN 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN Comparing objects# The compare() and compare() methods allow you to compare two DataFrame or Series, respectively, and summarize their differences. This feature was added in V1.1.0. For example, you might want to compare two DataFrame and stack their differences side by side. In [140]: df = pd.DataFrame( .....: { .....: "col1": ["a", "a", "b", "b", "a"], .....: "col2": [1.0, 2.0, 3.0, np.nan, 5.0], .....: "col3": [1.0, 2.0, 3.0, 4.0, 5.0], .....: }, .....: columns=["col1", "col2", "col3"], .....: ) .....: In [141]: df Out[141]: col1 col2 col3 0 a 1.0 1.0 1 a 2.0 2.0 2 b 3.0 3.0 3 b NaN 4.0 4 a 5.0 5.0 In [142]: df2 = df.copy() In [143]: df2.loc[0, "col1"] = "c" In [144]: df2.loc[2, "col3"] = 4.0 In [145]: df2 Out[145]: col1 col2 col3 0 c 1.0 1.0 1 a 2.0 2.0 2 b 3.0 4.0 3 b NaN 4.0 4 a 5.0 5.0 In [146]: df.compare(df2) Out[146]: col1 col3 self other self other 0 a c NaN NaN 2 NaN NaN 3.0 4.0 By default, if two corresponding values are equal, they will be shown as NaN. Furthermore, if all values in an entire row / column, the row / column will be omitted from the result. The remaining differences will be aligned on columns. If you wish, you may choose to stack the differences on rows. In [147]: df.compare(df2, align_axis=0) Out[147]: col1 col3 0 self a NaN other c NaN 2 self NaN 3.0 other NaN 4.0 If you wish to keep all original rows and columns, set keep_shape argument to True. In [148]: df.compare(df2, keep_shape=True) Out[148]: col1 col2 col3 self other self other self other 0 a c NaN NaN NaN NaN 1 NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN 3.0 4.0 3 NaN NaN NaN NaN NaN NaN 4 NaN NaN NaN NaN NaN NaN You may also keep all the original values even if they are equal. In [149]: df.compare(df2, keep_shape=True, keep_equal=True) Out[149]: col1 col2 col3 self other self other self other 0 a c 1.0 1.0 1.0 1.0 1 a a 2.0 2.0 2.0 2.0 2 b b 3.0 3.0 3.0 4.0 3 b b NaN NaN 4.0 4.0 4 a a 5.0 5.0 5.0 5.0
153
939
Combine dataframe with column value matching How can I do for below expected result with python code? [DF1] [DF2] **Name** **Configure** **Name** MD0001 2G MD0001 MD0002 3G MD0001 MD0003 4G MD0001 MD0002 MD0002 MD0003 MD0003 MD0003 Expected result: Name Configure MD0001 2G MD0001 2G MD0001 2G MD0002 3G MD0002 3G MD0003 4G MD0003 4G MD0003 4G Here is my code so far: data_xlsx = pd.read_excel(data,skiprows=1,sheet_name='KPI') new_xlsx = pd.read_excel(new, skiprows=1,sheet_name='KPI') data_df = pd.Dataframe[data_xlsx ] new_xlsx = pd.Dataframe[new_xlsx ]
[CLS] Python pre xlsx code [SEP] " ] ) ),.... : " Z " : [ 1, 2 ],.... : }.... : ).... : In [ 74 ] : right Out [ 74 ] : X Z 0 foo 1 1 bar 2 /
You should use merge or join. This is pandas' way of accomplishing SQL-like joins. # mock up the test dataframes df1 = pd.DataFrame.from_records( columns=["Name", "Configure"], data = [ ("MD0001", "2G"), ("MD0002", "3G"), ("MD0003", "4G") ] ) df2 = pd.DataFrame.from_records( columns = ["Name"], data = [ ("MD0001",), ("MD0001",), ("MD0001",), ("MD0002",), ("MD0002",), ("MD0003",), ("MD0003",), ("MD0003",) ] ) # index df1 on Name df1 = df1.set_index("Name") # then join it to df2 df2.join(df1['Configure'], on="Name") Output: Name Configure 0 MD0001 2G 1 MD0001 2G 2 MD0001 2G 3 MD0002 3G 4 MD0002 3G 5 MD0003 4G 6 MD0003 4G 7 MD0003 4G
61,981,842
Python pandas, what should I pay attention to when using groupby()
<p>I got an .tsv file <a href="https://i.stack.imgur.com/Yif3g.png" rel="nofollow noreferrer">imdb_actor_edges.tsv</a>, which includes 287074 rows in total. I wanna figure out how many times each actor shows up in the file, so here's my code(using python pandas):</p> <pre><code>import pandas as pd edges = pd.read_csv('imdb_actor_edges.tsv', sep='\t') edges.loc[:,'edges'] = 1; df1 = edges[['actor1','edges']] df2 = edges[['actor2','edges']] df1.columns = ['actor','edges'] df2.columns = ['actor','edges'] edges_sorted = pd.concat([df1,df2],join='inner',ignore_index=True) edges_sorted.reset_index(inplace=True,drop=True) edges_sorted.groupby('actor')['edges'].sum() </code></pre> <p>The problem is that edges_sorted is supposed to be less than 287074*2 rows('cause there're actors who shows more than one time) but actually not. It remains 287074*2 rows and the 'edges' is still 1.</p> <p>Did I do anything wrong? How can I solve the problem?</p>
61,981,950
"2020-05-24T05:00:30.717000"
1
null
0
47
python|pandas
<p>Please try this:-</p> <pre><code>import pandas as pd edges = pd.read_csv('imdb_actor_edges.tsv', sep='\t') edges.loc[:,'edges'] = 1; df1 = edges[['actor1','edges']] df2 = edges[['actor2','edges']] df1.columns = ['actor','edges'] df2.columns = ['actor','edges'] edges_sorted = pd.concat([df1,df2],join='inner',ignore_index=True) edges_sorted.reset_index(inplace=True,drop=True) result = edges_sorted.groupby('actor').agg(sum) result.reset_index(inplace=True) result </code></pre> <p>Hope this helps!!</p>
"2020-05-24T05:18:47.830000"
0
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sort_index.html
pandas.DataFrame.sort_index# pandas.DataFrame.sort_index# DataFrame.sort_index(*, axis=0, level=None, ascending=True, inplace=False, kind='quicksort', na_position='last', sort_remaining=True, ignore_index=False, key=None)[source]# Sort object by labels (along an axis). Returns a new DataFrame sorted by label if inplace argument is False, otherwise updates the original DataFrame and returns None. Please try this:- import pandas as pd edges = pd.read_csv('imdb_actor_edges.tsv', sep='\t') edges.loc[:,'edges'] = 1; df1 = edges[['actor1','edges']] df2 = edges[['actor2','edges']] df1.columns = ['actor','edges'] df2.columns = ['actor','edges'] edges_sorted = pd.concat([df1,df2],join='inner',ignore_index=True) edges_sorted.reset_index(inplace=True,drop=True) result = edges_sorted.groupby('actor').agg(sum) result.reset_index(inplace=True) result Hope this helps!! Parameters axis{0 or ‘index’, 1 or ‘columns’}, default 0The axis along which to sort. The value 0 identifies the rows, and 1 identifies the columns. levelint or level name or list of ints or list of level namesIf not None, sort on values in specified index level(s). ascendingbool or list-like of bools, default TrueSort ascending vs. descending. When the index is a MultiIndex the sort direction can be controlled for each level individually. inplacebool, default FalseWhether to modify the DataFrame rather than creating a new one. kind{‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}, default ‘quicksort’Choice of sorting algorithm. See also numpy.sort() for more information. mergesort and stable are the only stable algorithms. For DataFrames, this option is only applied when sorting on a single column or label. na_position{‘first’, ‘last’}, default ‘last’Puts NaNs at the beginning if first; last puts NaNs at the end. Not implemented for MultiIndex. sort_remainingbool, default TrueIf True and sorting by level and index is multilevel, sort by other levels too (in order) after sorting by specified level. ignore_indexbool, default FalseIf True, the resulting axis will be labeled 0, 1, …, n - 1. New in version 1.0.0. keycallable, optionalIf not None, apply the key function to the index values before sorting. This is similar to the key argument in the builtin sorted() function, with the notable difference that this key function should be vectorized. It should expect an Index and return an Index of the same shape. For MultiIndex inputs, the key is applied per level. New in version 1.1.0. Returns DataFrame or NoneThe original DataFrame sorted by the labels or None if inplace=True. See also Series.sort_indexSort Series by the index. DataFrame.sort_valuesSort DataFrame by the value. Series.sort_valuesSort Series by the value. Examples >>> df = pd.DataFrame([1, 2, 3, 4, 5], index=[100, 29, 234, 1, 150], ... columns=['A']) >>> df.sort_index() A 1 4 29 2 100 1 150 5 234 3 By default, it sorts in ascending order, to sort in descending order, use ascending=False >>> df.sort_index(ascending=False) A 234 3 150 5 100 1 29 2 1 4 A key function can be specified which is applied to the index before sorting. For a MultiIndex this is applied to each level separately. >>> df = pd.DataFrame({"a": [1, 2, 3, 4]}, index=['A', 'b', 'C', 'd']) >>> df.sort_index(key=lambda x: x.str.lower()) a A 1 b 2 C 3 d 4
403
879
Python pandas, what should I pay attention to when using groupby() I got an .tsv file imdb_actor_edges.tsv, which includes 287074 rows in total. I wanna figure out how many times each actor shows up in the file, so here's my code(using python pandas): import pandas as pd edges = pd.read_csv('imdb_actor_edges.tsv', sep='\t') edges.loc[:,'edges'] = 1; df1 = edges[['actor1','edges']] df2 = edges[['actor2','edges']] df1.columns = ['actor','edges'] df2.columns = ['actor','edges'] edges_sorted = pd.concat([df1,df2],join='inner',ignore_index=True) edges_sorted.reset_index(inplace=True,drop=True) edges_sorted.groupby('actor')['edges'].sum() The problem is that edges_sorted is supposed to be less than 287074*2 rows('cause there're actors who shows more than one time) but actually not. It remains 287074*2 rows and the 'edges' is still 1. Did I do anything wrong? How can I solve the problem?
Please try this:- import pandas as pd edges = pd.read_csv('imdb_actor_edges.tsv', sep='\t') edges.loc[:,'edges'] = 1; df1 = edges[['actor1','edges']] df2 = edges[['actor2','edges']] df1.columns = ['actor','edges'] df2.columns = ['actor','edges'] edges_sorted = pd.concat([df1,df2],join='inner',ignore_index=True) edges_sorted.reset_index(inplace=True,drop=True) result = edges_sorted.groupby('actor').agg(sum) result.reset_index(inplace=True) result Hope this helps!!
66,685,034
Pandas groupby split by NaN
<p>I have one question, I ask you guys</p> <p>I want to split the df like grp1, grp2, grp3, grp4</p> <p>The data below is a sample and I really need to process a large amount of data, so please let me know how to do it appropriately.</p> <pre><code>df = A1 B1 2021-03-18 00:00:00 10 23 2021-03-18 00:00:01 11 28 2021-03-18 00:00:02 12 29 2021-03-18 00:00:03 NaN NaN 2021-03-18 00:00:04 NaN NaN 2021-03-18 00:00:05 42 32 2021-03-18 00:00:06 51 90 2021-03-18 00:00:07 NaN NaN 2021-03-18 00:00:08 90 101 2021-03-18 00:00:09 32 42 2021-03-18 00:00:10 NaN NaN 2021-03-18 00:00:11 575 333 2021-03-18 00:00:12 62 421 </code></pre> <pre><code>grp1 = A1 B1 2021-03-18 00:00:00 10 23 2021-03-18 00:00:01 11 28 2021-03-18 00:00:02 12 29 </code></pre> <pre><code>grp2 = A1 B1 2021-03-18 00:00:05 42 32 2021-03-18 00:00:06 51 90 </code></pre> <pre><code>grp3 = A1 B1 2021-03-18 00:00:08 90 101 2021-03-18 00:00:09 32 42 </code></pre> <pre><code>grp4 = A1 B1 2021-03-18 00:00:11 575 333 2021-03-18 00:00:12 62 421 </code></pre>
66,685,149
"2021-03-18T04:59:57.167000"
1
null
0
71
python|pandas
<p>Try:</p> <pre><code>x = (df.shift(1).isnull() &amp; df.notnull()).cumsum()['B1'] df_list = [] for i,g in df.groupby(x): print(i,g.dropna()) df_list.append(g.dropna()) </code></pre> <hr /> <pre><code>1 A1 B1 2021-03-18 00:00:00 10.0 23.0 00:00:01 11.0 28.0 00:00:02 12.0 29.0 2 A1 B1 2021-03-18 00:00:05 42.0 32.0 00:00:06 51.0 90.0 3 A1 B1 2021-03-18 00:00:08 90.0 101.0 00:00:09 32.0 42.0 4 A1 B1 2021-03-18 00:00:11 575.0 333.0 00:00:12 62.0 421.0 </code></pre> <p><code>df_list</code> will have all your grouped dataframe.</p>
"2021-03-18T05:12:23.293000"
0
https://pandas.pydata.org/docs/reference/api/pandas.Series.str.split.html
pandas.Series.str.split# pandas.Series.str.split# Series.str.split(pat=None, *, n=- 1, expand=False, regex=None)[source]# Split strings around given separator/delimiter. Splits the string in the Series/Index from the beginning, at the specified delimiter string. Parameters patstr or compiled regex, optionalString or regular expression to split on. If not specified, split on whitespace. nint, default -1 (all)Limit number of splits in output. Try: x = (df.shift(1).isnull() & df.notnull()).cumsum()['B1'] df_list = [] for i,g in df.groupby(x): print(i,g.dropna()) df_list.append(g.dropna()) 1 A1 B1 2021-03-18 00:00:00 10.0 23.0 00:00:01 11.0 28.0 00:00:02 12.0 29.0 2 A1 B1 2021-03-18 00:00:05 42.0 32.0 00:00:06 51.0 90.0 3 A1 B1 2021-03-18 00:00:08 90.0 101.0 00:00:09 32.0 42.0 4 A1 B1 2021-03-18 00:00:11 575.0 333.0 00:00:12 62.0 421.0 df_list will have all your grouped dataframe. None, 0 and -1 will be interpreted as return all splits. expandbool, default FalseExpand the split strings into separate columns. If True, return DataFrame/MultiIndex expanding dimensionality. If False, return Series/Index, containing lists of strings. regexbool, default NoneDetermines 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 New in version 1.4.0. Returns Series, Index, DataFrame or MultiIndexType matches caller unless expand=True (see Notes). Raises ValueError if regex is False and pat is a compiled regex See also Series.str.splitSplit strings around given separator/delimiter. Series.str.rsplitSplits string around given separator/delimiter, starting from the right. Series.str.joinJoin lists contained as elements in the Series/Index with passed delimiter. str.splitStandard library version for split. str.rsplitStandard library version for rsplit. Notes The handling of the n keyword depends on the number of found splits: If found splits > n, make first n splits only If found splits <= n, make all splits If for a certain row the number of found splits < n, append None for padding up to n if expand=True 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. Examples >>> 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
452
1,089
Pandas groupby split by NaN I have one question, I ask you guys I want to split the df like grp1, grp2, grp3, grp4 The data below is a sample and I really need to process a large amount of data, so please let me know how to do it appropriately. df = A1 B1 2021-03-18 00:00:00 10 23 2021-03-18 00:00:01 11 28 2021-03-18 00:00:02 12 29 2021-03-18 00:00:03 NaN NaN 2021-03-18 00:00:04 NaN NaN 2021-03-18 00:00:05 42 32 2021-03-18 00:00:06 51 90 2021-03-18 00:00:07 NaN NaN 2021-03-18 00:00:08 90 101 2021-03-18 00:00:09 32 42 2021-03-18 00:00:10 NaN NaN 2021-03-18 00:00:11 575 333 2021-03-18 00:00:12 62 421 grp1 = A1 B1 2021-03-18 00:00:00 10 23 2021-03-18 00:00:01 11 28 2021-03-18 00:00:02 12 29 grp2 = A1 B1 2021-03-18 00:00:05 42 32 2021-03-18 00:00:06 51 90 grp3 = A1 B1 2021-03-18 00:00:08 90 101 2021-03-18 00:00:09 32 42 grp4 = A1 B1 2021-03-18 00:00:11 575 333 2021-03-18 00:00:12 62 421
Try: x = (df.shift(1).isnull() & df.notnull()).cumsum()['B1'] df_list = [] for i,g in df.groupby(x): print(i,g.dropna()) df_list.append(g.dropna()) 1 A1 B1 2021-03-18 00:00:00 10.0 23.0 00:00:01 11.0 28.0 00:00:02 12.0 29.0 2 A1 B1 2021-03-18 00:00:05 42.0 32.0 00:00:06 51.0 90.0 3 A1 B1 2021-03-18 00:00:08 90.0 101.0 00:00:09 32.0 42.0 4 A1 B1 2021-03-18 00:00:11 575.0 333.0 00:00:12 62.0 421.0 df_list will have all your grouped dataframe.
65,040,169
Pandas merging mixed length datasets without duplicate columns
<p>I'm trying to merge several mixed dataframes, with some missing values which sometimes exist in other dataframes to one combined dataset, some dataframes also might contain extra columns, then those should be added and all other rows have NaN as values.</p> <p>This based on one or several columns, the row index has no meaning, the true dataset has many columns so manually removing anything is very much less than desirable.</p> <p>So essentially, merging several dataframes based on one or several columns, prioritizing any non NaN value, or if two conflicting non NaN values would exist then prioritize the existing value in the base dataframe and not the one being merged in.</p> <pre><code>df1 = pd.DataFrame({ 'id': [1, 2, 4], 'data_one': [np.nan, 3, np.nan], 'data_two': [4, np.nan, np.nan], }) id data_one data_two 0 1 NaN 4.0 1 2 3.0 NaN 2 4 NaN NaN df2 = pd.DataFrame({ 'id': [1, 3], 'data_one': [8, np.nan], 'data_two': [np.nan, 4], 'data_three': [np.nan, 100] }) id data_one data_two data_three 0 1 8.0 NaN NaN 1 3 NaN 4.0 100.0 # Desired result res = pd.DataFrame({ 'id': [1, 2, 3, 4], 'data_one': [8, 3, np.nan, np.nan], 'data_two': [4, np.nan, 4, np.nan], 'data_three': [np.nan, np.nan, 100, np.nan], }) id data_one data_two data_three 0 1 8.0 4.0 NaN 1 2 3.0 NaN NaN 2 3 NaN 4.0 100.0 3 4 NaN NaN NaN </code></pre> <p>The functions I have been experimenting with so far is <code>pd.merge()</code>, <code>pd.join()</code>, <code>pd.combine_first()</code> but haven't had any success, maybe missing something easy.</p>
65,044,279
"2020-11-27T15:51:54.063000"
1
null
0
73
python|pandas
<p>You can do a <code>groupby()</code> coupled with <code>fillna()</code>:</p> <pre><code>pd.concat([df1,df2]).groupby('id').apply(lambda x: x.ffill().bfill()).drop_duplicates() </code></pre> <p>results in:</p> <pre><code> id data_one data_two data_three 0 1 8.0 4.0 NaN 1 2 3.0 NaN NaN 1 3 NaN 4.0 100.0 2 4 NaN NaN NaN </code></pre> <p>Note that it is gonna return separate rows for the positions where df1 and df2 both have a non-null values. Which is intentional since I don't know what you want to do with in such cases.</p>
"2020-11-27T21:58:55.613000"
0
https://pandas.pydata.org/docs/dev/user_guide/merging.html
Merge, join, concatenate and compare# Merge, join, concatenate and compare# pandas provides various facilities for easily combining together Series or DataFrame with various kinds of set logic for the indexes You can do a groupby() coupled with fillna(): pd.concat([df1,df2]).groupby('id').apply(lambda x: x.ffill().bfill()).drop_duplicates() results in: id data_one data_two data_three 0 1 8.0 4.0 NaN 1 2 3.0 NaN NaN 1 3 NaN 4.0 100.0 2 4 NaN NaN NaN Note that it is gonna return separate rows for the positions where df1 and df2 both have a non-null values. Which is intentional since I don't know what you want to do with in such cases. and relational algebra functionality in the case of join / merge-type operations. In addition, pandas also provides utilities to compare two Series or DataFrame and summarize their differences. Concatenating objects# The concat() function (in the main pandas namespace) does all of the heavy lifting of performing concatenation operations along an axis while performing optional set logic (union or intersection) of the indexes (if any) on the other axes. Note that I say “if any” because there is only a single possible axis of concatenation for Series. Before diving into all of the details of concat and what it can do, here is a simple example: In [1]: df1 = pd.DataFrame( ...: { ...: "A": ["A0", "A1", "A2", "A3"], ...: "B": ["B0", "B1", "B2", "B3"], ...: "C": ["C0", "C1", "C2", "C3"], ...: "D": ["D0", "D1", "D2", "D3"], ...: }, ...: index=[0, 1, 2, 3], ...: ) ...: In [2]: df2 = pd.DataFrame( ...: { ...: "A": ["A4", "A5", "A6", "A7"], ...: "B": ["B4", "B5", "B6", "B7"], ...: "C": ["C4", "C5", "C6", "C7"], ...: "D": ["D4", "D5", "D6", "D7"], ...: }, ...: index=[4, 5, 6, 7], ...: ) ...: In [3]: df3 = pd.DataFrame( ...: { ...: "A": ["A8", "A9", "A10", "A11"], ...: "B": ["B8", "B9", "B10", "B11"], ...: "C": ["C8", "C9", "C10", "C11"], ...: "D": ["D8", "D9", "D10", "D11"], ...: }, ...: index=[8, 9, 10, 11], ...: ) ...: In [4]: frames = [df1, df2, df3] In [5]: result = pd.concat(frames) Like its sibling function on ndarrays, numpy.concatenate, pandas.concat takes a list or dict of homogeneously-typed objects and concatenates them with some configurable handling of “what to do with the other axes”: pd.concat( objs, axis=0, join="outer", ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, copy=True, ) objs : a sequence or mapping of Series or DataFrame objects. If a dict is passed, the sorted keys will be used as the keys argument, unless it is passed, in which case the values will be selected (see below). Any None objects will be dropped silently unless they are all None in which case a ValueError will be raised. axis : {0, 1, …}, default 0. The axis to concatenate along. join : {‘inner’, ‘outer’}, default ‘outer’. How to handle indexes on other axis(es). Outer for union and inner for intersection. ignore_index : boolean, default False. If True, do not use the index values on the concatenation axis. The resulting axis will be labeled 0, …, n - 1. This is useful if you are concatenating objects where the concatenation axis does not have meaningful indexing information. Note the index values on the other axes are still respected in the join. keys : sequence, default None. Construct hierarchical index using the passed keys as the outermost level. If multiple levels passed, should contain tuples. levels : list of sequences, default None. Specific levels (unique values) to use for constructing a MultiIndex. Otherwise they will be inferred from the keys. names : list, default None. Names for the levels in the resulting hierarchical index. verify_integrity : boolean, default False. Check whether the new concatenated axis contains duplicates. This can be very expensive relative to the actual data concatenation. copy : boolean, default True. If False, do not copy data unnecessarily. Without a little bit of context many of these arguments don’t make much sense. Let’s revisit the above example. Suppose we wanted to associate specific keys with each of the pieces of the chopped up DataFrame. We can do this using the keys argument: In [6]: result = pd.concat(frames, keys=["x", "y", "z"]) As you can see (if you’ve read the rest of the documentation), the resulting object’s index has a hierarchical index. This means that we can now select out each chunk by key: In [7]: result.loc["y"] Out[7]: A B C D 4 A4 B4 C4 D4 5 A5 B5 C5 D5 6 A6 B6 C6 D6 7 A7 B7 C7 D7 It’s not a stretch to see how this can be very useful. More detail on this functionality below. Note It is worth noting that concat() makes a full copy of the data, and that constantly reusing this function can create a significant performance hit. If you need to use the operation over several datasets, use a list comprehension. frames = [ process_your_file(f) for f in files ] result = pd.concat(frames) Note When concatenating DataFrames with named axes, pandas will attempt to preserve these index/column names whenever possible. In the case where all inputs share a common name, this name will be assigned to the result. When the input names do not all agree, the result will be unnamed. The same is true for MultiIndex, but the logic is applied separately on a level-by-level basis. Set logic on the other axes# When gluing together multiple DataFrames, you have a choice of how to handle the other axes (other than the one being concatenated). This can be done in the following two ways: Take the union of them all, join='outer'. This is the default option as it results in zero information loss. Take the intersection, join='inner'. Here is an example of each of these methods. First, the default join='outer' behavior: In [8]: df4 = pd.DataFrame( ...: { ...: "B": ["B2", "B3", "B6", "B7"], ...: "D": ["D2", "D3", "D6", "D7"], ...: "F": ["F2", "F3", "F6", "F7"], ...: }, ...: index=[2, 3, 6, 7], ...: ) ...: In [9]: result = pd.concat([df1, df4], axis=1) Here is the same thing with join='inner': In [10]: result = pd.concat([df1, df4], axis=1, join="inner") Lastly, suppose we just wanted to reuse the exact index from the original DataFrame: In [11]: result = pd.concat([df1, df4], axis=1).reindex(df1.index) Similarly, we could index before the concatenation: In [12]: pd.concat([df1, df4.reindex(df1.index)], axis=1) Out[12]: A B C D B D F 0 A0 B0 C0 D0 NaN NaN NaN 1 A1 B1 C1 D1 NaN NaN NaN 2 A2 B2 C2 D2 B2 D2 F2 3 A3 B3 C3 D3 B3 D3 F3 Ignoring indexes on the concatenation axis# For DataFrame objects which don’t have a meaningful index, you may wish to append them and ignore the fact that they may have overlapping indexes. To do this, use the ignore_index argument: In [13]: result = pd.concat([df1, df4], ignore_index=True, sort=False) Concatenating with mixed ndims# You can concatenate a mix of Series and DataFrame objects. The Series will be transformed to DataFrame with the column name as the name of the Series. In [14]: s1 = pd.Series(["X0", "X1", "X2", "X3"], name="X") In [15]: result = pd.concat([df1, s1], axis=1) Note Since we’re concatenating a Series to a DataFrame, we could have achieved the same result with DataFrame.assign(). To concatenate an arbitrary number of pandas objects (DataFrame or Series), use concat. If unnamed Series are passed they will be numbered consecutively. In [16]: s2 = pd.Series(["_0", "_1", "_2", "_3"]) In [17]: result = pd.concat([df1, s2, s2, s2], axis=1) Passing ignore_index=True will drop all name references. In [18]: result = pd.concat([df1, s1], axis=1, ignore_index=True) More concatenating with group keys# A fairly common use of the keys argument is to override the column names when creating a new DataFrame based on existing Series. Notice how the default behaviour consists on letting the resulting DataFrame inherit the parent Series’ name, when these existed. In [19]: s3 = pd.Series([0, 1, 2, 3], name="foo") In [20]: s4 = pd.Series([0, 1, 2, 3]) In [21]: s5 = pd.Series([0, 1, 4, 5]) In [22]: pd.concat([s3, s4, s5], axis=1) Out[22]: foo 0 1 0 0 0 0 1 1 1 1 2 2 2 4 3 3 3 5 Through the keys argument we can override the existing column names. In [23]: pd.concat([s3, s4, s5], axis=1, keys=["red", "blue", "yellow"]) Out[23]: red blue yellow 0 0 0 0 1 1 1 1 2 2 2 4 3 3 3 5 Let’s consider a variation of the very first example presented: In [24]: result = pd.concat(frames, keys=["x", "y", "z"]) You can also pass a dict to concat in which case the dict keys will be used for the keys argument (unless other keys are specified): In [25]: pieces = {"x": df1, "y": df2, "z": df3} In [26]: result = pd.concat(pieces) In [27]: result = pd.concat(pieces, keys=["z", "y"]) The MultiIndex created has levels that are constructed from the passed keys and the index of the DataFrame pieces: In [28]: result.index.levels Out[28]: FrozenList([['z', 'y'], [4, 5, 6, 7, 8, 9, 10, 11]]) If you wish to specify other levels (as will occasionally be the case), you can do so using the levels argument: In [29]: result = pd.concat( ....: pieces, keys=["x", "y", "z"], levels=[["z", "y", "x", "w"]], names=["group_key"] ....: ) ....: In [30]: result.index.levels Out[30]: FrozenList([['z', 'y', 'x', 'w'], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]) This is fairly esoteric, but it is actually necessary for implementing things like GroupBy where the order of a categorical variable is meaningful. Appending rows to a DataFrame# If you have a series that you want to append as a single row to a DataFrame, you can convert the row into a DataFrame and use concat In [31]: s2 = pd.Series(["X0", "X1", "X2", "X3"], index=["A", "B", "C", "D"]) In [32]: result = pd.concat([df1, s2.to_frame().T], ignore_index=True) You should use ignore_index with this method to instruct DataFrame to discard its index. If you wish to preserve the index, you should construct an appropriately-indexed DataFrame and append or concatenate those objects. Database-style DataFrame or named Series joining/merging# pandas has full-featured, high performance in-memory join operations idiomatically very similar to relational databases like SQL. These methods perform significantly better (in some cases well over an order of magnitude better) than other open source implementations (like base::merge.data.frame in R). The reason for this is careful algorithmic design and the internal layout of the data in DataFrame. See the cookbook for some advanced strategies. Users who are familiar with SQL but new to pandas might be interested in a comparison with SQL. pandas provides a single function, merge(), as the entry point for all standard database join operations between DataFrame or named Series objects: pd.merge( left, right, how="inner", on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=True, suffixes=("_x", "_y"), copy=True, indicator=False, validate=None, ) left: A DataFrame or named Series object. right: Another DataFrame or named Series object. on: Column or index level names to join on. Must be found in both the left and right DataFrame and/or Series objects. If not passed and left_index and right_index are False, the intersection of the columns in the DataFrames and/or Series will be inferred to be the join keys. left_on: Columns or index levels from the left DataFrame or Series to use as keys. Can either be column names, index level names, or arrays with length equal to the length of the DataFrame or Series. right_on: Columns or index levels from the right DataFrame or Series to use as keys. Can either be column names, index level names, or arrays with length equal to the length of the DataFrame or Series. left_index: If True, use the index (row labels) from the left DataFrame or Series as its join key(s). In the case of a DataFrame or Series with a MultiIndex (hierarchical), the number of levels must match the number of join keys from the right DataFrame or Series. right_index: Same usage as left_index for the right DataFrame or Series how: One of 'left', 'right', 'outer', 'inner', 'cross'. Defaults to inner. See below for more detailed description of each method. sort: Sort the result DataFrame by the join keys in lexicographical order. Defaults to True, setting to False will improve performance substantially in many cases. suffixes: A tuple of string suffixes to apply to overlapping columns. Defaults to ('_x', '_y'). copy: Always copy data (default True) from the passed DataFrame or named Series objects, even when reindexing is not necessary. Cannot be avoided in many cases but may improve performance / memory usage. The cases where copying can be avoided are somewhat pathological but this option is provided nonetheless. indicator: Add a column to the output DataFrame called _merge with information on the source of each row. _merge is Categorical-type and takes on a value of left_only for observations whose merge key only appears in 'left' DataFrame or Series, right_only for observations whose merge key only appears in 'right' DataFrame or Series, and both if the observation’s merge key is found in both. validate : string, default None. If specified, checks if merge is of specified type. “one_to_one” or “1:1”: checks if merge keys are unique in both left and right datasets. “one_to_many” or “1:m”: checks if merge keys are unique in left dataset. “many_to_one” or “m:1”: checks if merge keys are unique in right dataset. “many_to_many” or “m:m”: allowed, but does not result in checks. Note Support for specifying index levels as the on, left_on, and right_on parameters was added in version 0.23.0. Support for merging named Series objects was added in version 0.24.0. The return type will be the same as left. If left is a DataFrame or named Series and right is a subclass of DataFrame, the return type will still be DataFrame. merge is a function in the pandas namespace, and it is also available as a DataFrame instance method merge(), with the calling DataFrame being implicitly considered the left object in the join. The related join() method, uses merge internally for the index-on-index (by default) and column(s)-on-index join. If you are joining on index only, you may wish to use DataFrame.join to save yourself some typing. Brief primer on merge methods (relational algebra)# Experienced users of relational databases like SQL will be familiar with the terminology used to describe join operations between two SQL-table like structures (DataFrame objects). There are several cases to consider which are very important to understand: one-to-one joins: for example when joining two DataFrame objects on their indexes (which must contain unique values). many-to-one joins: for example when joining an index (unique) to one or more columns in a different DataFrame. many-to-many joins: joining columns on columns. Note When joining columns on columns (potentially a many-to-many join), any indexes on the passed DataFrame objects will be discarded. It is worth spending some time understanding the result of the many-to-many join case. In SQL / standard relational algebra, if a key combination appears more than once in both tables, the resulting table will have the Cartesian product of the associated data. Here is a very basic example with one unique key combination: In [33]: left = pd.DataFrame( ....: { ....: "key": ["K0", "K1", "K2", "K3"], ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: } ....: ) ....: In [34]: right = pd.DataFrame( ....: { ....: "key": ["K0", "K1", "K2", "K3"], ....: "C": ["C0", "C1", "C2", "C3"], ....: "D": ["D0", "D1", "D2", "D3"], ....: } ....: ) ....: In [35]: result = pd.merge(left, right, on="key") Here is a more complicated example with multiple join keys. Only the keys appearing in left and right are present (the intersection), since how='inner' by default. In [36]: left = pd.DataFrame( ....: { ....: "key1": ["K0", "K0", "K1", "K2"], ....: "key2": ["K0", "K1", "K0", "K1"], ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: } ....: ) ....: In [37]: right = pd.DataFrame( ....: { ....: "key1": ["K0", "K1", "K1", "K2"], ....: "key2": ["K0", "K0", "K0", "K0"], ....: "C": ["C0", "C1", "C2", "C3"], ....: "D": ["D0", "D1", "D2", "D3"], ....: } ....: ) ....: In [38]: result = pd.merge(left, right, on=["key1", "key2"]) The how argument to merge specifies how to determine which keys are to be included in the resulting table. If a key combination does not appear in either the left or right tables, the values in the joined table will be NA. Here is a summary of the how options and their SQL equivalent names: Merge method SQL Join Name Description left LEFT OUTER JOIN Use keys from left frame only right RIGHT OUTER JOIN Use keys from right frame only outer FULL OUTER JOIN Use union of keys from both frames inner INNER JOIN Use intersection of keys from both frames cross CROSS JOIN Create the cartesian product of rows of both frames In [39]: result = pd.merge(left, right, how="left", on=["key1", "key2"]) In [40]: result = pd.merge(left, right, how="right", on=["key1", "key2"]) In [41]: result = pd.merge(left, right, how="outer", on=["key1", "key2"]) In [42]: result = pd.merge(left, right, how="inner", on=["key1", "key2"]) In [43]: result = pd.merge(left, right, how="cross") You can merge a mult-indexed Series and a DataFrame, if the names of the MultiIndex correspond to the columns from the DataFrame. Transform the Series to a DataFrame using Series.reset_index() before merging, as shown in the following example. In [44]: df = pd.DataFrame({"Let": ["A", "B", "C"], "Num": [1, 2, 3]}) In [45]: df Out[45]: Let Num 0 A 1 1 B 2 2 C 3 In [46]: ser = pd.Series( ....: ["a", "b", "c", "d", "e", "f"], ....: index=pd.MultiIndex.from_arrays( ....: [["A", "B", "C"] * 2, [1, 2, 3, 4, 5, 6]], names=["Let", "Num"] ....: ), ....: ) ....: In [47]: ser Out[47]: Let Num A 1 a B 2 b C 3 c A 4 d B 5 e C 6 f dtype: object In [48]: pd.merge(df, ser.reset_index(), on=["Let", "Num"]) Out[48]: Let Num 0 0 A 1 a 1 B 2 b 2 C 3 c Here is another example with duplicate join keys in DataFrames: In [49]: left = pd.DataFrame({"A": [1, 2], "B": [2, 2]}) In [50]: right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]}) In [51]: result = pd.merge(left, right, on="B", how="outer") Warning Joining / merging on duplicate keys can cause a returned frame that is the multiplication of the row dimensions, which may result in memory overflow. It is the user’ s responsibility to manage duplicate values in keys before joining large DataFrames. Checking for duplicate keys# Users can use the validate argument to automatically check whether there are unexpected duplicates in their merge keys. Key uniqueness is checked before merge operations and so should protect against memory overflows. Checking key uniqueness is also a good way to ensure user data structures are as expected. In the following example, there are duplicate values of B in the right DataFrame. As this is not a one-to-one merge – as specified in the validate argument – an exception will be raised. In [52]: left = pd.DataFrame({"A": [1, 2], "B": [1, 2]}) In [53]: right = pd.DataFrame({"A": [4, 5, 6], "B": [2, 2, 2]}) In [53]: result = pd.merge(left, right, on="B", how="outer", validate="one_to_one") ... MergeError: Merge keys are not unique in right dataset; not a one-to-one merge If the user is aware of the duplicates in the right DataFrame but wants to ensure there are no duplicates in the left DataFrame, one can use the validate='one_to_many' argument instead, which will not raise an exception. In [54]: pd.merge(left, right, on="B", how="outer", validate="one_to_many") Out[54]: A_x B A_y 0 1 1 NaN 1 2 2 4.0 2 2 2 5.0 3 2 2 6.0 The merge indicator# merge() accepts the argument indicator. If True, a Categorical-type column called _merge will be added to the output object that takes on values: Observation Origin _merge value Merge key only in 'left' frame left_only Merge key only in 'right' frame right_only Merge key in both frames both In [55]: df1 = pd.DataFrame({"col1": [0, 1], "col_left": ["a", "b"]}) In [56]: df2 = pd.DataFrame({"col1": [1, 2, 2], "col_right": [2, 2, 2]}) In [57]: pd.merge(df1, df2, on="col1", how="outer", indicator=True) Out[57]: col1 col_left col_right _merge 0 0 a NaN left_only 1 1 b 2.0 both 2 2 NaN 2.0 right_only 3 2 NaN 2.0 right_only The indicator argument will also accept string arguments, in which case the indicator function will use the value of the passed string as the name for the indicator column. In [58]: pd.merge(df1, df2, on="col1", how="outer", indicator="indicator_column") Out[58]: col1 col_left col_right indicator_column 0 0 a NaN left_only 1 1 b 2.0 both 2 2 NaN 2.0 right_only 3 2 NaN 2.0 right_only Merge dtypes# Merging will preserve the dtype of the join keys. In [59]: left = pd.DataFrame({"key": [1], "v1": [10]}) In [60]: left Out[60]: key v1 0 1 10 In [61]: right = pd.DataFrame({"key": [1, 2], "v1": [20, 30]}) In [62]: right Out[62]: key v1 0 1 20 1 2 30 We are able to preserve the join keys: In [63]: pd.merge(left, right, how="outer") Out[63]: key v1 0 1 10 1 1 20 2 2 30 In [64]: pd.merge(left, right, how="outer").dtypes Out[64]: key int64 v1 int64 dtype: object Of course if you have missing values that are introduced, then the resulting dtype will be upcast. In [65]: pd.merge(left, right, how="outer", on="key") Out[65]: key v1_x v1_y 0 1 10.0 20 1 2 NaN 30 In [66]: pd.merge(left, right, how="outer", on="key").dtypes Out[66]: key int64 v1_x float64 v1_y int64 dtype: object Merging will preserve category dtypes of the mergands. See also the section on categoricals. The left frame. In [67]: from pandas.api.types import CategoricalDtype In [68]: X = pd.Series(np.random.choice(["foo", "bar"], size=(10,))) In [69]: X = X.astype(CategoricalDtype(categories=["foo", "bar"])) In [70]: left = pd.DataFrame( ....: {"X": X, "Y": np.random.choice(["one", "two", "three"], size=(10,))} ....: ) ....: In [71]: left Out[71]: X Y 0 bar one 1 foo one 2 foo three 3 bar three 4 foo one 5 bar one 6 bar three 7 bar three 8 bar three 9 foo three In [72]: left.dtypes Out[72]: X category Y object dtype: object The right frame. In [73]: right = pd.DataFrame( ....: { ....: "X": pd.Series(["foo", "bar"], dtype=CategoricalDtype(["foo", "bar"])), ....: "Z": [1, 2], ....: } ....: ) ....: In [74]: right Out[74]: X Z 0 foo 1 1 bar 2 In [75]: right.dtypes Out[75]: X category Z int64 dtype: object The merged result: In [76]: result = pd.merge(left, right, how="outer") In [77]: result Out[77]: X Y Z 0 bar one 2 1 bar three 2 2 bar one 2 3 bar three 2 4 bar three 2 5 bar three 2 6 foo one 1 7 foo three 1 8 foo one 1 9 foo three 1 In [78]: result.dtypes Out[78]: X category Y object Z int64 dtype: object Note The category dtypes must be exactly the same, meaning the same categories and the ordered attribute. Otherwise the result will coerce to the categories’ dtype. Note Merging on category dtypes that are the same can be quite performant compared to object dtype merging. Joining on index# DataFrame.join() is a convenient method for combining the columns of two potentially differently-indexed DataFrames into a single result DataFrame. Here is a very basic example: In [79]: left = pd.DataFrame( ....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=["K0", "K1", "K2"] ....: ) ....: In [80]: right = pd.DataFrame( ....: {"C": ["C0", "C2", "C3"], "D": ["D0", "D2", "D3"]}, index=["K0", "K2", "K3"] ....: ) ....: In [81]: result = left.join(right) In [82]: result = left.join(right, how="outer") The same as above, but with how='inner'. In [83]: result = left.join(right, how="inner") The data alignment here is on the indexes (row labels). This same behavior can be achieved using merge plus additional arguments instructing it to use the indexes: In [84]: result = pd.merge(left, right, left_index=True, right_index=True, how="outer") In [85]: result = pd.merge(left, right, left_index=True, right_index=True, how="inner") Joining key columns on an index# join() takes an optional on argument which may be a column or multiple column names, which specifies that the passed DataFrame is to be aligned on that column in the DataFrame. These two function calls are completely equivalent: left.join(right, on=key_or_keys) pd.merge( left, right, left_on=key_or_keys, right_index=True, how="left", sort=False ) Obviously you can choose whichever form you find more convenient. For many-to-one joins (where one of the DataFrame’s is already indexed by the join key), using join may be more convenient. Here is a simple example: In [86]: left = pd.DataFrame( ....: { ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: "key": ["K0", "K1", "K0", "K1"], ....: } ....: ) ....: In [87]: right = pd.DataFrame({"C": ["C0", "C1"], "D": ["D0", "D1"]}, index=["K0", "K1"]) In [88]: result = left.join(right, on="key") In [89]: result = pd.merge( ....: left, right, left_on="key", right_index=True, how="left", sort=False ....: ) ....: To join on multiple keys, the passed DataFrame must have a MultiIndex: In [90]: left = pd.DataFrame( ....: { ....: "A": ["A0", "A1", "A2", "A3"], ....: "B": ["B0", "B1", "B2", "B3"], ....: "key1": ["K0", "K0", "K1", "K2"], ....: "key2": ["K0", "K1", "K0", "K1"], ....: } ....: ) ....: In [91]: index = pd.MultiIndex.from_tuples( ....: [("K0", "K0"), ("K1", "K0"), ("K2", "K0"), ("K2", "K1")] ....: ) ....: In [92]: right = pd.DataFrame( ....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=index ....: ) ....: Now this can be joined by passing the two key column names: In [93]: result = left.join(right, on=["key1", "key2"]) The default for DataFrame.join is to perform a left join (essentially a “VLOOKUP” operation, for Excel users), which uses only the keys found in the calling DataFrame. Other join types, for example inner join, can be just as easily performed: In [94]: result = left.join(right, on=["key1", "key2"], how="inner") As you can see, this drops any rows where there was no match. Joining a single Index to a MultiIndex# You can join a singly-indexed DataFrame with a level of a MultiIndexed DataFrame. The level will match on the name of the index of the singly-indexed frame against a level name of the MultiIndexed frame. In [95]: left = pd.DataFrame( ....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, ....: index=pd.Index(["K0", "K1", "K2"], name="key"), ....: ) ....: In [96]: index = pd.MultiIndex.from_tuples( ....: [("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")], ....: names=["key", "Y"], ....: ) ....: In [97]: right = pd.DataFrame( ....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, ....: index=index, ....: ) ....: In [98]: result = left.join(right, how="inner") This is equivalent but less verbose and more memory efficient / faster than this. In [99]: result = pd.merge( ....: left.reset_index(), right.reset_index(), on=["key"], how="inner" ....: ).set_index(["key","Y"]) ....: Joining with two MultiIndexes# This is supported in a limited way, provided that the index for the right argument is completely used in the join, and is a subset of the indices in the left argument, as in this example: In [100]: leftindex = pd.MultiIndex.from_product( .....: [list("abc"), list("xy"), [1, 2]], names=["abc", "xy", "num"] .....: ) .....: In [101]: left = pd.DataFrame({"v1": range(12)}, index=leftindex) In [102]: left Out[102]: v1 abc xy num a x 1 0 2 1 y 1 2 2 3 b x 1 4 2 5 y 1 6 2 7 c x 1 8 2 9 y 1 10 2 11 In [103]: rightindex = pd.MultiIndex.from_product( .....: [list("abc"), list("xy")], names=["abc", "xy"] .....: ) .....: In [104]: right = pd.DataFrame({"v2": [100 * i for i in range(1, 7)]}, index=rightindex) In [105]: right Out[105]: v2 abc xy a x 100 y 200 b x 300 y 400 c x 500 y 600 In [106]: left.join(right, on=["abc", "xy"], how="inner") Out[106]: v1 v2 abc xy num a x 1 0 100 2 1 100 y 1 2 200 2 3 200 b x 1 4 300 2 5 300 y 1 6 400 2 7 400 c x 1 8 500 2 9 500 y 1 10 600 2 11 600 If that condition is not satisfied, a join with two multi-indexes can be done using the following code. In [107]: leftindex = pd.MultiIndex.from_tuples( .....: [("K0", "X0"), ("K0", "X1"), ("K1", "X2")], names=["key", "X"] .....: ) .....: In [108]: left = pd.DataFrame( .....: {"A": ["A0", "A1", "A2"], "B": ["B0", "B1", "B2"]}, index=leftindex .....: ) .....: In [109]: rightindex = pd.MultiIndex.from_tuples( .....: [("K0", "Y0"), ("K1", "Y1"), ("K2", "Y2"), ("K2", "Y3")], names=["key", "Y"] .....: ) .....: In [110]: right = pd.DataFrame( .....: {"C": ["C0", "C1", "C2", "C3"], "D": ["D0", "D1", "D2", "D3"]}, index=rightindex .....: ) .....: In [111]: result = pd.merge( .....: left.reset_index(), right.reset_index(), on=["key"], how="inner" .....: ).set_index(["key", "X", "Y"]) .....: Merging on a combination of columns and index levels# Strings passed as the on, left_on, and right_on parameters may refer to either column names or index level names. This enables merging DataFrame instances on a combination of index levels and columns without resetting indexes. In [112]: left_index = pd.Index(["K0", "K0", "K1", "K2"], name="key1") In [113]: left = pd.DataFrame( .....: { .....: "A": ["A0", "A1", "A2", "A3"], .....: "B": ["B0", "B1", "B2", "B3"], .....: "key2": ["K0", "K1", "K0", "K1"], .....: }, .....: index=left_index, .....: ) .....: In [114]: right_index = pd.Index(["K0", "K1", "K2", "K2"], name="key1") In [115]: right = pd.DataFrame( .....: { .....: "C": ["C0", "C1", "C2", "C3"], .....: "D": ["D0", "D1", "D2", "D3"], .....: "key2": ["K0", "K0", "K0", "K1"], .....: }, .....: index=right_index, .....: ) .....: In [116]: result = left.merge(right, on=["key1", "key2"]) Note When DataFrames are merged on a string that matches an index level in both frames, the index level is preserved as an index level in the resulting DataFrame. Note When DataFrames are merged using only some of the levels of a MultiIndex, the extra levels will be dropped from the resulting merge. In order to preserve those levels, use reset_index on those level names to move those levels to columns prior to doing the merge. Note If a string matches both a column name and an index level name, then a warning is issued and the column takes precedence. This will result in an ambiguity error in a future version. Overlapping value columns# The merge suffixes argument takes a tuple of list of strings to append to overlapping column names in the input DataFrames to disambiguate the result columns: In [117]: left = pd.DataFrame({"k": ["K0", "K1", "K2"], "v": [1, 2, 3]}) In [118]: right = pd.DataFrame({"k": ["K0", "K0", "K3"], "v": [4, 5, 6]}) In [119]: result = pd.merge(left, right, on="k") In [120]: result = pd.merge(left, right, on="k", suffixes=("_l", "_r")) DataFrame.join() has lsuffix and rsuffix arguments which behave similarly. In [121]: left = left.set_index("k") In [122]: right = right.set_index("k") In [123]: result = left.join(right, lsuffix="_l", rsuffix="_r") Joining multiple DataFrames# A list or tuple of DataFrames can also be passed to join() to join them together on their indexes. In [124]: right2 = pd.DataFrame({"v": [7, 8, 9]}, index=["K1", "K1", "K2"]) In [125]: result = left.join([right, right2]) Merging together values within Series or DataFrame columns# Another fairly common situation is to have two like-indexed (or similarly indexed) Series or DataFrame objects and wanting to “patch” values in one object from values for matching indices in the other. Here is an example: In [126]: df1 = pd.DataFrame( .....: [[np.nan, 3.0, 5.0], [-4.6, np.nan, np.nan], [np.nan, 7.0, np.nan]] .....: ) .....: In [127]: df2 = pd.DataFrame([[-42.6, np.nan, -8.2], [-5.0, 1.6, 4]], index=[1, 2]) For this, use the combine_first() method: In [128]: result = df1.combine_first(df2) Note that this method only takes values from the right DataFrame if they are missing in the left DataFrame. A related method, update(), alters non-NA values in place: In [129]: df1.update(df2) Timeseries friendly merging# Merging ordered data# A merge_ordered() function allows combining time series and other ordered data. In particular it has an optional fill_method keyword to fill/interpolate missing data: In [130]: left = pd.DataFrame( .....: {"k": ["K0", "K1", "K1", "K2"], "lv": [1, 2, 3, 4], "s": ["a", "b", "c", "d"]} .....: ) .....: In [131]: right = pd.DataFrame({"k": ["K1", "K2", "K4"], "rv": [1, 2, 3]}) In [132]: pd.merge_ordered(left, right, fill_method="ffill", left_by="s") Out[132]: k lv s rv 0 K0 1.0 a NaN 1 K1 1.0 a 1.0 2 K2 1.0 a 2.0 3 K4 1.0 a 3.0 4 K1 2.0 b 1.0 5 K2 2.0 b 2.0 6 K4 2.0 b 3.0 7 K1 3.0 c 1.0 8 K2 3.0 c 2.0 9 K4 3.0 c 3.0 10 K1 NaN d 1.0 11 K2 4.0 d 2.0 12 K4 4.0 d 3.0 Merging asof# A merge_asof() is similar to an ordered left-join except that we match on nearest key rather than equal keys. For each row in the left DataFrame, we select the last row in the right DataFrame whose on key is less than the left’s key. Both DataFrames must be sorted by the key. Optionally an asof merge can perform a group-wise merge. This matches the by key equally, in addition to the nearest match on the on key. For example; we might have trades and quotes and we want to asof merge them. In [133]: trades = pd.DataFrame( .....: { .....: "time": pd.to_datetime( .....: [ .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.038", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.048", .....: ] .....: ), .....: "ticker": ["MSFT", "MSFT", "GOOG", "GOOG", "AAPL"], .....: "price": [51.95, 51.95, 720.77, 720.92, 98.00], .....: "quantity": [75, 155, 100, 100, 100], .....: }, .....: columns=["time", "ticker", "price", "quantity"], .....: ) .....: In [134]: quotes = pd.DataFrame( .....: { .....: "time": pd.to_datetime( .....: [ .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.023", .....: "20160525 13:30:00.030", .....: "20160525 13:30:00.041", .....: "20160525 13:30:00.048", .....: "20160525 13:30:00.049", .....: "20160525 13:30:00.072", .....: "20160525 13:30:00.075", .....: ] .....: ), .....: "ticker": ["GOOG", "MSFT", "MSFT", "MSFT", "GOOG", "AAPL", "GOOG", "MSFT"], .....: "bid": [720.50, 51.95, 51.97, 51.99, 720.50, 97.99, 720.50, 52.01], .....: "ask": [720.93, 51.96, 51.98, 52.00, 720.93, 98.01, 720.88, 52.03], .....: }, .....: columns=["time", "ticker", "bid", "ask"], .....: ) .....: In [135]: trades Out[135]: time ticker price quantity 0 2016-05-25 13:30:00.023 MSFT 51.95 75 1 2016-05-25 13:30:00.038 MSFT 51.95 155 2 2016-05-25 13:30:00.048 GOOG 720.77 100 3 2016-05-25 13:30:00.048 GOOG 720.92 100 4 2016-05-25 13:30:00.048 AAPL 98.00 100 In [136]: quotes Out[136]: time ticker bid ask 0 2016-05-25 13:30:00.023 GOOG 720.50 720.93 1 2016-05-25 13:30:00.023 MSFT 51.95 51.96 2 2016-05-25 13:30:00.030 MSFT 51.97 51.98 3 2016-05-25 13:30:00.041 MSFT 51.99 52.00 4 2016-05-25 13:30:00.048 GOOG 720.50 720.93 5 2016-05-25 13:30:00.049 AAPL 97.99 98.01 6 2016-05-25 13:30:00.072 GOOG 720.50 720.88 7 2016-05-25 13:30:00.075 MSFT 52.01 52.03 By default we are taking the asof of the quotes. In [137]: pd.merge_asof(trades, quotes, on="time", by="ticker") Out[137]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 51.95 51.96 1 2016-05-25 13:30:00.038 MSFT 51.95 155 51.97 51.98 2 2016-05-25 13:30:00.048 GOOG 720.77 100 720.50 720.93 3 2016-05-25 13:30:00.048 GOOG 720.92 100 720.50 720.93 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN We only asof within 2ms between the quote time and the trade time. In [138]: pd.merge_asof(trades, quotes, on="time", by="ticker", tolerance=pd.Timedelta("2ms")) Out[138]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 51.95 51.96 1 2016-05-25 13:30:00.038 MSFT 51.95 155 NaN NaN 2 2016-05-25 13:30:00.048 GOOG 720.77 100 720.50 720.93 3 2016-05-25 13:30:00.048 GOOG 720.92 100 720.50 720.93 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN We only asof within 10ms between the quote time and the trade time and we exclude exact matches on time. Note that though we exclude the exact matches (of the quotes), prior quotes do propagate to that point in time. In [139]: pd.merge_asof( .....: trades, .....: quotes, .....: on="time", .....: by="ticker", .....: tolerance=pd.Timedelta("10ms"), .....: allow_exact_matches=False, .....: ) .....: Out[139]: time ticker price quantity bid ask 0 2016-05-25 13:30:00.023 MSFT 51.95 75 NaN NaN 1 2016-05-25 13:30:00.038 MSFT 51.95 155 51.97 51.98 2 2016-05-25 13:30:00.048 GOOG 720.77 100 NaN NaN 3 2016-05-25 13:30:00.048 GOOG 720.92 100 NaN NaN 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN Comparing objects# The compare() and compare() methods allow you to compare two DataFrame or Series, respectively, and summarize their differences. This feature was added in V1.1.0. For example, you might want to compare two DataFrame and stack their differences side by side. In [140]: df = pd.DataFrame( .....: { .....: "col1": ["a", "a", "b", "b", "a"], .....: "col2": [1.0, 2.0, 3.0, np.nan, 5.0], .....: "col3": [1.0, 2.0, 3.0, 4.0, 5.0], .....: }, .....: columns=["col1", "col2", "col3"], .....: ) .....: In [141]: df Out[141]: col1 col2 col3 0 a 1.0 1.0 1 a 2.0 2.0 2 b 3.0 3.0 3 b NaN 4.0 4 a 5.0 5.0 In [142]: df2 = df.copy() In [143]: df2.loc[0, "col1"] = "c" In [144]: df2.loc[2, "col3"] = 4.0 In [145]: df2 Out[145]: col1 col2 col3 0 c 1.0 1.0 1 a 2.0 2.0 2 b 3.0 4.0 3 b NaN 4.0 4 a 5.0 5.0 In [146]: df.compare(df2) Out[146]: col1 col3 self other self other 0 a c NaN NaN 2 NaN NaN 3.0 4.0 By default, if two corresponding values are equal, they will be shown as NaN. Furthermore, if all values in an entire row / column, the row / column will be omitted from the result. The remaining differences will be aligned on columns. If you wish, you may choose to stack the differences on rows. In [147]: df.compare(df2, align_axis=0) Out[147]: col1 col3 0 self a NaN other c NaN 2 self NaN 3.0 other NaN 4.0 If you wish to keep all original rows and columns, set keep_shape argument to True. In [148]: df.compare(df2, keep_shape=True) Out[148]: col1 col2 col3 self other self other self other 0 a c NaN NaN NaN NaN 1 NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN 3.0 4.0 3 NaN NaN NaN NaN NaN NaN 4 NaN NaN NaN NaN NaN NaN You may also keep all the original values even if they are equal. In [149]: df.compare(df2, keep_shape=True, keep_equal=True) Out[149]: col1 col2 col3 self other self other self other 0 a c 1.0 1.0 1.0 1.0 1 a a 2.0 2.0 2.0 2.0 2 b b 3.0 3.0 3.0 4.0 3 b b NaN NaN 4.0 4.0 4 a a 5.0 5.0 5.0 5.0
211
735
Pandas merging mixed length datasets without duplicate columns I'm trying to merge several mixed dataframes, with some missing values which sometimes exist in other dataframes to one combined dataset, some dataframes also might contain extra columns, then those should be added and all other rows have NaN as values. This based on one or several columns, the row index has no meaning, the true dataset has many columns so manually removing anything is very much less than desirable. So essentially, merging several dataframes based on one or several columns, prioritizing any non NaN value, or if two conflicting non NaN values would exist then prioritize the existing value in the base dataframe and not the one being merged in. df1 = pd.DataFrame({ 'id': [1, 2, 4], 'data_one': [np.nan, 3, np.nan], 'data_two': [4, np.nan, np.nan], }) id data_one data_two 0 1 NaN 4.0 1 2 3.0 NaN 2 4 NaN NaN df2 = pd.DataFrame({ 'id': [1, 3], 'data_one': [8, np.nan], 'data_two': [np.nan, 4], 'data_three': [np.nan, 100] }) id data_one data_two data_three 0 1 8.0 NaN NaN 1 3 NaN 4.0 100.0 # Desired result res = pd.DataFrame({ 'id': [1, 2, 3, 4], 'data_one': [8, 3, np.nan, np.nan], 'data_two': [4, np.nan, 4, np.nan], 'data_three': [np.nan, np.nan, 100, np.nan], }) id data_one data_two data_three 0 1 8.0 4.0 NaN 1 2 3.0 NaN NaN 2 3 NaN 4.0 100.0 3 4 NaN NaN NaN The functions I have been experimenting with so far is pd.merge(), pd.join(), pd.combine_first() but haven't had any success, maybe missing something easy.
You can do a groupby() coupled with fillna(): pd.concat([df1,df2]).groupby('id').apply(lambda x: x.ffill().bfill()).drop_duplicates() results in: id data_one data_two data_three 0 1 8.0 4.0 NaN 1 2 3.0 NaN NaN 1 3 NaN 4.0 100.0 2 4 NaN NaN NaN Note that it is gonna return separate rows for the positions where df1 and df2 both have a non-null values. Which is intentional since I don't know what you want to do with in such cases.
66,204,524
Heatmap using pandas dataframe
<p>I have a data frame with the following rows:</p> <pre><code> protein IHD CM ARR VD CHD CCD VOO 0 q9uku9 0.000000 0.039457 0.032901 0.014793 0.006614 0.006591 0.000000 1 o75461 0.000000 0.005832 0.027698 0.000000 0.000000 0.006634 0.000000 </code></pre> <p>etc.</p> <p>I want a heatmap of the disease names (IHD, CM, etc) on the X axis with the protein name on the y=axis. I'm running into a float issue since the protein column is in letters. On my heatmap, I want the protein name to show though, so it wouldn't be helpful to just drop the entire column. Does anyone have any tips on how to do this?</p>
66,204,750
"2021-02-15T08:08:50.243000"
2
null
1
80
python|pandas
<p>You can try to drop the protein column from the dataframe.</p> <p>Editing with the suggestion from the comments.</p> <p>Also, the solution encompasses the case where you have a large list of diseases.</p> <pre><code>import seaborn as sns list_proteins = df['protein'].unique() y = df[&quot;protein&quot;] df = df.drop(['protein'], axis=1) column_name_list = df.columns.tolist() sns.heatmap(df, xticklabels=column_name_list , yticklabels=list_proteins) </code></pre>
"2021-02-15T08:27:10.483000"
0
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.hexbin.html
pandas.DataFrame.plot.hexbin# pandas.DataFrame.plot.hexbin# DataFrame.plot.hexbin(x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs)[source]# You can try to drop the protein column from the dataframe. Editing with the suggestion from the comments. Also, the solution encompasses the case where you have a large list of diseases. import seaborn as sns list_proteins = df['protein'].unique() y = df["protein"] df = df.drop(['protein'], axis=1) column_name_list = df.columns.tolist() sns.heatmap(df, xticklabels=column_name_list , yticklabels=list_proteins) Generate a hexagonal binning plot. Generate a hexagonal binning plot of x versus y. If C is None (the default), this is a histogram of the number of occurrences of the observations at (x[i], y[i]). If C is specified, specifies values at given coordinates (x[i], y[i]). These values are accumulated for each hexagonal bin and then reduced according to reduce_C_function, having as default the NumPy’s mean function (numpy.mean()). (If C is specified, it must also be a 1-D sequence of the same length as x and y, or a column label.) Parameters xint or strThe column label or position for x points. yint or strThe column label or position for y points. Cint or str, optionalThe column label or position for the value of (x, y) point. reduce_C_functioncallable, default np.meanFunction of one argument that reduces all the values in a bin to a single number (e.g. np.mean, np.max, np.sum, np.std). gridsizeint or tuple of (int, int), default 100The number of hexagons in the x-direction. The corresponding number of hexagons in the y-direction is chosen in a way that the hexagons are approximately regular. Alternatively, gridsize can be a tuple with two elements specifying the number of hexagons in the x-direction and the y-direction. **kwargsAdditional keyword arguments are documented in DataFrame.plot(). Returns matplotlib.AxesSubplotThe matplotlib Axes on which the hexbin is plotted. See also DataFrame.plotMake plots of a DataFrame. matplotlib.pyplot.hexbinHexagonal binning plot using matplotlib, the matplotlib function that is used under the hood. Examples The following examples are generated with random data from a normal distribution. >>> n = 10000 >>> df = pd.DataFrame({'x': np.random.randn(n), ... 'y': np.random.randn(n)}) >>> ax = df.plot.hexbin(x='x', y='y', gridsize=20) The next example uses C and np.sum as reduce_C_function. Note that ‘observations’ values ranges from 1 to 5 but the result plot shows values up to more than 25. This is because of the reduce_C_function. >>> n = 500 >>> df = pd.DataFrame({ ... 'coord_x': np.random.uniform(-3, 3, size=n), ... 'coord_y': np.random.uniform(30, 50, size=n), ... 'observations': np.random.randint(1,5, size=n) ... }) >>> ax = df.plot.hexbin(x='coord_x', ... y='coord_y', ... C='observations', ... reduce_C_function=np.sum, ... gridsize=10, ... cmap="viridis")
158
574
Heatmap using pandas dataframe I have a data frame with the following rows: protein IHD CM ARR VD CHD CCD VOO 0 q9uku9 0.000000 0.039457 0.032901 0.014793 0.006614 0.006591 0.000000 1 o75461 0.000000 0.005832 0.027698 0.000000 0.000000 0.006634 0.000000 etc. I want a heatmap of the disease names (IHD, CM, etc) on the X axis with the protein name on the y=axis. I'm running into a float issue since the protein column is in letters. On my heatmap, I want the protein name to show though, so it wouldn't be helpful to just drop the entire column. Does anyone have any tips on how to do this?
You can try to drop the protein column from the dataframe. Editing with the suggestion from the comments. Also, the solution encompasses the case where you have a large list of diseases. import seaborn as sns list_proteins = df['protein'].unique() y = df["protein"] df = df.drop(['protein'], axis=1) column_name_list = df.columns.tolist() sns.heatmap(df, xticklabels=column_name_list , yticklabels=list_proteins)