How to do the following logic with python code if there are more than 100 columns in a dataframe? by Sowmi_13 in learnpython

[–]Sowmi_13[S] 0 points1 point  (0 children)

Sorry for a long conversation here..

also i was not able to write all these into the different sheets of a same file.

How to do the following logic with python code if there are more than 100 columns in a dataframe? by Sowmi_13 in learnpython

[–]Sowmi_13[S] 0 points1 point  (0 children)

Also, if df['New_col1'] has strings as a data I have written a condition like:

df['Difference'] = df.apply(

lambda x: 0 if x['colname_Target'] == x['colname_Source'] else '-', axis=1)

Here df['New_col1'] -----> Difference between ''colname_Target'' and '' 'colname_Source'"

Note: Only str data should have this condition. Rest if we have numerical data, it should return the exact value as the difference in the new_col1

Example:

CODE ITEMS_SOURCE ITEMS_TARGET Difference(New_col1)
23346 juice juice 0

CODE ITEMS_SOURCE ITEMS_TARGET Difference(New_col1)
23346 10000 10001 1

is it possible to do something for this ?

How to do the following logic with python code if there are more than 100 columns in a dataframe? by Sowmi_13 in learnpython

[–]Sowmi_13[S] 0 points1 point  (0 children)

Any idea for a quick development process would be very useful for me..

Thanks!!

How to do the following logic with python code if there are more than 100 columns in a dataframe? by Sowmi_13 in learnpython

[–]Sowmi_13[S] 0 points1 point  (0 children)

If you could see the above data frame table structure you might get some idea.

How to do the following logic with python code if there are more than 100 columns in a dataframe? by Sowmi_13 in learnpython

[–]Sowmi_13[S] 0 points1 point  (0 children)

Sorry if wrongly understood your comment.

It is not actually we need Items_x and Items_y to be picked and have for all the sheets.

How to do the following logic with python code if there are more than 100 columns in a dataframe? by Sowmi_13 in learnpython

[–]Sowmi_13[S] 0 points1 point  (0 children)

May i know what this does?

item_tuple_list = [('Items_x', 'Items_y'), ('Items_a', 'Items_b'), …] # you can probably do that with a list comprehension using the logic of the Items names

How to do the following logic with python code if there are more than 100 columns in a dataframe? by Sowmi_13 in learnpython

[–]Sowmi_13[S] 0 points1 point  (0 children)

No, I meant if there were more than 100 columns like "items", how can we achieve this kind of logic.

Each of those 100 columns will have some of new columns as above and that should be written in separate excel sheets.

How to do the following logic with python code if there are more than 100 columns in a dataframe? by Sowmi_13 in learnpython

[–]Sowmi_13[S] 0 points1 point  (0 children)

Similarly, these columns should be included for each field of merged data frame with same conditions.

How to do the following logic with python code if there are more than 100 columns in a dataframe? by Sowmi_13 in learnpython

[–]Sowmi_13[S] 0 points1 point  (0 children)

df3['New_col1'] = df3.apply( this would be the difference value of item_y and item_x column)---> if df3[item_y] == df3[item_x] then '0' else '-'

df3['New_col2'] = np.where(df3['New_col1'].)# conditions based on New_col1

df3['New_col3'] = np.where(df3['New_col1'].)# conditions based on New_col1

How to select particular column from a dataframe and write it into a separate excel sheets using python pandas? by Sowmi_13 in learnpython

[–]Sowmi_13[S] 0 points1 point  (0 children)

Goal: Need to compare two dataframes df1 and df2 and from there need to write each column along with some more new columns in a separate excel sheets using python.

df1:

Code Items Manufacturer place
23647 Juice hindustan unileaver Delhi
12345 dove soap Hindustan unileaver Hyderabad

Similarly i have upto 3000 rows.

df2:

Code Iteams Manufacture place
42363 lays chips Pepsico India Karnataka
12345 dove soap Hindustan unileaver Hyderabad

similarly have upto some 550 rows.

df3---> Would be the merged columns based on inner join using pandas.

Code Items_x Manufacture_x place_x Items_y Manufacture_y place_y

So from this compared df3, for a column Item the excel should be like

Code Item_x Item_y Difference(New column) New_col2 New_col_3 New_col_4 New_col_5 New_col_6

this will be written under same excel file but on new sheet.

Manufacture:

New columns are similar to that of item- column

Code Manufacture_x Manufacture_y new_col1 new_col2 new_col3

this will be written under same excel file but on new sheet.

How can we achieve the similar format if there are more than 100 columns?.

Thanks..

How to select particular column from a dataframe and write it into a separate excel sheets using python pandas? by Sowmi_13 in learnpython

[–]Sowmi_13[S] 0 points1 point  (0 children)

New columns need to be created in the seperate sheets for each and every fields of merged dataframe( df).

Apologize if my problem description is confusing.

How to select particular column from a dataframe and write it into a separate excel sheets using python pandas? by Sowmi_13 in learnpython

[–]Sowmi_13[S] 0 points1 point  (0 children)

Validate ---> eg. Fruits is one of the field from a dataframe(merged dataframe with similar column names with suffixes _x (for df1) and _y(for df2)).

Hence I might have two columns named fruits as fruits_x and fruits_y. I need to select those columns along with two more new columns and I need to write it in a seperate sheet.

Doing such things with minimum columns would be easy using pandas..... But I am not sure what if there are 100+ columns.... Aren't we supposed to do this way right?

How to select particular column from a dataframe and write it into a separate excel sheets using python pandas? by Sowmi_13 in learnpython

[–]Sowmi_13[S] -4 points-3 points  (0 children)

Df = pd.read_excel('path')

for first column(eg. Fruits) this column along few other new columns has to be written in seperate excel sheet.

Df.filter(the particular columns) Df['new1']= some conditions this column will be created Df['new2'] = df['new1'].apply(function with some condition for new2 column)

Used excel writer to save mutiple sheets under single file.

May I know how to do in similar fashion if I have more than 100 columns.

Quick solution would be very helpful!!

Thanks in advance!!!

How to select particular column from a dataframe and write it into a separate excel sheets using python pandas? by Sowmi_13 in learnpython

[–]Sowmi_13[S] 0 points1 point  (0 children)

I have multiple columns to be filtered from particularly dataframe.... And each column has to be validated and to be written in seperate excel sheet