pandas

How to delete rows in a pandas dataframe?

做~自己de王妃 提交于 2021-02-05 08:12:40
问题 I have this pandas dataframe which is actually a excel spreadsheet: Unnamed: 0 Date Num Company Link ID 0 NaN 1990-11-15 131231 apple... http://www.example.com/201611141492/xellia... 290834 1 NaN 1990-10-22 1231 microsoft http://www.example.com/news/arnsno... NaN 2 NaN 2011-10-20 123 apple http://www.example.com/ator... 209384 3 NaN 2013-10-27 123 apple... http://example.com/sections/th-shots/2016/... 098 4 NaN 1990-10-26 123 google http://www.example.net/business/Drugmak... 098098 5 NaN 1990

Pandas dataframe covert wide to long multiple columns with name from column Name

拥有回忆 提交于 2021-02-05 08:12:28
问题 Consider I have a Pandas Dataframe with the following format. Date Product cost|us|2019 cost|us|2020 cost|us|2021 cost|de|2019 cost|de|2020 cost|de|2021 01/01/2020 prodA 10 12 14 12 13 15 How can we convert it into the following format? Date Product Year cost|us cost|de 01/01/2020 ProdA 2019 10 12 01/01/2020 ProdA 2020 12 13 01/01/2020 ProdA 2021 14 15 回答1: Convert non year columns to MultiIndex by DataFrame.set_index, then use str.rsplit by columns by last | , set new column nmae in

Troubleshooting“ ValueError: Index Date Invalid”?

拟墨画扇 提交于 2021-02-05 08:12:04
问题 I'm learning Python 3.7.4 using IDLE. I am receiving an error saying index date invalid. I've had success using excel files that i've formatted with dates & data in individual col's (A-C) but this is a csv file with all the data in one col separated by commas. In the format with the data formatted i would use the below... ANTM = pd.read_csv(r'C:\..\ANTM_stock'.csv', parse_dates= True, index_col=0) I'm pretty sure the parse_dates=True, index_col=0 is indicating look at the first col &

How to delete rows in a pandas dataframe?

£可爱£侵袭症+ 提交于 2021-02-05 08:11:56
问题 I have this pandas dataframe which is actually a excel spreadsheet: Unnamed: 0 Date Num Company Link ID 0 NaN 1990-11-15 131231 apple... http://www.example.com/201611141492/xellia... 290834 1 NaN 1990-10-22 1231 microsoft http://www.example.com/news/arnsno... NaN 2 NaN 2011-10-20 123 apple http://www.example.com/ator... 209384 3 NaN 2013-10-27 123 apple... http://example.com/sections/th-shots/2016/... 098 4 NaN 1990-10-26 123 google http://www.example.net/business/Drugmak... 098098 5 NaN 1990

How get all matches using str.contains in python regex?

自作多情 提交于 2021-02-05 08:10:55
问题 I have a data frame, in which I need to find all the possible matches rows which match with terms . My code is texts = ['foo abc', 'foobar xyz', 'xyz baz32', 'baz 45','fooz','bazzar','foo baz'] terms = ['foo','baz','foo baz'] # create df df = pd.DataFrame({'Match_text': texts}) #cretae pattern pat = r'\b(?:{})\b'.format('|'.join(terms)) # use str.contains to find matchs df = df[df['Match_text'].str.contains(pat)] #create pattern p = re.compile(pat) #search for pattern in the column results =

Python - Passing datetime parameters into a SQL Command

非 Y 不嫁゛ 提交于 2021-02-05 08:10:52
问题 I am trying to do something like this in Python, SQLCommand = ("Delete From %s where [Date] >= %s and [Date] <= %s", (calendar_table_name, required_starting_date, required_ending_date)) cursor.execute(SQLCommand) calendar_table_name is a string variable required_starting_date is a datetime variable required_ending_date is a datetime variable Trying this gives me an error: The first argument to execute must be a string or unicode query. Tried this and it gives me the same error: SQLCommand = (

Troubleshooting“ ValueError: Index Date Invalid”?

老子叫甜甜 提交于 2021-02-05 08:10:30
问题 I'm learning Python 3.7.4 using IDLE. I am receiving an error saying index date invalid. I've had success using excel files that i've formatted with dates & data in individual col's (A-C) but this is a csv file with all the data in one col separated by commas. In the format with the data formatted i would use the below... ANTM = pd.read_csv(r'C:\..\ANTM_stock'.csv', parse_dates= True, index_col=0) I'm pretty sure the parse_dates=True, index_col=0 is indicating look at the first col &

Converting Series to Pandas DateTime [duplicate]

末鹿安然 提交于 2021-02-05 08:09:52
问题 This question already has answers here : Python strptime parsing year without century: assume prior to this year? (2 answers) Closed 10 months ago . D = ["10Aug49","21Jan45","15Sep47","13Jun52"], convert this into pandas date, make sure that year is 1900 not 2000. So far i have this code which converts and prints the pandas date but century is 2000, i want 1900. import pandas as pd from datetime import datetime Dae = pd.Series(["10Aug49","21Jan45","15Sep47","13Jun52"]) x =[] for i in Dae: x =

Arrange two plots horizontally

谁都会走 提交于 2021-02-05 08:09:49
问题 As an exercise, I'm reproducing a plot from The Economist with matplotlib So far, I can generate a random data and produce two plots independently. I'm struggling now with putting them next to each other horizontally. import pandas as pd import matplotlib.pyplot as plt import numpy as np %matplotlib inline df1 = pd.DataFrame({"broadcast": np.random.randint(110, 150,size=8), "cable": np.random.randint(100, 250, size=8), "streaming" : np.random.randint(10, 50, size=8)}, index=pd.Series(np

Looking for specific value with if statement

你离开我真会死。 提交于 2021-02-05 08:09:36
问题 Hi I have a large dataset in excel (and it will keep growing) of products' orders. The dataset look like this Product Date Lsat24 Next24 Summary Buyer *day1* AX1 | 2/1/2019 |Checking inventory |Invoicing |The product request started today | HTN X5L | 2/1/2019 |Inventory counting |Future request planing|No operation update | null NO6 | 2/1/2019 |Request in-process |Closing request |Operation product request ended | KLW ZS5 | 2/1/2019 |Order placed |Processing request |Request started for the