本文整理汇总了Python中pandas.core.frame.DataFrame方法的典型用法代码示例。如果您正苦于以下问题:Python frame.DataFrame方法的具体用法?Python frame.DataFrame怎么用?Python frame.DataFrame使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.core.frame
的用法示例。
在下文中一共展示了frame.DataFrame方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from pandas.core import frame [as 别名]
# 或者: from pandas.core.frame import DataFrame [as 别名]
def run(self):
self.sem.acquire()
while datetime.now() < self.timeout:
try:
# Randomy length dataframe to keep appending to
df = DataFrame({'v': [self.last]}, [datetime.now()])
for i in range(random.randint(1, 10)):
df = df.append(DataFrame({'v': [self.last + i]}, [datetime.now()]))
self.last + i
df.index.name = 'index'
self.lib.append('symbol', df)
assert self.last in self.lib.read('symbol').data['v'].tolist()
self.last += 2
except OptimisticLockException:
# Concurrent write, not successful
pass
# time.sleep(self.begin)
示例2: unstack
# 需要导入模块: from pandas.core import frame [as 别名]
# 或者: from pandas.core.frame import DataFrame [as 别名]
def unstack(obj, level, fill_value=None):
if isinstance(level, (tuple, list)):
if len(level) != 1:
# _unstack_multiple only handles MultiIndexes,
# and isn't needed for a single level
return _unstack_multiple(obj, level, fill_value=fill_value)
else:
level = level[0]
if isinstance(obj, DataFrame):
if isinstance(obj.index, MultiIndex):
return _unstack_frame(obj, level, fill_value=fill_value)
else:
return obj.T.stack(dropna=False)
else:
if is_extension_array_dtype(obj.dtype):
return _unstack_extension_series(obj, level, fill_value)
unstacker = _Unstacker(obj.values, obj.index, level=level,
fill_value=fill_value,
constructor=obj._constructor_expanddim)
return unstacker.get_result()
示例3: _validate_names
# 需要导入模块: from pandas.core import frame [as 别名]
# 或者: from pandas.core.frame import DataFrame [as 别名]
def _validate_names(names):
"""
Check if the `names` parameter contains duplicates.
If duplicates are found, we issue a warning before returning.
Parameters
----------
names : array-like or None
An array containing a list of the names used for the output DataFrame.
Returns
-------
names : array-like or None
The original `names` parameter.
"""
if names is not None:
if len(names) != len(set(names)):
msg = ("Duplicate names specified. This "
"will raise an error in the future.")
warnings.warn(msg, UserWarning, stacklevel=3)
return names
示例4: read
# 需要导入模块: from pandas.core import frame [as 别名]
# 或者: from pandas.core.frame import DataFrame [as 别名]
def read(self, nrows=None):
nrows = _validate_integer('nrows', nrows)
ret = self._engine.read(nrows)
# May alter columns / col_dict
index, columns, col_dict = self._create_index(ret)
if index is None:
if col_dict:
# Any column is actually fine:
new_rows = len(compat.next(compat.itervalues(col_dict)))
index = RangeIndex(self._currow, self._currow + new_rows)
else:
new_rows = 0
else:
new_rows = len(index)
df = DataFrame(col_dict, columns=columns, index=index)
self._currow += new_rows
if self.squeeze and len(df.columns) == 1:
return df[df.columns[0]].copy()
return df
示例5: _get_series_result_type
# 需要导入模块: from pandas.core import frame [as 别名]
# 或者: from pandas.core.frame import DataFrame [as 别名]
def _get_series_result_type(result, objs=None):
"""
return appropriate class of Series concat
input is either dict or array-like
"""
# concat Series with axis 1
if isinstance(result, dict):
# concat Series with axis 1
if all(is_sparse(c) for c in compat.itervalues(result)):
from pandas.core.sparse.api import SparseDataFrame
return SparseDataFrame
else:
from pandas.core.frame import DataFrame
return DataFrame
# otherwise it is a SingleBlockManager (axis = 0)
if result._block.is_sparse:
from pandas.core.sparse.api import SparseSeries
return SparseSeries
else:
return objs[0]._constructor
示例6: _get_sliced_frame_result_type
# 需要导入模块: from pandas.core import frame [as 别名]
# 或者: from pandas.core.frame import DataFrame [as 别名]
def _get_sliced_frame_result_type(data, obj):
"""
return appropriate class of Series. When data is sparse
it will return a SparseSeries, otherwise it will return
the Series.
Parameters
----------
data : array-like
obj : DataFrame
Returns
-------
Series or SparseSeries
"""
if is_sparse(data):
from pandas.core.sparse.api import SparseSeries
return SparseSeries
return obj._constructor_sliced
示例7: major_xs
# 需要导入模块: from pandas.core import frame [as 别名]
# 或者: from pandas.core.frame import DataFrame [as 别名]
def major_xs(self, key):
"""
Return slice of panel along major axis
Parameters
----------
key : object
Major axis label
Returns
-------
y : DataFrame
index -> minor axis, columns -> items
"""
slices = dict((k, v.xs(key)) for k, v in compat.iteritems(self))
return DataFrame(slices, index=self.minor_axis, columns=self.items)
示例8: forecast
# 需要导入模块: from pandas.core import frame [as 别名]
# 或者: from pandas.core.frame import DataFrame [as 别名]
def forecast(self, h):
"""
Returns a DataFrame containing the forecasts for 1, 2, ..., n time
steps. Each column x1 contains the forecasts of the x1 column.
Parameters
----------
n: int
Number of time steps ahead to forecast.
Returns
-------
DataFrame
"""
forecast = self._forecast_raw(h)[:, 0, :]
return DataFrame(forecast, index=lrange(1, 1 + h),
columns=self._columns)
示例9: lag_select
# 需要导入模块: from pandas.core import frame [as 别名]
# 或者: from pandas.core.frame import DataFrame [as 别名]
def lag_select(data, max_lags=5, ic=None):
"""
Select number of lags based on a variety of information criteria
Parameters
----------
data : DataFrame-like
max_lags : int
Maximum number of lags to evaluate
ic : {None, 'aic', 'bic', ...}
Choosing None will just display the results
Returns
-------
None
"""
pass
示例10: _convertDummies
# 需要导入模块: from pandas.core import frame [as 别名]
# 或者: from pandas.core.frame import DataFrame [as 别名]
def _convertDummies(dummies, mapping):
# cleans up the names of the generated dummies
new_items = []
for item in dummies.columns:
if not mapping:
var = str(item)
if isinstance(item, float):
var = '%g' % item
new_items.append(var)
else:
# renames the dummies if a conversion dict is provided
new_items.append(mapping[int(item)])
dummies = DataFrame(dummies.values, index=dummies.index,
columns=new_items)
return dummies
示例11: add_intercept
# 需要导入模块: from pandas.core import frame [as 别名]
# 或者: from pandas.core.frame import DataFrame [as 别名]
def add_intercept(panel, name='intercept'):
"""
Add column of ones to input panel
Parameters
----------
panel: Panel / DataFrame
name: string, default 'intercept']
Returns
-------
New object (same type as input)
"""
panel = panel.copy()
panel[name] = 1.
return panel.consolidate()
示例12: get_result
# 需要导入模块: from pandas.core import frame [as 别名]
# 或者: from pandas.core.frame import DataFrame [as 别名]
def get_result(self):
join_index, left_indexer, right_indexer = self._get_join_info()
# this is a bit kludgy
ldata, rdata = self._get_merge_data()
# TODO: more efficiently handle group keys to avoid extra
# consolidation!
join_op = _BlockJoinOperation([ldata, rdata], join_index,
[left_indexer, right_indexer], axis=1,
copy=self.copy)
result_data = join_op.get_result()
result = DataFrame(result_data)
self._maybe_add_join_keys(result, left_indexer, right_indexer)
return result
示例13: get_group
# 需要导入模块: from pandas.core import frame [as 别名]
# 或者: from pandas.core.frame import DataFrame [as 别名]
def get_group(self, name, obj=None):
"""
Constructs NDFrame from group with provided name
Parameters
----------
name : object
the name of the group to get as a DataFrame
obj : NDFrame, default None
the NDFrame to take the DataFrame out of. If
it is None, the object groupby was called on will
be used
Returns
-------
group : type of obj
"""
if obj is None:
obj = self.obj
inds = self._get_index(name)
return obj.take(inds, axis=self.axis, convert=False)
示例14: _aggregate_item_by_item
# 需要导入模块: from pandas.core import frame [as 别名]
# 或者: from pandas.core.frame import DataFrame [as 别名]
def _aggregate_item_by_item(self, func, *args, **kwargs):
# only for axis==0
obj = self._obj_with_exclusions
result = {}
cannot_agg = []
for item in obj:
try:
data = obj[item]
colg = SeriesGroupBy(data, selection=item,
grouper=self.grouper)
result[item] = self._try_cast(
colg.aggregate(func, *args, **kwargs), data)
except ValueError:
cannot_agg.append(item)
continue
result_columns = obj.columns
if cannot_agg:
result_columns = result_columns.drop(cannot_agg)
return DataFrame(result, columns=result_columns)
示例15: _transform_item_by_item
# 需要导入模块: from pandas.core import frame [as 别名]
# 或者: from pandas.core.frame import DataFrame [as 别名]
def _transform_item_by_item(self, obj, wrapper):
# iterate through columns
output = {}
inds = []
for i, col in enumerate(obj):
try:
output[col] = self[col].transform(wrapper)
inds.append(i)
except Exception:
pass
if len(output) == 0: # pragma: no cover
raise TypeError('Transform function invalid for data types')
columns = obj.columns
if len(output) < len(obj.columns):
columns = columns.take(inds)
return DataFrame(output, index=obj.index, columns=columns)