向数据框追加行

2024-04-19 17:02:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我被一项简单的任务困住了。我想创建一个空的DataFrame,并基于另一个数据集的查询将行附加到其中。我试过这里的答案,但我错过了一些东西…初学者Python。任何帮助都将不胜感激。我想获取每个状态的前3行,并将它们添加到新的数据帧中进行处理。我也试着附加。。你知道吗

def test():

    #get the list of states
    states_df = census_df.STNAME.unique()
    population_df = pd.DataFrame()

    for st in states_df:
        temp_df = pd.DataFrame(census_df[census_df['STNAME'] == st].nlargest(3,'CENSUS2010POP'))
        pd.concat([temp_df, population_df], ignore_index = True)

    return 1

Tags: 数据答案testdataframedf状态deftemp
2条回答

我想我知道你在做什么,一年前我玩得很开心,继续!你知道吗

我发现连接一组切片数据帧的最简单/最快的方法是将每个df附加到一个列表中,然后在最后连接该列表。请参阅下面的工作代码(它实现了我对您的理解)。你知道吗

我同意David关于排序的建议,更容易使用sort,然后将前3个部分切片。因为nlargest()处理并返回一个我认为是序列而不是数据帧,而您希望保留整个数据帧结构(所有列)以进行连接。你知道吗

为什么你的函数返回1?打字错误?我猜如果你把它放在一个函数中,你想返回你想要的输出,所以我也改变了。你知道吗

import pandas as pd
import numpy as np


#create fake data random numbers
data = np.random.randint(2,11,(40,3))
census_df = pd.DataFrame(index=range(40), columns=['Blah', 'Blah2','CENSUS2010POP'], data=data)
#create fake STNAME column
census_df['STNAME'] = list('aaaabbbbccccddddeeeeffffgggghhhhiiiijjjj')

#Function:
def test(census_df):
    states_list = census_df.STNAME.unique() #changed naming to _list as it's not a df.
    list_of_dfs = list() #more efficient to append each df to a list
    for st in states_list:
        temp_df = census_df[census_df['STNAME']==st]
        temp_df = temp_df.sort_values(by=['CENSUS2010POP'], ascending=False).iloc[:3]
        list_of_dfs.append(temp_df)
    population_df = pd.concat(list_of_dfs,ignore_index=True)
    return population_df

population_df = test(census_df)

欢迎来到SO!您的问题是追加还是前三行?你知道吗

对于append,请尝试df.append函数。它可能看起来像:

#get the list of states
states_df = census_df.STNAME.unique()
population_df = pd.DataFrame()

for st in states_df:
    temp_df = pd.DataFrame(census_df[census_df['STNAME'] == st].nlargest(3,'CENSUS2010POP'))
    population_df = population_df.append(temp_df, ignore_index = True) #append the temp df to your main df, ignoring the index

在最上面的那排你可以告诉我们df.sort\u值(by=['column name',ascending=False),然后选择前三行:

population_df = population_df.append(temp_df[0:3], ignore_index = True)

相关问题 更多 >