Append Data frame to google sheet's specific column in python - python

I have a google sheet that consists of 5 columns in which the column category and short word are in columns C and D of that google sheet respectively. I have a data frame that consists of short word & category I just want to append that data frame in just 2 columns of the said google spreadsheet

Related

How to iterate through for loop and write each dataframe to different excel sheets

I'm looping through list of jsons and storing them in a dataframe. for each iteration i want to write the dataframe into excel with different sheets. how to achieve this?
for item in data:
#removing empty columns in raw data
drop_none = lambda path, key, value: key is not None and value is not None
cleaned = remap(item, visit=drop_none)
new_data=flatten(cleaned)
#my_df = new_data.dropna(axis='columns', how='all') # Drops columns with all NA values
dfFromRDD2 = spark.createDataFrame(new_data)
I want to save the dataframe dfFromRDD2 to excel with different sheets on each iterations.
is their a way to do it using python?

Mapping in excel using python

Excel 1
Group Summary Label Amount
Individual Member
Family Member
Family
Excel 2
Network Label Value
Individual Member 100
Family Member 200
Family 300
I have two Excel sheets and I am trying to map values. As you see both excels has different column names but rows are same. I am trying to map 'value' in excel 2 to Amount in excel 1
I am expecting result like this. How can I do this using Python? I am new and trying to learn.
Group Summary Label Amount
Individual Member 100
Family Member 200
Family 300
First, open two dataframe of each sheet inside the excel:
df = pd.ExcelFile('excelfilename.xls')
df1 = pd.read_excel(df, 'Sheet1')
df2 = pd.read_excel(df, 'Sheet2')
now you can match the two dataFrames into one
df1.drop(["Amount"], axis=1)
df2 = df2.join(df1)
df2 = df2.rename(columns={"Value": "Amount"}, errors="raise")

How to sum over sheets that have same index in pandas?

I have an excel data containing multiple sheets.
The data is hourly rainfall data value that have spatial index longitude in row and latitude in column.
This is the excel file
I need to sum over all these sheets to get the daily rainfall data.
How could I do with pandas in python?
You can use dict = pd.read_excel('myfile.xlsx', sheetname=None). This function gives you a dictionary of sheets that iterate by for sheet in dict.items(). The rest is same as the solution that #cs95 has provided. Concat them inside the for loop and then group by lat/long.

Fill an existing Excel file with data from a Pandas DataFrame

I have a Pandas DataFrame with a bunch of rows and labeled columns.
I also have an excel file which I prepared with one sheet which contains no data but only
labeled columns in row 1 and each column is formatted as it should be: for example if I
expect percentages in one column then that column will automatically convert a raw number to percentage.
What I want to do is fill the raw data from my DataFrame into that Excel sheet in such a way
that row 1 remains intact so the column names remain. The data from the DataFrame should fill
the excel rows starting from row 2 and the pre-formatted columns should take care of converting
the raw numbers to their appropriate type, hence filling the data should not override the column format.
I tried using openpyxl but it ended up creating a new sheet and overriding everything.
Any help?
If you're certain about the order of columns is same, you can try this after opening the sheet with openpyxl:
df.to_excel(writer, startrow = 2,index = False, Header = False)
If your # of columns and order is same then you may try xlsxwriter and also mention the sheet name to want to refresh:
df.to_excel('filename.xlsx', engine='xlsxwriter', sheet_name='sheetname', index=False)

Search entire excel sheet with Pandas for word(s)

I am trying to essentially replicate the Find function (control-f) in Python with Pandas. I want to search and entire sheet (all rows and columns) to see if any of the cells on the sheet contain a word and then print out the row in which the word was found. I'd like to do this across multiple sheets as well.
I've imported the sheet:
pdTestDataframe = pd.read_excel(TestFile, sheet_name="Sheet Name",
keep_default_na= False, na_values=[""])
And tried to create a list of columns that I could index into the values of all of the cells but it's still excluding many of the cells in the sheet. The attempted code is below.
columnsList = []
for i, data in enumerate(pdTestDataframe.columns):
columnList.append(pdTestDataframe.columns[i])
for j, data1 in enumerate(pdTestDataframe.index):
print(pdTestDataframe[columnList[i]][j])
I want to make sure that no matter the formatting of the excel sheet, all cells with data inside can be searched for the word(s). Would love any help I can get!
Pandas has a different way of thinking about this. Just calling df[df.text_column.str.contains('whatever')] will show you all the rows in which the text is contained in one specific column. To search the entire dataframe, you can use:
mask = np.column_stack([df[col].str.contains(r"\^", na=False) for col in df])
df.loc[mask.any(axis=1)]
(Source is here)

Categories

Resources