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

ia2d9nvy  于 2022-12-25  发布在  其他
关注(0)|答案(5)|浏览(275)

我有一个类型为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,怎么做呢?
我尝试了pd.DataFrame(对象)和from_dict,但它们仍抛出错误

xmjla07d

xmjla07d1#

有趣的是,它不会直接转换为 Dataframe ,而是转换为系列。一旦转换为系列,请使用系列的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
z0qdvdin

z0qdvdin2#

要从itertuple namedtuple创建新的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

要省略index,请使用param index = False(但我大多数情况下都需要index用于迭代)

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)
pzfprimi

pzfprimi3#

以下是我的工作:

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这样的"受保护属性",就没有简单的方法来保留列名。

ycl3bljg

ycl3bljg4#

在@伊戈尔的回答中做了一些调整
最后,我得到了这个令人满意的代码,它保留了列名,并尽可能少地使用Pandas代码。

import pandas as pd
df = pd.DataFrame({'col1': [1, 2], 'col2': [0.1, 0.2]})
# Or initialize another dataframe above

# Get list of column names
column_names = df.columns.values.tolist()

filtered_rows = []
for row in df.itertuples(index=False):
   # Some code logic to filter rows
   filtered_rows.append(row)

# Convert pandas.core.frame.Pandas to pandas.core.frame.Dataframe
# Combine filtered rows into a single dataframe
concatinated_df = pd.DataFrame.from_records(filtered_rows, columns=column_names)

concatinated_df.to_csv("path_to_csv", index=False)

结果是包含以下内容的csv:

col1  col2
   1   0.1
   2   0.2
esbemjvw

esbemjvw5#

要将Pandas .itertuples返回的对象列表转换为DataFrame,同时保留列名:

# Example source DF
data = [['cheetah', 120], ['human', 44.72], ['dragonfly', 54]]
source_df = pd.DataFrame(data, columns=['animal', 'top_speed'])

      animal  top_speed
0  cheetah       120.00
1  human          44.72
2  dragonfly      54.00

由于Pandas不建议通过在for循环中添加单行来构建DataFrame,因此我们将在最后迭代并构建DataFrame:

WOW_THAT_IS_FAST = 50
list_ = list()
for animal in source_df.itertuples(index=False, name='animal'):
    if animal.top_speed > 50:
        list_.append(animal)

现在,只需一个命令就可以构建DF,而无需手动重新创建列名。

filtered_df = pd.DataFrame(list_)

      animal  top_speed
0  cheetah       120.00
2  dragonfly      54.00

相关问题