pandas


Pandas dataframe without copy


How can I avoid taking a copy of the dictionary supplied when creating a Pandas DataFrame?
>>> a = np.arange(10)
>>> b = np.arange(10.0)
>>> df1 = pd.DataFrame(a)
>>> a[0] = 100
>>> df1
0
0 100
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
>>> d = {'a':a, 'b':b}
>>> df2 = pd.DataFrame(d)
>>> a[1] = 200
>>> d
{'a': array([100, 200, 2, 3, 4, 5, 6, 7, 8, 9]), 'b': array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])}
>>> df2
a b
0 100 0
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
If I create the dataframe from just a then changes to a are reflected in df (and vice versa).
Is there any way of making this work when supplying a dictionary?
There is no way to 'share' a dict and have the frame update based on the dict changes. The copy argument is not relevant for a dict, data is always copied, because it is transformed to an ndarray.
However, there is a way to get this type of dynamic behavior in a limited way.
In [9]: arr = np.array(np.random.rand(5,2))
In [10]: df = DataFrame(arr)
In [11]: arr[0,0] = 0
In [12]: df
Out[12]:
0 1
0 0.000000 0.192056
1 0.847185 0.609028
2 0.833997 0.422521
3 0.937638 0.711856
4 0.047569 0.033282
Thus a passed ndarray will at construction time be a view onto the underlying numpy array. Depending on how you operate on the DataFrame you could trigger a copy (e.g. if you assign say a new column, or change a columns dtype). This will also only work for a single dtyped frame.

Related Links

Copy numpy array into Panda multiindex (same size)
Average Previous and Current Row - Pandas
seaborn pointplot above swarmplot
Edit field and append value to a python dataframe column
column_stack returns non cotiguous array
pandas: conditionally select a row cell for each column based on a mask
pandas custom function apply on melted dataframe
How to check for boolean codition in pandas dataframe
Reading batches of data from BigQuery into Datalab
Jupyter/ipywidgets sorting dataframe on two levels
Groupby.sum() giving ValueError: overflow in timedelta operation
Why does DataFrameGroupBy.boxplot method throw error when given argument “subplots=True/False”?
Calculate age in months - optimize date transformations in pandas
pandas: list of dictionaries grouped by key from df
Pandas data frames and matplotlib.pyplot
Pandas.to_csv thousand separator

Categories

HOME
maven
cakephp
google-chrome-extension
image-processing
deezer
pycharm
path-finding
relative-path
appx
adb
yarn
sqlite3
packages
acquia
append
amazon-cloudformation
cloudkit
windows-10-universal
numeral.js
quicklook
quickbooks
windows-phone-7
clojurescript
fatal-error
google-static-maps
reactive-cocoa
oracle-coherence
interop
text-rendering
errorlevel
facebook-instant-articles
wijmo
java-7
intel-pin
entitlements
large-file-upload
.net-4.0
copying
subset-sum
language-concepts
devextreme
serve
jquery-bootgrid
impersonation
webdriver-manager
splice
eclipse-scout
glew
estimote
http-redirect
angular-resource
pubmed
dandelion
blogengine.net
tropo
libusb-win32
boost-preprocessor
qtwebview
mako
jspdf-autotable
pycaffe
passport-google-oauth
dstu2-fhir
ctest
xml-attribute
bstr
sigabrt
python-stackless
nessus
freedesktop.org
rtbkit
python-3.2
createprocessasuser
generic-programming
sonarqube5.1.2
truevault
fscommand
jscript.net
gadt
issuu
onactivityresult
eol
fluentautomation
jubula
jboss-weld
datagridviewcolumn
flexmojos
quantlib-swig
blending
bulkloader
file-locking
html-editor
frameset
jspinclude
buildr
work-stealing
window-management
gnustep
mod-auth
locationlistener
appender
recent-documents
remember-me
cxxtest
subviews
nhibernate.search
ctp4
mtj
lzh

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App