I have what appears to be a very simple JSON dict I need to convert into a Pandas dataframe. The dict is being pulled in for me as a string which I have little control over. I have tried the usual methods such as pd.read_json() and json_normalize() etc but can’t seem to get it anywhere close. Has anyone…
Tag: pandas
How to replace df.loc with df.reindex without KeyError
I have a huge dataframe which I get from a .csv file. After defining the columns I only want to use the one I need. I used Python 3.8.1 version and it worked great, although raising the “FutureWarning: Passing list-likes to .loc or [] with any missing label will raise KeyError in the future, you can use…
Python pandas printing correct dataframe
I am reading from a csv-file, and have the current values in my dataframe, where width and height is min and max value. And now i want to split and format the columns and print them: My problem is that it stills print: Whereas I want it to print: What am I doing wrong? Answer This code can help you
How to use df groupby to return counts on specific values in column across each month
I have a dataframe made up of dummy car purchases across a year which looks like: df = What I’m looking for is to get an aggregated count of each brand of car for each month in 2021, so it would look like this: df = So far I’ve used this code to group by the year, month but I
Pandas append does not work (dataframe is not getting bigger)
I am currently trying to write a code that is supposed to add mulitple dataframes into one, using the append method. However, with the code I currently use, it seems that only the first dataframe is read. I have tried locating the problem by adding a len(df) to my code and it seems to that the merged datafram…
How to add multiple columns to a dataframe based on calculations
I have a csv dataset (with > 8m rows) that I load into a dataframe. The csv has columns like: I am able to load the dataset into my dataframe, but then I need to add multiple calculated columns to the dataframe for each row. In otherwords, unlike this SO question, I do not want the rows of the new
Accessing and overwriting Multiindex df data
I’m trying to multiply all the values of the following multiindex df for which the first multiindex equals Property_2 with a scalar: I’ve tried various ways: but I am getting back nan’s in the relevant places. Answer That’s because the indices don’t match. One way to get around t…
How to obtain dataframe from grouped element after using apply
Let’s say this the dataframe: Then the goal is to produce this: The total Val1 is Y as long as one of the instances is Y. My code looks like this: This works except that cumulative has dtype object and I can only access Val1, that is, I cannot access First Name or Last Name (Although when I run print(cu…
How do you scrape a table from a website which is hosting the table data outside of the HTML?
I am trying to scrape the table data from this table URL: https://covid19criticalcare.com/pharmacies/ On my previous scrape I used the following Python packages: from bs4 import BeautifulSoup import requests import mysql.connector import pandas as pd from sqlalchemy import create_engine But this url’s H…
How fulfil empy df by FOR loop
I need to create a dataframe with two columns: variable, function based on this variable. There is an error in case of next code: What do I need to fix here? Looks like answer is easy, however it is uneasy to find it… Many thanks for your help Answer I like Vaishali’s way of approaching it. If you…