0

I am trying to grab some finanical data off a financial website. I wanna manipulate df['__this value__']. I did some research myself, and I understand the error fine, but I really have no idea how to fix it. This is how my code is like:

import requests
import bs4
import os
import pandas as pd


def worker_names(code=600110):

    ......

    df = pd.DataFrame({'class': name_list})
    worker_years(df)


def worker_years(df, code=600110, years=None):

    if years is None:
        years = ['2019', '2018', '2017', '2016']
        url = 'http://quotes.money.163.com/f10/dbfx_'\
              + str(code) + '.html?date='\
              + str(years) + '-12-31,'\
              + str(years) + '-09-30#01c08'

        ......

        df['{}-12-31'.format(years)] = number_list  # this is where the problem is
        df = df.drop_duplicates(subset=['class'], keep=False)
        df.to_csv(".\\__fundamentals__\\{:0>6}.csv".format(code),
                  index=False, encoding='GBK')
        print(df)

if __name__ == '__main__':
    pd.set_option('display.max_columns', None)
    pd.set_option('display.unicode.ambiguous_as_wide', True)
    pd.set_option('display.unicode.east_asian_width', True)

    fundamental_path = '.\\__fundamentals__'
    stock_path = '.\\__stock__'

    worker_names(code=600110)

Is there any ways that I can work around? please help! THX ALL!

7
  • where did you declare you str var in your code ? and why you are using is as a function ? str(code) ... ? please give us more data about str var where you declared it ... is it a function or just an array ? Commented May 15, 2020 at 9:21
  • I declared it where years = ['2019', '2018', '2017', '2016'], I wanna name my df[col] with years, respectively. @ZINEMahmoud Commented May 15, 2020 at 9:31
  • your str(code) str(year) ... make no sense to your code ? .... what is str in your code ? that was my question ? is it a function or a var ? you can't just set it without defining it Commented May 15, 2020 at 9:40
  • 'str' is still undefined sir Commented May 15, 2020 at 9:50
  • It seems I am not quite getting it by 'str' is not defined. my coding ability is still pretty amatuer. can you give me some hints by that? @ZINEMahmoud Commented May 15, 2020 at 9:53

1 Answer 1

1

your codes df['{}-12-31'.format(years)] = number_list demostrate a very good example of you can't make 2 variables on both side of the equation. Try this:

df_year = pd.DataFrame({'{}'.format(year): number_list})
df = pd.concat([df, df_year], axis=1)

work around with dataframe, there are many different ways to get the same result.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.