简体   繁体   English

我可以按列拆分数据框吗?

[英]Can i split a Dataframe by columns?

I need to split a Dataframe by the columns, I made a simple code, that runs without error, but didn't give me the return i expected.我需要按列拆分数据帧,我编写了一个简单的代码,该代码运行没有错误,但没有给我预期的回报。 Here's the simple code:这是简单的代码:

dados = pd.read_excel(r'XXX')

for x in range(1,13):
    selectmonth = x
    while selectmonth < 13:
        df_datas = dados.loc[dados['month'] == selectmonth]
        correlacao2 = df_datas.corr().round(4).iloc[0]
    else: break
print()

I did one by one by inputing the selected mouth manually like this:我是这样手动输入选中的嘴巴一一做的:

dfdatas = dados.loc[dados['month'] == selectmonth]
    print('\n Voce selecionou o mês: ', selectmonth)
colunas2 = list(dfdatas.columns.values)
correlacao2 = dfdatas.corr().round(4).iloc[0]
print(correlacao2)

is there some way to do this in a loop?有没有办法在循环中做到这一点? from month 1 to 12?从第 1 个月到第 12 个月?

With pandas, you should avoid using loops wherever possible, it is very slow.对于熊猫,您应该尽可能避免使用循环,它非常慢。 You can achieve what you want here with index slicing.您可以在此处使用索引切片实现您想要的功能。 I'm assuming your columns are just the month numbers, you can do this:我假设您的列只是月份数字,您可以这样做:

setting up an example df:设置示例 df:

df = pd.DataFrame([], columns=range(15))


df:
Columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
Index: []

getting columns with numbers 1 to 12:获取数字 1 到 12 的列:

dfdatas = df.loc[:, 1:12]

Columns: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Index: []

In the future, you should include example data in your question.将来,您应该在问题中包含示例数据。

Just try this:试试这个:

correlacao2 = dados.corr(method='pearson').round(4)
for month in dados.columns:
    print('\n Voce selecionou o mês: ', month)
    result=correlacao2.loc[month]
    result=pd.DataFrame(result)
    print(result)

Here I have used corr() and for-loop method and converted them to DataFrame这里我使用了corr()for-loop方法并将它们转换为DataFrame

dados is your dataframe name dados 是您的数据框名称

If your column name is number, then rename it with month name using dados.rename(columns={'1': 'Jan','2':'Feb','3':'Mar'}) .如果您的列名为 number,则使用dados.rename(columns={'1': 'Jan','2':'Feb','3':'Mar'})将其重命名为月份名称。 Similarly, you include other months too to rename the column names.同样,您也包括其他月份以重命名列名称。 After renaming, apply the above code to get your expected answer.重命名后,应用上述代码以获得预期的答案。

If you don't want want to rename, then use .iloc[] instead of .loc[] in above code如果你不想重命名,那么在上面的代码中使用.iloc[]而不是.loc[]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM