[PYTHON] [Pandas] Expand the character string to DataFrame

No.1 pandas recipe collection that can reach the itchy place

Itchy

I want to manage an array / DataFrame in which "category name (index)" and "value" are one element as a character string. Let's assign a value to a column whose column name is each category (index).

ID Fucking big string
0 A 0, B 1, C 2
1 B 3, D 4
2 A 5, C 6
3 C 7, D 8

↑ I want to ↓ this ↑ fucking big character string ↓.

ID A B C D
0 0 1 2 0
1 0 3 0 4
2 5 0 6 0
3 0 0 7 8

recipe

Each category is known. If you don't know, make a list of categories.

str_to_dataframe.py


import numpy as np
import pandas as pd

def str_to_dict(x):
    dic = {'A' : 0, 'B' : 0, 'C' : 0, 'D' : 0}
    for xx in x.split(","):
        xxs = xx.split(" ")
        dic[xxs[-2]] = int(xxs[-1])
    return dic

temp = df["Fucking big string"].apply(lambda x : pd.Series(str_to_dataframe(x)))
df = pd.concat((df, temp), axis=1)

This is fast!

Recommended Posts

[Pandas] Expand the character string to DataFrame
Convert pandas dataframe elements to regular string type
[Python] How to expand variables in a character string
Export pandas dataframe to excel
Put the lists together in pandas to make a DataFrame
Create a pandas Dataframe from a string.
[Introduction to Python] How to split a character string with the split function
How to find the memory address of a Pandas dataframe value
How to extract the desired character string from a line 4 commands
[Python] How to invert a character string
How to reassign index in pandas dataframe
[Python] Add total rows to Pandas DataFrame
[PowerShell] Get the reading of the character string
Is there NaN in the pandas DataFrame?
[Introduction to Python] How to write a character string with the format function
[Introduction to Python] Thorough explanation of the character string type used in Python!
[python] Create table from pandas DataFrame to postgres
Character range / character string range
Python version (PHP to Python) that deletes the subsequent character string from the specified character string (extension)
[Python] Sort the table by sort_values (pandas DataFrame)
I tried to generate a random character string
[Ansible] Example of playbook that adds a character string to the first line of the file
Software that saves the set character string to the clipboard when you click the [Python] button
Get the variable name of the variable as a character string.
Check if the expected column exists in Pandas DataFrame
# Function that returns the character code of a string
Convert comma-separated numeric strings to numbers in Pandas DataFrame
I want to split a character string with hiragana
I tried to summarize the string operations of Python
3D plot Pandas DataFrame
Various character string operations
Convert 202003 to 2020-03 with pandas
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
The road to Pythonista
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.
The road to Djangoist
Python application: Pandas # 3: Dataframe
[Python3] Format the character string using the variable name as the key.
I was in trouble because the character string in the PDF was strange
What I did to speed up the string search task
Convert from Pandas DataFrame to System.Data.DataTable using Python for .NET
Check what line caused the error with apply () (dataframe, Pandas)
Calculate the time difference between two columns with Pandas DataFrame
Remove double-byte spaces before and after the character string (python)
[Pandas] If the first row data is in the header in DataFrame
I tried to summarize the code often used in Pandas
A handy function to add a column anywhere in a Pandas DataFrame
I want to convert an ISO-8601 character string to Japan time
Find out how many each character is in the string.
Python learning basics ~ How to output (display) a character string? ~
How to set the extended iso8601 format date to the Dataframe index
In Django, how to abbreviate the long displayed string as ....
The minimum methods to remember when aggregating data in Pandas
How to connect the contents of a list into a string
Summary of character string format in Python3 Whether to live with the old model or the new model
I want to extract an arbitrary URL from the character string of the html source with python