Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advice to update just one column of a DataFrame in a worksheet (was: ValueError when trying to set a single column) #60

Closed
zwhitney-nc opened this issue Apr 26, 2024 · 2 comments

Comments

@zwhitney-nc
Copy link

zwhitney-nc commented Apr 26, 2024

I relatively frequently will be trying to use get_as_dataframe to pull in values from a gsheet, do some operation to them ad then only update a single column in the original gsheet with set_with_dataframe. However, I always get a value error when I do so, as in the below stacktrace:

Traceback (most recent call last):
  File "<string>", line 19, in __PYTHON_EL_eval
  File "~/Documents/Phon_Utils/scratch.py", line 16, in <module>
  File "~/Documents/Phon_Utils/venv/lib/python3.11/site-packages/gspread_dataframe.py", line 262, in set_with_dataframe
    y, x = dataframe.shape
    ^^^^
ValueError: not enough values to unpack (expected 2, got 1)

Would it be possible to update the function so that it can also accept dataframes that are only a single column wide as input?

@robin900
Copy link
Owner

@zwhitney-nc Sorry for the delay in responding! You are not passing a DataFrame to set_as_dataframe -- you are passing a Series representing a single column. To update just one column of the Google worksheet, using just one column from your DataFrame, you want to perform the following steps:

  1. Create a simple DataFrame with just the column you select: pandas.DataFrame(my_dataframe[my_column]). That's it -- the single column Series has the same index as the original DataFrame, and it keeps its name (header). The new DataFrame will keep that index and column name (header).
  2. Know which column on the Google worksheet corresponds to the column to update. This is a numeric value, starting with 1 for the leftmost column (A). B=2, C=3, etc.
  3. When you call set_with_dataframe, pass the new one-column DataFrame you created, and also col={the_column_number_you_determined} as a keyword argument.

The above should update just that single column in the worksheet. Let me know if it works for you.

@robin900 robin900 changed the title Value Error when trying to set a single column Advice to update just one column of a DataFrame in a worksheet (was: ValueError when trying to set a single column) May 24, 2024
@zwhitney-nc
Copy link
Author

Awesome, thanks so much! Will give that a shot, then!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants