How to insert data from one dataframe into an index present in another?

Asked

Viewed 1,584 times

-1

I have a folder with about 480 CSV files, only they don’t have an index and I created another CSV file to be their index (there are 119 columns in the index) When I enter using df.append the file becomes 238 columns, ie fold. I tried everything to insert this index as the first row before reading the files, as I do to insert the lines below the title of their columns?

I tried that:

df1= pd.read_csv('/home/user/TCC/2015/index.csv')
df2= pd.read_csv('/home/user/TCC/2015/data.csv')

df1 = df1.append(df2)

I tried that way too:

result = pd.concat([df1, df2], axis=1).reindex(df_index.index)

If you can help, thank you.

2 answers

0

I figured it out, and I come back here to answer like I did. Within the function: pd.read_csv

has an attribute called name, just put the name I want and the columns receive them, example:

df1 = pd.read_csv('path/item.csv', name= {"Column1", "Column2"})

can also create an array with the names and pass it ex: array ={"column1","column2"} df1=pd.read_csv('path/item.csv', name=array)

0

Merge, reset the index and then do the new index:

new =  df1.join(df2)
new.reset_index(inplace = True)
new.set_index('index', inplace = True)

Just to clarify, df.append appends the databases. That is, it adds lines (vertical sum), keeping the number of variables counting, but increasing the number of observations. On the other hand, when someone needs to join banks by increasing the number of variables with the constant number of observations, the command df.Join and pd.concatenate is used..

Here is an elaborate presentation on this (in English): https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html

  • I tried that but got the answer: Typeerror: set_index() takes from 2 to 6 positional Arguments but 119 Were Given

  • 'index' in the new.set_index('index', inplace = True) command is a string. It looks like you have put an object with 119 entries in place. If you have any questions check the documentation to see how the method works: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.set_index.html Something else, your problem would be clearer if you showed a replicable example or made the files available

  • I tried the function here it creates new columns, I want to insert the values of a Dataframe (which has 119 columns) and I want another Dataframe to receive this data to be the index. As in the example: df1 = A B C D ------------------- df2 = 1 2 3 4 5 6 7 8 9 10 11 12 df1 + df2 = dfmerged dfmerged = A B C D 1 2 3 4 5 6 7 8

Browser other questions tagged

You are not signed in. Login or sign up in order to post.