0

I wanna insert data into a data frame like:

df = pd.DataFrame(columns=["Date", "Title", "Artist"])

insertion happens here:

df.insert(loc=0, column="Date", value=dateTime.group(0), allow_duplicates=True)
df.insert(loc=0, column="Title", value=title, allow_duplicates=True)
df.insert(loc=0, column="Artist", value=artist, allow_duplicates=True)

sadly I don't know how to handle these errors:

Traceback (most recent call last):
  File "/Users/sashakaun/IdeaProjects/python/venv/lib/python3.8/site-packages/pandas/core/internals/construction.py", line 697, in _try_cast
    subarr = maybe_cast_to_datetime(arr, dtype)
  File "/Users/sashakaun/IdeaProjects/python/venv/lib/python3.8/site-packages/pandas/core/dtypes/cast.py", line 1067, in maybe_cast_to_datetime
    value = maybe_infer_to_datetimelike(value)
  File "/Users/sashakaun/IdeaProjects/python/venv/lib/python3.8/site-packages/pandas/core/dtypes/cast.py", line 865, in maybe_infer_to_datetimelike
    if isinstance(value, (ABCDatetimeIndex, ABCPeriodIndex,
  File "/Users/sashakaun/IdeaProjects/python/venv/lib/python3.8/site-packages/pandas/core/dtypes/generic.py", line 9, in _check
    return getattr(inst, attr, '_typ') in comp
TypeError: 'in <string>' requires string as left operand, not NoneType

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/sashakaun/IdeaProjects/scrapyscrape/test.py", line 24, in <module>
    df.insert(loc=0, column="Title", value=title, allow_duplicates=True)
  File "/Users/sashakaun/IdeaProjects/python/venv/lib/python3.8/site-packages/pandas/core/frame.py", line 3470, in insert
    self._ensure_valid_index(value)
  File "/Users/sashakaun/IdeaProjects/python/venv/lib/python3.8/site-packages/pandas/core/frame.py", line 3424, in _ensure_valid_index
    value = Series(value)
  File "/Users/sashakaun/IdeaProjects/python/venv/lib/python3.8/site-packages/pandas/core/series.py", line 261, in __init__
    data = sanitize_array(data, index, dtype, copy,
  File "/Users/sashakaun/IdeaProjects/python/venv/lib/python3.8/site-packages/pandas/core/internals/construction.py", line 625, in sanitize_array
    subarr = _try_cast(data, False, dtype, copy, raise_cast_failure)
  File "/Users/sashakaun/IdeaProjects/python/venv/lib/python3.8/site-packages/pandas/core/internals/construction.py", line 720, in _try_cast
    subarr = np.array(arr, dtype=object, copy=copy)
  File "/Users/sashakaun/IdeaProjects/python/venv/lib/python3.8/site-packages/bs4/element.py", line 971, in __getitem__
    return self.attrs[key]
KeyError: 0

its my first question, please be kind, thanks in advance

Sasha
  • 3
  • 2

1 Answers1

0

The error seems to be from your value=dateTime.group(0) value. Can you elaborate on what is structure of dateTime?

Plus, df.insert() inserts a column rather than adding the data to a dataframe.

You should first transform your data into a series object and then use df.concat() to concatenate with the original dataframe.

Below are some references:

https://kite.com/python/answers/how-to-insert-a-row-into-a-pandas-dataframe

Add one row to pandas DataFrame

https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html