本文整理匯總了Python中pandas.core.frame.DataFrame.set_index方法的典型用法代碼示例。如果您正苦於以下問題:Python DataFrame.set_index方法的具體用法?Python DataFrame.set_index怎麽用?Python DataFrame.set_index使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pandas.core.frame.DataFrame
的用法示例。
在下文中一共展示了DataFrame.set_index方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_column_order_plus_index
# 需要導入模塊: from pandas.core.frame import DataFrame [as 別名]
# 或者: from pandas.core.frame.DataFrame import set_index [as 別名]
def test_column_order_plus_index(self):
query = "SELECT 'a' as STRING_1, 'b' as STRING_2, 'c' as STRING_3"
col_order = ['STRING_3', 'STRING_2']
result_frame = gbq.read_gbq(query, project_id=PROJECT_ID, index_col='STRING_1', col_order=col_order)
correct_frame = DataFrame({'STRING_1' : ['a'], 'STRING_2' : ['b'], 'STRING_3' : ['c']})
correct_frame.set_index('STRING_1', inplace=True)
correct_frame = correct_frame[col_order]
tm.assert_frame_equal(result_frame, correct_frame)
示例2: test_column_order_plus_index
# 需要導入模塊: from pandas.core.frame import DataFrame [as 別名]
# 或者: from pandas.core.frame.DataFrame import set_index [as 別名]
def test_column_order_plus_index(self):
query = "SELECT 'a' as STRING_1, 'b' as STRING_2, 'c' as STRING_3"
col_order = ["STRING_3", "STRING_2"]
result_frame = gbq.read_gbq(query, project_id=PROJECT_ID, index_col="STRING_1", col_order=col_order)
correct_frame = DataFrame({"STRING_1": ["a"], "STRING_2": ["b"], "STRING_3": ["c"]})
correct_frame.set_index("STRING_1", inplace=True)
correct_frame = correct_frame[col_order]
tm.assert_frame_equal(result_frame, correct_frame)
示例3: test_column_order_plus_index
# 需要導入模塊: from pandas.core.frame import DataFrame [as 別名]
# 或者: from pandas.core.frame.DataFrame import set_index [as 別名]
def test_column_order_plus_index(self):
# A User should be able to specify an index and the order of THE REMAINING columns.. they should be notified
# if they screw up
col_order = ['corpus_date', 'word', 'corpus']
result_frame = gbq._parse_data(FakeClient(), self.fake_job, index_col='word_count', col_order=col_order)
correct_frame_small = DataFrame(self.correct_data_small)
correct_frame_small.set_index('word_count',inplace=True)
correct_frame_small = DataFrame(correct_frame_small)[col_order]
tm.assert_index_equal(result_frame.columns, correct_frame_small.columns)
示例4: test_index_column
# 需要導入模塊: from pandas.core.frame import DataFrame [as 別名]
# 或者: from pandas.core.frame.DataFrame import set_index [as 別名]
def test_index_column(self):
# A user should be able to specify an index column for return
result_frame = gbq._parse_data(FakeClient(), self.fake_job, index_col='word')
correct_frame = DataFrame(self.correct_data_small)
correct_frame.set_index('word', inplace=True)
self.assertTrue(result_frame.index.name == correct_frame.index.name)
示例5: tee
# 需要導入模塊: from pandas.core.frame import DataFrame [as 別名]
# 或者: from pandas.core.frame.DataFrame import set_index [as 別名]
stream = (line.decode('cp1251').strip().encode('utf-8')
for line in stdin)
# tee the stream to get the metadata for title
stream, stream_2 = tee(stream)
title = get_metadata(stream_2)['TITLE']
df = DataFrame()
for cur_data in iter_contextual_atom_data(stream):
current = DataFrame.from_dict([cur_data])
df = df.append(current, ignore_index=False)
index_cols = list(df.columns.values)
index_cols.remove('value')
df.set_index(index_cols, inplace=True)
df.columns = [title]
# create removable temp file for use with HDFStore
tmpfile = NamedTemporaryFile().name
store = HDFStore(tmpfile)
store['default'] = df
store.close()
# put h5 file to stdout
with open(tmpfile, 'rb') as f:
print f.read()
# temp file is automatically removed