我需要按行(在一列内)循环,然后按数据集中的其余列循环。
我已经尝试使用iterows(),iloc()和iat()。
import pandas as pd
# Two-dimensional data frame
table = {'A' : [0.1, -0.2, 0.3, -0.4],'B' : [-0.2, 0.4, 0.6, 0.8], 'C' : [0.3, -0.6, -0.9, -1.3]}
# Setting index to be new column
df = pd.DataFrame(table, index = ['W','X','Y','Z'])
column_list = list(df.columns)
total_columns = len(column_list)
total_rows = len(df.index)
column_num = 1
column = column_list[column_num]
winners = []
losers = []
zero = []
for col_num in df:
row_num = 1
for row_num in df:
if (df.iloc[row_num][column]) > 0:
winners.append(df.iloc[row_num][column])
elif (df.iloc[row_num][column]) < 0:
losers.append(df.iloc[row_num][column])
else:
zero.append(df.iloc[row_num][column])
row_num = row_num + 1
答案 0 :(得分:1)
您可以使用迭代,并以列表理解的方式进行。
In [11]: [x for _, col in df.iteritems() for x in col]
Out[11]: [0.1, 0.2, 0.3, 0.4, 0.2, 0.4, 0.6, 0.8, 0.3, 0.6, 0.9, 1.3]
或者您可以展平numpy数组:
In [12]: list(df.values.T.flatten())
Out[12]: [0.1, 0.2, 0.3, 0.4, 0.2, 0.4, 0.6, 0.8, 0.3, 0.6, 0.9, 1.3]