How to append pandas dataframe with similar names in a loop? [duplicate] - python

This question already has answers here:
Using a loop in Python to name variables [duplicate]
(5 answers)
Append multiple pandas data frames at once
(5 answers)
Closed 5 years ago.
I have pandas data frame numbered from x1,x2....x100 with same columns.
I want to append them all using a for loop. How can i do that?
I know how to append two dataframe but how to do it for 100 of them. The main problem here is how can i have a dynamic variable name.
I want to append the data frames not concat.
x=x1.append(x2)
x=x.append(x3)
and so on.
I want to this in a loop.

Related

Save pandas dataframe to file including index [duplicate]

This question already has answers here:
How to reversibly store and load a Pandas dataframe to/from disk
(13 answers)
Saving and Loading of dataframe to csv results in Unnamed columns
(4 answers)
Closed 6 months ago.
Which file format can be used to save a Pandas DataFrame object and then loading it back with the proper index? I.e. if column blah was an index before saving it to the file, I want that after loading it back again blah to be an index without me having to tell this to Pandas.
df.to_pickle('file.pickle')
df = pd.read_pickle('file.pickle')

Modify pandas iteration from iterrows to apply/vectorized format [duplicate]

This question already has answers here:
How can repetitive rows of data be collected in a single row in pandas?
(3 answers)
pandas group by and find first non null value for all columns
(3 answers)
Closed 7 months ago.
While using iterrows to implement the logic takes lot of time.Can some suggest a way on how I could optimize the code with vectorized/apply()
Below is the input table..From a partition of (ITEMSALE,ITEMID),I need to populate rows with rank=1 .If any column value is null in rank=1,I need to populate the next available value in that column.This has to be done for all columns in dataset.
Below is the output format expected
I have tried below logic using iterrows where am accessing values rowise.Performance is too low using this method.
This should get you what you need
df.loc[df.loc[df['Item_ID'].isna()].groupby('Item_Sale')['Date'].idxmin()]

Problem using Pandas for joining dataframes [duplicate]

This question already has answers here:
Import multiple CSV files into pandas and concatenate into one DataFrame
(20 answers)
How do I combine two dataframes?
(8 answers)
Closed 8 months ago.
I am trying to join a lot of CSV files into a single dataframe after doing some conversions and filters, when I use the append method for the sn2 dataframe, the exported CSV contains all the data I want, however when I use the append method for the sn3 dataframe, only the data from the last CSV is exported, what am I missing?
sn2=pd.DataFrame()
sn3=pd.DataFrame()
files=os.listdir(load_path)
for file in files:
df_temp=pd.read_csv(load_path+file)
df_temp['Date']=file.split('.')[0]
df_temp['Date']=pd.to_datetime(df_temp['Date'],format='%Y%m%d%H%M')
filter1=df_temp['Name']=='Atribute1'
temp1=df_temp[filter1]
sn2=sn2.append(temp1)
filter2=df_temp['Name']=='Atribute2'
temp2=df_temp[filter2]
sn3=pd.concat([temp2])
You have to pass all the dataframes that you want to concatenate to concat:
sn3 = pd.concat([sn3, temp2])

Can pandas join tables if the common headers are different names? [duplicate]

This question already has answers here:
Joining pandas DataFrames by Column names
(3 answers)
Pandas Merging 101
(8 answers)
Closed last year.
I am following this article, but I was only able to get it to work by making sure there were matching titles, the two still had computer names, but they were called differently in the title, how could I modify my command so that it still references the same column, is that possible?
lj_df2 = pd.merge(d2, d3, on="PrimaryUser", how="left")
For example, I have this, but on my other csv, I have Employee # not primary user

Difference in Pandas between df['myColumn'] and df.myColumn [duplicate]

This question already has answers here:
Pandas column creation
(3 answers)
Accessing Pandas column using squared brackets vs using a dot (like an attribute)
(5 answers)
pandas dataframe where clause with dot versus brackets column selection
(1 answer)
Closed 5 years ago.
I just thought I added a column to a pandas dataframe with
df.NewColumn = np.log1p(df.ExistingColumn)
but when I looked it wasn't there! No error was raised either.. I executed it many times not believing what I was seeing but yeah, it wasn't there. So then did this:
df['NewColumn'] = np.log1p(df.ExistingColumn)
and now the new column was there.
Does anyone know the reason for this confusing behaviour? I thought those two ways of operating on a column were equivalent..

Categories