简体   繁体   中英

How to concatenate a specific column from a pandas.DataFrame()?

I have a list of files, and I want to combine a specific column from it for all my files, to run some cumulative analysis.

import pandas as pd
import numpy as np
all_data_sets = pd.DataFrame([])
for file_name in file_list:
    my_data = pd.DataFrame([])
    my_data = pd.read_csv(file_name, delimiter=',', names=header_row)
    my_data = my_data.reset_index()
    all_data_sets.append(my_data['sales'])

#np.mean(all_data_sets['sales'])
np.mean(all_data_sets)

you can use concat to concatenate a list of DataFrames

 df_list = [pd.read_csv(file_name, delimiter=',', names=header_row) for file_name in file_list] #opens your csv

 df = pd.concat(df_list)

Then you calculate the mean via

 df.sales.mean()

A small example

 a = pd.DataFrame({'sales' : [2,4,6] , 'other' : [1,2,1]})
 b = pd.DataFrame({'sales' : [7,4,7] , 'other' : [9,2,1]})

 df = pd.concat([a,b])

the dataframe is

      other  sales
  0      1      2
  1      2      4
  2      1      6
  0      9      7
  1      2      4
  2      1      7

and the mean

 df.sales.mean()
    5.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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