Search code examples
python-3.xpandascsvanalysis

pandas: populate all rows in a dataframe column with the values from one cell in a separate data frame


I want to populate all rows in a dataframe column with the values from one cell in a separate data frame.

Both dfs are based on data read in from the same CSV.

data_description = pd.read_csv(file.csv, nrows=1) 

#this two rows of data: one row of column headers and one row of values. The value I want to use later is under the header "average duration"

data_table = pd.read_csv(file.csv, skiprows=3) 

#this is a multi row data table located directly below the description. I to want add a "duration" column will all rows populated by "average duration" from above.

df1 = pd.DataFrame(data_description)
df2 = pd.DataFram(data_table)

df2['duration'] = df1['average duration']

The final line only works for the first from in the column. How can extend down all rows?

If I directly assign the 'average duration' value it works, e.g. df2['duration'] = 60, but I want it to be dynamic.


Solution

  • You have to extract the value from df1 and then assign the value to df2. What you're assigning is a Series, not the value.

    data_description = pd.read_csv(file.csv, nrows=1) 
    
    data_table = pd.read_csv(file.csv, skiprows=3) 
    
    df1 = pd.DataFrame(data_description)
    df2 = pd.DataFram(data_table)
    
    df2['duration'] = df1['average duration'][0]