I am importing a CSV file like the one below, using pandas.read_csv: Example of CSV file: The problem is that when I later on in my code try to use these values I get this error: TypeError: can’t multiply sequence by non-int of type ‘float’ The error is because the number I’m trying to use is not written with a
Tag: pandas
How to switch columns rows in a pandas dataframe
I have the following dataframe: I tried with pivot table but I get the following error: any alternative to pivot table to do this? Answer You can use df = df.T to transpose the dataframe. This switches the dataframe round so that the rows become columns. You could also use pd.DataFrame.transpose().
Give column name when read csv file pandas
This is the example of my dataset. How to push down the first column and add the names column [TIME, X, Y, and Z] on the first column. The desired output is like this: Answer I’d do it like this:
groupby weighted average and sum in pandas dataframe
I have a dataframe: I need a sum of adjusted_lots , price which is weighted average , of price and adjusted_lots , grouped by all the other columns , ie. grouped by (contract, month , year and buys) Similar solution on R was achieved by following code, using dplyr, however unable to do the same in pandas. is the same
Format y axis as percent
I have an existing plot that was created with pandas like this: The y axis is format as float and I want to change the y axis to percentages. All of the solutions I found use ax.xyz syntax and I can only place code below the line above that creates the plot (I cannot add ax=ax to the line above.)
Replacing Header with Top Row
I currently have a dataframe that looks like this: I’m looking for a way to delete the header row and make the first row the new header row, so the new dataframe would look like this: I’ve tried stuff along the lines of if ‘Unnamed’ in df.columns: then make the dataframe without the header but I don’t seem to be
Import SAS data file into python data frame
I’m working on a data set (PSID) that gives data in a SAS format (a .txt and another file containing instructions to interpret the data). I cannot find anything in Python to read this type of data. Does anyone know of a pre-existing module/script to read SAS data? Edit (added from a comment to an answer): The data is in
Sorting entire csv by frequency of occurence in one column
I have a large CSV file, which is a log of caller data. A short snippet of my file: I want to sort the entire list by the frequency of occurrence of customers so it will be like: I’ve tried groupby, but that only prints out the Company Name and the frequency but not the other columns, I also tried
Pandas read multiindexed csv with blanks
I’m struggling with properly loading a csv that has a multi lines header with blanks. The CSV looks like this: What I would like to get is: When I try to load with pd.read_csv(file, header=[0,1], sep=’,’), I end up with the following: Is there a way to get the desired result? Note: alternatively, I would accept this as a result:
Change Series inplace in DataFrame after applying function on it
I’m trying to use pandas in order to change one of my columns in-place, using simple function. After reading the whole Dataframe, I tried to apply function on one Series: And it’s working great. The only problem occurs when I try to put it back into my DataFrame: or: Throwing the following warning: Of Course, I can set the DataFrame