python dataframe取一列_python - 从pandas DataFrame列标题中获取列表
python - 從pandas DataFrame列標題中獲取列表
我想從pandas DataFrame中獲取列標題列表。 DataFrame將來自用戶輸入,因此我不知道將會有多少列或將調用它們。
例如,如果我給這樣的DataFrame:
>>> my_dataframe
y gdp cap
0 1 2 5
1 2 3 9
2 8 7 2
3 3 4 7
4 6 7 7
5 4 8 3
6 8 2 8
7 9 9 10
8 6 6 4
9 10 10 7
我想得到一個如下所示的列表:
>>> header_list
[y, gdp, cap]
17個解決方案
1106 votes
您可以通過執行以下操作將值作為列表獲取:
list(my_dataframe.columns.values)
你也可以簡單地使用:
list(my_dataframe)
Simeon Visser answered 2019-01-20T10:54:11Z
289 votes
有一種內置的方法,性能最高:
my_dataframe.columns.values.tolist()
.columns返回Index,.columns.values返回array并且它有一個幫助函數返回list。
編輯
對于那些討厭打字的人來說,這可能是最短的方法:
list(df)
EdChum answered 2019-01-20T10:54:49Z
70 votes
做了一些快速測試,也許不出所料,使用list(dataframe)的內置版本是最快的:
In [1]: %timeit [column for column in df]
1000 loops, best of 3: 81.6 μs per loop
In [2]: %timeit df.columns.values.tolist()
10000 loops, best of 3: 16.1 μs per loop
In [3]: %timeit list(df)
10000 loops, best of 3: 44.9 μs per loop
In [4]: % timeit list(df.columns.values)
10000 loops, best of 3: 38.4 μs per loop
(我仍然非常喜歡list(dataframe),所以感謝EdChum!)
tegan answered 2019-01-20T10:55:18Z
36 votes
它變得更簡單(通過熊貓0.16.0):
df.columns.tolist()
將在一個很好的列表中給你列名稱。
fixxxer answered 2019-01-20T10:55:47Z
27 votes
>>> list(my_dataframe)
['y', 'gdp', 'cap']
要在調試器模式下列出數據幀的列,請使用列表推導:
>>> [c for c in my_dataframe]
['y', 'gdp', 'cap']
順便說一句,您只需使用sorted即可獲得排序列表:
>>> sorted(my_dataframe)
['cap', 'gdp', 'y']
Alexander answered 2019-01-20T10:56:16Z
18 votes
這是my_dataframe.columns。
BrenBarn answered 2019-01-20T10:56:38Z
14 votes
這很有趣,但df.columns.values.tolist()快幾乎是df.columns.tolist()的3倍,但我認為它們是相同的:
In [97]: %timeit df.columns.values.tolist()
100000 loops, best of 3: 2.97 μs per loop
In [98]: %timeit df.columns.tolist()
10000 loops, best of 3: 9.67 μs per loop
Anton Protopopov answered 2019-01-20T10:57:00Z
10 votes
DataFrame遵循迭代對象“鍵”的類似dict的約定。
my_dataframe.keys()
創建一個鍵/列列表 - 對象方法to_list()和pythonic方式
my_dataframe.keys().to_list()
list(my_dataframe.keys())
DataFrame上的基本迭代返回列標簽
[column for column in my_dataframe]
不要將DataFrame轉換為列表,只是為了獲取列標簽。 在尋找方便的代碼示例時不要停止思考。
xlarge = pd.DataFrame(np.arange(100000000).reshape(10000,10000))
list(xlarge) #compute time and memory consumption depend on dataframe size - O(N)
list(xlarge.keys()) #constant time operation - O(1)
Sascha Gottfried answered 2019-01-20T10:57:41Z
9 votes
在筆記本中
對于IPython筆記本中的數據探索,我首選的方法是:
sorted(df)
這將產生易于閱讀的按字母順序排列的列表。
在代碼存儲庫中
在代碼中我發現它更明確
df.columns
因為它告訴其他人讀你的代碼你在做什么。
firelynx answered 2019-01-20T10:58:35Z
2 votes
n = []
for i in my_dataframe.columns:
n.append(i)
print n
user21988 answered 2019-01-20T10:58:50Z
2 votes
我覺得問題應該得到額外的解釋。
正如@fixxxer所指出的,答案取決于您在項目中使用的pandas版本。您可以通過pd.__version__命令獲得。
如果你出于某種原因像我一樣(在debian jessie上使用0.14.1)使用比0.16.0更舊的熊貓版本,那么你需要使用:
df.keys().tolist()因為尚未實現df.columns方法。
這種密鑰方法的優點是,它甚至可以在較新版本的熊貓中使用,因此它更具通用性。
StefanK answered 2019-01-20T10:59:39Z
2 votes
正如Simeon Visser所回答的......你可以做到
list(my_dataframe.columns.values)
要么
list(my_dataframe) # for less typing.
但我認為最大的好處是:
list(my_dataframe.columns)
它是明確的,同時也不是不必要的長。
Vivek answered 2019-01-20T11:00:16Z
1 votes
要快速,整潔,直觀地檢查,請嘗試以下方法:
for col in df.columns:
print col
Joseph True answered 2019-01-20T11:00:38Z
1 votes
這為我們提供了列表中列的名稱:
list(my_dataframe.columns)
另一個名為tolist()的函數也可以使用:
my_dataframe.columns.tolist()
Harikrishna answered 2019-01-20T11:01:06Z
0 votes
此解決方案列出了對象my_dataframe的所有列:
print(list(my_dataframe))
Sunitha G answered 2019-01-20T11:01:28Z
0 votes
list(a_dataframe)
這應該做到!
Tahir Ahmad answered 2019-01-20T11:01:51Z
-1 votes
可以使用索引屬性
df = pd.DataFrame({'col1' : np.random.randn(3), 'col2' : np.random.randn(3)},
index=['a', 'b', 'c'])
Anirudh k v answered 2019-01-20T11:02:13Z
總結
以上是生活随笔為你收集整理的python dataframe取一列_python - 从pandas DataFrame列标题中获取列表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: echart 坐标数字间隔_用LaTeX
- 下一篇: 羽绒被怎么洗