[英]Iterate over columns in a pandas dataframe and output result into a new dataframe
I'm trying to iterate over the columns in a pandas dataframe that is 6 x 22 columns using df.apply(min_max, axis=0)
and get the output in a new dataframe with two columns as below. 我正在尝试使用
df.apply(min_max, axis=0)
遍历6 x 22列的pandas数据df.apply(min_max, axis=0)
列df.apply(min_max, axis=0)
并在具有两列的新数据df.apply(min_max, axis=0)
获得输出,如下所示。
def min_max(column):
column_names = ['column', 'value']
output = pd.DataFrame(columns = column_names)
output2 = pd.DataFrame([column, abs(column.max() - column.min())])
return output.append(output2)
df is shown in attached img: first few columns on current dataframe 附件img中显示了df: 当前数据帧的前几列
Two questions: 两个问题:
I'm getting the error below: 我收到以下错误:
TypeError: ("object of type 'Timedelta' has no len()", 'occurred at index date') TypeError :(“ Timedelta类型的对象没有len()”,“在索引日期发生”)
Is there a way I can exclude all columns that are not dtype = int
? 有没有办法排除所有不是
dtype = int
列? Does this go into the function? 这会进入功能吗?
Much oblidged. 太多了。
Here is a solution : 这是一个解决方案:
df.select_dtypes(exclude = ['int']).apply(min_max, axis = 0)
pandas.DataFrame.select_dtypes pandas.DataFrame.select_dtypes
To select all numeric values use numpy.number. 要选择所有数值,请使用numpy.number。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.