I'm trying to append the data frame values as rows but its appending them as columns. I have 32 files that i would like to take the second column from (called dataset_code) and append it. But its creating 32 rows and 101 columns. I would like 1 column and 3232 rows.
import pandas as pd
source_directory = r'file_path'
df_combined = pd.DataFrame(columns=["dataset_code"])
for file in os.listdir(source_directory):
#Read the new CSV to a dataframe.
df = pd.read_csv(source_directory + '\\' + file)
df = df["dataset_code"]
df["dataset_code"] is a
Series, not a
DataFrame. Since you want to append one DataFrame to another, you need to change the Series object to a DataFrame object.
>>> type(df) <class 'pandas.core.frame.DataFrame'> >>> type(df['dataset_code']) <class 'pandas.core.series.Series'>
To make the conversion, do this:
df = df["dataset_code"].to_frame()