pandas 如何按列拆分DataFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32730584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to split DataFrame by column
提问by GoingMyWay
I have a Pandas DataFrameand it has 11 columns, but I want to split the DataFrame from column 1, not column 0, to column 10. I can implement it by more complex method, not the method provided by Pandasitself. How can I do it by using the DataFramemethod supported by Pandas?
我有一个Pandas DataFrame,它有 11 列,但我想将 DataFrame 从第 1 列而不是第 0 列拆分到第 10 列。我可以通过更复杂的方法来实现它,而不是Pandas自己提供的方法。我如何使用DataFrame支持的方法来做到这一点Pandas?
The data is 595 rows by 11 columns and I want to get :
数据是 595 行乘 11 列,我想得到:
>>> import numpy as np
>>> import pandas as pd
>>> train_data = pd.DataFrame(my_data, columns=my_columns)
>>> train_data
stockid prich_m1 prich_m3 prich_m6 \
1 000002.SZ 1.55755700445 0.861009772647 5.42726384781
2 000009.SZ 3.00223270244e-07 4.8010096027 4.46164511978
.. ... ... ... ...
.. ... ... ... ...
594 603699.SH 0.0491892903353 0.934596516371 0.0196757161342
595 603993.SH 0.83105321611 0.771692272102 2.02816558693
rsi mkt_cap held_by_ins found_own \
1 0.650879566982 153108876954.0 42.6353598479 14.9550575226
2 0.462085308057 19492802690.5 25.8866394448 5.31468116104
.. ... ... ... ...
.. ... ... ... ...
.. ... ... ... ...
594 0.482105263158 14580000000.0 3.98638909091 3.41028242424
595 0.559681697613 44416492093.8 90.0529786043 1.56995953686
debt_to_equity eps_rate share_out
1 4.4521 0.292628452536 11021632763.0
2 2.8257 -50.6773540796 1489669062.0
.. ... ... ...
.. ... ... ...
594 0.6798 1.48454654486 82500000.0
595 0.8822 10.3388360485 3279577050.0
[595 rows x 11 columns]
And I want to drop the stockidcolumn and get the rest data.
我想删除该stockid列并获取其余数据。
回答by Stefan
Try
尝试
new_df = df.iloc[:, 1:]
there's also new_df = df.ix[:, 1:]. This should eliminate stock_id. If you want to drop this column then use new_df = df.drop('stock_id', axis=1).
还有new_df = df.ix[:, 1:]。这应该消除stock_id. 如果要删除此列,请使用new_df = df.drop('stock_id', axis=1).
You could also use column names with .ix[]or .loc[]if you prefer. Has been asked & answered herebefore. See relevant docs.

