'How do I replace a slice of a dataframe column with values from another dataframe column slice?
I have a two dataframes with several columns including a timestamp column. I would like to copy the first 1000 timestamps from the second dataframe to the first one.
df1 = pd.read_csv(file1.csv)
df2 = pd.read_csv(file2.csv)
df1.timestamp.iloc[:1000] = df2.timestamp.iloc[:1000]
I tried various things like adding .copy() to the right hand side, using .loc[:1000, 'timestamp'] instead of the columnname.iloc syntax, converting the column series into a numpy array first, but I keep getting errors ranging from "too many indexers", to a directive to use .loc[rowindexing, columnindexing] (which doesn't fix the issue), and other error messages.
Solution 1:[1]
Given df1, df2:
df1 = pd.DataFrame({'timestamp': range(0,2000)})
df2 = -df1
using .loc:
df1.loc[:999,'timestamp'] = df2.loc[:999,'timestamp']
df1.loc[997:1002,'timestamp']
997 -997
998 -998
999 -999
1000 1000
1001 1001
1002 1002
Name: timestamp, dtype: int64
or using iloc (optionally converting loc -> iloc using get_loc)
df1.iloc[:1000,0] = df2.iloc[:1000,0]
df1.loc[997:1002,'timestamp']
997 -997
998 -998
999 -999
1000 1000
1001 1001
1002 1002
Name: timestamp, dtype: int64
note that the slicing behavior on iloc and loc is differrent..loc includes the right value, .iloc doesn't include it (like in range)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | natbusa |
