如何以编程方式检索pandas数据框中的列数?我希望有类似的东西:
df.num_columns
答案 0 :(得分:223)
像这样:
import pandas as pd
df = pd.DataFrame({"pear": [1,2,3], "apple": [2,3,4], "orange": [3,4,5]})
len(df.columns)
3
答案 1 :(得分:77)
替代方案:
df.shape[1]
(df.shape[0]
是行数)
答案 2 :(得分:19)
如果持有数据帧的变量被称为df,那么:
len(df.columns)
给出了列数。
对于那些想要行数的人:
len(df.index)
对于包含行和列数的元组:
df.shape
答案 3 :(得分:2)
很惊讶我还没有看到这个,所以不用多说,这里是:
答案 4 :(得分:1)
这对我有用(len(list(df))。
答案 5 :(得分:1)
有多个选项可获取列号和列信息,例如:
让我们检查一下。
local_df = pd.DataFrame(np.random.randint(1,12,size =(2,6)),列= ['a','b','c','d','e' ,'F']) 1. local_df.shape [1]-> Shape属性返回元组为(行和列)(0,1)。
local_df.info()-> info方法将返回有关数据框及其列的详细信息,例如列数,列的数据类型, 不是空值计数,按数据帧的内存使用量
len(local_df.columns)->列属性将返回数据框列的索引对象,而len函数将返回可用列总数。
local_df.head(0)->具有参数0的head方法将返回df的第一行,实际上只有头而已。
假设列数不超过10。 li_count = 0 对于local_df中的x: li_count = li_count + 1 打印(li_count)
答案 6 :(得分:0)
df.info()函数将为您提供如下结果。 如果您使用的是不带sep参数或不带“,”的sep的Pandas的read_csv方法。
raw_data = pd.read_csv("a1:\aa2/aaa3/data.csv")
raw_data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5144 entries, 0 to 5143
Columns: 145 entries, R_fighter to R_age
答案 7 :(得分:0)
为了在您的总形状中包含行索引“列”的数量,我个人会将列数 df.columns.size
与属性 pd.Index.nlevels
/pd.MultiIndex.nlevels
相加:>
设置虚拟数据
import pandas as pd
flat_index = pd.Index([0, 1, 2])
multi_index = pd.MultiIndex.from_tuples([("a", 1), ("a", 2), ("b", 1), names=["letter", "id"])
columns = ["cat", "dog", "fish"]
data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flat_df = pd.DataFrame(data, index=flat_index, columns=columns)
multi_df = pd.DataFrame(data, index=multi_index, columns=columns)
# Show data
# -----------------
# 3 columns, 4 including the index
print(flat_df)
cat dog fish
id
0 1 2 3
1 4 5 6
2 7 8 9
# -----------------
# 3 columns, 5 including the index
print(multi_df)
cat dog fish
letter id
a 1 1 2 3
2 4 5 6
b 1 7 8 9
将我们的流程写成一个函数:
def total_ncols(df, include_index=False):
ncols = df.columns.size
if include_index is True:
ncols += df.index.nlevels
return ncols
print("Ignore the index:")
print(total_ncols(flat_df), total_ncols(multi_df))
print("Include the index:")
print(total_ncols(flat_df, include_index=True), total_ncols(multi_df, include_index=True))
打印:
Ignore the index:
3 3
Include the index:
4 5
如果您只想在索引为 pd.MultiIndex
时包含索引的数量,那么您可以在定义的函数中放入 isinstance
检查。
作为替代方案,您可以使用 df.reset_index().columns.size
来实现相同的结果,但这不会那么高效,因为我们暂时将新列插入索引并在获取数量之前创建新索引列。
答案 8 :(得分:0)
#use a regular expression to parse the column count
#https://docs.python.org/3/library/re.html
buffer = io.StringIO()
df.info(buf=buffer)
s = buffer.getvalue()
pat=re.search(r"total\s{1}[0-9]\s{1}column",s)
print(s)
phrase=pat.group(0)
value=re.findall(r'[0-9]+',phrase)[0]
print(int(value))
答案 9 :(得分:0)
import pandas as pd
df = pd.DataFrame({"pear": [1,2,3], "apple": [2,3,4], "orange": [3,4,5]})
print(len(list(df.iterrows())))
给出行的长度
3
[Program finished]
答案 10 :(得分:0)