本文整理汇总了Python中pandas.DataFrame.from_dict方法的典型用法代码示例。如果您正苦于以下问题:Python DataFrame.from_dict方法的具体用法?Python DataFrame.from_dict怎么用?Python DataFrame.from_dict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.DataFrame
的用法示例。
在下文中一共展示了DataFrame.from_dict方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_reader_seconds
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_dict [as 别名]
def test_reader_seconds(self, ext):
# Test reading times with and without milliseconds. GH5945.
expected = DataFrame.from_dict({"Time": [time(1, 2, 3),
time(2, 45, 56, 100000),
time(4, 29, 49, 200000),
time(6, 13, 42, 300000),
time(7, 57, 35, 400000),
time(9, 41, 28, 500000),
time(11, 25, 21, 600000),
time(13, 9, 14, 700000),
time(14, 53, 7, 800000),
time(16, 37, 0, 900000),
time(18, 20, 54)]})
actual = self.get_exceldf('times_1900', ext, 'Sheet1')
tm.assert_frame_equal(actual, expected)
actual = self.get_exceldf('times_1904', ext, 'Sheet1')
tm.assert_frame_equal(actual, expected)
示例2: test_reader_converters
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_dict [as 别名]
def test_reader_converters(self, ext):
basename = 'test_converters'
expected = DataFrame.from_dict(OrderedDict([
("IntCol", [1, 2, -3, -1000, 0]),
("FloatCol", [12.5, np.nan, 18.3, 19.2, 0.000000005]),
("BoolCol", ['Found', 'Found', 'Found', 'Not found', 'Found']),
("StrCol", ['1', np.nan, '3', '4', '5']),
]))
converters = {'IntCol': lambda x: int(x) if x != '' else -1000,
'FloatCol': lambda x: 10 * x if x else np.nan,
2: lambda x: 'Found' if x != '' else 'Not found',
3: lambda x: str(x) if x else '',
}
# should read in correctly and set types of single cells (not array
# dtypes)
actual = self.get_exceldf(basename, ext, 'Sheet1',
converters=converters)
tm.assert_frame_equal(actual, expected)
示例3: result
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_dict [as 别名]
def result(self):
# 根据daily_life里面的数据 获取最后的结果
result = defaultdict(list)
for daily in self.daily_life.values():
for key, value in daily.items():
result[key].append(value)
df = DataFrame.from_dict(result).set_index("date")
try:
import matplotlib.pyplot as plt
df['balance'].plot()
plt.show()
except ImportError as e:
pass
finally:
return self._cal_result(df)
示例4: test_frame_dict_constructor_empty_series
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_dict [as 别名]
def test_frame_dict_constructor_empty_series(self):
s1 = Series([
1, 2, 3, 4
], index=MultiIndex.from_tuples([(1, 2), (1, 3), (2, 2), (2, 4)]))
s2 = Series([
1, 2, 3, 4
], index=MultiIndex.from_tuples([(1, 2), (1, 3), (3, 2), (3, 4)]))
s3 = Series()
# it works!
DataFrame({'foo': s1, 'bar': s2, 'baz': s3})
DataFrame.from_dict({'foo': s1, 'baz': s3, 'bar': s2})
示例5: test_get_dummies_dont_sparsify_all_columns
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_dict [as 别名]
def test_get_dummies_dont_sparsify_all_columns(self, sparse):
# GH18914
df = DataFrame.from_dict(OrderedDict([('GDP', [1, 2]),
('Nation', ['AB', 'CD'])]))
df = get_dummies(df, columns=['Nation'], sparse=sparse)
df2 = df.reindex(columns=['GDP'])
tm.assert_frame_equal(df[['GDP']], df2)
示例6: test_to_dict_index_dtypes
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_dict [as 别名]
def test_to_dict_index_dtypes(self, into, expected):
# GH 18580
# When using to_dict(orient='index') on a dataframe with int
# and float columns only the int columns were cast to float
df = DataFrame({'int_col': [1, 2, 3],
'float_col': [1.0, 2.0, 3.0]})
result = df.to_dict(orient='index', into=into)
cols = ['int_col', 'float_col']
result = DataFrame.from_dict(result, orient='index')[cols]
expected = DataFrame.from_dict(expected, orient='index')[cols]
tm.assert_frame_equal(result, expected)
示例7: test_scientific_no_exponent
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_dict [as 别名]
def test_scientific_no_exponent(all_parsers):
# see gh-12215
df = DataFrame.from_dict(OrderedDict([("w", ["2e"]), ("x", ["3E"]),
("y", ["42e"]),
("z", ["632E"])]))
data = df.to_csv(index=False)
parser = all_parsers
for precision in parser.float_precision_choices:
df_roundtrip = parser.read_csv(StringIO(data),
float_precision=precision)
tm.assert_frame_equal(df_roundtrip, df)
示例8: test_scientific_no_exponent
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_dict [as 别名]
def test_scientific_no_exponent(self):
# see gh-12215
df = DataFrame.from_dict(OrderedDict([('w', ['2e']), ('x', ['3E']),
('y', ['42e']),
('z', ['632E'])]))
data = df.to_csv(index=False)
for prec in self.float_precision_choices:
df_roundtrip = self.read_csv(
StringIO(data), float_precision=prec)
tm.assert_frame_equal(df_roundtrip, df)
示例9: test_merge_nosort
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_dict [as 别名]
def test_merge_nosort(self):
# #2098, anything to do?
from datetime import datetime
d = {"var1": np.random.randint(0, 10, size=10),
"var2": np.random.randint(0, 10, size=10),
"var3": [datetime(2012, 1, 12), datetime(2011, 2, 4),
datetime(
2010, 2, 3), datetime(2012, 1, 12),
datetime(
2011, 2, 4), datetime(2012, 4, 3),
datetime(
2012, 3, 4), datetime(2008, 5, 1),
datetime(2010, 2, 3), datetime(2012, 2, 3)]}
df = DataFrame.from_dict(d)
var3 = df.var3.unique()
var3.sort()
new = DataFrame.from_dict({"var3": var3,
"var8": np.random.random(7)})
result = df.merge(new, on="var3", sort=False)
exp = merge(df, new, on='var3', sort=False)
assert_frame_equal(result, exp)
self.assert_((df.var3.unique() == result.var3.unique()).all())
示例10: test_panel_join_many
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_dict [as 别名]
def test_panel_join_many(self):
tm.K = 10
panel = tm.makePanel()
tm.K = 4
panels = [panel.ix[:2], panel.ix[2:6], panel.ix[6:]]
joined = panels[0].join(panels[1:])
tm.assert_panel_equal(joined, panel)
panels = [panel.ix[:2, :-5], panel.ix[2:6, 2:], panel.ix[6:, 5:-7]]
data_dict = {}
for p in panels:
data_dict.update(compat.iteritems(p))
joined = panels[0].join(panels[1:], how='inner')
expected = Panel.from_dict(data_dict, intersect=True)
tm.assert_panel_equal(joined, expected)
joined = panels[0].join(panels[1:], how='outer')
expected = Panel.from_dict(data_dict, intersect=False)
tm.assert_panel_equal(joined, expected)
# edge cases
self.assertRaises(ValueError, panels[0].join, panels[1:],
how='outer', lsuffix='foo', rsuffix='bar')
self.assertRaises(ValueError, panels[0].join, panels[1:],
how='right')
示例11: load_log
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_dict [as 别名]
def load_log(name):
log = cPickle.load(open(name + "_log.zip"))
models["log_" + name] = log
df = DataFrame.from_dict(log, orient='index')
models["df_" + name] = df
print "Iterations done for {}: {}".format(name, log.status['iterations_done'])
print "Average batch time for {} was {}".format(
name, df.time_train_this_batch.mean())
print "Best PER: {}".format(log.status.get('best_valid_per', '?'))
示例12: do
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_dict [as 别名]
def do(self, which_callback, *args):
df = DataFrame.from_dict(self.experiment_params, orient='index')
df.to_hdf(os.path.join(self.dir, 'params'), 'params', mode='w',
complevel=5, complib='blosc')
示例13: get_realizations
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_dict [as 别名]
def get_realizations(self, func, n=None, name=None, **kwargs):
"""Internal method to obtain n number of realizations."""
if name:
kwargs["name"] = name
params = self.get_parameter_sample(n=n, name=name)
data = {}
for i, param in enumerate(params):
data[i] = func(parameters=param, **kwargs)
return DataFrame.from_dict(data, orient="columns")
示例14: read_umi_tools
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_dict [as 别名]
def read_umi_tools(filename: PathLike, dtype: str = "float32") -> AnnData:
"""\
Read a gzipped condensed count matrix from umi_tools.
Parameters
----------
filename
File name to read from.
"""
# import pandas for conversion of a dict of dicts into a matrix
# import gzip to read a gzipped file :-)
import gzip
from pandas import DataFrame
dod = {} # this will contain basically everything
fh = gzip.open(fspath(filename))
header = fh.readline() # read the first line
for line in fh:
# gzip read bytes, hence the decoding
t = line.decode("ascii").split("\t")
try:
dod[t[1]].update({t[0]: int(t[2])})
except KeyError:
dod[t[1]] = {t[0]: int(t[2])}
df = DataFrame.from_dict(dod, orient="index") # build the matrix
df.fillna(value=0.0, inplace=True) # many NaN, replace with zeros
return AnnData(
np.array(df), dict(obs_names=df.index), dict(var_names=df.columns), dtype=dtype,
)
示例15: _add_item_to_sqlite
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_dict [as 别名]
def _add_item_to_sqlite(dbcon, item):
# modify item info to prep for appending to sqlite table
item_info = copy.deepcopy(item)
item_info['largeImage'] = str(item_info['largeImage'])
item_info_dtypes = {
'_id': String(),
'_modelType': String(),
'baseParentId': String(),
'baseParentType': String(),
'copyOfItem': String(),
'created': String(),
'creatorId': String(),
'description': String(),
'folderId': String(),
'largeImage': String(),
'name': String(),
'size': Integer(),
'updated': String(),
}
# in case anything is not in the schema, drop it
item_info = {
k: v for k, v in item_info.items()
if k in item_info_dtypes.keys()}
# convert to df and add to items table
item_info_df = DataFrame.from_dict(item_info, orient='index').T
item_info_df.to_sql(
name='items', con=dbcon, if_exists='append',
dtype=item_info_dtypes, index=False)