Pandas 使用自己的索引将dataframe分隔为各个列
我有以下建议:Pandas 使用自己的索引将dataframe分隔为各个列,pandas,Pandas,我有以下建议: Arrows :Current Arrows:previous 1M Arrows:previous 2M silverine 1 0 0 fire 2 2 2 ice 11
Arrows :Current Arrows:previous 1M Arrows:previous 2M
silverine 1 0 0
fire 2 2 2
ice 11 12 10
wind 4 4 4
poisonous 2 2 9
假设我不知道我有多少列,我想用索引独立地获得每一列
所需的输出如下所示:
Arrows :Current
silverine 1
fire 2
ice 11
wind 4
poisonous 2
Arrows:previous 1M
silverine 0
fire 2
ice 12
wind 4
poisonous 2
Arrows:previous 2M
silverine 0
fire 2
ice 10
wind 4
poisonous 9
pandas中是否有任何已定义的属性来获取索引中的每一列?您需要按列选择数据以
系列
,数据帧的每一列与df
具有相同的索引
如果需要所有系列的列表:
L = [df[col] for col in df.columns]
print (L)
[silverine 1
fire 2
ice 11
wind 4
poisonous 2
Name: Arrows :Current, dtype: int64, silverine 0
fire 2
ice 12
wind 4
poisonous 2
Name: Arrows:previous 1M, dtype: int64, silverine 0
fire 2
ice 10
wind 4
poisonous 9
Name: Arrows:previous 2M, dtype: int64]
它只需按所有列循环:
for col in df.columns:
print (df[col])
silverine 1
fire 2
ice 11
wind 4
poisonous 2
Name: Arrows :Current, dtype: int64
silverine 0
fire 2
ice 12
wind 4
poisonous 2
Name: Arrows:previous 1M, dtype: int64
silverine 0
fire 2
ice 10
wind 4
poisonous 9
Name: Arrows:previous 2M, dtype: int64
我不确定是否理解-期望的输出是什么<代码>列表?还是需要按列循环?你能再解释一下吗?谢谢你。嗨@jezrael再次感谢你的帮助,我怎样才能通过列表以外的方式获得输出以将其转换为json?请稍等。我认为最好是使用
df.to_json('file.json',orient='split')
然后使用读取数据df=pd.read_json('file.jon',orient='split')
谢谢你,jezrael!