Skip to content

Commit 8488233

Browse files
anetakahlelofcz
andcommitted
DOC: Improve reshape\concat
Co-Authored-By: Matěj Štágl <[email protected]>
1 parent 9dfef3a commit 8488233

File tree

2 files changed

+222
-11
lines changed

2 files changed

+222
-11
lines changed

pandas/core/reshape/concat.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -347,19 +347,17 @@ def concat(
347347
348348
Append a single row to the end of a ``DataFrame`` object.
349349
350-
>>> a = pd.DataFrame({"A": 1, "B": 2}, index=[0])
351-
>>> a
352-
A B
350+
>>> df7 = pd.DataFrame({'a': 1, 'b': 2}, index=[0])
351+
>>> df7
352+
a b
353353
0 1 2
354-
>>> b = pd.DataFrame({"A": 3}, index=[0])
355-
>>> b
356-
A
354+
>>> new_row = pd.Series([3])
355+
>>> new_row
357356
0 3
358-
>>> for rowIndex, row in b.iterrows():
359-
>>> print(pd.concat([a, row.to_frame().T], ignore_index=True))
360-
A B
361-
0 1 2.0
362-
1 3 NaN
357+
>>> pd.concat([df7, new_row.to_frame().T], ignore_index=True)
358+
a b 0
359+
0 1.0 2.0 NaN
360+
1 NaN NaN 3.0
363361
"""
364362
op = _Concatenator(
365363
objs,

pandas/core/reshape/exaple.ipynb

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"source": [
6+
"It is not recomended to build DataFrames by adding single rows in a\n",
7+
"not loop. Build a list of rows and make a DataFrame in a single concat.\n",
8+
"\n"
9+
],
10+
"metadata": {
11+
"collapsed": false,
12+
"pycharm": {
13+
"name": "#%% md\n"
14+
}
15+
}
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 22,
20+
"outputs": [],
21+
"source": [
22+
"import pandas as pd"
23+
],
24+
"metadata": {
25+
"collapsed": false,
26+
"pycharm": {
27+
"name": "#%%\n"
28+
}
29+
}
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 23,
34+
"outputs": [
35+
{
36+
"data": {
37+
"text/plain": " A B\n0 1 2",
38+
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>A</th>\n <th>B</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>1</td>\n <td>2</td>\n </tr>\n </tbody>\n</table>\n</div>"
39+
},
40+
"execution_count": 23,
41+
"metadata": {},
42+
"output_type": "execute_result"
43+
}
44+
],
45+
"source": [
46+
"a = pd.DataFrame({\"A\": 1, \"B\": 2}, index=[0])\n",
47+
"a"
48+
],
49+
"metadata": {
50+
"collapsed": false,
51+
"pycharm": {
52+
"name": "#%%\n"
53+
}
54+
}
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": 24,
59+
"outputs": [
60+
{
61+
"data": {
62+
"text/plain": "0 3\ndtype: int64"
63+
},
64+
"execution_count": 24,
65+
"metadata": {},
66+
"output_type": "execute_result"
67+
}
68+
],
69+
"source": [
70+
"new_row = pd.Series([3])\n",
71+
"new_row"
72+
],
73+
"metadata": {
74+
"collapsed": false,
75+
"pycharm": {
76+
"name": "#%%\n"
77+
}
78+
}
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": 25,
83+
"outputs": [
84+
{
85+
"data": {
86+
"text/plain": " A B 0\n0 1.0 2.0 NaN\n1 NaN NaN 3.0",
87+
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>A</th>\n <th>B</th>\n <th>0</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>1.0</td>\n <td>2.0</td>\n <td>NaN</td>\n </tr>\n <tr>\n <th>1</th>\n <td>NaN</td>\n <td>NaN</td>\n <td>3.0</td>\n </tr>\n </tbody>\n</table>\n</div>"
88+
},
89+
"execution_count": 25,
90+
"metadata": {},
91+
"output_type": "execute_result"
92+
}
93+
],
94+
"source": [
95+
"pd.concat([a, new_row.to_frame().T], ignore_index=True)"
96+
],
97+
"metadata": {
98+
"collapsed": false,
99+
"pycharm": {
100+
"name": "#%%\n"
101+
}
102+
}
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": 25,
107+
"outputs": [],
108+
"source": [],
109+
"metadata": {
110+
"collapsed": false,
111+
"pycharm": {
112+
"name": "#%%\n"
113+
}
114+
}
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 25,
119+
"outputs": [],
120+
"source": [],
121+
"metadata": {
122+
"collapsed": false,
123+
"pycharm": {
124+
"name": "#%%\n"
125+
}
126+
}
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": 25,
131+
"outputs": [],
132+
"source": [],
133+
"metadata": {
134+
"collapsed": false,
135+
"pycharm": {
136+
"name": "#%%\n"
137+
}
138+
}
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": 25,
143+
"outputs": [],
144+
"source": [],
145+
"metadata": {
146+
"collapsed": false,
147+
"pycharm": {
148+
"name": "#%%\n"
149+
}
150+
}
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": 25,
155+
"outputs": [],
156+
"source": [],
157+
"metadata": {
158+
"collapsed": false,
159+
"pycharm": {
160+
"name": "#%%\n"
161+
}
162+
}
163+
},
164+
{
165+
"cell_type": "code",
166+
"execution_count": 26,
167+
"outputs": [],
168+
"source": [
169+
"# b = pd.DataFrame({\"A\": 3}, index=[0])\n",
170+
"# b"
171+
],
172+
"metadata": {
173+
"collapsed": false,
174+
"pycharm": {
175+
"name": "#%%\n"
176+
}
177+
}
178+
},
179+
{
180+
"cell_type": "code",
181+
"execution_count": 26,
182+
"outputs": [],
183+
"source": [],
184+
"metadata": {
185+
"collapsed": false,
186+
"pycharm": {
187+
"name": "#%%\n"
188+
}
189+
}
190+
}
191+
],
192+
"metadata": {
193+
"kernelspec": {
194+
"display_name": "Python 3",
195+
"language": "python",
196+
"name": "python3"
197+
},
198+
"language_info": {
199+
"codemirror_mode": {
200+
"name": "ipython",
201+
"version": 2
202+
},
203+
"file_extension": ".py",
204+
"mimetype": "text/x-python",
205+
"name": "python",
206+
"nbconvert_exporter": "python",
207+
"pygments_lexer": "ipython2",
208+
"version": "2.7.6"
209+
}
210+
},
211+
"nbformat": 4,
212+
"nbformat_minor": 0
213+
}

0 commit comments

Comments
 (0)