[英]How to Iterate on dataframe columns
I have a little problem with my code. 我的代码有一点问题。 I would like to create a "for" cycle all over dataframe columns.
我想在所有dataframe列上创建一个“ for”循环。 My solution has a static list, i tried to create a dynamic one putting "df_list_cols= df.shape[1]" but obviusly it cannot iterate on an Int object.
我的解决方案有一个静态列表,我试图创建一个动态列表,将“ df_list_cols = df.shape [1]”放进去,但是显然它不能在Int对象上进行迭代。 Any ideas to create a dynamic solution?
有什么想法可以创建动态解决方案吗?
My solution above, Thank you in advance! 我上面的解决方案,预先谢谢!
df_list = [0,1,2,3,4]
for i in df_list_cols:
do stuff
You are close. 你近了 The way the
in
operator is defined for Pandas dataframes, iterating a pd.DataFrame
object is equivalent to iterating over its columns: 为Pandas数据帧定义
in
运算符的方式,迭代pd.DataFrame
对象等效于对其列进行迭代:
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4], 'C': [5, 6]})
for col in df:
print(col)
# A
# B
# C
In many cases, you may wish to use pd.DataFrame.apply
to apply the same function to each column: 在许多情况下,您可能希望使用
pd.DataFrame.apply
将相同的函数应用于每一列:
df = df.apply(lambda x: x*2)
print(df)
# A B C
# 0 2 6 10
# 1 4 8 12
The equivalent if you have a different function for each column is pd.DataFrame.transform
: 如果每列都有不同的功能,则
pd.DataFrame.transform
是pd.DataFrame.transform
:
df = df.transform({'A': lambda x: x*2, 'B': lambda x: x*3, 'C': lambda x: x*4})
You can iterate over df.columns
: 您可以遍历
df.columns
:
df.columns
gives you a list of the columns of the dataframe. df.columns
为您提供数据df.columns
的列的列表。
In [222]: df.columns.tolist()
Out[222]: ['A', 'B', 'C']
In [218]: for i in df.columns:
...: print(i)
...: ## do your stuff
A
B
C
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.