Python Pandas Dataframe附加行

时间:2016-08-14 13:39:15

标签: python python-2.7 pandas dataframe

我正在尝试将数据框值附加为行,但将其作为列附加。我有32个文件,我想从第二列(称为dataset_code)并附加它。但它创建了32行和101列。我想要1列和3232行。

import pandas as pd
import os



source_directory = r'file_path'

df_combined = pd.DataFrame(columns=["dataset_code"])

for file in os.listdir(source_directory):
    if file.endswith(".csv"):
            #Read the new CSV to a dataframe.  
            df = pd.read_csv(source_directory + '\\' + file)
            df = df["dataset_code"]
            df_combined=df_combined.append(df)



print(df_combined)

3 个答案:

答案 0 :(得分:7)

你已经有两个非常好的答案,但是我可以提出几点建议。

  1. 如果您只想要dataset_code列,请直接告诉pd.read_csvusecols=['dataset_code']),而不是将整个文件加载到内存中,以便立即对数据框进行子集化。
  2. 不是附加到最初为空的数据帧,而是收集数据帧列表并在最后一次连接它们。将行添加到pandas DataFrame是很昂贵的(它必须创建一个全新的),因此您的方法创建了65 DataFrame s:一个开头,一个读取每个文件,一个附加每个后者 - 甚至可能还有32个,具有子集。我提出的方法只创建了33个,这是这种导入的常用习惯。
  3. 以下是代码:

    import os
    import pandas as pd
    
    source_directory = r'file_path'
    
    dfs = []
    for file in os.listdir(source_directory):
        if file.endswith(".csv"):
            df = pd.read_csv(os.join.path(source_directory, file),
                            usecols=['dataset_code'])
            dfs.append(df)
    
    df_combined = pd.concat(dfs)
    

答案 1 :(得分:4)

ContextErrorException in DebugClassLoader.php line 194: Warning: class_implements(): Class does not exist and could not be loaded in DebugClassLoader.php line 194 at ErrorHandler->handleError('2', 'class_implements(): Class does not exist and could not be loaded', 'C:\xampp\htdocs\www\MY-Target\vendor\symfony\debug\DebugClassLoader.php', '194', array('class' => 'Symfony\Component\Security\Core\Exception\AuthenticationException', 'file' => 'C:\xampp\htdocs\www\MY-Target\vendor\composer/../symfony/security\Core\Exception\AuthenticationException.php', 'exists' => true, 'refl' => object(ReflectionClass), 'name' => 'Symfony\Component\Security\Core\Exception\AuthenticationException', 'notice' => array(), 'len' => '8', 'ns' => 'Symfony\', 'parent' => '', 'parentInterfaces' => array(), 'deprecatedInterfaces' => array())) at class_implements('') in DebugClassLoader.php line 194 at DebugClassLoader->loadClass('Symfony\Component\Security\Core\Exception\AuthenticationException') at spl_autoload_call('Symfony\Component\Security\Core\Exception\AuthenticationException') in BadCredentialsException.php line 20 at require_once('C:\xampp\htdocs\www\MY-Target\vendor\symfony\security\Core\Exception\BadCredentialsException.php') in DebugClassLoader.php line 142 at DebugClassLoader->loadClass('Symfony\Component\Security\Core\Exception\BadCredentialsException') at spl_autoload_call('Symfony\Component\Security\Core\Exception\BadCredentialsException') in DaoAuthenticationProvider.php line 67 at DaoAuthenticationProvider->checkAuthentication(object(User), object(UsernamePasswordToken)) in UserAuthenticationProvider.php line 86 at UserAuthenticationProvider->authenticate(object(UsernamePasswordToken)) in AuthenticationProviderManager.php line 80 at AuthenticationProviderManager->authenticate(object(UsernamePasswordToken)) in UsernamePasswordFormAuthenticationListener.php line 93 at UsernamePasswordFormAuthenticationListener->attemptAuthentication(object(Request)) in AbstractAuthenticationListener.php line 146 at AbstractAuthenticationListener->handle(object(GetResponseEvent)) in Firewall.php line 69 at Firewall->onKernelRequest(object(GetResponseEvent), 'kernel.request', object(TraceableEventDispatcher)) at call_user_func(array(object(Firewall), 'onKernelRequest'), object(GetResponseEvent), 'kernel.request', object(TraceableEventDispatcher)) in WrappedListener.php line 61 at WrappedListener->__invoke(object(GetResponseEvent), 'kernel.request', object(EventDispatcher)) at call_user_func(object(WrappedListener), object(GetResponseEvent), 'kernel.request', object(EventDispatcher)) in EventDispatcher.php line 174 at EventDispatcher->doDispatch(array(object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener)), 'kernel.request', object(GetResponseEvent)) in EventDispatcher.php line 43 at EventDispatcher->dispatch('kernel.request', object(GetResponseEvent)) in TraceableEventDispatcher.php line 136 at TraceableEventDispatcher->dispatch('kernel.request', object(GetResponseEvent)) in HttpKernel.php line 129 at HttpKernel->handleRaw(object(Request), '1') in HttpKernel.php line 68 at HttpKernel->handle(object(Request), '1', true) in Application.php line 496 at Application->handle(object(Request)) in Application.php line 477 at Application->run() in index_dev.php line 22 df["dataset_code"],而不是Series。由于您要将一个DataFrame附加到另一个DataFrame,因此需要将Series对象更改为DataFrame对象。

DataFrame

要进行转换,请执行以下操作:

>>> type(df)
<class 'pandas.core.frame.DataFrame'>
>>> type(df['dataset_code'])
<class 'pandas.core.series.Series'>

答案 2 :(得分:3)

或者,您可以使用双方括号创建数据框:

df = df[["dataset_code"]]