'Python - adding number to a value returned by a function
Following code correctly displays number 31:
str = 15
print(str+16)
Following code also correctly prints lengths of each column in a pandas' dataframe:
df = pd.read_csv(r'C:\Test\File1.csv', low_memory=False)
for col in df:
print(col, '->', df[col].str.len().max())
But I want to add 10 (in the print(...) method). But, when I add 10 to the last line of the above code, it ignores 10 and still displays only the actual length of each column.
Question: How can we make the last line of the code above to display length of each column + 10?
For example, if lengths of column1, column2,....column150 are as follows: 5, 11, ....,45 I would like these to be displayed as: 15,21,...55:
for col in df:
print(col, '->', df[col].str.len().max()+10)
Solution 1:[1]
When I run the following:
import pandas as pd
df = pd.DataFrame(data = ['abra','njcasjdcn'],columns = ["c1"])
df["c2"] = df["c1"]*2
for col in df:
print(col, '->', df[col].str.len().max()+10)
it outputs the correct result. The only things which could be different in your code are your pandas version, IDE, python version or DataFrame.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dominic Johnston Whiteley |
