I want to insert the index x th created df2 of df1
df2 = pd.DataFrame ( [ ['2020-11-14', 5, '2222','fuga'], ['2020-11-14', 6, '881','fuga'], ] )
When using pd.concat, it is vertically connected as it is.
What should I do to make it look like the following?
Answer # 1
Let's divide df1 vertically in slice notation and concat with df2 in between.
In addition, in the questioner's code, the columns of df2 are different from df1, so if you concat as it is, it will be misaligned. Therefore, copy columns as preprocessing. In addition, reset_index that reassigns the index is inserted as post-processing.
import pandas as pd import io data =''' Date A B C 2020-11-14 5 1822 hoge 2020-11-14 6 1899 hoge 2020-11-14 3 1442 hoge 2020-11-14 4 1255 hoge ''' df1 = pd.read_csv (io.StringIO (data), delimiter ='\ s +') df2 = pd.DataFrame ( [ ['2020-11-14', 5, '2222','fuga'], ['2020-11-14', 6, '881','fuga'], ] ) # Here is the answer body x = 3 df2.columns = df1.columns df = pd.concat ([df1 [: x], df2, df1 [x:]]). reset_index (drop = True) print (df) # Date A B C # 0 2020-11-14 5 1822 hoge # 1 2020-11-14 6 1899 hoge # 2 2020-11-14 3 1442 hoge # 3 2020-11-14 5 2222 fuga # 4 2020-11-14 6 881 fuga # 5 2020-11-14 4 1255 hoge
- python - i want to make two lists correspond
- python - i want to know how to keep outputting to csv
- python - i want to move images with pygame
- python - i want to open a file with an absolute path
- python - i want to send a list of yahoo news rankings to line
- python - i want to add an element to a double list
- python - i want to scrape google news, but i can't
- python - i want the block to disappear when the ball hits the block
- python - i want to handle webdriverchrome ()
- python - i want to add a line number to the data frame
- python - i want to solve this problem
- python - i want to pass a list as an argument of glob and repeat it
- python - i want to find the mode of a pixel with a pixel value of 1 or more
- python - combine input with built-in functions
- python - i want to avoid line breaks in the file path
- (python) i would like to know how to combine an int value and a multidimensional array
- i want to combine lists with python
- python - i want to ignore the header and loop
- python - if __name__ == i want to know about'__main__'
- How to increase DNSPYTHON processing speed
- In Python -Resulting Train Set Will Be Empty: How to "repair" sample size?
- python : Pandas: Merge Several DataFrame using several (2) common columns
- Python Pandas connecting two DataFrame
- python : How to convert in Float /int columns in Pandas
- python : Replace values in one datamaphrame from the other condition
- Excel Data Merge with Python
- python : How to get the last value by day in DataFrame Pandas
- python : Pandas Comparison of two DF and row sample
- python : Select speakers in DataFrame list