如何将Panda类型的数据转换为Panda.Dataframe?

时间:2016-08-03 11:12:35

标签: python pandas dataframe

我有一个对象,其类型是Panda,print(对象)给出了输出

            print(type(recomen_total))
            print(recomen_total)

输出

<class 'pandas.core.frame.Pandas'>

Pandas(Index=12, instrument_1='XXXXXX', instrument_2='XXXX', trade_strategy='XXX', earliest_timestamp='2016-08-02T10:00:00+0530', latest_timestamp='2016-08-02T10:00:00+0530', xy_signal_count=1)

我想在pd.DataFrame中转换这个obejct,我该怎么做?

我试过pd.DataFrame(对象),from_dict也是,他们抛出错误

3 个答案:

答案 0 :(得分:2)

有趣的是,它不会直接转换为数据帧而是转换为系列。将其转换为系列后,使用系列的to_frame方法将其转换为DataFrame

import pandas as pd
df = pd.DataFrame({'col1': [1, 2], 'col2': [0.1, 0.2]},
                      index=['a', 'b'])

for row in df.itertuples():
    print(pd.Series(row).to_frame())

希望这会有所帮助!!

修改

如果要保存列名,请使用_asdict()方法,如下所示:

import pandas as pd
df = pd.DataFrame({'col1': [1, 2], 'col2': [0.1, 0.2]},
                      index=['a', 'b'])

for row in df.itertuples():
    d = dict(row._asdict())
    print(pd.Series(d).to_frame())

Output:
         0
Index    a
col1     1
col2   0.1
         0
Index    b
col1     2
col2   0.2

答案 1 :(得分:0)

要从名为tuple的项创建新的DataFrame,也可以使用list()或Series:

import pandas as pd

# source DataFrame
df = pd.DataFrame({'a': [1,2], 'b':[3,4]})
# empty DataFrame
df_new_fromAppend = pd.DataFrame(columns=['x','y'], data=None)

for r in df.itertuples():
    # create new DataFrame from itertuples() via list() ([1:] for skipping the index):
    df_new_fromList = pd.DataFrame([list(r)[1:]], columns=['c','d'])
    # or create new DataFrame from itertuples() via Series (drop(0) to remove index, T to transpose column to row) 
    df_new_fromSeries = pd.DataFrame(pd.Series(r).drop(0)).T
    # or use append() to insert row into existing DataFrame ([1:] for skipping the index):
    df_new_fromAppend.loc[df_new_fromAppend.shape[0]] = list(r)[1:]

print('df_new_fromList:')
print(df_new_fromList, '\n')
print('df_new_fromSeries:')
print(df_new_fromSeries, '\n')
print('df_new_fromAppend:')
print(df_new_fromAppend, '\n')

输出:

df_new_fromList:
   c  d
0  2  4 

df_new_fromSeries:
   1  2
0  2  4 

df_new_fromAppend:
   x  y
0  1  3
1  2  4 

要省略索引,请使用param index = False(但我最需要迭代的索引)

for r in df.itertuples(index=False):
    # the [1:] needn't be used, for example:
    df_new_fromAppend.loc[df_new_fromAppend.shape[0]] = list(r) 

答案 2 :(得分:0)

以下对我有用:

import pandas as pd
df = pd.DataFrame({'col1': [1, 2], 'col2': [0.1, 0.2]}, index=['a', 'b'])

for row in df.itertuples():    
   row_as_df = pd.DataFrame.from_records([row], columns=row._fields)
   print(row_as_df)

结果是:

  Index  col1  col2
0     a     1   0.1
  Index  col1  col2
0     b     2   0.2

不幸的是,AFAIU没有简单的方法来保留列名,而无需显式使用_fields之类的“受保护的属性”。